blob: 939c8724feb4562f23e9344fae6e5af06140d9ca [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>
Paolo Bonziniecb586b2018-02-22 16:43:17 +010054#include <asm/microcode.h>
David Woodhouse117cc7a2018-01-12 11:11:27 +000055#include <asm/nospec-branch.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
124#define MSR_BITMAP_MODE_LM 4
125
Haozhong Zhang64903d62015-10-20 15:39:09 +0800126#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127
Yunhong Jiang64672c92016-06-13 14:19:59 -0700128/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
129static int __read_mostly cpu_preemption_timer_multi;
130static bool __read_mostly enable_preemption_timer = 1;
131#ifdef CONFIG_X86_64
132module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133#endif
134
Gleb Natapov50378782013-02-04 16:00:28 +0200135#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800136#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
137#define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
139 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200140#define KVM_CR4_GUEST_OWNED_BITS \
141 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800142 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200143
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800144#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200145#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
146#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147
Avi Kivity78ac8b42010-04-08 18:19:35 +0300148#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149
Jan Kiszkaf41245002014-03-07 20:03:13 +0100150#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
151
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800152/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300153 * Hyper-V requires all of these, so mark them as supported even though
154 * they are just treated the same as all-context.
155 */
156#define VMX_VPID_EXTENT_SUPPORTED_MASK \
157 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
158 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
160 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
161
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800162/*
163 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
164 * ple_gap: upper bound on the amount of time between two successive
165 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500166 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800167 * ple_window: upper bound on the amount of time a guest is allowed to execute
168 * in a PAUSE loop. Tests indicate that most spinlocks are held for
169 * less than 2^12 cycles
170 * Time is measured based on a counter that runs at the same rate as the TSC,
171 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400173static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800174
Babu Moger7fbc85a2018-03-16 16:37:22 -0400175static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
176module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800177
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200178/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400179static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400180module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200181
182/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400183static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200185
186/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400187static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
191
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700192struct kvm_vmx {
193 struct kvm kvm;
194
195 unsigned int tss_addr;
196 bool ept_identity_pagetable_done;
197 gpa_t ept_identity_map_addr;
198};
199
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200200#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300201
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400202struct vmcs {
203 u32 revision_id;
204 u32 abort;
205 char data[0];
206};
207
Nadav Har'Eld462b812011-05-24 15:26:10 +0300208/*
209 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
210 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
211 * loaded on this CPU (so we can clear them if the CPU goes down).
212 */
213struct loaded_vmcs {
214 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700215 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300216 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200217 bool launched;
218 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200219 unsigned long vmcs_host_cr3; /* May not match real cr3 */
220 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100221 /* Support for vnmi-less CPUs */
222 int soft_vnmi_blocked;
223 ktime_t entry_time;
224 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100225 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300226 struct list_head loaded_vmcss_on_cpu_link;
227};
228
Avi Kivity26bb0982009-09-07 11:14:12 +0300229struct shared_msr_entry {
230 unsigned index;
231 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200232 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300233};
234
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300235/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300236 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
237 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
238 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
239 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
240 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
241 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600242 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300243 * underlying hardware which will be used to run L2.
244 * This structure is packed to ensure that its layout is identical across
245 * machines (necessary for live migration).
246 * If there are changes in this struct, VMCS12_REVISION must be changed.
247 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300248typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300249struct __packed vmcs12 {
250 /* According to the Intel spec, a VMCS region must start with the
251 * following two fields. Then follow implementation-specific data.
252 */
253 u32 revision_id;
254 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300255
Nadav Har'El27d6c862011-05-25 23:06:59 +0300256 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
257 u32 padding[7]; /* room for future expansion */
258
Nadav Har'El22bd0352011-05-25 23:05:57 +0300259 u64 io_bitmap_a;
260 u64 io_bitmap_b;
261 u64 msr_bitmap;
262 u64 vm_exit_msr_store_addr;
263 u64 vm_exit_msr_load_addr;
264 u64 vm_entry_msr_load_addr;
265 u64 tsc_offset;
266 u64 virtual_apic_page_addr;
267 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800268 u64 posted_intr_desc_addr;
Bandan Das27c42a12017-08-03 15:54:42 -0400269 u64 vm_function_control;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300270 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800271 u64 eoi_exit_bitmap0;
272 u64 eoi_exit_bitmap1;
273 u64 eoi_exit_bitmap2;
274 u64 eoi_exit_bitmap3;
Bandan Das41ab9372017-08-03 15:54:43 -0400275 u64 eptp_list_address;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800276 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300277 u64 guest_physical_address;
278 u64 vmcs_link_pointer;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400279 u64 pml_address;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300280 u64 guest_ia32_debugctl;
281 u64 guest_ia32_pat;
282 u64 guest_ia32_efer;
283 u64 guest_ia32_perf_global_ctrl;
284 u64 guest_pdptr0;
285 u64 guest_pdptr1;
286 u64 guest_pdptr2;
287 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100288 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300289 u64 host_ia32_pat;
290 u64 host_ia32_efer;
291 u64 host_ia32_perf_global_ctrl;
292 u64 padding64[8]; /* room for future expansion */
293 /*
294 * To allow migration of L1 (complete with its L2 guests) between
295 * machines of different natural widths (32 or 64 bit), we cannot have
296 * unsigned long fields with no explict size. We use u64 (aliased
297 * natural_width) instead. Luckily, x86 is little-endian.
298 */
299 natural_width cr0_guest_host_mask;
300 natural_width cr4_guest_host_mask;
301 natural_width cr0_read_shadow;
302 natural_width cr4_read_shadow;
303 natural_width cr3_target_value0;
304 natural_width cr3_target_value1;
305 natural_width cr3_target_value2;
306 natural_width cr3_target_value3;
307 natural_width exit_qualification;
308 natural_width guest_linear_address;
309 natural_width guest_cr0;
310 natural_width guest_cr3;
311 natural_width guest_cr4;
312 natural_width guest_es_base;
313 natural_width guest_cs_base;
314 natural_width guest_ss_base;
315 natural_width guest_ds_base;
316 natural_width guest_fs_base;
317 natural_width guest_gs_base;
318 natural_width guest_ldtr_base;
319 natural_width guest_tr_base;
320 natural_width guest_gdtr_base;
321 natural_width guest_idtr_base;
322 natural_width guest_dr7;
323 natural_width guest_rsp;
324 natural_width guest_rip;
325 natural_width guest_rflags;
326 natural_width guest_pending_dbg_exceptions;
327 natural_width guest_sysenter_esp;
328 natural_width guest_sysenter_eip;
329 natural_width host_cr0;
330 natural_width host_cr3;
331 natural_width host_cr4;
332 natural_width host_fs_base;
333 natural_width host_gs_base;
334 natural_width host_tr_base;
335 natural_width host_gdtr_base;
336 natural_width host_idtr_base;
337 natural_width host_ia32_sysenter_esp;
338 natural_width host_ia32_sysenter_eip;
339 natural_width host_rsp;
340 natural_width host_rip;
341 natural_width paddingl[8]; /* room for future expansion */
342 u32 pin_based_vm_exec_control;
343 u32 cpu_based_vm_exec_control;
344 u32 exception_bitmap;
345 u32 page_fault_error_code_mask;
346 u32 page_fault_error_code_match;
347 u32 cr3_target_count;
348 u32 vm_exit_controls;
349 u32 vm_exit_msr_store_count;
350 u32 vm_exit_msr_load_count;
351 u32 vm_entry_controls;
352 u32 vm_entry_msr_load_count;
353 u32 vm_entry_intr_info_field;
354 u32 vm_entry_exception_error_code;
355 u32 vm_entry_instruction_len;
356 u32 tpr_threshold;
357 u32 secondary_vm_exec_control;
358 u32 vm_instruction_error;
359 u32 vm_exit_reason;
360 u32 vm_exit_intr_info;
361 u32 vm_exit_intr_error_code;
362 u32 idt_vectoring_info_field;
363 u32 idt_vectoring_error_code;
364 u32 vm_exit_instruction_len;
365 u32 vmx_instruction_info;
366 u32 guest_es_limit;
367 u32 guest_cs_limit;
368 u32 guest_ss_limit;
369 u32 guest_ds_limit;
370 u32 guest_fs_limit;
371 u32 guest_gs_limit;
372 u32 guest_ldtr_limit;
373 u32 guest_tr_limit;
374 u32 guest_gdtr_limit;
375 u32 guest_idtr_limit;
376 u32 guest_es_ar_bytes;
377 u32 guest_cs_ar_bytes;
378 u32 guest_ss_ar_bytes;
379 u32 guest_ds_ar_bytes;
380 u32 guest_fs_ar_bytes;
381 u32 guest_gs_ar_bytes;
382 u32 guest_ldtr_ar_bytes;
383 u32 guest_tr_ar_bytes;
384 u32 guest_interruptibility_info;
385 u32 guest_activity_state;
386 u32 guest_sysenter_cs;
387 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100388 u32 vmx_preemption_timer_value;
389 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300390 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800391 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300392 u16 guest_es_selector;
393 u16 guest_cs_selector;
394 u16 guest_ss_selector;
395 u16 guest_ds_selector;
396 u16 guest_fs_selector;
397 u16 guest_gs_selector;
398 u16 guest_ldtr_selector;
399 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800400 u16 guest_intr_status;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400401 u16 guest_pml_index;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300402 u16 host_es_selector;
403 u16 host_cs_selector;
404 u16 host_ss_selector;
405 u16 host_ds_selector;
406 u16 host_fs_selector;
407 u16 host_gs_selector;
408 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300409};
410
411/*
412 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
413 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
414 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
415 */
416#define VMCS12_REVISION 0x11e57ed0
417
418/*
419 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
420 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
421 * current implementation, 4K are reserved to avoid future complications.
422 */
423#define VMCS12_SIZE 0x1000
424
425/*
Jim Mattson5b157062017-12-22 12:11:12 -0800426 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
427 * supported VMCS12 field encoding.
428 */
429#define VMCS12_MAX_FIELD_INDEX 0x17
430
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100431struct nested_vmx_msrs {
432 /*
433 * We only store the "true" versions of the VMX capability MSRs. We
434 * generate the "non-true" versions by setting the must-be-1 bits
435 * according to the SDM.
436 */
437 u32 procbased_ctls_low;
438 u32 procbased_ctls_high;
439 u32 secondary_ctls_low;
440 u32 secondary_ctls_high;
441 u32 pinbased_ctls_low;
442 u32 pinbased_ctls_high;
443 u32 exit_ctls_low;
444 u32 exit_ctls_high;
445 u32 entry_ctls_low;
446 u32 entry_ctls_high;
447 u32 misc_low;
448 u32 misc_high;
449 u32 ept_caps;
450 u32 vpid_caps;
451 u64 basic;
452 u64 cr0_fixed0;
453 u64 cr0_fixed1;
454 u64 cr4_fixed0;
455 u64 cr4_fixed1;
456 u64 vmcs_enum;
457 u64 vmfunc_controls;
458};
459
Jim Mattson5b157062017-12-22 12:11:12 -0800460/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300461 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
462 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
463 */
464struct nested_vmx {
465 /* Has the level1 guest done vmxon? */
466 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400467 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400468 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300469
470 /* The guest-physical address of the current VMCS L1 keeps for L2 */
471 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700472 /*
473 * Cache of the guest's VMCS, existing outside of guest memory.
474 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700475 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700476 */
477 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300478 /*
479 * Indicates if the shadow vmcs must be updated with the
480 * data hold by vmcs12
481 */
482 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100483 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300484
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200485 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300486 /* L2 must run next, and mustn't decide to exit to L1. */
487 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600488
489 struct loaded_vmcs vmcs02;
490
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300491 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600492 * Guest pages referred to in the vmcs02 with host-physical
493 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300494 */
495 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800496 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800497 struct page *pi_desc_page;
498 struct pi_desc *pi_desc;
499 bool pi_pending;
500 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100501
502 struct hrtimer preemption_timer;
503 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200504
505 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
506 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800507
Wanpeng Li5c614b32015-10-13 09:18:36 -0700508 u16 vpid02;
509 u16 last_vpid;
510
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100511 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200512
513 /* SMM related state */
514 struct {
515 /* in VMX operation on SMM entry? */
516 bool vmxon;
517 /* in guest mode on SMM entry? */
518 bool guest_mode;
519 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300520};
521
Yang Zhang01e439b2013-04-11 19:25:12 +0800522#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800523#define POSTED_INTR_SN 1
524
Yang Zhang01e439b2013-04-11 19:25:12 +0800525/* Posted-Interrupt Descriptor */
526struct pi_desc {
527 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800528 union {
529 struct {
530 /* bit 256 - Outstanding Notification */
531 u16 on : 1,
532 /* bit 257 - Suppress Notification */
533 sn : 1,
534 /* bit 271:258 - Reserved */
535 rsvd_1 : 14;
536 /* bit 279:272 - Notification Vector */
537 u8 nv;
538 /* bit 287:280 - Reserved */
539 u8 rsvd_2;
540 /* bit 319:288 - Notification Destination */
541 u32 ndst;
542 };
543 u64 control;
544 };
545 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800546} __aligned(64);
547
Yang Zhanga20ed542013-04-11 19:25:15 +0800548static bool pi_test_and_set_on(struct pi_desc *pi_desc)
549{
550 return test_and_set_bit(POSTED_INTR_ON,
551 (unsigned long *)&pi_desc->control);
552}
553
554static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
555{
556 return test_and_clear_bit(POSTED_INTR_ON,
557 (unsigned long *)&pi_desc->control);
558}
559
560static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
561{
562 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
563}
564
Feng Wuebbfc762015-09-18 22:29:46 +0800565static inline void pi_clear_sn(struct pi_desc *pi_desc)
566{
567 return clear_bit(POSTED_INTR_SN,
568 (unsigned long *)&pi_desc->control);
569}
570
571static inline void pi_set_sn(struct pi_desc *pi_desc)
572{
573 return set_bit(POSTED_INTR_SN,
574 (unsigned long *)&pi_desc->control);
575}
576
Paolo Bonziniad361092016-09-20 16:15:05 +0200577static inline void pi_clear_on(struct pi_desc *pi_desc)
578{
579 clear_bit(POSTED_INTR_ON,
580 (unsigned long *)&pi_desc->control);
581}
582
Feng Wuebbfc762015-09-18 22:29:46 +0800583static inline int pi_test_on(struct pi_desc *pi_desc)
584{
585 return test_bit(POSTED_INTR_ON,
586 (unsigned long *)&pi_desc->control);
587}
588
589static inline int pi_test_sn(struct pi_desc *pi_desc)
590{
591 return test_bit(POSTED_INTR_SN,
592 (unsigned long *)&pi_desc->control);
593}
594
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400595struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000596 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300597 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300598 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100599 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300600 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200601 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200602 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300603 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400604 int nmsrs;
605 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800606 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400607#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300608 u64 msr_host_kernel_gs_base;
609 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400610#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100611
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100612 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100613 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100614
Gleb Natapov2961e8762013-11-25 15:37:13 +0200615 u32 vm_entry_controls_shadow;
616 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200617 u32 secondary_exec_control;
618
Nadav Har'Eld462b812011-05-24 15:26:10 +0300619 /*
620 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
621 * non-nested (L1) guest, it always points to vmcs01. For a nested
622 * guest (L2), it points to a different VMCS.
623 */
624 struct loaded_vmcs vmcs01;
625 struct loaded_vmcs *loaded_vmcs;
626 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300627 struct msr_autoload {
628 unsigned nr;
629 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
630 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
631 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400632 struct {
633 int loaded;
634 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300635#ifdef CONFIG_X86_64
636 u16 ds_sel, es_sel;
637#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200638 int gs_ldt_reload_needed;
639 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000640 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400641 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200642 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300643 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300644 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300645 struct kvm_segment segs[8];
646 } rmode;
647 struct {
648 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300649 struct kvm_save_segment {
650 u16 selector;
651 unsigned long base;
652 u32 limit;
653 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300654 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300655 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800656 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300657 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200658
Andi Kleena0861c02009-06-08 17:37:09 +0800659 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800660
Yang Zhang01e439b2013-04-11 19:25:12 +0800661 /* Posted interrupt descriptor */
662 struct pi_desc pi_desc;
663
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300664 /* Support for a guest hypervisor (nested VMX) */
665 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200666
667 /* Dynamic PLE window. */
668 int ple_window;
669 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800670
671 /* Support for PML */
672#define PML_ENTITY_NUM 512
673 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800674
Yunhong Jiang64672c92016-06-13 14:19:59 -0700675 /* apic deadline value in host tsc */
676 u64 hv_deadline_tsc;
677
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800678 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800679
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800680 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800681
Wanpeng Li74c55932017-11-29 01:31:20 -0800682 unsigned long host_debugctlmsr;
683
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800684 /*
685 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
686 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
687 * in msr_ia32_feature_control_valid_bits.
688 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800689 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800690 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400691};
692
Avi Kivity2fb92db2011-04-27 19:42:18 +0300693enum segment_cache_field {
694 SEG_FIELD_SEL = 0,
695 SEG_FIELD_BASE = 1,
696 SEG_FIELD_LIMIT = 2,
697 SEG_FIELD_AR = 3,
698
699 SEG_FIELD_NR = 4
700};
701
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700702static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
703{
704 return container_of(kvm, struct kvm_vmx, kvm);
705}
706
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400707static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
708{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000709 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400710}
711
Feng Wuefc64402015-09-18 22:29:51 +0800712static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
713{
714 return &(to_vmx(vcpu)->pi_desc);
715}
716
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800717#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300718#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800719#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
720#define FIELD64(number, name) \
721 FIELD(number, name), \
722 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300723
Abel Gordon4607c2d2013-04-18 14:35:55 +0300724
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100725static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100726#define SHADOW_FIELD_RO(x) x,
727#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300728};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400729static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300730 ARRAY_SIZE(shadow_read_only_fields);
731
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100732static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100733#define SHADOW_FIELD_RW(x) x,
734#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300735};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400736static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300737 ARRAY_SIZE(shadow_read_write_fields);
738
Mathias Krause772e0312012-08-30 01:30:19 +0200739static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300740 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800741 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300742 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
743 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
744 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
745 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
746 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
747 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
748 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
749 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800750 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400751 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300752 FIELD(HOST_ES_SELECTOR, host_es_selector),
753 FIELD(HOST_CS_SELECTOR, host_cs_selector),
754 FIELD(HOST_SS_SELECTOR, host_ss_selector),
755 FIELD(HOST_DS_SELECTOR, host_ds_selector),
756 FIELD(HOST_FS_SELECTOR, host_fs_selector),
757 FIELD(HOST_GS_SELECTOR, host_gs_selector),
758 FIELD(HOST_TR_SELECTOR, host_tr_selector),
759 FIELD64(IO_BITMAP_A, io_bitmap_a),
760 FIELD64(IO_BITMAP_B, io_bitmap_b),
761 FIELD64(MSR_BITMAP, msr_bitmap),
762 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
763 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
764 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
765 FIELD64(TSC_OFFSET, tsc_offset),
766 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
767 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800768 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400769 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300770 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800771 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
772 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
773 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
774 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400775 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800776 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300777 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
778 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400779 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300780 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
781 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
782 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
783 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
784 FIELD64(GUEST_PDPTR0, guest_pdptr0),
785 FIELD64(GUEST_PDPTR1, guest_pdptr1),
786 FIELD64(GUEST_PDPTR2, guest_pdptr2),
787 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100788 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300789 FIELD64(HOST_IA32_PAT, host_ia32_pat),
790 FIELD64(HOST_IA32_EFER, host_ia32_efer),
791 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
792 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
793 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
794 FIELD(EXCEPTION_BITMAP, exception_bitmap),
795 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
796 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
797 FIELD(CR3_TARGET_COUNT, cr3_target_count),
798 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
799 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
800 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
801 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
802 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
803 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
804 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
805 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
806 FIELD(TPR_THRESHOLD, tpr_threshold),
807 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
808 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
809 FIELD(VM_EXIT_REASON, vm_exit_reason),
810 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
811 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
812 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
813 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
814 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
815 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
816 FIELD(GUEST_ES_LIMIT, guest_es_limit),
817 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
818 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
819 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
820 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
821 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
822 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
823 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
824 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
825 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
826 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
827 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
828 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
829 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
830 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
831 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
832 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
833 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
834 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
835 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
836 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
837 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100838 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300839 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
840 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
841 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
842 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
843 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
844 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
845 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
846 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
847 FIELD(EXIT_QUALIFICATION, exit_qualification),
848 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
849 FIELD(GUEST_CR0, guest_cr0),
850 FIELD(GUEST_CR3, guest_cr3),
851 FIELD(GUEST_CR4, guest_cr4),
852 FIELD(GUEST_ES_BASE, guest_es_base),
853 FIELD(GUEST_CS_BASE, guest_cs_base),
854 FIELD(GUEST_SS_BASE, guest_ss_base),
855 FIELD(GUEST_DS_BASE, guest_ds_base),
856 FIELD(GUEST_FS_BASE, guest_fs_base),
857 FIELD(GUEST_GS_BASE, guest_gs_base),
858 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
859 FIELD(GUEST_TR_BASE, guest_tr_base),
860 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
861 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
862 FIELD(GUEST_DR7, guest_dr7),
863 FIELD(GUEST_RSP, guest_rsp),
864 FIELD(GUEST_RIP, guest_rip),
865 FIELD(GUEST_RFLAGS, guest_rflags),
866 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
867 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
868 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
869 FIELD(HOST_CR0, host_cr0),
870 FIELD(HOST_CR3, host_cr3),
871 FIELD(HOST_CR4, host_cr4),
872 FIELD(HOST_FS_BASE, host_fs_base),
873 FIELD(HOST_GS_BASE, host_gs_base),
874 FIELD(HOST_TR_BASE, host_tr_base),
875 FIELD(HOST_GDTR_BASE, host_gdtr_base),
876 FIELD(HOST_IDTR_BASE, host_idtr_base),
877 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
878 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
879 FIELD(HOST_RSP, host_rsp),
880 FIELD(HOST_RIP, host_rip),
881};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300882
883static inline short vmcs_field_to_offset(unsigned long field)
884{
Dan Williams085331d2018-01-31 17:47:03 -0800885 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
886 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800887 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100888
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800889 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -0800890 return -ENOENT;
891
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800892 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -0800893 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -0800894 return -ENOENT;
895
Linus Torvalds15303ba2018-02-10 13:16:35 -0800896 index = array_index_nospec(index, size);
897 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -0800898 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100899 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -0800900 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300901}
902
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300903static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
904{
David Matlack4f2777b2016-07-13 17:16:37 -0700905 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300906}
907
Peter Feiner995f00a2017-06-30 17:26:32 -0700908static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300909static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -0700910static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800911static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300912static void vmx_set_segment(struct kvm_vcpu *vcpu,
913 struct kvm_segment *var, int seg);
914static void vmx_get_segment(struct kvm_vcpu *vcpu,
915 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200916static bool guest_state_valid(struct kvm_vcpu *vcpu);
917static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +0300918static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +0200919static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
920static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
921static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
922 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100923static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +0100924static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
925 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300926
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927static DEFINE_PER_CPU(struct vmcs *, vmxarea);
928static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300929/*
930 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
931 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
932 */
933static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934
Feng Wubf9f6ac2015-09-18 22:29:55 +0800935/*
936 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
937 * can find which vCPU should be waken up.
938 */
939static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
940static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
941
Radim Krčmář23611332016-09-29 22:41:33 +0200942enum {
Radim Krčmář23611332016-09-29 22:41:33 +0200943 VMX_VMREAD_BITMAP,
944 VMX_VMWRITE_BITMAP,
945 VMX_BITMAP_NR
946};
947
948static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
949
Radim Krčmář23611332016-09-29 22:41:33 +0200950#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
951#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300952
Avi Kivity110312c2010-12-21 12:54:20 +0200953static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200954static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200955
Sheng Yang2384d2b2008-01-17 15:14:33 +0800956static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
957static DEFINE_SPINLOCK(vmx_vpid_lock);
958
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300959static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 int size;
961 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300962 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300964 u32 pin_based_exec_ctrl;
965 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800966 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300967 u32 vmexit_ctrl;
968 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +0100969 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300970} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
Hannes Ederefff9e52008-11-28 17:02:06 +0100972static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800973 u32 ept;
974 u32 vpid;
975} vmx_capability;
976
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977#define VMX_SEGMENT_FIELD(seg) \
978 [VCPU_SREG_##seg] = { \
979 .selector = GUEST_##seg##_SELECTOR, \
980 .base = GUEST_##seg##_BASE, \
981 .limit = GUEST_##seg##_LIMIT, \
982 .ar_bytes = GUEST_##seg##_AR_BYTES, \
983 }
984
Mathias Krause772e0312012-08-30 01:30:19 +0200985static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 unsigned selector;
987 unsigned base;
988 unsigned limit;
989 unsigned ar_bytes;
990} kvm_vmx_segment_fields[] = {
991 VMX_SEGMENT_FIELD(CS),
992 VMX_SEGMENT_FIELD(DS),
993 VMX_SEGMENT_FIELD(ES),
994 VMX_SEGMENT_FIELD(FS),
995 VMX_SEGMENT_FIELD(GS),
996 VMX_SEGMENT_FIELD(SS),
997 VMX_SEGMENT_FIELD(TR),
998 VMX_SEGMENT_FIELD(LDTR),
999};
1000
Avi Kivity26bb0982009-09-07 11:14:12 +03001001static u64 host_efer;
1002
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001003static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1004
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001005/*
Brian Gerst8c065852010-07-17 09:03:26 -04001006 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001007 * away by decrementing the array size.
1008 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001010#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001011 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001013 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001016DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1017
1018#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1019
1020#define KVM_EVMCS_VERSION 1
1021
1022#if IS_ENABLED(CONFIG_HYPERV)
1023static bool __read_mostly enlightened_vmcs = true;
1024module_param(enlightened_vmcs, bool, 0444);
1025
1026static inline void evmcs_write64(unsigned long field, u64 value)
1027{
1028 u16 clean_field;
1029 int offset = get_evmcs_offset(field, &clean_field);
1030
1031 if (offset < 0)
1032 return;
1033
1034 *(u64 *)((char *)current_evmcs + offset) = value;
1035
1036 current_evmcs->hv_clean_fields &= ~clean_field;
1037}
1038
1039static inline void evmcs_write32(unsigned long field, u32 value)
1040{
1041 u16 clean_field;
1042 int offset = get_evmcs_offset(field, &clean_field);
1043
1044 if (offset < 0)
1045 return;
1046
1047 *(u32 *)((char *)current_evmcs + offset) = value;
1048 current_evmcs->hv_clean_fields &= ~clean_field;
1049}
1050
1051static inline void evmcs_write16(unsigned long field, u16 value)
1052{
1053 u16 clean_field;
1054 int offset = get_evmcs_offset(field, &clean_field);
1055
1056 if (offset < 0)
1057 return;
1058
1059 *(u16 *)((char *)current_evmcs + offset) = value;
1060 current_evmcs->hv_clean_fields &= ~clean_field;
1061}
1062
1063static inline u64 evmcs_read64(unsigned long field)
1064{
1065 int offset = get_evmcs_offset(field, NULL);
1066
1067 if (offset < 0)
1068 return 0;
1069
1070 return *(u64 *)((char *)current_evmcs + offset);
1071}
1072
1073static inline u32 evmcs_read32(unsigned long field)
1074{
1075 int offset = get_evmcs_offset(field, NULL);
1076
1077 if (offset < 0)
1078 return 0;
1079
1080 return *(u32 *)((char *)current_evmcs + offset);
1081}
1082
1083static inline u16 evmcs_read16(unsigned long field)
1084{
1085 int offset = get_evmcs_offset(field, NULL);
1086
1087 if (offset < 0)
1088 return 0;
1089
1090 return *(u16 *)((char *)current_evmcs + offset);
1091}
1092
1093static void evmcs_load(u64 phys_addr)
1094{
1095 struct hv_vp_assist_page *vp_ap =
1096 hv_get_vp_assist_page(smp_processor_id());
1097
1098 vp_ap->current_nested_vmcs = phys_addr;
1099 vp_ap->enlighten_vmentry = 1;
1100}
1101
1102static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1103{
1104 /*
1105 * Enlightened VMCSv1 doesn't support these:
1106 *
1107 * POSTED_INTR_NV = 0x00000002,
1108 * GUEST_INTR_STATUS = 0x00000810,
1109 * APIC_ACCESS_ADDR = 0x00002014,
1110 * POSTED_INTR_DESC_ADDR = 0x00002016,
1111 * EOI_EXIT_BITMAP0 = 0x0000201c,
1112 * EOI_EXIT_BITMAP1 = 0x0000201e,
1113 * EOI_EXIT_BITMAP2 = 0x00002020,
1114 * EOI_EXIT_BITMAP3 = 0x00002022,
1115 */
1116 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1117 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1118 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1119 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1120 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1121 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1122 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1123
1124 /*
1125 * GUEST_PML_INDEX = 0x00000812,
1126 * PML_ADDRESS = 0x0000200e,
1127 */
1128 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1129
1130 /* VM_FUNCTION_CONTROL = 0x00002018, */
1131 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1132
1133 /*
1134 * EPTP_LIST_ADDRESS = 0x00002024,
1135 * VMREAD_BITMAP = 0x00002026,
1136 * VMWRITE_BITMAP = 0x00002028,
1137 */
1138 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1139
1140 /*
1141 * TSC_MULTIPLIER = 0x00002032,
1142 */
1143 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1144
1145 /*
1146 * PLE_GAP = 0x00004020,
1147 * PLE_WINDOW = 0x00004022,
1148 */
1149 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1150
1151 /*
1152 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1153 */
1154 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1155
1156 /*
1157 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1158 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1159 */
1160 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1161 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1162
1163 /*
1164 * Currently unsupported in KVM:
1165 * GUEST_IA32_RTIT_CTL = 0x00002814,
1166 */
1167}
1168#else /* !IS_ENABLED(CONFIG_HYPERV) */
1169static inline void evmcs_write64(unsigned long field, u64 value) {}
1170static inline void evmcs_write32(unsigned long field, u32 value) {}
1171static inline void evmcs_write16(unsigned long field, u16 value) {}
1172static inline u64 evmcs_read64(unsigned long field) { return 0; }
1173static inline u32 evmcs_read32(unsigned long field) { return 0; }
1174static inline u16 evmcs_read16(unsigned long field) { return 0; }
1175static inline void evmcs_load(u64 phys_addr) {}
1176static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
1177#endif /* IS_ENABLED(CONFIG_HYPERV) */
1178
Jan Kiszka5bb16012016-02-09 20:14:21 +01001179static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180{
1181 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1182 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001183 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1184}
1185
Jan Kiszka6f054852016-02-09 20:15:18 +01001186static inline bool is_debug(u32 intr_info)
1187{
1188 return is_exception_n(intr_info, DB_VECTOR);
1189}
1190
1191static inline bool is_breakpoint(u32 intr_info)
1192{
1193 return is_exception_n(intr_info, BP_VECTOR);
1194}
1195
Jan Kiszka5bb16012016-02-09 20:14:21 +01001196static inline bool is_page_fault(u32 intr_info)
1197{
1198 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199}
1200
Gui Jianfeng31299942010-03-15 17:29:09 +08001201static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001202{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001203 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001204}
1205
Gui Jianfeng31299942010-03-15 17:29:09 +08001206static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001207{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001208 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001209}
1210
Liran Alon9e869482018-03-12 13:12:51 +02001211static inline bool is_gp_fault(u32 intr_info)
1212{
1213 return is_exception_n(intr_info, GP_VECTOR);
1214}
1215
Gui Jianfeng31299942010-03-15 17:29:09 +08001216static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217{
1218 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1219 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1220}
1221
Gui Jianfeng31299942010-03-15 17:29:09 +08001222static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001223{
1224 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1225 INTR_INFO_VALID_MASK)) ==
1226 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1227}
1228
Gui Jianfeng31299942010-03-15 17:29:09 +08001229static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001230{
Sheng Yang04547152009-04-01 15:52:31 +08001231 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001232}
1233
Gui Jianfeng31299942010-03-15 17:29:09 +08001234static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001235{
Sheng Yang04547152009-04-01 15:52:31 +08001236 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001237}
1238
Paolo Bonzini35754c92015-07-29 12:05:37 +02001239static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001240{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001241 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001242}
1243
Gui Jianfeng31299942010-03-15 17:29:09 +08001244static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001245{
Sheng Yang04547152009-04-01 15:52:31 +08001246 return vmcs_config.cpu_based_exec_ctrl &
1247 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001248}
1249
Avi Kivity774ead32007-12-26 13:57:04 +02001250static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001251{
Sheng Yang04547152009-04-01 15:52:31 +08001252 return vmcs_config.cpu_based_2nd_exec_ctrl &
1253 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1254}
1255
Yang Zhang8d146952013-01-25 10:18:50 +08001256static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1257{
1258 return vmcs_config.cpu_based_2nd_exec_ctrl &
1259 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1260}
1261
Yang Zhang83d4c282013-01-25 10:18:49 +08001262static inline bool cpu_has_vmx_apic_register_virt(void)
1263{
1264 return vmcs_config.cpu_based_2nd_exec_ctrl &
1265 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1266}
1267
Yang Zhangc7c9c562013-01-25 10:18:51 +08001268static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1269{
1270 return vmcs_config.cpu_based_2nd_exec_ctrl &
1271 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1272}
1273
Yunhong Jiang64672c92016-06-13 14:19:59 -07001274/*
1275 * Comment's format: document - errata name - stepping - processor name.
1276 * Refer from
1277 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1278 */
1279static u32 vmx_preemption_cpu_tfms[] = {
1280/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
12810x000206E6,
1282/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1283/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1284/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
12850x00020652,
1286/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
12870x00020655,
1288/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1289/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1290/*
1291 * 320767.pdf - AAP86 - B1 -
1292 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1293 */
12940x000106E5,
1295/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
12960x000106A0,
1297/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
12980x000106A1,
1299/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
13000x000106A4,
1301 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1302 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1303 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
13040x000106A5,
1305};
1306
1307static inline bool cpu_has_broken_vmx_preemption_timer(void)
1308{
1309 u32 eax = cpuid_eax(0x00000001), i;
1310
1311 /* Clear the reserved bits */
1312 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001313 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001314 if (eax == vmx_preemption_cpu_tfms[i])
1315 return true;
1316
1317 return false;
1318}
1319
1320static inline bool cpu_has_vmx_preemption_timer(void)
1321{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001322 return vmcs_config.pin_based_exec_ctrl &
1323 PIN_BASED_VMX_PREEMPTION_TIMER;
1324}
1325
Yang Zhang01e439b2013-04-11 19:25:12 +08001326static inline bool cpu_has_vmx_posted_intr(void)
1327{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001328 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1329 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001330}
1331
1332static inline bool cpu_has_vmx_apicv(void)
1333{
1334 return cpu_has_vmx_apic_register_virt() &&
1335 cpu_has_vmx_virtual_intr_delivery() &&
1336 cpu_has_vmx_posted_intr();
1337}
1338
Sheng Yang04547152009-04-01 15:52:31 +08001339static inline bool cpu_has_vmx_flexpriority(void)
1340{
1341 return cpu_has_vmx_tpr_shadow() &&
1342 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001343}
1344
Marcelo Tosattie7997942009-06-11 12:07:40 -03001345static inline bool cpu_has_vmx_ept_execute_only(void)
1346{
Gui Jianfeng31299942010-03-15 17:29:09 +08001347 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001348}
1349
Marcelo Tosattie7997942009-06-11 12:07:40 -03001350static inline bool cpu_has_vmx_ept_2m_page(void)
1351{
Gui Jianfeng31299942010-03-15 17:29:09 +08001352 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001353}
1354
Sheng Yang878403b2010-01-05 19:02:29 +08001355static inline bool cpu_has_vmx_ept_1g_page(void)
1356{
Gui Jianfeng31299942010-03-15 17:29:09 +08001357 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001358}
1359
Sheng Yang4bc9b982010-06-02 14:05:24 +08001360static inline bool cpu_has_vmx_ept_4levels(void)
1361{
1362 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1363}
1364
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001365static inline bool cpu_has_vmx_ept_mt_wb(void)
1366{
1367 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1368}
1369
Yu Zhang855feb62017-08-24 20:27:55 +08001370static inline bool cpu_has_vmx_ept_5levels(void)
1371{
1372 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1373}
1374
Xudong Hao83c3a332012-05-28 19:33:35 +08001375static inline bool cpu_has_vmx_ept_ad_bits(void)
1376{
1377 return vmx_capability.ept & VMX_EPT_AD_BIT;
1378}
1379
Gui Jianfeng31299942010-03-15 17:29:09 +08001380static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001381{
Gui Jianfeng31299942010-03-15 17:29:09 +08001382 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001383}
1384
Gui Jianfeng31299942010-03-15 17:29:09 +08001385static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001386{
Gui Jianfeng31299942010-03-15 17:29:09 +08001387 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001388}
1389
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001390static inline bool cpu_has_vmx_invvpid_single(void)
1391{
1392 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1393}
1394
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001395static inline bool cpu_has_vmx_invvpid_global(void)
1396{
1397 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1398}
1399
Wanpeng Li08d839c2017-03-23 05:30:08 -07001400static inline bool cpu_has_vmx_invvpid(void)
1401{
1402 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1403}
1404
Gui Jianfeng31299942010-03-15 17:29:09 +08001405static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001406{
Sheng Yang04547152009-04-01 15:52:31 +08001407 return vmcs_config.cpu_based_2nd_exec_ctrl &
1408 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001409}
1410
Gui Jianfeng31299942010-03-15 17:29:09 +08001411static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001412{
1413 return vmcs_config.cpu_based_2nd_exec_ctrl &
1414 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1415}
1416
Gui Jianfeng31299942010-03-15 17:29:09 +08001417static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001418{
1419 return vmcs_config.cpu_based_2nd_exec_ctrl &
1420 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1421}
1422
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001423static inline bool cpu_has_vmx_basic_inout(void)
1424{
1425 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1426}
1427
Paolo Bonzini35754c92015-07-29 12:05:37 +02001428static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001429{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001430 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001431}
1432
Gui Jianfeng31299942010-03-15 17:29:09 +08001433static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001434{
Sheng Yang04547152009-04-01 15:52:31 +08001435 return vmcs_config.cpu_based_2nd_exec_ctrl &
1436 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001437}
1438
Gui Jianfeng31299942010-03-15 17:29:09 +08001439static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001440{
1441 return vmcs_config.cpu_based_2nd_exec_ctrl &
1442 SECONDARY_EXEC_RDTSCP;
1443}
1444
Mao, Junjiead756a12012-07-02 01:18:48 +00001445static inline bool cpu_has_vmx_invpcid(void)
1446{
1447 return vmcs_config.cpu_based_2nd_exec_ctrl &
1448 SECONDARY_EXEC_ENABLE_INVPCID;
1449}
1450
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001451static inline bool cpu_has_virtual_nmis(void)
1452{
1453 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1454}
1455
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001456static inline bool cpu_has_vmx_wbinvd_exit(void)
1457{
1458 return vmcs_config.cpu_based_2nd_exec_ctrl &
1459 SECONDARY_EXEC_WBINVD_EXITING;
1460}
1461
Abel Gordonabc4fc52013-04-18 14:35:25 +03001462static inline bool cpu_has_vmx_shadow_vmcs(void)
1463{
1464 u64 vmx_msr;
1465 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1466 /* check if the cpu supports writing r/o exit information fields */
1467 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1468 return false;
1469
1470 return vmcs_config.cpu_based_2nd_exec_ctrl &
1471 SECONDARY_EXEC_SHADOW_VMCS;
1472}
1473
Kai Huang843e4332015-01-28 10:54:28 +08001474static inline bool cpu_has_vmx_pml(void)
1475{
1476 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1477}
1478
Haozhong Zhang64903d62015-10-20 15:39:09 +08001479static inline bool cpu_has_vmx_tsc_scaling(void)
1480{
1481 return vmcs_config.cpu_based_2nd_exec_ctrl &
1482 SECONDARY_EXEC_TSC_SCALING;
1483}
1484
Bandan Das2a499e42017-08-03 15:54:41 -04001485static inline bool cpu_has_vmx_vmfunc(void)
1486{
1487 return vmcs_config.cpu_based_2nd_exec_ctrl &
1488 SECONDARY_EXEC_ENABLE_VMFUNC;
1489}
1490
Sheng Yang04547152009-04-01 15:52:31 +08001491static inline bool report_flexpriority(void)
1492{
1493 return flexpriority_enabled;
1494}
1495
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001496static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1497{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001498 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001499}
1500
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001501static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1502{
1503 return vmcs12->cpu_based_vm_exec_control & bit;
1504}
1505
1506static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1507{
1508 return (vmcs12->cpu_based_vm_exec_control &
1509 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1510 (vmcs12->secondary_vm_exec_control & bit);
1511}
1512
Jan Kiszkaf41245002014-03-07 20:03:13 +01001513static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1514{
1515 return vmcs12->pin_based_vm_exec_control &
1516 PIN_BASED_VMX_PREEMPTION_TIMER;
1517}
1518
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001519static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1520{
1521 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1522}
1523
1524static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1525{
1526 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1527}
1528
Nadav Har'El155a97a2013-08-05 11:07:16 +03001529static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1530{
1531 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1532}
1533
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001534static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1535{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001536 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001537}
1538
Bandan Dasc5f983f2017-05-05 15:25:14 -04001539static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1540{
1541 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1542}
1543
Wincy Vanf2b93282015-02-03 23:56:03 +08001544static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1545{
1546 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1547}
1548
Wanpeng Li5c614b32015-10-13 09:18:36 -07001549static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1550{
1551 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1552}
1553
Wincy Van82f0dd42015-02-03 23:57:18 +08001554static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1555{
1556 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1557}
1558
Wincy Van608406e2015-02-03 23:57:51 +08001559static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1560{
1561 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1562}
1563
Wincy Van705699a2015-02-03 23:58:17 +08001564static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1565{
1566 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1567}
1568
Bandan Das27c42a12017-08-03 15:54:42 -04001569static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1570{
1571 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1572}
1573
Bandan Das41ab9372017-08-03 15:54:43 -04001574static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1575{
1576 return nested_cpu_has_vmfunc(vmcs12) &&
1577 (vmcs12->vm_function_control &
1578 VMX_VMFUNC_EPTP_SWITCHING);
1579}
1580
Jim Mattsonef85b672016-12-12 11:01:37 -08001581static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001582{
1583 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001584 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001585}
1586
Jan Kiszka533558b2014-01-04 18:47:20 +01001587static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1588 u32 exit_intr_info,
1589 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001590static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1591 struct vmcs12 *vmcs12,
1592 u32 reason, unsigned long qualification);
1593
Rusty Russell8b9cf982007-07-30 16:31:43 +10001594static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001595{
1596 int i;
1597
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001598 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001599 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001600 return i;
1601 return -1;
1602}
1603
Sheng Yang2384d2b2008-01-17 15:14:33 +08001604static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1605{
1606 struct {
1607 u64 vpid : 16;
1608 u64 rsvd : 48;
1609 u64 gva;
1610 } operand = { vpid, 0, gva };
1611
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001612 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001613 /* CF==1 or ZF==1 --> rc = -1 */
1614 "; ja 1f ; ud2 ; 1:"
1615 : : "a"(&operand), "c"(ext) : "cc", "memory");
1616}
1617
Sheng Yang14394422008-04-28 12:24:45 +08001618static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1619{
1620 struct {
1621 u64 eptp, gpa;
1622 } operand = {eptp, gpa};
1623
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001624 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001625 /* CF==1 or ZF==1 --> rc = -1 */
1626 "; ja 1f ; ud2 ; 1:\n"
1627 : : "a" (&operand), "c" (ext) : "cc", "memory");
1628}
1629
Avi Kivity26bb0982009-09-07 11:14:12 +03001630static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001631{
1632 int i;
1633
Rusty Russell8b9cf982007-07-30 16:31:43 +10001634 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001635 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001636 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001637 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001638}
1639
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640static void vmcs_clear(struct vmcs *vmcs)
1641{
1642 u64 phys_addr = __pa(vmcs);
1643 u8 error;
1644
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001645 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001646 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647 : "cc", "memory");
1648 if (error)
1649 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1650 vmcs, phys_addr);
1651}
1652
Nadav Har'Eld462b812011-05-24 15:26:10 +03001653static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1654{
1655 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001656 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1657 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001658 loaded_vmcs->cpu = -1;
1659 loaded_vmcs->launched = 0;
1660}
1661
Dongxiao Xu7725b892010-05-11 18:29:38 +08001662static void vmcs_load(struct vmcs *vmcs)
1663{
1664 u64 phys_addr = __pa(vmcs);
1665 u8 error;
1666
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001667 if (static_branch_unlikely(&enable_evmcs))
1668 return evmcs_load(phys_addr);
1669
Dongxiao Xu7725b892010-05-11 18:29:38 +08001670 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001671 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001672 : "cc", "memory");
1673 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001674 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001675 vmcs, phys_addr);
1676}
1677
Dave Young2965faa2015-09-09 15:38:55 -07001678#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001679/*
1680 * This bitmap is used to indicate whether the vmclear
1681 * operation is enabled on all cpus. All disabled by
1682 * default.
1683 */
1684static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1685
1686static inline void crash_enable_local_vmclear(int cpu)
1687{
1688 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1689}
1690
1691static inline void crash_disable_local_vmclear(int cpu)
1692{
1693 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1694}
1695
1696static inline int crash_local_vmclear_enabled(int cpu)
1697{
1698 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1699}
1700
1701static void crash_vmclear_local_loaded_vmcss(void)
1702{
1703 int cpu = raw_smp_processor_id();
1704 struct loaded_vmcs *v;
1705
1706 if (!crash_local_vmclear_enabled(cpu))
1707 return;
1708
1709 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1710 loaded_vmcss_on_cpu_link)
1711 vmcs_clear(v->vmcs);
1712}
1713#else
1714static inline void crash_enable_local_vmclear(int cpu) { }
1715static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001716#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001717
Nadav Har'Eld462b812011-05-24 15:26:10 +03001718static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001720 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001721 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001722
Nadav Har'Eld462b812011-05-24 15:26:10 +03001723 if (loaded_vmcs->cpu != cpu)
1724 return; /* vcpu migration can race with cpu offline */
1725 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001727 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001728 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001729
1730 /*
1731 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1732 * is before setting loaded_vmcs->vcpu to -1 which is done in
1733 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1734 * then adds the vmcs into percpu list before it is deleted.
1735 */
1736 smp_wmb();
1737
Nadav Har'Eld462b812011-05-24 15:26:10 +03001738 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001739 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740}
1741
Nadav Har'Eld462b812011-05-24 15:26:10 +03001742static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001743{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001744 int cpu = loaded_vmcs->cpu;
1745
1746 if (cpu != -1)
1747 smp_call_function_single(cpu,
1748 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001749}
1750
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001751static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001752{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001753 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001754 return;
1755
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001756 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001757 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001758}
1759
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001760static inline void vpid_sync_vcpu_global(void)
1761{
1762 if (cpu_has_vmx_invvpid_global())
1763 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1764}
1765
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001766static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001767{
1768 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001769 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001770 else
1771 vpid_sync_vcpu_global();
1772}
1773
Sheng Yang14394422008-04-28 12:24:45 +08001774static inline void ept_sync_global(void)
1775{
David Hildenbrandf5f51582017-08-24 20:51:30 +02001776 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08001777}
1778
1779static inline void ept_sync_context(u64 eptp)
1780{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02001781 if (cpu_has_vmx_invept_context())
1782 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1783 else
1784 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08001785}
1786
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001787static __always_inline void vmcs_check16(unsigned long field)
1788{
1789 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1790 "16-bit accessor invalid for 64-bit field");
1791 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1792 "16-bit accessor invalid for 64-bit high field");
1793 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1794 "16-bit accessor invalid for 32-bit high field");
1795 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1796 "16-bit accessor invalid for natural width field");
1797}
1798
1799static __always_inline void vmcs_check32(unsigned long field)
1800{
1801 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1802 "32-bit accessor invalid for 16-bit field");
1803 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1804 "32-bit accessor invalid for natural width field");
1805}
1806
1807static __always_inline void vmcs_check64(unsigned long field)
1808{
1809 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1810 "64-bit accessor invalid for 16-bit field");
1811 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1812 "64-bit accessor invalid for 64-bit high field");
1813 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1814 "64-bit accessor invalid for 32-bit field");
1815 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1816 "64-bit accessor invalid for natural width field");
1817}
1818
1819static __always_inline void vmcs_checkl(unsigned long field)
1820{
1821 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1822 "Natural width accessor invalid for 16-bit field");
1823 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1824 "Natural width accessor invalid for 64-bit field");
1825 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1826 "Natural width accessor invalid for 64-bit high field");
1827 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1828 "Natural width accessor invalid for 32-bit field");
1829}
1830
1831static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832{
Avi Kivity5e520e62011-05-15 10:13:12 -04001833 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001834
Avi Kivity5e520e62011-05-15 10:13:12 -04001835 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1836 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837 return value;
1838}
1839
Avi Kivity96304212011-05-15 10:13:13 -04001840static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001842 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001843 if (static_branch_unlikely(&enable_evmcs))
1844 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001845 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001846}
1847
Avi Kivity96304212011-05-15 10:13:13 -04001848static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001849{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001850 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001851 if (static_branch_unlikely(&enable_evmcs))
1852 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001853 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854}
1855
Avi Kivity96304212011-05-15 10:13:13 -04001856static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001858 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001859 if (static_branch_unlikely(&enable_evmcs))
1860 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001861#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001862 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001864 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865#endif
1866}
1867
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001868static __always_inline unsigned long vmcs_readl(unsigned long field)
1869{
1870 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001871 if (static_branch_unlikely(&enable_evmcs))
1872 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001873 return __vmcs_readl(field);
1874}
1875
Avi Kivitye52de1b2007-01-05 16:36:56 -08001876static noinline void vmwrite_error(unsigned long field, unsigned long value)
1877{
1878 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1879 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1880 dump_stack();
1881}
1882
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001883static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884{
1885 u8 error;
1886
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001887 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001888 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001889 if (unlikely(error))
1890 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891}
1892
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001893static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001894{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001895 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001896 if (static_branch_unlikely(&enable_evmcs))
1897 return evmcs_write16(field, value);
1898
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001899 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900}
1901
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001902static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001904 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001905 if (static_branch_unlikely(&enable_evmcs))
1906 return evmcs_write32(field, value);
1907
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001908 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909}
1910
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001911static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001913 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001914 if (static_branch_unlikely(&enable_evmcs))
1915 return evmcs_write64(field, value);
1916
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001917 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001918#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001920 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921#endif
1922}
1923
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001924static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001925{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001926 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001927 if (static_branch_unlikely(&enable_evmcs))
1928 return evmcs_write64(field, value);
1929
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001930 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001931}
1932
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001933static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001934{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001935 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1936 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001937 if (static_branch_unlikely(&enable_evmcs))
1938 return evmcs_write32(field, evmcs_read32(field) & ~mask);
1939
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001940 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1941}
1942
1943static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1944{
1945 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1946 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001947 if (static_branch_unlikely(&enable_evmcs))
1948 return evmcs_write32(field, evmcs_read32(field) | mask);
1949
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001950 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001951}
1952
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001953static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1954{
1955 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1956}
1957
Gleb Natapov2961e8762013-11-25 15:37:13 +02001958static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1959{
1960 vmcs_write32(VM_ENTRY_CONTROLS, val);
1961 vmx->vm_entry_controls_shadow = val;
1962}
1963
1964static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1965{
1966 if (vmx->vm_entry_controls_shadow != val)
1967 vm_entry_controls_init(vmx, val);
1968}
1969
1970static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1971{
1972 return vmx->vm_entry_controls_shadow;
1973}
1974
1975
1976static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1977{
1978 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1979}
1980
1981static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1982{
1983 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1984}
1985
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001986static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1987{
1988 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1989}
1990
Gleb Natapov2961e8762013-11-25 15:37:13 +02001991static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1992{
1993 vmcs_write32(VM_EXIT_CONTROLS, val);
1994 vmx->vm_exit_controls_shadow = val;
1995}
1996
1997static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1998{
1999 if (vmx->vm_exit_controls_shadow != val)
2000 vm_exit_controls_init(vmx, val);
2001}
2002
2003static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2004{
2005 return vmx->vm_exit_controls_shadow;
2006}
2007
2008
2009static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2010{
2011 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2012}
2013
2014static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2015{
2016 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2017}
2018
Avi Kivity2fb92db2011-04-27 19:42:18 +03002019static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2020{
2021 vmx->segment_cache.bitmask = 0;
2022}
2023
2024static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2025 unsigned field)
2026{
2027 bool ret;
2028 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2029
2030 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2031 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2032 vmx->segment_cache.bitmask = 0;
2033 }
2034 ret = vmx->segment_cache.bitmask & mask;
2035 vmx->segment_cache.bitmask |= mask;
2036 return ret;
2037}
2038
2039static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2040{
2041 u16 *p = &vmx->segment_cache.seg[seg].selector;
2042
2043 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2044 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2045 return *p;
2046}
2047
2048static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2049{
2050 ulong *p = &vmx->segment_cache.seg[seg].base;
2051
2052 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2053 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2054 return *p;
2055}
2056
2057static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2058{
2059 u32 *p = &vmx->segment_cache.seg[seg].limit;
2060
2061 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2062 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2063 return *p;
2064}
2065
2066static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2067{
2068 u32 *p = &vmx->segment_cache.seg[seg].ar;
2069
2070 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2071 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2072 return *p;
2073}
2074
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002075static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2076{
2077 u32 eb;
2078
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002079 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002080 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002081 /*
2082 * Guest access to VMware backdoor ports could legitimately
2083 * trigger #GP because of TSS I/O permission bitmap.
2084 * We intercept those #GP and allow access to them anyway
2085 * as VMware does.
2086 */
2087 if (enable_vmware_backdoor)
2088 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002089 if ((vcpu->guest_debug &
2090 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2091 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2092 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002093 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002094 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002095 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002096 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002097
2098 /* When we are running a nested L2 guest and L1 specified for it a
2099 * certain exception bitmap, we must trap the same exceptions and pass
2100 * them to L1. When running L2, we will only handle the exceptions
2101 * specified above if L1 did not want them.
2102 */
2103 if (is_guest_mode(vcpu))
2104 eb |= get_vmcs12(vcpu)->exception_bitmap;
2105
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002106 vmcs_write32(EXCEPTION_BITMAP, eb);
2107}
2108
Ashok Raj15d45072018-02-01 22:59:43 +01002109/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002110 * Check if MSR is intercepted for currently loaded MSR bitmap.
2111 */
2112static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2113{
2114 unsigned long *msr_bitmap;
2115 int f = sizeof(unsigned long);
2116
2117 if (!cpu_has_vmx_msr_bitmap())
2118 return true;
2119
2120 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2121
2122 if (msr <= 0x1fff) {
2123 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2124 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2125 msr &= 0x1fff;
2126 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2127 }
2128
2129 return true;
2130}
2131
2132/*
Ashok Raj15d45072018-02-01 22:59:43 +01002133 * Check if MSR is intercepted for L01 MSR bitmap.
2134 */
2135static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2136{
2137 unsigned long *msr_bitmap;
2138 int f = sizeof(unsigned long);
2139
2140 if (!cpu_has_vmx_msr_bitmap())
2141 return true;
2142
2143 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2144
2145 if (msr <= 0x1fff) {
2146 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2147 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2148 msr &= 0x1fff;
2149 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2150 }
2151
2152 return true;
2153}
2154
Gleb Natapov2961e8762013-11-25 15:37:13 +02002155static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2156 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002157{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002158 vm_entry_controls_clearbit(vmx, entry);
2159 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002160}
2161
Avi Kivity61d2ef22010-04-28 16:40:38 +03002162static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2163{
2164 unsigned i;
2165 struct msr_autoload *m = &vmx->msr_autoload;
2166
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002167 switch (msr) {
2168 case MSR_EFER:
2169 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002170 clear_atomic_switch_msr_special(vmx,
2171 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002172 VM_EXIT_LOAD_IA32_EFER);
2173 return;
2174 }
2175 break;
2176 case MSR_CORE_PERF_GLOBAL_CTRL:
2177 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002178 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002179 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2180 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2181 return;
2182 }
2183 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002184 }
2185
Avi Kivity61d2ef22010-04-28 16:40:38 +03002186 for (i = 0; i < m->nr; ++i)
2187 if (m->guest[i].index == msr)
2188 break;
2189
2190 if (i == m->nr)
2191 return;
2192 --m->nr;
2193 m->guest[i] = m->guest[m->nr];
2194 m->host[i] = m->host[m->nr];
2195 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2196 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2197}
2198
Gleb Natapov2961e8762013-11-25 15:37:13 +02002199static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2200 unsigned long entry, unsigned long exit,
2201 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2202 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002203{
2204 vmcs_write64(guest_val_vmcs, guest_val);
2205 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002206 vm_entry_controls_setbit(vmx, entry);
2207 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002208}
2209
Avi Kivity61d2ef22010-04-28 16:40:38 +03002210static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2211 u64 guest_val, u64 host_val)
2212{
2213 unsigned i;
2214 struct msr_autoload *m = &vmx->msr_autoload;
2215
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002216 switch (msr) {
2217 case MSR_EFER:
2218 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002219 add_atomic_switch_msr_special(vmx,
2220 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002221 VM_EXIT_LOAD_IA32_EFER,
2222 GUEST_IA32_EFER,
2223 HOST_IA32_EFER,
2224 guest_val, host_val);
2225 return;
2226 }
2227 break;
2228 case MSR_CORE_PERF_GLOBAL_CTRL:
2229 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002230 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002231 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2232 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2233 GUEST_IA32_PERF_GLOBAL_CTRL,
2234 HOST_IA32_PERF_GLOBAL_CTRL,
2235 guest_val, host_val);
2236 return;
2237 }
2238 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002239 case MSR_IA32_PEBS_ENABLE:
2240 /* PEBS needs a quiescent period after being disabled (to write
2241 * a record). Disabling PEBS through VMX MSR swapping doesn't
2242 * provide that period, so a CPU could write host's record into
2243 * guest's memory.
2244 */
2245 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002246 }
2247
Avi Kivity61d2ef22010-04-28 16:40:38 +03002248 for (i = 0; i < m->nr; ++i)
2249 if (m->guest[i].index == msr)
2250 break;
2251
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002252 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002253 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002254 "Can't add msr %x\n", msr);
2255 return;
2256 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002257 ++m->nr;
2258 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2259 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2260 }
2261
2262 m->guest[i].index = msr;
2263 m->guest[i].value = guest_val;
2264 m->host[i].index = msr;
2265 m->host[i].value = host_val;
2266}
2267
Avi Kivity92c0d902009-10-29 11:00:16 +02002268static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002269{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002270 u64 guest_efer = vmx->vcpu.arch.efer;
2271 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002272
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002273 if (!enable_ept) {
2274 /*
2275 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2276 * host CPUID is more efficient than testing guest CPUID
2277 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2278 */
2279 if (boot_cpu_has(X86_FEATURE_SMEP))
2280 guest_efer |= EFER_NX;
2281 else if (!(guest_efer & EFER_NX))
2282 ignore_bits |= EFER_NX;
2283 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002284
Avi Kivity51c6cf62007-08-29 03:48:05 +03002285 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002286 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002287 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002288 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002289#ifdef CONFIG_X86_64
2290 ignore_bits |= EFER_LMA | EFER_LME;
2291 /* SCE is meaningful only in long mode on Intel */
2292 if (guest_efer & EFER_LMA)
2293 ignore_bits &= ~(u64)EFER_SCE;
2294#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002295
2296 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002297
2298 /*
2299 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2300 * On CPUs that support "load IA32_EFER", always switch EFER
2301 * atomically, since it's faster than switching it manually.
2302 */
2303 if (cpu_has_load_ia32_efer ||
2304 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002305 if (!(guest_efer & EFER_LMA))
2306 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002307 if (guest_efer != host_efer)
2308 add_atomic_switch_msr(vmx, MSR_EFER,
2309 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002310 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002311 } else {
2312 guest_efer &= ~ignore_bits;
2313 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002314
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002315 vmx->guest_msrs[efer_offset].data = guest_efer;
2316 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2317
2318 return true;
2319 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002320}
2321
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002322#ifdef CONFIG_X86_32
2323/*
2324 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2325 * VMCS rather than the segment table. KVM uses this helper to figure
2326 * out the current bases to poke them into the VMCS before entry.
2327 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002328static unsigned long segment_base(u16 selector)
2329{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002330 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002331 unsigned long v;
2332
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002333 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002334 return 0;
2335
Thomas Garnier45fc8752017-03-14 10:05:08 -07002336 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002337
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002338 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002339 u16 ldt_selector = kvm_read_ldt();
2340
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002341 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002342 return 0;
2343
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002344 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002345 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002346 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002347 return v;
2348}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002349#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002350
Avi Kivity04d2cc72007-09-10 18:10:54 +03002351static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002352{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002353 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002354#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002355 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002356#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002357 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002358
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002359 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002360 return;
2361
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002362 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002363 /*
2364 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2365 * allow segment selectors with cpl > 0 or ti == 1.
2366 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002367 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002368 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002369
2370#ifdef CONFIG_X86_64
2371 save_fsgs_for_kvm();
2372 vmx->host_state.fs_sel = current->thread.fsindex;
2373 vmx->host_state.gs_sel = current->thread.gsindex;
2374#else
Avi Kivity9581d442010-10-19 16:46:55 +02002375 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002376 savesegment(gs, vmx->host_state.gs_sel);
2377#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002378 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002379 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002380 vmx->host_state.fs_reload_needed = 0;
2381 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002382 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002383 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002384 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002385 if (!(vmx->host_state.gs_sel & 7))
2386 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002387 else {
2388 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002389 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002390 }
2391
2392#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002393 savesegment(ds, vmx->host_state.ds_sel);
2394 savesegment(es, vmx->host_state.es_sel);
2395#endif
2396
2397#ifdef CONFIG_X86_64
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002398 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002399 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity33ed6322007-05-02 16:54:03 +03002400#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002401 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2402 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002403#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002404
2405#ifdef CONFIG_X86_64
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002406 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002407 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002408 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002409#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002410 if (boot_cpu_has(X86_FEATURE_MPX))
2411 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002412 for (i = 0; i < vmx->save_nmsrs; ++i)
2413 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002414 vmx->guest_msrs[i].data,
2415 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002416}
2417
Avi Kivitya9b21b62008-06-24 11:48:49 +03002418static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002419{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002420 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002421 return;
2422
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002423 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002424 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002425#ifdef CONFIG_X86_64
2426 if (is_long_mode(&vmx->vcpu))
2427 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2428#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002429 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002430 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002431#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002432 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002433#else
2434 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002435#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002436 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002437 if (vmx->host_state.fs_reload_needed)
2438 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002439#ifdef CONFIG_X86_64
2440 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2441 loadsegment(ds, vmx->host_state.ds_sel);
2442 loadsegment(es, vmx->host_state.es_sel);
2443 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002444#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002445 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002446#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002447 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002448#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002449 if (vmx->host_state.msr_host_bndcfgs)
2450 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002451 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002452}
2453
Avi Kivitya9b21b62008-06-24 11:48:49 +03002454static void vmx_load_host_state(struct vcpu_vmx *vmx)
2455{
2456 preempt_disable();
2457 __vmx_load_host_state(vmx);
2458 preempt_enable();
2459}
2460
Feng Wu28b835d2015-09-18 22:29:54 +08002461static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2462{
2463 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2464 struct pi_desc old, new;
2465 unsigned int dest;
2466
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002467 /*
2468 * In case of hot-plug or hot-unplug, we may have to undo
2469 * vmx_vcpu_pi_put even if there is no assigned device. And we
2470 * always keep PI.NDST up to date for simplicity: it makes the
2471 * code easier, and CPU migration is not a fast path.
2472 */
2473 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002474 return;
2475
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002476 /*
2477 * First handle the simple case where no cmpxchg is necessary; just
2478 * allow posting non-urgent interrupts.
2479 *
2480 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2481 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2482 * expects the VCPU to be on the blocked_vcpu_list that matches
2483 * PI.NDST.
2484 */
2485 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2486 vcpu->cpu == cpu) {
2487 pi_clear_sn(pi_desc);
2488 return;
2489 }
2490
2491 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002492 do {
2493 old.control = new.control = pi_desc->control;
2494
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002495 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002496
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002497 if (x2apic_enabled())
2498 new.ndst = dest;
2499 else
2500 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002501
Feng Wu28b835d2015-09-18 22:29:54 +08002502 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002503 } while (cmpxchg64(&pi_desc->control, old.control,
2504 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002505}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002506
Peter Feinerc95ba922016-08-17 09:36:47 -07002507static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2508{
2509 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2510 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2511}
2512
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513/*
2514 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2515 * vcpu mutex is already taken.
2516 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002517static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002518{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002519 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002520 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002522 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002523 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002524 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002525 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002526
2527 /*
2528 * Read loaded_vmcs->cpu should be before fetching
2529 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2530 * See the comments in __loaded_vmcs_clear().
2531 */
2532 smp_rmb();
2533
Nadav Har'Eld462b812011-05-24 15:26:10 +03002534 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2535 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002536 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002537 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002538 }
2539
2540 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2541 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2542 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002543 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002544 }
2545
2546 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002547 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002548 unsigned long sysenter_esp;
2549
2550 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002551
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552 /*
2553 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002554 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002556 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002557 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002558 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002560 /*
2561 * VM exits change the host TR limit to 0x67 after a VM
2562 * exit. This is okay, since 0x67 covers everything except
2563 * the IO bitmap and have have code to handle the IO bitmap
2564 * being lost after a VM exit.
2565 */
2566 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2567
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2569 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002570
Nadav Har'Eld462b812011-05-24 15:26:10 +03002571 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 }
Feng Wu28b835d2015-09-18 22:29:54 +08002573
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002574 /* Setup TSC multiplier */
2575 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002576 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2577 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002578
Feng Wu28b835d2015-09-18 22:29:54 +08002579 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002580 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002581 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002582}
2583
2584static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2585{
2586 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2587
2588 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002589 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2590 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002591 return;
2592
2593 /* Set SN when the vCPU is preempted */
2594 if (vcpu->preempted)
2595 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596}
2597
2598static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2599{
Feng Wu28b835d2015-09-18 22:29:54 +08002600 vmx_vcpu_pi_put(vcpu);
2601
Avi Kivitya9b21b62008-06-24 11:48:49 +03002602 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603}
2604
Wanpeng Lif244dee2017-07-20 01:11:54 -07002605static bool emulation_required(struct kvm_vcpu *vcpu)
2606{
2607 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2608}
2609
Avi Kivityedcafe32009-12-30 18:07:40 +02002610static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2611
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002612/*
2613 * Return the cr0 value that a nested guest would read. This is a combination
2614 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2615 * its hypervisor (cr0_read_shadow).
2616 */
2617static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2618{
2619 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2620 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2621}
2622static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2623{
2624 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2625 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2626}
2627
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2629{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002630 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002631
Avi Kivity6de12732011-03-07 12:51:22 +02002632 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2633 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2634 rflags = vmcs_readl(GUEST_RFLAGS);
2635 if (to_vmx(vcpu)->rmode.vm86_active) {
2636 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2637 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2638 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2639 }
2640 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002641 }
Avi Kivity6de12732011-03-07 12:51:22 +02002642 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643}
2644
2645static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2646{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002647 unsigned long old_rflags = vmx_get_rflags(vcpu);
2648
Avi Kivity6de12732011-03-07 12:51:22 +02002649 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2650 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002651 if (to_vmx(vcpu)->rmode.vm86_active) {
2652 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002653 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002654 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002656
2657 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2658 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659}
2660
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002661static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002662{
2663 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2664 int ret = 0;
2665
2666 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002667 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002668 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002669 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002670
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002671 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002672}
2673
2674static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2675{
2676 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2677 u32 interruptibility = interruptibility_old;
2678
2679 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2680
Jan Kiszka48005f62010-02-19 19:38:07 +01002681 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002682 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002683 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002684 interruptibility |= GUEST_INTR_STATE_STI;
2685
2686 if ((interruptibility != interruptibility_old))
2687 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2688}
2689
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2691{
2692 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002694 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002696 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002697
Glauber Costa2809f5d2009-05-12 16:21:05 -04002698 /* skipping an emulated instruction also counts */
2699 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700}
2701
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002702static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2703 unsigned long exit_qual)
2704{
2705 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2706 unsigned int nr = vcpu->arch.exception.nr;
2707 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2708
2709 if (vcpu->arch.exception.has_error_code) {
2710 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2711 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2712 }
2713
2714 if (kvm_exception_is_soft(nr))
2715 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2716 else
2717 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2718
2719 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2720 vmx_get_nmi_mask(vcpu))
2721 intr_info |= INTR_INFO_UNBLOCK_NMI;
2722
2723 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2724}
2725
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002726/*
2727 * KVM wants to inject page-faults which it got to the guest. This function
2728 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002729 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002730static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002731{
2732 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002733 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002734
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002735 if (nr == PF_VECTOR) {
2736 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002737 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002738 return 1;
2739 }
2740 /*
2741 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
2742 * The fix is to add the ancillary datum (CR2 or DR6) to structs
2743 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
2744 * can be written only when inject_pending_event runs. This should be
2745 * conditional on a new capability---if the capability is disabled,
2746 * kvm_multiple_exception would write the ancillary information to
2747 * CR2 or DR6, for backwards ABI-compatibility.
2748 */
2749 if (nested_vmx_is_page_fault_vmexit(vmcs12,
2750 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002751 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002752 return 1;
2753 }
2754 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002755 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07002756 if (nr == DB_VECTOR)
2757 *exit_qual = vcpu->arch.dr6;
2758 else
2759 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002760 return 1;
2761 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07002762 }
2763
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002764 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002765}
2766
Wanpeng Licaa057a2018-03-12 04:53:03 -07002767static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2768{
2769 /*
2770 * Ensure that we clear the HLT state in the VMCS. We don't need to
2771 * explicitly skip the instruction because if the HLT state is set,
2772 * then the instruction is already executing and RIP has already been
2773 * advanced.
2774 */
2775 if (kvm_hlt_in_guest(vcpu->kvm) &&
2776 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2777 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2778}
2779
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002780static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02002781{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002782 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002783 unsigned nr = vcpu->arch.exception.nr;
2784 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07002785 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002786 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002787
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002788 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002789 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002790 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2791 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002792
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002793 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002794 int inc_eip = 0;
2795 if (kvm_exception_is_soft(nr))
2796 inc_eip = vcpu->arch.event_exit_inst_len;
2797 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002798 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002799 return;
2800 }
2801
Sean Christophersonadd5ff72018-03-23 09:34:00 -07002802 WARN_ON_ONCE(vmx->emulation_required);
2803
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002804 if (kvm_exception_is_soft(nr)) {
2805 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2806 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002807 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2808 } else
2809 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2810
2811 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07002812
2813 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02002814}
2815
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002816static bool vmx_rdtscp_supported(void)
2817{
2818 return cpu_has_vmx_rdtscp();
2819}
2820
Mao, Junjiead756a12012-07-02 01:18:48 +00002821static bool vmx_invpcid_supported(void)
2822{
2823 return cpu_has_vmx_invpcid() && enable_ept;
2824}
2825
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826/*
Eddie Donga75beee2007-05-17 18:55:15 +03002827 * Swap MSR entry in host/guest MSR entry array.
2828 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002829static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002830{
Avi Kivity26bb0982009-09-07 11:14:12 +03002831 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002832
2833 tmp = vmx->guest_msrs[to];
2834 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2835 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002836}
2837
2838/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002839 * Set up the vmcs to automatically save and restore system
2840 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2841 * mode, as fiddling with msrs is very expensive.
2842 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002843static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002844{
Avi Kivity26bb0982009-09-07 11:14:12 +03002845 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002846
Eddie Donga75beee2007-05-17 18:55:15 +03002847 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002848#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002849 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002850 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002851 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002852 move_msr_up(vmx, index, save_nmsrs++);
2853 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002854 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002855 move_msr_up(vmx, index, save_nmsrs++);
2856 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002857 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002858 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002859 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02002860 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002861 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002862 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002863 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002864 * if efer.sce is enabled.
2865 */
Brian Gerst8c065852010-07-17 09:03:26 -04002866 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002867 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002868 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002869 }
Eddie Donga75beee2007-05-17 18:55:15 +03002870#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002871 index = __find_msr_index(vmx, MSR_EFER);
2872 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002873 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002874
Avi Kivity26bb0982009-09-07 11:14:12 +03002875 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002876
Yang Zhang8d146952013-01-25 10:18:50 +08002877 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002878 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002879}
2880
2881/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002883 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2884 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002886static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887{
2888 u64 host_tsc, tsc_offset;
2889
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002890 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002892 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893}
2894
2895/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002896 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002898static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002900 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002901 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002902 * We're here if L1 chose not to trap WRMSR to TSC. According
2903 * to the spec, this should set L1's TSC; The offset that L1
2904 * set for L2 remains unchanged, and still needs to be added
2905 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002906 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002907 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002908 /* recalculate vmcs02.TSC_OFFSET: */
2909 vmcs12 = get_vmcs12(vcpu);
2910 vmcs_write64(TSC_OFFSET, offset +
2911 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2912 vmcs12->tsc_offset : 0));
2913 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002914 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2915 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002916 vmcs_write64(TSC_OFFSET, offset);
2917 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918}
2919
Nadav Har'El801d3422011-05-25 23:02:23 +03002920/*
2921 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2922 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2923 * all guests if the "nested" module option is off, and can also be disabled
2924 * for a single guest by disabling its VMX cpuid bit.
2925 */
2926static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2927{
Radim Krčmářd6321d42017-08-05 00:12:49 +02002928 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03002929}
2930
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002932 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2933 * returned for the various VMX controls MSRs when nested VMX is enabled.
2934 * The same values should also be used to verify that vmcs12 control fields are
2935 * valid during nested entry from L1 to L2.
2936 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2937 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2938 * bit in the high half is on if the corresponding bit in the control field
2939 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002940 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002941static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002942{
Paolo Bonzini13893092018-02-26 13:40:09 +01002943 if (!nested) {
2944 memset(msrs, 0, sizeof(*msrs));
2945 return;
2946 }
2947
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002948 /*
2949 * Note that as a general rule, the high half of the MSRs (bits in
2950 * the control fields which may be 1) should be initialized by the
2951 * intersection of the underlying hardware's MSR (i.e., features which
2952 * can be supported) and the list of features we want to expose -
2953 * because they are known to be properly supported in our code.
2954 * Also, usually, the low half of the MSRs (bits which must be 1) can
2955 * be set to 0, meaning that L1 may turn off any of these bits. The
2956 * reason is that if one of these bits is necessary, it will appear
2957 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2958 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02002959 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002960 * These rules have exceptions below.
2961 */
2962
2963 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002964 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002965 msrs->pinbased_ctls_low,
2966 msrs->pinbased_ctls_high);
2967 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002968 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002969 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002970 PIN_BASED_EXT_INTR_MASK |
2971 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01002972 PIN_BASED_VIRTUAL_NMIS |
2973 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002974 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002975 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002976 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002977
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002978 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002979 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002980 msrs->exit_ctls_low,
2981 msrs->exit_ctls_high);
2982 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08002983 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002984
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002985 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002986#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002987 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002988#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002989 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002990 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08002991 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002992 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002993 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2994
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002995 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002996 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002997
Jan Kiszka2996fca2014-06-16 13:59:43 +02002998 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01002999 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003000
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003001 /* entry controls */
3002 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003003 msrs->entry_ctls_low,
3004 msrs->entry_ctls_high);
3005 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003006 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003007 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003008#ifdef CONFIG_X86_64
3009 VM_ENTRY_IA32E_MODE |
3010#endif
3011 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003012 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003013 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003014 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003015 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003016
Jan Kiszka2996fca2014-06-16 13:59:43 +02003017 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003018 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003019
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003020 /* cpu-based controls */
3021 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003022 msrs->procbased_ctls_low,
3023 msrs->procbased_ctls_high);
3024 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003025 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003026 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003027 CPU_BASED_VIRTUAL_INTR_PENDING |
3028 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003029 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3030 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3031 CPU_BASED_CR3_STORE_EXITING |
3032#ifdef CONFIG_X86_64
3033 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3034#endif
3035 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003036 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3037 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3038 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3039 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003040 /*
3041 * We can allow some features even when not supported by the
3042 * hardware. For example, L1 can specify an MSR bitmap - and we
3043 * can use it to avoid exits to L1 - even when L0 runs L2
3044 * without MSR bitmaps.
3045 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003046 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003047 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003048 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003049
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003050 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003051 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003052 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3053
Paolo Bonzini80154d72017-08-24 13:55:35 +02003054 /*
3055 * secondary cpu-based controls. Do not include those that
3056 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3057 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003058 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003059 msrs->secondary_ctls_low,
3060 msrs->secondary_ctls_high);
3061 msrs->secondary_ctls_low = 0;
3062 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003063 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003064 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003065 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003066 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003067 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003068 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003069
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003070 if (enable_ept) {
3071 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003072 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003073 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003074 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003075 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003076 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003077 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003078 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003079 msrs->ept_caps &= vmx_capability.ept;
3080 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003081 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3082 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003083 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003084 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003085 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003086 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003087 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003088 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003089
Bandan Das27c42a12017-08-03 15:54:42 -04003090 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003091 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003092 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003093 /*
3094 * Advertise EPTP switching unconditionally
3095 * since we emulate it
3096 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003097 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003098 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003099 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003100 }
3101
Paolo Bonzinief697a72016-03-18 16:58:38 +01003102 /*
3103 * Old versions of KVM use the single-context version without
3104 * checking for support, so declare that it is supported even
3105 * though it is treated as global context. The alternative is
3106 * not failing the single-context invvpid, and it is worse.
3107 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003108 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003109 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003110 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003111 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003112 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003113 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003114
Radim Krčmář0790ec12015-03-17 14:02:32 +01003115 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003116 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003117 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3118
Jan Kiszkac18911a2013-03-13 16:06:41 +01003119 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003120 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003121 msrs->misc_low,
3122 msrs->misc_high);
3123 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3124 msrs->misc_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003125 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003126 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003127 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003128
3129 /*
3130 * This MSR reports some information about VMX support. We
3131 * should return information about the VMX we emulate for the
3132 * guest, and the VMCS structure we give it - not about the
3133 * VMX support of the underlying hardware.
3134 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003135 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003136 VMCS12_REVISION |
3137 VMX_BASIC_TRUE_CTLS |
3138 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3139 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3140
3141 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003142 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003143
3144 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003145 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003146 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3147 * We picked the standard core2 setting.
3148 */
3149#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3150#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003151 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3152 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003153
3154 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003155 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3156 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003157
3158 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003159 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003160}
3161
David Matlack38991522016-11-29 18:14:08 -08003162/*
3163 * if fixed0[i] == 1: val[i] must be 1
3164 * if fixed1[i] == 0: val[i] must be 0
3165 */
3166static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3167{
3168 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003169}
3170
3171static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3172{
David Matlack38991522016-11-29 18:14:08 -08003173 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003174}
3175
3176static inline u64 vmx_control_msr(u32 low, u32 high)
3177{
3178 return low | ((u64)high << 32);
3179}
3180
David Matlack62cc6b9d2016-11-29 18:14:07 -08003181static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3182{
3183 superset &= mask;
3184 subset &= mask;
3185
3186 return (superset | subset) == superset;
3187}
3188
3189static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3190{
3191 const u64 feature_and_reserved =
3192 /* feature (except bit 48; see below) */
3193 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3194 /* reserved */
3195 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003196 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003197
3198 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3199 return -EINVAL;
3200
3201 /*
3202 * KVM does not emulate a version of VMX that constrains physical
3203 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3204 */
3205 if (data & BIT_ULL(48))
3206 return -EINVAL;
3207
3208 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3209 vmx_basic_vmcs_revision_id(data))
3210 return -EINVAL;
3211
3212 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3213 return -EINVAL;
3214
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003215 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003216 return 0;
3217}
3218
3219static int
3220vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3221{
3222 u64 supported;
3223 u32 *lowp, *highp;
3224
3225 switch (msr_index) {
3226 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003227 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3228 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003229 break;
3230 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003231 lowp = &vmx->nested.msrs.procbased_ctls_low;
3232 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003233 break;
3234 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003235 lowp = &vmx->nested.msrs.exit_ctls_low;
3236 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003237 break;
3238 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003239 lowp = &vmx->nested.msrs.entry_ctls_low;
3240 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003241 break;
3242 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003243 lowp = &vmx->nested.msrs.secondary_ctls_low;
3244 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003245 break;
3246 default:
3247 BUG();
3248 }
3249
3250 supported = vmx_control_msr(*lowp, *highp);
3251
3252 /* Check must-be-1 bits are still 1. */
3253 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3254 return -EINVAL;
3255
3256 /* Check must-be-0 bits are still 0. */
3257 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3258 return -EINVAL;
3259
3260 *lowp = data;
3261 *highp = data >> 32;
3262 return 0;
3263}
3264
3265static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3266{
3267 const u64 feature_and_reserved_bits =
3268 /* feature */
3269 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3270 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3271 /* reserved */
3272 GENMASK_ULL(13, 9) | BIT_ULL(31);
3273 u64 vmx_misc;
3274
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003275 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3276 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003277
3278 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3279 return -EINVAL;
3280
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003281 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003282 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3283 vmx_misc_preemption_timer_rate(data) !=
3284 vmx_misc_preemption_timer_rate(vmx_misc))
3285 return -EINVAL;
3286
3287 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3288 return -EINVAL;
3289
3290 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3291 return -EINVAL;
3292
3293 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3294 return -EINVAL;
3295
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003296 vmx->nested.msrs.misc_low = data;
3297 vmx->nested.msrs.misc_high = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003298 return 0;
3299}
3300
3301static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3302{
3303 u64 vmx_ept_vpid_cap;
3304
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003305 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3306 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003307
3308 /* Every bit is either reserved or a feature bit. */
3309 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3310 return -EINVAL;
3311
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003312 vmx->nested.msrs.ept_caps = data;
3313 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003314 return 0;
3315}
3316
3317static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3318{
3319 u64 *msr;
3320
3321 switch (msr_index) {
3322 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003323 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003324 break;
3325 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003326 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003327 break;
3328 default:
3329 BUG();
3330 }
3331
3332 /*
3333 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3334 * must be 1 in the restored value.
3335 */
3336 if (!is_bitwise_subset(data, *msr, -1ULL))
3337 return -EINVAL;
3338
3339 *msr = data;
3340 return 0;
3341}
3342
3343/*
3344 * Called when userspace is restoring VMX MSRs.
3345 *
3346 * Returns 0 on success, non-0 otherwise.
3347 */
3348static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3349{
3350 struct vcpu_vmx *vmx = to_vmx(vcpu);
3351
3352 switch (msr_index) {
3353 case MSR_IA32_VMX_BASIC:
3354 return vmx_restore_vmx_basic(vmx, data);
3355 case MSR_IA32_VMX_PINBASED_CTLS:
3356 case MSR_IA32_VMX_PROCBASED_CTLS:
3357 case MSR_IA32_VMX_EXIT_CTLS:
3358 case MSR_IA32_VMX_ENTRY_CTLS:
3359 /*
3360 * The "non-true" VMX capability MSRs are generated from the
3361 * "true" MSRs, so we do not support restoring them directly.
3362 *
3363 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3364 * should restore the "true" MSRs with the must-be-1 bits
3365 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3366 * DEFAULT SETTINGS".
3367 */
3368 return -EINVAL;
3369 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3370 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3371 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3372 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3373 case MSR_IA32_VMX_PROCBASED_CTLS2:
3374 return vmx_restore_control_msr(vmx, msr_index, data);
3375 case MSR_IA32_VMX_MISC:
3376 return vmx_restore_vmx_misc(vmx, data);
3377 case MSR_IA32_VMX_CR0_FIXED0:
3378 case MSR_IA32_VMX_CR4_FIXED0:
3379 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3380 case MSR_IA32_VMX_CR0_FIXED1:
3381 case MSR_IA32_VMX_CR4_FIXED1:
3382 /*
3383 * These MSRs are generated based on the vCPU's CPUID, so we
3384 * do not support restoring them directly.
3385 */
3386 return -EINVAL;
3387 case MSR_IA32_VMX_EPT_VPID_CAP:
3388 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3389 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003390 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003391 return 0;
3392 default:
3393 /*
3394 * The rest of the VMX capability MSRs do not support restore.
3395 */
3396 return -EINVAL;
3397 }
3398}
3399
Jan Kiszkacae50132014-01-04 18:47:22 +01003400/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003401static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003402{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003403 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003404 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003405 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003406 break;
3407 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3408 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003409 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003410 msrs->pinbased_ctls_low,
3411 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003412 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3413 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003414 break;
3415 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3416 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003417 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003418 msrs->procbased_ctls_low,
3419 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003420 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3421 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003422 break;
3423 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3424 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003425 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003426 msrs->exit_ctls_low,
3427 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003428 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3429 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003430 break;
3431 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3432 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003433 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003434 msrs->entry_ctls_low,
3435 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003436 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3437 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003438 break;
3439 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003440 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003441 msrs->misc_low,
3442 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003443 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003444 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003445 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003446 break;
3447 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003448 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003449 break;
3450 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003451 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003452 break;
3453 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003454 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003455 break;
3456 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003457 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003458 break;
3459 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003460 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003461 msrs->secondary_ctls_low,
3462 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003463 break;
3464 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003465 *pdata = msrs->ept_caps |
3466 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003467 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003468 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003469 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003470 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003471 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003472 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003473 }
3474
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003475 return 0;
3476}
3477
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003478static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3479 uint64_t val)
3480{
3481 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3482
3483 return !(val & ~valid_bits);
3484}
3485
Tom Lendacky801e4592018-02-21 13:39:51 -06003486static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3487{
Paolo Bonzini13893092018-02-26 13:40:09 +01003488 switch (msr->index) {
3489 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3490 if (!nested)
3491 return 1;
3492 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3493 default:
3494 return 1;
3495 }
3496
3497 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003498}
3499
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003500/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501 * Reads an msr value (of 'msr_index') into 'pdata'.
3502 * Returns 0 on success, non-0 otherwise.
3503 * Assumes vcpu_load() was already called.
3504 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003505static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003506{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003507 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003508 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003510 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003511#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003513 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514 break;
3515 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003516 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003518 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003519 vmx_load_host_state(vmx);
3520 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003521 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003522#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003524 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303525 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003526 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003528 case MSR_IA32_SPEC_CTRL:
3529 if (!msr_info->host_initiated &&
3530 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3531 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3532 return 1;
3533
3534 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3535 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003536 case MSR_IA32_ARCH_CAPABILITIES:
3537 if (!msr_info->host_initiated &&
3538 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3539 return 1;
3540 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3541 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003543 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544 break;
3545 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003546 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003547 break;
3548 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003549 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003551 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003552 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003553 (!msr_info->host_initiated &&
3554 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003555 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003556 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003557 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003558 case MSR_IA32_MCG_EXT_CTL:
3559 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003560 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003561 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003562 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003563 msr_info->data = vcpu->arch.mcg_ext_ctl;
3564 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003565 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003566 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003567 break;
3568 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3569 if (!nested_vmx_allowed(vcpu))
3570 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003571 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3572 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003573 case MSR_IA32_XSS:
3574 if (!vmx_xsaves_supported())
3575 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003576 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003577 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003578 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003579 if (!msr_info->host_initiated &&
3580 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003581 return 1;
3582 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003584 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003585 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003586 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003587 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003588 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003589 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003590 }
3591
Avi Kivity6aa8b732006-12-10 02:21:36 -08003592 return 0;
3593}
3594
Jan Kiszkacae50132014-01-04 18:47:22 +01003595static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3596
Avi Kivity6aa8b732006-12-10 02:21:36 -08003597/*
3598 * Writes msr value into into the appropriate "register".
3599 * Returns 0 on success, non-0 otherwise.
3600 * Assumes vcpu_load() was already called.
3601 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003602static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003603{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003604 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003605 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003606 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003607 u32 msr_index = msr_info->index;
3608 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003609
Avi Kivity6aa8b732006-12-10 02:21:36 -08003610 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003611 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003612 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003613 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003614#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003616 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617 vmcs_writel(GUEST_FS_BASE, data);
3618 break;
3619 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003620 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003621 vmcs_writel(GUEST_GS_BASE, data);
3622 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003623 case MSR_KERNEL_GS_BASE:
3624 vmx_load_host_state(vmx);
3625 vmx->msr_guest_kernel_gs_base = data;
3626 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003627#endif
3628 case MSR_IA32_SYSENTER_CS:
3629 vmcs_write32(GUEST_SYSENTER_CS, data);
3630 break;
3631 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003632 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633 break;
3634 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003635 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003637 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003638 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003639 (!msr_info->host_initiated &&
3640 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003641 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003642 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003643 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003644 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003645 vmcs_write64(GUEST_BNDCFGS, data);
3646 break;
3647 case MSR_IA32_TSC:
3648 kvm_write_tsc(vcpu, msr_info);
3649 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003650 case MSR_IA32_SPEC_CTRL:
3651 if (!msr_info->host_initiated &&
3652 !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) &&
3653 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3654 return 1;
3655
3656 /* The STIBP bit doesn't fault even if it's not advertised */
3657 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP))
3658 return 1;
3659
3660 vmx->spec_ctrl = data;
3661
3662 if (!data)
3663 break;
3664
3665 /*
3666 * For non-nested:
3667 * When it's written (to non-zero) for the first time, pass
3668 * it through.
3669 *
3670 * For nested:
3671 * The handling of the MSR bitmap for L2 guests is done in
3672 * nested_vmx_merge_msr_bitmap. We should not touch the
3673 * vmcs02.msr_bitmap here since it gets completely overwritten
3674 * in the merging. We update the vmcs01 here for L1 as well
3675 * since it will end up touching the MSR anyway now.
3676 */
3677 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3678 MSR_IA32_SPEC_CTRL,
3679 MSR_TYPE_RW);
3680 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003681 case MSR_IA32_PRED_CMD:
3682 if (!msr_info->host_initiated &&
3683 !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) &&
3684 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3685 return 1;
3686
3687 if (data & ~PRED_CMD_IBPB)
3688 return 1;
3689
3690 if (!data)
3691 break;
3692
3693 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3694
3695 /*
3696 * For non-nested:
3697 * When it's written (to non-zero) for the first time, pass
3698 * it through.
3699 *
3700 * For nested:
3701 * The handling of the MSR bitmap for L2 guests is done in
3702 * nested_vmx_merge_msr_bitmap. We should not touch the
3703 * vmcs02.msr_bitmap here since it gets completely overwritten
3704 * in the merging.
3705 */
3706 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3707 MSR_TYPE_W);
3708 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003709 case MSR_IA32_ARCH_CAPABILITIES:
3710 if (!msr_info->host_initiated)
3711 return 1;
3712 vmx->arch_capabilities = data;
3713 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003714 case MSR_IA32_CR_PAT:
3715 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003716 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3717 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003718 vmcs_write64(GUEST_IA32_PAT, data);
3719 vcpu->arch.pat = data;
3720 break;
3721 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003722 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003723 break;
Will Auldba904632012-11-29 12:42:50 -08003724 case MSR_IA32_TSC_ADJUST:
3725 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003726 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003727 case MSR_IA32_MCG_EXT_CTL:
3728 if ((!msr_info->host_initiated &&
3729 !(to_vmx(vcpu)->msr_ia32_feature_control &
3730 FEATURE_CONTROL_LMCE)) ||
3731 (data & ~MCG_EXT_CTL_LMCE_EN))
3732 return 1;
3733 vcpu->arch.mcg_ext_ctl = data;
3734 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003735 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003736 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003737 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003738 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3739 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003740 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003741 if (msr_info->host_initiated && data == 0)
3742 vmx_leave_nested(vcpu);
3743 break;
3744 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003745 if (!msr_info->host_initiated)
3746 return 1; /* they are read-only */
3747 if (!nested_vmx_allowed(vcpu))
3748 return 1;
3749 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003750 case MSR_IA32_XSS:
3751 if (!vmx_xsaves_supported())
3752 return 1;
3753 /*
3754 * The only supported bit as of Skylake is bit 8, but
3755 * it is not supported on KVM.
3756 */
3757 if (data != 0)
3758 return 1;
3759 vcpu->arch.ia32_xss = data;
3760 if (vcpu->arch.ia32_xss != host_xss)
3761 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3762 vcpu->arch.ia32_xss, host_xss);
3763 else
3764 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3765 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003766 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003767 if (!msr_info->host_initiated &&
3768 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003769 return 1;
3770 /* Check reserved bit, higher 32 bits should be zero */
3771 if ((data >> 32) != 0)
3772 return 1;
3773 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003774 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003775 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003776 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003777 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003778 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003779 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3780 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003781 ret = kvm_set_shared_msr(msr->index, msr->data,
3782 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003783 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003784 if (ret)
3785 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003786 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003787 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003789 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003790 }
3791
Eddie Dong2cc51562007-05-21 07:28:09 +03003792 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793}
3794
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003795static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003797 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3798 switch (reg) {
3799 case VCPU_REGS_RSP:
3800 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3801 break;
3802 case VCPU_REGS_RIP:
3803 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3804 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003805 case VCPU_EXREG_PDPTR:
3806 if (enable_ept)
3807 ept_save_pdptrs(vcpu);
3808 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003809 default:
3810 break;
3811 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003812}
3813
Avi Kivity6aa8b732006-12-10 02:21:36 -08003814static __init int cpu_has_kvm_support(void)
3815{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003816 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003817}
3818
3819static __init int vmx_disabled_by_bios(void)
3820{
3821 u64 msr;
3822
3823 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003824 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003825 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003826 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3827 && tboot_enabled())
3828 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003829 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003830 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003831 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003832 && !tboot_enabled()) {
3833 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003834 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003835 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003836 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003837 /* launched w/o TXT and VMX disabled */
3838 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3839 && !tboot_enabled())
3840 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003841 }
3842
3843 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844}
3845
Dongxiao Xu7725b892010-05-11 18:29:38 +08003846static void kvm_cpu_vmxon(u64 addr)
3847{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003848 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003849 intel_pt_handle_vmx(1);
3850
Dongxiao Xu7725b892010-05-11 18:29:38 +08003851 asm volatile (ASM_VMX_VMXON_RAX
3852 : : "a"(&addr), "m"(addr)
3853 : "memory", "cc");
3854}
3855
Radim Krčmář13a34e02014-08-28 15:13:03 +02003856static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857{
3858 int cpu = raw_smp_processor_id();
3859 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003860 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003862 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003863 return -EBUSY;
3864
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01003865 /*
3866 * This can happen if we hot-added a CPU but failed to allocate
3867 * VP assist page for it.
3868 */
3869 if (static_branch_unlikely(&enable_evmcs) &&
3870 !hv_get_vp_assist_page(cpu))
3871 return -EFAULT;
3872
Nadav Har'Eld462b812011-05-24 15:26:10 +03003873 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003874 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3875 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003876
3877 /*
3878 * Now we can enable the vmclear operation in kdump
3879 * since the loaded_vmcss_on_cpu list on this cpu
3880 * has been initialized.
3881 *
3882 * Though the cpu is not in VMX operation now, there
3883 * is no problem to enable the vmclear operation
3884 * for the loaded_vmcss_on_cpu list is empty!
3885 */
3886 crash_enable_local_vmclear(cpu);
3887
Avi Kivity6aa8b732006-12-10 02:21:36 -08003888 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003889
3890 test_bits = FEATURE_CONTROL_LOCKED;
3891 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3892 if (tboot_enabled())
3893 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3894
3895 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003896 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003897 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3898 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003899 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02003900 if (enable_ept)
3901 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003902
3903 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003904}
3905
Nadav Har'Eld462b812011-05-24 15:26:10 +03003906static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003907{
3908 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003909 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003910
Nadav Har'Eld462b812011-05-24 15:26:10 +03003911 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3912 loaded_vmcss_on_cpu_link)
3913 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003914}
3915
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003916
3917/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3918 * tricks.
3919 */
3920static void kvm_cpu_vmxoff(void)
3921{
3922 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003923
3924 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003925 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003926}
3927
Radim Krčmář13a34e02014-08-28 15:13:03 +02003928static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003930 vmclear_local_loaded_vmcss();
3931 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932}
3933
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003934static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003935 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936{
3937 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003938 u32 ctl = ctl_min | ctl_opt;
3939
3940 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3941
3942 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3943 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3944
3945 /* Ensure minimum (required) set of control bits are supported. */
3946 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003947 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003948
3949 *result = ctl;
3950 return 0;
3951}
3952
Avi Kivity110312c2010-12-21 12:54:20 +02003953static __init bool allow_1_setting(u32 msr, u32 ctl)
3954{
3955 u32 vmx_msr_low, vmx_msr_high;
3956
3957 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3958 return vmx_msr_high & ctl;
3959}
3960
Yang, Sheng002c7f72007-07-31 14:23:01 +03003961static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003962{
3963 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003964 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003965 u32 _pin_based_exec_control = 0;
3966 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003967 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003968 u32 _vmexit_control = 0;
3969 u32 _vmentry_control = 0;
3970
Paolo Bonzini13893092018-02-26 13:40:09 +01003971 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05303972 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003973#ifdef CONFIG_X86_64
3974 CPU_BASED_CR8_LOAD_EXITING |
3975 CPU_BASED_CR8_STORE_EXITING |
3976#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003977 CPU_BASED_CR3_LOAD_EXITING |
3978 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08003979 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003980 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003981 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07003982 CPU_BASED_MWAIT_EXITING |
3983 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003984 CPU_BASED_INVLPG_EXITING |
3985 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003986
Sheng Yangf78e0e22007-10-29 09:40:42 +08003987 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003988 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003989 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003990 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3991 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003992 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003993#ifdef CONFIG_X86_64
3994 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3995 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3996 ~CPU_BASED_CR8_STORE_EXITING;
3997#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003998 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003999 min2 = 0;
4000 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004001 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004002 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004003 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004004 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004005 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004006 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004007 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004008 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004009 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004010 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004011 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004012 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004013 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004014 SECONDARY_EXEC_RDSEED_EXITING |
4015 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004016 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004017 SECONDARY_EXEC_TSC_SCALING |
4018 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004019 if (adjust_vmx_controls(min2, opt2,
4020 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004021 &_cpu_based_2nd_exec_control) < 0)
4022 return -EIO;
4023 }
4024#ifndef CONFIG_X86_64
4025 if (!(_cpu_based_2nd_exec_control &
4026 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4027 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4028#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004029
4030 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4031 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004032 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004033 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4034 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004035
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004036 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4037 &vmx_capability.ept, &vmx_capability.vpid);
4038
Sheng Yangd56f5462008-04-25 10:13:16 +08004039 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004040 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4041 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004042 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4043 CPU_BASED_CR3_STORE_EXITING |
4044 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004045 } else if (vmx_capability.ept) {
4046 vmx_capability.ept = 0;
4047 pr_warn_once("EPT CAP should not exist if not support "
4048 "1-setting enable EPT VM-execution control\n");
4049 }
4050 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4051 vmx_capability.vpid) {
4052 vmx_capability.vpid = 0;
4053 pr_warn_once("VPID CAP should not exist if not support "
4054 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004055 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004056
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004057 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004058#ifdef CONFIG_X86_64
4059 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4060#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004061 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004062 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004063 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4064 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004065 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004066
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004067 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4068 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4069 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004070 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4071 &_pin_based_exec_control) < 0)
4072 return -EIO;
4073
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004074 if (cpu_has_broken_vmx_preemption_timer())
4075 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004076 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004077 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004078 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4079
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004080 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004081 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004082 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4083 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004084 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004085
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004086 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004087
4088 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4089 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004090 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004091
4092#ifdef CONFIG_X86_64
4093 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4094 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004095 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004096#endif
4097
4098 /* Require Write-Back (WB) memory type for VMCS accesses. */
4099 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004100 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004101
Yang, Sheng002c7f72007-07-31 14:23:01 +03004102 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004103 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004104 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004105
4106 /* KVM supports Enlightened VMCS v1 only */
4107 if (static_branch_unlikely(&enable_evmcs))
4108 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4109 else
4110 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004111
Yang, Sheng002c7f72007-07-31 14:23:01 +03004112 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4113 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004114 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004115 vmcs_conf->vmexit_ctrl = _vmexit_control;
4116 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004117
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004118 if (static_branch_unlikely(&enable_evmcs))
4119 evmcs_sanitize_exec_ctrls(vmcs_conf);
4120
Avi Kivity110312c2010-12-21 12:54:20 +02004121 cpu_has_load_ia32_efer =
4122 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4123 VM_ENTRY_LOAD_IA32_EFER)
4124 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4125 VM_EXIT_LOAD_IA32_EFER);
4126
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004127 cpu_has_load_perf_global_ctrl =
4128 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4129 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4130 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4131 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4132
4133 /*
4134 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004135 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004136 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4137 *
4138 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4139 *
4140 * AAK155 (model 26)
4141 * AAP115 (model 30)
4142 * AAT100 (model 37)
4143 * BC86,AAY89,BD102 (model 44)
4144 * BA97 (model 46)
4145 *
4146 */
4147 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4148 switch (boot_cpu_data.x86_model) {
4149 case 26:
4150 case 30:
4151 case 37:
4152 case 44:
4153 case 46:
4154 cpu_has_load_perf_global_ctrl = false;
4155 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4156 "does not work properly. Using workaround\n");
4157 break;
4158 default:
4159 break;
4160 }
4161 }
4162
Borislav Petkov782511b2016-04-04 22:25:03 +02004163 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004164 rdmsrl(MSR_IA32_XSS, host_xss);
4165
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004166 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004167}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168
4169static struct vmcs *alloc_vmcs_cpu(int cpu)
4170{
4171 int node = cpu_to_node(cpu);
4172 struct page *pages;
4173 struct vmcs *vmcs;
4174
Vlastimil Babka96db8002015-09-08 15:03:50 -07004175 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 if (!pages)
4177 return NULL;
4178 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004179 memset(vmcs, 0, vmcs_config.size);
4180 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 return vmcs;
4182}
4183
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184static void free_vmcs(struct vmcs *vmcs)
4185{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004186 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187}
4188
Nadav Har'Eld462b812011-05-24 15:26:10 +03004189/*
4190 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4191 */
4192static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4193{
4194 if (!loaded_vmcs->vmcs)
4195 return;
4196 loaded_vmcs_clear(loaded_vmcs);
4197 free_vmcs(loaded_vmcs->vmcs);
4198 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004199 if (loaded_vmcs->msr_bitmap)
4200 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004201 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004202}
4203
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004204static struct vmcs *alloc_vmcs(void)
4205{
4206 return alloc_vmcs_cpu(raw_smp_processor_id());
4207}
4208
4209static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4210{
4211 loaded_vmcs->vmcs = alloc_vmcs();
4212 if (!loaded_vmcs->vmcs)
4213 return -ENOMEM;
4214
4215 loaded_vmcs->shadow_vmcs = NULL;
4216 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004217
4218 if (cpu_has_vmx_msr_bitmap()) {
4219 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4220 if (!loaded_vmcs->msr_bitmap)
4221 goto out_vmcs;
4222 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
4223 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004224 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004225
4226out_vmcs:
4227 free_loaded_vmcs(loaded_vmcs);
4228 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004229}
4230
Sam Ravnborg39959582007-06-01 00:47:13 -07004231static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004232{
4233 int cpu;
4234
Zachary Amsden3230bb42009-09-29 11:38:37 -10004235 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004236 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004237 per_cpu(vmxarea, cpu) = NULL;
4238 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004239}
4240
Jim Mattsond37f4262017-12-22 12:12:16 -08004241enum vmcs_field_width {
4242 VMCS_FIELD_WIDTH_U16 = 0,
4243 VMCS_FIELD_WIDTH_U64 = 1,
4244 VMCS_FIELD_WIDTH_U32 = 2,
4245 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004246};
4247
Jim Mattsond37f4262017-12-22 12:12:16 -08004248static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004249{
4250 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004251 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004252 return (field >> 13) & 0x3 ;
4253}
4254
4255static inline int vmcs_field_readonly(unsigned long field)
4256{
4257 return (((field >> 10) & 0x3) == 1);
4258}
4259
Bandan Dasfe2b2012014-04-21 15:20:14 -04004260static void init_vmcs_shadow_fields(void)
4261{
4262 int i, j;
4263
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004264 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4265 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004266 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004267 (i + 1 == max_shadow_read_only_fields ||
4268 shadow_read_only_fields[i + 1] != field + 1))
4269 pr_err("Missing field from shadow_read_only_field %x\n",
4270 field + 1);
4271
4272 clear_bit(field, vmx_vmread_bitmap);
4273#ifdef CONFIG_X86_64
4274 if (field & 1)
4275 continue;
4276#endif
4277 if (j < i)
4278 shadow_read_only_fields[j] = field;
4279 j++;
4280 }
4281 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004282
4283 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004284 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004285 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004286 (i + 1 == max_shadow_read_write_fields ||
4287 shadow_read_write_fields[i + 1] != field + 1))
4288 pr_err("Missing field from shadow_read_write_field %x\n",
4289 field + 1);
4290
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004291 /*
4292 * PML and the preemption timer can be emulated, but the
4293 * processor cannot vmwrite to fields that don't exist
4294 * on bare metal.
4295 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004296 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004297 case GUEST_PML_INDEX:
4298 if (!cpu_has_vmx_pml())
4299 continue;
4300 break;
4301 case VMX_PREEMPTION_TIMER_VALUE:
4302 if (!cpu_has_vmx_preemption_timer())
4303 continue;
4304 break;
4305 case GUEST_INTR_STATUS:
4306 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004307 continue;
4308 break;
4309 default:
4310 break;
4311 }
4312
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004313 clear_bit(field, vmx_vmwrite_bitmap);
4314 clear_bit(field, vmx_vmread_bitmap);
4315#ifdef CONFIG_X86_64
4316 if (field & 1)
4317 continue;
4318#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004319 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004320 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004321 j++;
4322 }
4323 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004324}
4325
Avi Kivity6aa8b732006-12-10 02:21:36 -08004326static __init int alloc_kvm_area(void)
4327{
4328 int cpu;
4329
Zachary Amsden3230bb42009-09-29 11:38:37 -10004330 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004331 struct vmcs *vmcs;
4332
4333 vmcs = alloc_vmcs_cpu(cpu);
4334 if (!vmcs) {
4335 free_kvm_area();
4336 return -ENOMEM;
4337 }
4338
4339 per_cpu(vmxarea, cpu) = vmcs;
4340 }
4341 return 0;
4342}
4343
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004344static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004345 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004347 if (!emulate_invalid_guest_state) {
4348 /*
4349 * CS and SS RPL should be equal during guest entry according
4350 * to VMX spec, but in reality it is not always so. Since vcpu
4351 * is in the middle of the transition from real mode to
4352 * protected mode it is safe to assume that RPL 0 is a good
4353 * default value.
4354 */
4355 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004356 save->selector &= ~SEGMENT_RPL_MASK;
4357 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004358 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004360 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004361}
4362
4363static void enter_pmode(struct kvm_vcpu *vcpu)
4364{
4365 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004366 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004367
Gleb Natapovd99e4152012-12-20 16:57:45 +02004368 /*
4369 * Update real mode segment cache. It may be not up-to-date if sement
4370 * register was written while vcpu was in a guest mode.
4371 */
4372 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4373 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4374 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4375 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4376 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4377 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4378
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004379 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004380
Avi Kivity2fb92db2011-04-27 19:42:18 +03004381 vmx_segment_cache_clear(vmx);
4382
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004383 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004384
4385 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004386 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4387 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004388 vmcs_writel(GUEST_RFLAGS, flags);
4389
Rusty Russell66aee912007-07-17 23:34:16 +10004390 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4391 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392
4393 update_exception_bitmap(vcpu);
4394
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004395 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4396 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4397 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4398 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4399 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4400 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004401}
4402
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004403static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004404{
Mathias Krause772e0312012-08-30 01:30:19 +02004405 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004406 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004407
Gleb Natapovd99e4152012-12-20 16:57:45 +02004408 var.dpl = 0x3;
4409 if (seg == VCPU_SREG_CS)
4410 var.type = 0x3;
4411
4412 if (!emulate_invalid_guest_state) {
4413 var.selector = var.base >> 4;
4414 var.base = var.base & 0xffff0;
4415 var.limit = 0xffff;
4416 var.g = 0;
4417 var.db = 0;
4418 var.present = 1;
4419 var.s = 1;
4420 var.l = 0;
4421 var.unusable = 0;
4422 var.type = 0x3;
4423 var.avl = 0;
4424 if (save->base & 0xf)
4425 printk_once(KERN_WARNING "kvm: segment base is not "
4426 "paragraph aligned when entering "
4427 "protected mode (seg=%d)", seg);
4428 }
4429
4430 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004431 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004432 vmcs_write32(sf->limit, var.limit);
4433 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004434}
4435
4436static void enter_rmode(struct kvm_vcpu *vcpu)
4437{
4438 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004439 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004440 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004442 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4443 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4444 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4445 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4446 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004447 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4448 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004449
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004450 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004451
Gleb Natapov776e58e2011-03-13 12:34:27 +02004452 /*
4453 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004454 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004455 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004456 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004457 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4458 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004459
Avi Kivity2fb92db2011-04-27 19:42:18 +03004460 vmx_segment_cache_clear(vmx);
4461
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004462 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004463 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004464 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4465
4466 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004467 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004469 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470
4471 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004472 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473 update_exception_bitmap(vcpu);
4474
Gleb Natapovd99e4152012-12-20 16:57:45 +02004475 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4476 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4477 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4478 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4479 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4480 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004481
Eddie Dong8668a3c2007-10-10 14:26:45 +08004482 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004483}
4484
Amit Shah401d10d2009-02-20 22:53:37 +05304485static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4486{
4487 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004488 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4489
4490 if (!msr)
4491 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304492
Avi Kivity44ea2b12009-09-06 15:55:37 +03004493 /*
4494 * Force kernel_gs_base reloading before EFER changes, as control
4495 * of this msr depends on is_long_mode().
4496 */
4497 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004498 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304499 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004500 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304501 msr->data = efer;
4502 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004503 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304504
4505 msr->data = efer & ~EFER_LME;
4506 }
4507 setup_msrs(vmx);
4508}
4509
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004510#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004511
4512static void enter_lmode(struct kvm_vcpu *vcpu)
4513{
4514 u32 guest_tr_ar;
4515
Avi Kivity2fb92db2011-04-27 19:42:18 +03004516 vmx_segment_cache_clear(to_vmx(vcpu));
4517
Avi Kivity6aa8b732006-12-10 02:21:36 -08004518 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004519 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004520 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4521 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004522 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004523 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4524 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004525 }
Avi Kivityda38f432010-07-06 11:30:49 +03004526 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004527}
4528
4529static void exit_lmode(struct kvm_vcpu *vcpu)
4530{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004531 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004532 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004533}
4534
4535#endif
4536
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004537static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4538 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004539{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004540 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004541 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4542 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004543 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004544 } else {
4545 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004546 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004547}
4548
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004549static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004550{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004551 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004552}
4553
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004554static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4555{
4556 if (enable_ept)
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004557 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004558}
4559
Avi Kivitye8467fd2009-12-29 18:43:06 +02004560static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4561{
4562 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4563
4564 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4565 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4566}
4567
Avi Kivityaff48ba2010-12-05 18:56:11 +02004568static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4569{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004570 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004571 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4572 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4573}
4574
Anthony Liguori25c4c272007-04-27 09:29:21 +03004575static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004576{
Avi Kivityfc78f512009-12-07 12:16:48 +02004577 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4578
4579 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4580 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004581}
4582
Sheng Yang14394422008-04-28 12:24:45 +08004583static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4584{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004585 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4586
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004587 if (!test_bit(VCPU_EXREG_PDPTR,
4588 (unsigned long *)&vcpu->arch.regs_dirty))
4589 return;
4590
Sheng Yang14394422008-04-28 12:24:45 +08004591 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004592 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4593 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4594 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4595 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004596 }
4597}
4598
Avi Kivity8f5d5492009-05-31 18:41:29 +03004599static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4600{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004601 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4602
Avi Kivity8f5d5492009-05-31 18:41:29 +03004603 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004604 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4605 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4606 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4607 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004608 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004609
4610 __set_bit(VCPU_EXREG_PDPTR,
4611 (unsigned long *)&vcpu->arch.regs_avail);
4612 __set_bit(VCPU_EXREG_PDPTR,
4613 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004614}
4615
David Matlack38991522016-11-29 18:14:08 -08004616static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4617{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004618 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4619 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004620 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4621
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004622 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004623 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4624 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4625 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4626
4627 return fixed_bits_valid(val, fixed0, fixed1);
4628}
4629
4630static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4631{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004632 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4633 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004634
4635 return fixed_bits_valid(val, fixed0, fixed1);
4636}
4637
4638static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4639{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004640 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4641 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004642
4643 return fixed_bits_valid(val, fixed0, fixed1);
4644}
4645
4646/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4647#define nested_guest_cr4_valid nested_cr4_valid
4648#define nested_host_cr4_valid nested_cr4_valid
4649
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004650static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004651
4652static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4653 unsigned long cr0,
4654 struct kvm_vcpu *vcpu)
4655{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004656 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4657 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004658 if (!(cr0 & X86_CR0_PG)) {
4659 /* From paging/starting to nonpaging */
4660 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004661 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004662 (CPU_BASED_CR3_LOAD_EXITING |
4663 CPU_BASED_CR3_STORE_EXITING));
4664 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004665 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004666 } else if (!is_paging(vcpu)) {
4667 /* From nonpaging to paging */
4668 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004669 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004670 ~(CPU_BASED_CR3_LOAD_EXITING |
4671 CPU_BASED_CR3_STORE_EXITING));
4672 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004673 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004674 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004675
4676 if (!(cr0 & X86_CR0_WP))
4677 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004678}
4679
Avi Kivity6aa8b732006-12-10 02:21:36 -08004680static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4681{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004682 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004683 unsigned long hw_cr0;
4684
Gleb Natapov50378782013-02-04 16:00:28 +02004685 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004686 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004687 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004688 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004689 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004690
Gleb Natapov218e7632013-01-21 15:36:45 +02004691 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4692 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004693
Gleb Natapov218e7632013-01-21 15:36:45 +02004694 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4695 enter_rmode(vcpu);
4696 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004697
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004698#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004699 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004700 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004702 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004703 exit_lmode(vcpu);
4704 }
4705#endif
4706
Sean Christophersonb4d18512018-03-05 12:04:40 -08004707 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004708 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4709
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004711 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004712 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004713
4714 /* depends on vcpu->arch.cr0 to be set to a new value */
4715 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716}
4717
Yu Zhang855feb62017-08-24 20:27:55 +08004718static int get_ept_level(struct kvm_vcpu *vcpu)
4719{
4720 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
4721 return 5;
4722 return 4;
4723}
4724
Peter Feiner995f00a2017-06-30 17:26:32 -07004725static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08004726{
Yu Zhang855feb62017-08-24 20:27:55 +08004727 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08004728
Yu Zhang855feb62017-08-24 20:27:55 +08004729 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08004730
Peter Feiner995f00a2017-06-30 17:26:32 -07004731 if (enable_ept_ad_bits &&
4732 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02004733 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004734 eptp |= (root_hpa & PAGE_MASK);
4735
4736 return eptp;
4737}
4738
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4740{
Sheng Yang14394422008-04-28 12:24:45 +08004741 unsigned long guest_cr3;
4742 u64 eptp;
4743
4744 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004745 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07004746 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08004747 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08004748 if (enable_unrestricted_guest || is_paging(vcpu) ||
4749 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004750 guest_cr3 = kvm_read_cr3(vcpu);
4751 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004752 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004753 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004754 }
4755
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004756 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08004757 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004758}
4759
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004760static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004762 /*
4763 * Pass through host's Machine Check Enable value to hw_cr4, which
4764 * is in force while we are in guest mode. Do not let guests control
4765 * this bit, even if host CR4.MCE == 0.
4766 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004767 unsigned long hw_cr4;
4768
4769 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
4770 if (enable_unrestricted_guest)
4771 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
4772 else if (to_vmx(vcpu)->rmode.vm86_active)
4773 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
4774 else
4775 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004776
Paolo Bonzini0367f202016-07-12 10:44:55 +02004777 if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) {
4778 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4779 SECONDARY_EXEC_DESC);
4780 hw_cr4 &= ~X86_CR4_UMIP;
Radim Krčmář99158242018-01-31 18:12:50 +01004781 } else if (!is_guest_mode(vcpu) ||
4782 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
Paolo Bonzini0367f202016-07-12 10:44:55 +02004783 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4784 SECONDARY_EXEC_DESC);
4785
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004786 if (cr4 & X86_CR4_VMXE) {
4787 /*
4788 * To use VMXON (and later other VMX instructions), a guest
4789 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4790 * So basically the check on whether to allow nested VMX
4791 * is here.
4792 */
4793 if (!nested_vmx_allowed(vcpu))
4794 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004795 }
David Matlack38991522016-11-29 18:14:08 -08004796
4797 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004798 return 1;
4799
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004800 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08004801
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004802 if (!enable_unrestricted_guest) {
4803 if (enable_ept) {
4804 if (!is_paging(vcpu)) {
4805 hw_cr4 &= ~X86_CR4_PAE;
4806 hw_cr4 |= X86_CR4_PSE;
4807 } else if (!(cr4 & X86_CR4_PAE)) {
4808 hw_cr4 &= ~X86_CR4_PAE;
4809 }
4810 }
4811
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004812 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004813 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4814 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4815 * to be manually disabled when guest switches to non-paging
4816 * mode.
4817 *
4818 * If !enable_unrestricted_guest, the CPU is always running
4819 * with CR0.PG=1 and CR4 needs to be modified.
4820 * If enable_unrestricted_guest, the CPU automatically
4821 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004822 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08004823 if (!is_paging(vcpu))
4824 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
4825 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004826
Sheng Yang14394422008-04-28 12:24:45 +08004827 vmcs_writel(CR4_READ_SHADOW, cr4);
4828 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004829 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004830}
4831
Avi Kivity6aa8b732006-12-10 02:21:36 -08004832static void vmx_get_segment(struct kvm_vcpu *vcpu,
4833 struct kvm_segment *var, int seg)
4834{
Avi Kivitya9179492011-01-03 14:28:52 +02004835 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004836 u32 ar;
4837
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004838 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004839 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004840 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004841 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004842 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004843 var->base = vmx_read_guest_seg_base(vmx, seg);
4844 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4845 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004846 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004847 var->base = vmx_read_guest_seg_base(vmx, seg);
4848 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4849 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4850 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004851 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004852 var->type = ar & 15;
4853 var->s = (ar >> 4) & 1;
4854 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004855 /*
4856 * Some userspaces do not preserve unusable property. Since usable
4857 * segment has to be present according to VMX spec we can use present
4858 * property to amend userspace bug by making unusable segment always
4859 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4860 * segment as unusable.
4861 */
4862 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 var->avl = (ar >> 12) & 1;
4864 var->l = (ar >> 13) & 1;
4865 var->db = (ar >> 14) & 1;
4866 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867}
4868
Avi Kivitya9179492011-01-03 14:28:52 +02004869static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4870{
Avi Kivitya9179492011-01-03 14:28:52 +02004871 struct kvm_segment s;
4872
4873 if (to_vmx(vcpu)->rmode.vm86_active) {
4874 vmx_get_segment(vcpu, &s, seg);
4875 return s.base;
4876 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004877 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004878}
4879
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004880static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004881{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004882 struct vcpu_vmx *vmx = to_vmx(vcpu);
4883
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004884 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004885 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004886 else {
4887 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004888 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004889 }
Avi Kivity69c73022011-03-07 15:26:44 +02004890}
4891
Avi Kivity653e3102007-05-07 10:55:37 +03004892static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004894 u32 ar;
4895
Avi Kivityf0495f92012-06-07 17:06:10 +03004896 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004897 ar = 1 << 16;
4898 else {
4899 ar = var->type & 15;
4900 ar |= (var->s & 1) << 4;
4901 ar |= (var->dpl & 3) << 5;
4902 ar |= (var->present & 1) << 7;
4903 ar |= (var->avl & 1) << 12;
4904 ar |= (var->l & 1) << 13;
4905 ar |= (var->db & 1) << 14;
4906 ar |= (var->g & 1) << 15;
4907 }
Avi Kivity653e3102007-05-07 10:55:37 +03004908
4909 return ar;
4910}
4911
4912static void vmx_set_segment(struct kvm_vcpu *vcpu,
4913 struct kvm_segment *var, int seg)
4914{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004915 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004916 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004917
Avi Kivity2fb92db2011-04-27 19:42:18 +03004918 vmx_segment_cache_clear(vmx);
4919
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004920 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4921 vmx->rmode.segs[seg] = *var;
4922 if (seg == VCPU_SREG_TR)
4923 vmcs_write16(sf->selector, var->selector);
4924 else if (var->s)
4925 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004926 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004927 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004928
Avi Kivity653e3102007-05-07 10:55:37 +03004929 vmcs_writel(sf->base, var->base);
4930 vmcs_write32(sf->limit, var->limit);
4931 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004932
4933 /*
4934 * Fix the "Accessed" bit in AR field of segment registers for older
4935 * qemu binaries.
4936 * IA32 arch specifies that at the time of processor reset the
4937 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004938 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004939 * state vmexit when "unrestricted guest" mode is turned on.
4940 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4941 * tree. Newer qemu binaries with that qemu fix would not need this
4942 * kvm hack.
4943 */
4944 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004945 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004946
Gleb Natapovf924d662012-12-12 19:10:55 +02004947 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004948
4949out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004950 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951}
4952
Avi Kivity6aa8b732006-12-10 02:21:36 -08004953static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4954{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004955 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004956
4957 *db = (ar >> 14) & 1;
4958 *l = (ar >> 13) & 1;
4959}
4960
Gleb Natapov89a27f42010-02-16 10:51:48 +02004961static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004962{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004963 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4964 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965}
4966
Gleb Natapov89a27f42010-02-16 10:51:48 +02004967static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004969 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4970 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971}
4972
Gleb Natapov89a27f42010-02-16 10:51:48 +02004973static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004975 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4976 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977}
4978
Gleb Natapov89a27f42010-02-16 10:51:48 +02004979static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004981 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4982 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983}
4984
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004985static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4986{
4987 struct kvm_segment var;
4988 u32 ar;
4989
4990 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004991 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004992 if (seg == VCPU_SREG_CS)
4993 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004994 ar = vmx_segment_access_rights(&var);
4995
4996 if (var.base != (var.selector << 4))
4997 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004998 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004999 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005000 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005001 return false;
5002
5003 return true;
5004}
5005
5006static bool code_segment_valid(struct kvm_vcpu *vcpu)
5007{
5008 struct kvm_segment cs;
5009 unsigned int cs_rpl;
5010
5011 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005012 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005013
Avi Kivity1872a3f2009-01-04 23:26:52 +02005014 if (cs.unusable)
5015 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005016 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005017 return false;
5018 if (!cs.s)
5019 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005020 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005021 if (cs.dpl > cs_rpl)
5022 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005023 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005024 if (cs.dpl != cs_rpl)
5025 return false;
5026 }
5027 if (!cs.present)
5028 return false;
5029
5030 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5031 return true;
5032}
5033
5034static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5035{
5036 struct kvm_segment ss;
5037 unsigned int ss_rpl;
5038
5039 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005040 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005041
Avi Kivity1872a3f2009-01-04 23:26:52 +02005042 if (ss.unusable)
5043 return true;
5044 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005045 return false;
5046 if (!ss.s)
5047 return false;
5048 if (ss.dpl != ss_rpl) /* DPL != RPL */
5049 return false;
5050 if (!ss.present)
5051 return false;
5052
5053 return true;
5054}
5055
5056static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5057{
5058 struct kvm_segment var;
5059 unsigned int rpl;
5060
5061 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005062 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005063
Avi Kivity1872a3f2009-01-04 23:26:52 +02005064 if (var.unusable)
5065 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005066 if (!var.s)
5067 return false;
5068 if (!var.present)
5069 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005070 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005071 if (var.dpl < rpl) /* DPL < RPL */
5072 return false;
5073 }
5074
5075 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5076 * rights flags
5077 */
5078 return true;
5079}
5080
5081static bool tr_valid(struct kvm_vcpu *vcpu)
5082{
5083 struct kvm_segment tr;
5084
5085 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5086
Avi Kivity1872a3f2009-01-04 23:26:52 +02005087 if (tr.unusable)
5088 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005089 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005090 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005091 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005092 return false;
5093 if (!tr.present)
5094 return false;
5095
5096 return true;
5097}
5098
5099static bool ldtr_valid(struct kvm_vcpu *vcpu)
5100{
5101 struct kvm_segment ldtr;
5102
5103 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5104
Avi Kivity1872a3f2009-01-04 23:26:52 +02005105 if (ldtr.unusable)
5106 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005107 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005108 return false;
5109 if (ldtr.type != 2)
5110 return false;
5111 if (!ldtr.present)
5112 return false;
5113
5114 return true;
5115}
5116
5117static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5118{
5119 struct kvm_segment cs, ss;
5120
5121 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5122 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5123
Nadav Amitb32a9912015-03-29 16:33:04 +03005124 return ((cs.selector & SEGMENT_RPL_MASK) ==
5125 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005126}
5127
5128/*
5129 * Check if guest state is valid. Returns true if valid, false if
5130 * not.
5131 * We assume that registers are always usable
5132 */
5133static bool guest_state_valid(struct kvm_vcpu *vcpu)
5134{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005135 if (enable_unrestricted_guest)
5136 return true;
5137
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005138 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005139 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005140 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5141 return false;
5142 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5143 return false;
5144 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5145 return false;
5146 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5147 return false;
5148 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5149 return false;
5150 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5151 return false;
5152 } else {
5153 /* protected mode guest state checks */
5154 if (!cs_ss_rpl_check(vcpu))
5155 return false;
5156 if (!code_segment_valid(vcpu))
5157 return false;
5158 if (!stack_segment_valid(vcpu))
5159 return false;
5160 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5161 return false;
5162 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5163 return false;
5164 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5165 return false;
5166 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5167 return false;
5168 if (!tr_valid(vcpu))
5169 return false;
5170 if (!ldtr_valid(vcpu))
5171 return false;
5172 }
5173 /* TODO:
5174 * - Add checks on RIP
5175 * - Add checks on RFLAGS
5176 */
5177
5178 return true;
5179}
5180
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005181static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5182{
5183 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5184}
5185
Mike Dayd77c26f2007-10-08 09:02:08 -04005186static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005187{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005188 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005189 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005190 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005191
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005192 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005193 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005194 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5195 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005196 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005197 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005198 r = kvm_write_guest_page(kvm, fn++, &data,
5199 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005200 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005201 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005202 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5203 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005204 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005205 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5206 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005207 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005208 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005209 r = kvm_write_guest_page(kvm, fn, &data,
5210 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5211 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005212out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005213 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005214 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005215}
5216
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005217static int init_rmode_identity_map(struct kvm *kvm)
5218{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005219 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005220 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005221 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005222 u32 tmp;
5223
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005224 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005225 mutex_lock(&kvm->slots_lock);
5226
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005227 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005228 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005229
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005230 if (!kvm_vmx->ept_identity_map_addr)
5231 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5232 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005233
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005234 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005235 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005236 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005237 goto out2;
5238
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005239 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005240 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5241 if (r < 0)
5242 goto out;
5243 /* Set up identity-mapping pagetable for EPT in real mode */
5244 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5245 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5246 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5247 r = kvm_write_guest_page(kvm, identity_map_pfn,
5248 &tmp, i * sizeof(tmp), sizeof(tmp));
5249 if (r < 0)
5250 goto out;
5251 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005252 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005253
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005254out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005255 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005256
5257out2:
5258 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005259 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005260}
5261
Avi Kivity6aa8b732006-12-10 02:21:36 -08005262static void seg_setup(int seg)
5263{
Mathias Krause772e0312012-08-30 01:30:19 +02005264 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005265 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266
5267 vmcs_write16(sf->selector, 0);
5268 vmcs_writel(sf->base, 0);
5269 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005270 ar = 0x93;
5271 if (seg == VCPU_SREG_CS)
5272 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005273
5274 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005275}
5276
Sheng Yangf78e0e22007-10-29 09:40:42 +08005277static int alloc_apic_access_page(struct kvm *kvm)
5278{
Xiao Guangrong44841412012-09-07 14:14:20 +08005279 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005280 int r = 0;
5281
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005282 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005283 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005284 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005285 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5286 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005287 if (r)
5288 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005289
Tang Chen73a6d942014-09-11 13:38:00 +08005290 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005291 if (is_error_page(page)) {
5292 r = -EFAULT;
5293 goto out;
5294 }
5295
Tang Chenc24ae0d2014-09-24 15:57:58 +08005296 /*
5297 * Do not pin the page in memory, so that memory hot-unplug
5298 * is able to migrate it.
5299 */
5300 put_page(page);
5301 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005302out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005303 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005304 return r;
5305}
5306
Wanpeng Li991e7a02015-09-16 17:30:05 +08005307static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005308{
5309 int vpid;
5310
Avi Kivity919818a2009-03-23 18:01:29 +02005311 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005312 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005313 spin_lock(&vmx_vpid_lock);
5314 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005315 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005316 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005317 else
5318 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005319 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005320 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005321}
5322
Wanpeng Li991e7a02015-09-16 17:30:05 +08005323static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005324{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005325 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005326 return;
5327 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005328 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005329 spin_unlock(&vmx_vpid_lock);
5330}
5331
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005332static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5333 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005334{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005335 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005336
5337 if (!cpu_has_vmx_msr_bitmap())
5338 return;
5339
5340 /*
5341 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5342 * have the write-low and read-high bitmap offsets the wrong way round.
5343 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5344 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005345 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005346 if (type & MSR_TYPE_R)
5347 /* read-low */
5348 __clear_bit(msr, msr_bitmap + 0x000 / f);
5349
5350 if (type & MSR_TYPE_W)
5351 /* write-low */
5352 __clear_bit(msr, msr_bitmap + 0x800 / f);
5353
Sheng Yang25c5f222008-03-28 13:18:56 +08005354 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5355 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005356 if (type & MSR_TYPE_R)
5357 /* read-high */
5358 __clear_bit(msr, msr_bitmap + 0x400 / f);
5359
5360 if (type & MSR_TYPE_W)
5361 /* write-high */
5362 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5363
5364 }
5365}
5366
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005367static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5368 u32 msr, int type)
5369{
5370 int f = sizeof(unsigned long);
5371
5372 if (!cpu_has_vmx_msr_bitmap())
5373 return;
5374
5375 /*
5376 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5377 * have the write-low and read-high bitmap offsets the wrong way round.
5378 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5379 */
5380 if (msr <= 0x1fff) {
5381 if (type & MSR_TYPE_R)
5382 /* read-low */
5383 __set_bit(msr, msr_bitmap + 0x000 / f);
5384
5385 if (type & MSR_TYPE_W)
5386 /* write-low */
5387 __set_bit(msr, msr_bitmap + 0x800 / f);
5388
5389 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5390 msr &= 0x1fff;
5391 if (type & MSR_TYPE_R)
5392 /* read-high */
5393 __set_bit(msr, msr_bitmap + 0x400 / f);
5394
5395 if (type & MSR_TYPE_W)
5396 /* write-high */
5397 __set_bit(msr, msr_bitmap + 0xc00 / f);
5398
5399 }
5400}
5401
5402static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5403 u32 msr, int type, bool value)
5404{
5405 if (value)
5406 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5407 else
5408 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5409}
5410
Wincy Vanf2b93282015-02-03 23:56:03 +08005411/*
5412 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5413 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5414 */
5415static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5416 unsigned long *msr_bitmap_nested,
5417 u32 msr, int type)
5418{
5419 int f = sizeof(unsigned long);
5420
Wincy Vanf2b93282015-02-03 23:56:03 +08005421 /*
5422 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5423 * have the write-low and read-high bitmap offsets the wrong way round.
5424 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5425 */
5426 if (msr <= 0x1fff) {
5427 if (type & MSR_TYPE_R &&
5428 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5429 /* read-low */
5430 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5431
5432 if (type & MSR_TYPE_W &&
5433 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5434 /* write-low */
5435 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5436
5437 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5438 msr &= 0x1fff;
5439 if (type & MSR_TYPE_R &&
5440 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5441 /* read-high */
5442 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5443
5444 if (type & MSR_TYPE_W &&
5445 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5446 /* write-high */
5447 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5448
5449 }
5450}
5451
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005452static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005453{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005454 u8 mode = 0;
5455
5456 if (cpu_has_secondary_exec_ctrls() &&
5457 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5458 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5459 mode |= MSR_BITMAP_MODE_X2APIC;
5460 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5461 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5462 }
5463
5464 if (is_long_mode(vcpu))
5465 mode |= MSR_BITMAP_MODE_LM;
5466
5467 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005468}
5469
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005470#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5471
5472static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5473 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005474{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005475 int msr;
5476
5477 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5478 unsigned word = msr / BITS_PER_LONG;
5479 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5480 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005481 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005482
5483 if (mode & MSR_BITMAP_MODE_X2APIC) {
5484 /*
5485 * TPR reads and writes can be virtualized even if virtual interrupt
5486 * delivery is not in use.
5487 */
5488 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5489 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5490 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5491 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5492 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5493 }
5494 }
5495}
5496
5497static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5498{
5499 struct vcpu_vmx *vmx = to_vmx(vcpu);
5500 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5501 u8 mode = vmx_msr_bitmap_mode(vcpu);
5502 u8 changed = mode ^ vmx->msr_bitmap_mode;
5503
5504 if (!changed)
5505 return;
5506
5507 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5508 !(mode & MSR_BITMAP_MODE_LM));
5509
5510 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5511 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5512
5513 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005514}
5515
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005516static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005517{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005518 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005519}
5520
David Matlackc9f04402017-08-01 14:00:40 -07005521static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5522{
5523 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5524 gfn_t gfn;
5525
5526 /*
5527 * Don't need to mark the APIC access page dirty; it is never
5528 * written to by the CPU during APIC virtualization.
5529 */
5530
5531 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5532 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5533 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5534 }
5535
5536 if (nested_cpu_has_posted_intr(vmcs12)) {
5537 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5538 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5539 }
5540}
5541
5542
David Hildenbrand6342c502017-01-25 11:58:58 +01005543static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005544{
5545 struct vcpu_vmx *vmx = to_vmx(vcpu);
5546 int max_irr;
5547 void *vapic_page;
5548 u16 status;
5549
David Matlackc9f04402017-08-01 14:00:40 -07005550 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5551 return;
Wincy Van705699a2015-02-03 23:58:17 +08005552
David Matlackc9f04402017-08-01 14:00:40 -07005553 vmx->nested.pi_pending = false;
5554 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5555 return;
Wincy Van705699a2015-02-03 23:58:17 +08005556
David Matlackc9f04402017-08-01 14:00:40 -07005557 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5558 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005559 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005560 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5561 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005562 kunmap(vmx->nested.virtual_apic_page);
5563
5564 status = vmcs_read16(GUEST_INTR_STATUS);
5565 if ((u8)max_irr > ((u8)status & 0xff)) {
5566 status &= ~0xff;
5567 status |= (u8)max_irr;
5568 vmcs_write16(GUEST_INTR_STATUS, status);
5569 }
5570 }
David Matlackc9f04402017-08-01 14:00:40 -07005571
5572 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005573}
5574
Wincy Van06a55242017-04-28 13:13:59 +08005575static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5576 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005577{
5578#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005579 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5580
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005581 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005582 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005583 * The vector of interrupt to be delivered to vcpu had
5584 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005585 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005586 * Following cases will be reached in this block, and
5587 * we always send a notification event in all cases as
5588 * explained below.
5589 *
5590 * Case 1: vcpu keeps in non-root mode. Sending a
5591 * notification event posts the interrupt to vcpu.
5592 *
5593 * Case 2: vcpu exits to root mode and is still
5594 * runnable. PIR will be synced to vIRR before the
5595 * next vcpu entry. Sending a notification event in
5596 * this case has no effect, as vcpu is not in root
5597 * mode.
5598 *
5599 * Case 3: vcpu exits to root mode and is blocked.
5600 * vcpu_block() has already synced PIR to vIRR and
5601 * never blocks vcpu if vIRR is not cleared. Therefore,
5602 * a blocked vcpu here does not wait for any requested
5603 * interrupts in PIR, and sending a notification event
5604 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005605 */
Feng Wu28b835d2015-09-18 22:29:54 +08005606
Wincy Van06a55242017-04-28 13:13:59 +08005607 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005608 return true;
5609 }
5610#endif
5611 return false;
5612}
5613
Wincy Van705699a2015-02-03 23:58:17 +08005614static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5615 int vector)
5616{
5617 struct vcpu_vmx *vmx = to_vmx(vcpu);
5618
5619 if (is_guest_mode(vcpu) &&
5620 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005621 /*
5622 * If a posted intr is not recognized by hardware,
5623 * we will accomplish it in the next vmentry.
5624 */
5625 vmx->nested.pi_pending = true;
5626 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005627 /* the PIR and ON have been set by L1. */
5628 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5629 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005630 return 0;
5631 }
5632 return -1;
5633}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005634/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005635 * Send interrupt to vcpu via posted interrupt way.
5636 * 1. If target vcpu is running(non-root mode), send posted interrupt
5637 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5638 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5639 * interrupt from PIR in next vmentry.
5640 */
5641static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5642{
5643 struct vcpu_vmx *vmx = to_vmx(vcpu);
5644 int r;
5645
Wincy Van705699a2015-02-03 23:58:17 +08005646 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5647 if (!r)
5648 return;
5649
Yang Zhanga20ed542013-04-11 19:25:15 +08005650 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5651 return;
5652
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005653 /* If a previous notification has sent the IPI, nothing to do. */
5654 if (pi_test_and_set_on(&vmx->pi_desc))
5655 return;
5656
Wincy Van06a55242017-04-28 13:13:59 +08005657 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005658 kvm_vcpu_kick(vcpu);
5659}
5660
Avi Kivity6aa8b732006-12-10 02:21:36 -08005661/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005662 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5663 * will not change in the lifetime of the guest.
5664 * Note that host-state that does change is set elsewhere. E.g., host-state
5665 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5666 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005667static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005668{
5669 u32 low32, high32;
5670 unsigned long tmpl;
5671 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005672 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005673
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005674 cr0 = read_cr0();
5675 WARN_ON(cr0 & X86_CR0_TS);
5676 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005677
5678 /*
5679 * Save the most likely value for this task's CR3 in the VMCS.
5680 * We can't use __get_current_cr3_fast() because we're not atomic.
5681 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005682 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005683 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005684 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005685
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005686 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005687 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005688 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005689 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005690
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005691 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005692#ifdef CONFIG_X86_64
5693 /*
5694 * Load null selectors, so we can avoid reloading them in
5695 * __vmx_load_host_state(), in case userspace uses the null selectors
5696 * too (the expected case).
5697 */
5698 vmcs_write16(HOST_DS_SELECTOR, 0);
5699 vmcs_write16(HOST_ES_SELECTOR, 0);
5700#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005701 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5702 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005703#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005704 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5705 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5706
Juergen Gross87930012017-09-04 12:25:27 +02005707 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005708 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005709 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005710
Avi Kivity83287ea422012-09-16 15:10:57 +03005711 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005712
5713 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5714 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5715 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5716 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5717
5718 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5719 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5720 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5721 }
5722}
5723
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005724static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5725{
5726 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5727 if (enable_ept)
5728 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005729 if (is_guest_mode(&vmx->vcpu))
5730 vmx->vcpu.arch.cr4_guest_owned_bits &=
5731 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005732 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5733}
5734
Yang Zhang01e439b2013-04-11 19:25:12 +08005735static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5736{
5737 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5738
Andrey Smetanind62caab2015-11-10 15:36:33 +03005739 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005740 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005741
5742 if (!enable_vnmi)
5743 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
5744
Yunhong Jiang64672c92016-06-13 14:19:59 -07005745 /* Enable the preemption timer dynamically */
5746 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005747 return pin_based_exec_ctrl;
5748}
5749
Andrey Smetanind62caab2015-11-10 15:36:33 +03005750static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5751{
5752 struct vcpu_vmx *vmx = to_vmx(vcpu);
5753
5754 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005755 if (cpu_has_secondary_exec_ctrls()) {
5756 if (kvm_vcpu_apicv_active(vcpu))
5757 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5758 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5759 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5760 else
5761 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5762 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5763 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5764 }
5765
5766 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005767 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005768}
5769
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005770static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5771{
5772 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005773
5774 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5775 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5776
Paolo Bonzini35754c92015-07-29 12:05:37 +02005777 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005778 exec_control &= ~CPU_BASED_TPR_SHADOW;
5779#ifdef CONFIG_X86_64
5780 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5781 CPU_BASED_CR8_LOAD_EXITING;
5782#endif
5783 }
5784 if (!enable_ept)
5785 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5786 CPU_BASED_CR3_LOAD_EXITING |
5787 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07005788 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
5789 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
5790 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07005791 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
5792 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005793 return exec_control;
5794}
5795
Jim Mattson45ec3682017-08-23 16:32:04 -07005796static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005797{
Jim Mattson45ec3682017-08-23 16:32:04 -07005798 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005799 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005800}
5801
Jim Mattson75f4fc82017-08-23 16:32:03 -07005802static bool vmx_rdseed_supported(void)
5803{
5804 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02005805 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005806}
5807
Paolo Bonzini80154d72017-08-24 13:55:35 +02005808static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005809{
Paolo Bonzini80154d72017-08-24 13:55:35 +02005810 struct kvm_vcpu *vcpu = &vmx->vcpu;
5811
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005812 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005813
Paolo Bonzini80154d72017-08-24 13:55:35 +02005814 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005815 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5816 if (vmx->vpid == 0)
5817 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5818 if (!enable_ept) {
5819 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5820 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005821 /* Enable INVPCID for non-ept guests may cause performance regression. */
5822 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005823 }
5824 if (!enable_unrestricted_guest)
5825 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07005826 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005827 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02005828 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005829 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5830 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005831 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02005832
5833 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
5834 * in vmx_set_cr4. */
5835 exec_control &= ~SECONDARY_EXEC_DESC;
5836
Abel Gordonabc4fc52013-04-18 14:35:25 +03005837 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5838 (handle_vmptrld).
5839 We can NOT enable shadow_vmcs here because we don't have yet
5840 a current VMCS12
5841 */
5842 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005843
5844 if (!enable_pml)
5845 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005846
Paolo Bonzini3db13482017-08-24 14:48:03 +02005847 if (vmx_xsaves_supported()) {
5848 /* Exposing XSAVES only when XSAVE is exposed */
5849 bool xsaves_enabled =
5850 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
5851 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
5852
5853 if (!xsaves_enabled)
5854 exec_control &= ~SECONDARY_EXEC_XSAVES;
5855
5856 if (nested) {
5857 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005858 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005859 SECONDARY_EXEC_XSAVES;
5860 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005861 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02005862 ~SECONDARY_EXEC_XSAVES;
5863 }
5864 }
5865
Paolo Bonzini80154d72017-08-24 13:55:35 +02005866 if (vmx_rdtscp_supported()) {
5867 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
5868 if (!rdtscp_enabled)
5869 exec_control &= ~SECONDARY_EXEC_RDTSCP;
5870
5871 if (nested) {
5872 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005873 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005874 SECONDARY_EXEC_RDTSCP;
5875 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005876 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005877 ~SECONDARY_EXEC_RDTSCP;
5878 }
5879 }
5880
5881 if (vmx_invpcid_supported()) {
5882 /* Exposing INVPCID only when PCID is exposed */
5883 bool invpcid_enabled =
5884 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
5885 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
5886
5887 if (!invpcid_enabled) {
5888 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
5889 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
5890 }
5891
5892 if (nested) {
5893 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005894 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005895 SECONDARY_EXEC_ENABLE_INVPCID;
5896 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005897 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02005898 ~SECONDARY_EXEC_ENABLE_INVPCID;
5899 }
5900 }
5901
Jim Mattson45ec3682017-08-23 16:32:04 -07005902 if (vmx_rdrand_supported()) {
5903 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
5904 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005905 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005906
5907 if (nested) {
5908 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005909 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005910 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005911 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005912 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005913 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07005914 }
5915 }
5916
Jim Mattson75f4fc82017-08-23 16:32:03 -07005917 if (vmx_rdseed_supported()) {
5918 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
5919 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02005920 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005921
5922 if (nested) {
5923 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005924 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005925 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005926 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005927 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02005928 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07005929 }
5930 }
5931
Paolo Bonzini80154d72017-08-24 13:55:35 +02005932 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005933}
5934
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005935static void ept_set_mmio_spte_mask(void)
5936{
5937 /*
5938 * EPT Misconfigurations can be generated if the value of bits 2:0
5939 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005940 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07005941 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5942 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005943}
5944
Wanpeng Lif53cd632014-12-02 19:14:58 +08005945#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005946/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005947 * Sets up the vmcs for emulated real mode.
5948 */
David Hildenbrand12d79912017-08-24 20:51:26 +02005949static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005950{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005951#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005952 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005953#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005954 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005955
Abel Gordon4607c2d2013-04-18 14:35:55 +03005956 if (enable_shadow_vmcs) {
5957 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5958 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5959 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005960 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005961 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005962
Avi Kivity6aa8b732006-12-10 02:21:36 -08005963 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5964
Avi Kivity6aa8b732006-12-10 02:21:36 -08005965 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005966 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005967 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005968
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005969 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005970
Dan Williamsdfa169b2016-06-02 11:17:24 -07005971 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02005972 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005973 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02005974 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07005975 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005976
Andrey Smetanind62caab2015-11-10 15:36:33 +03005977 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005978 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5979 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5980 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5981 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5982
5983 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005984
Li RongQing0bcf2612015-12-03 13:29:34 +08005985 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005986 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005987 }
5988
Wanpeng Lib31c1142018-03-12 04:53:04 -07005989 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005990 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005991 vmx->ple_window = ple_window;
5992 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005993 }
5994
Xiao Guangrongc3707952011-07-12 03:28:04 +08005995 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5996 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005997 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5998
Avi Kivity9581d442010-10-19 16:46:55 +02005999 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6000 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006001 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006002#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006003 rdmsrl(MSR_FS_BASE, a);
6004 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6005 rdmsrl(MSR_GS_BASE, a);
6006 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6007#else
6008 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6009 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6010#endif
6011
Bandan Das2a499e42017-08-03 15:54:41 -04006012 if (cpu_has_vmx_vmfunc())
6013 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6014
Eddie Dong2cc51562007-05-21 07:28:09 +03006015 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6016 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006017 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006018 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006019 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006020
Radim Krčmář74545702015-04-27 15:11:25 +02006021 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6022 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006023
Paolo Bonzini03916db2014-07-24 14:21:57 +02006024 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006025 u32 index = vmx_msr_index[i];
6026 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006027 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006028
6029 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6030 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006031 if (wrmsr_safe(index, data_low, data_high) < 0)
6032 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006033 vmx->guest_msrs[j].index = i;
6034 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006035 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006036 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006037 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006038
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006039 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6040 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006041
6042 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006043
6044 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006045 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006046
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006047 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6048 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6049
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006050 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006051
Wanpeng Lif53cd632014-12-02 19:14:58 +08006052 if (vmx_xsaves_supported())
6053 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6054
Peter Feiner4e595162016-07-07 14:49:58 -07006055 if (enable_pml) {
6056 ASSERT(vmx->pml_pg);
6057 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6058 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6059 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006060}
6061
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006062static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006063{
6064 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006065 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006066 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006067
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006068 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006069 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006070
Wanpeng Li518e7b92018-02-28 14:03:31 +08006071 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006072 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006073 kvm_set_cr8(vcpu, 0);
6074
6075 if (!init_event) {
6076 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6077 MSR_IA32_APICBASE_ENABLE;
6078 if (kvm_vcpu_is_reset_bsp(vcpu))
6079 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6080 apic_base_msr.host_initiated = true;
6081 kvm_set_apic_base(vcpu, &apic_base_msr);
6082 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006083
Avi Kivity2fb92db2011-04-27 19:42:18 +03006084 vmx_segment_cache_clear(vmx);
6085
Avi Kivity5706be02008-08-20 15:07:31 +03006086 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006087 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006088 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006089
6090 seg_setup(VCPU_SREG_DS);
6091 seg_setup(VCPU_SREG_ES);
6092 seg_setup(VCPU_SREG_FS);
6093 seg_setup(VCPU_SREG_GS);
6094 seg_setup(VCPU_SREG_SS);
6095
6096 vmcs_write16(GUEST_TR_SELECTOR, 0);
6097 vmcs_writel(GUEST_TR_BASE, 0);
6098 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6099 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6100
6101 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6102 vmcs_writel(GUEST_LDTR_BASE, 0);
6103 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6104 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6105
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006106 if (!init_event) {
6107 vmcs_write32(GUEST_SYSENTER_CS, 0);
6108 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6109 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6110 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6111 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006112
Wanpeng Lic37c2872017-11-20 14:52:21 -08006113 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006114 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006115
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006116 vmcs_writel(GUEST_GDTR_BASE, 0);
6117 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6118
6119 vmcs_writel(GUEST_IDTR_BASE, 0);
6120 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6121
Anthony Liguori443381a2010-12-06 10:53:38 -06006122 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006123 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006124 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006125 if (kvm_mpx_supported())
6126 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006127
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006128 setup_msrs(vmx);
6129
Avi Kivity6aa8b732006-12-10 02:21:36 -08006130 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6131
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006132 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006133 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006134 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006135 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006136 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006137 vmcs_write32(TPR_THRESHOLD, 0);
6138 }
6139
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006140 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006141
Sheng Yang2384d2b2008-01-17 15:14:33 +08006142 if (vmx->vpid != 0)
6143 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6144
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006145 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006146 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006147 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006148 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006149 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006150
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006151 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006152
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006153 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006154 if (init_event)
6155 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006156}
6157
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006158/*
6159 * In nested virtualization, check if L1 asked to exit on external interrupts.
6160 * For most existing hypervisors, this will always return true.
6161 */
6162static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6163{
6164 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6165 PIN_BASED_EXT_INTR_MASK;
6166}
6167
Bandan Das77b0f5d2014-04-19 18:17:45 -04006168/*
6169 * In nested virtualization, check if L1 has set
6170 * VM_EXIT_ACK_INTR_ON_EXIT
6171 */
6172static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6173{
6174 return get_vmcs12(vcpu)->vm_exit_controls &
6175 VM_EXIT_ACK_INTR_ON_EXIT;
6176}
6177
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006178static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6179{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006180 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006181}
6182
Jan Kiszkac9a79532014-03-07 20:03:15 +01006183static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006184{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006185 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6186 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006187}
6188
Jan Kiszkac9a79532014-03-07 20:03:15 +01006189static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006190{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006191 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006192 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006193 enable_irq_window(vcpu);
6194 return;
6195 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006196
Paolo Bonzini47c01522016-12-19 11:44:07 +01006197 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6198 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006199}
6200
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006201static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006202{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006203 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006204 uint32_t intr;
6205 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006206
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006207 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006208
Avi Kivityfa89a812008-09-01 15:57:51 +03006209 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006210 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006211 int inc_eip = 0;
6212 if (vcpu->arch.interrupt.soft)
6213 inc_eip = vcpu->arch.event_exit_inst_len;
6214 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006215 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006216 return;
6217 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006218 intr = irq | INTR_INFO_VALID_MASK;
6219 if (vcpu->arch.interrupt.soft) {
6220 intr |= INTR_TYPE_SOFT_INTR;
6221 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6222 vmx->vcpu.arch.event_exit_inst_len);
6223 } else
6224 intr |= INTR_TYPE_EXT_INTR;
6225 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006226
6227 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006228}
6229
Sheng Yangf08864b2008-05-15 18:23:25 +08006230static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6231{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006232 struct vcpu_vmx *vmx = to_vmx(vcpu);
6233
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006234 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006235 /*
6236 * Tracking the NMI-blocked state in software is built upon
6237 * finding the next open IRQ window. This, in turn, depends on
6238 * well-behaving guests: They have to keep IRQs disabled at
6239 * least as long as the NMI handler runs. Otherwise we may
6240 * cause NMI nesting, maybe breaking the guest. But as this is
6241 * highly unlikely, we can live with the residual risk.
6242 */
6243 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6244 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6245 }
6246
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006247 ++vcpu->stat.nmi_injections;
6248 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006249
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006250 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006251 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006252 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006253 return;
6254 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006255
Sheng Yangf08864b2008-05-15 18:23:25 +08006256 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6257 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006258
6259 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006260}
6261
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006262static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6263{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006264 struct vcpu_vmx *vmx = to_vmx(vcpu);
6265 bool masked;
6266
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006267 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006268 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006269 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006270 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006271 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6272 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6273 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006274}
6275
6276static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6277{
6278 struct vcpu_vmx *vmx = to_vmx(vcpu);
6279
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006280 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006281 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6282 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6283 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6284 }
6285 } else {
6286 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6287 if (masked)
6288 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6289 GUEST_INTR_STATE_NMI);
6290 else
6291 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6292 GUEST_INTR_STATE_NMI);
6293 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006294}
6295
Jan Kiszka2505dc92013-04-14 12:12:47 +02006296static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6297{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006298 if (to_vmx(vcpu)->nested.nested_run_pending)
6299 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006300
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006301 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006302 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6303 return 0;
6304
Jan Kiszka2505dc92013-04-14 12:12:47 +02006305 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6306 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6307 | GUEST_INTR_STATE_NMI));
6308}
6309
Gleb Natapov78646122009-03-23 12:12:11 +02006310static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6311{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006312 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6313 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006314 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6315 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006316}
6317
Izik Eiduscbc94022007-10-25 00:29:55 +02006318static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6319{
6320 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006321
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006322 if (enable_unrestricted_guest)
6323 return 0;
6324
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006325 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6326 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006327 if (ret)
6328 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006329 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006330 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006331}
6332
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006333static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6334{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006335 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006336 return 0;
6337}
6338
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006339static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006340{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006341 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006342 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006343 /*
6344 * Update instruction length as we may reinject the exception
6345 * from user space while in guest debugging mode.
6346 */
6347 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6348 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006349 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006350 return false;
6351 /* fall through */
6352 case DB_VECTOR:
6353 if (vcpu->guest_debug &
6354 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6355 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006356 /* fall through */
6357 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006358 case OF_VECTOR:
6359 case BR_VECTOR:
6360 case UD_VECTOR:
6361 case DF_VECTOR:
6362 case SS_VECTOR:
6363 case GP_VECTOR:
6364 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006365 return true;
6366 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006367 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006368 return false;
6369}
6370
6371static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6372 int vec, u32 err_code)
6373{
6374 /*
6375 * Instruction with address size override prefix opcode 0x67
6376 * Cause the #SS fault with 0 error code in VM86 mode.
6377 */
6378 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6379 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6380 if (vcpu->arch.halt_request) {
6381 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006382 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006383 }
6384 return 1;
6385 }
6386 return 0;
6387 }
6388
6389 /*
6390 * Forward all other exceptions that are valid in real mode.
6391 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6392 * the required debugging infrastructure rework.
6393 */
6394 kvm_queue_exception(vcpu, vec);
6395 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006396}
6397
Andi Kleena0861c02009-06-08 17:37:09 +08006398/*
6399 * Trigger machine check on the host. We assume all the MSRs are already set up
6400 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6401 * We pass a fake environment to the machine check handler because we want
6402 * the guest to be always treated like user space, no matter what context
6403 * it used internally.
6404 */
6405static void kvm_machine_check(void)
6406{
6407#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6408 struct pt_regs regs = {
6409 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6410 .flags = X86_EFLAGS_IF,
6411 };
6412
6413 do_machine_check(&regs, 0);
6414#endif
6415}
6416
Avi Kivity851ba692009-08-24 11:10:17 +03006417static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006418{
6419 /* already handled by vcpu_run */
6420 return 1;
6421}
6422
Avi Kivity851ba692009-08-24 11:10:17 +03006423static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006424{
Avi Kivity1155f762007-11-22 11:30:47 +02006425 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006426 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006427 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006428 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006429 u32 vect_info;
6430 enum emulation_result er;
6431
Avi Kivity1155f762007-11-22 11:30:47 +02006432 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006433 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006434
Andi Kleena0861c02009-06-08 17:37:09 +08006435 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006436 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006437
Jim Mattsonef85b672016-12-12 11:01:37 -08006438 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006439 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006440
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006441 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01006442 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alon61cb57c2017-11-05 16:56:32 +02006443 if (er == EMULATE_USER_EXIT)
6444 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006445 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02006446 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006447 return 1;
6448 }
6449
Avi Kivity6aa8b732006-12-10 02:21:36 -08006450 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006451 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006452 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006453
Liran Alon9e869482018-03-12 13:12:51 +02006454 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6455 WARN_ON_ONCE(!enable_vmware_backdoor);
6456 er = emulate_instruction(vcpu,
6457 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6458 if (er == EMULATE_USER_EXIT)
6459 return 0;
6460 else if (er != EMULATE_DONE)
6461 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6462 return 1;
6463 }
6464
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006465 /*
6466 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6467 * MMIO, it is better to report an internal error.
6468 * See the comments in vmx_handle_exit.
6469 */
6470 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6471 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6472 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6473 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006474 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006475 vcpu->run->internal.data[0] = vect_info;
6476 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006477 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006478 return 0;
6479 }
6480
Avi Kivity6aa8b732006-12-10 02:21:36 -08006481 if (is_page_fault(intr_info)) {
6482 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006483 /* EPT won't cause page fault directly */
6484 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006485 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006486 }
6487
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006488 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006489
6490 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6491 return handle_rmode_exception(vcpu, ex_no, error_code);
6492
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006493 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006494 case AC_VECTOR:
6495 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6496 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006497 case DB_VECTOR:
6498 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6499 if (!(vcpu->guest_debug &
6500 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006501 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006502 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01006503 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
6504 skip_emulated_instruction(vcpu);
6505
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006506 kvm_queue_exception(vcpu, DB_VECTOR);
6507 return 1;
6508 }
6509 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6510 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6511 /* fall through */
6512 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006513 /*
6514 * Update instruction length as we may reinject #BP from
6515 * user space while in guest debugging mode. Reading it for
6516 * #DB as well causes no harm, it is not used in that case.
6517 */
6518 vmx->vcpu.arch.event_exit_inst_len =
6519 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006520 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006521 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006522 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6523 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006524 break;
6525 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006526 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6527 kvm_run->ex.exception = ex_no;
6528 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006529 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006530 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006531 return 0;
6532}
6533
Avi Kivity851ba692009-08-24 11:10:17 +03006534static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006535{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006536 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006537 return 1;
6538}
6539
Avi Kivity851ba692009-08-24 11:10:17 +03006540static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006541{
Avi Kivity851ba692009-08-24 11:10:17 +03006542 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006543 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006544 return 0;
6545}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006546
Avi Kivity851ba692009-08-24 11:10:17 +03006547static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006548{
He, Qingbfdaab02007-09-12 14:18:28 +08006549 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006550 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006551 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006552
He, Qingbfdaab02007-09-12 14:18:28 +08006553 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006554 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006555
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006556 ++vcpu->stat.io_exits;
6557
Sean Christopherson432baf62018-03-08 08:57:26 -08006558 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006559 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006560
6561 port = exit_qualification >> 16;
6562 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006563 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006564
Sean Christophersondca7f122018-03-08 08:57:27 -08006565 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006566}
6567
Ingo Molnar102d8322007-02-19 14:37:47 +02006568static void
6569vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6570{
6571 /*
6572 * Patch in the VMCALL instruction:
6573 */
6574 hypercall[0] = 0x0f;
6575 hypercall[1] = 0x01;
6576 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006577}
6578
Guo Chao0fa06072012-06-28 15:16:19 +08006579/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006580static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6581{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006582 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006583 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6584 unsigned long orig_val = val;
6585
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006586 /*
6587 * We get here when L2 changed cr0 in a way that did not change
6588 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006589 * but did change L0 shadowed bits. So we first calculate the
6590 * effective cr0 value that L1 would like to write into the
6591 * hardware. It consists of the L2-owned bits from the new
6592 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006593 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006594 val = (val & ~vmcs12->cr0_guest_host_mask) |
6595 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6596
David Matlack38991522016-11-29 18:14:08 -08006597 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006598 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006599
6600 if (kvm_set_cr0(vcpu, val))
6601 return 1;
6602 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006603 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006604 } else {
6605 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006606 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006607 return 1;
David Matlack38991522016-11-29 18:14:08 -08006608
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006609 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006610 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006611}
6612
6613static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6614{
6615 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006616 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6617 unsigned long orig_val = val;
6618
6619 /* analogously to handle_set_cr0 */
6620 val = (val & ~vmcs12->cr4_guest_host_mask) |
6621 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6622 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006623 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006624 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006625 return 0;
6626 } else
6627 return kvm_set_cr4(vcpu, val);
6628}
6629
Paolo Bonzini0367f202016-07-12 10:44:55 +02006630static int handle_desc(struct kvm_vcpu *vcpu)
6631{
6632 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6633 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6634}
6635
Avi Kivity851ba692009-08-24 11:10:17 +03006636static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006638 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006639 int cr;
6640 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006641 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006642 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006643
He, Qingbfdaab02007-09-12 14:18:28 +08006644 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006645 cr = exit_qualification & 15;
6646 reg = (exit_qualification >> 8) & 15;
6647 switch ((exit_qualification >> 4) & 3) {
6648 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006649 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006650 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006651 switch (cr) {
6652 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006653 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006654 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006655 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006656 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006657 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006658 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006659 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006660 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006661 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006662 case 8: {
6663 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006664 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006665 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006666 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006667 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006668 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006669 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006670 return ret;
6671 /*
6672 * TODO: we might be squashing a
6673 * KVM_GUESTDBG_SINGLESTEP-triggered
6674 * KVM_EXIT_DEBUG here.
6675 */
Avi Kivity851ba692009-08-24 11:10:17 +03006676 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006677 return 0;
6678 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006679 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006680 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006681 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006682 WARN_ONCE(1, "Guest should always own CR0.TS");
6683 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006684 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006685 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006686 case 1: /*mov from cr*/
6687 switch (cr) {
6688 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006689 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006690 val = kvm_read_cr3(vcpu);
6691 kvm_register_write(vcpu, reg, val);
6692 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006693 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006694 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006695 val = kvm_get_cr8(vcpu);
6696 kvm_register_write(vcpu, reg, val);
6697 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006698 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006699 }
6700 break;
6701 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006702 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006703 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006704 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006705
Kyle Huey6affcbe2016-11-29 12:40:40 -08006706 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006707 default:
6708 break;
6709 }
Avi Kivity851ba692009-08-24 11:10:17 +03006710 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006711 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006712 (int)(exit_qualification >> 4) & 3, cr);
6713 return 0;
6714}
6715
Avi Kivity851ba692009-08-24 11:10:17 +03006716static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006717{
He, Qingbfdaab02007-09-12 14:18:28 +08006718 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006719 int dr, dr7, reg;
6720
6721 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6722 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6723
6724 /* First, if DR does not exist, trigger UD */
6725 if (!kvm_require_dr(vcpu, dr))
6726 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006727
Jan Kiszkaf2483412010-01-20 18:20:20 +01006728 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006729 if (!kvm_require_cpl(vcpu, 0))
6730 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006731 dr7 = vmcs_readl(GUEST_DR7);
6732 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006733 /*
6734 * As the vm-exit takes precedence over the debug trap, we
6735 * need to emulate the latter, either for the host or the
6736 * guest debugging itself.
6737 */
6738 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006739 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006740 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006741 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006742 vcpu->run->debug.arch.exception = DB_VECTOR;
6743 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006744 return 0;
6745 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006746 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006747 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006748 kvm_queue_exception(vcpu, DB_VECTOR);
6749 return 1;
6750 }
6751 }
6752
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006753 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006754 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6755 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006756
6757 /*
6758 * No more DR vmexits; force a reload of the debug registers
6759 * and reenter on this instruction. The next vmexit will
6760 * retrieve the full state of the debug registers.
6761 */
6762 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6763 return 1;
6764 }
6765
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006766 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6767 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006768 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006769
6770 if (kvm_get_dr(vcpu, dr, &val))
6771 return 1;
6772 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006773 } else
Nadav Amit57773922014-06-18 17:19:23 +03006774 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006775 return 1;
6776
Kyle Huey6affcbe2016-11-29 12:40:40 -08006777 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006778}
6779
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006780static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6781{
6782 return vcpu->arch.dr6;
6783}
6784
6785static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6786{
6787}
6788
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006789static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6790{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006791 get_debugreg(vcpu->arch.db[0], 0);
6792 get_debugreg(vcpu->arch.db[1], 1);
6793 get_debugreg(vcpu->arch.db[2], 2);
6794 get_debugreg(vcpu->arch.db[3], 3);
6795 get_debugreg(vcpu->arch.dr6, 6);
6796 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6797
6798 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006799 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006800}
6801
Gleb Natapov020df072010-04-13 10:05:23 +03006802static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6803{
6804 vmcs_writel(GUEST_DR7, val);
6805}
6806
Avi Kivity851ba692009-08-24 11:10:17 +03006807static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006808{
Kyle Huey6a908b62016-11-29 12:40:37 -08006809 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006810}
6811
Avi Kivity851ba692009-08-24 11:10:17 +03006812static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006813{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006814 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006815 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006816
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006817 msr_info.index = ecx;
6818 msr_info.host_initiated = false;
6819 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006820 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006821 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006822 return 1;
6823 }
6824
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006825 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006826
Avi Kivity6aa8b732006-12-10 02:21:36 -08006827 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006828 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6829 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006830 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006831}
6832
Avi Kivity851ba692009-08-24 11:10:17 +03006833static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006834{
Will Auld8fe8ab42012-11-29 12:42:12 -08006835 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006836 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6837 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6838 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006839
Will Auld8fe8ab42012-11-29 12:42:12 -08006840 msr.data = data;
6841 msr.index = ecx;
6842 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006843 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006844 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006845 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006846 return 1;
6847 }
6848
Avi Kivity59200272010-01-25 19:47:02 +02006849 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006850 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006851}
6852
Avi Kivity851ba692009-08-24 11:10:17 +03006853static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006854{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006855 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006856 return 1;
6857}
6858
Avi Kivity851ba692009-08-24 11:10:17 +03006859static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006860{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006861 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6862 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006863
Avi Kivity3842d132010-07-27 12:30:24 +03006864 kvm_make_request(KVM_REQ_EVENT, vcpu);
6865
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006866 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006867 return 1;
6868}
6869
Avi Kivity851ba692009-08-24 11:10:17 +03006870static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006871{
Avi Kivityd3bef152007-06-05 15:53:05 +03006872 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006873}
6874
Avi Kivity851ba692009-08-24 11:10:17 +03006875static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006876{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006877 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006878}
6879
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006880static int handle_invd(struct kvm_vcpu *vcpu)
6881{
Andre Przywara51d8b662010-12-21 11:12:02 +01006882 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006883}
6884
Avi Kivity851ba692009-08-24 11:10:17 +03006885static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006886{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006887 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006888
6889 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006890 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006891}
6892
Avi Kivityfee84b02011-11-10 14:57:25 +02006893static int handle_rdpmc(struct kvm_vcpu *vcpu)
6894{
6895 int err;
6896
6897 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006898 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006899}
6900
Avi Kivity851ba692009-08-24 11:10:17 +03006901static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006902{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006903 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006904}
6905
Dexuan Cui2acf9232010-06-10 11:27:12 +08006906static int handle_xsetbv(struct kvm_vcpu *vcpu)
6907{
6908 u64 new_bv = kvm_read_edx_eax(vcpu);
6909 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6910
6911 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006912 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006913 return 1;
6914}
6915
Wanpeng Lif53cd632014-12-02 19:14:58 +08006916static int handle_xsaves(struct kvm_vcpu *vcpu)
6917{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006918 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006919 WARN(1, "this should never happen\n");
6920 return 1;
6921}
6922
6923static int handle_xrstors(struct kvm_vcpu *vcpu)
6924{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006925 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006926 WARN(1, "this should never happen\n");
6927 return 1;
6928}
6929
Avi Kivity851ba692009-08-24 11:10:17 +03006930static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006931{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006932 if (likely(fasteoi)) {
6933 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6934 int access_type, offset;
6935
6936 access_type = exit_qualification & APIC_ACCESS_TYPE;
6937 offset = exit_qualification & APIC_ACCESS_OFFSET;
6938 /*
6939 * Sane guest uses MOV to write EOI, with written value
6940 * not cared. So make a short-circuit here by avoiding
6941 * heavy instruction emulation.
6942 */
6943 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6944 (offset == APIC_EOI)) {
6945 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006946 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006947 }
6948 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006949 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006950}
6951
Yang Zhangc7c9c562013-01-25 10:18:51 +08006952static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6953{
6954 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6955 int vector = exit_qualification & 0xff;
6956
6957 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6958 kvm_apic_set_eoi_accelerated(vcpu, vector);
6959 return 1;
6960}
6961
Yang Zhang83d4c282013-01-25 10:18:49 +08006962static int handle_apic_write(struct kvm_vcpu *vcpu)
6963{
6964 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6965 u32 offset = exit_qualification & 0xfff;
6966
6967 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6968 kvm_apic_write_nodecode(vcpu, offset);
6969 return 1;
6970}
6971
Avi Kivity851ba692009-08-24 11:10:17 +03006972static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006973{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006974 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006975 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006976 bool has_error_code = false;
6977 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006978 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006979 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006980
6981 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006982 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006983 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006984
6985 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6986
6987 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006988 if (reason == TASK_SWITCH_GATE && idt_v) {
6989 switch (type) {
6990 case INTR_TYPE_NMI_INTR:
6991 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006992 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006993 break;
6994 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006995 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006996 kvm_clear_interrupt_queue(vcpu);
6997 break;
6998 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006999 if (vmx->idt_vectoring_info &
7000 VECTORING_INFO_DELIVER_CODE_MASK) {
7001 has_error_code = true;
7002 error_code =
7003 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7004 }
7005 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007006 case INTR_TYPE_SOFT_EXCEPTION:
7007 kvm_clear_exception_queue(vcpu);
7008 break;
7009 default:
7010 break;
7011 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007012 }
Izik Eidus37817f22008-03-24 23:14:53 +02007013 tss_selector = exit_qualification;
7014
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007015 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7016 type != INTR_TYPE_EXT_INTR &&
7017 type != INTR_TYPE_NMI_INTR))
7018 skip_emulated_instruction(vcpu);
7019
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007020 if (kvm_task_switch(vcpu, tss_selector,
7021 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7022 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007023 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7024 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7025 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007026 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007027 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007028
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007029 /*
7030 * TODO: What about debug traps on tss switch?
7031 * Are we supposed to inject them and update dr6?
7032 */
7033
7034 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007035}
7036
Avi Kivity851ba692009-08-24 11:10:17 +03007037static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007038{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007039 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007040 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007041 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007042
Sheng Yangf9c617f2009-03-25 10:08:52 +08007043 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007044
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007045 /*
7046 * EPT violation happened while executing iret from NMI,
7047 * "blocked by NMI" bit has to be set before next VM entry.
7048 * There are errata that may cause this bit to not be set:
7049 * AAK134, BY25.
7050 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007051 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007052 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007053 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007054 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7055
Sheng Yang14394422008-04-28 12:24:45 +08007056 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007057 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007058
Junaid Shahid27959a42016-12-06 16:46:10 -08007059 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007060 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007061 ? PFERR_USER_MASK : 0;
7062 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007063 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007064 ? PFERR_WRITE_MASK : 0;
7065 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007066 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007067 ? PFERR_FETCH_MASK : 0;
7068 /* ept page table entry is present? */
7069 error_code |= (exit_qualification &
7070 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7071 EPT_VIOLATION_EXECUTABLE))
7072 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007073
Paolo Bonzinieebed242016-11-28 14:39:58 +01007074 error_code |= (exit_qualification & 0x100) != 0 ?
7075 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007076
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007077 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007078 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007079}
7080
Avi Kivity851ba692009-08-24 11:10:17 +03007081static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007082{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007083 gpa_t gpa;
7084
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007085 /*
7086 * A nested guest cannot optimize MMIO vmexits, because we have an
7087 * nGPA here instead of the required GPA.
7088 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007089 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007090 if (!is_guest_mode(vcpu) &&
7091 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007092 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007093 /*
7094 * Doing kvm_skip_emulated_instruction() depends on undefined
7095 * behavior: Intel's manual doesn't mandate
7096 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7097 * occurs and while on real hardware it was observed to be set,
7098 * other hypervisors (namely Hyper-V) don't set it, we end up
7099 * advancing IP with some random value. Disable fast mmio when
7100 * running nested and keep it for real hardware in hope that
7101 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7102 */
7103 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7104 return kvm_skip_emulated_instruction(vcpu);
7105 else
7106 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7107 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007108 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007109
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007110 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007111}
7112
Avi Kivity851ba692009-08-24 11:10:17 +03007113static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007114{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007115 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007116 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7117 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007118 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007119 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007120
7121 return 1;
7122}
7123
Mohammed Gamal80ced182009-09-01 12:48:18 +02007124static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007125{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007126 struct vcpu_vmx *vmx = to_vmx(vcpu);
7127 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007128 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007129 u32 cpu_exec_ctrl;
7130 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007131 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007132
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007133 /*
7134 * We should never reach the point where we are emulating L2
7135 * due to invalid guest state as that means we incorrectly
7136 * allowed a nested VMEntry with an invalid vmcs12.
7137 */
7138 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7139
Avi Kivity49e9d552010-09-19 14:34:08 +02007140 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7141 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007142
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007143 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007144 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007145 return handle_interrupt_window(&vmx->vcpu);
7146
Radim Krčmář72875d82017-04-26 22:32:19 +02007147 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007148 return 1;
7149
Liran Alon9b8ae632017-11-05 16:56:34 +02007150 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007151
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007152 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007153 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007154 ret = 0;
7155 goto out;
7156 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007157
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007158 if (err != EMULATE_DONE)
7159 goto emulation_error;
7160
7161 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7162 vcpu->arch.exception.pending)
7163 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007164
Gleb Natapov8d76c492013-05-08 18:38:44 +03007165 if (vcpu->arch.halt_request) {
7166 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007167 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007168 goto out;
7169 }
7170
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007171 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007172 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007173 if (need_resched())
7174 schedule();
7175 }
7176
Mohammed Gamal80ced182009-09-01 12:48:18 +02007177out:
7178 return ret;
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007179
7180emulation_error:
7181 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7182 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7183 vcpu->run->internal.ndata = 0;
7184 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007185}
7186
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007187static void grow_ple_window(struct kvm_vcpu *vcpu)
7188{
7189 struct vcpu_vmx *vmx = to_vmx(vcpu);
7190 int old = vmx->ple_window;
7191
Babu Mogerc8e88712018-03-16 16:37:24 -04007192 vmx->ple_window = __grow_ple_window(old, ple_window,
7193 ple_window_grow,
7194 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007195
7196 if (vmx->ple_window != old)
7197 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007198
7199 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007200}
7201
7202static void shrink_ple_window(struct kvm_vcpu *vcpu)
7203{
7204 struct vcpu_vmx *vmx = to_vmx(vcpu);
7205 int old = vmx->ple_window;
7206
Babu Mogerc8e88712018-03-16 16:37:24 -04007207 vmx->ple_window = __shrink_ple_window(old, ple_window,
7208 ple_window_shrink,
7209 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007210
7211 if (vmx->ple_window != old)
7212 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007213
7214 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007215}
7216
7217/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007218 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7219 */
7220static void wakeup_handler(void)
7221{
7222 struct kvm_vcpu *vcpu;
7223 int cpu = smp_processor_id();
7224
7225 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7226 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7227 blocked_vcpu_list) {
7228 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7229
7230 if (pi_test_on(pi_desc) == 1)
7231 kvm_vcpu_kick(vcpu);
7232 }
7233 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7234}
7235
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007236void vmx_enable_tdp(void)
7237{
7238 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7239 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7240 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7241 0ull, VMX_EPT_EXECUTABLE_MASK,
7242 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007243 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007244
7245 ept_set_mmio_spte_mask();
7246 kvm_enable_tdp();
7247}
7248
Tiejun Chenf2c76482014-10-28 10:14:47 +08007249static __init int hardware_setup(void)
7250{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007251 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007252
7253 rdmsrl_safe(MSR_EFER, &host_efer);
7254
7255 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7256 kvm_define_shared_msr(i, vmx_msr_index[i]);
7257
Radim Krčmář23611332016-09-29 22:41:33 +02007258 for (i = 0; i < VMX_BITMAP_NR; i++) {
7259 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7260 if (!vmx_bitmap[i])
7261 goto out;
7262 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007263
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007264 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7265 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7266
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007267 if (setup_vmcs_config(&vmcs_config) < 0) {
7268 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007269 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007270 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007271
7272 if (boot_cpu_has(X86_FEATURE_NX))
7273 kvm_enable_efer_bits(EFER_NX);
7274
Wanpeng Li08d839c2017-03-23 05:30:08 -07007275 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7276 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007277 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007278
Tiejun Chenf2c76482014-10-28 10:14:47 +08007279 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007280 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007281 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007282 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007283 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007284
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007285 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007286 enable_ept_ad_bits = 0;
7287
Wanpeng Li8ad81822017-10-09 15:51:53 -07007288 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007289 enable_unrestricted_guest = 0;
7290
Paolo Bonziniad15a292015-01-30 16:18:49 +01007291 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007292 flexpriority_enabled = 0;
7293
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007294 if (!cpu_has_virtual_nmis())
7295 enable_vnmi = 0;
7296
Paolo Bonziniad15a292015-01-30 16:18:49 +01007297 /*
7298 * set_apic_access_page_addr() is used to reload apic access
7299 * page upon invalidation. No need to do anything if not
7300 * using the APIC_ACCESS_ADDR VMCS field.
7301 */
7302 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007303 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007304
7305 if (!cpu_has_vmx_tpr_shadow())
7306 kvm_x86_ops->update_cr8_intercept = NULL;
7307
7308 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7309 kvm_disable_largepages();
7310
Wanpeng Li0f107682017-09-28 18:06:24 -07007311 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007312 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007313 ple_window = 0;
7314 ple_window_grow = 0;
7315 ple_window_max = 0;
7316 ple_window_shrink = 0;
7317 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007318
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007319 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007320 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007321 kvm_x86_ops->sync_pir_to_irr = NULL;
7322 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007323
Haozhong Zhang64903d62015-10-20 15:39:09 +08007324 if (cpu_has_vmx_tsc_scaling()) {
7325 kvm_has_tsc_control = true;
7326 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7327 kvm_tsc_scaling_ratio_frac_bits = 48;
7328 }
7329
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007330 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7331
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007332 if (enable_ept)
7333 vmx_enable_tdp();
7334 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007335 kvm_disable_tdp();
7336
Kai Huang843e4332015-01-28 10:54:28 +08007337 /*
7338 * Only enable PML when hardware supports PML feature, and both EPT
7339 * and EPT A/D bit features are enabled -- PML depends on them to work.
7340 */
7341 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7342 enable_pml = 0;
7343
7344 if (!enable_pml) {
7345 kvm_x86_ops->slot_enable_log_dirty = NULL;
7346 kvm_x86_ops->slot_disable_log_dirty = NULL;
7347 kvm_x86_ops->flush_log_dirty = NULL;
7348 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7349 }
7350
Yunhong Jiang64672c92016-06-13 14:19:59 -07007351 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7352 u64 vmx_msr;
7353
7354 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7355 cpu_preemption_timer_multi =
7356 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7357 } else {
7358 kvm_x86_ops->set_hv_timer = NULL;
7359 kvm_x86_ops->cancel_hv_timer = NULL;
7360 }
7361
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007362 if (!cpu_has_vmx_shadow_vmcs())
7363 enable_shadow_vmcs = 0;
7364 if (enable_shadow_vmcs)
7365 init_vmcs_shadow_fields();
7366
Feng Wubf9f6ac2015-09-18 22:29:55 +08007367 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007368 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007369
Ashok Rajc45dcc72016-06-22 14:59:56 +08007370 kvm_mce_cap_supported |= MCG_LMCE_P;
7371
Tiejun Chenf2c76482014-10-28 10:14:47 +08007372 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007373
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007374out:
Radim Krčmář23611332016-09-29 22:41:33 +02007375 for (i = 0; i < VMX_BITMAP_NR; i++)
7376 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007377
7378 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007379}
7380
7381static __exit void hardware_unsetup(void)
7382{
Radim Krčmář23611332016-09-29 22:41:33 +02007383 int i;
7384
7385 for (i = 0; i < VMX_BITMAP_NR; i++)
7386 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007387
Tiejun Chenf2c76482014-10-28 10:14:47 +08007388 free_kvm_area();
7389}
7390
Avi Kivity6aa8b732006-12-10 02:21:36 -08007391/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007392 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7393 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7394 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007395static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007396{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007397 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007398 grow_ple_window(vcpu);
7399
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007400 /*
7401 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7402 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7403 * never set PAUSE_EXITING and just set PLE if supported,
7404 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7405 */
7406 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007407 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007408}
7409
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007410static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007411{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007412 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007413}
7414
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007415static int handle_mwait(struct kvm_vcpu *vcpu)
7416{
7417 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7418 return handle_nop(vcpu);
7419}
7420
Jim Mattson45ec3682017-08-23 16:32:04 -07007421static int handle_invalid_op(struct kvm_vcpu *vcpu)
7422{
7423 kvm_queue_exception(vcpu, UD_VECTOR);
7424 return 1;
7425}
7426
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007427static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7428{
7429 return 1;
7430}
7431
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007432static int handle_monitor(struct kvm_vcpu *vcpu)
7433{
7434 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7435 return handle_nop(vcpu);
7436}
7437
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007438/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007439 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7440 * set the success or error code of an emulated VMX instruction, as specified
7441 * by Vol 2B, VMX Instruction Reference, "Conventions".
7442 */
7443static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7444{
7445 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7446 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7447 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7448}
7449
7450static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7451{
7452 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7453 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7454 X86_EFLAGS_SF | X86_EFLAGS_OF))
7455 | X86_EFLAGS_CF);
7456}
7457
Abel Gordon145c28d2013-04-18 14:36:55 +03007458static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007459 u32 vm_instruction_error)
7460{
7461 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7462 /*
7463 * failValid writes the error number to the current VMCS, which
7464 * can't be done there isn't a current VMCS.
7465 */
7466 nested_vmx_failInvalid(vcpu);
7467 return;
7468 }
7469 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7470 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7471 X86_EFLAGS_SF | X86_EFLAGS_OF))
7472 | X86_EFLAGS_ZF);
7473 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7474 /*
7475 * We don't need to force a shadow sync because
7476 * VM_INSTRUCTION_ERROR is not shadowed
7477 */
7478}
Abel Gordon145c28d2013-04-18 14:36:55 +03007479
Wincy Vanff651cb2014-12-11 08:52:58 +03007480static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7481{
7482 /* TODO: not to reset guest simply here. */
7483 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007484 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007485}
7486
Jan Kiszkaf41245002014-03-07 20:03:13 +01007487static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7488{
7489 struct vcpu_vmx *vmx =
7490 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7491
7492 vmx->nested.preemption_timer_expired = true;
7493 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7494 kvm_vcpu_kick(&vmx->vcpu);
7495
7496 return HRTIMER_NORESTART;
7497}
7498
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007499/*
Bandan Das19677e32014-05-06 02:19:15 -04007500 * Decode the memory-address operand of a vmx instruction, as recorded on an
7501 * exit caused by such an instruction (run by a guest hypervisor).
7502 * On success, returns 0. When the operand is invalid, returns 1 and throws
7503 * #UD or #GP.
7504 */
7505static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7506 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007507 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007508{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007509 gva_t off;
7510 bool exn;
7511 struct kvm_segment s;
7512
Bandan Das19677e32014-05-06 02:19:15 -04007513 /*
7514 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7515 * Execution", on an exit, vmx_instruction_info holds most of the
7516 * addressing components of the operand. Only the displacement part
7517 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7518 * For how an actual address is calculated from all these components,
7519 * refer to Vol. 1, "Operand Addressing".
7520 */
7521 int scaling = vmx_instruction_info & 3;
7522 int addr_size = (vmx_instruction_info >> 7) & 7;
7523 bool is_reg = vmx_instruction_info & (1u << 10);
7524 int seg_reg = (vmx_instruction_info >> 15) & 7;
7525 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7526 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7527 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7528 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7529
7530 if (is_reg) {
7531 kvm_queue_exception(vcpu, UD_VECTOR);
7532 return 1;
7533 }
7534
7535 /* Addr = segment_base + offset */
7536 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007537 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007538 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007539 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007540 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007541 off += kvm_register_read(vcpu, index_reg)<<scaling;
7542 vmx_get_segment(vcpu, &s, seg_reg);
7543 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007544
7545 if (addr_size == 1) /* 32 bit */
7546 *ret &= 0xffffffff;
7547
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007548 /* Checks for #GP/#SS exceptions. */
7549 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007550 if (is_long_mode(vcpu)) {
7551 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7552 * non-canonical form. This is the only check on the memory
7553 * destination for long mode!
7554 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007555 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007556 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007557 /* Protected mode: apply checks for segment validity in the
7558 * following order:
7559 * - segment type check (#GP(0) may be thrown)
7560 * - usability check (#GP(0)/#SS(0))
7561 * - limit check (#GP(0)/#SS(0))
7562 */
7563 if (wr)
7564 /* #GP(0) if the destination operand is located in a
7565 * read-only data segment or any code segment.
7566 */
7567 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7568 else
7569 /* #GP(0) if the source operand is located in an
7570 * execute-only code segment
7571 */
7572 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007573 if (exn) {
7574 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7575 return 1;
7576 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007577 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7578 */
7579 exn = (s.unusable != 0);
7580 /* Protected mode: #GP(0)/#SS(0) if the memory
7581 * operand is outside the segment limit.
7582 */
7583 exn = exn || (off + sizeof(u64) > s.limit);
7584 }
7585 if (exn) {
7586 kvm_queue_exception_e(vcpu,
7587 seg_reg == VCPU_SREG_SS ?
7588 SS_VECTOR : GP_VECTOR,
7589 0);
7590 return 1;
7591 }
7592
Bandan Das19677e32014-05-06 02:19:15 -04007593 return 0;
7594}
7595
Radim Krčmářcbf71272017-05-19 15:48:51 +02007596static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007597{
7598 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007599 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007600
7601 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007602 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007603 return 1;
7604
Radim Krčmářcbf71272017-05-19 15:48:51 +02007605 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer,
7606 sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007607 kvm_inject_page_fault(vcpu, &e);
7608 return 1;
7609 }
7610
Bandan Das3573e222014-05-06 02:19:16 -04007611 return 0;
7612}
7613
Jim Mattsone29acc52016-11-30 12:03:43 -08007614static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7615{
7616 struct vcpu_vmx *vmx = to_vmx(vcpu);
7617 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007618 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007619
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007620 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7621 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007622 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007623
7624 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7625 if (!vmx->nested.cached_vmcs12)
7626 goto out_cached_vmcs12;
7627
7628 if (enable_shadow_vmcs) {
7629 shadow_vmcs = alloc_vmcs();
7630 if (!shadow_vmcs)
7631 goto out_shadow_vmcs;
7632 /* mark vmcs as shadow */
7633 shadow_vmcs->revision_id |= (1u << 31);
7634 /* init shadow vmcs */
7635 vmcs_clear(shadow_vmcs);
7636 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7637 }
7638
Jim Mattsone29acc52016-11-30 12:03:43 -08007639 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7640 HRTIMER_MODE_REL_PINNED);
7641 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7642
7643 vmx->nested.vmxon = true;
7644 return 0;
7645
7646out_shadow_vmcs:
7647 kfree(vmx->nested.cached_vmcs12);
7648
7649out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007650 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007651
Jim Mattsonde3a0022017-11-27 17:22:25 -06007652out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007653 return -ENOMEM;
7654}
7655
Bandan Das3573e222014-05-06 02:19:16 -04007656/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007657 * Emulate the VMXON instruction.
7658 * Currently, we just remember that VMX is active, and do not save or even
7659 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7660 * do not currently need to store anything in that guest-allocated memory
7661 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7662 * argument is different from the VMXON pointer (which the spec says they do).
7663 */
7664static int handle_vmon(struct kvm_vcpu *vcpu)
7665{
Jim Mattsone29acc52016-11-30 12:03:43 -08007666 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007667 gpa_t vmptr;
7668 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007669 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007670 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7671 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007672
Jim Mattson70f3aac2017-04-26 08:53:46 -07007673 /*
7674 * The Intel VMX Instruction Reference lists a bunch of bits that are
7675 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7676 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7677 * Otherwise, we should fail with #UD. But most faulting conditions
7678 * have already been checked by hardware, prior to the VM-exit for
7679 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7680 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007681 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007682 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007683 kvm_queue_exception(vcpu, UD_VECTOR);
7684 return 1;
7685 }
7686
Abel Gordon145c28d2013-04-18 14:36:55 +03007687 if (vmx->nested.vmxon) {
7688 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007689 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007690 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007691
Haozhong Zhang3b840802016-06-22 14:59:54 +08007692 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007693 != VMXON_NEEDED_FEATURES) {
7694 kvm_inject_gp(vcpu, 0);
7695 return 1;
7696 }
7697
Radim Krčmářcbf71272017-05-19 15:48:51 +02007698 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007699 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007700
7701 /*
7702 * SDM 3: 24.11.5
7703 * The first 4 bytes of VMXON region contain the supported
7704 * VMCS revision identifier
7705 *
7706 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
7707 * which replaces physical address width with 32
7708 */
7709 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7710 nested_vmx_failInvalid(vcpu);
7711 return kvm_skip_emulated_instruction(vcpu);
7712 }
7713
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02007714 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
7715 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02007716 nested_vmx_failInvalid(vcpu);
7717 return kvm_skip_emulated_instruction(vcpu);
7718 }
7719 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
7720 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007721 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007722 nested_vmx_failInvalid(vcpu);
7723 return kvm_skip_emulated_instruction(vcpu);
7724 }
7725 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007726 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02007727
7728 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08007729 ret = enter_vmx_operation(vcpu);
7730 if (ret)
7731 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007732
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007733 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007734 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007735}
7736
7737/*
7738 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7739 * for running VMX instructions (except VMXON, whose prerequisites are
7740 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07007741 * Note that many of these exceptions have priority over VM exits, so they
7742 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007743 */
7744static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7745{
Jim Mattson70f3aac2017-04-26 08:53:46 -07007746 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007747 kvm_queue_exception(vcpu, UD_VECTOR);
7748 return 0;
7749 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007750 return 1;
7751}
7752
David Matlack8ca44e82017-08-01 14:00:39 -07007753static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
7754{
7755 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
7756 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7757}
7758
Abel Gordone7953d72013-04-18 14:37:55 +03007759static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7760{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007761 if (vmx->nested.current_vmptr == -1ull)
7762 return;
7763
Abel Gordon012f83c2013-04-18 14:39:25 +03007764 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007765 /* copy to memory all shadowed fields in case
7766 they were modified */
7767 copy_shadow_to_vmcs12(vmx);
7768 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07007769 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03007770 }
Wincy Van705699a2015-02-03 23:58:17 +08007771 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007772
7773 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02007774 kvm_vcpu_write_guest_page(&vmx->vcpu,
7775 vmx->nested.current_vmptr >> PAGE_SHIFT,
7776 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07007777
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007778 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03007779}
7780
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007781/*
7782 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7783 * just stops using VMX.
7784 */
7785static void free_nested(struct vcpu_vmx *vmx)
7786{
Wanpeng Lib7455822017-11-22 14:04:00 -08007787 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007788 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007789
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007790 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08007791 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007792 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07007793 vmx->nested.posted_intr_nv = -1;
7794 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007795 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07007796 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007797 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7798 free_vmcs(vmx->vmcs01.shadow_vmcs);
7799 vmx->vmcs01.shadow_vmcs = NULL;
7800 }
David Matlack4f2777b2016-07-13 17:16:37 -07007801 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06007802 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007803 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007804 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007805 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007806 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007807 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02007808 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007809 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007810 }
Wincy Van705699a2015-02-03 23:58:17 +08007811 if (vmx->nested.pi_desc_page) {
7812 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02007813 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08007814 vmx->nested.pi_desc_page = NULL;
7815 vmx->nested.pi_desc = NULL;
7816 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007817
Jim Mattsonde3a0022017-11-27 17:22:25 -06007818 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007819}
7820
7821/* Emulate the VMXOFF instruction */
7822static int handle_vmoff(struct kvm_vcpu *vcpu)
7823{
7824 if (!nested_vmx_check_permission(vcpu))
7825 return 1;
7826 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007827 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007828 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007829}
7830
Nadav Har'El27d6c862011-05-25 23:06:59 +03007831/* Emulate the VMCLEAR instruction */
7832static int handle_vmclear(struct kvm_vcpu *vcpu)
7833{
7834 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007835 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007836 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007837
7838 if (!nested_vmx_check_permission(vcpu))
7839 return 1;
7840
Radim Krčmářcbf71272017-05-19 15:48:51 +02007841 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007842 return 1;
7843
Radim Krčmářcbf71272017-05-19 15:48:51 +02007844 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
7845 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
7846 return kvm_skip_emulated_instruction(vcpu);
7847 }
7848
7849 if (vmptr == vmx->nested.vmxon_ptr) {
7850 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
7851 return kvm_skip_emulated_instruction(vcpu);
7852 }
7853
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007854 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007855 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007856
Jim Mattson587d7e722017-03-02 12:41:48 -08007857 kvm_vcpu_write_guest(vcpu,
7858 vmptr + offsetof(struct vmcs12, launch_state),
7859 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007860
Nadav Har'El27d6c862011-05-25 23:06:59 +03007861 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007862 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007863}
7864
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007865static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7866
7867/* Emulate the VMLAUNCH instruction */
7868static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7869{
7870 return nested_vmx_run(vcpu, true);
7871}
7872
7873/* Emulate the VMRESUME instruction */
7874static int handle_vmresume(struct kvm_vcpu *vcpu)
7875{
7876
7877 return nested_vmx_run(vcpu, false);
7878}
7879
Nadav Har'El49f705c2011-05-25 23:08:30 +03007880/*
7881 * Read a vmcs12 field. Since these can have varying lengths and we return
7882 * one type, we chose the biggest type (u64) and zero-extend the return value
7883 * to that size. Note that the caller, handle_vmread, might need to use only
7884 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7885 * 64-bit fields are to be returned).
7886 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007887static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7888 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007889{
7890 short offset = vmcs_field_to_offset(field);
7891 char *p;
7892
7893 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007894 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007895
7896 p = ((char *)(get_vmcs12(vcpu))) + offset;
7897
Jim Mattsond37f4262017-12-22 12:12:16 -08007898 switch (vmcs_field_width(field)) {
7899 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007900 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007901 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007902 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007903 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007904 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007905 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007906 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007907 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007908 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03007909 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007910 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007911 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007912 WARN_ON(1);
7913 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007914 }
7915}
7916
Abel Gordon20b97fe2013-04-18 14:36:25 +03007917
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007918static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7919 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007920 short offset = vmcs_field_to_offset(field);
7921 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7922 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007923 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007924
Jim Mattsond37f4262017-12-22 12:12:16 -08007925 switch (vmcs_field_width(field)) {
7926 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007927 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007928 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007929 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007930 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007931 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007932 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007933 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007934 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08007935 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03007936 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007937 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007938 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007939 WARN_ON(1);
7940 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007941 }
7942
7943}
7944
Abel Gordon16f5b902013-04-18 14:38:25 +03007945static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7946{
7947 int i;
7948 unsigned long field;
7949 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007950 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007951 const u16 *fields = shadow_read_write_fields;
Mathias Krausec2bae892013-06-26 20:36:21 +02007952 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007953
Jan Kiszka282da872014-10-08 18:05:39 +02007954 preempt_disable();
7955
Abel Gordon16f5b902013-04-18 14:38:25 +03007956 vmcs_load(shadow_vmcs);
7957
7958 for (i = 0; i < num_fields; i++) {
7959 field = fields[i];
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007960 field_value = __vmcs_readl(field);
Abel Gordon16f5b902013-04-18 14:38:25 +03007961 vmcs12_write_any(&vmx->vcpu, field, field_value);
7962 }
7963
7964 vmcs_clear(shadow_vmcs);
7965 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007966
7967 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007968}
7969
Abel Gordonc3114422013-04-18 14:38:55 +03007970static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7971{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007972 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02007973 shadow_read_write_fields,
7974 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007975 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007976 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007977 max_shadow_read_write_fields,
7978 max_shadow_read_only_fields
7979 };
7980 int i, q;
7981 unsigned long field;
7982 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007983 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007984
7985 vmcs_load(shadow_vmcs);
7986
Mathias Krausec2bae892013-06-26 20:36:21 +02007987 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007988 for (i = 0; i < max_fields[q]; i++) {
7989 field = fields[q][i];
7990 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01007991 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03007992 }
7993 }
7994
7995 vmcs_clear(shadow_vmcs);
7996 vmcs_load(vmx->loaded_vmcs->vmcs);
7997}
7998
Nadav Har'El49f705c2011-05-25 23:08:30 +03007999/*
8000 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8001 * used before) all generate the same failure when it is missing.
8002 */
8003static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8004{
8005 struct vcpu_vmx *vmx = to_vmx(vcpu);
8006 if (vmx->nested.current_vmptr == -1ull) {
8007 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008008 return 0;
8009 }
8010 return 1;
8011}
8012
8013static int handle_vmread(struct kvm_vcpu *vcpu)
8014{
8015 unsigned long field;
8016 u64 field_value;
8017 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8018 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8019 gva_t gva = 0;
8020
Kyle Hueyeb277562016-11-29 12:40:39 -08008021 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008022 return 1;
8023
Kyle Huey6affcbe2016-11-29 12:40:40 -08008024 if (!nested_vmx_check_vmcs12(vcpu))
8025 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008026
Nadav Har'El49f705c2011-05-25 23:08:30 +03008027 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008028 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008029 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008030 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008031 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008032 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008033 }
8034 /*
8035 * Now copy part of this value to register or memory, as requested.
8036 * Note that the number of bits actually copied is 32 or 64 depending
8037 * on the guest's mode (32 or 64 bit), not on the given field's length.
8038 */
8039 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008040 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008041 field_value);
8042 } else {
8043 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008044 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008045 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008046 /* _system ok, as hardware has verified cpl=0 */
Nadav Har'El49f705c2011-05-25 23:08:30 +03008047 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
8048 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
8049 }
8050
8051 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008052 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008053}
8054
8055
8056static int handle_vmwrite(struct kvm_vcpu *vcpu)
8057{
8058 unsigned long field;
8059 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008061 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8062 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008063
Nadav Har'El49f705c2011-05-25 23:08:30 +03008064 /* The value to write might be 32 or 64 bits, depending on L1's long
8065 * mode, and eventually we need to write that into a field of several
8066 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008067 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008068 * bits into the vmcs12 field.
8069 */
8070 u64 field_value = 0;
8071 struct x86_exception e;
8072
Kyle Hueyeb277562016-11-29 12:40:39 -08008073 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008074 return 1;
8075
Kyle Huey6affcbe2016-11-29 12:40:40 -08008076 if (!nested_vmx_check_vmcs12(vcpu))
8077 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008078
Nadav Har'El49f705c2011-05-25 23:08:30 +03008079 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008080 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008081 (((vmx_instruction_info) >> 3) & 0xf));
8082 else {
8083 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008084 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008085 return 1;
8086 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03008087 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008088 kvm_inject_page_fault(vcpu, &e);
8089 return 1;
8090 }
8091 }
8092
8093
Nadav Amit27e6fb52014-06-18 17:19:26 +03008094 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008095 if (vmcs_field_readonly(field)) {
8096 nested_vmx_failValid(vcpu,
8097 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008098 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008099 }
8100
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008101 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008102 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008103 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008104 }
8105
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008106 switch (field) {
8107#define SHADOW_FIELD_RW(x) case x:
8108#include "vmx_shadow_fields.h"
8109 /*
8110 * The fields that can be updated by L1 without a vmexit are
8111 * always updated in the vmcs02, the others go down the slow
8112 * path of prepare_vmcs02.
8113 */
8114 break;
8115 default:
8116 vmx->nested.dirty_vmcs12 = true;
8117 break;
8118 }
8119
Nadav Har'El49f705c2011-05-25 23:08:30 +03008120 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008121 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008122}
8123
Jim Mattsona8bc2842016-11-30 12:03:44 -08008124static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8125{
8126 vmx->nested.current_vmptr = vmptr;
8127 if (enable_shadow_vmcs) {
8128 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8129 SECONDARY_EXEC_SHADOW_VMCS);
8130 vmcs_write64(VMCS_LINK_POINTER,
8131 __pa(vmx->vmcs01.shadow_vmcs));
8132 vmx->nested.sync_shadow_vmcs = true;
8133 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008134 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008135}
8136
Nadav Har'El63846662011-05-25 23:07:29 +03008137/* Emulate the VMPTRLD instruction */
8138static int handle_vmptrld(struct kvm_vcpu *vcpu)
8139{
8140 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008141 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008142
8143 if (!nested_vmx_check_permission(vcpu))
8144 return 1;
8145
Radim Krčmářcbf71272017-05-19 15:48:51 +02008146 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008147 return 1;
8148
Radim Krčmářcbf71272017-05-19 15:48:51 +02008149 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8150 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8151 return kvm_skip_emulated_instruction(vcpu);
8152 }
8153
8154 if (vmptr == vmx->nested.vmxon_ptr) {
8155 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8156 return kvm_skip_emulated_instruction(vcpu);
8157 }
8158
Nadav Har'El63846662011-05-25 23:07:29 +03008159 if (vmx->nested.current_vmptr != vmptr) {
8160 struct vmcs12 *new_vmcs12;
8161 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008162 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8163 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008164 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008165 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008166 }
8167 new_vmcs12 = kmap(page);
8168 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8169 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008170 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008171 nested_vmx_failValid(vcpu,
8172 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008173 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008174 }
Nadav Har'El63846662011-05-25 23:07:29 +03008175
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008176 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008177 /*
8178 * Load VMCS12 from guest memory since it is not already
8179 * cached.
8180 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008181 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8182 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008183 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008184
Jim Mattsona8bc2842016-11-30 12:03:44 -08008185 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008186 }
8187
8188 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008189 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008190}
8191
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008192/* Emulate the VMPTRST instruction */
8193static int handle_vmptrst(struct kvm_vcpu *vcpu)
8194{
8195 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8196 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8197 gva_t vmcs_gva;
8198 struct x86_exception e;
8199
8200 if (!nested_vmx_check_permission(vcpu))
8201 return 1;
8202
8203 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008204 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008205 return 1;
Jim Mattson70f3aac2017-04-26 08:53:46 -07008206 /* ok to use *_system, as hardware has verified cpl=0 */
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008207 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
8208 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8209 sizeof(u64), &e)) {
8210 kvm_inject_page_fault(vcpu, &e);
8211 return 1;
8212 }
8213 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008214 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008215}
8216
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008217/* Emulate the INVEPT instruction */
8218static int handle_invept(struct kvm_vcpu *vcpu)
8219{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008220 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008221 u32 vmx_instruction_info, types;
8222 unsigned long type;
8223 gva_t gva;
8224 struct x86_exception e;
8225 struct {
8226 u64 eptp, gpa;
8227 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008228
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008229 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008230 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008231 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008232 kvm_queue_exception(vcpu, UD_VECTOR);
8233 return 1;
8234 }
8235
8236 if (!nested_vmx_check_permission(vcpu))
8237 return 1;
8238
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008239 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008240 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008241
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008242 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008243
Jim Mattson85c856b2016-10-26 08:38:38 -07008244 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008245 nested_vmx_failValid(vcpu,
8246 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008247 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008248 }
8249
8250 /* According to the Intel VMX instruction reference, the memory
8251 * operand is read even if it isn't needed (e.g., for type==global)
8252 */
8253 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008254 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008255 return 1;
8256 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8257 sizeof(operand), &e)) {
8258 kvm_inject_page_fault(vcpu, &e);
8259 return 1;
8260 }
8261
8262 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008263 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008264 /*
8265 * TODO: track mappings and invalidate
8266 * single context requests appropriately
8267 */
8268 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008269 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008270 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008271 nested_vmx_succeed(vcpu);
8272 break;
8273 default:
8274 BUG_ON(1);
8275 break;
8276 }
8277
Kyle Huey6affcbe2016-11-29 12:40:40 -08008278 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008279}
8280
Petr Matouseka642fc32014-09-23 20:22:30 +02008281static int handle_invvpid(struct kvm_vcpu *vcpu)
8282{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008283 struct vcpu_vmx *vmx = to_vmx(vcpu);
8284 u32 vmx_instruction_info;
8285 unsigned long type, types;
8286 gva_t gva;
8287 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008288 struct {
8289 u64 vpid;
8290 u64 gla;
8291 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008292
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008293 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008294 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008295 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008296 kvm_queue_exception(vcpu, UD_VECTOR);
8297 return 1;
8298 }
8299
8300 if (!nested_vmx_check_permission(vcpu))
8301 return 1;
8302
8303 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8304 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8305
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008306 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008307 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008308
Jim Mattson85c856b2016-10-26 08:38:38 -07008309 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008310 nested_vmx_failValid(vcpu,
8311 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008312 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008313 }
8314
8315 /* according to the intel vmx instruction reference, the memory
8316 * operand is read even if it isn't needed (e.g., for type==global)
8317 */
8318 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8319 vmx_instruction_info, false, &gva))
8320 return 1;
Jim Mattson40352602017-06-28 09:37:37 -07008321 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
8322 sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008323 kvm_inject_page_fault(vcpu, &e);
8324 return 1;
8325 }
Jim Mattson40352602017-06-28 09:37:37 -07008326 if (operand.vpid >> 16) {
8327 nested_vmx_failValid(vcpu,
8328 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8329 return kvm_skip_emulated_instruction(vcpu);
8330 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008331
8332 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008333 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Yu Zhangfd8cb432017-08-24 20:27:56 +08008334 if (is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008335 nested_vmx_failValid(vcpu,
8336 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8337 return kvm_skip_emulated_instruction(vcpu);
8338 }
8339 /* fall through */
Paolo Bonzinief697a72016-03-18 16:58:38 +01008340 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008341 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008342 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008343 nested_vmx_failValid(vcpu,
8344 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008345 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008346 }
8347 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008348 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008349 break;
8350 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008351 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008352 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008353 }
8354
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08008355 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008356 nested_vmx_succeed(vcpu);
8357
Kyle Huey6affcbe2016-11-29 12:40:40 -08008358 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008359}
8360
Kai Huang843e4332015-01-28 10:54:28 +08008361static int handle_pml_full(struct kvm_vcpu *vcpu)
8362{
8363 unsigned long exit_qualification;
8364
8365 trace_kvm_pml_full(vcpu->vcpu_id);
8366
8367 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8368
8369 /*
8370 * PML buffer FULL happened while executing iret from NMI,
8371 * "blocked by NMI" bit has to be set before next VM entry.
8372 */
8373 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008374 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008375 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8376 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8377 GUEST_INTR_STATE_NMI);
8378
8379 /*
8380 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8381 * here.., and there's no userspace involvement needed for PML.
8382 */
8383 return 1;
8384}
8385
Yunhong Jiang64672c92016-06-13 14:19:59 -07008386static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8387{
8388 kvm_lapic_expired_hv_timer(vcpu);
8389 return 1;
8390}
8391
Bandan Das41ab9372017-08-03 15:54:43 -04008392static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8393{
8394 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008395 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8396
8397 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008398 switch (address & VMX_EPTP_MT_MASK) {
8399 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008400 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008401 return false;
8402 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008403 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008404 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008405 return false;
8406 break;
8407 default:
8408 return false;
8409 }
8410
David Hildenbrandbb97a012017-08-10 23:15:28 +02008411 /* only 4 levels page-walk length are valid */
8412 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008413 return false;
8414
8415 /* Reserved bits should not be set */
8416 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8417 return false;
8418
8419 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008420 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008421 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008422 return false;
8423 }
8424
8425 return true;
8426}
8427
8428static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8429 struct vmcs12 *vmcs12)
8430{
8431 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8432 u64 address;
8433 bool accessed_dirty;
8434 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8435
8436 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8437 !nested_cpu_has_ept(vmcs12))
8438 return 1;
8439
8440 if (index >= VMFUNC_EPTP_ENTRIES)
8441 return 1;
8442
8443
8444 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8445 &address, index * 8, 8))
8446 return 1;
8447
David Hildenbrandbb97a012017-08-10 23:15:28 +02008448 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008449
8450 /*
8451 * If the (L2) guest does a vmfunc to the currently
8452 * active ept pointer, we don't have to do anything else
8453 */
8454 if (vmcs12->ept_pointer != address) {
8455 if (!valid_ept_address(vcpu, address))
8456 return 1;
8457
8458 kvm_mmu_unload(vcpu);
8459 mmu->ept_ad = accessed_dirty;
8460 mmu->base_role.ad_disabled = !accessed_dirty;
8461 vmcs12->ept_pointer = address;
8462 /*
8463 * TODO: Check what's the correct approach in case
8464 * mmu reload fails. Currently, we just let the next
8465 * reload potentially fail
8466 */
8467 kvm_mmu_reload(vcpu);
8468 }
8469
8470 return 0;
8471}
8472
Bandan Das2a499e42017-08-03 15:54:41 -04008473static int handle_vmfunc(struct kvm_vcpu *vcpu)
8474{
Bandan Das27c42a12017-08-03 15:54:42 -04008475 struct vcpu_vmx *vmx = to_vmx(vcpu);
8476 struct vmcs12 *vmcs12;
8477 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8478
8479 /*
8480 * VMFUNC is only supported for nested guests, but we always enable the
8481 * secondary control for simplicity; for non-nested mode, fake that we
8482 * didn't by injecting #UD.
8483 */
8484 if (!is_guest_mode(vcpu)) {
8485 kvm_queue_exception(vcpu, UD_VECTOR);
8486 return 1;
8487 }
8488
8489 vmcs12 = get_vmcs12(vcpu);
8490 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8491 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008492
8493 switch (function) {
8494 case 0:
8495 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8496 goto fail;
8497 break;
8498 default:
8499 goto fail;
8500 }
8501 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008502
8503fail:
8504 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8505 vmcs_read32(VM_EXIT_INTR_INFO),
8506 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008507 return 1;
8508}
8509
Nadav Har'El0140cae2011-05-25 23:06:28 +03008510/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008511 * The exit handlers return 1 if the exit was handled fully and guest execution
8512 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8513 * to be done to userspace and return 0.
8514 */
Mathias Krause772e0312012-08-30 01:30:19 +02008515static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008516 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8517 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008518 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008519 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008520 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008521 [EXIT_REASON_CR_ACCESS] = handle_cr,
8522 [EXIT_REASON_DR_ACCESS] = handle_dr,
8523 [EXIT_REASON_CPUID] = handle_cpuid,
8524 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8525 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8526 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8527 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008528 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008529 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008530 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008531 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008532 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008533 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008534 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008535 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008536 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008537 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008538 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008539 [EXIT_REASON_VMOFF] = handle_vmoff,
8540 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008541 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8542 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008543 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008544 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008545 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008546 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008547 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008548 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008549 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8550 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008551 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8552 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008553 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008554 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008555 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008556 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008557 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008558 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008559 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008560 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008561 [EXIT_REASON_XSAVES] = handle_xsaves,
8562 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008563 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008564 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008565 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008566};
8567
8568static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008569 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008570
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008571static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8572 struct vmcs12 *vmcs12)
8573{
8574 unsigned long exit_qualification;
8575 gpa_t bitmap, last_bitmap;
8576 unsigned int port;
8577 int size;
8578 u8 b;
8579
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008580 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008581 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008582
8583 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8584
8585 port = exit_qualification >> 16;
8586 size = (exit_qualification & 7) + 1;
8587
8588 last_bitmap = (gpa_t)-1;
8589 b = -1;
8590
8591 while (size > 0) {
8592 if (port < 0x8000)
8593 bitmap = vmcs12->io_bitmap_a;
8594 else if (port < 0x10000)
8595 bitmap = vmcs12->io_bitmap_b;
8596 else
Joe Perches1d804d02015-03-30 16:46:09 -07008597 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008598 bitmap += (port & 0x7fff) / 8;
8599
8600 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008601 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008602 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008603 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008604 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008605
8606 port++;
8607 size--;
8608 last_bitmap = bitmap;
8609 }
8610
Joe Perches1d804d02015-03-30 16:46:09 -07008611 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008612}
8613
Nadav Har'El644d7112011-05-25 23:12:35 +03008614/*
8615 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8616 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8617 * disinterest in the current event (read or write a specific MSR) by using an
8618 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8619 */
8620static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8621 struct vmcs12 *vmcs12, u32 exit_reason)
8622{
8623 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8624 gpa_t bitmap;
8625
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008626 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008627 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008628
8629 /*
8630 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8631 * for the four combinations of read/write and low/high MSR numbers.
8632 * First we need to figure out which of the four to use:
8633 */
8634 bitmap = vmcs12->msr_bitmap;
8635 if (exit_reason == EXIT_REASON_MSR_WRITE)
8636 bitmap += 2048;
8637 if (msr_index >= 0xc0000000) {
8638 msr_index -= 0xc0000000;
8639 bitmap += 1024;
8640 }
8641
8642 /* Then read the msr_index'th bit from this bitmap: */
8643 if (msr_index < 1024*8) {
8644 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008645 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008646 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008647 return 1 & (b >> (msr_index & 7));
8648 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008649 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008650}
8651
8652/*
8653 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8654 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8655 * intercept (via guest_host_mask etc.) the current event.
8656 */
8657static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8658 struct vmcs12 *vmcs12)
8659{
8660 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8661 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008662 int reg;
8663 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008664
8665 switch ((exit_qualification >> 4) & 3) {
8666 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008667 reg = (exit_qualification >> 8) & 15;
8668 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008669 switch (cr) {
8670 case 0:
8671 if (vmcs12->cr0_guest_host_mask &
8672 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008673 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008674 break;
8675 case 3:
8676 if ((vmcs12->cr3_target_count >= 1 &&
8677 vmcs12->cr3_target_value0 == val) ||
8678 (vmcs12->cr3_target_count >= 2 &&
8679 vmcs12->cr3_target_value1 == val) ||
8680 (vmcs12->cr3_target_count >= 3 &&
8681 vmcs12->cr3_target_value2 == val) ||
8682 (vmcs12->cr3_target_count >= 4 &&
8683 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008684 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008685 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008686 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008687 break;
8688 case 4:
8689 if (vmcs12->cr4_guest_host_mask &
8690 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008691 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008692 break;
8693 case 8:
8694 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008695 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008696 break;
8697 }
8698 break;
8699 case 2: /* clts */
8700 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8701 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008702 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008703 break;
8704 case 1: /* mov from cr */
8705 switch (cr) {
8706 case 3:
8707 if (vmcs12->cpu_based_vm_exec_control &
8708 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008709 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008710 break;
8711 case 8:
8712 if (vmcs12->cpu_based_vm_exec_control &
8713 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008714 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008715 break;
8716 }
8717 break;
8718 case 3: /* lmsw */
8719 /*
8720 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8721 * cr0. Other attempted changes are ignored, with no exit.
8722 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008723 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008724 if (vmcs12->cr0_guest_host_mask & 0xe &
8725 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008726 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008727 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8728 !(vmcs12->cr0_read_shadow & 0x1) &&
8729 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008730 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008731 break;
8732 }
Joe Perches1d804d02015-03-30 16:46:09 -07008733 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008734}
8735
8736/*
8737 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8738 * should handle it ourselves in L0 (and then continue L2). Only call this
8739 * when in is_guest_mode (L2).
8740 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02008741static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03008742{
Nadav Har'El644d7112011-05-25 23:12:35 +03008743 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8744 struct vcpu_vmx *vmx = to_vmx(vcpu);
8745 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8746
Jim Mattson4f350c62017-09-14 16:31:44 -07008747 if (vmx->nested.nested_run_pending)
8748 return false;
8749
8750 if (unlikely(vmx->fail)) {
8751 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8752 vmcs_read32(VM_INSTRUCTION_ERROR));
8753 return true;
8754 }
Jan Kiszka542060e2014-01-04 18:47:21 +01008755
David Matlackc9f04402017-08-01 14:00:40 -07008756 /*
8757 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06008758 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8759 * Page). The CPU may write to these pages via their host
8760 * physical address while L2 is running, bypassing any
8761 * address-translation-based dirty tracking (e.g. EPT write
8762 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07008763 *
8764 * Mark them dirty on every exit from L2 to prevent them from
8765 * getting out of sync with dirty tracking.
8766 */
8767 nested_mark_vmcs12_pages_dirty(vcpu);
8768
Jim Mattson4f350c62017-09-14 16:31:44 -07008769 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8770 vmcs_readl(EXIT_QUALIFICATION),
8771 vmx->idt_vectoring_info,
8772 intr_info,
8773 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8774 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03008775
8776 switch (exit_reason) {
8777 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008778 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008779 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008780 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07008781 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008782 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008783 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008784 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008785 else if (is_debug(intr_info) &&
8786 vcpu->guest_debug &
8787 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8788 return false;
8789 else if (is_breakpoint(intr_info) &&
8790 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8791 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008792 return vmcs12->exception_bitmap &
8793 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8794 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008795 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008796 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008797 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008798 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008799 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008800 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008801 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008802 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008803 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008804 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008805 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008806 case EXIT_REASON_HLT:
8807 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8808 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008809 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008810 case EXIT_REASON_INVLPG:
8811 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8812 case EXIT_REASON_RDPMC:
8813 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008814 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008815 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008816 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02008817 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008818 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008819 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8820 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8821 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8822 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8823 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8824 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008825 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008826 /*
8827 * VMX instructions trap unconditionally. This allows L1 to
8828 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8829 */
Joe Perches1d804d02015-03-30 16:46:09 -07008830 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008831 case EXIT_REASON_CR_ACCESS:
8832 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8833 case EXIT_REASON_DR_ACCESS:
8834 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8835 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008836 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008837 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8838 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008839 case EXIT_REASON_MSR_READ:
8840 case EXIT_REASON_MSR_WRITE:
8841 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8842 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008843 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008844 case EXIT_REASON_MWAIT_INSTRUCTION:
8845 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008846 case EXIT_REASON_MONITOR_TRAP_FLAG:
8847 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008848 case EXIT_REASON_MONITOR_INSTRUCTION:
8849 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8850 case EXIT_REASON_PAUSE_INSTRUCTION:
8851 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8852 nested_cpu_has2(vmcs12,
8853 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8854 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008855 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008856 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008857 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008858 case EXIT_REASON_APIC_ACCESS:
8859 return nested_cpu_has2(vmcs12,
8860 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008861 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008862 case EXIT_REASON_EOI_INDUCED:
8863 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008864 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008865 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008866 /*
8867 * L0 always deals with the EPT violation. If nested EPT is
8868 * used, and the nested mmu code discovers that the address is
8869 * missing in the guest EPT table (EPT12), the EPT violation
8870 * will be injected with nested_ept_inject_page_fault()
8871 */
Joe Perches1d804d02015-03-30 16:46:09 -07008872 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008873 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008874 /*
8875 * L2 never uses directly L1's EPT, but rather L0's own EPT
8876 * table (shadow on EPT) or a merged EPT table that L0 built
8877 * (EPT on EPT). So any problems with the structure of the
8878 * table is L0's fault.
8879 */
Joe Perches1d804d02015-03-30 16:46:09 -07008880 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02008881 case EXIT_REASON_INVPCID:
8882 return
8883 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
8884 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008885 case EXIT_REASON_WBINVD:
8886 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8887 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008888 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008889 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8890 /*
8891 * This should never happen, since it is not possible to
8892 * set XSS to a non-zero value---neither in L1 nor in L2.
8893 * If if it were, XSS would have to be checked against
8894 * the XSS exit bitmap in vmcs12.
8895 */
8896 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008897 case EXIT_REASON_PREEMPTION_TIMER:
8898 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02008899 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04008900 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02008901 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04008902 case EXIT_REASON_VMFUNC:
8903 /* VM functions are emulated through L2->L0 vmexits. */
8904 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008905 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008906 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008907 }
8908}
8909
Paolo Bonzini7313c692017-07-27 10:31:25 +02008910static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
8911{
8912 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8913
8914 /*
8915 * At this point, the exit interruption info in exit_intr_info
8916 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
8917 * we need to query the in-kernel LAPIC.
8918 */
8919 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
8920 if ((exit_intr_info &
8921 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8922 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
8923 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8924 vmcs12->vm_exit_intr_error_code =
8925 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
8926 }
8927
8928 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
8929 vmcs_readl(EXIT_QUALIFICATION));
8930 return 1;
8931}
8932
Avi Kivity586f9602010-11-18 13:09:54 +02008933static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8934{
8935 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8936 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8937}
8938
Kai Huanga3eaa862015-11-04 13:46:05 +08008939static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008940{
Kai Huanga3eaa862015-11-04 13:46:05 +08008941 if (vmx->pml_pg) {
8942 __free_page(vmx->pml_pg);
8943 vmx->pml_pg = NULL;
8944 }
Kai Huang843e4332015-01-28 10:54:28 +08008945}
8946
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008947static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008948{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008949 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008950 u64 *pml_buf;
8951 u16 pml_idx;
8952
8953 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8954
8955 /* Do nothing if PML buffer is empty */
8956 if (pml_idx == (PML_ENTITY_NUM - 1))
8957 return;
8958
8959 /* PML index always points to next available PML buffer entity */
8960 if (pml_idx >= PML_ENTITY_NUM)
8961 pml_idx = 0;
8962 else
8963 pml_idx++;
8964
8965 pml_buf = page_address(vmx->pml_pg);
8966 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8967 u64 gpa;
8968
8969 gpa = pml_buf[pml_idx];
8970 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008971 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008972 }
8973
8974 /* reset PML index */
8975 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8976}
8977
8978/*
8979 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8980 * Called before reporting dirty_bitmap to userspace.
8981 */
8982static void kvm_flush_pml_buffers(struct kvm *kvm)
8983{
8984 int i;
8985 struct kvm_vcpu *vcpu;
8986 /*
8987 * We only need to kick vcpu out of guest mode here, as PML buffer
8988 * is flushed at beginning of all VMEXITs, and it's obvious that only
8989 * vcpus running in guest are possible to have unflushed GPAs in PML
8990 * buffer.
8991 */
8992 kvm_for_each_vcpu(i, vcpu, kvm)
8993 kvm_vcpu_kick(vcpu);
8994}
8995
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008996static void vmx_dump_sel(char *name, uint32_t sel)
8997{
8998 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008999 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009000 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9001 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9002 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9003}
9004
9005static void vmx_dump_dtsel(char *name, uint32_t limit)
9006{
9007 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9008 name, vmcs_read32(limit),
9009 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9010}
9011
9012static void dump_vmcs(void)
9013{
9014 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9015 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9016 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9017 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9018 u32 secondary_exec_control = 0;
9019 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009020 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009021 int i, n;
9022
9023 if (cpu_has_secondary_exec_ctrls())
9024 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9025
9026 pr_err("*** Guest State ***\n");
9027 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9028 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9029 vmcs_readl(CR0_GUEST_HOST_MASK));
9030 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9031 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9032 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9033 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9034 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9035 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009036 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9037 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9038 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9039 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009040 }
9041 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9042 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9043 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9044 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9045 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9046 vmcs_readl(GUEST_SYSENTER_ESP),
9047 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9048 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9049 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9050 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9051 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9052 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9053 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9054 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9055 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9056 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9057 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9058 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9059 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009060 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9061 efer, vmcs_read64(GUEST_IA32_PAT));
9062 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9063 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009064 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009065 if (cpu_has_load_perf_global_ctrl &&
9066 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009067 pr_err("PerfGlobCtl = 0x%016llx\n",
9068 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009069 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009070 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009071 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9072 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9073 vmcs_read32(GUEST_ACTIVITY_STATE));
9074 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9075 pr_err("InterruptStatus = %04x\n",
9076 vmcs_read16(GUEST_INTR_STATUS));
9077
9078 pr_err("*** Host State ***\n");
9079 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9080 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9081 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9082 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9083 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9084 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9085 vmcs_read16(HOST_TR_SELECTOR));
9086 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9087 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9088 vmcs_readl(HOST_TR_BASE));
9089 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9090 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9091 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9092 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9093 vmcs_readl(HOST_CR4));
9094 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9095 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9096 vmcs_read32(HOST_IA32_SYSENTER_CS),
9097 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9098 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009099 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9100 vmcs_read64(HOST_IA32_EFER),
9101 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009102 if (cpu_has_load_perf_global_ctrl &&
9103 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009104 pr_err("PerfGlobCtl = 0x%016llx\n",
9105 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009106
9107 pr_err("*** Control State ***\n");
9108 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9109 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9110 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9111 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9112 vmcs_read32(EXCEPTION_BITMAP),
9113 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9114 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9115 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9116 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9117 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9118 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9119 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9120 vmcs_read32(VM_EXIT_INTR_INFO),
9121 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9122 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9123 pr_err(" reason=%08x qualification=%016lx\n",
9124 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9125 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9126 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9127 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009128 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009129 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009130 pr_err("TSC Multiplier = 0x%016llx\n",
9131 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009132 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9133 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9134 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9135 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9136 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009137 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009138 n = vmcs_read32(CR3_TARGET_COUNT);
9139 for (i = 0; i + 1 < n; i += 4)
9140 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9141 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9142 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9143 if (i < n)
9144 pr_err("CR3 target%u=%016lx\n",
9145 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9146 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9147 pr_err("PLE Gap=%08x Window=%08x\n",
9148 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9149 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9150 pr_err("Virtual processor ID = 0x%04x\n",
9151 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9152}
9153
Avi Kivity6aa8b732006-12-10 02:21:36 -08009154/*
9155 * The guest has exited. See if we can fix it or if we need userspace
9156 * assistance.
9157 */
Avi Kivity851ba692009-08-24 11:10:17 +03009158static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009159{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009160 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009161 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009162 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009163
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009164 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9165
Kai Huang843e4332015-01-28 10:54:28 +08009166 /*
9167 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9168 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9169 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9170 * mode as if vcpus is in root mode, the PML buffer must has been
9171 * flushed already.
9172 */
9173 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009174 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009175
Mohammed Gamal80ced182009-09-01 12:48:18 +02009176 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009177 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009178 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009179
Paolo Bonzini7313c692017-07-27 10:31:25 +02009180 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9181 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009182
Mohammed Gamal51207022010-05-31 22:40:54 +03009183 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009184 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009185 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9186 vcpu->run->fail_entry.hardware_entry_failure_reason
9187 = exit_reason;
9188 return 0;
9189 }
9190
Avi Kivity29bd8a72007-09-10 17:27:03 +03009191 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009192 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9193 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009194 = vmcs_read32(VM_INSTRUCTION_ERROR);
9195 return 0;
9196 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009197
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009198 /*
9199 * Note:
9200 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9201 * delivery event since it indicates guest is accessing MMIO.
9202 * The vm-exit can be triggered again after return to guest that
9203 * will cause infinite loop.
9204 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009205 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009206 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009207 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009208 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009209 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9210 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9211 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009212 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009213 vcpu->run->internal.data[0] = vectoring_info;
9214 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009215 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9216 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9217 vcpu->run->internal.ndata++;
9218 vcpu->run->internal.data[3] =
9219 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9220 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009221 return 0;
9222 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009223
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009224 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009225 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9226 if (vmx_interrupt_allowed(vcpu)) {
9227 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9228 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9229 vcpu->arch.nmi_pending) {
9230 /*
9231 * This CPU don't support us in finding the end of an
9232 * NMI-blocked window if the guest runs with IRQs
9233 * disabled. So we pull the trigger after 1 s of
9234 * futile waiting, but inform the user about this.
9235 */
9236 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9237 "state on VCPU %d after 1 s timeout\n",
9238 __func__, vcpu->vcpu_id);
9239 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9240 }
9241 }
9242
Avi Kivity6aa8b732006-12-10 02:21:36 -08009243 if (exit_reason < kvm_vmx_max_exit_handlers
9244 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009245 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009246 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009247 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9248 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009249 kvm_queue_exception(vcpu, UD_VECTOR);
9250 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009251 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009252}
9253
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009254static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009255{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009256 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9257
9258 if (is_guest_mode(vcpu) &&
9259 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9260 return;
9261
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009262 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009263 vmcs_write32(TPR_THRESHOLD, 0);
9264 return;
9265 }
9266
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009267 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009268}
9269
Yang Zhang8d146952013-01-25 10:18:50 +08009270static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
9271{
9272 u32 sec_exec_control;
9273
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009274 /* Postpone execution until vmcs01 is the current VMCS. */
9275 if (is_guest_mode(vcpu)) {
9276 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
9277 return;
9278 }
9279
Wanpeng Lif6e90f92016-09-22 07:43:25 +08009280 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08009281 return;
9282
Paolo Bonzini35754c92015-07-29 12:05:37 +02009283 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009284 return;
9285
9286 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9287
9288 if (set) {
9289 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9290 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9291 } else {
9292 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9293 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009294 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009295 }
9296 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9297
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009298 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009299}
9300
Tang Chen38b99172014-09-24 15:57:54 +08009301static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9302{
9303 struct vcpu_vmx *vmx = to_vmx(vcpu);
9304
9305 /*
9306 * Currently we do not handle the nested case where L2 has an
9307 * APIC access page of its own; that page is still pinned.
9308 * Hence, we skip the case where the VCPU is in guest mode _and_
9309 * L1 prepared an APIC access page for L2.
9310 *
9311 * For the case where L1 and L2 share the same APIC access page
9312 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
9313 * in the vmcs12), this function will only update either the vmcs01
9314 * or the vmcs02. If the former, the vmcs02 will be updated by
9315 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
9316 * the next L2->L1 exit.
9317 */
9318 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07009319 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009320 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08009321 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009322 vmx_flush_tlb_ept_only(vcpu);
9323 }
Tang Chen38b99172014-09-24 15:57:54 +08009324}
9325
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009326static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009327{
9328 u16 status;
9329 u8 old;
9330
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009331 if (max_isr == -1)
9332 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009333
9334 status = vmcs_read16(GUEST_INTR_STATUS);
9335 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009336 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009337 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009338 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009339 vmcs_write16(GUEST_INTR_STATUS, status);
9340 }
9341}
9342
9343static void vmx_set_rvi(int vector)
9344{
9345 u16 status;
9346 u8 old;
9347
Wei Wang4114c272014-11-05 10:53:43 +08009348 if (vector == -1)
9349 vector = 0;
9350
Yang Zhangc7c9c562013-01-25 10:18:51 +08009351 status = vmcs_read16(GUEST_INTR_STATUS);
9352 old = (u8)status & 0xff;
9353 if ((u8)vector != old) {
9354 status &= ~0xff;
9355 status |= (u8)vector;
9356 vmcs_write16(GUEST_INTR_STATUS, status);
9357 }
9358}
9359
9360static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9361{
Liran Alon851c1a182017-12-24 18:12:56 +02009362 /*
9363 * When running L2, updating RVI is only relevant when
9364 * vmcs12 virtual-interrupt-delivery enabled.
9365 * However, it can be enabled only when L1 also
9366 * intercepts external-interrupts and in that case
9367 * we should not update vmcs02 RVI but instead intercept
9368 * interrupt. Therefore, do nothing when running L2.
9369 */
9370 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009371 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009372}
9373
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009374static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009375{
9376 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009377 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009378 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009379
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009380 WARN_ON(!vcpu->arch.apicv_active);
9381 if (pi_test_on(&vmx->pi_desc)) {
9382 pi_clear_on(&vmx->pi_desc);
9383 /*
9384 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9385 * But on x86 this is just a compiler barrier anyway.
9386 */
9387 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009388 max_irr_updated =
9389 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9390
9391 /*
9392 * If we are running L2 and L1 has a new pending interrupt
9393 * which can be injected, we should re-evaluate
9394 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009395 * If L1 intercepts external-interrupts, we should
9396 * exit from L2 to L1. Otherwise, interrupt should be
9397 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009398 */
Liran Alon851c1a182017-12-24 18:12:56 +02009399 if (is_guest_mode(vcpu) && max_irr_updated) {
9400 if (nested_exit_on_intr(vcpu))
9401 kvm_vcpu_exiting_guest_mode(vcpu);
9402 else
9403 kvm_make_request(KVM_REQ_EVENT, vcpu);
9404 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009405 } else {
9406 max_irr = kvm_lapic_find_highest_irr(vcpu);
9407 }
9408 vmx_hwapic_irr_update(vcpu, max_irr);
9409 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009410}
9411
Andrey Smetanin63086302015-11-10 15:36:32 +03009412static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009413{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009414 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009415 return;
9416
Yang Zhangc7c9c562013-01-25 10:18:51 +08009417 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9418 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9419 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9420 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9421}
9422
Paolo Bonzini967235d2016-12-19 14:03:45 +01009423static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9424{
9425 struct vcpu_vmx *vmx = to_vmx(vcpu);
9426
9427 pi_clear_on(&vmx->pi_desc);
9428 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9429}
9430
Avi Kivity51aa01d2010-07-20 14:31:20 +03009431static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009432{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009433 u32 exit_intr_info = 0;
9434 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009435
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009436 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9437 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009438 return;
9439
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009440 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9441 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9442 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009443
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009444 /* if exit due to PF check for async PF */
9445 if (is_page_fault(exit_intr_info))
9446 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9447
Andi Kleena0861c02009-06-08 17:37:09 +08009448 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009449 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9450 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009451 kvm_machine_check();
9452
Gleb Natapov20f65982009-05-11 13:35:55 +03009453 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009454 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009455 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009456 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009457 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009458 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009459}
Gleb Natapov20f65982009-05-11 13:35:55 +03009460
Yang Zhanga547c6d2013-04-11 19:25:10 +08009461static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9462{
9463 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9464
Yang Zhanga547c6d2013-04-11 19:25:10 +08009465 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9466 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9467 unsigned int vector;
9468 unsigned long entry;
9469 gate_desc *desc;
9470 struct vcpu_vmx *vmx = to_vmx(vcpu);
9471#ifdef CONFIG_X86_64
9472 unsigned long tmp;
9473#endif
9474
9475 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9476 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009477 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009478 asm volatile(
9479#ifdef CONFIG_X86_64
9480 "mov %%" _ASM_SP ", %[sp]\n\t"
9481 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9482 "push $%c[ss]\n\t"
9483 "push %[sp]\n\t"
9484#endif
9485 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009486 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009487 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009488 :
9489#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009490 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009491#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009492 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009493 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009494 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009495 [ss]"i"(__KERNEL_DS),
9496 [cs]"i"(__KERNEL_CS)
9497 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009498 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009499}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009500STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009501
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009502static bool vmx_has_high_real_mode_segbase(void)
9503{
9504 return enable_unrestricted_guest || emulate_invalid_guest_state;
9505}
9506
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009507static bool vmx_mpx_supported(void)
9508{
9509 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9510 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9511}
9512
Wanpeng Li55412b22014-12-02 19:21:30 +08009513static bool vmx_xsaves_supported(void)
9514{
9515 return vmcs_config.cpu_based_2nd_exec_ctrl &
9516 SECONDARY_EXEC_XSAVES;
9517}
9518
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009519static bool vmx_umip_emulated(void)
9520{
Paolo Bonzini0367f202016-07-12 10:44:55 +02009521 return vmcs_config.cpu_based_2nd_exec_ctrl &
9522 SECONDARY_EXEC_DESC;
Paolo Bonzini66336ca2016-07-12 10:36:41 +02009523}
9524
Avi Kivity51aa01d2010-07-20 14:31:20 +03009525static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9526{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009527 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009528 bool unblock_nmi;
9529 u8 vector;
9530 bool idtv_info_valid;
9531
9532 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009533
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009534 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009535 if (vmx->loaded_vmcs->nmi_known_unmasked)
9536 return;
9537 /*
9538 * Can't use vmx->exit_intr_info since we're not sure what
9539 * the exit reason is.
9540 */
9541 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9542 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9543 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9544 /*
9545 * SDM 3: 27.7.1.2 (September 2008)
9546 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9547 * a guest IRET fault.
9548 * SDM 3: 23.2.2 (September 2008)
9549 * Bit 12 is undefined in any of the following cases:
9550 * If the VM exit sets the valid bit in the IDT-vectoring
9551 * information field.
9552 * If the VM exit is due to a double fault.
9553 */
9554 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9555 vector != DF_VECTOR && !idtv_info_valid)
9556 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9557 GUEST_INTR_STATE_NMI);
9558 else
9559 vmx->loaded_vmcs->nmi_known_unmasked =
9560 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9561 & GUEST_INTR_STATE_NMI);
9562 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9563 vmx->loaded_vmcs->vnmi_blocked_time +=
9564 ktime_to_ns(ktime_sub(ktime_get(),
9565 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009566}
9567
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009568static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009569 u32 idt_vectoring_info,
9570 int instr_len_field,
9571 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009572{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009573 u8 vector;
9574 int type;
9575 bool idtv_info_valid;
9576
9577 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009578
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009579 vcpu->arch.nmi_injected = false;
9580 kvm_clear_exception_queue(vcpu);
9581 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009582
9583 if (!idtv_info_valid)
9584 return;
9585
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009586 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009587
Avi Kivity668f6122008-07-02 09:28:55 +03009588 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9589 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009590
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009591 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009592 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009593 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009594 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009595 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009596 * Clear bit "block by NMI" before VM entry if a NMI
9597 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009598 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009599 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009600 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009601 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009602 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009603 /* fall through */
9604 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009605 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009606 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009607 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03009608 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03009609 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009610 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009611 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009612 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009613 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03009614 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009615 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009616 break;
9617 default:
9618 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03009619 }
Avi Kivitycf393f72008-07-01 16:20:21 +03009620}
9621
Avi Kivity83422e12010-07-20 14:43:23 +03009622static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
9623{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009624 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03009625 VM_EXIT_INSTRUCTION_LEN,
9626 IDT_VECTORING_ERROR_CODE);
9627}
9628
Avi Kivityb463a6f2010-07-20 15:06:17 +03009629static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
9630{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009631 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009632 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9633 VM_ENTRY_INSTRUCTION_LEN,
9634 VM_ENTRY_EXCEPTION_ERROR_CODE);
9635
9636 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9637}
9638
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009639static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
9640{
9641 int i, nr_msrs;
9642 struct perf_guest_switch_msr *msrs;
9643
9644 msrs = perf_guest_get_msrs(&nr_msrs);
9645
9646 if (!msrs)
9647 return;
9648
9649 for (i = 0; i < nr_msrs; i++)
9650 if (msrs[i].host == msrs[i].guest)
9651 clear_atomic_switch_msr(vmx, msrs[i].msr);
9652 else
9653 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
9654 msrs[i].host);
9655}
9656
Jiang Biao33365e72016-11-03 15:03:37 +08009657static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07009658{
9659 struct vcpu_vmx *vmx = to_vmx(vcpu);
9660 u64 tscl;
9661 u32 delta_tsc;
9662
9663 if (vmx->hv_deadline_tsc == -1)
9664 return;
9665
9666 tscl = rdtsc();
9667 if (vmx->hv_deadline_tsc > tscl)
9668 /* sure to be 32 bit only because checked on set_hv_timer */
9669 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9670 cpu_preemption_timer_multi);
9671 else
9672 delta_tsc = 0;
9673
9674 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9675}
9676
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009677static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009678{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009679 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009680 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +02009681
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009682 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009683 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009684 vmx->loaded_vmcs->soft_vnmi_blocked))
9685 vmx->loaded_vmcs->entry_time = ktime_get();
9686
Avi Kivity104f2262010-11-18 13:12:52 +02009687 /* Don't enter VMX if guest state is invalid, let the exit handler
9688 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009689 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009690 return;
9691
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009692 if (vmx->ple_window_dirty) {
9693 vmx->ple_window_dirty = false;
9694 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9695 }
9696
Abel Gordon012f83c2013-04-18 14:39:25 +03009697 if (vmx->nested.sync_shadow_vmcs) {
9698 copy_vmcs12_to_shadow(vmx);
9699 vmx->nested.sync_shadow_vmcs = false;
9700 }
9701
Avi Kivity104f2262010-11-18 13:12:52 +02009702 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9703 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9704 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9705 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9706
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009707 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +02009708 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009709 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +02009710 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07009711 }
9712
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009713 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +02009714 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009715 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +02009716 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009717 }
9718
Avi Kivity104f2262010-11-18 13:12:52 +02009719 /* When single-stepping over STI and MOV SS, we must clear the
9720 * corresponding interruptibility bits in the guest state. Otherwise
9721 * vmentry fails as it then expects bit 14 (BS) in pending debug
9722 * exceptions being set, but that's not correct for the guest debugging
9723 * case. */
9724 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9725 vmx_set_interrupt_shadow(vcpu, 0);
9726
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009727 if (static_cpu_has(X86_FEATURE_PKU) &&
9728 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
9729 vcpu->arch.pkru != vmx->host_pkru)
9730 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009731
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009732 atomic_switch_perf_msrs(vmx);
9733
Yunhong Jiang64672c92016-06-13 14:19:59 -07009734 vmx_arm_hv_timer(vcpu);
9735
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009736 /*
9737 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9738 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9739 * is no need to worry about the conditional branch over the wrmsr
9740 * being speculatively taken.
9741 */
9742 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009743 native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009744
Nadav Har'Eld462b812011-05-24 15:26:10 +03009745 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009746
9747 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
9748 (unsigned long)&current_evmcs->host_rsp : 0;
9749
Avi Kivity104f2262010-11-18 13:12:52 +02009750 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009751 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009752 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9753 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9754 "push %%" _ASM_CX " \n\t"
9755 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009756 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009757 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009758 /* Avoid VMWRITE when Enlightened VMCS is in use */
9759 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
9760 "jz 2f \n\t"
9761 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
9762 "jmp 1f \n\t"
9763 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009764 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009765 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009766 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009767 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9768 "mov %%cr2, %%" _ASM_DX " \n\t"
9769 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009770 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009771 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009772 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009773 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009774 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009775 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009776 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9777 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9778 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9779 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9780 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9781 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009782#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009783 "mov %c[r8](%0), %%r8 \n\t"
9784 "mov %c[r9](%0), %%r9 \n\t"
9785 "mov %c[r10](%0), %%r10 \n\t"
9786 "mov %c[r11](%0), %%r11 \n\t"
9787 "mov %c[r12](%0), %%r12 \n\t"
9788 "mov %c[r13](%0), %%r13 \n\t"
9789 "mov %c[r14](%0), %%r14 \n\t"
9790 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009791#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009792 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009793
Avi Kivity6aa8b732006-12-10 02:21:36 -08009794 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009795 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009796 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009797 "jmp 2f \n\t"
9798 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9799 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009800 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009801 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009802 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009803 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009804 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9805 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9806 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9807 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9808 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9809 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9810 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009811#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009812 "mov %%r8, %c[r8](%0) \n\t"
9813 "mov %%r9, %c[r9](%0) \n\t"
9814 "mov %%r10, %c[r10](%0) \n\t"
9815 "mov %%r11, %c[r11](%0) \n\t"
9816 "mov %%r12, %c[r12](%0) \n\t"
9817 "mov %%r13, %c[r13](%0) \n\t"
9818 "mov %%r14, %c[r14](%0) \n\t"
9819 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08009820 "xor %%r8d, %%r8d \n\t"
9821 "xor %%r9d, %%r9d \n\t"
9822 "xor %%r10d, %%r10d \n\t"
9823 "xor %%r11d, %%r11d \n\t"
9824 "xor %%r12d, %%r12d \n\t"
9825 "xor %%r13d, %%r13d \n\t"
9826 "xor %%r14d, %%r14d \n\t"
9827 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009828#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009829 "mov %%cr2, %%" _ASM_AX " \n\t"
9830 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009831
Jim Mattson0cb5b302018-01-03 14:31:38 -08009832 "xor %%eax, %%eax \n\t"
9833 "xor %%ebx, %%ebx \n\t"
9834 "xor %%esi, %%esi \n\t"
9835 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009836 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009837 ".pushsection .rodata \n\t"
9838 ".global vmx_return \n\t"
9839 "vmx_return: " _ASM_PTR " 2b \n\t"
9840 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009841 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009842 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009843 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009844 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009845 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9846 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9847 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9848 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9849 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9850 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9851 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009852#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009853 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9854 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9855 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9856 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9857 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9858 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9859 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9860 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009861#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009862 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9863 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009864 : "cc", "memory"
9865#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009866 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009867 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009868#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009869 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009870#endif
9871 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009872
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009873 /*
9874 * We do not use IBRS in the kernel. If this vCPU has used the
9875 * SPEC_CTRL MSR it may have left it on; save the value and
9876 * turn it off. This is much more efficient than blindly adding
9877 * it to the atomic save/restore list. Especially as the former
9878 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9879 *
9880 * For non-nested case:
9881 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9882 * save it.
9883 *
9884 * For nested case:
9885 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9886 * save it.
9887 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01009888 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009889 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009890
9891 if (vmx->spec_ctrl)
Paolo Bonziniecb586b2018-02-22 16:43:17 +01009892 native_wrmsrl(MSR_IA32_SPEC_CTRL, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01009893
David Woodhouse117cc7a2018-01-12 11:11:27 +00009894 /* Eliminate branch target predictions from guest mode */
9895 vmexit_fill_RSB();
9896
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009897 /* All fields are clean at this point */
9898 if (static_branch_unlikely(&enable_evmcs))
9899 current_evmcs->hv_clean_fields |=
9900 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
9901
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009902 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08009903 if (vmx->host_debugctlmsr)
9904 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009905
Avi Kivityaa67f602012-08-01 16:48:03 +03009906#ifndef CONFIG_X86_64
9907 /*
9908 * The sysexit path does not restore ds/es, so we must set them to
9909 * a reasonable value ourselves.
9910 *
9911 * We can't defer this to vmx_load_host_state() since that function
9912 * may be executed in interrupt context, which saves and restore segments
9913 * around it, nullifying its effect.
9914 */
9915 loadsegment(ds, __USER_DS);
9916 loadsegment(es, __USER_DS);
9917#endif
9918
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009919 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009920 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009921 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009922 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009923 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009924 vcpu->arch.regs_dirty = 0;
9925
Gleb Natapove0b890d2013-09-25 12:51:33 +03009926 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009927 * eager fpu is enabled if PKEY is supported and CR4 is switched
9928 * back on host, so it is safe to read guest PKRU from current
9929 * XSAVE.
9930 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +02009931 if (static_cpu_has(X86_FEATURE_PKU) &&
9932 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
9933 vcpu->arch.pkru = __read_pkru();
9934 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009935 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009936 }
9937
Gleb Natapove0b890d2013-09-25 12:51:33 +03009938 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07009939 vmx->idt_vectoring_info = 0;
9940
9941 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
9942 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9943 return;
9944
9945 vmx->loaded_vmcs->launched = 1;
9946 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03009947
Avi Kivity51aa01d2010-07-20 14:31:20 +03009948 vmx_complete_atomic_exit(vmx);
9949 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009950 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009951}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009952STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009953
Sean Christopherson434a1e92018-03-20 12:17:18 -07009954static struct kvm *vmx_vm_alloc(void)
9955{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009956 struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
9957 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -07009958}
9959
9960static void vmx_vm_free(struct kvm *kvm)
9961{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07009962 kfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -07009963}
9964
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009965static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009966{
9967 struct vcpu_vmx *vmx = to_vmx(vcpu);
9968 int cpu;
9969
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009970 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009971 return;
9972
9973 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009974 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009975 vmx_vcpu_put(vcpu);
9976 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009977 put_cpu();
9978}
9979
Jim Mattson2f1fe812016-07-08 15:36:06 -07009980/*
9981 * Ensure that the current vmcs of the logical processor is the
9982 * vmcs01 of the vcpu before calling free_nested().
9983 */
9984static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9985{
9986 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009987
Christoffer Dallec7660c2017-12-04 21:35:23 +01009988 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009989 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009990 free_nested(vmx);
9991 vcpu_put(vcpu);
9992}
9993
Avi Kivity6aa8b732006-12-10 02:21:36 -08009994static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9995{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009996 struct vcpu_vmx *vmx = to_vmx(vcpu);
9997
Kai Huang843e4332015-01-28 10:54:28 +08009998 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009999 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010000 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010001 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010002 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010003 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010004 kfree(vmx->guest_msrs);
10005 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010006 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010007}
10008
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010009static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010010{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010011 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010012 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010013 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010014 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010015
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010016 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010017 return ERR_PTR(-ENOMEM);
10018
Wanpeng Li991e7a02015-09-16 17:30:05 +080010019 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010020
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010021 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10022 if (err)
10023 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010024
Peter Feiner4e595162016-07-07 14:49:58 -070010025 err = -ENOMEM;
10026
10027 /*
10028 * If PML is turned on, failure on enabling PML just results in failure
10029 * of creating the vcpu, therefore we can simplify PML logic (by
10030 * avoiding dealing with cases, such as enabling PML partially on vcpus
10031 * for the guest, etc.
10032 */
10033 if (enable_pml) {
10034 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10035 if (!vmx->pml_pg)
10036 goto uninit_vcpu;
10037 }
10038
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010039 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010040 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10041 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010042
Peter Feiner4e595162016-07-07 14:49:58 -070010043 if (!vmx->guest_msrs)
10044 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010045
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010046 err = alloc_loaded_vmcs(&vmx->vmcs01);
10047 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010048 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010049
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010050 msr_bitmap = vmx->vmcs01.msr_bitmap;
10051 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10052 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10053 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10054 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10055 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10056 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10057 vmx->msr_bitmap_mode = 0;
10058
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010059 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010060 cpu = get_cpu();
10061 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010062 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010063 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010064 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010065 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010066 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010067 err = alloc_apic_access_page(kvm);
10068 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010069 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010070 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010071
Sean Christophersone90008d2018-03-05 12:04:37 -080010072 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010073 err = init_rmode_identity_map(kvm);
10074 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010075 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010076 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010077
Wanpeng Li5c614b32015-10-13 09:18:36 -070010078 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010079 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10080 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010081 vmx->nested.vpid02 = allocate_vpid();
10082 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010083
Wincy Van705699a2015-02-03 23:58:17 +080010084 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010085 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010086
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010087 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10088
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010089 /*
10090 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10091 * or POSTED_INTR_WAKEUP_VECTOR.
10092 */
10093 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10094 vmx->pi_desc.sn = 1;
10095
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010096 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010097
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010098free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010099 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010100 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010101free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010102 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010103free_pml:
10104 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010105uninit_vcpu:
10106 kvm_vcpu_uninit(&vmx->vcpu);
10107free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010108 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010109 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010110 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010111}
10112
Wanpeng Lib31c1142018-03-12 04:53:04 -070010113static int vmx_vm_init(struct kvm *kvm)
10114{
10115 if (!ple_gap)
10116 kvm->arch.pause_in_guest = true;
10117 return 0;
10118}
10119
Yang, Sheng002c7f72007-07-31 14:23:01 +030010120static void __init vmx_check_processor_compat(void *rtn)
10121{
10122 struct vmcs_config vmcs_conf;
10123
10124 *(int *)rtn = 0;
10125 if (setup_vmcs_config(&vmcs_conf) < 0)
10126 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010127 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010128 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10129 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10130 smp_processor_id());
10131 *(int *)rtn = -EIO;
10132 }
10133}
10134
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010135static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010136{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010137 u8 cache;
10138 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010139
Sheng Yang522c68c2009-04-27 20:35:43 +080010140 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010141 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010142 * 2. EPT with VT-d:
10143 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010144 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010145 * b. VT-d with snooping control feature: snooping control feature of
10146 * VT-d engine can guarantee the cache correctness. Just set it
10147 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010148 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010149 * consistent with host MTRR
10150 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010151 if (is_mmio) {
10152 cache = MTRR_TYPE_UNCACHABLE;
10153 goto exit;
10154 }
10155
10156 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010157 ipat = VMX_EPT_IPAT_BIT;
10158 cache = MTRR_TYPE_WRBACK;
10159 goto exit;
10160 }
10161
10162 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10163 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010164 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010165 cache = MTRR_TYPE_WRBACK;
10166 else
10167 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010168 goto exit;
10169 }
10170
Xiao Guangrongff536042015-06-15 16:55:22 +080010171 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010172
10173exit:
10174 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010175}
10176
Sheng Yang17cc3932010-01-05 19:02:27 +080010177static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010178{
Sheng Yang878403b2010-01-05 19:02:29 +080010179 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10180 return PT_DIRECTORY_LEVEL;
10181 else
10182 /* For shadow and EPT supported 1GB page */
10183 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010184}
10185
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010186static void vmcs_set_secondary_exec_control(u32 new_ctl)
10187{
10188 /*
10189 * These bits in the secondary execution controls field
10190 * are dynamic, the others are mostly based on the hypervisor
10191 * architecture and the guest's CPUID. Do not touch the
10192 * dynamic bits.
10193 */
10194 u32 mask =
10195 SECONDARY_EXEC_SHADOW_VMCS |
10196 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010197 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10198 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010199
10200 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10201
10202 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10203 (new_ctl & ~mask) | (cur_ctl & mask));
10204}
10205
David Matlack8322ebb2016-11-29 18:14:09 -080010206/*
10207 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10208 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10209 */
10210static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10211{
10212 struct vcpu_vmx *vmx = to_vmx(vcpu);
10213 struct kvm_cpuid_entry2 *entry;
10214
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010215 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10216 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010217
10218#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10219 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010220 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010221} while (0)
10222
10223 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10224 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10225 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10226 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10227 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10228 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10229 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10230 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10231 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10232 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10233 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10234 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10235 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10236 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10237 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10238
10239 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10240 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10241 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10242 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10243 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010244 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010245
10246#undef cr4_fixed1_update
10247}
10248
Sheng Yang0e851882009-12-18 16:48:46 +080010249static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10250{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010252
Paolo Bonzini80154d72017-08-24 13:55:35 +020010253 if (cpu_has_secondary_exec_ctrls()) {
10254 vmx_compute_secondary_exec_control(vmx);
10255 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010256 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010257
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010258 if (nested_vmx_allowed(vcpu))
10259 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10260 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10261 else
10262 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10263 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010264
10265 if (nested_vmx_allowed(vcpu))
10266 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010267}
10268
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010269static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10270{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010271 if (func == 1 && nested)
10272 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010273}
10274
Yang Zhang25d92082013-08-06 12:00:32 +030010275static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10276 struct x86_exception *fault)
10277{
Jan Kiszka533558b2014-01-04 18:47:20 +010010278 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010279 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010280 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010281 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010282
Bandan Dasc5f983f2017-05-05 15:25:14 -040010283 if (vmx->nested.pml_full) {
10284 exit_reason = EXIT_REASON_PML_FULL;
10285 vmx->nested.pml_full = false;
10286 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10287 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010288 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010289 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010290 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010291
10292 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010293 vmcs12->guest_physical_address = fault->address;
10294}
10295
Peter Feiner995f00a2017-06-30 17:26:32 -070010296static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10297{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010298 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010299}
10300
Nadav Har'El155a97a2013-08-05 11:07:16 +030010301/* Callbacks for nested_ept_init_mmu_context: */
10302
10303static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10304{
10305 /* return the page table to be shadowed - in our case, EPT12 */
10306 return get_vmcs12(vcpu)->ept_pointer;
10307}
10308
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010309static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010310{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010311 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010312 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010313 return 1;
10314
10315 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010316 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010317 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010318 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010319 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010320 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10321 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10322 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10323
10324 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010325 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010326}
10327
10328static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10329{
10330 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10331}
10332
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010333static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10334 u16 error_code)
10335{
10336 bool inequality, bit;
10337
10338 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10339 inequality =
10340 (error_code & vmcs12->page_fault_error_code_mask) !=
10341 vmcs12->page_fault_error_code_match;
10342 return inequality ^ bit;
10343}
10344
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010345static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10346 struct x86_exception *fault)
10347{
10348 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10349
10350 WARN_ON(!is_guest_mode(vcpu));
10351
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010352 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10353 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010354 vmcs12->vm_exit_intr_error_code = fault->error_code;
10355 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10356 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10357 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10358 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010359 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010360 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010361 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010362}
10363
Paolo Bonzinic9923842017-12-13 14:16:30 +010010364static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10365 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010366
10367static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010368 struct vmcs12 *vmcs12)
10369{
10370 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010371 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010372 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010373
10374 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010375 /*
10376 * Translate L1 physical address to host physical
10377 * address for vmcs02. Keep the page pinned, so this
10378 * physical address remains valid. We keep a reference
10379 * to it so we can release it later.
10380 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010381 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010382 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010383 vmx->nested.apic_access_page = NULL;
10384 }
10385 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010386 /*
10387 * If translation failed, no matter: This feature asks
10388 * to exit when accessing the given address, and if it
10389 * can never be accessed, this feature won't do
10390 * anything anyway.
10391 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010392 if (!is_error_page(page)) {
10393 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010394 hpa = page_to_phys(vmx->nested.apic_access_page);
10395 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10396 } else {
10397 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10398 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10399 }
10400 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
10401 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
10402 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
10403 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10404 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010405 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010406
10407 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010408 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010409 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010410 vmx->nested.virtual_apic_page = NULL;
10411 }
10412 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010413
10414 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010415 * If translation failed, VM entry will fail because
10416 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10417 * Failing the vm entry is _not_ what the processor
10418 * does but it's basically the only possibility we
10419 * have. We could still enter the guest if CR8 load
10420 * exits are enabled, CR8 store exits are enabled, and
10421 * virtualize APIC access is disabled; in this case
10422 * the processor would never use the TPR shadow and we
10423 * could simply clear the bit from the execution
10424 * control. But such a configuration is useless, so
10425 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010426 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010427 if (!is_error_page(page)) {
10428 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010429 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10430 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10431 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010432 }
10433
Wincy Van705699a2015-02-03 23:58:17 +080010434 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010435 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10436 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010437 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010438 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010439 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010440 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10441 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010442 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010443 vmx->nested.pi_desc_page = page;
10444 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010445 vmx->nested.pi_desc =
10446 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10447 (unsigned long)(vmcs12->posted_intr_desc_addr &
10448 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010449 vmcs_write64(POSTED_INTR_DESC_ADDR,
10450 page_to_phys(vmx->nested.pi_desc_page) +
10451 (unsigned long)(vmcs12->posted_intr_desc_addr &
10452 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010453 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010454 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010455 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10456 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010457 else
10458 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10459 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010460}
10461
Jan Kiszkaf41245002014-03-07 20:03:13 +010010462static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10463{
10464 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10465 struct vcpu_vmx *vmx = to_vmx(vcpu);
10466
10467 if (vcpu->arch.virtual_tsc_khz == 0)
10468 return;
10469
10470 /* Make sure short timeouts reliably trigger an immediate vmexit.
10471 * hrtimer_start does not guarantee this. */
10472 if (preemption_timeout <= 1) {
10473 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10474 return;
10475 }
10476
10477 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10478 preemption_timeout *= 1000000;
10479 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10480 hrtimer_start(&vmx->nested.preemption_timer,
10481 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10482}
10483
Jim Mattson56a20512017-07-06 16:33:06 -070010484static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10485 struct vmcs12 *vmcs12)
10486{
10487 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10488 return 0;
10489
10490 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10491 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10492 return -EINVAL;
10493
10494 return 0;
10495}
10496
Wincy Van3af18d92015-02-03 23:49:31 +080010497static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10498 struct vmcs12 *vmcs12)
10499{
Wincy Van3af18d92015-02-03 23:49:31 +080010500 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10501 return 0;
10502
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010503 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010504 return -EINVAL;
10505
10506 return 0;
10507}
10508
Jim Mattson712b12d2017-08-24 13:24:47 -070010509static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10510 struct vmcs12 *vmcs12)
10511{
10512 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10513 return 0;
10514
10515 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10516 return -EINVAL;
10517
10518 return 0;
10519}
10520
Wincy Van3af18d92015-02-03 23:49:31 +080010521/*
10522 * Merge L0's and L1's MSR bitmap, return false to indicate that
10523 * we do not use the hardware.
10524 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010525static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10526 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010527{
Wincy Van82f0dd42015-02-03 23:57:18 +080010528 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010529 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010530 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010531 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010532 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010533 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010534 *
10535 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10536 * ensures that we do not accidentally generate an L02 MSR bitmap
10537 * from the L12 MSR bitmap that is too permissive.
10538 * 2. That L1 or L2s have actually used the MSR. This avoids
10539 * unnecessarily merging of the bitmap if the MSR is unused. This
10540 * works properly because we only update the L01 MSR bitmap lazily.
10541 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10542 * updated to reflect this when L1 (or its L2s) actually write to
10543 * the MSR.
10544 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010545 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10546 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010547
Paolo Bonzinic9923842017-12-13 14:16:30 +010010548 /* Nothing to do if the MSR bitmap is not in use. */
10549 if (!cpu_has_vmx_msr_bitmap() ||
10550 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10551 return false;
10552
Ashok Raj15d45072018-02-01 22:59:43 +010010553 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010554 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010555 return false;
10556
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010557 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10558 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010559 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010560
Radim Krčmářd048c092016-08-08 20:16:22 +020010561 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010562 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10563 /*
10564 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10565 * just lets the processor take the value from the virtual-APIC page;
10566 * take those 256 bits directly from the L1 bitmap.
10567 */
10568 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10569 unsigned word = msr / BITS_PER_LONG;
10570 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10571 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010572 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010573 } else {
10574 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10575 unsigned word = msr / BITS_PER_LONG;
10576 msr_bitmap_l0[word] = ~0;
10577 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10578 }
10579 }
10580
10581 nested_vmx_disable_intercept_for_msr(
10582 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010583 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010584 MSR_TYPE_W);
10585
10586 if (nested_cpu_has_vid(vmcs12)) {
10587 nested_vmx_disable_intercept_for_msr(
10588 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010589 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010590 MSR_TYPE_W);
10591 nested_vmx_disable_intercept_for_msr(
10592 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010593 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010594 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010595 }
Ashok Raj15d45072018-02-01 22:59:43 +010010596
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010597 if (spec_ctrl)
10598 nested_vmx_disable_intercept_for_msr(
10599 msr_bitmap_l1, msr_bitmap_l0,
10600 MSR_IA32_SPEC_CTRL,
10601 MSR_TYPE_R | MSR_TYPE_W);
10602
Ashok Raj15d45072018-02-01 22:59:43 +010010603 if (pred_cmd)
10604 nested_vmx_disable_intercept_for_msr(
10605 msr_bitmap_l1, msr_bitmap_l0,
10606 MSR_IA32_PRED_CMD,
10607 MSR_TYPE_W);
10608
Wincy Vanf2b93282015-02-03 23:56:03 +080010609 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010610 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080010611
10612 return true;
10613}
10614
10615static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
10616 struct vmcs12 *vmcs12)
10617{
Wincy Van82f0dd42015-02-03 23:57:18 +080010618 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080010619 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080010620 !nested_cpu_has_vid(vmcs12) &&
10621 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080010622 return 0;
10623
10624 /*
10625 * If virtualize x2apic mode is enabled,
10626 * virtualize apic access must be disabled.
10627 */
Wincy Van82f0dd42015-02-03 23:57:18 +080010628 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
10629 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080010630 return -EINVAL;
10631
Wincy Van608406e2015-02-03 23:57:51 +080010632 /*
10633 * If virtual interrupt delivery is enabled,
10634 * we must exit on external interrupts.
10635 */
10636 if (nested_cpu_has_vid(vmcs12) &&
10637 !nested_exit_on_intr(vcpu))
10638 return -EINVAL;
10639
Wincy Van705699a2015-02-03 23:58:17 +080010640 /*
10641 * bits 15:8 should be zero in posted_intr_nv,
10642 * the descriptor address has been already checked
10643 * in nested_get_vmcs12_pages.
10644 */
10645 if (nested_cpu_has_posted_intr(vmcs12) &&
10646 (!nested_cpu_has_vid(vmcs12) ||
10647 !nested_exit_intr_ack_set(vcpu) ||
10648 vmcs12->posted_intr_nv & 0xff00))
10649 return -EINVAL;
10650
Wincy Vanf2b93282015-02-03 23:56:03 +080010651 /* tpr shadow is needed by all apicv features. */
10652 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10653 return -EINVAL;
10654
10655 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080010656}
10657
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010658static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
10659 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010660 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030010661{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010662 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010663 u64 count, addr;
10664
10665 if (vmcs12_read_any(vcpu, count_field, &count) ||
10666 vmcs12_read_any(vcpu, addr_field, &addr)) {
10667 WARN_ON(1);
10668 return -EINVAL;
10669 }
10670 if (count == 0)
10671 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010672 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010673 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
10674 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010675 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010676 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
10677 addr_field, maxphyaddr, count, addr);
10678 return -EINVAL;
10679 }
10680 return 0;
10681}
10682
10683static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
10684 struct vmcs12 *vmcs12)
10685{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010686 if (vmcs12->vm_exit_msr_load_count == 0 &&
10687 vmcs12->vm_exit_msr_store_count == 0 &&
10688 vmcs12->vm_entry_msr_load_count == 0)
10689 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010690 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010691 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010692 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010693 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010694 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030010695 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030010696 return -EINVAL;
10697 return 0;
10698}
10699
Bandan Dasc5f983f2017-05-05 15:25:14 -040010700static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
10701 struct vmcs12 *vmcs12)
10702{
10703 u64 address = vmcs12->pml_address;
10704 int maxphyaddr = cpuid_maxphyaddr(vcpu);
10705
10706 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
10707 if (!nested_cpu_has_ept(vmcs12) ||
10708 !IS_ALIGNED(address, 4096) ||
10709 address >> maxphyaddr)
10710 return -EINVAL;
10711 }
10712
10713 return 0;
10714}
10715
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010716static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
10717 struct vmx_msr_entry *e)
10718{
10719 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020010720 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010721 return -EINVAL;
10722 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
10723 e->index == MSR_IA32_UCODE_REV)
10724 return -EINVAL;
10725 if (e->reserved != 0)
10726 return -EINVAL;
10727 return 0;
10728}
10729
10730static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
10731 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030010732{
10733 if (e->index == MSR_FS_BASE ||
10734 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010735 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
10736 nested_vmx_msr_check_common(vcpu, e))
10737 return -EINVAL;
10738 return 0;
10739}
10740
10741static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
10742 struct vmx_msr_entry *e)
10743{
10744 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
10745 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030010746 return -EINVAL;
10747 return 0;
10748}
10749
10750/*
10751 * Load guest's/host's msr at nested entry/exit.
10752 * return 0 for success, entry index for failure.
10753 */
10754static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10755{
10756 u32 i;
10757 struct vmx_msr_entry e;
10758 struct msr_data msr;
10759
10760 msr.host_initiated = false;
10761 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010762 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
10763 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010764 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010765 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10766 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010767 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010768 }
10769 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010770 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010771 "%s check failed (%u, 0x%x, 0x%x)\n",
10772 __func__, i, e.index, e.reserved);
10773 goto fail;
10774 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010775 msr.index = e.index;
10776 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010777 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010778 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010779 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10780 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010781 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010782 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010783 }
10784 return 0;
10785fail:
10786 return i + 1;
10787}
10788
10789static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10790{
10791 u32 i;
10792 struct vmx_msr_entry e;
10793
10794 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010795 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010796 if (kvm_vcpu_read_guest(vcpu,
10797 gpa + i * sizeof(e),
10798 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010799 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010800 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10801 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010802 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010803 }
10804 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010805 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010806 "%s check failed (%u, 0x%x, 0x%x)\n",
10807 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010808 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010809 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010810 msr_info.host_initiated = false;
10811 msr_info.index = e.index;
10812 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010813 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010814 "%s cannot read MSR (%u, 0x%x)\n",
10815 __func__, i, e.index);
10816 return -EINVAL;
10817 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010818 if (kvm_vcpu_write_guest(vcpu,
10819 gpa + i * sizeof(e) +
10820 offsetof(struct vmx_msr_entry, value),
10821 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010822 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010823 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010824 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010825 return -EINVAL;
10826 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010827 }
10828 return 0;
10829}
10830
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010831static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
10832{
10833 unsigned long invalid_mask;
10834
10835 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
10836 return (val & invalid_mask) == 0;
10837}
10838
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010839/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010840 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
10841 * emulating VM entry into a guest with EPT enabled.
10842 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
10843 * is assigned to entry_failure_code on failure.
10844 */
10845static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080010846 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010847{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010848 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010849 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010850 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10851 return 1;
10852 }
10853
10854 /*
10855 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
10856 * must not be dereferenced.
10857 */
10858 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
10859 !nested_ept) {
10860 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
10861 *entry_failure_code = ENTRY_FAIL_PDPTE;
10862 return 1;
10863 }
10864 }
10865
10866 vcpu->arch.cr3 = cr3;
10867 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
10868 }
10869
10870 kvm_mmu_reset_context(vcpu);
10871 return 0;
10872}
10873
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010874static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10875 bool from_vmentry)
10876{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010010877 struct vcpu_vmx *vmx = to_vmx(vcpu);
10878
10879 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10880 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10881 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10882 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10883 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10884 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10885 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10886 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10887 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10888 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10889 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10890 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10891 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10892 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10893 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10894 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10895 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10896 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10897 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10898 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10899 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10900 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10901 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10902 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10903 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10904 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10905 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10906 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10907 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10908 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10909 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010010910
10911 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
10912 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10913 vmcs12->guest_pending_dbg_exceptions);
10914 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10915 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10916
10917 if (nested_cpu_has_xsaves(vmcs12))
10918 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
10919 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10920
10921 if (cpu_has_vmx_posted_intr())
10922 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
10923
10924 /*
10925 * Whether page-faults are trapped is determined by a combination of
10926 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10927 * If enable_ept, L0 doesn't care about page faults and we should
10928 * set all of these to L1's desires. However, if !enable_ept, L0 does
10929 * care about (at least some) page faults, and because it is not easy
10930 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10931 * to exit on each and every L2 page fault. This is done by setting
10932 * MASK=MATCH=0 and (see below) EB.PF=1.
10933 * Note that below we don't need special code to set EB.PF beyond the
10934 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10935 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10936 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10937 */
10938 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10939 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10940 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10941 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10942
10943 /* All VMFUNCs are currently emulated through L0 vmexits. */
10944 if (cpu_has_vmx_vmfunc())
10945 vmcs_write64(VM_FUNCTION_CONTROL, 0);
10946
10947 if (cpu_has_vmx_apicv()) {
10948 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
10949 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
10950 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
10951 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
10952 }
10953
10954 /*
10955 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10956 * Some constant fields are set here by vmx_set_constant_host_state().
10957 * Other fields are different per CPU, and will be set later when
10958 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10959 */
10960 vmx_set_constant_host_state(vmx);
10961
10962 /*
10963 * Set the MSR load/store lists to match L0's settings.
10964 */
10965 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10966 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10967 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10968 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10969 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10970
10971 set_cr4_guest_host_mask(vmx);
10972
10973 if (vmx_mpx_supported())
10974 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10975
10976 if (enable_vpid) {
10977 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
10978 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10979 else
10980 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10981 }
10982
10983 /*
10984 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10985 */
10986 if (enable_ept) {
10987 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10988 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10989 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10990 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10991 }
Radim Krčmář80132f42018-02-02 18:26:58 +010010992
10993 if (cpu_has_vmx_msr_bitmap())
10994 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010010995}
10996
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010997/*
10998 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10999 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011000 * 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 +030011001 * guest in a way that will both be appropriate to L1's requests, and our
11002 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11003 * function also has additional necessary side-effects, like setting various
11004 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011005 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11006 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011007 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011008static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011009 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011010{
11011 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011012 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011013
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011014 /*
11015 * First, the fields that are shadowed. This must be kept in sync
11016 * with vmx_shadow_fields.h.
11017 */
11018
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011019 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011020 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011021 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011022 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11023 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011024
11025 /*
11026 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11027 * HOST_FS_BASE, HOST_GS_BASE.
11028 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011029
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011030 if (from_vmentry &&
11031 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011032 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11033 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11034 } else {
11035 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11036 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11037 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011038 if (from_vmentry) {
11039 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11040 vmcs12->vm_entry_intr_info_field);
11041 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11042 vmcs12->vm_entry_exception_error_code);
11043 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11044 vmcs12->vm_entry_instruction_len);
11045 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11046 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011047 vmx->loaded_vmcs->nmi_known_unmasked =
11048 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011049 } else {
11050 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11051 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011052 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011053
Jan Kiszkaf41245002014-03-07 20:03:13 +010011054 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011055
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011056 /* Preemption timer setting is only taken from vmcs01. */
11057 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11058 exec_control |= vmcs_config.pin_based_exec_ctrl;
11059 if (vmx->hv_deadline_tsc == -1)
11060 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11061
11062 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011063 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011064 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11065 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011066 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011067 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011068 }
Wincy Van705699a2015-02-03 23:58:17 +080011069
Jan Kiszkaf41245002014-03-07 20:03:13 +010011070 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011071
Jan Kiszkaf41245002014-03-07 20:03:13 +010011072 vmx->nested.preemption_timer_expired = false;
11073 if (nested_cpu_has_preemption_timer(vmcs12))
11074 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011075
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011076 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011077 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011078
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011079 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011080 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011081 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011082 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011083 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011084 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011085 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11086 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011087 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011088 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11089 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11090 ~SECONDARY_EXEC_ENABLE_PML;
11091 exec_control |= vmcs12_exec_ctrl;
11092 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011093
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011094 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011095 vmcs_write16(GUEST_INTR_STATUS,
11096 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011097
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011098 /*
11099 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11100 * nested_get_vmcs12_pages will either fix it up or
11101 * remove the VM execution control.
11102 */
11103 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11104 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11105
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011106 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11107 }
11108
Jim Mattson83bafef2016-10-04 10:48:38 -070011109 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011110 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11111 * entry, but only if the current (host) sp changed from the value
11112 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11113 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11114 * here we just force the write to happen on entry.
11115 */
11116 vmx->host_rsp = 0;
11117
11118 exec_control = vmx_exec_control(vmx); /* L0's desires */
11119 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11120 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11121 exec_control &= ~CPU_BASED_TPR_SHADOW;
11122 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011123
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011124 /*
11125 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11126 * nested_get_vmcs12_pages can't fix it up, the illegal value
11127 * will result in a VM entry failure.
11128 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011129 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011130 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011131 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011132 } else {
11133#ifdef CONFIG_X86_64
11134 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11135 CPU_BASED_CR8_STORE_EXITING;
11136#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011137 }
11138
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011139 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011140 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11141 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011142 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011143 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11144 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11145
11146 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11147
11148 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11149 * bitwise-or of what L1 wants to trap for L2, and what we want to
11150 * trap. Note that CR0.TS also needs updating - we do this later.
11151 */
11152 update_exception_bitmap(vcpu);
11153 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11154 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11155
Nadav Har'El8049d652013-08-05 11:07:06 +030011156 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11157 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11158 * bits are further modified by vmx_set_efer() below.
11159 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011160 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011161
11162 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11163 * emulated by vmx_set_efer(), below.
11164 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011165 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011166 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11167 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011168 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11169
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011170 if (from_vmentry &&
11171 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011172 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011173 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011174 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011175 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011176 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011177
Nadav Har'El27fc51b2011-08-02 15:54:52 +030011178 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11179 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011180 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030011181 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011182 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070011183 if (kvm_has_tsc_control)
11184 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011185
11186 if (enable_vpid) {
11187 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011188 * There is no direct mapping between vpid02 and vpid12, the
11189 * vpid02 is per-vCPU for L0 and reused while the value of
11190 * vpid12 is changed w/ one invvpid during nested vmentry.
11191 * The vpid12 is allocated by L1 for L2, so it will not
11192 * influence global bitmap(for vpid01 and vpid02 allocation)
11193 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011194 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011195 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011196 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11197 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011198 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011199 }
11200 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011201 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011202 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011203 }
11204
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011205 if (enable_pml) {
11206 /*
11207 * Conceptually we want to copy the PML address and index from
11208 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11209 * since we always flush the log on each vmexit, this happens
11210 * to be equivalent to simply resetting the fields in vmcs02.
11211 */
11212 ASSERT(vmx->pml_pg);
11213 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11214 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11215 }
11216
Nadav Har'El155a97a2013-08-05 11:07:16 +030011217 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011218 if (nested_ept_init_mmu_context(vcpu)) {
11219 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11220 return 1;
11221 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011222 } else if (nested_cpu_has2(vmcs12,
11223 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11224 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011225 }
11226
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011227 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011228 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11229 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011230 * The CR0_READ_SHADOW is what L2 should have expected to read given
11231 * the specifications by L1; It's not enough to take
11232 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11233 * have more bits than L1 expected.
11234 */
11235 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11236 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11237
11238 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11239 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11240
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011241 if (from_vmentry &&
11242 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011243 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11244 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11245 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11246 else
11247 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11248 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11249 vmx_set_efer(vcpu, vcpu->arch.efer);
11250
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011251 if (vmx->nested.dirty_vmcs12) {
11252 prepare_vmcs02_full(vcpu, vmcs12, from_vmentry);
11253 vmx->nested.dirty_vmcs12 = false;
11254 }
11255
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011256 /*
11257 * Guest state is invalid and unrestricted guest is disabled,
11258 * which means L1 attempted VMEntry to L2 with invalid state.
11259 * Fail the VMEntry.
11260 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011261 if (vmx->emulation_required) {
11262 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011263 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011264 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011265
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011266 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011267 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011268 entry_failure_code))
11269 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011270
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011271 if (!enable_ept)
11272 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11273
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011274 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11275 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011276 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011277}
11278
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011279static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11280{
11281 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11282 nested_cpu_has_virtual_nmis(vmcs12))
11283 return -EINVAL;
11284
11285 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11286 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11287 return -EINVAL;
11288
11289 return 0;
11290}
11291
Jim Mattsonca0bde22016-11-30 12:03:46 -080011292static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11293{
11294 struct vcpu_vmx *vmx = to_vmx(vcpu);
11295
11296 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11297 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11298 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11299
Jim Mattson56a20512017-07-06 16:33:06 -070011300 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11301 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11302
Jim Mattsonca0bde22016-11-30 12:03:46 -080011303 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11304 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11305
Jim Mattson712b12d2017-08-24 13:24:47 -070011306 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11307 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11308
Jim Mattsonca0bde22016-11-30 12:03:46 -080011309 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11310 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11311
11312 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11313 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11314
Bandan Dasc5f983f2017-05-05 15:25:14 -040011315 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11316 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11317
Jim Mattsonca0bde22016-11-30 12:03:46 -080011318 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011319 vmx->nested.msrs.procbased_ctls_low,
11320 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011321 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11322 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011323 vmx->nested.msrs.secondary_ctls_low,
11324 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011325 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011326 vmx->nested.msrs.pinbased_ctls_low,
11327 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011328 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011329 vmx->nested.msrs.exit_ctls_low,
11330 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011331 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011332 vmx->nested.msrs.entry_ctls_low,
11333 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011334 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11335
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011336 if (nested_vmx_check_nmi_controls(vmcs12))
11337 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11338
Bandan Das41ab9372017-08-03 15:54:43 -040011339 if (nested_cpu_has_vmfunc(vmcs12)) {
11340 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011341 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011342 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11343
11344 if (nested_cpu_has_eptp_switching(vmcs12)) {
11345 if (!nested_cpu_has_ept(vmcs12) ||
11346 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11347 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11348 }
11349 }
Bandan Das27c42a12017-08-03 15:54:42 -040011350
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011351 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11352 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11353
Jim Mattsonca0bde22016-11-30 12:03:46 -080011354 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11355 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11356 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11357 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11358
11359 return 0;
11360}
11361
11362static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11363 u32 *exit_qual)
11364{
11365 bool ia32e;
11366
11367 *exit_qual = ENTRY_FAIL_DEFAULT;
11368
11369 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11370 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11371 return 1;
11372
11373 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11374 vmcs12->vmcs_link_pointer != -1ull) {
11375 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11376 return 1;
11377 }
11378
11379 /*
11380 * If the load IA32_EFER VM-entry control is 1, the following checks
11381 * are performed on the field for the IA32_EFER MSR:
11382 * - Bits reserved in the IA32_EFER MSR must be 0.
11383 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11384 * the IA-32e mode guest VM-exit control. It must also be identical
11385 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11386 * CR0.PG) is 1.
11387 */
11388 if (to_vmx(vcpu)->nested.nested_run_pending &&
11389 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11390 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11391 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11392 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11393 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11394 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11395 return 1;
11396 }
11397
11398 /*
11399 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11400 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11401 * the values of the LMA and LME bits in the field must each be that of
11402 * the host address-space size VM-exit control.
11403 */
11404 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11405 ia32e = (vmcs12->vm_exit_controls &
11406 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11407 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11408 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11409 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11410 return 1;
11411 }
11412
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011413 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11414 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11415 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11416 return 1;
11417
Jim Mattsonca0bde22016-11-30 12:03:46 -080011418 return 0;
11419}
11420
Jim Mattson858e25c2016-11-30 12:03:47 -080011421static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
11422{
11423 struct vcpu_vmx *vmx = to_vmx(vcpu);
11424 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011425 u32 msr_entry_idx;
11426 u32 exit_qual;
11427
Jim Mattson858e25c2016-11-30 12:03:47 -080011428 enter_guest_mode(vcpu);
11429
11430 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11431 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11432
Jim Mattsonde3a0022017-11-27 17:22:25 -060011433 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011434 vmx_segment_cache_clear(vmx);
11435
11436 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
11437 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011438 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011439 nested_vmx_entry_failure(vcpu, vmcs12,
11440 EXIT_REASON_INVALID_STATE, exit_qual);
11441 return 1;
11442 }
11443
11444 nested_get_vmcs12_pages(vcpu, vmcs12);
11445
11446 msr_entry_idx = nested_vmx_load_msr(vcpu,
11447 vmcs12->vm_entry_msr_load_addr,
11448 vmcs12->vm_entry_msr_load_count);
11449 if (msr_entry_idx) {
11450 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010011451 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080011452 nested_vmx_entry_failure(vcpu, vmcs12,
11453 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
11454 return 1;
11455 }
11456
Jim Mattson858e25c2016-11-30 12:03:47 -080011457 /*
11458 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11459 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11460 * returned as far as L1 is concerned. It will only return (and set
11461 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11462 */
11463 return 0;
11464}
11465
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011466/*
11467 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11468 * for running an L2 nested guest.
11469 */
11470static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11471{
11472 struct vmcs12 *vmcs12;
11473 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011474 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011475 u32 exit_qual;
11476 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011477
Kyle Hueyeb277562016-11-29 12:40:39 -080011478 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011479 return 1;
11480
Kyle Hueyeb277562016-11-29 12:40:39 -080011481 if (!nested_vmx_check_vmcs12(vcpu))
11482 goto out;
11483
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011484 vmcs12 = get_vmcs12(vcpu);
11485
Abel Gordon012f83c2013-04-18 14:39:25 +030011486 if (enable_shadow_vmcs)
11487 copy_shadow_to_vmcs12(vmx);
11488
Nadav Har'El7c177932011-05-25 23:12:04 +030011489 /*
11490 * The nested entry process starts with enforcing various prerequisites
11491 * on vmcs12 as required by the Intel SDM, and act appropriately when
11492 * they fail: As the SDM explains, some conditions should cause the
11493 * instruction to fail, while others will cause the instruction to seem
11494 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11495 * To speed up the normal (success) code path, we should avoid checking
11496 * for misconfigurations which will anyway be caught by the processor
11497 * when using the merged vmcs02.
11498 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011499 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11500 nested_vmx_failValid(vcpu,
11501 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11502 goto out;
11503 }
11504
Nadav Har'El7c177932011-05-25 23:12:04 +030011505 if (vmcs12->launch_state == launch) {
11506 nested_vmx_failValid(vcpu,
11507 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11508 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011509 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011510 }
11511
Jim Mattsonca0bde22016-11-30 12:03:46 -080011512 ret = check_vmentry_prereqs(vcpu, vmcs12);
11513 if (ret) {
11514 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011515 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011516 }
11517
Nadav Har'El7c177932011-05-25 23:12:04 +030011518 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011519 * After this point, the trap flag no longer triggers a singlestep trap
11520 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11521 * This is not 100% correct; for performance reasons, we delegate most
11522 * of the checks on host state to the processor. If those fail,
11523 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011524 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011525 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011526
Jim Mattsonca0bde22016-11-30 12:03:46 -080011527 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11528 if (ret) {
11529 nested_vmx_entry_failure(vcpu, vmcs12,
11530 EXIT_REASON_INVALID_STATE, exit_qual);
11531 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011532 }
11533
11534 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011535 * We're finally done with prerequisite checking, and can start with
11536 * the nested entry.
11537 */
11538
Jim Mattson858e25c2016-11-30 12:03:47 -080011539 ret = enter_vmx_non_root_mode(vcpu, true);
11540 if (ret)
11541 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030011542
Chao Gao135a06c2018-02-11 10:06:30 +080011543 /*
11544 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11545 * by event injection, halt vcpu.
11546 */
11547 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
11548 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK))
Joel Schopp5cb56052015-03-02 13:43:31 -060011549 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010011550
Jan Kiszka7af40ad32014-01-04 18:47:23 +010011551 vmx->nested.nested_run_pending = 1;
11552
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011553 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011554
11555out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011556 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011557}
11558
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011559/*
11560 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11561 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11562 * This function returns the new value we should put in vmcs12.guest_cr0.
11563 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11564 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11565 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11566 * didn't trap the bit, because if L1 did, so would L0).
11567 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11568 * been modified by L2, and L1 knows it. So just leave the old value of
11569 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11570 * isn't relevant, because if L0 traps this bit it can set it to anything.
11571 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11572 * changed these bits, and therefore they need to be updated, but L0
11573 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11574 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11575 */
11576static inline unsigned long
11577vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11578{
11579 return
11580 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11581 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
11582 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
11583 vcpu->arch.cr0_guest_owned_bits));
11584}
11585
11586static inline unsigned long
11587vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11588{
11589 return
11590 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
11591 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
11592 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
11593 vcpu->arch.cr4_guest_owned_bits));
11594}
11595
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011596static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
11597 struct vmcs12 *vmcs12)
11598{
11599 u32 idt_vectoring;
11600 unsigned int nr;
11601
Wanpeng Li664f8e22017-08-24 03:35:09 -070011602 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011603 nr = vcpu->arch.exception.nr;
11604 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11605
11606 if (kvm_exception_is_soft(nr)) {
11607 vmcs12->vm_exit_instruction_len =
11608 vcpu->arch.event_exit_inst_len;
11609 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
11610 } else
11611 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
11612
11613 if (vcpu->arch.exception.has_error_code) {
11614 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
11615 vmcs12->idt_vectoring_error_code =
11616 vcpu->arch.exception.error_code;
11617 }
11618
11619 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010011620 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011621 vmcs12->idt_vectoring_info_field =
11622 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030011623 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011624 nr = vcpu->arch.interrupt.nr;
11625 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
11626
11627 if (vcpu->arch.interrupt.soft) {
11628 idt_vectoring |= INTR_TYPE_SOFT_INTR;
11629 vmcs12->vm_entry_instruction_len =
11630 vcpu->arch.event_exit_inst_len;
11631 } else
11632 idt_vectoring |= INTR_TYPE_EXT_INTR;
11633
11634 vmcs12->idt_vectoring_info_field = idt_vectoring;
11635 }
11636}
11637
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011638static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
11639{
11640 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011641 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020011642 bool block_nested_events =
11643 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080011644
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011645 if (vcpu->arch.exception.pending &&
11646 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020011647 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011648 return -EBUSY;
11649 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070011650 return 0;
11651 }
11652
Jan Kiszkaf41245002014-03-07 20:03:13 +010011653 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
11654 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020011655 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010011656 return -EBUSY;
11657 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
11658 return 0;
11659 }
11660
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011661 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011662 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011663 return -EBUSY;
11664 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11665 NMI_VECTOR | INTR_TYPE_NMI_INTR |
11666 INTR_INFO_VALID_MASK, 0);
11667 /*
11668 * The NMI-triggered VM exit counts as injection:
11669 * clear this one and block further NMIs.
11670 */
11671 vcpu->arch.nmi_pending = 0;
11672 vmx_set_nmi_mask(vcpu, true);
11673 return 0;
11674 }
11675
11676 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
11677 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020011678 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011679 return -EBUSY;
11680 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080011681 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011682 }
11683
David Hildenbrand6342c502017-01-25 11:58:58 +010011684 vmx_complete_nested_posted_interrupt(vcpu);
11685 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011686}
11687
Jan Kiszkaf41245002014-03-07 20:03:13 +010011688static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
11689{
11690 ktime_t remaining =
11691 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
11692 u64 value;
11693
11694 if (ktime_to_ns(remaining) <= 0)
11695 return 0;
11696
11697 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
11698 do_div(value, 1000000);
11699 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11700}
11701
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011702/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011703 * Update the guest state fields of vmcs12 to reflect changes that
11704 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
11705 * VM-entry controls is also updated, since this is really a guest
11706 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011707 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011708static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011709{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011710 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
11711 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
11712
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011713 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
11714 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
11715 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
11716
11717 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
11718 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
11719 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
11720 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
11721 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
11722 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
11723 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
11724 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
11725 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
11726 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
11727 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
11728 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
11729 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
11730 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
11731 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
11732 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
11733 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
11734 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
11735 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
11736 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
11737 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
11738 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
11739 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
11740 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
11741 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
11742 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
11743 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
11744 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
11745 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
11746 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
11747 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
11748 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
11749 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
11750 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
11751 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
11752 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
11753
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011754 vmcs12->guest_interruptibility_info =
11755 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
11756 vmcs12->guest_pending_dbg_exceptions =
11757 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010011758 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
11759 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
11760 else
11761 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011762
Jan Kiszkaf41245002014-03-07 20:03:13 +010011763 if (nested_cpu_has_preemption_timer(vmcs12)) {
11764 if (vmcs12->vm_exit_controls &
11765 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
11766 vmcs12->vmx_preemption_timer_value =
11767 vmx_get_preemption_timer_value(vcpu);
11768 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
11769 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080011770
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011771 /*
11772 * In some cases (usually, nested EPT), L2 is allowed to change its
11773 * own CR3 without exiting. If it has changed it, we must keep it.
11774 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
11775 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
11776 *
11777 * Additionally, restore L2's PDPTR to vmcs12.
11778 */
11779 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010011780 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030011781 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
11782 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
11783 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
11784 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
11785 }
11786
Jim Mattsond281e132017-06-01 12:44:46 -070011787 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030011788
Wincy Van608406e2015-02-03 23:57:51 +080011789 if (nested_cpu_has_vid(vmcs12))
11790 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
11791
Jan Kiszkac18911a2013-03-13 16:06:41 +010011792 vmcs12->vm_entry_controls =
11793 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020011794 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010011795
Jan Kiszka2996fca2014-06-16 13:59:43 +020011796 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
11797 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
11798 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11799 }
11800
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011801 /* TODO: These cannot have changed unless we have MSR bitmaps and
11802 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020011803 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011804 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020011805 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
11806 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011807 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
11808 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
11809 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010011810 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011811 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011812}
11813
11814/*
11815 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
11816 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
11817 * and this function updates it to reflect the changes to the guest state while
11818 * L2 was running (and perhaps made some exits which were handled directly by L0
11819 * without going back to L1), and to reflect the exit reason.
11820 * Note that we do not have to copy here all VMCS fields, just those that
11821 * could have changed by the L2 guest or the exit - i.e., the guest-state and
11822 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
11823 * which already writes to vmcs12 directly.
11824 */
11825static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11826 u32 exit_reason, u32 exit_intr_info,
11827 unsigned long exit_qualification)
11828{
11829 /* update guest state fields: */
11830 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011831
11832 /* update exit information fields: */
11833
Jan Kiszka533558b2014-01-04 18:47:20 +010011834 vmcs12->vm_exit_reason = exit_reason;
11835 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010011836 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020011837
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011838 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011839 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
11840 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
11841
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011842 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070011843 vmcs12->launch_state = 1;
11844
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011845 /* vm_entry_intr_info_field is cleared on exit. Emulate this
11846 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011847 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011848
11849 /*
11850 * Transfer the event that L0 or L1 may wanted to inject into
11851 * L2 to IDT_VECTORING_INFO_FIELD.
11852 */
11853 vmcs12_save_pending_event(vcpu, vmcs12);
11854 }
11855
11856 /*
11857 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
11858 * preserved above and would only end up incorrectly in L1.
11859 */
11860 vcpu->arch.nmi_injected = false;
11861 kvm_clear_exception_queue(vcpu);
11862 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011863}
11864
Wanpeng Li5af41572017-11-05 16:54:49 -080011865static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
11866 struct vmcs12 *vmcs12)
11867{
11868 u32 entry_failure_code;
11869
11870 nested_ept_uninit_mmu_context(vcpu);
11871
11872 /*
11873 * Only PDPTE load can fail as the value of cr3 was checked on entry and
11874 * couldn't have changed.
11875 */
11876 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
11877 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
11878
11879 if (!enable_ept)
11880 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
11881}
11882
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011883/*
11884 * A part of what we need to when the nested L2 guest exits and we want to
11885 * run its L1 parent, is to reset L1's guest state to the host state specified
11886 * in vmcs12.
11887 * This function is to be called not only on normal nested exit, but also on
11888 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
11889 * Failures During or After Loading Guest State").
11890 * This function should be called when the active VMCS is L1's (vmcs01).
11891 */
Jan Kiszka733568f2013-02-23 15:07:47 +010011892static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
11893 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011894{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011895 struct kvm_segment seg;
11896
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011897 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
11898 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020011899 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011900 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11901 else
11902 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11903 vmx_set_efer(vcpu, vcpu->arch.efer);
11904
11905 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
11906 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070011907 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011908 /*
11909 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011910 * actually changed, because vmx_set_cr0 refers to efer set above.
11911 *
11912 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
11913 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011914 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011915 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020011916 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011917
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011918 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011919 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080011920 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011921
Wanpeng Li5af41572017-11-05 16:54:49 -080011922 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011923
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011924 if (enable_vpid) {
11925 /*
11926 * Trivially support vpid by letting L2s share their parent
11927 * L1's vpid. TODO: move to a more elaborate solution, giving
11928 * each L2 its own vpid and exposing the vpid feature to L1.
11929 */
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011930 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011931 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011932
11933 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
11934 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
11935 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
11936 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
11937 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020011938 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
11939 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011940
Paolo Bonzini36be0b92014-02-24 12:30:04 +010011941 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
11942 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
11943 vmcs_write64(GUEST_BNDCFGS, 0);
11944
Jan Kiszka44811c02013-08-04 17:17:27 +020011945 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011946 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011947 vcpu->arch.pat = vmcs12->host_ia32_pat;
11948 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011949 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
11950 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
11951 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011952
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011953 /* Set L1 segment info according to Intel SDM
11954 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11955 seg = (struct kvm_segment) {
11956 .base = 0,
11957 .limit = 0xFFFFFFFF,
11958 .selector = vmcs12->host_cs_selector,
11959 .type = 11,
11960 .present = 1,
11961 .s = 1,
11962 .g = 1
11963 };
11964 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11965 seg.l = 1;
11966 else
11967 seg.db = 1;
11968 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11969 seg = (struct kvm_segment) {
11970 .base = 0,
11971 .limit = 0xFFFFFFFF,
11972 .type = 3,
11973 .present = 1,
11974 .s = 1,
11975 .db = 1,
11976 .g = 1
11977 };
11978 seg.selector = vmcs12->host_ds_selector;
11979 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11980 seg.selector = vmcs12->host_es_selector;
11981 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11982 seg.selector = vmcs12->host_ss_selector;
11983 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11984 seg.selector = vmcs12->host_fs_selector;
11985 seg.base = vmcs12->host_fs_base;
11986 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11987 seg.selector = vmcs12->host_gs_selector;
11988 seg.base = vmcs12->host_gs_base;
11989 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11990 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011991 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011992 .limit = 0x67,
11993 .selector = vmcs12->host_tr_selector,
11994 .type = 11,
11995 .present = 1
11996 };
11997 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11998
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011999 kvm_set_dr(vcpu, 7, 0x400);
12000 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012001
Wincy Van3af18d92015-02-03 23:49:31 +080012002 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012003 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012004
Wincy Vanff651cb2014-12-11 08:52:58 +030012005 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12006 vmcs12->vm_exit_msr_load_count))
12007 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012008}
12009
12010/*
12011 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12012 * and modify vmcs12 to make it see what it would expect to see there if
12013 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12014 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012015static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12016 u32 exit_intr_info,
12017 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012018{
12019 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012020 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12021
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012022 /* trying to cancel vmlaunch/vmresume is a bug */
12023 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12024
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012025 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012026 * The only expected VM-instruction error is "VM entry with
12027 * invalid control field(s)." Anything else indicates a
12028 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012029 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012030 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12031 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12032
12033 leave_guest_mode(vcpu);
12034
12035 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012036 if (exit_reason == -1)
12037 sync_vmcs12(vcpu, vmcs12);
12038 else
12039 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12040 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012041
12042 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12043 vmcs12->vm_exit_msr_store_count))
12044 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012045 }
12046
Jim Mattson4f350c62017-09-14 16:31:44 -070012047 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012048 vm_entry_controls_reset_shadow(vmx);
12049 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012050 vmx_segment_cache_clear(vmx);
12051
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012052 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012053 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12054 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012055 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012056 if (vmx->hv_deadline_tsc == -1)
12057 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12058 PIN_BASED_VMX_PREEMPTION_TIMER);
12059 else
12060 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12061 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012062 if (kvm_has_tsc_control)
12063 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012064
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012065 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
12066 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
12067 vmx_set_virtual_x2apic_mode(vcpu,
12068 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012069 } else if (!nested_cpu_has_ept(vmcs12) &&
12070 nested_cpu_has2(vmcs12,
12071 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
12072 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012073 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012074
12075 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12076 vmx->host_rsp = 0;
12077
12078 /* Unpin physical memory we referred to in vmcs02 */
12079 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012080 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012081 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012082 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012083 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012084 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012085 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012086 }
Wincy Van705699a2015-02-03 23:58:17 +080012087 if (vmx->nested.pi_desc_page) {
12088 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012089 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012090 vmx->nested.pi_desc_page = NULL;
12091 vmx->nested.pi_desc = NULL;
12092 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012093
12094 /*
Tang Chen38b99172014-09-24 15:57:54 +080012095 * We are now running in L2, mmu_notifier will force to reload the
12096 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12097 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012098 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012099
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012100 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012101 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012102
12103 /* in case we halted in L2 */
12104 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012105
12106 if (likely(!vmx->fail)) {
12107 /*
12108 * TODO: SDM says that with acknowledge interrupt on
12109 * exit, bit 31 of the VM-exit interrupt information
12110 * (valid interrupt) is always set to 1 on
12111 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12112 * need kvm_cpu_has_interrupt(). See the commit
12113 * message for details.
12114 */
12115 if (nested_exit_intr_ack_set(vcpu) &&
12116 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12117 kvm_cpu_has_interrupt(vcpu)) {
12118 int irq = kvm_cpu_get_interrupt(vcpu);
12119 WARN_ON(irq < 0);
12120 vmcs12->vm_exit_intr_info = irq |
12121 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12122 }
12123
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012124 if (exit_reason != -1)
12125 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12126 vmcs12->exit_qualification,
12127 vmcs12->idt_vectoring_info_field,
12128 vmcs12->vm_exit_intr_info,
12129 vmcs12->vm_exit_intr_error_code,
12130 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012131
12132 load_vmcs12_host_state(vcpu, vmcs12);
12133
12134 return;
12135 }
12136
12137 /*
12138 * After an early L2 VM-entry failure, we're now back
12139 * in L1 which thinks it just finished a VMLAUNCH or
12140 * VMRESUME instruction, so we need to set the failure
12141 * flag and the VM-instruction error field of the VMCS
12142 * accordingly.
12143 */
12144 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012145
12146 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12147
Jim Mattson4f350c62017-09-14 16:31:44 -070012148 /*
12149 * The emulated instruction was already skipped in
12150 * nested_vmx_run, but the updated RIP was never
12151 * written back to the vmcs01.
12152 */
12153 skip_emulated_instruction(vcpu);
12154 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012155}
12156
Nadav Har'El7c177932011-05-25 23:12:04 +030012157/*
Jan Kiszka42124922014-01-04 18:47:19 +010012158 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12159 */
12160static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12161{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012162 if (is_guest_mode(vcpu)) {
12163 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012164 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012165 }
Jan Kiszka42124922014-01-04 18:47:19 +010012166 free_nested(to_vmx(vcpu));
12167}
12168
12169/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012170 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12171 * 23.7 "VM-entry failures during or after loading guest state" (this also
12172 * lists the acceptable exit-reason and exit-qualification parameters).
12173 * It should only be called before L2 actually succeeded to run, and when
12174 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12175 */
12176static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12177 struct vmcs12 *vmcs12,
12178 u32 reason, unsigned long qualification)
12179{
12180 load_vmcs12_host_state(vcpu, vmcs12);
12181 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12182 vmcs12->exit_qualification = qualification;
12183 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012184 if (enable_shadow_vmcs)
12185 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012186}
12187
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012188static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12189 struct x86_instruction_info *info,
12190 enum x86_intercept_stage stage)
12191{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012192 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12193 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12194
12195 /*
12196 * RDPID causes #UD if disabled through secondary execution controls.
12197 * Because it is marked as EmulateOnUD, we need to intercept it here.
12198 */
12199 if (info->intercept == x86_intercept_rdtscp &&
12200 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12201 ctxt->exception.vector = UD_VECTOR;
12202 ctxt->exception.error_code_valid = false;
12203 return X86EMUL_PROPAGATE_FAULT;
12204 }
12205
12206 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012207 return X86EMUL_CONTINUE;
12208}
12209
Yunhong Jiang64672c92016-06-13 14:19:59 -070012210#ifdef CONFIG_X86_64
12211/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12212static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12213 u64 divisor, u64 *result)
12214{
12215 u64 low = a << shift, high = a >> (64 - shift);
12216
12217 /* To avoid the overflow on divq */
12218 if (high >= divisor)
12219 return 1;
12220
12221 /* Low hold the result, high hold rem which is discarded */
12222 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12223 "rm" (divisor), "0" (low), "1" (high));
12224 *result = low;
12225
12226 return 0;
12227}
12228
12229static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12230{
12231 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020012232 u64 tscl = rdtsc();
12233 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12234 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012235
12236 /* Convert to host delta tsc if tsc scaling is enabled */
12237 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12238 u64_shl_div_u64(delta_tsc,
12239 kvm_tsc_scaling_ratio_frac_bits,
12240 vcpu->arch.tsc_scaling_ratio,
12241 &delta_tsc))
12242 return -ERANGE;
12243
12244 /*
12245 * If the delta tsc can't fit in the 32 bit after the multi shift,
12246 * we can't use the preemption timer.
12247 * It's possible that it fits on later vmentries, but checking
12248 * on every vmentry is costly so we just use an hrtimer.
12249 */
12250 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12251 return -ERANGE;
12252
12253 vmx->hv_deadline_tsc = tscl + delta_tsc;
12254 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12255 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012256
12257 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012258}
12259
12260static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12261{
12262 struct vcpu_vmx *vmx = to_vmx(vcpu);
12263 vmx->hv_deadline_tsc = -1;
12264 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12265 PIN_BASED_VMX_PREEMPTION_TIMER);
12266}
12267#endif
12268
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012269static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012270{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012271 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012272 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012273}
12274
Kai Huang843e4332015-01-28 10:54:28 +080012275static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12276 struct kvm_memory_slot *slot)
12277{
12278 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12279 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12280}
12281
12282static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12283 struct kvm_memory_slot *slot)
12284{
12285 kvm_mmu_slot_set_dirty(kvm, slot);
12286}
12287
12288static void vmx_flush_log_dirty(struct kvm *kvm)
12289{
12290 kvm_flush_pml_buffers(kvm);
12291}
12292
Bandan Dasc5f983f2017-05-05 15:25:14 -040012293static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12294{
12295 struct vmcs12 *vmcs12;
12296 struct vcpu_vmx *vmx = to_vmx(vcpu);
12297 gpa_t gpa;
12298 struct page *page = NULL;
12299 u64 *pml_address;
12300
12301 if (is_guest_mode(vcpu)) {
12302 WARN_ON_ONCE(vmx->nested.pml_full);
12303
12304 /*
12305 * Check if PML is enabled for the nested guest.
12306 * Whether eptp bit 6 is set is already checked
12307 * as part of A/D emulation.
12308 */
12309 vmcs12 = get_vmcs12(vcpu);
12310 if (!nested_cpu_has_pml(vmcs12))
12311 return 0;
12312
Dan Carpenter47698862017-05-10 22:43:17 +030012313 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012314 vmx->nested.pml_full = true;
12315 return 1;
12316 }
12317
12318 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12319
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012320 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12321 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012322 return 0;
12323
12324 pml_address = kmap(page);
12325 pml_address[vmcs12->guest_pml_index--] = gpa;
12326 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012327 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012328 }
12329
12330 return 0;
12331}
12332
Kai Huang843e4332015-01-28 10:54:28 +080012333static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12334 struct kvm_memory_slot *memslot,
12335 gfn_t offset, unsigned long mask)
12336{
12337 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12338}
12339
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012340static void __pi_post_block(struct kvm_vcpu *vcpu)
12341{
12342 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12343 struct pi_desc old, new;
12344 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012345
12346 do {
12347 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012348 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12349 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012350
12351 dest = cpu_physical_id(vcpu->cpu);
12352
12353 if (x2apic_enabled())
12354 new.ndst = dest;
12355 else
12356 new.ndst = (dest << 8) & 0xFF00;
12357
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012358 /* set 'NV' to 'notification vector' */
12359 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012360 } while (cmpxchg64(&pi_desc->control, old.control,
12361 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012362
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012363 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12364 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012365 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012366 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012367 vcpu->pre_pcpu = -1;
12368 }
12369}
12370
Feng Wuefc64402015-09-18 22:29:51 +080012371/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012372 * This routine does the following things for vCPU which is going
12373 * to be blocked if VT-d PI is enabled.
12374 * - Store the vCPU to the wakeup list, so when interrupts happen
12375 * we can find the right vCPU to wake up.
12376 * - Change the Posted-interrupt descriptor as below:
12377 * 'NDST' <-- vcpu->pre_pcpu
12378 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12379 * - If 'ON' is set during this process, which means at least one
12380 * interrupt is posted for this vCPU, we cannot block it, in
12381 * this case, return 1, otherwise, return 0.
12382 *
12383 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012384static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012385{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012386 unsigned int dest;
12387 struct pi_desc old, new;
12388 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12389
12390 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012391 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12392 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012393 return 0;
12394
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012395 WARN_ON(irqs_disabled());
12396 local_irq_disable();
12397 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12398 vcpu->pre_pcpu = vcpu->cpu;
12399 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12400 list_add_tail(&vcpu->blocked_vcpu_list,
12401 &per_cpu(blocked_vcpu_on_cpu,
12402 vcpu->pre_pcpu));
12403 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12404 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012405
12406 do {
12407 old.control = new.control = pi_desc->control;
12408
Feng Wubf9f6ac2015-09-18 22:29:55 +080012409 WARN((pi_desc->sn == 1),
12410 "Warning: SN field of posted-interrupts "
12411 "is set before blocking\n");
12412
12413 /*
12414 * Since vCPU can be preempted during this process,
12415 * vcpu->cpu could be different with pre_pcpu, we
12416 * need to set pre_pcpu as the destination of wakeup
12417 * notification event, then we can find the right vCPU
12418 * to wakeup in wakeup handler if interrupts happen
12419 * when the vCPU is in blocked state.
12420 */
12421 dest = cpu_physical_id(vcpu->pre_pcpu);
12422
12423 if (x2apic_enabled())
12424 new.ndst = dest;
12425 else
12426 new.ndst = (dest << 8) & 0xFF00;
12427
12428 /* set 'NV' to 'wakeup vector' */
12429 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012430 } while (cmpxchg64(&pi_desc->control, old.control,
12431 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012432
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012433 /* We should not block the vCPU if an interrupt is posted for it. */
12434 if (pi_test_on(pi_desc) == 1)
12435 __pi_post_block(vcpu);
12436
12437 local_irq_enable();
12438 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012439}
12440
Yunhong Jiangbc225122016-06-13 14:19:58 -070012441static int vmx_pre_block(struct kvm_vcpu *vcpu)
12442{
12443 if (pi_pre_block(vcpu))
12444 return 1;
12445
Yunhong Jiang64672c92016-06-13 14:19:59 -070012446 if (kvm_lapic_hv_timer_in_use(vcpu))
12447 kvm_lapic_switch_to_sw_timer(vcpu);
12448
Yunhong Jiangbc225122016-06-13 14:19:58 -070012449 return 0;
12450}
12451
12452static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012453{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012454 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012455 return;
12456
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012457 WARN_ON(irqs_disabled());
12458 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012459 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012460 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012461}
12462
Yunhong Jiangbc225122016-06-13 14:19:58 -070012463static void vmx_post_block(struct kvm_vcpu *vcpu)
12464{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012465 if (kvm_x86_ops->set_hv_timer)
12466 kvm_lapic_switch_to_hv_timer(vcpu);
12467
Yunhong Jiangbc225122016-06-13 14:19:58 -070012468 pi_post_block(vcpu);
12469}
12470
Feng Wubf9f6ac2015-09-18 22:29:55 +080012471/*
Feng Wuefc64402015-09-18 22:29:51 +080012472 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12473 *
12474 * @kvm: kvm
12475 * @host_irq: host irq of the interrupt
12476 * @guest_irq: gsi of the interrupt
12477 * @set: set or unset PI
12478 * returns 0 on success, < 0 on failure
12479 */
12480static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12481 uint32_t guest_irq, bool set)
12482{
12483 struct kvm_kernel_irq_routing_entry *e;
12484 struct kvm_irq_routing_table *irq_rt;
12485 struct kvm_lapic_irq irq;
12486 struct kvm_vcpu *vcpu;
12487 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012488 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012489
12490 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012491 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12492 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012493 return 0;
12494
12495 idx = srcu_read_lock(&kvm->irq_srcu);
12496 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012497 if (guest_irq >= irq_rt->nr_rt_entries ||
12498 hlist_empty(&irq_rt->map[guest_irq])) {
12499 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12500 guest_irq, irq_rt->nr_rt_entries);
12501 goto out;
12502 }
Feng Wuefc64402015-09-18 22:29:51 +080012503
12504 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12505 if (e->type != KVM_IRQ_ROUTING_MSI)
12506 continue;
12507 /*
12508 * VT-d PI cannot support posting multicast/broadcast
12509 * interrupts to a vCPU, we still use interrupt remapping
12510 * for these kind of interrupts.
12511 *
12512 * For lowest-priority interrupts, we only support
12513 * those with single CPU as the destination, e.g. user
12514 * configures the interrupts via /proc/irq or uses
12515 * irqbalance to make the interrupts single-CPU.
12516 *
12517 * We will support full lowest-priority interrupt later.
12518 */
12519
Radim Krčmář371313132016-07-12 22:09:27 +020012520 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012521 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12522 /*
12523 * Make sure the IRTE is in remapped mode if
12524 * we don't handle it in posted mode.
12525 */
12526 ret = irq_set_vcpu_affinity(host_irq, NULL);
12527 if (ret < 0) {
12528 printk(KERN_INFO
12529 "failed to back to remapped mode, irq: %u\n",
12530 host_irq);
12531 goto out;
12532 }
12533
Feng Wuefc64402015-09-18 22:29:51 +080012534 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012535 }
Feng Wuefc64402015-09-18 22:29:51 +080012536
12537 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12538 vcpu_info.vector = irq.vector;
12539
Feng Wub6ce9782016-01-25 16:53:35 +080012540 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012541 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12542
12543 if (set)
12544 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012545 else
Feng Wuefc64402015-09-18 22:29:51 +080012546 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012547
12548 if (ret < 0) {
12549 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12550 __func__);
12551 goto out;
12552 }
12553 }
12554
12555 ret = 0;
12556out:
12557 srcu_read_unlock(&kvm->irq_srcu, idx);
12558 return ret;
12559}
12560
Ashok Rajc45dcc72016-06-22 14:59:56 +080012561static void vmx_setup_mce(struct kvm_vcpu *vcpu)
12562{
12563 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
12564 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
12565 FEATURE_CONTROL_LMCE;
12566 else
12567 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
12568 ~FEATURE_CONTROL_LMCE;
12569}
12570
Ladi Prosek72d7b372017-10-11 16:54:41 +020012571static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
12572{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012573 /* we need a nested vmexit to enter SMM, postpone if run is pending */
12574 if (to_vmx(vcpu)->nested.nested_run_pending)
12575 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020012576 return 1;
12577}
12578
Ladi Prosek0234bf82017-10-11 16:54:40 +020012579static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
12580{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012581 struct vcpu_vmx *vmx = to_vmx(vcpu);
12582
12583 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
12584 if (vmx->nested.smm.guest_mode)
12585 nested_vmx_vmexit(vcpu, -1, 0, 0);
12586
12587 vmx->nested.smm.vmxon = vmx->nested.vmxon;
12588 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070012589 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020012590 return 0;
12591}
12592
12593static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
12594{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012595 struct vcpu_vmx *vmx = to_vmx(vcpu);
12596 int ret;
12597
12598 if (vmx->nested.smm.vmxon) {
12599 vmx->nested.vmxon = true;
12600 vmx->nested.smm.vmxon = false;
12601 }
12602
12603 if (vmx->nested.smm.guest_mode) {
12604 vcpu->arch.hflags &= ~HF_SMM_MASK;
12605 ret = enter_vmx_non_root_mode(vcpu, false);
12606 vcpu->arch.hflags |= HF_SMM_MASK;
12607 if (ret)
12608 return ret;
12609
12610 vmx->nested.smm.guest_mode = false;
12611 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020012612 return 0;
12613}
12614
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012615static int enable_smi_window(struct kvm_vcpu *vcpu)
12616{
12617 return 0;
12618}
12619
Kees Cook404f6aa2016-08-08 16:29:06 -070012620static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080012621 .cpu_has_kvm_support = cpu_has_kvm_support,
12622 .disabled_by_bios = vmx_disabled_by_bios,
12623 .hardware_setup = hardware_setup,
12624 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030012625 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012626 .hardware_enable = hardware_enable,
12627 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080012628 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020012629 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012630
Wanpeng Lib31c1142018-03-12 04:53:04 -070012631 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070012632 .vm_alloc = vmx_vm_alloc,
12633 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070012634
Avi Kivity6aa8b732006-12-10 02:21:36 -080012635 .vcpu_create = vmx_create_vcpu,
12636 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030012637 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012638
Avi Kivity04d2cc72007-09-10 18:10:54 +030012639 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012640 .vcpu_load = vmx_vcpu_load,
12641 .vcpu_put = vmx_vcpu_put,
12642
Paolo Bonzinia96036b2015-11-10 11:55:36 +010012643 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060012644 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012645 .get_msr = vmx_get_msr,
12646 .set_msr = vmx_set_msr,
12647 .get_segment_base = vmx_get_segment_base,
12648 .get_segment = vmx_get_segment,
12649 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020012650 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012651 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020012652 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020012653 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030012654 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012655 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012656 .set_cr3 = vmx_set_cr3,
12657 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012658 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012659 .get_idt = vmx_get_idt,
12660 .set_idt = vmx_set_idt,
12661 .get_gdt = vmx_get_gdt,
12662 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010012663 .get_dr6 = vmx_get_dr6,
12664 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030012665 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010012666 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030012667 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012668 .get_rflags = vmx_get_rflags,
12669 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080012670
Avi Kivity6aa8b732006-12-10 02:21:36 -080012671 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012672
Avi Kivity6aa8b732006-12-10 02:21:36 -080012673 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020012674 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012675 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040012676 .set_interrupt_shadow = vmx_set_interrupt_shadow,
12677 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020012678 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030012679 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012680 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020012681 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030012682 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020012683 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012684 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010012685 .get_nmi_mask = vmx_get_nmi_mask,
12686 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012687 .enable_nmi_window = enable_nmi_window,
12688 .enable_irq_window = enable_irq_window,
12689 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080012690 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080012691 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030012692 .get_enable_apicv = vmx_get_enable_apicv,
12693 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012694 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010012695 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080012696 .hwapic_irr_update = vmx_hwapic_irr_update,
12697 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080012698 .sync_pir_to_irr = vmx_sync_pir_to_irr,
12699 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030012700
Izik Eiduscbc94022007-10-25 00:29:55 +020012701 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070012702 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080012703 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080012704 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030012705
Avi Kivity586f9602010-11-18 13:09:54 +020012706 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020012707
Sheng Yang17cc3932010-01-05 19:02:27 +080012708 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080012709
12710 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080012711
12712 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000012713 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020012714
12715 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080012716
12717 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100012718
12719 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020012720
12721 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012722
12723 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080012724 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000012725 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080012726 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020012727 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012728
12729 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012730
12731 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080012732
12733 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
12734 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
12735 .flush_log_dirty = vmx_flush_log_dirty,
12736 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040012737 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020012738
Feng Wubf9f6ac2015-09-18 22:29:55 +080012739 .pre_block = vmx_pre_block,
12740 .post_block = vmx_post_block,
12741
Wei Huang25462f72015-06-19 15:45:05 +020012742 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080012743
12744 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070012745
12746#ifdef CONFIG_X86_64
12747 .set_hv_timer = vmx_set_hv_timer,
12748 .cancel_hv_timer = vmx_cancel_hv_timer,
12749#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080012750
12751 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012752
Ladi Prosek72d7b372017-10-11 16:54:41 +020012753 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020012754 .pre_enter_smm = vmx_pre_enter_smm,
12755 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020012756 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080012757};
12758
12759static int __init vmx_init(void)
12760{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012761 int r;
12762
12763#if IS_ENABLED(CONFIG_HYPERV)
12764 /*
12765 * Enlightened VMCS usage should be recommended and the host needs
12766 * to support eVMCS v1 or above. We can also disable eVMCS support
12767 * with module parameter.
12768 */
12769 if (enlightened_vmcs &&
12770 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
12771 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
12772 KVM_EVMCS_VERSION) {
12773 int cpu;
12774
12775 /* Check that we have assist pages on all online CPUs */
12776 for_each_online_cpu(cpu) {
12777 if (!hv_get_vp_assist_page(cpu)) {
12778 enlightened_vmcs = false;
12779 break;
12780 }
12781 }
12782
12783 if (enlightened_vmcs) {
12784 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
12785 static_branch_enable(&enable_evmcs);
12786 }
12787 } else {
12788 enlightened_vmcs = false;
12789 }
12790#endif
12791
12792 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012793 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030012794 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080012795 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080012796
Dave Young2965faa2015-09-09 15:38:55 -070012797#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012798 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
12799 crash_vmclear_local_loaded_vmcss);
12800#endif
12801
He, Qingfdef3ad2007-04-30 09:45:24 +030012802 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080012803}
12804
12805static void __exit vmx_exit(void)
12806{
Dave Young2965faa2015-09-09 15:38:55 -070012807#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053012808 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080012809 synchronize_rcu();
12810#endif
12811
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080012812 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010012813
12814#if IS_ENABLED(CONFIG_HYPERV)
12815 if (static_branch_unlikely(&enable_evmcs)) {
12816 int cpu;
12817 struct hv_vp_assist_page *vp_ap;
12818 /*
12819 * Reset everything to support using non-enlightened VMCS
12820 * access later (e.g. when we reload the module with
12821 * enlightened_vmcs=0)
12822 */
12823 for_each_online_cpu(cpu) {
12824 vp_ap = hv_get_vp_assist_page(cpu);
12825
12826 if (!vp_ap)
12827 continue;
12828
12829 vp_ap->current_nested_vmcs = 0;
12830 vp_ap->enlighten_vmentry = 0;
12831 }
12832
12833 static_branch_disable(&enable_evmcs);
12834 }
12835#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080012836}
12837
12838module_init(vmx_init)
12839module_exit(vmx_exit)