blob: e553e43a114cde219c0f90a30cbe2364e8b205cb [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>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040074static bool __read_mostly nosmt;
75module_param(nosmt, bool, S_IRUGO);
76
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020078module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080079
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010080static bool __read_mostly enable_vnmi = 1;
81module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
82
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020087module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080088
Rusty Russell476bc002012-01-13 09:32:18 +103089static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070090module_param_named(unrestricted_guest,
91 enable_unrestricted_guest, bool, S_IRUGO);
92
Xudong Hao83c3a332012-05-28 19:33:35 +080093static bool __read_mostly enable_ept_ad_bits = 1;
94module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
95
Avi Kivitya27685c2012-06-12 20:30:18 +030096static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020097module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030098
Rusty Russell476bc002012-01-13 09:32:18 +103099static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +0300100module_param(fasteoi, bool, S_IRUGO);
101
Yang Zhang5a717852013-04-11 19:25:16 +0800102static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800103module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800104
Abel Gordonabc4fc52013-04-18 14:35:25 +0300105static bool __read_mostly enable_shadow_vmcs = 1;
106module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300107/*
108 * If nested=1, nested virtualization is supported, i.e., guests may use
109 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
110 * use VMX instructions.
111 */
Rusty Russell476bc002012-01-13 09:32:18 +1030112static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300113module_param(nested, bool, S_IRUGO);
114
Wanpeng Li20300092014-12-02 19:14:59 +0800115static u64 __read_mostly host_xss;
116
Kai Huang843e4332015-01-28 10:54:28 +0800117static bool __read_mostly enable_pml = 1;
118module_param_named(pml, enable_pml, bool, S_IRUGO);
119
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100120#define MSR_TYPE_R 1
121#define MSR_TYPE_W 2
122#define MSR_TYPE_RW 3
123
124#define MSR_BITMAP_MODE_X2APIC 1
125#define MSR_BITMAP_MODE_X2APIC_APICV 2
126#define MSR_BITMAP_MODE_LM 4
127
Haozhong Zhang64903d62015-10-20 15:39:09 +0800128#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
129
Yunhong Jiang64672c92016-06-13 14:19:59 -0700130/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
131static int __read_mostly cpu_preemption_timer_multi;
132static bool __read_mostly enable_preemption_timer = 1;
133#ifdef CONFIG_X86_64
134module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
135#endif
136
Gleb Natapov50378782013-02-04 16:00:28 +0200137#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800138#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
139#define KVM_VM_CR0_ALWAYS_ON \
140 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
141 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200142#define KVM_CR4_GUEST_OWNED_BITS \
143 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800144 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200145
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800146#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200147#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
148#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
149
Avi Kivity78ac8b42010-04-08 18:19:35 +0300150#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
151
Jan Kiszkaf41245002014-03-07 20:03:13 +0100152#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800154/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300155 * Hyper-V requires all of these, so mark them as supported even though
156 * they are just treated the same as all-context.
157 */
158#define VMX_VPID_EXTENT_SUPPORTED_MASK \
159 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
160 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
161 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164/*
165 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
166 * ple_gap: upper bound on the amount of time between two successive
167 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500168 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169 * ple_window: upper bound on the amount of time a guest is allowed to execute
170 * in a PAUSE loop. Tests indicate that most spinlocks are held for
171 * less than 2^12 cycles
172 * Time is measured based on a counter that runs at the same rate as the TSC,
173 * refer SDM volume 3b section 21.6.13 & 22.1.3.
174 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400175static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176
Babu Moger7fbc85a2018-03-16 16:37:22 -0400177static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
178module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800179
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400181static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400182module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200183
184/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400185static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200187
188/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400189static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
190module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200191
Avi Kivity83287ea422012-09-16 15:10:57 +0300192extern const ulong vmx_return;
193
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
195
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200196static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush = VMENTER_L1D_FLUSH_COND;
197
198static const struct {
199 const char *option;
200 enum vmx_l1d_flush_state cmd;
201} vmentry_l1d_param[] = {
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +0200202 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200203 {"never", VMENTER_L1D_FLUSH_NEVER},
204 {"cond", VMENTER_L1D_FLUSH_COND},
205 {"always", VMENTER_L1D_FLUSH_ALWAYS},
206};
207
208static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
209{
210 unsigned int i;
211
212 if (!s)
213 return -EINVAL;
214
215 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
216 if (!strcmp(s, vmentry_l1d_param[i].option)) {
217 vmentry_l1d_flush = vmentry_l1d_param[i].cmd;
218 return 0;
219 }
220 }
221
222 return -EINVAL;
223}
224
225static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
226{
227 return sprintf(s, "%s\n", vmentry_l1d_param[vmentry_l1d_flush].option);
228}
229
230static const struct kernel_param_ops vmentry_l1d_flush_ops = {
231 .set = vmentry_l1d_flush_set,
232 .get = vmentry_l1d_flush_get,
233};
234module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, &vmentry_l1d_flush, S_IRUGO);
235
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700236struct kvm_vmx {
237 struct kvm kvm;
238
239 unsigned int tss_addr;
240 bool ept_identity_pagetable_done;
241 gpa_t ept_identity_map_addr;
242};
243
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200244#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300245
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400246struct vmcs {
247 u32 revision_id;
248 u32 abort;
249 char data[0];
250};
251
Nadav Har'Eld462b812011-05-24 15:26:10 +0300252/*
253 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
254 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
255 * loaded on this CPU (so we can clear them if the CPU goes down).
256 */
257struct loaded_vmcs {
258 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700259 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300260 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200261 bool launched;
262 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200263 unsigned long vmcs_host_cr3; /* May not match real cr3 */
264 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100265 /* Support for vnmi-less CPUs */
266 int soft_vnmi_blocked;
267 ktime_t entry_time;
268 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100269 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300270 struct list_head loaded_vmcss_on_cpu_link;
271};
272
Avi Kivity26bb0982009-09-07 11:14:12 +0300273struct shared_msr_entry {
274 unsigned index;
275 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200276 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300277};
278
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300279/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300280 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
281 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
282 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
283 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
284 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
285 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600286 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300287 * underlying hardware which will be used to run L2.
288 * This structure is packed to ensure that its layout is identical across
289 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700290 *
291 * IMPORTANT: Changing the layout of existing fields in this structure
292 * will break save/restore compatibility with older kvm releases. When
293 * adding new fields, either use space in the reserved padding* arrays
294 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300295 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300296typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300297struct __packed vmcs12 {
298 /* According to the Intel spec, a VMCS region must start with the
299 * following two fields. Then follow implementation-specific data.
300 */
301 u32 revision_id;
302 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300303
Nadav Har'El27d6c862011-05-25 23:06:59 +0300304 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
305 u32 padding[7]; /* room for future expansion */
306
Nadav Har'El22bd0352011-05-25 23:05:57 +0300307 u64 io_bitmap_a;
308 u64 io_bitmap_b;
309 u64 msr_bitmap;
310 u64 vm_exit_msr_store_addr;
311 u64 vm_exit_msr_load_addr;
312 u64 vm_entry_msr_load_addr;
313 u64 tsc_offset;
314 u64 virtual_apic_page_addr;
315 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800316 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300317 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800318 u64 eoi_exit_bitmap0;
319 u64 eoi_exit_bitmap1;
320 u64 eoi_exit_bitmap2;
321 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800322 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300323 u64 guest_physical_address;
324 u64 vmcs_link_pointer;
325 u64 guest_ia32_debugctl;
326 u64 guest_ia32_pat;
327 u64 guest_ia32_efer;
328 u64 guest_ia32_perf_global_ctrl;
329 u64 guest_pdptr0;
330 u64 guest_pdptr1;
331 u64 guest_pdptr2;
332 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100333 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300334 u64 host_ia32_pat;
335 u64 host_ia32_efer;
336 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700337 u64 vmread_bitmap;
338 u64 vmwrite_bitmap;
339 u64 vm_function_control;
340 u64 eptp_list_address;
341 u64 pml_address;
342 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300343 /*
344 * To allow migration of L1 (complete with its L2 guests) between
345 * machines of different natural widths (32 or 64 bit), we cannot have
346 * unsigned long fields with no explict size. We use u64 (aliased
347 * natural_width) instead. Luckily, x86 is little-endian.
348 */
349 natural_width cr0_guest_host_mask;
350 natural_width cr4_guest_host_mask;
351 natural_width cr0_read_shadow;
352 natural_width cr4_read_shadow;
353 natural_width cr3_target_value0;
354 natural_width cr3_target_value1;
355 natural_width cr3_target_value2;
356 natural_width cr3_target_value3;
357 natural_width exit_qualification;
358 natural_width guest_linear_address;
359 natural_width guest_cr0;
360 natural_width guest_cr3;
361 natural_width guest_cr4;
362 natural_width guest_es_base;
363 natural_width guest_cs_base;
364 natural_width guest_ss_base;
365 natural_width guest_ds_base;
366 natural_width guest_fs_base;
367 natural_width guest_gs_base;
368 natural_width guest_ldtr_base;
369 natural_width guest_tr_base;
370 natural_width guest_gdtr_base;
371 natural_width guest_idtr_base;
372 natural_width guest_dr7;
373 natural_width guest_rsp;
374 natural_width guest_rip;
375 natural_width guest_rflags;
376 natural_width guest_pending_dbg_exceptions;
377 natural_width guest_sysenter_esp;
378 natural_width guest_sysenter_eip;
379 natural_width host_cr0;
380 natural_width host_cr3;
381 natural_width host_cr4;
382 natural_width host_fs_base;
383 natural_width host_gs_base;
384 natural_width host_tr_base;
385 natural_width host_gdtr_base;
386 natural_width host_idtr_base;
387 natural_width host_ia32_sysenter_esp;
388 natural_width host_ia32_sysenter_eip;
389 natural_width host_rsp;
390 natural_width host_rip;
391 natural_width paddingl[8]; /* room for future expansion */
392 u32 pin_based_vm_exec_control;
393 u32 cpu_based_vm_exec_control;
394 u32 exception_bitmap;
395 u32 page_fault_error_code_mask;
396 u32 page_fault_error_code_match;
397 u32 cr3_target_count;
398 u32 vm_exit_controls;
399 u32 vm_exit_msr_store_count;
400 u32 vm_exit_msr_load_count;
401 u32 vm_entry_controls;
402 u32 vm_entry_msr_load_count;
403 u32 vm_entry_intr_info_field;
404 u32 vm_entry_exception_error_code;
405 u32 vm_entry_instruction_len;
406 u32 tpr_threshold;
407 u32 secondary_vm_exec_control;
408 u32 vm_instruction_error;
409 u32 vm_exit_reason;
410 u32 vm_exit_intr_info;
411 u32 vm_exit_intr_error_code;
412 u32 idt_vectoring_info_field;
413 u32 idt_vectoring_error_code;
414 u32 vm_exit_instruction_len;
415 u32 vmx_instruction_info;
416 u32 guest_es_limit;
417 u32 guest_cs_limit;
418 u32 guest_ss_limit;
419 u32 guest_ds_limit;
420 u32 guest_fs_limit;
421 u32 guest_gs_limit;
422 u32 guest_ldtr_limit;
423 u32 guest_tr_limit;
424 u32 guest_gdtr_limit;
425 u32 guest_idtr_limit;
426 u32 guest_es_ar_bytes;
427 u32 guest_cs_ar_bytes;
428 u32 guest_ss_ar_bytes;
429 u32 guest_ds_ar_bytes;
430 u32 guest_fs_ar_bytes;
431 u32 guest_gs_ar_bytes;
432 u32 guest_ldtr_ar_bytes;
433 u32 guest_tr_ar_bytes;
434 u32 guest_interruptibility_info;
435 u32 guest_activity_state;
436 u32 guest_sysenter_cs;
437 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100438 u32 vmx_preemption_timer_value;
439 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300440 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800441 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300442 u16 guest_es_selector;
443 u16 guest_cs_selector;
444 u16 guest_ss_selector;
445 u16 guest_ds_selector;
446 u16 guest_fs_selector;
447 u16 guest_gs_selector;
448 u16 guest_ldtr_selector;
449 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800450 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300451 u16 host_es_selector;
452 u16 host_cs_selector;
453 u16 host_ss_selector;
454 u16 host_ds_selector;
455 u16 host_fs_selector;
456 u16 host_gs_selector;
457 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700458 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300459};
460
461/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700462 * For save/restore compatibility, the vmcs12 field offsets must not change.
463 */
464#define CHECK_OFFSET(field, loc) \
465 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
466 "Offset of " #field " in struct vmcs12 has changed.")
467
468static inline void vmx_check_vmcs12_offsets(void) {
469 CHECK_OFFSET(revision_id, 0);
470 CHECK_OFFSET(abort, 4);
471 CHECK_OFFSET(launch_state, 8);
472 CHECK_OFFSET(io_bitmap_a, 40);
473 CHECK_OFFSET(io_bitmap_b, 48);
474 CHECK_OFFSET(msr_bitmap, 56);
475 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
476 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
477 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
478 CHECK_OFFSET(tsc_offset, 88);
479 CHECK_OFFSET(virtual_apic_page_addr, 96);
480 CHECK_OFFSET(apic_access_addr, 104);
481 CHECK_OFFSET(posted_intr_desc_addr, 112);
482 CHECK_OFFSET(ept_pointer, 120);
483 CHECK_OFFSET(eoi_exit_bitmap0, 128);
484 CHECK_OFFSET(eoi_exit_bitmap1, 136);
485 CHECK_OFFSET(eoi_exit_bitmap2, 144);
486 CHECK_OFFSET(eoi_exit_bitmap3, 152);
487 CHECK_OFFSET(xss_exit_bitmap, 160);
488 CHECK_OFFSET(guest_physical_address, 168);
489 CHECK_OFFSET(vmcs_link_pointer, 176);
490 CHECK_OFFSET(guest_ia32_debugctl, 184);
491 CHECK_OFFSET(guest_ia32_pat, 192);
492 CHECK_OFFSET(guest_ia32_efer, 200);
493 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
494 CHECK_OFFSET(guest_pdptr0, 216);
495 CHECK_OFFSET(guest_pdptr1, 224);
496 CHECK_OFFSET(guest_pdptr2, 232);
497 CHECK_OFFSET(guest_pdptr3, 240);
498 CHECK_OFFSET(guest_bndcfgs, 248);
499 CHECK_OFFSET(host_ia32_pat, 256);
500 CHECK_OFFSET(host_ia32_efer, 264);
501 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
502 CHECK_OFFSET(vmread_bitmap, 280);
503 CHECK_OFFSET(vmwrite_bitmap, 288);
504 CHECK_OFFSET(vm_function_control, 296);
505 CHECK_OFFSET(eptp_list_address, 304);
506 CHECK_OFFSET(pml_address, 312);
507 CHECK_OFFSET(cr0_guest_host_mask, 344);
508 CHECK_OFFSET(cr4_guest_host_mask, 352);
509 CHECK_OFFSET(cr0_read_shadow, 360);
510 CHECK_OFFSET(cr4_read_shadow, 368);
511 CHECK_OFFSET(cr3_target_value0, 376);
512 CHECK_OFFSET(cr3_target_value1, 384);
513 CHECK_OFFSET(cr3_target_value2, 392);
514 CHECK_OFFSET(cr3_target_value3, 400);
515 CHECK_OFFSET(exit_qualification, 408);
516 CHECK_OFFSET(guest_linear_address, 416);
517 CHECK_OFFSET(guest_cr0, 424);
518 CHECK_OFFSET(guest_cr3, 432);
519 CHECK_OFFSET(guest_cr4, 440);
520 CHECK_OFFSET(guest_es_base, 448);
521 CHECK_OFFSET(guest_cs_base, 456);
522 CHECK_OFFSET(guest_ss_base, 464);
523 CHECK_OFFSET(guest_ds_base, 472);
524 CHECK_OFFSET(guest_fs_base, 480);
525 CHECK_OFFSET(guest_gs_base, 488);
526 CHECK_OFFSET(guest_ldtr_base, 496);
527 CHECK_OFFSET(guest_tr_base, 504);
528 CHECK_OFFSET(guest_gdtr_base, 512);
529 CHECK_OFFSET(guest_idtr_base, 520);
530 CHECK_OFFSET(guest_dr7, 528);
531 CHECK_OFFSET(guest_rsp, 536);
532 CHECK_OFFSET(guest_rip, 544);
533 CHECK_OFFSET(guest_rflags, 552);
534 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
535 CHECK_OFFSET(guest_sysenter_esp, 568);
536 CHECK_OFFSET(guest_sysenter_eip, 576);
537 CHECK_OFFSET(host_cr0, 584);
538 CHECK_OFFSET(host_cr3, 592);
539 CHECK_OFFSET(host_cr4, 600);
540 CHECK_OFFSET(host_fs_base, 608);
541 CHECK_OFFSET(host_gs_base, 616);
542 CHECK_OFFSET(host_tr_base, 624);
543 CHECK_OFFSET(host_gdtr_base, 632);
544 CHECK_OFFSET(host_idtr_base, 640);
545 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
546 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
547 CHECK_OFFSET(host_rsp, 664);
548 CHECK_OFFSET(host_rip, 672);
549 CHECK_OFFSET(pin_based_vm_exec_control, 744);
550 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
551 CHECK_OFFSET(exception_bitmap, 752);
552 CHECK_OFFSET(page_fault_error_code_mask, 756);
553 CHECK_OFFSET(page_fault_error_code_match, 760);
554 CHECK_OFFSET(cr3_target_count, 764);
555 CHECK_OFFSET(vm_exit_controls, 768);
556 CHECK_OFFSET(vm_exit_msr_store_count, 772);
557 CHECK_OFFSET(vm_exit_msr_load_count, 776);
558 CHECK_OFFSET(vm_entry_controls, 780);
559 CHECK_OFFSET(vm_entry_msr_load_count, 784);
560 CHECK_OFFSET(vm_entry_intr_info_field, 788);
561 CHECK_OFFSET(vm_entry_exception_error_code, 792);
562 CHECK_OFFSET(vm_entry_instruction_len, 796);
563 CHECK_OFFSET(tpr_threshold, 800);
564 CHECK_OFFSET(secondary_vm_exec_control, 804);
565 CHECK_OFFSET(vm_instruction_error, 808);
566 CHECK_OFFSET(vm_exit_reason, 812);
567 CHECK_OFFSET(vm_exit_intr_info, 816);
568 CHECK_OFFSET(vm_exit_intr_error_code, 820);
569 CHECK_OFFSET(idt_vectoring_info_field, 824);
570 CHECK_OFFSET(idt_vectoring_error_code, 828);
571 CHECK_OFFSET(vm_exit_instruction_len, 832);
572 CHECK_OFFSET(vmx_instruction_info, 836);
573 CHECK_OFFSET(guest_es_limit, 840);
574 CHECK_OFFSET(guest_cs_limit, 844);
575 CHECK_OFFSET(guest_ss_limit, 848);
576 CHECK_OFFSET(guest_ds_limit, 852);
577 CHECK_OFFSET(guest_fs_limit, 856);
578 CHECK_OFFSET(guest_gs_limit, 860);
579 CHECK_OFFSET(guest_ldtr_limit, 864);
580 CHECK_OFFSET(guest_tr_limit, 868);
581 CHECK_OFFSET(guest_gdtr_limit, 872);
582 CHECK_OFFSET(guest_idtr_limit, 876);
583 CHECK_OFFSET(guest_es_ar_bytes, 880);
584 CHECK_OFFSET(guest_cs_ar_bytes, 884);
585 CHECK_OFFSET(guest_ss_ar_bytes, 888);
586 CHECK_OFFSET(guest_ds_ar_bytes, 892);
587 CHECK_OFFSET(guest_fs_ar_bytes, 896);
588 CHECK_OFFSET(guest_gs_ar_bytes, 900);
589 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
590 CHECK_OFFSET(guest_tr_ar_bytes, 908);
591 CHECK_OFFSET(guest_interruptibility_info, 912);
592 CHECK_OFFSET(guest_activity_state, 916);
593 CHECK_OFFSET(guest_sysenter_cs, 920);
594 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
595 CHECK_OFFSET(vmx_preemption_timer_value, 928);
596 CHECK_OFFSET(virtual_processor_id, 960);
597 CHECK_OFFSET(posted_intr_nv, 962);
598 CHECK_OFFSET(guest_es_selector, 964);
599 CHECK_OFFSET(guest_cs_selector, 966);
600 CHECK_OFFSET(guest_ss_selector, 968);
601 CHECK_OFFSET(guest_ds_selector, 970);
602 CHECK_OFFSET(guest_fs_selector, 972);
603 CHECK_OFFSET(guest_gs_selector, 974);
604 CHECK_OFFSET(guest_ldtr_selector, 976);
605 CHECK_OFFSET(guest_tr_selector, 978);
606 CHECK_OFFSET(guest_intr_status, 980);
607 CHECK_OFFSET(host_es_selector, 982);
608 CHECK_OFFSET(host_cs_selector, 984);
609 CHECK_OFFSET(host_ss_selector, 986);
610 CHECK_OFFSET(host_ds_selector, 988);
611 CHECK_OFFSET(host_fs_selector, 990);
612 CHECK_OFFSET(host_gs_selector, 992);
613 CHECK_OFFSET(host_tr_selector, 994);
614 CHECK_OFFSET(guest_pml_index, 996);
615}
616
617/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300618 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
619 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
620 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700621 *
622 * IMPORTANT: Changing this value will break save/restore compatibility with
623 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300624 */
625#define VMCS12_REVISION 0x11e57ed0
626
627/*
628 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
629 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
630 * current implementation, 4K are reserved to avoid future complications.
631 */
632#define VMCS12_SIZE 0x1000
633
634/*
Jim Mattson5b157062017-12-22 12:11:12 -0800635 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
636 * supported VMCS12 field encoding.
637 */
638#define VMCS12_MAX_FIELD_INDEX 0x17
639
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100640struct nested_vmx_msrs {
641 /*
642 * We only store the "true" versions of the VMX capability MSRs. We
643 * generate the "non-true" versions by setting the must-be-1 bits
644 * according to the SDM.
645 */
646 u32 procbased_ctls_low;
647 u32 procbased_ctls_high;
648 u32 secondary_ctls_low;
649 u32 secondary_ctls_high;
650 u32 pinbased_ctls_low;
651 u32 pinbased_ctls_high;
652 u32 exit_ctls_low;
653 u32 exit_ctls_high;
654 u32 entry_ctls_low;
655 u32 entry_ctls_high;
656 u32 misc_low;
657 u32 misc_high;
658 u32 ept_caps;
659 u32 vpid_caps;
660 u64 basic;
661 u64 cr0_fixed0;
662 u64 cr0_fixed1;
663 u64 cr4_fixed0;
664 u64 cr4_fixed1;
665 u64 vmcs_enum;
666 u64 vmfunc_controls;
667};
668
Jim Mattson5b157062017-12-22 12:11:12 -0800669/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300670 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
671 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
672 */
673struct nested_vmx {
674 /* Has the level1 guest done vmxon? */
675 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400676 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400677 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300678
679 /* The guest-physical address of the current VMCS L1 keeps for L2 */
680 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700681 /*
682 * Cache of the guest's VMCS, existing outside of guest memory.
683 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700684 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700685 */
686 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300687 /*
688 * Indicates if the shadow vmcs must be updated with the
689 * data hold by vmcs12
690 */
691 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100692 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300693
Jim Mattson8d860bb2018-05-09 16:56:05 -0400694 bool change_vmcs01_virtual_apic_mode;
695
Nadav Har'El644d7112011-05-25 23:12:35 +0300696 /* L2 must run next, and mustn't decide to exit to L1. */
697 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600698
699 struct loaded_vmcs vmcs02;
700
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300701 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600702 * Guest pages referred to in the vmcs02 with host-physical
703 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300704 */
705 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800706 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800707 struct page *pi_desc_page;
708 struct pi_desc *pi_desc;
709 bool pi_pending;
710 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100711
712 struct hrtimer preemption_timer;
713 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200714
715 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
716 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800717
Wanpeng Li5c614b32015-10-13 09:18:36 -0700718 u16 vpid02;
719 u16 last_vpid;
720
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100721 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200722
723 /* SMM related state */
724 struct {
725 /* in VMX operation on SMM entry? */
726 bool vmxon;
727 /* in guest mode on SMM entry? */
728 bool guest_mode;
729 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300730};
731
Yang Zhang01e439b2013-04-11 19:25:12 +0800732#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800733#define POSTED_INTR_SN 1
734
Yang Zhang01e439b2013-04-11 19:25:12 +0800735/* Posted-Interrupt Descriptor */
736struct pi_desc {
737 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800738 union {
739 struct {
740 /* bit 256 - Outstanding Notification */
741 u16 on : 1,
742 /* bit 257 - Suppress Notification */
743 sn : 1,
744 /* bit 271:258 - Reserved */
745 rsvd_1 : 14;
746 /* bit 279:272 - Notification Vector */
747 u8 nv;
748 /* bit 287:280 - Reserved */
749 u8 rsvd_2;
750 /* bit 319:288 - Notification Destination */
751 u32 ndst;
752 };
753 u64 control;
754 };
755 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800756} __aligned(64);
757
Yang Zhanga20ed542013-04-11 19:25:15 +0800758static bool pi_test_and_set_on(struct pi_desc *pi_desc)
759{
760 return test_and_set_bit(POSTED_INTR_ON,
761 (unsigned long *)&pi_desc->control);
762}
763
764static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
765{
766 return test_and_clear_bit(POSTED_INTR_ON,
767 (unsigned long *)&pi_desc->control);
768}
769
770static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
771{
772 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
773}
774
Feng Wuebbfc762015-09-18 22:29:46 +0800775static inline void pi_clear_sn(struct pi_desc *pi_desc)
776{
777 return clear_bit(POSTED_INTR_SN,
778 (unsigned long *)&pi_desc->control);
779}
780
781static inline void pi_set_sn(struct pi_desc *pi_desc)
782{
783 return set_bit(POSTED_INTR_SN,
784 (unsigned long *)&pi_desc->control);
785}
786
Paolo Bonziniad361092016-09-20 16:15:05 +0200787static inline void pi_clear_on(struct pi_desc *pi_desc)
788{
789 clear_bit(POSTED_INTR_ON,
790 (unsigned long *)&pi_desc->control);
791}
792
Feng Wuebbfc762015-09-18 22:29:46 +0800793static inline int pi_test_on(struct pi_desc *pi_desc)
794{
795 return test_bit(POSTED_INTR_ON,
796 (unsigned long *)&pi_desc->control);
797}
798
799static inline int pi_test_sn(struct pi_desc *pi_desc)
800{
801 return test_bit(POSTED_INTR_SN,
802 (unsigned long *)&pi_desc->control);
803}
804
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400805struct vmx_msrs {
806 unsigned int nr;
807 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
808};
809
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400810struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000811 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300812 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300813 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100814 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300815 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200816 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200817 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300818 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400819 int nmsrs;
820 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800821 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400822#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300823 u64 msr_host_kernel_gs_base;
824 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400825#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100826
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100827 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100828 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100829
Gleb Natapov2961e8762013-11-25 15:37:13 +0200830 u32 vm_entry_controls_shadow;
831 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200832 u32 secondary_exec_control;
833
Nadav Har'Eld462b812011-05-24 15:26:10 +0300834 /*
835 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
836 * non-nested (L1) guest, it always points to vmcs01. For a nested
837 * guest (L2), it points to a different VMCS.
838 */
839 struct loaded_vmcs vmcs01;
840 struct loaded_vmcs *loaded_vmcs;
841 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300842 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400843 struct vmx_msrs guest;
844 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300845 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400846 struct {
847 int loaded;
848 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300849#ifdef CONFIG_X86_64
850 u16 ds_sel, es_sel;
851#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200852 int gs_ldt_reload_needed;
853 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000854 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400855 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200856 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300857 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300858 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300859 struct kvm_segment segs[8];
860 } rmode;
861 struct {
862 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300863 struct kvm_save_segment {
864 u16 selector;
865 unsigned long base;
866 u32 limit;
867 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300868 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300869 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800870 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300871 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200872
Andi Kleena0861c02009-06-08 17:37:09 +0800873 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800874
Yang Zhang01e439b2013-04-11 19:25:12 +0800875 /* Posted interrupt descriptor */
876 struct pi_desc pi_desc;
877
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300878 /* Support for a guest hypervisor (nested VMX) */
879 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200880
881 /* Dynamic PLE window. */
882 int ple_window;
883 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800884
885 /* Support for PML */
886#define PML_ENTITY_NUM 512
887 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800888
Yunhong Jiang64672c92016-06-13 14:19:59 -0700889 /* apic deadline value in host tsc */
890 u64 hv_deadline_tsc;
891
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800892 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800893
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800894 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800895
Wanpeng Li74c55932017-11-29 01:31:20 -0800896 unsigned long host_debugctlmsr;
897
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800898 /*
899 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
900 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
901 * in msr_ia32_feature_control_valid_bits.
902 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800903 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800904 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400905};
906
Avi Kivity2fb92db2011-04-27 19:42:18 +0300907enum segment_cache_field {
908 SEG_FIELD_SEL = 0,
909 SEG_FIELD_BASE = 1,
910 SEG_FIELD_LIMIT = 2,
911 SEG_FIELD_AR = 3,
912
913 SEG_FIELD_NR = 4
914};
915
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700916static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
917{
918 return container_of(kvm, struct kvm_vmx, kvm);
919}
920
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400921static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
922{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000923 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400924}
925
Feng Wuefc64402015-09-18 22:29:51 +0800926static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
927{
928 return &(to_vmx(vcpu)->pi_desc);
929}
930
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800931#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300932#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800933#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
934#define FIELD64(number, name) \
935 FIELD(number, name), \
936 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300937
Abel Gordon4607c2d2013-04-18 14:35:55 +0300938
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100939static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100940#define SHADOW_FIELD_RO(x) x,
941#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300942};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400943static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300944 ARRAY_SIZE(shadow_read_only_fields);
945
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100946static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100947#define SHADOW_FIELD_RW(x) x,
948#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300949};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400950static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300951 ARRAY_SIZE(shadow_read_write_fields);
952
Mathias Krause772e0312012-08-30 01:30:19 +0200953static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300954 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800955 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300956 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
957 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
958 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
959 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
960 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
961 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
962 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
963 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800964 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400965 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300966 FIELD(HOST_ES_SELECTOR, host_es_selector),
967 FIELD(HOST_CS_SELECTOR, host_cs_selector),
968 FIELD(HOST_SS_SELECTOR, host_ss_selector),
969 FIELD(HOST_DS_SELECTOR, host_ds_selector),
970 FIELD(HOST_FS_SELECTOR, host_fs_selector),
971 FIELD(HOST_GS_SELECTOR, host_gs_selector),
972 FIELD(HOST_TR_SELECTOR, host_tr_selector),
973 FIELD64(IO_BITMAP_A, io_bitmap_a),
974 FIELD64(IO_BITMAP_B, io_bitmap_b),
975 FIELD64(MSR_BITMAP, msr_bitmap),
976 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
977 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
978 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700979 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300980 FIELD64(TSC_OFFSET, tsc_offset),
981 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
982 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800983 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400984 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300985 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800986 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
987 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
988 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
989 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400990 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700991 FIELD64(VMREAD_BITMAP, vmread_bitmap),
992 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800993 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300994 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
995 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
996 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
997 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
998 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
999 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1000 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1001 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1002 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1003 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001004 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001005 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1006 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1007 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1008 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1009 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1010 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1011 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1012 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1013 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1014 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1015 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1016 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1017 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1018 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1019 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1020 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1021 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1022 FIELD(TPR_THRESHOLD, tpr_threshold),
1023 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1024 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1025 FIELD(VM_EXIT_REASON, vm_exit_reason),
1026 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1027 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1028 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1029 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1030 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1031 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1032 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1033 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1034 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1035 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1036 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1037 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1038 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1039 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1040 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1041 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1042 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1043 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1044 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1045 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1046 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1047 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1048 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1049 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1050 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1051 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1052 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1053 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001054 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001055 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1056 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1057 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1058 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1059 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1060 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1061 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1062 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1063 FIELD(EXIT_QUALIFICATION, exit_qualification),
1064 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1065 FIELD(GUEST_CR0, guest_cr0),
1066 FIELD(GUEST_CR3, guest_cr3),
1067 FIELD(GUEST_CR4, guest_cr4),
1068 FIELD(GUEST_ES_BASE, guest_es_base),
1069 FIELD(GUEST_CS_BASE, guest_cs_base),
1070 FIELD(GUEST_SS_BASE, guest_ss_base),
1071 FIELD(GUEST_DS_BASE, guest_ds_base),
1072 FIELD(GUEST_FS_BASE, guest_fs_base),
1073 FIELD(GUEST_GS_BASE, guest_gs_base),
1074 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1075 FIELD(GUEST_TR_BASE, guest_tr_base),
1076 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1077 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1078 FIELD(GUEST_DR7, guest_dr7),
1079 FIELD(GUEST_RSP, guest_rsp),
1080 FIELD(GUEST_RIP, guest_rip),
1081 FIELD(GUEST_RFLAGS, guest_rflags),
1082 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1083 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1084 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1085 FIELD(HOST_CR0, host_cr0),
1086 FIELD(HOST_CR3, host_cr3),
1087 FIELD(HOST_CR4, host_cr4),
1088 FIELD(HOST_FS_BASE, host_fs_base),
1089 FIELD(HOST_GS_BASE, host_gs_base),
1090 FIELD(HOST_TR_BASE, host_tr_base),
1091 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1092 FIELD(HOST_IDTR_BASE, host_idtr_base),
1093 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1094 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1095 FIELD(HOST_RSP, host_rsp),
1096 FIELD(HOST_RIP, host_rip),
1097};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001098
1099static inline short vmcs_field_to_offset(unsigned long field)
1100{
Dan Williams085331d2018-01-31 17:47:03 -08001101 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1102 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001103 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001104
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001105 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001106 return -ENOENT;
1107
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001108 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001109 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001110 return -ENOENT;
1111
Linus Torvalds15303ba2018-02-10 13:16:35 -08001112 index = array_index_nospec(index, size);
1113 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001114 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001115 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001116 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001117}
1118
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001119static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1120{
David Matlack4f2777b2016-07-13 17:16:37 -07001121 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001122}
1123
Peter Feiner995f00a2017-06-30 17:26:32 -07001124static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001125static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001126static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001127static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001128static void vmx_set_segment(struct kvm_vcpu *vcpu,
1129 struct kvm_segment *var, int seg);
1130static void vmx_get_segment(struct kvm_vcpu *vcpu,
1131 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001132static bool guest_state_valid(struct kvm_vcpu *vcpu);
1133static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001134static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001135static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1136static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1137static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1138 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001139static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001140static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1141 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001142
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1144static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001145/*
1146 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1147 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1148 */
1149static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150
Feng Wubf9f6ac2015-09-18 22:29:55 +08001151/*
1152 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1153 * can find which vCPU should be waken up.
1154 */
1155static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1156static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1157
Radim Krčmář23611332016-09-29 22:41:33 +02001158enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001159 VMX_VMREAD_BITMAP,
1160 VMX_VMWRITE_BITMAP,
1161 VMX_BITMAP_NR
1162};
1163
1164static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1165
Radim Krčmář23611332016-09-29 22:41:33 +02001166#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1167#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001168
Avi Kivity110312c2010-12-21 12:54:20 +02001169static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001170static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001171
Sheng Yang2384d2b2008-01-17 15:14:33 +08001172static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1173static DEFINE_SPINLOCK(vmx_vpid_lock);
1174
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001175static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176 int size;
1177 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001178 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001180 u32 pin_based_exec_ctrl;
1181 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001182 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001183 u32 vmexit_ctrl;
1184 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001185 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001186} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187
Hannes Ederefff9e52008-11-28 17:02:06 +01001188static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001189 u32 ept;
1190 u32 vpid;
1191} vmx_capability;
1192
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193#define VMX_SEGMENT_FIELD(seg) \
1194 [VCPU_SREG_##seg] = { \
1195 .selector = GUEST_##seg##_SELECTOR, \
1196 .base = GUEST_##seg##_BASE, \
1197 .limit = GUEST_##seg##_LIMIT, \
1198 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1199 }
1200
Mathias Krause772e0312012-08-30 01:30:19 +02001201static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202 unsigned selector;
1203 unsigned base;
1204 unsigned limit;
1205 unsigned ar_bytes;
1206} kvm_vmx_segment_fields[] = {
1207 VMX_SEGMENT_FIELD(CS),
1208 VMX_SEGMENT_FIELD(DS),
1209 VMX_SEGMENT_FIELD(ES),
1210 VMX_SEGMENT_FIELD(FS),
1211 VMX_SEGMENT_FIELD(GS),
1212 VMX_SEGMENT_FIELD(SS),
1213 VMX_SEGMENT_FIELD(TR),
1214 VMX_SEGMENT_FIELD(LDTR),
1215};
1216
Avi Kivity26bb0982009-09-07 11:14:12 +03001217static u64 host_efer;
1218
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001219static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1220
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001221/*
Brian Gerst8c065852010-07-17 09:03:26 -04001222 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001223 * away by decrementing the array size.
1224 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001226#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001227 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001229 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001232DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1233
1234#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1235
1236#define KVM_EVMCS_VERSION 1
1237
1238#if IS_ENABLED(CONFIG_HYPERV)
1239static bool __read_mostly enlightened_vmcs = true;
1240module_param(enlightened_vmcs, bool, 0444);
1241
1242static inline void evmcs_write64(unsigned long field, u64 value)
1243{
1244 u16 clean_field;
1245 int offset = get_evmcs_offset(field, &clean_field);
1246
1247 if (offset < 0)
1248 return;
1249
1250 *(u64 *)((char *)current_evmcs + offset) = value;
1251
1252 current_evmcs->hv_clean_fields &= ~clean_field;
1253}
1254
1255static inline void evmcs_write32(unsigned long field, u32 value)
1256{
1257 u16 clean_field;
1258 int offset = get_evmcs_offset(field, &clean_field);
1259
1260 if (offset < 0)
1261 return;
1262
1263 *(u32 *)((char *)current_evmcs + offset) = value;
1264 current_evmcs->hv_clean_fields &= ~clean_field;
1265}
1266
1267static inline void evmcs_write16(unsigned long field, u16 value)
1268{
1269 u16 clean_field;
1270 int offset = get_evmcs_offset(field, &clean_field);
1271
1272 if (offset < 0)
1273 return;
1274
1275 *(u16 *)((char *)current_evmcs + offset) = value;
1276 current_evmcs->hv_clean_fields &= ~clean_field;
1277}
1278
1279static inline u64 evmcs_read64(unsigned long field)
1280{
1281 int offset = get_evmcs_offset(field, NULL);
1282
1283 if (offset < 0)
1284 return 0;
1285
1286 return *(u64 *)((char *)current_evmcs + offset);
1287}
1288
1289static inline u32 evmcs_read32(unsigned long field)
1290{
1291 int offset = get_evmcs_offset(field, NULL);
1292
1293 if (offset < 0)
1294 return 0;
1295
1296 return *(u32 *)((char *)current_evmcs + offset);
1297}
1298
1299static inline u16 evmcs_read16(unsigned long field)
1300{
1301 int offset = get_evmcs_offset(field, NULL);
1302
1303 if (offset < 0)
1304 return 0;
1305
1306 return *(u16 *)((char *)current_evmcs + offset);
1307}
1308
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001309static inline void evmcs_touch_msr_bitmap(void)
1310{
1311 if (unlikely(!current_evmcs))
1312 return;
1313
1314 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1315 current_evmcs->hv_clean_fields &=
1316 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1317}
1318
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001319static void evmcs_load(u64 phys_addr)
1320{
1321 struct hv_vp_assist_page *vp_ap =
1322 hv_get_vp_assist_page(smp_processor_id());
1323
1324 vp_ap->current_nested_vmcs = phys_addr;
1325 vp_ap->enlighten_vmentry = 1;
1326}
1327
1328static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1329{
1330 /*
1331 * Enlightened VMCSv1 doesn't support these:
1332 *
1333 * POSTED_INTR_NV = 0x00000002,
1334 * GUEST_INTR_STATUS = 0x00000810,
1335 * APIC_ACCESS_ADDR = 0x00002014,
1336 * POSTED_INTR_DESC_ADDR = 0x00002016,
1337 * EOI_EXIT_BITMAP0 = 0x0000201c,
1338 * EOI_EXIT_BITMAP1 = 0x0000201e,
1339 * EOI_EXIT_BITMAP2 = 0x00002020,
1340 * EOI_EXIT_BITMAP3 = 0x00002022,
1341 */
1342 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1343 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1344 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1345 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1346 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1347 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1348 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1349
1350 /*
1351 * GUEST_PML_INDEX = 0x00000812,
1352 * PML_ADDRESS = 0x0000200e,
1353 */
1354 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1355
1356 /* VM_FUNCTION_CONTROL = 0x00002018, */
1357 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1358
1359 /*
1360 * EPTP_LIST_ADDRESS = 0x00002024,
1361 * VMREAD_BITMAP = 0x00002026,
1362 * VMWRITE_BITMAP = 0x00002028,
1363 */
1364 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1365
1366 /*
1367 * TSC_MULTIPLIER = 0x00002032,
1368 */
1369 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1370
1371 /*
1372 * PLE_GAP = 0x00004020,
1373 * PLE_WINDOW = 0x00004022,
1374 */
1375 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1376
1377 /*
1378 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1379 */
1380 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1381
1382 /*
1383 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1384 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1385 */
1386 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1387 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1388
1389 /*
1390 * Currently unsupported in KVM:
1391 * GUEST_IA32_RTIT_CTL = 0x00002814,
1392 */
1393}
1394#else /* !IS_ENABLED(CONFIG_HYPERV) */
1395static inline void evmcs_write64(unsigned long field, u64 value) {}
1396static inline void evmcs_write32(unsigned long field, u32 value) {}
1397static inline void evmcs_write16(unsigned long field, u16 value) {}
1398static inline u64 evmcs_read64(unsigned long field) { return 0; }
1399static inline u32 evmcs_read32(unsigned long field) { return 0; }
1400static inline u16 evmcs_read16(unsigned long field) { return 0; }
1401static inline void evmcs_load(u64 phys_addr) {}
1402static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001403static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001404#endif /* IS_ENABLED(CONFIG_HYPERV) */
1405
Jan Kiszka5bb16012016-02-09 20:14:21 +01001406static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407{
1408 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1409 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001410 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1411}
1412
Jan Kiszka6f054852016-02-09 20:15:18 +01001413static inline bool is_debug(u32 intr_info)
1414{
1415 return is_exception_n(intr_info, DB_VECTOR);
1416}
1417
1418static inline bool is_breakpoint(u32 intr_info)
1419{
1420 return is_exception_n(intr_info, BP_VECTOR);
1421}
1422
Jan Kiszka5bb16012016-02-09 20:14:21 +01001423static inline bool is_page_fault(u32 intr_info)
1424{
1425 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001426}
1427
Gui Jianfeng31299942010-03-15 17:29:09 +08001428static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001429{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001430 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001431}
1432
Gui Jianfeng31299942010-03-15 17:29:09 +08001433static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001434{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001435 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001436}
1437
Liran Alon9e869482018-03-12 13:12:51 +02001438static inline bool is_gp_fault(u32 intr_info)
1439{
1440 return is_exception_n(intr_info, GP_VECTOR);
1441}
1442
Gui Jianfeng31299942010-03-15 17:29:09 +08001443static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444{
1445 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1446 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1447}
1448
Gui Jianfeng31299942010-03-15 17:29:09 +08001449static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001450{
1451 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1452 INTR_INFO_VALID_MASK)) ==
1453 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1454}
1455
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001456/* Undocumented: icebp/int1 */
1457static inline bool is_icebp(u32 intr_info)
1458{
1459 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1460 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1461}
1462
Gui Jianfeng31299942010-03-15 17:29:09 +08001463static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001464{
Sheng Yang04547152009-04-01 15:52:31 +08001465 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001466}
1467
Gui Jianfeng31299942010-03-15 17:29:09 +08001468static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001469{
Sheng Yang04547152009-04-01 15:52:31 +08001470 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001471}
1472
Paolo Bonzini35754c92015-07-29 12:05:37 +02001473static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001474{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001475 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001476}
1477
Gui Jianfeng31299942010-03-15 17:29:09 +08001478static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001479{
Sheng Yang04547152009-04-01 15:52:31 +08001480 return vmcs_config.cpu_based_exec_ctrl &
1481 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001482}
1483
Avi Kivity774ead32007-12-26 13:57:04 +02001484static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001485{
Sheng Yang04547152009-04-01 15:52:31 +08001486 return vmcs_config.cpu_based_2nd_exec_ctrl &
1487 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1488}
1489
Yang Zhang8d146952013-01-25 10:18:50 +08001490static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1491{
1492 return vmcs_config.cpu_based_2nd_exec_ctrl &
1493 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1494}
1495
Yang Zhang83d4c282013-01-25 10:18:49 +08001496static inline bool cpu_has_vmx_apic_register_virt(void)
1497{
1498 return vmcs_config.cpu_based_2nd_exec_ctrl &
1499 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1500}
1501
Yang Zhangc7c9c562013-01-25 10:18:51 +08001502static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1503{
1504 return vmcs_config.cpu_based_2nd_exec_ctrl &
1505 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1506}
1507
Yunhong Jiang64672c92016-06-13 14:19:59 -07001508/*
1509 * Comment's format: document - errata name - stepping - processor name.
1510 * Refer from
1511 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1512 */
1513static u32 vmx_preemption_cpu_tfms[] = {
1514/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15150x000206E6,
1516/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1517/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1518/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15190x00020652,
1520/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15210x00020655,
1522/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1523/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1524/*
1525 * 320767.pdf - AAP86 - B1 -
1526 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1527 */
15280x000106E5,
1529/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15300x000106A0,
1531/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15320x000106A1,
1533/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15340x000106A4,
1535 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1536 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1537 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15380x000106A5,
1539};
1540
1541static inline bool cpu_has_broken_vmx_preemption_timer(void)
1542{
1543 u32 eax = cpuid_eax(0x00000001), i;
1544
1545 /* Clear the reserved bits */
1546 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001547 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001548 if (eax == vmx_preemption_cpu_tfms[i])
1549 return true;
1550
1551 return false;
1552}
1553
1554static inline bool cpu_has_vmx_preemption_timer(void)
1555{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001556 return vmcs_config.pin_based_exec_ctrl &
1557 PIN_BASED_VMX_PREEMPTION_TIMER;
1558}
1559
Yang Zhang01e439b2013-04-11 19:25:12 +08001560static inline bool cpu_has_vmx_posted_intr(void)
1561{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001562 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1563 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001564}
1565
1566static inline bool cpu_has_vmx_apicv(void)
1567{
1568 return cpu_has_vmx_apic_register_virt() &&
1569 cpu_has_vmx_virtual_intr_delivery() &&
1570 cpu_has_vmx_posted_intr();
1571}
1572
Sheng Yang04547152009-04-01 15:52:31 +08001573static inline bool cpu_has_vmx_flexpriority(void)
1574{
1575 return cpu_has_vmx_tpr_shadow() &&
1576 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001577}
1578
Marcelo Tosattie7997942009-06-11 12:07:40 -03001579static inline bool cpu_has_vmx_ept_execute_only(void)
1580{
Gui Jianfeng31299942010-03-15 17:29:09 +08001581 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001582}
1583
Marcelo Tosattie7997942009-06-11 12:07:40 -03001584static inline bool cpu_has_vmx_ept_2m_page(void)
1585{
Gui Jianfeng31299942010-03-15 17:29:09 +08001586 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001587}
1588
Sheng Yang878403b2010-01-05 19:02:29 +08001589static inline bool cpu_has_vmx_ept_1g_page(void)
1590{
Gui Jianfeng31299942010-03-15 17:29:09 +08001591 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001592}
1593
Sheng Yang4bc9b982010-06-02 14:05:24 +08001594static inline bool cpu_has_vmx_ept_4levels(void)
1595{
1596 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1597}
1598
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001599static inline bool cpu_has_vmx_ept_mt_wb(void)
1600{
1601 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1602}
1603
Yu Zhang855feb62017-08-24 20:27:55 +08001604static inline bool cpu_has_vmx_ept_5levels(void)
1605{
1606 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1607}
1608
Xudong Hao83c3a332012-05-28 19:33:35 +08001609static inline bool cpu_has_vmx_ept_ad_bits(void)
1610{
1611 return vmx_capability.ept & VMX_EPT_AD_BIT;
1612}
1613
Gui Jianfeng31299942010-03-15 17:29:09 +08001614static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001615{
Gui Jianfeng31299942010-03-15 17:29:09 +08001616 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001617}
1618
Gui Jianfeng31299942010-03-15 17:29:09 +08001619static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001620{
Gui Jianfeng31299942010-03-15 17:29:09 +08001621 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001622}
1623
Liran Aloncd9a4912018-05-22 17:16:15 +03001624static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1625{
1626 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1627}
1628
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001629static inline bool cpu_has_vmx_invvpid_single(void)
1630{
1631 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1632}
1633
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001634static inline bool cpu_has_vmx_invvpid_global(void)
1635{
1636 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1637}
1638
Wanpeng Li08d839c2017-03-23 05:30:08 -07001639static inline bool cpu_has_vmx_invvpid(void)
1640{
1641 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1642}
1643
Gui Jianfeng31299942010-03-15 17:29:09 +08001644static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001645{
Sheng Yang04547152009-04-01 15:52:31 +08001646 return vmcs_config.cpu_based_2nd_exec_ctrl &
1647 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001648}
1649
Gui Jianfeng31299942010-03-15 17:29:09 +08001650static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001651{
1652 return vmcs_config.cpu_based_2nd_exec_ctrl &
1653 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1654}
1655
Gui Jianfeng31299942010-03-15 17:29:09 +08001656static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001657{
1658 return vmcs_config.cpu_based_2nd_exec_ctrl &
1659 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1660}
1661
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001662static inline bool cpu_has_vmx_basic_inout(void)
1663{
1664 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1665}
1666
Paolo Bonzini35754c92015-07-29 12:05:37 +02001667static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001668{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001669 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001670}
1671
Gui Jianfeng31299942010-03-15 17:29:09 +08001672static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001673{
Sheng Yang04547152009-04-01 15:52:31 +08001674 return vmcs_config.cpu_based_2nd_exec_ctrl &
1675 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001676}
1677
Gui Jianfeng31299942010-03-15 17:29:09 +08001678static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001679{
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_RDTSCP;
1682}
1683
Mao, Junjiead756a12012-07-02 01:18:48 +00001684static inline bool cpu_has_vmx_invpcid(void)
1685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_ENABLE_INVPCID;
1688}
1689
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001690static inline bool cpu_has_virtual_nmis(void)
1691{
1692 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1693}
1694
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001695static inline bool cpu_has_vmx_wbinvd_exit(void)
1696{
1697 return vmcs_config.cpu_based_2nd_exec_ctrl &
1698 SECONDARY_EXEC_WBINVD_EXITING;
1699}
1700
Abel Gordonabc4fc52013-04-18 14:35:25 +03001701static inline bool cpu_has_vmx_shadow_vmcs(void)
1702{
1703 u64 vmx_msr;
1704 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1705 /* check if the cpu supports writing r/o exit information fields */
1706 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1707 return false;
1708
1709 return vmcs_config.cpu_based_2nd_exec_ctrl &
1710 SECONDARY_EXEC_SHADOW_VMCS;
1711}
1712
Kai Huang843e4332015-01-28 10:54:28 +08001713static inline bool cpu_has_vmx_pml(void)
1714{
1715 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1716}
1717
Haozhong Zhang64903d62015-10-20 15:39:09 +08001718static inline bool cpu_has_vmx_tsc_scaling(void)
1719{
1720 return vmcs_config.cpu_based_2nd_exec_ctrl &
1721 SECONDARY_EXEC_TSC_SCALING;
1722}
1723
Bandan Das2a499e42017-08-03 15:54:41 -04001724static inline bool cpu_has_vmx_vmfunc(void)
1725{
1726 return vmcs_config.cpu_based_2nd_exec_ctrl &
1727 SECONDARY_EXEC_ENABLE_VMFUNC;
1728}
1729
Sean Christopherson64f7a112018-04-30 10:01:06 -07001730static bool vmx_umip_emulated(void)
1731{
1732 return vmcs_config.cpu_based_2nd_exec_ctrl &
1733 SECONDARY_EXEC_DESC;
1734}
1735
Sheng Yang04547152009-04-01 15:52:31 +08001736static inline bool report_flexpriority(void)
1737{
1738 return flexpriority_enabled;
1739}
1740
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001741static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1742{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001743 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001744}
1745
Jim Mattsonf4160e42018-05-29 09:11:33 -07001746/*
1747 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1748 * to modify any valid field of the VMCS, or are the VM-exit
1749 * information fields read-only?
1750 */
1751static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1752{
1753 return to_vmx(vcpu)->nested.msrs.misc_low &
1754 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1755}
1756
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001757static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1758{
1759 return vmcs12->cpu_based_vm_exec_control & bit;
1760}
1761
1762static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1763{
1764 return (vmcs12->cpu_based_vm_exec_control &
1765 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1766 (vmcs12->secondary_vm_exec_control & bit);
1767}
1768
Jan Kiszkaf41245002014-03-07 20:03:13 +01001769static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1770{
1771 return vmcs12->pin_based_vm_exec_control &
1772 PIN_BASED_VMX_PREEMPTION_TIMER;
1773}
1774
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001775static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1776{
1777 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1778}
1779
1780static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1781{
1782 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1783}
1784
Nadav Har'El155a97a2013-08-05 11:07:16 +03001785static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1786{
1787 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1788}
1789
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001790static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1791{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001792 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001793}
1794
Bandan Dasc5f983f2017-05-05 15:25:14 -04001795static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1796{
1797 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1798}
1799
Wincy Vanf2b93282015-02-03 23:56:03 +08001800static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1801{
1802 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1803}
1804
Wanpeng Li5c614b32015-10-13 09:18:36 -07001805static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1806{
1807 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1808}
1809
Wincy Van82f0dd42015-02-03 23:57:18 +08001810static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1811{
1812 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1813}
1814
Wincy Van608406e2015-02-03 23:57:51 +08001815static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1816{
1817 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1818}
1819
Wincy Van705699a2015-02-03 23:58:17 +08001820static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1821{
1822 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1823}
1824
Bandan Das27c42a12017-08-03 15:54:42 -04001825static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1826{
1827 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1828}
1829
Bandan Das41ab9372017-08-03 15:54:43 -04001830static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1831{
1832 return nested_cpu_has_vmfunc(vmcs12) &&
1833 (vmcs12->vm_function_control &
1834 VMX_VMFUNC_EPTP_SWITCHING);
1835}
1836
Jim Mattsonef85b672016-12-12 11:01:37 -08001837static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001838{
1839 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001840 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001841}
1842
Jan Kiszka533558b2014-01-04 18:47:20 +01001843static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1844 u32 exit_intr_info,
1845 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001846static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1847 struct vmcs12 *vmcs12,
1848 u32 reason, unsigned long qualification);
1849
Rusty Russell8b9cf982007-07-30 16:31:43 +10001850static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001851{
1852 int i;
1853
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001854 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001855 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001856 return i;
1857 return -1;
1858}
1859
Sheng Yang2384d2b2008-01-17 15:14:33 +08001860static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1861{
1862 struct {
1863 u64 vpid : 16;
1864 u64 rsvd : 48;
1865 u64 gva;
1866 } operand = { vpid, 0, gva };
1867
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001868 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001869 /* CF==1 or ZF==1 --> rc = -1 */
1870 "; ja 1f ; ud2 ; 1:"
1871 : : "a"(&operand), "c"(ext) : "cc", "memory");
1872}
1873
Sheng Yang14394422008-04-28 12:24:45 +08001874static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1875{
1876 struct {
1877 u64 eptp, gpa;
1878 } operand = {eptp, gpa};
1879
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001880 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001881 /* CF==1 or ZF==1 --> rc = -1 */
1882 "; ja 1f ; ud2 ; 1:\n"
1883 : : "a" (&operand), "c" (ext) : "cc", "memory");
1884}
1885
Avi Kivity26bb0982009-09-07 11:14:12 +03001886static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001887{
1888 int i;
1889
Rusty Russell8b9cf982007-07-30 16:31:43 +10001890 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001891 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001892 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001893 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001894}
1895
Avi Kivity6aa8b732006-12-10 02:21:36 -08001896static void vmcs_clear(struct vmcs *vmcs)
1897{
1898 u64 phys_addr = __pa(vmcs);
1899 u8 error;
1900
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001901 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001902 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903 : "cc", "memory");
1904 if (error)
1905 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1906 vmcs, phys_addr);
1907}
1908
Nadav Har'Eld462b812011-05-24 15:26:10 +03001909static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1910{
1911 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001912 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1913 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001914 loaded_vmcs->cpu = -1;
1915 loaded_vmcs->launched = 0;
1916}
1917
Dongxiao Xu7725b892010-05-11 18:29:38 +08001918static void vmcs_load(struct vmcs *vmcs)
1919{
1920 u64 phys_addr = __pa(vmcs);
1921 u8 error;
1922
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001923 if (static_branch_unlikely(&enable_evmcs))
1924 return evmcs_load(phys_addr);
1925
Dongxiao Xu7725b892010-05-11 18:29:38 +08001926 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001927 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001928 : "cc", "memory");
1929 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001930 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001931 vmcs, phys_addr);
1932}
1933
Dave Young2965faa2015-09-09 15:38:55 -07001934#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001935/*
1936 * This bitmap is used to indicate whether the vmclear
1937 * operation is enabled on all cpus. All disabled by
1938 * default.
1939 */
1940static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1941
1942static inline void crash_enable_local_vmclear(int cpu)
1943{
1944 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1945}
1946
1947static inline void crash_disable_local_vmclear(int cpu)
1948{
1949 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1950}
1951
1952static inline int crash_local_vmclear_enabled(int cpu)
1953{
1954 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1955}
1956
1957static void crash_vmclear_local_loaded_vmcss(void)
1958{
1959 int cpu = raw_smp_processor_id();
1960 struct loaded_vmcs *v;
1961
1962 if (!crash_local_vmclear_enabled(cpu))
1963 return;
1964
1965 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1966 loaded_vmcss_on_cpu_link)
1967 vmcs_clear(v->vmcs);
1968}
1969#else
1970static inline void crash_enable_local_vmclear(int cpu) { }
1971static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001972#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001973
Nadav Har'Eld462b812011-05-24 15:26:10 +03001974static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001975{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001976 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001977 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978
Nadav Har'Eld462b812011-05-24 15:26:10 +03001979 if (loaded_vmcs->cpu != cpu)
1980 return; /* vcpu migration can race with cpu offline */
1981 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001983 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001984 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001985
1986 /*
1987 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1988 * is before setting loaded_vmcs->vcpu to -1 which is done in
1989 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1990 * then adds the vmcs into percpu list before it is deleted.
1991 */
1992 smp_wmb();
1993
Nadav Har'Eld462b812011-05-24 15:26:10 +03001994 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001995 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996}
1997
Nadav Har'Eld462b812011-05-24 15:26:10 +03001998static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001999{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002000 int cpu = loaded_vmcs->cpu;
2001
2002 if (cpu != -1)
2003 smp_call_function_single(cpu,
2004 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002005}
2006
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002007static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002008{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002009 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002010 return;
2011
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002012 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002013 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002014}
2015
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002016static inline void vpid_sync_vcpu_global(void)
2017{
2018 if (cpu_has_vmx_invvpid_global())
2019 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2020}
2021
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002022static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002023{
2024 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002025 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002026 else
2027 vpid_sync_vcpu_global();
2028}
2029
Sheng Yang14394422008-04-28 12:24:45 +08002030static inline void ept_sync_global(void)
2031{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002032 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002033}
2034
2035static inline void ept_sync_context(u64 eptp)
2036{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002037 if (cpu_has_vmx_invept_context())
2038 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2039 else
2040 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002041}
2042
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002043static __always_inline void vmcs_check16(unsigned long field)
2044{
2045 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2046 "16-bit accessor invalid for 64-bit field");
2047 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2048 "16-bit accessor invalid for 64-bit high field");
2049 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2050 "16-bit accessor invalid for 32-bit high field");
2051 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2052 "16-bit accessor invalid for natural width field");
2053}
2054
2055static __always_inline void vmcs_check32(unsigned long field)
2056{
2057 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2058 "32-bit accessor invalid for 16-bit field");
2059 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2060 "32-bit accessor invalid for natural width field");
2061}
2062
2063static __always_inline void vmcs_check64(unsigned long field)
2064{
2065 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2066 "64-bit accessor invalid for 16-bit field");
2067 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2068 "64-bit accessor invalid for 64-bit high field");
2069 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2070 "64-bit accessor invalid for 32-bit field");
2071 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2072 "64-bit accessor invalid for natural width field");
2073}
2074
2075static __always_inline void vmcs_checkl(unsigned long field)
2076{
2077 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2078 "Natural width accessor invalid for 16-bit field");
2079 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2080 "Natural width accessor invalid for 64-bit field");
2081 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2082 "Natural width accessor invalid for 64-bit high field");
2083 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2084 "Natural width accessor invalid for 32-bit field");
2085}
2086
2087static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088{
Avi Kivity5e520e62011-05-15 10:13:12 -04002089 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090
Avi Kivity5e520e62011-05-15 10:13:12 -04002091 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2092 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093 return value;
2094}
2095
Avi Kivity96304212011-05-15 10:13:13 -04002096static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002097{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002098 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002099 if (static_branch_unlikely(&enable_evmcs))
2100 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002101 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102}
2103
Avi Kivity96304212011-05-15 10:13:13 -04002104static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002106 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002107 if (static_branch_unlikely(&enable_evmcs))
2108 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002109 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002110}
2111
Avi Kivity96304212011-05-15 10:13:13 -04002112static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002114 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002115 if (static_branch_unlikely(&enable_evmcs))
2116 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002117#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002118 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002120 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002121#endif
2122}
2123
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002124static __always_inline unsigned long vmcs_readl(unsigned long field)
2125{
2126 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002127 if (static_branch_unlikely(&enable_evmcs))
2128 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002129 return __vmcs_readl(field);
2130}
2131
Avi Kivitye52de1b2007-01-05 16:36:56 -08002132static noinline void vmwrite_error(unsigned long field, unsigned long value)
2133{
2134 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2135 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2136 dump_stack();
2137}
2138
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002139static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002140{
2141 u8 error;
2142
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002143 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002144 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002145 if (unlikely(error))
2146 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002147}
2148
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002149static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002151 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002152 if (static_branch_unlikely(&enable_evmcs))
2153 return evmcs_write16(field, value);
2154
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002155 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156}
2157
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002158static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002159{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002160 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002161 if (static_branch_unlikely(&enable_evmcs))
2162 return evmcs_write32(field, value);
2163
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002164 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165}
2166
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002167static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002169 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002170 if (static_branch_unlikely(&enable_evmcs))
2171 return evmcs_write64(field, value);
2172
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002173 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002174#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002175 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002176 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177#endif
2178}
2179
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002180static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002181{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002182 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002183 if (static_branch_unlikely(&enable_evmcs))
2184 return evmcs_write64(field, value);
2185
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002186 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002187}
2188
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002189static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002190{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002191 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2192 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002193 if (static_branch_unlikely(&enable_evmcs))
2194 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2195
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002196 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2197}
2198
2199static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2200{
2201 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2202 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002203 if (static_branch_unlikely(&enable_evmcs))
2204 return evmcs_write32(field, evmcs_read32(field) | mask);
2205
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002206 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002207}
2208
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002209static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2210{
2211 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2212}
2213
Gleb Natapov2961e8762013-11-25 15:37:13 +02002214static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2215{
2216 vmcs_write32(VM_ENTRY_CONTROLS, val);
2217 vmx->vm_entry_controls_shadow = val;
2218}
2219
2220static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2221{
2222 if (vmx->vm_entry_controls_shadow != val)
2223 vm_entry_controls_init(vmx, val);
2224}
2225
2226static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2227{
2228 return vmx->vm_entry_controls_shadow;
2229}
2230
2231
2232static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2233{
2234 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2235}
2236
2237static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2238{
2239 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2240}
2241
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002242static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2243{
2244 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2245}
2246
Gleb Natapov2961e8762013-11-25 15:37:13 +02002247static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2248{
2249 vmcs_write32(VM_EXIT_CONTROLS, val);
2250 vmx->vm_exit_controls_shadow = val;
2251}
2252
2253static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2254{
2255 if (vmx->vm_exit_controls_shadow != val)
2256 vm_exit_controls_init(vmx, val);
2257}
2258
2259static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2260{
2261 return vmx->vm_exit_controls_shadow;
2262}
2263
2264
2265static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2266{
2267 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2268}
2269
2270static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2271{
2272 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2273}
2274
Avi Kivity2fb92db2011-04-27 19:42:18 +03002275static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2276{
2277 vmx->segment_cache.bitmask = 0;
2278}
2279
2280static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2281 unsigned field)
2282{
2283 bool ret;
2284 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2285
2286 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2287 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2288 vmx->segment_cache.bitmask = 0;
2289 }
2290 ret = vmx->segment_cache.bitmask & mask;
2291 vmx->segment_cache.bitmask |= mask;
2292 return ret;
2293}
2294
2295static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2296{
2297 u16 *p = &vmx->segment_cache.seg[seg].selector;
2298
2299 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2300 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2301 return *p;
2302}
2303
2304static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2305{
2306 ulong *p = &vmx->segment_cache.seg[seg].base;
2307
2308 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2309 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2310 return *p;
2311}
2312
2313static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2314{
2315 u32 *p = &vmx->segment_cache.seg[seg].limit;
2316
2317 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2318 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2319 return *p;
2320}
2321
2322static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2323{
2324 u32 *p = &vmx->segment_cache.seg[seg].ar;
2325
2326 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2327 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2328 return *p;
2329}
2330
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002331static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2332{
2333 u32 eb;
2334
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002335 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002336 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002337 /*
2338 * Guest access to VMware backdoor ports could legitimately
2339 * trigger #GP because of TSS I/O permission bitmap.
2340 * We intercept those #GP and allow access to them anyway
2341 * as VMware does.
2342 */
2343 if (enable_vmware_backdoor)
2344 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002345 if ((vcpu->guest_debug &
2346 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2347 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2348 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002349 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002350 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002351 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002352 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002353
2354 /* When we are running a nested L2 guest and L1 specified for it a
2355 * certain exception bitmap, we must trap the same exceptions and pass
2356 * them to L1. When running L2, we will only handle the exceptions
2357 * specified above if L1 did not want them.
2358 */
2359 if (is_guest_mode(vcpu))
2360 eb |= get_vmcs12(vcpu)->exception_bitmap;
2361
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002362 vmcs_write32(EXCEPTION_BITMAP, eb);
2363}
2364
Ashok Raj15d45072018-02-01 22:59:43 +01002365/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002366 * Check if MSR is intercepted for currently loaded MSR bitmap.
2367 */
2368static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2369{
2370 unsigned long *msr_bitmap;
2371 int f = sizeof(unsigned long);
2372
2373 if (!cpu_has_vmx_msr_bitmap())
2374 return true;
2375
2376 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2377
2378 if (msr <= 0x1fff) {
2379 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2380 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2381 msr &= 0x1fff;
2382 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2383 }
2384
2385 return true;
2386}
2387
2388/*
Ashok Raj15d45072018-02-01 22:59:43 +01002389 * Check if MSR is intercepted for L01 MSR bitmap.
2390 */
2391static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2392{
2393 unsigned long *msr_bitmap;
2394 int f = sizeof(unsigned long);
2395
2396 if (!cpu_has_vmx_msr_bitmap())
2397 return true;
2398
2399 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2400
2401 if (msr <= 0x1fff) {
2402 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2403 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2404 msr &= 0x1fff;
2405 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2406 }
2407
2408 return true;
2409}
2410
Gleb Natapov2961e8762013-11-25 15:37:13 +02002411static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2412 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002413{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002414 vm_entry_controls_clearbit(vmx, entry);
2415 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002416}
2417
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002418static int find_msr(struct vmx_msrs *m, unsigned int msr)
2419{
2420 unsigned int i;
2421
2422 for (i = 0; i < m->nr; ++i) {
2423 if (m->val[i].index == msr)
2424 return i;
2425 }
2426 return -ENOENT;
2427}
2428
Avi Kivity61d2ef22010-04-28 16:40:38 +03002429static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2430{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002431 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002432 struct msr_autoload *m = &vmx->msr_autoload;
2433
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002434 switch (msr) {
2435 case MSR_EFER:
2436 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002437 clear_atomic_switch_msr_special(vmx,
2438 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002439 VM_EXIT_LOAD_IA32_EFER);
2440 return;
2441 }
2442 break;
2443 case MSR_CORE_PERF_GLOBAL_CTRL:
2444 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002445 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002446 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2447 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2448 return;
2449 }
2450 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002451 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002452 i = find_msr(&m->guest, msr);
2453 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002454 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002455 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002456 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002457 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002458
2459skip_guest:
2460 i = find_msr(&m->host, msr);
2461 if (i < 0)
2462 return;
2463
2464 --m->host.nr;
2465 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002466 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002467}
2468
Gleb Natapov2961e8762013-11-25 15:37:13 +02002469static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2470 unsigned long entry, unsigned long exit,
2471 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2472 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002473{
2474 vmcs_write64(guest_val_vmcs, guest_val);
2475 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002476 vm_entry_controls_setbit(vmx, entry);
2477 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002478}
2479
Avi Kivity61d2ef22010-04-28 16:40:38 +03002480static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002481 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002482{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002483 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002484 struct msr_autoload *m = &vmx->msr_autoload;
2485
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002486 switch (msr) {
2487 case MSR_EFER:
2488 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002489 add_atomic_switch_msr_special(vmx,
2490 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002491 VM_EXIT_LOAD_IA32_EFER,
2492 GUEST_IA32_EFER,
2493 HOST_IA32_EFER,
2494 guest_val, host_val);
2495 return;
2496 }
2497 break;
2498 case MSR_CORE_PERF_GLOBAL_CTRL:
2499 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002500 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002501 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2502 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2503 GUEST_IA32_PERF_GLOBAL_CTRL,
2504 HOST_IA32_PERF_GLOBAL_CTRL,
2505 guest_val, host_val);
2506 return;
2507 }
2508 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002509 case MSR_IA32_PEBS_ENABLE:
2510 /* PEBS needs a quiescent period after being disabled (to write
2511 * a record). Disabling PEBS through VMX MSR swapping doesn't
2512 * provide that period, so a CPU could write host's record into
2513 * guest's memory.
2514 */
2515 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002516 }
2517
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002518 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002519 if (!entry_only)
2520 j = find_msr(&m->host, msr);
2521
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002522 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002523 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002524 "Can't add msr %x\n", msr);
2525 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002526 }
2527 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002528 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002529 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002530 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002531 m->guest.val[i].index = msr;
2532 m->guest.val[i].value = guest_val;
2533
2534 if (entry_only)
2535 return;
2536
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002537 if (j < 0) {
2538 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002539 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002540 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002541 m->host.val[j].index = msr;
2542 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002543}
2544
Avi Kivity92c0d902009-10-29 11:00:16 +02002545static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002546{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002547 u64 guest_efer = vmx->vcpu.arch.efer;
2548 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002549
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002550 if (!enable_ept) {
2551 /*
2552 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2553 * host CPUID is more efficient than testing guest CPUID
2554 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2555 */
2556 if (boot_cpu_has(X86_FEATURE_SMEP))
2557 guest_efer |= EFER_NX;
2558 else if (!(guest_efer & EFER_NX))
2559 ignore_bits |= EFER_NX;
2560 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002561
Avi Kivity51c6cf62007-08-29 03:48:05 +03002562 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002563 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002564 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002565 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002566#ifdef CONFIG_X86_64
2567 ignore_bits |= EFER_LMA | EFER_LME;
2568 /* SCE is meaningful only in long mode on Intel */
2569 if (guest_efer & EFER_LMA)
2570 ignore_bits &= ~(u64)EFER_SCE;
2571#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002572
2573 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002574
2575 /*
2576 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2577 * On CPUs that support "load IA32_EFER", always switch EFER
2578 * atomically, since it's faster than switching it manually.
2579 */
2580 if (cpu_has_load_ia32_efer ||
2581 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002582 if (!(guest_efer & EFER_LMA))
2583 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002584 if (guest_efer != host_efer)
2585 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002586 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002587 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002588 } else {
2589 guest_efer &= ~ignore_bits;
2590 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002591
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002592 vmx->guest_msrs[efer_offset].data = guest_efer;
2593 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2594
2595 return true;
2596 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002597}
2598
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002599#ifdef CONFIG_X86_32
2600/*
2601 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2602 * VMCS rather than the segment table. KVM uses this helper to figure
2603 * out the current bases to poke them into the VMCS before entry.
2604 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002605static unsigned long segment_base(u16 selector)
2606{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002607 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002608 unsigned long v;
2609
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002610 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002611 return 0;
2612
Thomas Garnier45fc8752017-03-14 10:05:08 -07002613 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002614
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002615 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002616 u16 ldt_selector = kvm_read_ldt();
2617
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002618 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002619 return 0;
2620
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002621 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002622 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002623 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002624 return v;
2625}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002626#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002627
Avi Kivity04d2cc72007-09-10 18:10:54 +03002628static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002629{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002630 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002631#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002632 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002633#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002634 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002635
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002636 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002637 return;
2638
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002639 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002640 /*
2641 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2642 * allow segment selectors with cpl > 0 or ti == 1.
2643 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002644 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002645 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002646
2647#ifdef CONFIG_X86_64
2648 save_fsgs_for_kvm();
2649 vmx->host_state.fs_sel = current->thread.fsindex;
2650 vmx->host_state.gs_sel = current->thread.gsindex;
2651#else
Avi Kivity9581d442010-10-19 16:46:55 +02002652 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002653 savesegment(gs, vmx->host_state.gs_sel);
2654#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002655 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002656 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002657 vmx->host_state.fs_reload_needed = 0;
2658 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002659 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002660 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002661 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002662 if (!(vmx->host_state.gs_sel & 7))
2663 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002664 else {
2665 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002666 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002667 }
2668
2669#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002670 savesegment(ds, vmx->host_state.ds_sel);
2671 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002672
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002673 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002674 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002675
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002676 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002677 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002678 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002679#else
2680 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2681 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2682#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002683 if (boot_cpu_has(X86_FEATURE_MPX))
2684 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002685 for (i = 0; i < vmx->save_nmsrs; ++i)
2686 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002687 vmx->guest_msrs[i].data,
2688 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002689}
2690
Avi Kivitya9b21b62008-06-24 11:48:49 +03002691static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002692{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002693 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002694 return;
2695
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002696 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002697 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002698#ifdef CONFIG_X86_64
2699 if (is_long_mode(&vmx->vcpu))
2700 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2701#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002702 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002703 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002704#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002705 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002706#else
2707 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002708#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002709 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002710 if (vmx->host_state.fs_reload_needed)
2711 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002712#ifdef CONFIG_X86_64
2713 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2714 loadsegment(ds, vmx->host_state.ds_sel);
2715 loadsegment(es, vmx->host_state.es_sel);
2716 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002717#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002718 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002719#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002720 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002721#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002722 if (vmx->host_state.msr_host_bndcfgs)
2723 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002724 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002725}
2726
Avi Kivitya9b21b62008-06-24 11:48:49 +03002727static void vmx_load_host_state(struct vcpu_vmx *vmx)
2728{
2729 preempt_disable();
2730 __vmx_load_host_state(vmx);
2731 preempt_enable();
2732}
2733
Feng Wu28b835d2015-09-18 22:29:54 +08002734static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2735{
2736 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2737 struct pi_desc old, new;
2738 unsigned int dest;
2739
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002740 /*
2741 * In case of hot-plug or hot-unplug, we may have to undo
2742 * vmx_vcpu_pi_put even if there is no assigned device. And we
2743 * always keep PI.NDST up to date for simplicity: it makes the
2744 * code easier, and CPU migration is not a fast path.
2745 */
2746 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002747 return;
2748
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002749 /*
2750 * First handle the simple case where no cmpxchg is necessary; just
2751 * allow posting non-urgent interrupts.
2752 *
2753 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2754 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2755 * expects the VCPU to be on the blocked_vcpu_list that matches
2756 * PI.NDST.
2757 */
2758 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2759 vcpu->cpu == cpu) {
2760 pi_clear_sn(pi_desc);
2761 return;
2762 }
2763
2764 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002765 do {
2766 old.control = new.control = pi_desc->control;
2767
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002768 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002769
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002770 if (x2apic_enabled())
2771 new.ndst = dest;
2772 else
2773 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002774
Feng Wu28b835d2015-09-18 22:29:54 +08002775 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002776 } while (cmpxchg64(&pi_desc->control, old.control,
2777 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002778}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002779
Peter Feinerc95ba922016-08-17 09:36:47 -07002780static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2781{
2782 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2783 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2784}
2785
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786/*
2787 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2788 * vcpu mutex is already taken.
2789 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002790static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002792 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002793 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002795 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002796 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002797 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002798 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002799
2800 /*
2801 * Read loaded_vmcs->cpu should be before fetching
2802 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2803 * See the comments in __loaded_vmcs_clear().
2804 */
2805 smp_rmb();
2806
Nadav Har'Eld462b812011-05-24 15:26:10 +03002807 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2808 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002809 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002810 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002811 }
2812
2813 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2814 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2815 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002816 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002817 }
2818
2819 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002820 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002821 unsigned long sysenter_esp;
2822
2823 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002824
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 /*
2826 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002827 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002829 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002830 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002831 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002833 /*
2834 * VM exits change the host TR limit to 0x67 after a VM
2835 * exit. This is okay, since 0x67 covers everything except
2836 * the IO bitmap and have have code to handle the IO bitmap
2837 * being lost after a VM exit.
2838 */
2839 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2840
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2842 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002843
Nadav Har'Eld462b812011-05-24 15:26:10 +03002844 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 }
Feng Wu28b835d2015-09-18 22:29:54 +08002846
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002847 /* Setup TSC multiplier */
2848 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002849 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2850 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002851
Feng Wu28b835d2015-09-18 22:29:54 +08002852 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002853 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002854 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002855}
2856
2857static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2858{
2859 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2860
2861 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002862 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2863 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002864 return;
2865
2866 /* Set SN when the vCPU is preempted */
2867 if (vcpu->preempted)
2868 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869}
2870
2871static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2872{
Feng Wu28b835d2015-09-18 22:29:54 +08002873 vmx_vcpu_pi_put(vcpu);
2874
Avi Kivitya9b21b62008-06-24 11:48:49 +03002875 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876}
2877
Wanpeng Lif244dee2017-07-20 01:11:54 -07002878static bool emulation_required(struct kvm_vcpu *vcpu)
2879{
2880 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2881}
2882
Avi Kivityedcafe32009-12-30 18:07:40 +02002883static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2884
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002885/*
2886 * Return the cr0 value that a nested guest would read. This is a combination
2887 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2888 * its hypervisor (cr0_read_shadow).
2889 */
2890static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2891{
2892 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2893 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2894}
2895static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2896{
2897 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2898 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2899}
2900
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2902{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002903 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002904
Avi Kivity6de12732011-03-07 12:51:22 +02002905 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2906 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2907 rflags = vmcs_readl(GUEST_RFLAGS);
2908 if (to_vmx(vcpu)->rmode.vm86_active) {
2909 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2910 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2911 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2912 }
2913 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002914 }
Avi Kivity6de12732011-03-07 12:51:22 +02002915 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002916}
2917
2918static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2919{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002920 unsigned long old_rflags = vmx_get_rflags(vcpu);
2921
Avi Kivity6de12732011-03-07 12:51:22 +02002922 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2923 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002924 if (to_vmx(vcpu)->rmode.vm86_active) {
2925 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002926 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002927 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002929
2930 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2931 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932}
2933
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002934static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002935{
2936 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2937 int ret = 0;
2938
2939 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002940 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002941 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002942 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002943
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002944 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002945}
2946
2947static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2948{
2949 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2950 u32 interruptibility = interruptibility_old;
2951
2952 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2953
Jan Kiszka48005f62010-02-19 19:38:07 +01002954 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002955 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002956 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002957 interruptibility |= GUEST_INTR_STATE_STI;
2958
2959 if ((interruptibility != interruptibility_old))
2960 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2961}
2962
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2964{
2965 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002967 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002969 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970
Glauber Costa2809f5d2009-05-12 16:21:05 -04002971 /* skipping an emulated instruction also counts */
2972 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973}
2974
Paolo Bonzinib96fb432017-07-27 12:29:32 +02002975static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
2976 unsigned long exit_qual)
2977{
2978 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2979 unsigned int nr = vcpu->arch.exception.nr;
2980 u32 intr_info = nr | INTR_INFO_VALID_MASK;
2981
2982 if (vcpu->arch.exception.has_error_code) {
2983 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
2984 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2985 }
2986
2987 if (kvm_exception_is_soft(nr))
2988 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2989 else
2990 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2991
2992 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
2993 vmx_get_nmi_mask(vcpu))
2994 intr_info |= INTR_INFO_UNBLOCK_NMI;
2995
2996 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
2997}
2998
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002999/*
3000 * KVM wants to inject page-faults which it got to the guest. This function
3001 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003002 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003003static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003004{
3005 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003006 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003007
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003008 if (nr == PF_VECTOR) {
3009 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003010 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003011 return 1;
3012 }
3013 /*
3014 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3015 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3016 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3017 * can be written only when inject_pending_event runs. This should be
3018 * conditional on a new capability---if the capability is disabled,
3019 * kvm_multiple_exception would write the ancillary information to
3020 * CR2 or DR6, for backwards ABI-compatibility.
3021 */
3022 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3023 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003024 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003025 return 1;
3026 }
3027 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003028 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003029 if (nr == DB_VECTOR)
3030 *exit_qual = vcpu->arch.dr6;
3031 else
3032 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003033 return 1;
3034 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003035 }
3036
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003037 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003038}
3039
Wanpeng Licaa057a2018-03-12 04:53:03 -07003040static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3041{
3042 /*
3043 * Ensure that we clear the HLT state in the VMCS. We don't need to
3044 * explicitly skip the instruction because if the HLT state is set,
3045 * then the instruction is already executing and RIP has already been
3046 * advanced.
3047 */
3048 if (kvm_hlt_in_guest(vcpu->kvm) &&
3049 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3050 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3051}
3052
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003053static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003054{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003055 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003056 unsigned nr = vcpu->arch.exception.nr;
3057 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003058 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003059 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003060
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003061 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003062 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003063 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3064 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003065
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003066 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003067 int inc_eip = 0;
3068 if (kvm_exception_is_soft(nr))
3069 inc_eip = vcpu->arch.event_exit_inst_len;
3070 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003071 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003072 return;
3073 }
3074
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003075 WARN_ON_ONCE(vmx->emulation_required);
3076
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003077 if (kvm_exception_is_soft(nr)) {
3078 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3079 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003080 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3081 } else
3082 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3083
3084 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003085
3086 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003087}
3088
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003089static bool vmx_rdtscp_supported(void)
3090{
3091 return cpu_has_vmx_rdtscp();
3092}
3093
Mao, Junjiead756a12012-07-02 01:18:48 +00003094static bool vmx_invpcid_supported(void)
3095{
3096 return cpu_has_vmx_invpcid() && enable_ept;
3097}
3098
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099/*
Eddie Donga75beee2007-05-17 18:55:15 +03003100 * Swap MSR entry in host/guest MSR entry array.
3101 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003102static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003103{
Avi Kivity26bb0982009-09-07 11:14:12 +03003104 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003105
3106 tmp = vmx->guest_msrs[to];
3107 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3108 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003109}
3110
3111/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003112 * Set up the vmcs to automatically save and restore system
3113 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3114 * mode, as fiddling with msrs is very expensive.
3115 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003116static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003117{
Avi Kivity26bb0982009-09-07 11:14:12 +03003118 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003119
Eddie Donga75beee2007-05-17 18:55:15 +03003120 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003121#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003122 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003123 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003124 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003125 move_msr_up(vmx, index, save_nmsrs++);
3126 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003127 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003128 move_msr_up(vmx, index, save_nmsrs++);
3129 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003130 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003131 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003132 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003133 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003134 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003135 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003136 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003137 * if efer.sce is enabled.
3138 */
Brian Gerst8c065852010-07-17 09:03:26 -04003139 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003140 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003141 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003142 }
Eddie Donga75beee2007-05-17 18:55:15 +03003143#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003144 index = __find_msr_index(vmx, MSR_EFER);
3145 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003146 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003147
Avi Kivity26bb0982009-09-07 11:14:12 +03003148 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003149
Yang Zhang8d146952013-01-25 10:18:50 +08003150 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003151 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003152}
3153
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003154static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003156 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003158 if (is_guest_mode(vcpu) &&
3159 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3160 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3161
3162 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163}
3164
3165/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003166 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003168static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003170 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003171 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003172 * We're here if L1 chose not to trap WRMSR to TSC. According
3173 * to the spec, this should set L1's TSC; The offset that L1
3174 * set for L2 remains unchanged, and still needs to be added
3175 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003176 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003177 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003178 /* recalculate vmcs02.TSC_OFFSET: */
3179 vmcs12 = get_vmcs12(vcpu);
3180 vmcs_write64(TSC_OFFSET, offset +
3181 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3182 vmcs12->tsc_offset : 0));
3183 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003184 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3185 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003186 vmcs_write64(TSC_OFFSET, offset);
3187 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188}
3189
Nadav Har'El801d3422011-05-25 23:02:23 +03003190/*
3191 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3192 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3193 * all guests if the "nested" module option is off, and can also be disabled
3194 * for a single guest by disabling its VMX cpuid bit.
3195 */
3196static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3197{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003198 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003199}
3200
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003202 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3203 * returned for the various VMX controls MSRs when nested VMX is enabled.
3204 * The same values should also be used to verify that vmcs12 control fields are
3205 * valid during nested entry from L1 to L2.
3206 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3207 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3208 * bit in the high half is on if the corresponding bit in the control field
3209 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003210 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003211static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003212{
Paolo Bonzini13893092018-02-26 13:40:09 +01003213 if (!nested) {
3214 memset(msrs, 0, sizeof(*msrs));
3215 return;
3216 }
3217
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003218 /*
3219 * Note that as a general rule, the high half of the MSRs (bits in
3220 * the control fields which may be 1) should be initialized by the
3221 * intersection of the underlying hardware's MSR (i.e., features which
3222 * can be supported) and the list of features we want to expose -
3223 * because they are known to be properly supported in our code.
3224 * Also, usually, the low half of the MSRs (bits which must be 1) can
3225 * be set to 0, meaning that L1 may turn off any of these bits. The
3226 * reason is that if one of these bits is necessary, it will appear
3227 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3228 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003229 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003230 * These rules have exceptions below.
3231 */
3232
3233 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003234 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003235 msrs->pinbased_ctls_low,
3236 msrs->pinbased_ctls_high);
3237 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003238 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003239 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003240 PIN_BASED_EXT_INTR_MASK |
3241 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003242 PIN_BASED_VIRTUAL_NMIS |
3243 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003244 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003245 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003246 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003247
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003248 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003249 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003250 msrs->exit_ctls_low,
3251 msrs->exit_ctls_high);
3252 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003253 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003254
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003255 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003256#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003257 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003258#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003259 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003260 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003261 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003262 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003263 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3264
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003265 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003266 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003267
Jan Kiszka2996fca2014-06-16 13:59:43 +02003268 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003269 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003270
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003271 /* entry controls */
3272 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003273 msrs->entry_ctls_low,
3274 msrs->entry_ctls_high);
3275 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003276 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003277 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003278#ifdef CONFIG_X86_64
3279 VM_ENTRY_IA32E_MODE |
3280#endif
3281 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003282 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003283 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003284 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003285 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003286
Jan Kiszka2996fca2014-06-16 13:59:43 +02003287 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003288 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003289
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003290 /* cpu-based controls */
3291 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003292 msrs->procbased_ctls_low,
3293 msrs->procbased_ctls_high);
3294 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003295 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003296 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003297 CPU_BASED_VIRTUAL_INTR_PENDING |
3298 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003299 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3300 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3301 CPU_BASED_CR3_STORE_EXITING |
3302#ifdef CONFIG_X86_64
3303 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3304#endif
3305 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003306 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3307 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3308 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3309 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003310 /*
3311 * We can allow some features even when not supported by the
3312 * hardware. For example, L1 can specify an MSR bitmap - and we
3313 * can use it to avoid exits to L1 - even when L0 runs L2
3314 * without MSR bitmaps.
3315 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003316 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003317 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003318 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003319
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003320 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003321 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003322 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3323
Paolo Bonzini80154d72017-08-24 13:55:35 +02003324 /*
3325 * secondary cpu-based controls. Do not include those that
3326 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3327 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003328 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003329 msrs->secondary_ctls_low,
3330 msrs->secondary_ctls_high);
3331 msrs->secondary_ctls_low = 0;
3332 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003333 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003334 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003335 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003336 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003337 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003338 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003339
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003340 if (enable_ept) {
3341 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003342 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003343 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003344 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003345 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003346 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003347 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003348 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003349 msrs->ept_caps &= vmx_capability.ept;
3350 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003351 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3352 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003353 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003354 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003355 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003356 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003357 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003358 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003359
Bandan Das27c42a12017-08-03 15:54:42 -04003360 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003361 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003362 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003363 /*
3364 * Advertise EPTP switching unconditionally
3365 * since we emulate it
3366 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003367 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003368 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003369 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003370 }
3371
Paolo Bonzinief697a72016-03-18 16:58:38 +01003372 /*
3373 * Old versions of KVM use the single-context version without
3374 * checking for support, so declare that it is supported even
3375 * though it is treated as global context. The alternative is
3376 * not failing the single-context invvpid, and it is worse.
3377 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003378 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003379 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003380 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003381 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003382 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003383 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003384
Radim Krčmář0790ec12015-03-17 14:02:32 +01003385 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003386 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003387 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3388
Jan Kiszkac18911a2013-03-13 16:06:41 +01003389 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003390 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003391 msrs->misc_low,
3392 msrs->misc_high);
3393 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3394 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003395 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003396 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003397 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003398 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003399
3400 /*
3401 * This MSR reports some information about VMX support. We
3402 * should return information about the VMX we emulate for the
3403 * guest, and the VMCS structure we give it - not about the
3404 * VMX support of the underlying hardware.
3405 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003406 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003407 VMCS12_REVISION |
3408 VMX_BASIC_TRUE_CTLS |
3409 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3410 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3411
3412 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003413 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003414
3415 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003416 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003417 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3418 * We picked the standard core2 setting.
3419 */
3420#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3421#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003422 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3423 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003424
3425 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003426 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3427 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003428
3429 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003430 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003431}
3432
David Matlack38991522016-11-29 18:14:08 -08003433/*
3434 * if fixed0[i] == 1: val[i] must be 1
3435 * if fixed1[i] == 0: val[i] must be 0
3436 */
3437static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3438{
3439 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003440}
3441
3442static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3443{
David Matlack38991522016-11-29 18:14:08 -08003444 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003445}
3446
3447static inline u64 vmx_control_msr(u32 low, u32 high)
3448{
3449 return low | ((u64)high << 32);
3450}
3451
David Matlack62cc6b9d2016-11-29 18:14:07 -08003452static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3453{
3454 superset &= mask;
3455 subset &= mask;
3456
3457 return (superset | subset) == superset;
3458}
3459
3460static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3461{
3462 const u64 feature_and_reserved =
3463 /* feature (except bit 48; see below) */
3464 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3465 /* reserved */
3466 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003467 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003468
3469 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3470 return -EINVAL;
3471
3472 /*
3473 * KVM does not emulate a version of VMX that constrains physical
3474 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3475 */
3476 if (data & BIT_ULL(48))
3477 return -EINVAL;
3478
3479 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3480 vmx_basic_vmcs_revision_id(data))
3481 return -EINVAL;
3482
3483 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3484 return -EINVAL;
3485
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003486 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003487 return 0;
3488}
3489
3490static int
3491vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3492{
3493 u64 supported;
3494 u32 *lowp, *highp;
3495
3496 switch (msr_index) {
3497 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003498 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3499 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003500 break;
3501 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003502 lowp = &vmx->nested.msrs.procbased_ctls_low;
3503 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003504 break;
3505 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003506 lowp = &vmx->nested.msrs.exit_ctls_low;
3507 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003508 break;
3509 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 lowp = &vmx->nested.msrs.entry_ctls_low;
3511 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003512 break;
3513 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003514 lowp = &vmx->nested.msrs.secondary_ctls_low;
3515 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003516 break;
3517 default:
3518 BUG();
3519 }
3520
3521 supported = vmx_control_msr(*lowp, *highp);
3522
3523 /* Check must-be-1 bits are still 1. */
3524 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3525 return -EINVAL;
3526
3527 /* Check must-be-0 bits are still 0. */
3528 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3529 return -EINVAL;
3530
3531 *lowp = data;
3532 *highp = data >> 32;
3533 return 0;
3534}
3535
3536static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3537{
3538 const u64 feature_and_reserved_bits =
3539 /* feature */
3540 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3541 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3542 /* reserved */
3543 GENMASK_ULL(13, 9) | BIT_ULL(31);
3544 u64 vmx_misc;
3545
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003546 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3547 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003548
3549 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3550 return -EINVAL;
3551
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003552 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003553 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3554 vmx_misc_preemption_timer_rate(data) !=
3555 vmx_misc_preemption_timer_rate(vmx_misc))
3556 return -EINVAL;
3557
3558 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3559 return -EINVAL;
3560
3561 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3562 return -EINVAL;
3563
3564 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3565 return -EINVAL;
3566
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003567 vmx->nested.msrs.misc_low = data;
3568 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003569
3570 /*
3571 * If L1 has read-only VM-exit information fields, use the
3572 * less permissive vmx_vmwrite_bitmap to specify write
3573 * permissions for the shadow VMCS.
3574 */
3575 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3576 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3577
David Matlack62cc6b9d2016-11-29 18:14:07 -08003578 return 0;
3579}
3580
3581static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3582{
3583 u64 vmx_ept_vpid_cap;
3584
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003585 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3586 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003587
3588 /* Every bit is either reserved or a feature bit. */
3589 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3590 return -EINVAL;
3591
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003592 vmx->nested.msrs.ept_caps = data;
3593 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003594 return 0;
3595}
3596
3597static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3598{
3599 u64 *msr;
3600
3601 switch (msr_index) {
3602 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003603 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003604 break;
3605 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003606 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003607 break;
3608 default:
3609 BUG();
3610 }
3611
3612 /*
3613 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3614 * must be 1 in the restored value.
3615 */
3616 if (!is_bitwise_subset(data, *msr, -1ULL))
3617 return -EINVAL;
3618
3619 *msr = data;
3620 return 0;
3621}
3622
3623/*
3624 * Called when userspace is restoring VMX MSRs.
3625 *
3626 * Returns 0 on success, non-0 otherwise.
3627 */
3628static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3629{
3630 struct vcpu_vmx *vmx = to_vmx(vcpu);
3631
Jim Mattsona943ac52018-05-29 09:11:32 -07003632 /*
3633 * Don't allow changes to the VMX capability MSRs while the vCPU
3634 * is in VMX operation.
3635 */
3636 if (vmx->nested.vmxon)
3637 return -EBUSY;
3638
David Matlack62cc6b9d2016-11-29 18:14:07 -08003639 switch (msr_index) {
3640 case MSR_IA32_VMX_BASIC:
3641 return vmx_restore_vmx_basic(vmx, data);
3642 case MSR_IA32_VMX_PINBASED_CTLS:
3643 case MSR_IA32_VMX_PROCBASED_CTLS:
3644 case MSR_IA32_VMX_EXIT_CTLS:
3645 case MSR_IA32_VMX_ENTRY_CTLS:
3646 /*
3647 * The "non-true" VMX capability MSRs are generated from the
3648 * "true" MSRs, so we do not support restoring them directly.
3649 *
3650 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3651 * should restore the "true" MSRs with the must-be-1 bits
3652 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3653 * DEFAULT SETTINGS".
3654 */
3655 return -EINVAL;
3656 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3657 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3658 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3659 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3660 case MSR_IA32_VMX_PROCBASED_CTLS2:
3661 return vmx_restore_control_msr(vmx, msr_index, data);
3662 case MSR_IA32_VMX_MISC:
3663 return vmx_restore_vmx_misc(vmx, data);
3664 case MSR_IA32_VMX_CR0_FIXED0:
3665 case MSR_IA32_VMX_CR4_FIXED0:
3666 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3667 case MSR_IA32_VMX_CR0_FIXED1:
3668 case MSR_IA32_VMX_CR4_FIXED1:
3669 /*
3670 * These MSRs are generated based on the vCPU's CPUID, so we
3671 * do not support restoring them directly.
3672 */
3673 return -EINVAL;
3674 case MSR_IA32_VMX_EPT_VPID_CAP:
3675 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3676 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003677 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003678 return 0;
3679 default:
3680 /*
3681 * The rest of the VMX capability MSRs do not support restore.
3682 */
3683 return -EINVAL;
3684 }
3685}
3686
Jan Kiszkacae50132014-01-04 18:47:22 +01003687/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003688static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003689{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003690 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003691 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003692 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003693 break;
3694 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3695 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003696 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003697 msrs->pinbased_ctls_low,
3698 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003699 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3700 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003701 break;
3702 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3703 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003704 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003705 msrs->procbased_ctls_low,
3706 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003707 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3708 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003709 break;
3710 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3711 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003712 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003713 msrs->exit_ctls_low,
3714 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003715 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3716 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717 break;
3718 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3719 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003720 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003721 msrs->entry_ctls_low,
3722 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003723 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3724 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003725 break;
3726 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003727 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003728 msrs->misc_low,
3729 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003730 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003731 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003732 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003733 break;
3734 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003735 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736 break;
3737 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003738 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003739 break;
3740 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003741 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003742 break;
3743 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003744 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003745 break;
3746 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003747 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003748 msrs->secondary_ctls_low,
3749 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003750 break;
3751 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003752 *pdata = msrs->ept_caps |
3753 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003754 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003755 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003756 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003757 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003758 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003759 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003760 }
3761
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003762 return 0;
3763}
3764
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003765static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3766 uint64_t val)
3767{
3768 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3769
3770 return !(val & ~valid_bits);
3771}
3772
Tom Lendacky801e4592018-02-21 13:39:51 -06003773static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3774{
Paolo Bonzini13893092018-02-26 13:40:09 +01003775 switch (msr->index) {
3776 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3777 if (!nested)
3778 return 1;
3779 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3780 default:
3781 return 1;
3782 }
3783
3784 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003785}
3786
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003787/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788 * Reads an msr value (of 'msr_index') into 'pdata'.
3789 * Returns 0 on success, non-0 otherwise.
3790 * Assumes vcpu_load() was already called.
3791 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003792static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003794 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003795 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003797 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003798#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003800 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003801 break;
3802 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003803 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003805 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003806 vmx_load_host_state(vmx);
3807 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003808 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003809#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003811 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003812 case MSR_IA32_SPEC_CTRL:
3813 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003814 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3815 return 1;
3816
3817 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3818 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003819 case MSR_IA32_ARCH_CAPABILITIES:
3820 if (!msr_info->host_initiated &&
3821 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3822 return 1;
3823 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3824 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003826 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003827 break;
3828 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003829 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003830 break;
3831 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003832 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003833 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003834 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003835 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003836 (!msr_info->host_initiated &&
3837 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003838 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003839 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003840 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003841 case MSR_IA32_MCG_EXT_CTL:
3842 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003843 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003844 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003845 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003846 msr_info->data = vcpu->arch.mcg_ext_ctl;
3847 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003848 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003849 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003850 break;
3851 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3852 if (!nested_vmx_allowed(vcpu))
3853 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003854 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3855 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003856 case MSR_IA32_XSS:
3857 if (!vmx_xsaves_supported())
3858 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003859 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003860 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003861 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003862 if (!msr_info->host_initiated &&
3863 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003864 return 1;
3865 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003867 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003868 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003869 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003872 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873 }
3874
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875 return 0;
3876}
3877
Jan Kiszkacae50132014-01-04 18:47:22 +01003878static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3879
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880/*
3881 * Writes msr value into into the appropriate "register".
3882 * Returns 0 on success, non-0 otherwise.
3883 * Assumes vcpu_load() was already called.
3884 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003885static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003887 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003888 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003889 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003890 u32 msr_index = msr_info->index;
3891 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003892
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003894 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003895 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003896 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003897#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003899 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003900 vmcs_writel(GUEST_FS_BASE, data);
3901 break;
3902 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003903 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003904 vmcs_writel(GUEST_GS_BASE, data);
3905 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003906 case MSR_KERNEL_GS_BASE:
3907 vmx_load_host_state(vmx);
3908 vmx->msr_guest_kernel_gs_base = data;
3909 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910#endif
3911 case MSR_IA32_SYSENTER_CS:
3912 vmcs_write32(GUEST_SYSENTER_CS, data);
3913 break;
3914 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003915 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916 break;
3917 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003918 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003920 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003921 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003922 (!msr_info->host_initiated &&
3923 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003924 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003925 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003926 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003927 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003928 vmcs_write64(GUEST_BNDCFGS, data);
3929 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003930 case MSR_IA32_SPEC_CTRL:
3931 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003932 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3933 return 1;
3934
3935 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003936 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003937 return 1;
3938
3939 vmx->spec_ctrl = data;
3940
3941 if (!data)
3942 break;
3943
3944 /*
3945 * For non-nested:
3946 * When it's written (to non-zero) for the first time, pass
3947 * it through.
3948 *
3949 * For nested:
3950 * The handling of the MSR bitmap for L2 guests is done in
3951 * nested_vmx_merge_msr_bitmap. We should not touch the
3952 * vmcs02.msr_bitmap here since it gets completely overwritten
3953 * in the merging. We update the vmcs01 here for L1 as well
3954 * since it will end up touching the MSR anyway now.
3955 */
3956 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3957 MSR_IA32_SPEC_CTRL,
3958 MSR_TYPE_RW);
3959 break;
Ashok Raj15d45072018-02-01 22:59:43 +01003960 case MSR_IA32_PRED_CMD:
3961 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01003962 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3963 return 1;
3964
3965 if (data & ~PRED_CMD_IBPB)
3966 return 1;
3967
3968 if (!data)
3969 break;
3970
3971 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3972
3973 /*
3974 * For non-nested:
3975 * When it's written (to non-zero) for the first time, pass
3976 * it through.
3977 *
3978 * For nested:
3979 * The handling of the MSR bitmap for L2 guests is done in
3980 * nested_vmx_merge_msr_bitmap. We should not touch the
3981 * vmcs02.msr_bitmap here since it gets completely overwritten
3982 * in the merging.
3983 */
3984 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3985 MSR_TYPE_W);
3986 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003987 case MSR_IA32_ARCH_CAPABILITIES:
3988 if (!msr_info->host_initiated)
3989 return 1;
3990 vmx->arch_capabilities = data;
3991 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003992 case MSR_IA32_CR_PAT:
3993 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003994 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3995 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003996 vmcs_write64(GUEST_IA32_PAT, data);
3997 vcpu->arch.pat = data;
3998 break;
3999 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004000 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004001 break;
Will Auldba904632012-11-29 12:42:50 -08004002 case MSR_IA32_TSC_ADJUST:
4003 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004004 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004005 case MSR_IA32_MCG_EXT_CTL:
4006 if ((!msr_info->host_initiated &&
4007 !(to_vmx(vcpu)->msr_ia32_feature_control &
4008 FEATURE_CONTROL_LMCE)) ||
4009 (data & ~MCG_EXT_CTL_LMCE_EN))
4010 return 1;
4011 vcpu->arch.mcg_ext_ctl = data;
4012 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004013 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004014 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004015 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004016 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4017 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004018 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004019 if (msr_info->host_initiated && data == 0)
4020 vmx_leave_nested(vcpu);
4021 break;
4022 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004023 if (!msr_info->host_initiated)
4024 return 1; /* they are read-only */
4025 if (!nested_vmx_allowed(vcpu))
4026 return 1;
4027 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004028 case MSR_IA32_XSS:
4029 if (!vmx_xsaves_supported())
4030 return 1;
4031 /*
4032 * The only supported bit as of Skylake is bit 8, but
4033 * it is not supported on KVM.
4034 */
4035 if (data != 0)
4036 return 1;
4037 vcpu->arch.ia32_xss = data;
4038 if (vcpu->arch.ia32_xss != host_xss)
4039 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004040 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004041 else
4042 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4043 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004044 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004045 if (!msr_info->host_initiated &&
4046 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004047 return 1;
4048 /* Check reserved bit, higher 32 bits should be zero */
4049 if ((data >> 32) != 0)
4050 return 1;
4051 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004053 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004054 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004055 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004056 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004057 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4058 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004059 ret = kvm_set_shared_msr(msr->index, msr->data,
4060 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004061 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004062 if (ret)
4063 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004064 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004065 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004067 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068 }
4069
Eddie Dong2cc51562007-05-21 07:28:09 +03004070 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071}
4072
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004073static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004075 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4076 switch (reg) {
4077 case VCPU_REGS_RSP:
4078 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4079 break;
4080 case VCPU_REGS_RIP:
4081 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4082 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004083 case VCPU_EXREG_PDPTR:
4084 if (enable_ept)
4085 ept_save_pdptrs(vcpu);
4086 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004087 default:
4088 break;
4089 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004090}
4091
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092static __init int cpu_has_kvm_support(void)
4093{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004094 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095}
4096
4097static __init int vmx_disabled_by_bios(void)
4098{
4099 u64 msr;
4100
4101 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004102 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004103 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004104 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4105 && tboot_enabled())
4106 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004107 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004108 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004109 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004110 && !tboot_enabled()) {
4111 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004112 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004113 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004114 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004115 /* launched w/o TXT and VMX disabled */
4116 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4117 && !tboot_enabled())
4118 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004119 }
4120
4121 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004122}
4123
Dongxiao Xu7725b892010-05-11 18:29:38 +08004124static void kvm_cpu_vmxon(u64 addr)
4125{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004126 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004127 intel_pt_handle_vmx(1);
4128
Dongxiao Xu7725b892010-05-11 18:29:38 +08004129 asm volatile (ASM_VMX_VMXON_RAX
4130 : : "a"(&addr), "m"(addr)
4131 : "memory", "cc");
4132}
4133
Radim Krčmář13a34e02014-08-28 15:13:03 +02004134static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004135{
4136 int cpu = raw_smp_processor_id();
4137 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004138 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004140 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004141 return -EBUSY;
4142
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004143 /*
4144 * This can happen if we hot-added a CPU but failed to allocate
4145 * VP assist page for it.
4146 */
4147 if (static_branch_unlikely(&enable_evmcs) &&
4148 !hv_get_vp_assist_page(cpu))
4149 return -EFAULT;
4150
Nadav Har'Eld462b812011-05-24 15:26:10 +03004151 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004152 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4153 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004154
4155 /*
4156 * Now we can enable the vmclear operation in kdump
4157 * since the loaded_vmcss_on_cpu list on this cpu
4158 * has been initialized.
4159 *
4160 * Though the cpu is not in VMX operation now, there
4161 * is no problem to enable the vmclear operation
4162 * for the loaded_vmcss_on_cpu list is empty!
4163 */
4164 crash_enable_local_vmclear(cpu);
4165
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004167
4168 test_bits = FEATURE_CONTROL_LOCKED;
4169 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4170 if (tboot_enabled())
4171 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4172
4173 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004175 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4176 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004177 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004178 if (enable_ept)
4179 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004180
4181 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182}
4183
Nadav Har'Eld462b812011-05-24 15:26:10 +03004184static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004185{
4186 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004187 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004188
Nadav Har'Eld462b812011-05-24 15:26:10 +03004189 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4190 loaded_vmcss_on_cpu_link)
4191 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004192}
4193
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004194
4195/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4196 * tricks.
4197 */
4198static void kvm_cpu_vmxoff(void)
4199{
4200 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004201
4202 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004203 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004204}
4205
Radim Krčmář13a34e02014-08-28 15:13:03 +02004206static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004208 vmclear_local_loaded_vmcss();
4209 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004210}
4211
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004212static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004213 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004214{
4215 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004216 u32 ctl = ctl_min | ctl_opt;
4217
4218 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4219
4220 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4221 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4222
4223 /* Ensure minimum (required) set of control bits are supported. */
4224 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004225 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004226
4227 *result = ctl;
4228 return 0;
4229}
4230
Avi Kivity110312c2010-12-21 12:54:20 +02004231static __init bool allow_1_setting(u32 msr, u32 ctl)
4232{
4233 u32 vmx_msr_low, vmx_msr_high;
4234
4235 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4236 return vmx_msr_high & ctl;
4237}
4238
Yang, Sheng002c7f72007-07-31 14:23:01 +03004239static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004240{
4241 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004242 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004243 u32 _pin_based_exec_control = 0;
4244 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004245 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004246 u32 _vmexit_control = 0;
4247 u32 _vmentry_control = 0;
4248
Paolo Bonzini13893092018-02-26 13:40:09 +01004249 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304250 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004251#ifdef CONFIG_X86_64
4252 CPU_BASED_CR8_LOAD_EXITING |
4253 CPU_BASED_CR8_STORE_EXITING |
4254#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004255 CPU_BASED_CR3_LOAD_EXITING |
4256 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004257 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004258 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004259 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004260 CPU_BASED_MWAIT_EXITING |
4261 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004262 CPU_BASED_INVLPG_EXITING |
4263 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004264
Sheng Yangf78e0e22007-10-29 09:40:42 +08004265 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004266 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004267 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004268 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4269 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004270 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004271#ifdef CONFIG_X86_64
4272 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4273 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4274 ~CPU_BASED_CR8_STORE_EXITING;
4275#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004276 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004277 min2 = 0;
4278 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004279 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004280 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004281 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004282 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004283 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004284 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004285 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004286 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004287 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004288 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004289 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004290 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004291 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004292 SECONDARY_EXEC_RDSEED_EXITING |
4293 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004294 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004295 SECONDARY_EXEC_TSC_SCALING |
4296 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004297 if (adjust_vmx_controls(min2, opt2,
4298 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004299 &_cpu_based_2nd_exec_control) < 0)
4300 return -EIO;
4301 }
4302#ifndef CONFIG_X86_64
4303 if (!(_cpu_based_2nd_exec_control &
4304 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4305 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4306#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004307
4308 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4309 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004310 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004311 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4312 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004313
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004314 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4315 &vmx_capability.ept, &vmx_capability.vpid);
4316
Sheng Yangd56f5462008-04-25 10:13:16 +08004317 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004318 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4319 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004320 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4321 CPU_BASED_CR3_STORE_EXITING |
4322 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004323 } else if (vmx_capability.ept) {
4324 vmx_capability.ept = 0;
4325 pr_warn_once("EPT CAP should not exist if not support "
4326 "1-setting enable EPT VM-execution control\n");
4327 }
4328 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4329 vmx_capability.vpid) {
4330 vmx_capability.vpid = 0;
4331 pr_warn_once("VPID CAP should not exist if not support "
4332 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004333 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004334
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004335 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004336#ifdef CONFIG_X86_64
4337 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4338#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004339 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004340 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004341 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4342 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004343 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004344
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004345 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4346 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4347 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004348 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4349 &_pin_based_exec_control) < 0)
4350 return -EIO;
4351
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004352 if (cpu_has_broken_vmx_preemption_timer())
4353 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004354 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004355 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004356 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4357
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004358 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004359 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004360 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4361 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004362 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004363
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004364 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004365
4366 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4367 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004368 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004369
4370#ifdef CONFIG_X86_64
4371 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4372 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004373 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004374#endif
4375
4376 /* Require Write-Back (WB) memory type for VMCS accesses. */
4377 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004378 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004379
Yang, Sheng002c7f72007-07-31 14:23:01 +03004380 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004381 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004382 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004383
4384 /* KVM supports Enlightened VMCS v1 only */
4385 if (static_branch_unlikely(&enable_evmcs))
4386 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4387 else
4388 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004389
Yang, Sheng002c7f72007-07-31 14:23:01 +03004390 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4391 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004392 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004393 vmcs_conf->vmexit_ctrl = _vmexit_control;
4394 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004395
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004396 if (static_branch_unlikely(&enable_evmcs))
4397 evmcs_sanitize_exec_ctrls(vmcs_conf);
4398
Avi Kivity110312c2010-12-21 12:54:20 +02004399 cpu_has_load_ia32_efer =
4400 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4401 VM_ENTRY_LOAD_IA32_EFER)
4402 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4403 VM_EXIT_LOAD_IA32_EFER);
4404
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004405 cpu_has_load_perf_global_ctrl =
4406 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4407 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4408 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4409 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4410
4411 /*
4412 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004413 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004414 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4415 *
4416 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4417 *
4418 * AAK155 (model 26)
4419 * AAP115 (model 30)
4420 * AAT100 (model 37)
4421 * BC86,AAY89,BD102 (model 44)
4422 * BA97 (model 46)
4423 *
4424 */
4425 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4426 switch (boot_cpu_data.x86_model) {
4427 case 26:
4428 case 30:
4429 case 37:
4430 case 44:
4431 case 46:
4432 cpu_has_load_perf_global_ctrl = false;
4433 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4434 "does not work properly. Using workaround\n");
4435 break;
4436 default:
4437 break;
4438 }
4439 }
4440
Borislav Petkov782511b2016-04-04 22:25:03 +02004441 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004442 rdmsrl(MSR_IA32_XSS, host_xss);
4443
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004444 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004445}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446
4447static struct vmcs *alloc_vmcs_cpu(int cpu)
4448{
4449 int node = cpu_to_node(cpu);
4450 struct page *pages;
4451 struct vmcs *vmcs;
4452
Vlastimil Babka96db8002015-09-08 15:03:50 -07004453 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004454 if (!pages)
4455 return NULL;
4456 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004457 memset(vmcs, 0, vmcs_config.size);
4458 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004459 return vmcs;
4460}
4461
Avi Kivity6aa8b732006-12-10 02:21:36 -08004462static void free_vmcs(struct vmcs *vmcs)
4463{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004464 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465}
4466
Nadav Har'Eld462b812011-05-24 15:26:10 +03004467/*
4468 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4469 */
4470static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4471{
4472 if (!loaded_vmcs->vmcs)
4473 return;
4474 loaded_vmcs_clear(loaded_vmcs);
4475 free_vmcs(loaded_vmcs->vmcs);
4476 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004477 if (loaded_vmcs->msr_bitmap)
4478 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004479 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004480}
4481
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004482static struct vmcs *alloc_vmcs(void)
4483{
4484 return alloc_vmcs_cpu(raw_smp_processor_id());
4485}
4486
4487static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4488{
4489 loaded_vmcs->vmcs = alloc_vmcs();
4490 if (!loaded_vmcs->vmcs)
4491 return -ENOMEM;
4492
4493 loaded_vmcs->shadow_vmcs = NULL;
4494 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004495
4496 if (cpu_has_vmx_msr_bitmap()) {
4497 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4498 if (!loaded_vmcs->msr_bitmap)
4499 goto out_vmcs;
4500 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004501
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004502 if (IS_ENABLED(CONFIG_HYPERV) &&
4503 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004504 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4505 struct hv_enlightened_vmcs *evmcs =
4506 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4507
4508 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4509 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004510 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004511 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004512
4513out_vmcs:
4514 free_loaded_vmcs(loaded_vmcs);
4515 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004516}
4517
Sam Ravnborg39959582007-06-01 00:47:13 -07004518static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004519{
4520 int cpu;
4521
Zachary Amsden3230bb42009-09-29 11:38:37 -10004522 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004524 per_cpu(vmxarea, cpu) = NULL;
4525 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004526}
4527
Jim Mattsond37f4262017-12-22 12:12:16 -08004528enum vmcs_field_width {
4529 VMCS_FIELD_WIDTH_U16 = 0,
4530 VMCS_FIELD_WIDTH_U64 = 1,
4531 VMCS_FIELD_WIDTH_U32 = 2,
4532 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004533};
4534
Jim Mattsond37f4262017-12-22 12:12:16 -08004535static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004536{
4537 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004538 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004539 return (field >> 13) & 0x3 ;
4540}
4541
4542static inline int vmcs_field_readonly(unsigned long field)
4543{
4544 return (((field >> 10) & 0x3) == 1);
4545}
4546
Bandan Dasfe2b2012014-04-21 15:20:14 -04004547static void init_vmcs_shadow_fields(void)
4548{
4549 int i, j;
4550
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004551 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4552 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004553 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004554 (i + 1 == max_shadow_read_only_fields ||
4555 shadow_read_only_fields[i + 1] != field + 1))
4556 pr_err("Missing field from shadow_read_only_field %x\n",
4557 field + 1);
4558
4559 clear_bit(field, vmx_vmread_bitmap);
4560#ifdef CONFIG_X86_64
4561 if (field & 1)
4562 continue;
4563#endif
4564 if (j < i)
4565 shadow_read_only_fields[j] = field;
4566 j++;
4567 }
4568 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004569
4570 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004571 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004572 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004573 (i + 1 == max_shadow_read_write_fields ||
4574 shadow_read_write_fields[i + 1] != field + 1))
4575 pr_err("Missing field from shadow_read_write_field %x\n",
4576 field + 1);
4577
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004578 /*
4579 * PML and the preemption timer can be emulated, but the
4580 * processor cannot vmwrite to fields that don't exist
4581 * on bare metal.
4582 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004583 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004584 case GUEST_PML_INDEX:
4585 if (!cpu_has_vmx_pml())
4586 continue;
4587 break;
4588 case VMX_PREEMPTION_TIMER_VALUE:
4589 if (!cpu_has_vmx_preemption_timer())
4590 continue;
4591 break;
4592 case GUEST_INTR_STATUS:
4593 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004594 continue;
4595 break;
4596 default:
4597 break;
4598 }
4599
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004600 clear_bit(field, vmx_vmwrite_bitmap);
4601 clear_bit(field, vmx_vmread_bitmap);
4602#ifdef CONFIG_X86_64
4603 if (field & 1)
4604 continue;
4605#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004606 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004607 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004608 j++;
4609 }
4610 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004611}
4612
Avi Kivity6aa8b732006-12-10 02:21:36 -08004613static __init int alloc_kvm_area(void)
4614{
4615 int cpu;
4616
Zachary Amsden3230bb42009-09-29 11:38:37 -10004617 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004618 struct vmcs *vmcs;
4619
4620 vmcs = alloc_vmcs_cpu(cpu);
4621 if (!vmcs) {
4622 free_kvm_area();
4623 return -ENOMEM;
4624 }
4625
4626 per_cpu(vmxarea, cpu) = vmcs;
4627 }
4628 return 0;
4629}
4630
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004631static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004632 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004634 if (!emulate_invalid_guest_state) {
4635 /*
4636 * CS and SS RPL should be equal during guest entry according
4637 * to VMX spec, but in reality it is not always so. Since vcpu
4638 * is in the middle of the transition from real mode to
4639 * protected mode it is safe to assume that RPL 0 is a good
4640 * default value.
4641 */
4642 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004643 save->selector &= ~SEGMENT_RPL_MASK;
4644 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004645 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004646 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004647 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004648}
4649
4650static void enter_pmode(struct kvm_vcpu *vcpu)
4651{
4652 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004653 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004654
Gleb Natapovd99e4152012-12-20 16:57:45 +02004655 /*
4656 * Update real mode segment cache. It may be not up-to-date if sement
4657 * register was written while vcpu was in a guest mode.
4658 */
4659 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4660 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4661 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4662 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4663 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4664 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4665
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004666 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004667
Avi Kivity2fb92db2011-04-27 19:42:18 +03004668 vmx_segment_cache_clear(vmx);
4669
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004670 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004671
4672 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004673 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4674 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004675 vmcs_writel(GUEST_RFLAGS, flags);
4676
Rusty Russell66aee912007-07-17 23:34:16 +10004677 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4678 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004679
4680 update_exception_bitmap(vcpu);
4681
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004682 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4683 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4684 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4685 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4686 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4687 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004688}
4689
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004690static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004691{
Mathias Krause772e0312012-08-30 01:30:19 +02004692 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004693 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694
Gleb Natapovd99e4152012-12-20 16:57:45 +02004695 var.dpl = 0x3;
4696 if (seg == VCPU_SREG_CS)
4697 var.type = 0x3;
4698
4699 if (!emulate_invalid_guest_state) {
4700 var.selector = var.base >> 4;
4701 var.base = var.base & 0xffff0;
4702 var.limit = 0xffff;
4703 var.g = 0;
4704 var.db = 0;
4705 var.present = 1;
4706 var.s = 1;
4707 var.l = 0;
4708 var.unusable = 0;
4709 var.type = 0x3;
4710 var.avl = 0;
4711 if (save->base & 0xf)
4712 printk_once(KERN_WARNING "kvm: segment base is not "
4713 "paragraph aligned when entering "
4714 "protected mode (seg=%d)", seg);
4715 }
4716
4717 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004718 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004719 vmcs_write32(sf->limit, var.limit);
4720 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721}
4722
4723static void enter_rmode(struct kvm_vcpu *vcpu)
4724{
4725 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004726 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004727 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004729 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4730 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4731 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4732 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4733 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004734 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4735 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004736
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004737 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004738
Gleb Natapov776e58e2011-03-13 12:34:27 +02004739 /*
4740 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004741 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004742 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004743 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004744 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4745 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004746
Avi Kivity2fb92db2011-04-27 19:42:18 +03004747 vmx_segment_cache_clear(vmx);
4748
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004749 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4752
4753 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004754 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004755
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004756 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757
4758 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004759 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004760 update_exception_bitmap(vcpu);
4761
Gleb Natapovd99e4152012-12-20 16:57:45 +02004762 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4763 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4764 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4765 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4766 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4767 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004768
Eddie Dong8668a3c2007-10-10 14:26:45 +08004769 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770}
4771
Amit Shah401d10d2009-02-20 22:53:37 +05304772static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4773{
4774 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004775 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4776
4777 if (!msr)
4778 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304779
Avi Kivity44ea2b12009-09-06 15:55:37 +03004780 /*
4781 * Force kernel_gs_base reloading before EFER changes, as control
4782 * of this msr depends on is_long_mode().
4783 */
4784 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004785 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304786 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004787 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304788 msr->data = efer;
4789 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004790 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304791
4792 msr->data = efer & ~EFER_LME;
4793 }
4794 setup_msrs(vmx);
4795}
4796
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004797#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798
4799static void enter_lmode(struct kvm_vcpu *vcpu)
4800{
4801 u32 guest_tr_ar;
4802
Avi Kivity2fb92db2011-04-27 19:42:18 +03004803 vmx_segment_cache_clear(to_vmx(vcpu));
4804
Avi Kivity6aa8b732006-12-10 02:21:36 -08004805 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004806 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004807 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4808 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004810 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4811 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812 }
Avi Kivityda38f432010-07-06 11:30:49 +03004813 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004814}
4815
4816static void exit_lmode(struct kvm_vcpu *vcpu)
4817{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004818 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004819 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820}
4821
4822#endif
4823
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004824static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4825 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004826{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004827 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004828 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4829 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004830 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004831 } else {
4832 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004833 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004834}
4835
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004836static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004837{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004838 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004839}
4840
Avi Kivitye8467fd2009-12-29 18:43:06 +02004841static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4842{
4843 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4844
4845 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4846 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4847}
4848
Avi Kivityaff48ba2010-12-05 18:56:11 +02004849static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4850{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004851 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004852 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4853 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4854}
4855
Anthony Liguori25c4c272007-04-27 09:29:21 +03004856static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004857{
Avi Kivityfc78f512009-12-07 12:16:48 +02004858 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4859
4860 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4861 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004862}
4863
Sheng Yang14394422008-04-28 12:24:45 +08004864static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4865{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004866 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4867
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004868 if (!test_bit(VCPU_EXREG_PDPTR,
4869 (unsigned long *)&vcpu->arch.regs_dirty))
4870 return;
4871
Sheng Yang14394422008-04-28 12:24:45 +08004872 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004873 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4874 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4875 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4876 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004877 }
4878}
4879
Avi Kivity8f5d5492009-05-31 18:41:29 +03004880static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4881{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004882 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4883
Avi Kivity8f5d5492009-05-31 18:41:29 +03004884 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004885 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4886 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4887 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4888 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004889 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004890
4891 __set_bit(VCPU_EXREG_PDPTR,
4892 (unsigned long *)&vcpu->arch.regs_avail);
4893 __set_bit(VCPU_EXREG_PDPTR,
4894 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004895}
4896
David Matlack38991522016-11-29 18:14:08 -08004897static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4898{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004899 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4900 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004901 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4902
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004903 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004904 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4905 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4906 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4907
4908 return fixed_bits_valid(val, fixed0, fixed1);
4909}
4910
4911static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4912{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004913 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4914 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004915
4916 return fixed_bits_valid(val, fixed0, fixed1);
4917}
4918
4919static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4920{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004921 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4922 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004923
4924 return fixed_bits_valid(val, fixed0, fixed1);
4925}
4926
4927/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4928#define nested_guest_cr4_valid nested_cr4_valid
4929#define nested_host_cr4_valid nested_cr4_valid
4930
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004931static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004932
4933static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4934 unsigned long cr0,
4935 struct kvm_vcpu *vcpu)
4936{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004937 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4938 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004939 if (!(cr0 & X86_CR0_PG)) {
4940 /* From paging/starting to nonpaging */
4941 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004942 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004943 (CPU_BASED_CR3_LOAD_EXITING |
4944 CPU_BASED_CR3_STORE_EXITING));
4945 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004946 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004947 } else if (!is_paging(vcpu)) {
4948 /* From nonpaging to paging */
4949 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004950 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004951 ~(CPU_BASED_CR3_LOAD_EXITING |
4952 CPU_BASED_CR3_STORE_EXITING));
4953 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004954 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004955 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004956
4957 if (!(cr0 & X86_CR0_WP))
4958 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004959}
4960
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4962{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004963 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004964 unsigned long hw_cr0;
4965
Gleb Natapov50378782013-02-04 16:00:28 +02004966 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004967 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004968 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004969 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004970 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004971
Gleb Natapov218e7632013-01-21 15:36:45 +02004972 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4973 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974
Gleb Natapov218e7632013-01-21 15:36:45 +02004975 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4976 enter_rmode(vcpu);
4977 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004979#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004980 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004981 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004983 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984 exit_lmode(vcpu);
4985 }
4986#endif
4987
Sean Christophersonb4d18512018-03-05 12:04:40 -08004988 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08004989 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4990
Avi Kivity6aa8b732006-12-10 02:21:36 -08004991 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004992 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004993 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004994
4995 /* depends on vcpu->arch.cr0 to be set to a new value */
4996 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004997}
4998
Yu Zhang855feb62017-08-24 20:27:55 +08004999static int get_ept_level(struct kvm_vcpu *vcpu)
5000{
5001 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5002 return 5;
5003 return 4;
5004}
5005
Peter Feiner995f00a2017-06-30 17:26:32 -07005006static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005007{
Yu Zhang855feb62017-08-24 20:27:55 +08005008 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005009
Yu Zhang855feb62017-08-24 20:27:55 +08005010 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005011
Peter Feiner995f00a2017-06-30 17:26:32 -07005012 if (enable_ept_ad_bits &&
5013 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005014 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005015 eptp |= (root_hpa & PAGE_MASK);
5016
5017 return eptp;
5018}
5019
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5021{
Sheng Yang14394422008-04-28 12:24:45 +08005022 unsigned long guest_cr3;
5023 u64 eptp;
5024
5025 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005026 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005027 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005028 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005029 if (enable_unrestricted_guest || is_paging(vcpu) ||
5030 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005031 guest_cr3 = kvm_read_cr3(vcpu);
5032 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005033 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005034 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005035 }
5036
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005037 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005038 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005039}
5040
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005041static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005043 /*
5044 * Pass through host's Machine Check Enable value to hw_cr4, which
5045 * is in force while we are in guest mode. Do not let guests control
5046 * this bit, even if host CR4.MCE == 0.
5047 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005048 unsigned long hw_cr4;
5049
5050 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5051 if (enable_unrestricted_guest)
5052 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5053 else if (to_vmx(vcpu)->rmode.vm86_active)
5054 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5055 else
5056 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005057
Sean Christopherson64f7a112018-04-30 10:01:06 -07005058 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5059 if (cr4 & X86_CR4_UMIP) {
5060 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005061 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005062 hw_cr4 &= ~X86_CR4_UMIP;
5063 } else if (!is_guest_mode(vcpu) ||
5064 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5065 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5066 SECONDARY_EXEC_DESC);
5067 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005068
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005069 if (cr4 & X86_CR4_VMXE) {
5070 /*
5071 * To use VMXON (and later other VMX instructions), a guest
5072 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5073 * So basically the check on whether to allow nested VMX
5074 * is here.
5075 */
5076 if (!nested_vmx_allowed(vcpu))
5077 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005078 }
David Matlack38991522016-11-29 18:14:08 -08005079
5080 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005081 return 1;
5082
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005083 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005084
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005085 if (!enable_unrestricted_guest) {
5086 if (enable_ept) {
5087 if (!is_paging(vcpu)) {
5088 hw_cr4 &= ~X86_CR4_PAE;
5089 hw_cr4 |= X86_CR4_PSE;
5090 } else if (!(cr4 & X86_CR4_PAE)) {
5091 hw_cr4 &= ~X86_CR4_PAE;
5092 }
5093 }
5094
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005095 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005096 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5097 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5098 * to be manually disabled when guest switches to non-paging
5099 * mode.
5100 *
5101 * If !enable_unrestricted_guest, the CPU is always running
5102 * with CR0.PG=1 and CR4 needs to be modified.
5103 * If enable_unrestricted_guest, the CPU automatically
5104 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005105 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005106 if (!is_paging(vcpu))
5107 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5108 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005109
Sheng Yang14394422008-04-28 12:24:45 +08005110 vmcs_writel(CR4_READ_SHADOW, cr4);
5111 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005112 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005113}
5114
Avi Kivity6aa8b732006-12-10 02:21:36 -08005115static void vmx_get_segment(struct kvm_vcpu *vcpu,
5116 struct kvm_segment *var, int seg)
5117{
Avi Kivitya9179492011-01-03 14:28:52 +02005118 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005119 u32 ar;
5120
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005121 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005122 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005123 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005124 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005125 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005126 var->base = vmx_read_guest_seg_base(vmx, seg);
5127 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5128 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005129 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005130 var->base = vmx_read_guest_seg_base(vmx, seg);
5131 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5132 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5133 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005134 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005135 var->type = ar & 15;
5136 var->s = (ar >> 4) & 1;
5137 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005138 /*
5139 * Some userspaces do not preserve unusable property. Since usable
5140 * segment has to be present according to VMX spec we can use present
5141 * property to amend userspace bug by making unusable segment always
5142 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5143 * segment as unusable.
5144 */
5145 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005146 var->avl = (ar >> 12) & 1;
5147 var->l = (ar >> 13) & 1;
5148 var->db = (ar >> 14) & 1;
5149 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005150}
5151
Avi Kivitya9179492011-01-03 14:28:52 +02005152static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5153{
Avi Kivitya9179492011-01-03 14:28:52 +02005154 struct kvm_segment s;
5155
5156 if (to_vmx(vcpu)->rmode.vm86_active) {
5157 vmx_get_segment(vcpu, &s, seg);
5158 return s.base;
5159 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005160 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005161}
5162
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005163static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005164{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005165 struct vcpu_vmx *vmx = to_vmx(vcpu);
5166
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005167 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005168 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005169 else {
5170 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005171 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005172 }
Avi Kivity69c73022011-03-07 15:26:44 +02005173}
5174
Avi Kivity653e3102007-05-07 10:55:37 +03005175static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005176{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177 u32 ar;
5178
Avi Kivityf0495f92012-06-07 17:06:10 +03005179 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005180 ar = 1 << 16;
5181 else {
5182 ar = var->type & 15;
5183 ar |= (var->s & 1) << 4;
5184 ar |= (var->dpl & 3) << 5;
5185 ar |= (var->present & 1) << 7;
5186 ar |= (var->avl & 1) << 12;
5187 ar |= (var->l & 1) << 13;
5188 ar |= (var->db & 1) << 14;
5189 ar |= (var->g & 1) << 15;
5190 }
Avi Kivity653e3102007-05-07 10:55:37 +03005191
5192 return ar;
5193}
5194
5195static void vmx_set_segment(struct kvm_vcpu *vcpu,
5196 struct kvm_segment *var, int seg)
5197{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005198 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005199 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005200
Avi Kivity2fb92db2011-04-27 19:42:18 +03005201 vmx_segment_cache_clear(vmx);
5202
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005203 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5204 vmx->rmode.segs[seg] = *var;
5205 if (seg == VCPU_SREG_TR)
5206 vmcs_write16(sf->selector, var->selector);
5207 else if (var->s)
5208 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005209 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005210 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005211
Avi Kivity653e3102007-05-07 10:55:37 +03005212 vmcs_writel(sf->base, var->base);
5213 vmcs_write32(sf->limit, var->limit);
5214 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005215
5216 /*
5217 * Fix the "Accessed" bit in AR field of segment registers for older
5218 * qemu binaries.
5219 * IA32 arch specifies that at the time of processor reset the
5220 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005221 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005222 * state vmexit when "unrestricted guest" mode is turned on.
5223 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5224 * tree. Newer qemu binaries with that qemu fix would not need this
5225 * kvm hack.
5226 */
5227 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005228 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005229
Gleb Natapovf924d662012-12-12 19:10:55 +02005230 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005231
5232out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005233 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005234}
5235
Avi Kivity6aa8b732006-12-10 02:21:36 -08005236static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5237{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005238 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005239
5240 *db = (ar >> 14) & 1;
5241 *l = (ar >> 13) & 1;
5242}
5243
Gleb Natapov89a27f42010-02-16 10:51:48 +02005244static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005245{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005246 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5247 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005248}
5249
Gleb Natapov89a27f42010-02-16 10:51:48 +02005250static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005251{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005252 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5253 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005254}
5255
Gleb Natapov89a27f42010-02-16 10:51:48 +02005256static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005257{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005258 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5259 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260}
5261
Gleb Natapov89a27f42010-02-16 10:51:48 +02005262static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005264 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5265 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266}
5267
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005268static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5269{
5270 struct kvm_segment var;
5271 u32 ar;
5272
5273 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005274 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005275 if (seg == VCPU_SREG_CS)
5276 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005277 ar = vmx_segment_access_rights(&var);
5278
5279 if (var.base != (var.selector << 4))
5280 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005281 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005282 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005283 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005284 return false;
5285
5286 return true;
5287}
5288
5289static bool code_segment_valid(struct kvm_vcpu *vcpu)
5290{
5291 struct kvm_segment cs;
5292 unsigned int cs_rpl;
5293
5294 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005295 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005296
Avi Kivity1872a3f2009-01-04 23:26:52 +02005297 if (cs.unusable)
5298 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005299 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005300 return false;
5301 if (!cs.s)
5302 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005303 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005304 if (cs.dpl > cs_rpl)
5305 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005306 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005307 if (cs.dpl != cs_rpl)
5308 return false;
5309 }
5310 if (!cs.present)
5311 return false;
5312
5313 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5314 return true;
5315}
5316
5317static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5318{
5319 struct kvm_segment ss;
5320 unsigned int ss_rpl;
5321
5322 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005323 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005324
Avi Kivity1872a3f2009-01-04 23:26:52 +02005325 if (ss.unusable)
5326 return true;
5327 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005328 return false;
5329 if (!ss.s)
5330 return false;
5331 if (ss.dpl != ss_rpl) /* DPL != RPL */
5332 return false;
5333 if (!ss.present)
5334 return false;
5335
5336 return true;
5337}
5338
5339static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5340{
5341 struct kvm_segment var;
5342 unsigned int rpl;
5343
5344 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005345 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005346
Avi Kivity1872a3f2009-01-04 23:26:52 +02005347 if (var.unusable)
5348 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005349 if (!var.s)
5350 return false;
5351 if (!var.present)
5352 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005353 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005354 if (var.dpl < rpl) /* DPL < RPL */
5355 return false;
5356 }
5357
5358 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5359 * rights flags
5360 */
5361 return true;
5362}
5363
5364static bool tr_valid(struct kvm_vcpu *vcpu)
5365{
5366 struct kvm_segment tr;
5367
5368 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5369
Avi Kivity1872a3f2009-01-04 23:26:52 +02005370 if (tr.unusable)
5371 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005372 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005373 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005374 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005375 return false;
5376 if (!tr.present)
5377 return false;
5378
5379 return true;
5380}
5381
5382static bool ldtr_valid(struct kvm_vcpu *vcpu)
5383{
5384 struct kvm_segment ldtr;
5385
5386 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5387
Avi Kivity1872a3f2009-01-04 23:26:52 +02005388 if (ldtr.unusable)
5389 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005390 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005391 return false;
5392 if (ldtr.type != 2)
5393 return false;
5394 if (!ldtr.present)
5395 return false;
5396
5397 return true;
5398}
5399
5400static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5401{
5402 struct kvm_segment cs, ss;
5403
5404 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5405 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5406
Nadav Amitb32a9912015-03-29 16:33:04 +03005407 return ((cs.selector & SEGMENT_RPL_MASK) ==
5408 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005409}
5410
5411/*
5412 * Check if guest state is valid. Returns true if valid, false if
5413 * not.
5414 * We assume that registers are always usable
5415 */
5416static bool guest_state_valid(struct kvm_vcpu *vcpu)
5417{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005418 if (enable_unrestricted_guest)
5419 return true;
5420
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005421 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005422 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005423 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5424 return false;
5425 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5426 return false;
5427 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5428 return false;
5429 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5430 return false;
5431 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5432 return false;
5433 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5434 return false;
5435 } else {
5436 /* protected mode guest state checks */
5437 if (!cs_ss_rpl_check(vcpu))
5438 return false;
5439 if (!code_segment_valid(vcpu))
5440 return false;
5441 if (!stack_segment_valid(vcpu))
5442 return false;
5443 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5444 return false;
5445 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5446 return false;
5447 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5448 return false;
5449 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5450 return false;
5451 if (!tr_valid(vcpu))
5452 return false;
5453 if (!ldtr_valid(vcpu))
5454 return false;
5455 }
5456 /* TODO:
5457 * - Add checks on RIP
5458 * - Add checks on RFLAGS
5459 */
5460
5461 return true;
5462}
5463
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005464static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5465{
5466 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5467}
5468
Mike Dayd77c26f2007-10-08 09:02:08 -04005469static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005470{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005471 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005472 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005473 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005474
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005475 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005476 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005477 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5478 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005479 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005480 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005481 r = kvm_write_guest_page(kvm, fn++, &data,
5482 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005483 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005484 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005485 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5486 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005487 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005488 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5489 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005490 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005491 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005492 r = kvm_write_guest_page(kvm, fn, &data,
5493 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5494 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005495out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005496 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005497 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005498}
5499
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005500static int init_rmode_identity_map(struct kvm *kvm)
5501{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005502 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005503 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005504 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005505 u32 tmp;
5506
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005507 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005508 mutex_lock(&kvm->slots_lock);
5509
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005510 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005511 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005512
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005513 if (!kvm_vmx->ept_identity_map_addr)
5514 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5515 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005516
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005517 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005518 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005519 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005520 goto out2;
5521
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005522 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005523 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5524 if (r < 0)
5525 goto out;
5526 /* Set up identity-mapping pagetable for EPT in real mode */
5527 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5528 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5529 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5530 r = kvm_write_guest_page(kvm, identity_map_pfn,
5531 &tmp, i * sizeof(tmp), sizeof(tmp));
5532 if (r < 0)
5533 goto out;
5534 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005535 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005536
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005537out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005538 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005539
5540out2:
5541 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005542 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005543}
5544
Avi Kivity6aa8b732006-12-10 02:21:36 -08005545static void seg_setup(int seg)
5546{
Mathias Krause772e0312012-08-30 01:30:19 +02005547 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005548 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005549
5550 vmcs_write16(sf->selector, 0);
5551 vmcs_writel(sf->base, 0);
5552 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005553 ar = 0x93;
5554 if (seg == VCPU_SREG_CS)
5555 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005556
5557 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558}
5559
Sheng Yangf78e0e22007-10-29 09:40:42 +08005560static int alloc_apic_access_page(struct kvm *kvm)
5561{
Xiao Guangrong44841412012-09-07 14:14:20 +08005562 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005563 int r = 0;
5564
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005565 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005566 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005567 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005568 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5569 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005570 if (r)
5571 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005572
Tang Chen73a6d942014-09-11 13:38:00 +08005573 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005574 if (is_error_page(page)) {
5575 r = -EFAULT;
5576 goto out;
5577 }
5578
Tang Chenc24ae0d2014-09-24 15:57:58 +08005579 /*
5580 * Do not pin the page in memory, so that memory hot-unplug
5581 * is able to migrate it.
5582 */
5583 put_page(page);
5584 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005585out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005586 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005587 return r;
5588}
5589
Wanpeng Li991e7a02015-09-16 17:30:05 +08005590static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005591{
5592 int vpid;
5593
Avi Kivity919818a2009-03-23 18:01:29 +02005594 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005595 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005596 spin_lock(&vmx_vpid_lock);
5597 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005598 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005599 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005600 else
5601 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005602 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005603 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005604}
5605
Wanpeng Li991e7a02015-09-16 17:30:05 +08005606static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005607{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005608 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005609 return;
5610 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005611 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005612 spin_unlock(&vmx_vpid_lock);
5613}
5614
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005615static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5616 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005617{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005618 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005619
5620 if (!cpu_has_vmx_msr_bitmap())
5621 return;
5622
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005623 if (static_branch_unlikely(&enable_evmcs))
5624 evmcs_touch_msr_bitmap();
5625
Sheng Yang25c5f222008-03-28 13:18:56 +08005626 /*
5627 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5628 * have the write-low and read-high bitmap offsets the wrong way round.
5629 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5630 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005631 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005632 if (type & MSR_TYPE_R)
5633 /* read-low */
5634 __clear_bit(msr, msr_bitmap + 0x000 / f);
5635
5636 if (type & MSR_TYPE_W)
5637 /* write-low */
5638 __clear_bit(msr, msr_bitmap + 0x800 / f);
5639
Sheng Yang25c5f222008-03-28 13:18:56 +08005640 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5641 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005642 if (type & MSR_TYPE_R)
5643 /* read-high */
5644 __clear_bit(msr, msr_bitmap + 0x400 / f);
5645
5646 if (type & MSR_TYPE_W)
5647 /* write-high */
5648 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5649
5650 }
5651}
5652
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005653static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5654 u32 msr, int type)
5655{
5656 int f = sizeof(unsigned long);
5657
5658 if (!cpu_has_vmx_msr_bitmap())
5659 return;
5660
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005661 if (static_branch_unlikely(&enable_evmcs))
5662 evmcs_touch_msr_bitmap();
5663
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005664 /*
5665 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5666 * have the write-low and read-high bitmap offsets the wrong way round.
5667 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5668 */
5669 if (msr <= 0x1fff) {
5670 if (type & MSR_TYPE_R)
5671 /* read-low */
5672 __set_bit(msr, msr_bitmap + 0x000 / f);
5673
5674 if (type & MSR_TYPE_W)
5675 /* write-low */
5676 __set_bit(msr, msr_bitmap + 0x800 / f);
5677
5678 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5679 msr &= 0x1fff;
5680 if (type & MSR_TYPE_R)
5681 /* read-high */
5682 __set_bit(msr, msr_bitmap + 0x400 / f);
5683
5684 if (type & MSR_TYPE_W)
5685 /* write-high */
5686 __set_bit(msr, msr_bitmap + 0xc00 / f);
5687
5688 }
5689}
5690
5691static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5692 u32 msr, int type, bool value)
5693{
5694 if (value)
5695 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5696 else
5697 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5698}
5699
Wincy Vanf2b93282015-02-03 23:56:03 +08005700/*
5701 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5702 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5703 */
5704static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5705 unsigned long *msr_bitmap_nested,
5706 u32 msr, int type)
5707{
5708 int f = sizeof(unsigned long);
5709
Wincy Vanf2b93282015-02-03 23:56:03 +08005710 /*
5711 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5712 * have the write-low and read-high bitmap offsets the wrong way round.
5713 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5714 */
5715 if (msr <= 0x1fff) {
5716 if (type & MSR_TYPE_R &&
5717 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5718 /* read-low */
5719 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5720
5721 if (type & MSR_TYPE_W &&
5722 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5723 /* write-low */
5724 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5725
5726 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5727 msr &= 0x1fff;
5728 if (type & MSR_TYPE_R &&
5729 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5730 /* read-high */
5731 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5732
5733 if (type & MSR_TYPE_W &&
5734 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5735 /* write-high */
5736 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5737
5738 }
5739}
5740
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005741static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005742{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005743 u8 mode = 0;
5744
5745 if (cpu_has_secondary_exec_ctrls() &&
5746 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5747 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5748 mode |= MSR_BITMAP_MODE_X2APIC;
5749 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5750 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5751 }
5752
5753 if (is_long_mode(vcpu))
5754 mode |= MSR_BITMAP_MODE_LM;
5755
5756 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005757}
5758
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005759#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5760
5761static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5762 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005763{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005764 int msr;
5765
5766 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5767 unsigned word = msr / BITS_PER_LONG;
5768 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5769 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005770 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005771
5772 if (mode & MSR_BITMAP_MODE_X2APIC) {
5773 /*
5774 * TPR reads and writes can be virtualized even if virtual interrupt
5775 * delivery is not in use.
5776 */
5777 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5778 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5779 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5780 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5781 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5782 }
5783 }
5784}
5785
5786static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5787{
5788 struct vcpu_vmx *vmx = to_vmx(vcpu);
5789 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5790 u8 mode = vmx_msr_bitmap_mode(vcpu);
5791 u8 changed = mode ^ vmx->msr_bitmap_mode;
5792
5793 if (!changed)
5794 return;
5795
5796 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5797 !(mode & MSR_BITMAP_MODE_LM));
5798
5799 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5800 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5801
5802 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005803}
5804
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005805static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005806{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005807 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005808}
5809
David Matlackc9f04402017-08-01 14:00:40 -07005810static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5811{
5812 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5813 gfn_t gfn;
5814
5815 /*
5816 * Don't need to mark the APIC access page dirty; it is never
5817 * written to by the CPU during APIC virtualization.
5818 */
5819
5820 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5821 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5822 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5823 }
5824
5825 if (nested_cpu_has_posted_intr(vmcs12)) {
5826 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5827 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5828 }
5829}
5830
5831
David Hildenbrand6342c502017-01-25 11:58:58 +01005832static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005833{
5834 struct vcpu_vmx *vmx = to_vmx(vcpu);
5835 int max_irr;
5836 void *vapic_page;
5837 u16 status;
5838
David Matlackc9f04402017-08-01 14:00:40 -07005839 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5840 return;
Wincy Van705699a2015-02-03 23:58:17 +08005841
David Matlackc9f04402017-08-01 14:00:40 -07005842 vmx->nested.pi_pending = false;
5843 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5844 return;
Wincy Van705699a2015-02-03 23:58:17 +08005845
David Matlackc9f04402017-08-01 14:00:40 -07005846 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5847 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005848 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005849 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5850 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005851 kunmap(vmx->nested.virtual_apic_page);
5852
5853 status = vmcs_read16(GUEST_INTR_STATUS);
5854 if ((u8)max_irr > ((u8)status & 0xff)) {
5855 status &= ~0xff;
5856 status |= (u8)max_irr;
5857 vmcs_write16(GUEST_INTR_STATUS, status);
5858 }
5859 }
David Matlackc9f04402017-08-01 14:00:40 -07005860
5861 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005862}
5863
Wincy Van06a55242017-04-28 13:13:59 +08005864static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5865 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005866{
5867#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005868 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5869
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005870 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005871 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005872 * The vector of interrupt to be delivered to vcpu had
5873 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005874 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005875 * Following cases will be reached in this block, and
5876 * we always send a notification event in all cases as
5877 * explained below.
5878 *
5879 * Case 1: vcpu keeps in non-root mode. Sending a
5880 * notification event posts the interrupt to vcpu.
5881 *
5882 * Case 2: vcpu exits to root mode and is still
5883 * runnable. PIR will be synced to vIRR before the
5884 * next vcpu entry. Sending a notification event in
5885 * this case has no effect, as vcpu is not in root
5886 * mode.
5887 *
5888 * Case 3: vcpu exits to root mode and is blocked.
5889 * vcpu_block() has already synced PIR to vIRR and
5890 * never blocks vcpu if vIRR is not cleared. Therefore,
5891 * a blocked vcpu here does not wait for any requested
5892 * interrupts in PIR, and sending a notification event
5893 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005894 */
Feng Wu28b835d2015-09-18 22:29:54 +08005895
Wincy Van06a55242017-04-28 13:13:59 +08005896 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005897 return true;
5898 }
5899#endif
5900 return false;
5901}
5902
Wincy Van705699a2015-02-03 23:58:17 +08005903static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5904 int vector)
5905{
5906 struct vcpu_vmx *vmx = to_vmx(vcpu);
5907
5908 if (is_guest_mode(vcpu) &&
5909 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005910 /*
5911 * If a posted intr is not recognized by hardware,
5912 * we will accomplish it in the next vmentry.
5913 */
5914 vmx->nested.pi_pending = true;
5915 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005916 /* the PIR and ON have been set by L1. */
5917 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5918 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005919 return 0;
5920 }
5921 return -1;
5922}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005923/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005924 * Send interrupt to vcpu via posted interrupt way.
5925 * 1. If target vcpu is running(non-root mode), send posted interrupt
5926 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5927 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5928 * interrupt from PIR in next vmentry.
5929 */
5930static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5931{
5932 struct vcpu_vmx *vmx = to_vmx(vcpu);
5933 int r;
5934
Wincy Van705699a2015-02-03 23:58:17 +08005935 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5936 if (!r)
5937 return;
5938
Yang Zhanga20ed542013-04-11 19:25:15 +08005939 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5940 return;
5941
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005942 /* If a previous notification has sent the IPI, nothing to do. */
5943 if (pi_test_and_set_on(&vmx->pi_desc))
5944 return;
5945
Wincy Van06a55242017-04-28 13:13:59 +08005946 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08005947 kvm_vcpu_kick(vcpu);
5948}
5949
Avi Kivity6aa8b732006-12-10 02:21:36 -08005950/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005951 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5952 * will not change in the lifetime of the guest.
5953 * Note that host-state that does change is set elsewhere. E.g., host-state
5954 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5955 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005956static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005957{
5958 u32 low32, high32;
5959 unsigned long tmpl;
5960 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005961 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005962
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005963 cr0 = read_cr0();
5964 WARN_ON(cr0 & X86_CR0_TS);
5965 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005966
5967 /*
5968 * Save the most likely value for this task's CR3 in the VMCS.
5969 * We can't use __get_current_cr3_fast() because we're not atomic.
5970 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07005971 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07005972 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02005973 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005974
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005975 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005976 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005977 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02005978 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005979
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005980 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005981#ifdef CONFIG_X86_64
5982 /*
5983 * Load null selectors, so we can avoid reloading them in
5984 * __vmx_load_host_state(), in case userspace uses the null selectors
5985 * too (the expected case).
5986 */
5987 vmcs_write16(HOST_DS_SELECTOR, 0);
5988 vmcs_write16(HOST_ES_SELECTOR, 0);
5989#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005990 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5991 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005992#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005993 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5994 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5995
Juergen Gross87930012017-09-04 12:25:27 +02005996 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005997 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005998 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005999
Avi Kivity83287ea422012-09-16 15:10:57 +03006000 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006001
6002 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6003 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6004 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6005 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6006
6007 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6008 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6009 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6010 }
6011}
6012
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006013static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6014{
6015 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6016 if (enable_ept)
6017 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006018 if (is_guest_mode(&vmx->vcpu))
6019 vmx->vcpu.arch.cr4_guest_owned_bits &=
6020 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006021 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6022}
6023
Yang Zhang01e439b2013-04-11 19:25:12 +08006024static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6025{
6026 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6027
Andrey Smetanind62caab2015-11-10 15:36:33 +03006028 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006029 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006030
6031 if (!enable_vnmi)
6032 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6033
Yunhong Jiang64672c92016-06-13 14:19:59 -07006034 /* Enable the preemption timer dynamically */
6035 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006036 return pin_based_exec_ctrl;
6037}
6038
Andrey Smetanind62caab2015-11-10 15:36:33 +03006039static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6040{
6041 struct vcpu_vmx *vmx = to_vmx(vcpu);
6042
6043 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006044 if (cpu_has_secondary_exec_ctrls()) {
6045 if (kvm_vcpu_apicv_active(vcpu))
6046 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6047 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6048 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6049 else
6050 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6051 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6052 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6053 }
6054
6055 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006056 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006057}
6058
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006059static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6060{
6061 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006062
6063 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6064 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6065
Paolo Bonzini35754c92015-07-29 12:05:37 +02006066 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006067 exec_control &= ~CPU_BASED_TPR_SHADOW;
6068#ifdef CONFIG_X86_64
6069 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6070 CPU_BASED_CR8_LOAD_EXITING;
6071#endif
6072 }
6073 if (!enable_ept)
6074 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6075 CPU_BASED_CR3_LOAD_EXITING |
6076 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006077 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6078 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6079 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006080 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6081 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006082 return exec_control;
6083}
6084
Jim Mattson45ec3682017-08-23 16:32:04 -07006085static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006086{
Jim Mattson45ec3682017-08-23 16:32:04 -07006087 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006088 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006089}
6090
Jim Mattson75f4fc82017-08-23 16:32:03 -07006091static bool vmx_rdseed_supported(void)
6092{
6093 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006094 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006095}
6096
Paolo Bonzini80154d72017-08-24 13:55:35 +02006097static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006098{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006099 struct kvm_vcpu *vcpu = &vmx->vcpu;
6100
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006101 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006102
Paolo Bonzini80154d72017-08-24 13:55:35 +02006103 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006104 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6105 if (vmx->vpid == 0)
6106 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6107 if (!enable_ept) {
6108 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6109 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006110 /* Enable INVPCID for non-ept guests may cause performance regression. */
6111 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006112 }
6113 if (!enable_unrestricted_guest)
6114 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006115 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006116 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006117 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006118 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6119 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006120 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006121
6122 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6123 * in vmx_set_cr4. */
6124 exec_control &= ~SECONDARY_EXEC_DESC;
6125
Abel Gordonabc4fc52013-04-18 14:35:25 +03006126 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6127 (handle_vmptrld).
6128 We can NOT enable shadow_vmcs here because we don't have yet
6129 a current VMCS12
6130 */
6131 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006132
6133 if (!enable_pml)
6134 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006135
Paolo Bonzini3db13482017-08-24 14:48:03 +02006136 if (vmx_xsaves_supported()) {
6137 /* Exposing XSAVES only when XSAVE is exposed */
6138 bool xsaves_enabled =
6139 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6140 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6141
6142 if (!xsaves_enabled)
6143 exec_control &= ~SECONDARY_EXEC_XSAVES;
6144
6145 if (nested) {
6146 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006147 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006148 SECONDARY_EXEC_XSAVES;
6149 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006150 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006151 ~SECONDARY_EXEC_XSAVES;
6152 }
6153 }
6154
Paolo Bonzini80154d72017-08-24 13:55:35 +02006155 if (vmx_rdtscp_supported()) {
6156 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6157 if (!rdtscp_enabled)
6158 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6159
6160 if (nested) {
6161 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006162 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006163 SECONDARY_EXEC_RDTSCP;
6164 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006165 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006166 ~SECONDARY_EXEC_RDTSCP;
6167 }
6168 }
6169
6170 if (vmx_invpcid_supported()) {
6171 /* Exposing INVPCID only when PCID is exposed */
6172 bool invpcid_enabled =
6173 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6174 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6175
6176 if (!invpcid_enabled) {
6177 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6178 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6179 }
6180
6181 if (nested) {
6182 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006183 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006184 SECONDARY_EXEC_ENABLE_INVPCID;
6185 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006186 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006187 ~SECONDARY_EXEC_ENABLE_INVPCID;
6188 }
6189 }
6190
Jim Mattson45ec3682017-08-23 16:32:04 -07006191 if (vmx_rdrand_supported()) {
6192 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6193 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006194 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006195
6196 if (nested) {
6197 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006198 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006199 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006200 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006201 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006202 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006203 }
6204 }
6205
Jim Mattson75f4fc82017-08-23 16:32:03 -07006206 if (vmx_rdseed_supported()) {
6207 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6208 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006209 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006210
6211 if (nested) {
6212 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006213 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006214 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006215 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006216 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006217 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006218 }
6219 }
6220
Paolo Bonzini80154d72017-08-24 13:55:35 +02006221 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006222}
6223
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006224static void ept_set_mmio_spte_mask(void)
6225{
6226 /*
6227 * EPT Misconfigurations can be generated if the value of bits 2:0
6228 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006229 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006230 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6231 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006232}
6233
Wanpeng Lif53cd632014-12-02 19:14:58 +08006234#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006235/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006236 * Sets up the vmcs for emulated real mode.
6237 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006238static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006239{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006240#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006241 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006242#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006243 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006244
Abel Gordon4607c2d2013-04-18 14:35:55 +03006245 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006246 /*
6247 * At vCPU creation, "VMWRITE to any supported field
6248 * in the VMCS" is supported, so use the more
6249 * permissive vmx_vmread_bitmap to specify both read
6250 * and write permissions for the shadow VMCS.
6251 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006252 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006253 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006254 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006255 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006256 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006257
Avi Kivity6aa8b732006-12-10 02:21:36 -08006258 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6259
Avi Kivity6aa8b732006-12-10 02:21:36 -08006260 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006261 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006262 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006263
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006264 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006265
Dan Williamsdfa169b2016-06-02 11:17:24 -07006266 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006267 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006268 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006269 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006270 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006271
Andrey Smetanind62caab2015-11-10 15:36:33 +03006272 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006273 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6274 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6275 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6276 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6277
6278 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006279
Li RongQing0bcf2612015-12-03 13:29:34 +08006280 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006281 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006282 }
6283
Wanpeng Lib31c1142018-03-12 04:53:04 -07006284 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006285 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006286 vmx->ple_window = ple_window;
6287 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006288 }
6289
Xiao Guangrongc3707952011-07-12 03:28:04 +08006290 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6291 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006292 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6293
Avi Kivity9581d442010-10-19 16:46:55 +02006294 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6295 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006296 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006297#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006298 rdmsrl(MSR_FS_BASE, a);
6299 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6300 rdmsrl(MSR_GS_BASE, a);
6301 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6302#else
6303 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6304 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6305#endif
6306
Bandan Das2a499e42017-08-03 15:54:41 -04006307 if (cpu_has_vmx_vmfunc())
6308 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6309
Eddie Dong2cc51562007-05-21 07:28:09 +03006310 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6311 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006312 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006313 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006314 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006315
Radim Krčmář74545702015-04-27 15:11:25 +02006316 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6317 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006318
Paolo Bonzini03916db2014-07-24 14:21:57 +02006319 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320 u32 index = vmx_msr_index[i];
6321 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006322 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006323
6324 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6325 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006326 if (wrmsr_safe(index, data_low, data_high) < 0)
6327 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006328 vmx->guest_msrs[j].index = i;
6329 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006330 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006331 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006332 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006333
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006334 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6335 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006336
6337 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006338
6339 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006340 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006341
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006342 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6343 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6344
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006345 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006346
Wanpeng Lif53cd632014-12-02 19:14:58 +08006347 if (vmx_xsaves_supported())
6348 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6349
Peter Feiner4e595162016-07-07 14:49:58 -07006350 if (enable_pml) {
6351 ASSERT(vmx->pml_pg);
6352 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6353 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6354 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006355}
6356
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006357static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006358{
6359 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006360 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006361 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006362
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006363 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006364 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006365
Wanpeng Li518e7b92018-02-28 14:03:31 +08006366 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006367 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006368 kvm_set_cr8(vcpu, 0);
6369
6370 if (!init_event) {
6371 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6372 MSR_IA32_APICBASE_ENABLE;
6373 if (kvm_vcpu_is_reset_bsp(vcpu))
6374 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6375 apic_base_msr.host_initiated = true;
6376 kvm_set_apic_base(vcpu, &apic_base_msr);
6377 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006378
Avi Kivity2fb92db2011-04-27 19:42:18 +03006379 vmx_segment_cache_clear(vmx);
6380
Avi Kivity5706be02008-08-20 15:07:31 +03006381 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006382 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006383 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006384
6385 seg_setup(VCPU_SREG_DS);
6386 seg_setup(VCPU_SREG_ES);
6387 seg_setup(VCPU_SREG_FS);
6388 seg_setup(VCPU_SREG_GS);
6389 seg_setup(VCPU_SREG_SS);
6390
6391 vmcs_write16(GUEST_TR_SELECTOR, 0);
6392 vmcs_writel(GUEST_TR_BASE, 0);
6393 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6394 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6395
6396 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6397 vmcs_writel(GUEST_LDTR_BASE, 0);
6398 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6399 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6400
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006401 if (!init_event) {
6402 vmcs_write32(GUEST_SYSENTER_CS, 0);
6403 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6404 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6405 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6406 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006407
Wanpeng Lic37c2872017-11-20 14:52:21 -08006408 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006409 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006410
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006411 vmcs_writel(GUEST_GDTR_BASE, 0);
6412 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6413
6414 vmcs_writel(GUEST_IDTR_BASE, 0);
6415 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6416
Anthony Liguori443381a2010-12-06 10:53:38 -06006417 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006418 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006419 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006420 if (kvm_mpx_supported())
6421 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006422
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006423 setup_msrs(vmx);
6424
Avi Kivity6aa8b732006-12-10 02:21:36 -08006425 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6426
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006427 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006428 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006429 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006430 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006431 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006432 vmcs_write32(TPR_THRESHOLD, 0);
6433 }
6434
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006435 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006436
Sheng Yang2384d2b2008-01-17 15:14:33 +08006437 if (vmx->vpid != 0)
6438 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6439
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006440 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006441 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006442 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006443 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006444 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006445
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006446 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006447
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006448 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006449 if (init_event)
6450 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006451}
6452
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006453/*
6454 * In nested virtualization, check if L1 asked to exit on external interrupts.
6455 * For most existing hypervisors, this will always return true.
6456 */
6457static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6458{
6459 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6460 PIN_BASED_EXT_INTR_MASK;
6461}
6462
Bandan Das77b0f5d2014-04-19 18:17:45 -04006463/*
6464 * In nested virtualization, check if L1 has set
6465 * VM_EXIT_ACK_INTR_ON_EXIT
6466 */
6467static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6468{
6469 return get_vmcs12(vcpu)->vm_exit_controls &
6470 VM_EXIT_ACK_INTR_ON_EXIT;
6471}
6472
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006473static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6474{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006475 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006476}
6477
Jan Kiszkac9a79532014-03-07 20:03:15 +01006478static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006479{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006480 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6481 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006482}
6483
Jan Kiszkac9a79532014-03-07 20:03:15 +01006484static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006485{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006486 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006487 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006488 enable_irq_window(vcpu);
6489 return;
6490 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006491
Paolo Bonzini47c01522016-12-19 11:44:07 +01006492 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6493 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006494}
6495
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006496static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006497{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006498 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006499 uint32_t intr;
6500 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006501
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006502 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006503
Avi Kivityfa89a812008-09-01 15:57:51 +03006504 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006505 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006506 int inc_eip = 0;
6507 if (vcpu->arch.interrupt.soft)
6508 inc_eip = vcpu->arch.event_exit_inst_len;
6509 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006510 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006511 return;
6512 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006513 intr = irq | INTR_INFO_VALID_MASK;
6514 if (vcpu->arch.interrupt.soft) {
6515 intr |= INTR_TYPE_SOFT_INTR;
6516 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6517 vmx->vcpu.arch.event_exit_inst_len);
6518 } else
6519 intr |= INTR_TYPE_EXT_INTR;
6520 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006521
6522 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006523}
6524
Sheng Yangf08864b2008-05-15 18:23:25 +08006525static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6526{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006527 struct vcpu_vmx *vmx = to_vmx(vcpu);
6528
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006529 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006530 /*
6531 * Tracking the NMI-blocked state in software is built upon
6532 * finding the next open IRQ window. This, in turn, depends on
6533 * well-behaving guests: They have to keep IRQs disabled at
6534 * least as long as the NMI handler runs. Otherwise we may
6535 * cause NMI nesting, maybe breaking the guest. But as this is
6536 * highly unlikely, we can live with the residual risk.
6537 */
6538 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6539 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6540 }
6541
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006542 ++vcpu->stat.nmi_injections;
6543 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006544
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006545 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006546 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006547 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006548 return;
6549 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006550
Sheng Yangf08864b2008-05-15 18:23:25 +08006551 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6552 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006553
6554 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006555}
6556
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006557static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6558{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006559 struct vcpu_vmx *vmx = to_vmx(vcpu);
6560 bool masked;
6561
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006562 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006563 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006564 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006565 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006566 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6567 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6568 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006569}
6570
6571static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6572{
6573 struct vcpu_vmx *vmx = to_vmx(vcpu);
6574
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006575 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006576 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6577 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6578 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6579 }
6580 } else {
6581 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6582 if (masked)
6583 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6584 GUEST_INTR_STATE_NMI);
6585 else
6586 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6587 GUEST_INTR_STATE_NMI);
6588 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006589}
6590
Jan Kiszka2505dc92013-04-14 12:12:47 +02006591static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6592{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006593 if (to_vmx(vcpu)->nested.nested_run_pending)
6594 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006595
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006596 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006597 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6598 return 0;
6599
Jan Kiszka2505dc92013-04-14 12:12:47 +02006600 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6601 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6602 | GUEST_INTR_STATE_NMI));
6603}
6604
Gleb Natapov78646122009-03-23 12:12:11 +02006605static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6606{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006607 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6608 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006609 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6610 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006611}
6612
Izik Eiduscbc94022007-10-25 00:29:55 +02006613static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6614{
6615 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006616
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006617 if (enable_unrestricted_guest)
6618 return 0;
6619
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006620 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6621 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006622 if (ret)
6623 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006624 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006625 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006626}
6627
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006628static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6629{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006630 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006631 return 0;
6632}
6633
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006634static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006635{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006636 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006637 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006638 /*
6639 * Update instruction length as we may reinject the exception
6640 * from user space while in guest debugging mode.
6641 */
6642 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6643 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006644 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006645 return false;
6646 /* fall through */
6647 case DB_VECTOR:
6648 if (vcpu->guest_debug &
6649 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6650 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006651 /* fall through */
6652 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006653 case OF_VECTOR:
6654 case BR_VECTOR:
6655 case UD_VECTOR:
6656 case DF_VECTOR:
6657 case SS_VECTOR:
6658 case GP_VECTOR:
6659 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006660 return true;
6661 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006662 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006663 return false;
6664}
6665
6666static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6667 int vec, u32 err_code)
6668{
6669 /*
6670 * Instruction with address size override prefix opcode 0x67
6671 * Cause the #SS fault with 0 error code in VM86 mode.
6672 */
6673 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6674 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6675 if (vcpu->arch.halt_request) {
6676 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006677 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006678 }
6679 return 1;
6680 }
6681 return 0;
6682 }
6683
6684 /*
6685 * Forward all other exceptions that are valid in real mode.
6686 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6687 * the required debugging infrastructure rework.
6688 */
6689 kvm_queue_exception(vcpu, vec);
6690 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006691}
6692
Andi Kleena0861c02009-06-08 17:37:09 +08006693/*
6694 * Trigger machine check on the host. We assume all the MSRs are already set up
6695 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6696 * We pass a fake environment to the machine check handler because we want
6697 * the guest to be always treated like user space, no matter what context
6698 * it used internally.
6699 */
6700static void kvm_machine_check(void)
6701{
6702#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6703 struct pt_regs regs = {
6704 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6705 .flags = X86_EFLAGS_IF,
6706 };
6707
6708 do_machine_check(&regs, 0);
6709#endif
6710}
6711
Avi Kivity851ba692009-08-24 11:10:17 +03006712static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006713{
6714 /* already handled by vcpu_run */
6715 return 1;
6716}
6717
Avi Kivity851ba692009-08-24 11:10:17 +03006718static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006719{
Avi Kivity1155f762007-11-22 11:30:47 +02006720 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006721 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006722 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006723 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006724 u32 vect_info;
6725 enum emulation_result er;
6726
Avi Kivity1155f762007-11-22 11:30:47 +02006727 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006728 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006729
Andi Kleena0861c02009-06-08 17:37:09 +08006730 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006731 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006732
Jim Mattsonef85b672016-12-12 11:01:37 -08006733 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006734 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006735
Wanpeng Li082d06e2018-04-03 16:28:48 -07006736 if (is_invalid_opcode(intr_info))
6737 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006738
Avi Kivity6aa8b732006-12-10 02:21:36 -08006739 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006740 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006741 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006742
Liran Alon9e869482018-03-12 13:12:51 +02006743 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6744 WARN_ON_ONCE(!enable_vmware_backdoor);
6745 er = emulate_instruction(vcpu,
6746 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6747 if (er == EMULATE_USER_EXIT)
6748 return 0;
6749 else if (er != EMULATE_DONE)
6750 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6751 return 1;
6752 }
6753
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006754 /*
6755 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6756 * MMIO, it is better to report an internal error.
6757 * See the comments in vmx_handle_exit.
6758 */
6759 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6760 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6761 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6762 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006763 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006764 vcpu->run->internal.data[0] = vect_info;
6765 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006766 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006767 return 0;
6768 }
6769
Avi Kivity6aa8b732006-12-10 02:21:36 -08006770 if (is_page_fault(intr_info)) {
6771 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006772 /* EPT won't cause page fault directly */
6773 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006774 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006775 }
6776
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006777 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006778
6779 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6780 return handle_rmode_exception(vcpu, ex_no, error_code);
6781
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006782 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006783 case AC_VECTOR:
6784 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6785 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006786 case DB_VECTOR:
6787 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6788 if (!(vcpu->guest_debug &
6789 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006790 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006791 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006792 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006793 skip_emulated_instruction(vcpu);
6794
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006795 kvm_queue_exception(vcpu, DB_VECTOR);
6796 return 1;
6797 }
6798 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6799 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6800 /* fall through */
6801 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006802 /*
6803 * Update instruction length as we may reinject #BP from
6804 * user space while in guest debugging mode. Reading it for
6805 * #DB as well causes no harm, it is not used in that case.
6806 */
6807 vmx->vcpu.arch.event_exit_inst_len =
6808 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006809 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006810 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006811 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6812 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006813 break;
6814 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006815 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6816 kvm_run->ex.exception = ex_no;
6817 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006818 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006819 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006820 return 0;
6821}
6822
Avi Kivity851ba692009-08-24 11:10:17 +03006823static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006824{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006825 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006826 return 1;
6827}
6828
Avi Kivity851ba692009-08-24 11:10:17 +03006829static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006830{
Avi Kivity851ba692009-08-24 11:10:17 +03006831 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006832 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006833 return 0;
6834}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006835
Avi Kivity851ba692009-08-24 11:10:17 +03006836static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006837{
He, Qingbfdaab02007-09-12 14:18:28 +08006838 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006839 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006840 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006841
He, Qingbfdaab02007-09-12 14:18:28 +08006842 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006843 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006844
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006845 ++vcpu->stat.io_exits;
6846
Sean Christopherson432baf62018-03-08 08:57:26 -08006847 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006848 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006849
6850 port = exit_qualification >> 16;
6851 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006852 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006853
Sean Christophersondca7f122018-03-08 08:57:27 -08006854 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006855}
6856
Ingo Molnar102d8322007-02-19 14:37:47 +02006857static void
6858vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6859{
6860 /*
6861 * Patch in the VMCALL instruction:
6862 */
6863 hypercall[0] = 0x0f;
6864 hypercall[1] = 0x01;
6865 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006866}
6867
Guo Chao0fa06072012-06-28 15:16:19 +08006868/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006869static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6870{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006871 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006872 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6873 unsigned long orig_val = val;
6874
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006875 /*
6876 * We get here when L2 changed cr0 in a way that did not change
6877 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006878 * but did change L0 shadowed bits. So we first calculate the
6879 * effective cr0 value that L1 would like to write into the
6880 * hardware. It consists of the L2-owned bits from the new
6881 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006882 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006883 val = (val & ~vmcs12->cr0_guest_host_mask) |
6884 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6885
David Matlack38991522016-11-29 18:14:08 -08006886 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006887 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006888
6889 if (kvm_set_cr0(vcpu, val))
6890 return 1;
6891 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006892 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006893 } else {
6894 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006895 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006896 return 1;
David Matlack38991522016-11-29 18:14:08 -08006897
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006898 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006899 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006900}
6901
6902static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6903{
6904 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006905 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6906 unsigned long orig_val = val;
6907
6908 /* analogously to handle_set_cr0 */
6909 val = (val & ~vmcs12->cr4_guest_host_mask) |
6910 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6911 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006912 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006913 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006914 return 0;
6915 } else
6916 return kvm_set_cr4(vcpu, val);
6917}
6918
Paolo Bonzini0367f202016-07-12 10:44:55 +02006919static int handle_desc(struct kvm_vcpu *vcpu)
6920{
6921 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6922 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6923}
6924
Avi Kivity851ba692009-08-24 11:10:17 +03006925static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006926{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006927 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006928 int cr;
6929 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006930 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006931 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006932
He, Qingbfdaab02007-09-12 14:18:28 +08006933 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006934 cr = exit_qualification & 15;
6935 reg = (exit_qualification >> 8) & 15;
6936 switch ((exit_qualification >> 4) & 3) {
6937 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006938 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006939 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006940 switch (cr) {
6941 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006942 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006943 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006944 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006945 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006946 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006947 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006948 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006949 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006950 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006951 case 8: {
6952 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006953 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006954 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006955 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006956 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08006957 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006958 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006959 return ret;
6960 /*
6961 * TODO: we might be squashing a
6962 * KVM_GUESTDBG_SINGLESTEP-triggered
6963 * KVM_EXIT_DEBUG here.
6964 */
Avi Kivity851ba692009-08-24 11:10:17 +03006965 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006966 return 0;
6967 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006968 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006969 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006970 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006971 WARN_ONCE(1, "Guest should always own CR0.TS");
6972 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02006973 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08006974 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006975 case 1: /*mov from cr*/
6976 switch (cr) {
6977 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006978 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02006979 val = kvm_read_cr3(vcpu);
6980 kvm_register_write(vcpu, reg, val);
6981 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006982 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006983 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006984 val = kvm_get_cr8(vcpu);
6985 kvm_register_write(vcpu, reg, val);
6986 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006987 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006988 }
6989 break;
6990 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006991 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006992 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006993 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006994
Kyle Huey6affcbe2016-11-29 12:40:40 -08006995 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006996 default:
6997 break;
6998 }
Avi Kivity851ba692009-08-24 11:10:17 +03006999 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007000 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007001 (int)(exit_qualification >> 4) & 3, cr);
7002 return 0;
7003}
7004
Avi Kivity851ba692009-08-24 11:10:17 +03007005static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007006{
He, Qingbfdaab02007-09-12 14:18:28 +08007007 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007008 int dr, dr7, reg;
7009
7010 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7011 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7012
7013 /* First, if DR does not exist, trigger UD */
7014 if (!kvm_require_dr(vcpu, dr))
7015 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007016
Jan Kiszkaf2483412010-01-20 18:20:20 +01007017 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007018 if (!kvm_require_cpl(vcpu, 0))
7019 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007020 dr7 = vmcs_readl(GUEST_DR7);
7021 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007022 /*
7023 * As the vm-exit takes precedence over the debug trap, we
7024 * need to emulate the latter, either for the host or the
7025 * guest debugging itself.
7026 */
7027 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007028 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007029 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007030 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007031 vcpu->run->debug.arch.exception = DB_VECTOR;
7032 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007033 return 0;
7034 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007035 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007036 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007037 kvm_queue_exception(vcpu, DB_VECTOR);
7038 return 1;
7039 }
7040 }
7041
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007042 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007043 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7044 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007045
7046 /*
7047 * No more DR vmexits; force a reload of the debug registers
7048 * and reenter on this instruction. The next vmexit will
7049 * retrieve the full state of the debug registers.
7050 */
7051 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7052 return 1;
7053 }
7054
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007055 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7056 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007057 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007058
7059 if (kvm_get_dr(vcpu, dr, &val))
7060 return 1;
7061 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007062 } else
Nadav Amit57773922014-06-18 17:19:23 +03007063 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007064 return 1;
7065
Kyle Huey6affcbe2016-11-29 12:40:40 -08007066 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007067}
7068
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007069static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7070{
7071 return vcpu->arch.dr6;
7072}
7073
7074static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7075{
7076}
7077
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007078static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7079{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007080 get_debugreg(vcpu->arch.db[0], 0);
7081 get_debugreg(vcpu->arch.db[1], 1);
7082 get_debugreg(vcpu->arch.db[2], 2);
7083 get_debugreg(vcpu->arch.db[3], 3);
7084 get_debugreg(vcpu->arch.dr6, 6);
7085 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7086
7087 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007088 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007089}
7090
Gleb Natapov020df072010-04-13 10:05:23 +03007091static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7092{
7093 vmcs_writel(GUEST_DR7, val);
7094}
7095
Avi Kivity851ba692009-08-24 11:10:17 +03007096static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007097{
Kyle Huey6a908b62016-11-29 12:40:37 -08007098 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007099}
7100
Avi Kivity851ba692009-08-24 11:10:17 +03007101static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007102{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007103 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007104 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007105
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007106 msr_info.index = ecx;
7107 msr_info.host_initiated = false;
7108 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007109 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007110 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007111 return 1;
7112 }
7113
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007114 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007115
Avi Kivity6aa8b732006-12-10 02:21:36 -08007116 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007117 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7118 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007119 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007120}
7121
Avi Kivity851ba692009-08-24 11:10:17 +03007122static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007123{
Will Auld8fe8ab42012-11-29 12:42:12 -08007124 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007125 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7126 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7127 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007128
Will Auld8fe8ab42012-11-29 12:42:12 -08007129 msr.data = data;
7130 msr.index = ecx;
7131 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007132 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007133 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007134 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007135 return 1;
7136 }
7137
Avi Kivity59200272010-01-25 19:47:02 +02007138 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007139 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007140}
7141
Avi Kivity851ba692009-08-24 11:10:17 +03007142static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007143{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007144 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007145 return 1;
7146}
7147
Avi Kivity851ba692009-08-24 11:10:17 +03007148static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007149{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007150 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7151 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007152
Avi Kivity3842d132010-07-27 12:30:24 +03007153 kvm_make_request(KVM_REQ_EVENT, vcpu);
7154
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007155 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007156 return 1;
7157}
7158
Avi Kivity851ba692009-08-24 11:10:17 +03007159static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007160{
Avi Kivityd3bef152007-06-05 15:53:05 +03007161 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007162}
7163
Avi Kivity851ba692009-08-24 11:10:17 +03007164static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007165{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007166 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007167}
7168
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007169static int handle_invd(struct kvm_vcpu *vcpu)
7170{
Andre Przywara51d8b662010-12-21 11:12:02 +01007171 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007172}
7173
Avi Kivity851ba692009-08-24 11:10:17 +03007174static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007175{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007176 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007177
7178 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007179 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007180}
7181
Avi Kivityfee84b02011-11-10 14:57:25 +02007182static int handle_rdpmc(struct kvm_vcpu *vcpu)
7183{
7184 int err;
7185
7186 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007187 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007188}
7189
Avi Kivity851ba692009-08-24 11:10:17 +03007190static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007191{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007192 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007193}
7194
Dexuan Cui2acf9232010-06-10 11:27:12 +08007195static int handle_xsetbv(struct kvm_vcpu *vcpu)
7196{
7197 u64 new_bv = kvm_read_edx_eax(vcpu);
7198 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7199
7200 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007201 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007202 return 1;
7203}
7204
Wanpeng Lif53cd632014-12-02 19:14:58 +08007205static int handle_xsaves(struct kvm_vcpu *vcpu)
7206{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007207 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007208 WARN(1, "this should never happen\n");
7209 return 1;
7210}
7211
7212static int handle_xrstors(struct kvm_vcpu *vcpu)
7213{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007214 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007215 WARN(1, "this should never happen\n");
7216 return 1;
7217}
7218
Avi Kivity851ba692009-08-24 11:10:17 +03007219static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007220{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007221 if (likely(fasteoi)) {
7222 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7223 int access_type, offset;
7224
7225 access_type = exit_qualification & APIC_ACCESS_TYPE;
7226 offset = exit_qualification & APIC_ACCESS_OFFSET;
7227 /*
7228 * Sane guest uses MOV to write EOI, with written value
7229 * not cared. So make a short-circuit here by avoiding
7230 * heavy instruction emulation.
7231 */
7232 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7233 (offset == APIC_EOI)) {
7234 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007235 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007236 }
7237 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007238 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007239}
7240
Yang Zhangc7c9c562013-01-25 10:18:51 +08007241static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7242{
7243 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7244 int vector = exit_qualification & 0xff;
7245
7246 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7247 kvm_apic_set_eoi_accelerated(vcpu, vector);
7248 return 1;
7249}
7250
Yang Zhang83d4c282013-01-25 10:18:49 +08007251static int handle_apic_write(struct kvm_vcpu *vcpu)
7252{
7253 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7254 u32 offset = exit_qualification & 0xfff;
7255
7256 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7257 kvm_apic_write_nodecode(vcpu, offset);
7258 return 1;
7259}
7260
Avi Kivity851ba692009-08-24 11:10:17 +03007261static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007262{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007263 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007264 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007265 bool has_error_code = false;
7266 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007267 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007268 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007269
7270 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007271 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007272 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007273
7274 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7275
7276 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007277 if (reason == TASK_SWITCH_GATE && idt_v) {
7278 switch (type) {
7279 case INTR_TYPE_NMI_INTR:
7280 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007281 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007282 break;
7283 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007284 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007285 kvm_clear_interrupt_queue(vcpu);
7286 break;
7287 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007288 if (vmx->idt_vectoring_info &
7289 VECTORING_INFO_DELIVER_CODE_MASK) {
7290 has_error_code = true;
7291 error_code =
7292 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7293 }
7294 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007295 case INTR_TYPE_SOFT_EXCEPTION:
7296 kvm_clear_exception_queue(vcpu);
7297 break;
7298 default:
7299 break;
7300 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007301 }
Izik Eidus37817f22008-03-24 23:14:53 +02007302 tss_selector = exit_qualification;
7303
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007304 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7305 type != INTR_TYPE_EXT_INTR &&
7306 type != INTR_TYPE_NMI_INTR))
7307 skip_emulated_instruction(vcpu);
7308
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007309 if (kvm_task_switch(vcpu, tss_selector,
7310 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7311 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007312 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7313 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7314 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007315 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007316 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007317
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007318 /*
7319 * TODO: What about debug traps on tss switch?
7320 * Are we supposed to inject them and update dr6?
7321 */
7322
7323 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007324}
7325
Avi Kivity851ba692009-08-24 11:10:17 +03007326static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007327{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007328 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007329 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007330 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007331
Sheng Yangf9c617f2009-03-25 10:08:52 +08007332 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007333
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007334 /*
7335 * EPT violation happened while executing iret from NMI,
7336 * "blocked by NMI" bit has to be set before next VM entry.
7337 * There are errata that may cause this bit to not be set:
7338 * AAK134, BY25.
7339 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007340 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007341 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007342 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007343 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7344
Sheng Yang14394422008-04-28 12:24:45 +08007345 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007346 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007347
Junaid Shahid27959a42016-12-06 16:46:10 -08007348 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007349 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007350 ? PFERR_USER_MASK : 0;
7351 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007352 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007353 ? PFERR_WRITE_MASK : 0;
7354 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007355 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007356 ? PFERR_FETCH_MASK : 0;
7357 /* ept page table entry is present? */
7358 error_code |= (exit_qualification &
7359 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7360 EPT_VIOLATION_EXECUTABLE))
7361 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007362
Paolo Bonzinieebed242016-11-28 14:39:58 +01007363 error_code |= (exit_qualification & 0x100) != 0 ?
7364 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007365
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007366 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007367 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007368}
7369
Avi Kivity851ba692009-08-24 11:10:17 +03007370static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007371{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007372 gpa_t gpa;
7373
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007374 /*
7375 * A nested guest cannot optimize MMIO vmexits, because we have an
7376 * nGPA here instead of the required GPA.
7377 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007378 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007379 if (!is_guest_mode(vcpu) &&
7380 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007381 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007382 /*
7383 * Doing kvm_skip_emulated_instruction() depends on undefined
7384 * behavior: Intel's manual doesn't mandate
7385 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7386 * occurs and while on real hardware it was observed to be set,
7387 * other hypervisors (namely Hyper-V) don't set it, we end up
7388 * advancing IP with some random value. Disable fast mmio when
7389 * running nested and keep it for real hardware in hope that
7390 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7391 */
7392 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7393 return kvm_skip_emulated_instruction(vcpu);
7394 else
7395 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7396 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007397 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007398
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007399 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007400}
7401
Avi Kivity851ba692009-08-24 11:10:17 +03007402static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007403{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007404 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007405 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7406 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007407 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007408 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007409
7410 return 1;
7411}
7412
Mohammed Gamal80ced182009-09-01 12:48:18 +02007413static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007414{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007415 struct vcpu_vmx *vmx = to_vmx(vcpu);
7416 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007417 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007418 u32 cpu_exec_ctrl;
7419 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007420 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007421
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007422 /*
7423 * We should never reach the point where we are emulating L2
7424 * due to invalid guest state as that means we incorrectly
7425 * allowed a nested VMEntry with an invalid vmcs12.
7426 */
7427 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7428
Avi Kivity49e9d552010-09-19 14:34:08 +02007429 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7430 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007431
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007432 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007433 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007434 return handle_interrupt_window(&vmx->vcpu);
7435
Radim Krčmář72875d82017-04-26 22:32:19 +02007436 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007437 return 1;
7438
Liran Alon9b8ae632017-11-05 16:56:34 +02007439 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007440
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007441 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007442 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007443 ret = 0;
7444 goto out;
7445 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007446
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007447 if (err != EMULATE_DONE)
7448 goto emulation_error;
7449
7450 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7451 vcpu->arch.exception.pending)
7452 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007453
Gleb Natapov8d76c492013-05-08 18:38:44 +03007454 if (vcpu->arch.halt_request) {
7455 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007456 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007457 goto out;
7458 }
7459
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007460 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007461 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007462 if (need_resched())
7463 schedule();
7464 }
7465
Mohammed Gamal80ced182009-09-01 12:48:18 +02007466out:
7467 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007468
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007469emulation_error:
7470 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7471 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7472 vcpu->run->internal.ndata = 0;
7473 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007474}
7475
7476static void grow_ple_window(struct kvm_vcpu *vcpu)
7477{
7478 struct vcpu_vmx *vmx = to_vmx(vcpu);
7479 int old = vmx->ple_window;
7480
Babu Mogerc8e88712018-03-16 16:37:24 -04007481 vmx->ple_window = __grow_ple_window(old, ple_window,
7482 ple_window_grow,
7483 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007484
7485 if (vmx->ple_window != old)
7486 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007487
7488 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007489}
7490
7491static void shrink_ple_window(struct kvm_vcpu *vcpu)
7492{
7493 struct vcpu_vmx *vmx = to_vmx(vcpu);
7494 int old = vmx->ple_window;
7495
Babu Mogerc8e88712018-03-16 16:37:24 -04007496 vmx->ple_window = __shrink_ple_window(old, ple_window,
7497 ple_window_shrink,
7498 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007499
7500 if (vmx->ple_window != old)
7501 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007502
7503 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007504}
7505
7506/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007507 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7508 */
7509static void wakeup_handler(void)
7510{
7511 struct kvm_vcpu *vcpu;
7512 int cpu = smp_processor_id();
7513
7514 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7515 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7516 blocked_vcpu_list) {
7517 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7518
7519 if (pi_test_on(pi_desc) == 1)
7520 kvm_vcpu_kick(vcpu);
7521 }
7522 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7523}
7524
Peng Haoe01bca22018-04-07 05:47:32 +08007525static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007526{
7527 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7528 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7529 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7530 0ull, VMX_EPT_EXECUTABLE_MASK,
7531 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007532 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007533
7534 ept_set_mmio_spte_mask();
7535 kvm_enable_tdp();
7536}
7537
Tiejun Chenf2c76482014-10-28 10:14:47 +08007538static __init int hardware_setup(void)
7539{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007540 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007541
7542 rdmsrl_safe(MSR_EFER, &host_efer);
7543
7544 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7545 kvm_define_shared_msr(i, vmx_msr_index[i]);
7546
Radim Krčmář23611332016-09-29 22:41:33 +02007547 for (i = 0; i < VMX_BITMAP_NR; i++) {
7548 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7549 if (!vmx_bitmap[i])
7550 goto out;
7551 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007552
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007553 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7554 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7555
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007556 if (setup_vmcs_config(&vmcs_config) < 0) {
7557 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007558 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007559 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007560
7561 if (boot_cpu_has(X86_FEATURE_NX))
7562 kvm_enable_efer_bits(EFER_NX);
7563
Wanpeng Li08d839c2017-03-23 05:30:08 -07007564 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7565 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007566 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007567
Tiejun Chenf2c76482014-10-28 10:14:47 +08007568 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007569 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007570 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007571 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007572 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007573
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007574 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007575 enable_ept_ad_bits = 0;
7576
Wanpeng Li8ad81822017-10-09 15:51:53 -07007577 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007578 enable_unrestricted_guest = 0;
7579
Paolo Bonziniad15a292015-01-30 16:18:49 +01007580 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007581 flexpriority_enabled = 0;
7582
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007583 if (!cpu_has_virtual_nmis())
7584 enable_vnmi = 0;
7585
Paolo Bonziniad15a292015-01-30 16:18:49 +01007586 /*
7587 * set_apic_access_page_addr() is used to reload apic access
7588 * page upon invalidation. No need to do anything if not
7589 * using the APIC_ACCESS_ADDR VMCS field.
7590 */
7591 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007592 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007593
7594 if (!cpu_has_vmx_tpr_shadow())
7595 kvm_x86_ops->update_cr8_intercept = NULL;
7596
7597 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7598 kvm_disable_largepages();
7599
Wanpeng Li0f107682017-09-28 18:06:24 -07007600 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007601 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007602 ple_window = 0;
7603 ple_window_grow = 0;
7604 ple_window_max = 0;
7605 ple_window_shrink = 0;
7606 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007607
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007608 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007609 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007610 kvm_x86_ops->sync_pir_to_irr = NULL;
7611 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007612
Haozhong Zhang64903d62015-10-20 15:39:09 +08007613 if (cpu_has_vmx_tsc_scaling()) {
7614 kvm_has_tsc_control = true;
7615 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7616 kvm_tsc_scaling_ratio_frac_bits = 48;
7617 }
7618
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007619 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7620
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007621 if (enable_ept)
7622 vmx_enable_tdp();
7623 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007624 kvm_disable_tdp();
7625
Kai Huang843e4332015-01-28 10:54:28 +08007626 /*
7627 * Only enable PML when hardware supports PML feature, and both EPT
7628 * and EPT A/D bit features are enabled -- PML depends on them to work.
7629 */
7630 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7631 enable_pml = 0;
7632
7633 if (!enable_pml) {
7634 kvm_x86_ops->slot_enable_log_dirty = NULL;
7635 kvm_x86_ops->slot_disable_log_dirty = NULL;
7636 kvm_x86_ops->flush_log_dirty = NULL;
7637 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7638 }
7639
Yunhong Jiang64672c92016-06-13 14:19:59 -07007640 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7641 u64 vmx_msr;
7642
7643 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7644 cpu_preemption_timer_multi =
7645 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7646 } else {
7647 kvm_x86_ops->set_hv_timer = NULL;
7648 kvm_x86_ops->cancel_hv_timer = NULL;
7649 }
7650
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007651 if (!cpu_has_vmx_shadow_vmcs())
7652 enable_shadow_vmcs = 0;
7653 if (enable_shadow_vmcs)
7654 init_vmcs_shadow_fields();
7655
Feng Wubf9f6ac2015-09-18 22:29:55 +08007656 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007657 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007658
Ashok Rajc45dcc72016-06-22 14:59:56 +08007659 kvm_mce_cap_supported |= MCG_LMCE_P;
7660
Tiejun Chenf2c76482014-10-28 10:14:47 +08007661 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007662
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007663out:
Radim Krčmář23611332016-09-29 22:41:33 +02007664 for (i = 0; i < VMX_BITMAP_NR; i++)
7665 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007666
7667 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007668}
7669
7670static __exit void hardware_unsetup(void)
7671{
Radim Krčmář23611332016-09-29 22:41:33 +02007672 int i;
7673
7674 for (i = 0; i < VMX_BITMAP_NR; i++)
7675 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007676
Tiejun Chenf2c76482014-10-28 10:14:47 +08007677 free_kvm_area();
7678}
7679
Avi Kivity6aa8b732006-12-10 02:21:36 -08007680/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007681 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7682 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7683 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007684static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007685{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007686 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007687 grow_ple_window(vcpu);
7688
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007689 /*
7690 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7691 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7692 * never set PAUSE_EXITING and just set PLE if supported,
7693 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7694 */
7695 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007696 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007697}
7698
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007699static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007700{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007701 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007702}
7703
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007704static int handle_mwait(struct kvm_vcpu *vcpu)
7705{
7706 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7707 return handle_nop(vcpu);
7708}
7709
Jim Mattson45ec3682017-08-23 16:32:04 -07007710static int handle_invalid_op(struct kvm_vcpu *vcpu)
7711{
7712 kvm_queue_exception(vcpu, UD_VECTOR);
7713 return 1;
7714}
7715
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007716static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7717{
7718 return 1;
7719}
7720
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007721static int handle_monitor(struct kvm_vcpu *vcpu)
7722{
7723 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7724 return handle_nop(vcpu);
7725}
7726
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007727/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007728 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7729 * set the success or error code of an emulated VMX instruction, as specified
7730 * by Vol 2B, VMX Instruction Reference, "Conventions".
7731 */
7732static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7733{
7734 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7735 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7736 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7737}
7738
7739static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7740{
7741 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7742 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7743 X86_EFLAGS_SF | X86_EFLAGS_OF))
7744 | X86_EFLAGS_CF);
7745}
7746
Abel Gordon145c28d2013-04-18 14:36:55 +03007747static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007748 u32 vm_instruction_error)
7749{
7750 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7751 /*
7752 * failValid writes the error number to the current VMCS, which
7753 * can't be done there isn't a current VMCS.
7754 */
7755 nested_vmx_failInvalid(vcpu);
7756 return;
7757 }
7758 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7759 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7760 X86_EFLAGS_SF | X86_EFLAGS_OF))
7761 | X86_EFLAGS_ZF);
7762 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7763 /*
7764 * We don't need to force a shadow sync because
7765 * VM_INSTRUCTION_ERROR is not shadowed
7766 */
7767}
Abel Gordon145c28d2013-04-18 14:36:55 +03007768
Wincy Vanff651cb2014-12-11 08:52:58 +03007769static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7770{
7771 /* TODO: not to reset guest simply here. */
7772 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007773 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007774}
7775
Jan Kiszkaf41245002014-03-07 20:03:13 +01007776static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7777{
7778 struct vcpu_vmx *vmx =
7779 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7780
7781 vmx->nested.preemption_timer_expired = true;
7782 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7783 kvm_vcpu_kick(&vmx->vcpu);
7784
7785 return HRTIMER_NORESTART;
7786}
7787
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007788/*
Bandan Das19677e32014-05-06 02:19:15 -04007789 * Decode the memory-address operand of a vmx instruction, as recorded on an
7790 * exit caused by such an instruction (run by a guest hypervisor).
7791 * On success, returns 0. When the operand is invalid, returns 1 and throws
7792 * #UD or #GP.
7793 */
7794static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7795 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007796 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007797{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007798 gva_t off;
7799 bool exn;
7800 struct kvm_segment s;
7801
Bandan Das19677e32014-05-06 02:19:15 -04007802 /*
7803 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7804 * Execution", on an exit, vmx_instruction_info holds most of the
7805 * addressing components of the operand. Only the displacement part
7806 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7807 * For how an actual address is calculated from all these components,
7808 * refer to Vol. 1, "Operand Addressing".
7809 */
7810 int scaling = vmx_instruction_info & 3;
7811 int addr_size = (vmx_instruction_info >> 7) & 7;
7812 bool is_reg = vmx_instruction_info & (1u << 10);
7813 int seg_reg = (vmx_instruction_info >> 15) & 7;
7814 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7815 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7816 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7817 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7818
7819 if (is_reg) {
7820 kvm_queue_exception(vcpu, UD_VECTOR);
7821 return 1;
7822 }
7823
7824 /* Addr = segment_base + offset */
7825 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007826 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007827 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007828 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007829 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007830 off += kvm_register_read(vcpu, index_reg)<<scaling;
7831 vmx_get_segment(vcpu, &s, seg_reg);
7832 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007833
7834 if (addr_size == 1) /* 32 bit */
7835 *ret &= 0xffffffff;
7836
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007837 /* Checks for #GP/#SS exceptions. */
7838 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007839 if (is_long_mode(vcpu)) {
7840 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7841 * non-canonical form. This is the only check on the memory
7842 * destination for long mode!
7843 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007844 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007845 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007846 /* Protected mode: apply checks for segment validity in the
7847 * following order:
7848 * - segment type check (#GP(0) may be thrown)
7849 * - usability check (#GP(0)/#SS(0))
7850 * - limit check (#GP(0)/#SS(0))
7851 */
7852 if (wr)
7853 /* #GP(0) if the destination operand is located in a
7854 * read-only data segment or any code segment.
7855 */
7856 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7857 else
7858 /* #GP(0) if the source operand is located in an
7859 * execute-only code segment
7860 */
7861 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007862 if (exn) {
7863 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7864 return 1;
7865 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007866 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7867 */
7868 exn = (s.unusable != 0);
7869 /* Protected mode: #GP(0)/#SS(0) if the memory
7870 * operand is outside the segment limit.
7871 */
7872 exn = exn || (off + sizeof(u64) > s.limit);
7873 }
7874 if (exn) {
7875 kvm_queue_exception_e(vcpu,
7876 seg_reg == VCPU_SREG_SS ?
7877 SS_VECTOR : GP_VECTOR,
7878 0);
7879 return 1;
7880 }
7881
Bandan Das19677e32014-05-06 02:19:15 -04007882 return 0;
7883}
7884
Radim Krčmářcbf71272017-05-19 15:48:51 +02007885static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007886{
7887 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007888 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007889
7890 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007891 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007892 return 1;
7893
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007894 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007895 kvm_inject_page_fault(vcpu, &e);
7896 return 1;
7897 }
7898
Bandan Das3573e222014-05-06 02:19:16 -04007899 return 0;
7900}
7901
Jim Mattsone29acc52016-11-30 12:03:43 -08007902static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7903{
7904 struct vcpu_vmx *vmx = to_vmx(vcpu);
7905 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007906 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007907
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007908 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7909 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007910 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007911
7912 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7913 if (!vmx->nested.cached_vmcs12)
7914 goto out_cached_vmcs12;
7915
7916 if (enable_shadow_vmcs) {
7917 shadow_vmcs = alloc_vmcs();
7918 if (!shadow_vmcs)
7919 goto out_shadow_vmcs;
7920 /* mark vmcs as shadow */
7921 shadow_vmcs->revision_id |= (1u << 31);
7922 /* init shadow vmcs */
7923 vmcs_clear(shadow_vmcs);
7924 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7925 }
7926
Jim Mattsone29acc52016-11-30 12:03:43 -08007927 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7928 HRTIMER_MODE_REL_PINNED);
7929 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7930
7931 vmx->nested.vmxon = true;
7932 return 0;
7933
7934out_shadow_vmcs:
7935 kfree(vmx->nested.cached_vmcs12);
7936
7937out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007938 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007939
Jim Mattsonde3a0022017-11-27 17:22:25 -06007940out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007941 return -ENOMEM;
7942}
7943
Bandan Das3573e222014-05-06 02:19:16 -04007944/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007945 * Emulate the VMXON instruction.
7946 * Currently, we just remember that VMX is active, and do not save or even
7947 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7948 * do not currently need to store anything in that guest-allocated memory
7949 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7950 * argument is different from the VMXON pointer (which the spec says they do).
7951 */
7952static int handle_vmon(struct kvm_vcpu *vcpu)
7953{
Jim Mattsone29acc52016-11-30 12:03:43 -08007954 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007955 gpa_t vmptr;
7956 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007957 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007958 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7959 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007960
Jim Mattson70f3aac2017-04-26 08:53:46 -07007961 /*
7962 * The Intel VMX Instruction Reference lists a bunch of bits that are
7963 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
7964 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
7965 * Otherwise, we should fail with #UD. But most faulting conditions
7966 * have already been checked by hardware, prior to the VM-exit for
7967 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
7968 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007969 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07007970 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007971 kvm_queue_exception(vcpu, UD_VECTOR);
7972 return 1;
7973 }
7974
Felix Wilhelm727ba742018-06-11 09:43:44 +02007975 /* CPL=0 must be checked manually. */
7976 if (vmx_get_cpl(vcpu)) {
7977 kvm_queue_exception(vcpu, UD_VECTOR);
7978 return 1;
7979 }
7980
Abel Gordon145c28d2013-04-18 14:36:55 +03007981 if (vmx->nested.vmxon) {
7982 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007983 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007984 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007985
Haozhong Zhang3b840802016-06-22 14:59:54 +08007986 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007987 != VMXON_NEEDED_FEATURES) {
7988 kvm_inject_gp(vcpu, 0);
7989 return 1;
7990 }
7991
Radim Krčmářcbf71272017-05-19 15:48:51 +02007992 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007993 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02007994
7995 /*
7996 * SDM 3: 24.11.5
7997 * The first 4 bytes of VMXON region contain the supported
7998 * VMCS revision identifier
7999 *
8000 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8001 * which replaces physical address width with 32
8002 */
8003 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8004 nested_vmx_failInvalid(vcpu);
8005 return kvm_skip_emulated_instruction(vcpu);
8006 }
8007
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008008 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8009 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008010 nested_vmx_failInvalid(vcpu);
8011 return kvm_skip_emulated_instruction(vcpu);
8012 }
8013 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8014 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008015 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008016 nested_vmx_failInvalid(vcpu);
8017 return kvm_skip_emulated_instruction(vcpu);
8018 }
8019 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008020 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008021
8022 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008023 ret = enter_vmx_operation(vcpu);
8024 if (ret)
8025 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008026
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008027 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008028 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008029}
8030
8031/*
8032 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8033 * for running VMX instructions (except VMXON, whose prerequisites are
8034 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008035 * Note that many of these exceptions have priority over VM exits, so they
8036 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008037 */
8038static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8039{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008040 if (vmx_get_cpl(vcpu)) {
8041 kvm_queue_exception(vcpu, UD_VECTOR);
8042 return 0;
8043 }
8044
Jim Mattson70f3aac2017-04-26 08:53:46 -07008045 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008046 kvm_queue_exception(vcpu, UD_VECTOR);
8047 return 0;
8048 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008049 return 1;
8050}
8051
David Matlack8ca44e82017-08-01 14:00:39 -07008052static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8053{
8054 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8055 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8056}
8057
Abel Gordone7953d72013-04-18 14:37:55 +03008058static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8059{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008060 if (vmx->nested.current_vmptr == -1ull)
8061 return;
8062
Abel Gordon012f83c2013-04-18 14:39:25 +03008063 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008064 /* copy to memory all shadowed fields in case
8065 they were modified */
8066 copy_shadow_to_vmcs12(vmx);
8067 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008068 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008069 }
Wincy Van705699a2015-02-03 23:58:17 +08008070 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008071
8072 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008073 kvm_vcpu_write_guest_page(&vmx->vcpu,
8074 vmx->nested.current_vmptr >> PAGE_SHIFT,
8075 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008076
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008077 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008078}
8079
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008080/*
8081 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8082 * just stops using VMX.
8083 */
8084static void free_nested(struct vcpu_vmx *vmx)
8085{
Wanpeng Lib7455822017-11-22 14:04:00 -08008086 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008087 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008088
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008089 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008090 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008091 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008092 vmx->nested.posted_intr_nv = -1;
8093 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008094 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008095 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008096 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8097 free_vmcs(vmx->vmcs01.shadow_vmcs);
8098 vmx->vmcs01.shadow_vmcs = NULL;
8099 }
David Matlack4f2777b2016-07-13 17:16:37 -07008100 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008101 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008102 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008103 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008104 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008105 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008106 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008107 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008108 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008109 }
Wincy Van705699a2015-02-03 23:58:17 +08008110 if (vmx->nested.pi_desc_page) {
8111 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008112 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008113 vmx->nested.pi_desc_page = NULL;
8114 vmx->nested.pi_desc = NULL;
8115 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008116
Jim Mattsonde3a0022017-11-27 17:22:25 -06008117 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008118}
8119
8120/* Emulate the VMXOFF instruction */
8121static int handle_vmoff(struct kvm_vcpu *vcpu)
8122{
8123 if (!nested_vmx_check_permission(vcpu))
8124 return 1;
8125 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008126 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008127 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008128}
8129
Nadav Har'El27d6c862011-05-25 23:06:59 +03008130/* Emulate the VMCLEAR instruction */
8131static int handle_vmclear(struct kvm_vcpu *vcpu)
8132{
8133 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008134 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008135 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008136
8137 if (!nested_vmx_check_permission(vcpu))
8138 return 1;
8139
Radim Krčmářcbf71272017-05-19 15:48:51 +02008140 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008141 return 1;
8142
Radim Krčmářcbf71272017-05-19 15:48:51 +02008143 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8144 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8145 return kvm_skip_emulated_instruction(vcpu);
8146 }
8147
8148 if (vmptr == vmx->nested.vmxon_ptr) {
8149 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8150 return kvm_skip_emulated_instruction(vcpu);
8151 }
8152
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008153 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008154 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008155
Jim Mattson587d7e722017-03-02 12:41:48 -08008156 kvm_vcpu_write_guest(vcpu,
8157 vmptr + offsetof(struct vmcs12, launch_state),
8158 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008159
Nadav Har'El27d6c862011-05-25 23:06:59 +03008160 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008161 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008162}
8163
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008164static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8165
8166/* Emulate the VMLAUNCH instruction */
8167static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8168{
8169 return nested_vmx_run(vcpu, true);
8170}
8171
8172/* Emulate the VMRESUME instruction */
8173static int handle_vmresume(struct kvm_vcpu *vcpu)
8174{
8175
8176 return nested_vmx_run(vcpu, false);
8177}
8178
Nadav Har'El49f705c2011-05-25 23:08:30 +03008179/*
8180 * Read a vmcs12 field. Since these can have varying lengths and we return
8181 * one type, we chose the biggest type (u64) and zero-extend the return value
8182 * to that size. Note that the caller, handle_vmread, might need to use only
8183 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8184 * 64-bit fields are to be returned).
8185 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008186static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8187 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008188{
8189 short offset = vmcs_field_to_offset(field);
8190 char *p;
8191
8192 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008193 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008194
8195 p = ((char *)(get_vmcs12(vcpu))) + offset;
8196
Jim Mattsond37f4262017-12-22 12:12:16 -08008197 switch (vmcs_field_width(field)) {
8198 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008199 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008200 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008201 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008202 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008203 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008204 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008205 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008206 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008207 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008208 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008209 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008210 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008211 WARN_ON(1);
8212 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008213 }
8214}
8215
Abel Gordon20b97fe2013-04-18 14:36:25 +03008216
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008217static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8218 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008219 short offset = vmcs_field_to_offset(field);
8220 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8221 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008222 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008223
Jim Mattsond37f4262017-12-22 12:12:16 -08008224 switch (vmcs_field_width(field)) {
8225 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008226 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008227 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008228 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008229 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008230 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008231 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008232 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008233 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008234 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008235 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008236 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008237 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008238 WARN_ON(1);
8239 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008240 }
8241
8242}
8243
Jim Mattsonf4160e42018-05-29 09:11:33 -07008244/*
8245 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8246 * they have been modified by the L1 guest. Note that the "read-only"
8247 * VM-exit information fields are actually writable if the vCPU is
8248 * configured to support "VMWRITE to any supported field in the VMCS."
8249 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008250static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8251{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008252 const u16 *fields[] = {
8253 shadow_read_write_fields,
8254 shadow_read_only_fields
8255 };
8256 const int max_fields[] = {
8257 max_shadow_read_write_fields,
8258 max_shadow_read_only_fields
8259 };
8260 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008261 unsigned long field;
8262 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008263 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008264
Jan Kiszka282da872014-10-08 18:05:39 +02008265 preempt_disable();
8266
Abel Gordon16f5b902013-04-18 14:38:25 +03008267 vmcs_load(shadow_vmcs);
8268
Jim Mattsonf4160e42018-05-29 09:11:33 -07008269 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8270 for (i = 0; i < max_fields[q]; i++) {
8271 field = fields[q][i];
8272 field_value = __vmcs_readl(field);
8273 vmcs12_write_any(&vmx->vcpu, field, field_value);
8274 }
8275 /*
8276 * Skip the VM-exit information fields if they are read-only.
8277 */
8278 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8279 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008280 }
8281
8282 vmcs_clear(shadow_vmcs);
8283 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008284
8285 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008286}
8287
Abel Gordonc3114422013-04-18 14:38:55 +03008288static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8289{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008290 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008291 shadow_read_write_fields,
8292 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008293 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008294 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008295 max_shadow_read_write_fields,
8296 max_shadow_read_only_fields
8297 };
8298 int i, q;
8299 unsigned long field;
8300 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008301 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008302
8303 vmcs_load(shadow_vmcs);
8304
Mathias Krausec2bae892013-06-26 20:36:21 +02008305 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008306 for (i = 0; i < max_fields[q]; i++) {
8307 field = fields[q][i];
8308 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008309 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008310 }
8311 }
8312
8313 vmcs_clear(shadow_vmcs);
8314 vmcs_load(vmx->loaded_vmcs->vmcs);
8315}
8316
Nadav Har'El49f705c2011-05-25 23:08:30 +03008317/*
8318 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8319 * used before) all generate the same failure when it is missing.
8320 */
8321static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8322{
8323 struct vcpu_vmx *vmx = to_vmx(vcpu);
8324 if (vmx->nested.current_vmptr == -1ull) {
8325 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008326 return 0;
8327 }
8328 return 1;
8329}
8330
8331static int handle_vmread(struct kvm_vcpu *vcpu)
8332{
8333 unsigned long field;
8334 u64 field_value;
8335 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8336 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8337 gva_t gva = 0;
8338
Kyle Hueyeb277562016-11-29 12:40:39 -08008339 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008340 return 1;
8341
Kyle Huey6affcbe2016-11-29 12:40:40 -08008342 if (!nested_vmx_check_vmcs12(vcpu))
8343 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008344
Nadav Har'El49f705c2011-05-25 23:08:30 +03008345 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008346 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008347 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008348 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008349 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008350 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008351 }
8352 /*
8353 * Now copy part of this value to register or memory, as requested.
8354 * Note that the number of bits actually copied is 32 or 64 depending
8355 * on the guest's mode (32 or 64 bit), not on the given field's length.
8356 */
8357 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008358 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008359 field_value);
8360 } else {
8361 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008362 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008363 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008364 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008365 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8366 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008367 }
8368
8369 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008370 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008371}
8372
8373
8374static int handle_vmwrite(struct kvm_vcpu *vcpu)
8375{
8376 unsigned long field;
8377 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008378 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008379 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8380 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008381
Nadav Har'El49f705c2011-05-25 23:08:30 +03008382 /* The value to write might be 32 or 64 bits, depending on L1's long
8383 * mode, and eventually we need to write that into a field of several
8384 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008385 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008386 * bits into the vmcs12 field.
8387 */
8388 u64 field_value = 0;
8389 struct x86_exception e;
8390
Kyle Hueyeb277562016-11-29 12:40:39 -08008391 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008392 return 1;
8393
Kyle Huey6affcbe2016-11-29 12:40:40 -08008394 if (!nested_vmx_check_vmcs12(vcpu))
8395 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008396
Nadav Har'El49f705c2011-05-25 23:08:30 +03008397 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008398 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008399 (((vmx_instruction_info) >> 3) & 0xf));
8400 else {
8401 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008402 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008403 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008404 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8405 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008406 kvm_inject_page_fault(vcpu, &e);
8407 return 1;
8408 }
8409 }
8410
8411
Nadav Amit27e6fb52014-06-18 17:19:26 +03008412 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008413 /*
8414 * If the vCPU supports "VMWRITE to any supported field in the
8415 * VMCS," then the "read-only" fields are actually read/write.
8416 */
8417 if (vmcs_field_readonly(field) &&
8418 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008419 nested_vmx_failValid(vcpu,
8420 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008421 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008422 }
8423
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008424 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008425 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008426 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008427 }
8428
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008429 switch (field) {
8430#define SHADOW_FIELD_RW(x) case x:
8431#include "vmx_shadow_fields.h"
8432 /*
8433 * The fields that can be updated by L1 without a vmexit are
8434 * always updated in the vmcs02, the others go down the slow
8435 * path of prepare_vmcs02.
8436 */
8437 break;
8438 default:
8439 vmx->nested.dirty_vmcs12 = true;
8440 break;
8441 }
8442
Nadav Har'El49f705c2011-05-25 23:08:30 +03008443 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008444 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008445}
8446
Jim Mattsona8bc2842016-11-30 12:03:44 -08008447static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8448{
8449 vmx->nested.current_vmptr = vmptr;
8450 if (enable_shadow_vmcs) {
8451 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8452 SECONDARY_EXEC_SHADOW_VMCS);
8453 vmcs_write64(VMCS_LINK_POINTER,
8454 __pa(vmx->vmcs01.shadow_vmcs));
8455 vmx->nested.sync_shadow_vmcs = true;
8456 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008457 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008458}
8459
Nadav Har'El63846662011-05-25 23:07:29 +03008460/* Emulate the VMPTRLD instruction */
8461static int handle_vmptrld(struct kvm_vcpu *vcpu)
8462{
8463 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008464 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008465
8466 if (!nested_vmx_check_permission(vcpu))
8467 return 1;
8468
Radim Krčmářcbf71272017-05-19 15:48:51 +02008469 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008470 return 1;
8471
Radim Krčmářcbf71272017-05-19 15:48:51 +02008472 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8473 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8474 return kvm_skip_emulated_instruction(vcpu);
8475 }
8476
8477 if (vmptr == vmx->nested.vmxon_ptr) {
8478 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8479 return kvm_skip_emulated_instruction(vcpu);
8480 }
8481
Nadav Har'El63846662011-05-25 23:07:29 +03008482 if (vmx->nested.current_vmptr != vmptr) {
8483 struct vmcs12 *new_vmcs12;
8484 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008485 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8486 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008487 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008488 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008489 }
8490 new_vmcs12 = kmap(page);
8491 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8492 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008493 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008494 nested_vmx_failValid(vcpu,
8495 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008496 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008497 }
Nadav Har'El63846662011-05-25 23:07:29 +03008498
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008499 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008500 /*
8501 * Load VMCS12 from guest memory since it is not already
8502 * cached.
8503 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008504 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8505 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008506 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008507
Jim Mattsona8bc2842016-11-30 12:03:44 -08008508 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008509 }
8510
8511 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008512 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008513}
8514
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008515/* Emulate the VMPTRST instruction */
8516static int handle_vmptrst(struct kvm_vcpu *vcpu)
8517{
8518 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8519 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8520 gva_t vmcs_gva;
8521 struct x86_exception e;
8522
8523 if (!nested_vmx_check_permission(vcpu))
8524 return 1;
8525
8526 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008527 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008528 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008529 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008530 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8531 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8532 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008533 kvm_inject_page_fault(vcpu, &e);
8534 return 1;
8535 }
8536 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008537 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008538}
8539
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008540/* Emulate the INVEPT instruction */
8541static int handle_invept(struct kvm_vcpu *vcpu)
8542{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008543 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008544 u32 vmx_instruction_info, types;
8545 unsigned long type;
8546 gva_t gva;
8547 struct x86_exception e;
8548 struct {
8549 u64 eptp, gpa;
8550 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008551
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008552 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008553 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008554 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008555 kvm_queue_exception(vcpu, UD_VECTOR);
8556 return 1;
8557 }
8558
8559 if (!nested_vmx_check_permission(vcpu))
8560 return 1;
8561
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008562 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008563 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008564
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008565 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008566
Jim Mattson85c856b2016-10-26 08:38:38 -07008567 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008568 nested_vmx_failValid(vcpu,
8569 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008570 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008571 }
8572
8573 /* According to the Intel VMX instruction reference, the memory
8574 * operand is read even if it isn't needed (e.g., for type==global)
8575 */
8576 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008577 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008578 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008579 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008580 kvm_inject_page_fault(vcpu, &e);
8581 return 1;
8582 }
8583
8584 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008585 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008586 /*
8587 * TODO: track mappings and invalidate
8588 * single context requests appropriately
8589 */
8590 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008591 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008592 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008593 nested_vmx_succeed(vcpu);
8594 break;
8595 default:
8596 BUG_ON(1);
8597 break;
8598 }
8599
Kyle Huey6affcbe2016-11-29 12:40:40 -08008600 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008601}
8602
Petr Matouseka642fc32014-09-23 20:22:30 +02008603static int handle_invvpid(struct kvm_vcpu *vcpu)
8604{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008605 struct vcpu_vmx *vmx = to_vmx(vcpu);
8606 u32 vmx_instruction_info;
8607 unsigned long type, types;
8608 gva_t gva;
8609 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008610 struct {
8611 u64 vpid;
8612 u64 gla;
8613 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008614
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008615 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008616 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008617 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008618 kvm_queue_exception(vcpu, UD_VECTOR);
8619 return 1;
8620 }
8621
8622 if (!nested_vmx_check_permission(vcpu))
8623 return 1;
8624
8625 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8626 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8627
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008628 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008629 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008630
Jim Mattson85c856b2016-10-26 08:38:38 -07008631 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008632 nested_vmx_failValid(vcpu,
8633 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008634 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008635 }
8636
8637 /* according to the intel vmx instruction reference, the memory
8638 * operand is read even if it isn't needed (e.g., for type==global)
8639 */
8640 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8641 vmx_instruction_info, false, &gva))
8642 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008643 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008644 kvm_inject_page_fault(vcpu, &e);
8645 return 1;
8646 }
Jim Mattson40352602017-06-28 09:37:37 -07008647 if (operand.vpid >> 16) {
8648 nested_vmx_failValid(vcpu,
8649 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8650 return kvm_skip_emulated_instruction(vcpu);
8651 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008652
8653 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008654 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008655 if (!operand.vpid ||
8656 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008657 nested_vmx_failValid(vcpu,
8658 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8659 return kvm_skip_emulated_instruction(vcpu);
8660 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008661 if (cpu_has_vmx_invvpid_individual_addr() &&
8662 vmx->nested.vpid02) {
8663 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8664 vmx->nested.vpid02, operand.gla);
8665 } else
8666 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8667 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008668 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008669 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008670 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008671 nested_vmx_failValid(vcpu,
8672 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008673 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008674 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008675 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008676 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008677 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008678 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008679 break;
8680 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008681 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008682 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008683 }
8684
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008685 nested_vmx_succeed(vcpu);
8686
Kyle Huey6affcbe2016-11-29 12:40:40 -08008687 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008688}
8689
Kai Huang843e4332015-01-28 10:54:28 +08008690static int handle_pml_full(struct kvm_vcpu *vcpu)
8691{
8692 unsigned long exit_qualification;
8693
8694 trace_kvm_pml_full(vcpu->vcpu_id);
8695
8696 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8697
8698 /*
8699 * PML buffer FULL happened while executing iret from NMI,
8700 * "blocked by NMI" bit has to be set before next VM entry.
8701 */
8702 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008703 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008704 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8705 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8706 GUEST_INTR_STATE_NMI);
8707
8708 /*
8709 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8710 * here.., and there's no userspace involvement needed for PML.
8711 */
8712 return 1;
8713}
8714
Yunhong Jiang64672c92016-06-13 14:19:59 -07008715static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8716{
8717 kvm_lapic_expired_hv_timer(vcpu);
8718 return 1;
8719}
8720
Bandan Das41ab9372017-08-03 15:54:43 -04008721static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8722{
8723 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008724 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8725
8726 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008727 switch (address & VMX_EPTP_MT_MASK) {
8728 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008729 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008730 return false;
8731 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008732 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008733 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008734 return false;
8735 break;
8736 default:
8737 return false;
8738 }
8739
David Hildenbrandbb97a012017-08-10 23:15:28 +02008740 /* only 4 levels page-walk length are valid */
8741 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008742 return false;
8743
8744 /* Reserved bits should not be set */
8745 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8746 return false;
8747
8748 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008749 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008750 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008751 return false;
8752 }
8753
8754 return true;
8755}
8756
8757static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8758 struct vmcs12 *vmcs12)
8759{
8760 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8761 u64 address;
8762 bool accessed_dirty;
8763 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8764
8765 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8766 !nested_cpu_has_ept(vmcs12))
8767 return 1;
8768
8769 if (index >= VMFUNC_EPTP_ENTRIES)
8770 return 1;
8771
8772
8773 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8774 &address, index * 8, 8))
8775 return 1;
8776
David Hildenbrandbb97a012017-08-10 23:15:28 +02008777 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008778
8779 /*
8780 * If the (L2) guest does a vmfunc to the currently
8781 * active ept pointer, we don't have to do anything else
8782 */
8783 if (vmcs12->ept_pointer != address) {
8784 if (!valid_ept_address(vcpu, address))
8785 return 1;
8786
8787 kvm_mmu_unload(vcpu);
8788 mmu->ept_ad = accessed_dirty;
8789 mmu->base_role.ad_disabled = !accessed_dirty;
8790 vmcs12->ept_pointer = address;
8791 /*
8792 * TODO: Check what's the correct approach in case
8793 * mmu reload fails. Currently, we just let the next
8794 * reload potentially fail
8795 */
8796 kvm_mmu_reload(vcpu);
8797 }
8798
8799 return 0;
8800}
8801
Bandan Das2a499e42017-08-03 15:54:41 -04008802static int handle_vmfunc(struct kvm_vcpu *vcpu)
8803{
Bandan Das27c42a12017-08-03 15:54:42 -04008804 struct vcpu_vmx *vmx = to_vmx(vcpu);
8805 struct vmcs12 *vmcs12;
8806 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8807
8808 /*
8809 * VMFUNC is only supported for nested guests, but we always enable the
8810 * secondary control for simplicity; for non-nested mode, fake that we
8811 * didn't by injecting #UD.
8812 */
8813 if (!is_guest_mode(vcpu)) {
8814 kvm_queue_exception(vcpu, UD_VECTOR);
8815 return 1;
8816 }
8817
8818 vmcs12 = get_vmcs12(vcpu);
8819 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8820 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008821
8822 switch (function) {
8823 case 0:
8824 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8825 goto fail;
8826 break;
8827 default:
8828 goto fail;
8829 }
8830 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008831
8832fail:
8833 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8834 vmcs_read32(VM_EXIT_INTR_INFO),
8835 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008836 return 1;
8837}
8838
Nadav Har'El0140cae2011-05-25 23:06:28 +03008839/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008840 * The exit handlers return 1 if the exit was handled fully and guest execution
8841 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8842 * to be done to userspace and return 0.
8843 */
Mathias Krause772e0312012-08-30 01:30:19 +02008844static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008845 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8846 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008847 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008848 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008849 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008850 [EXIT_REASON_CR_ACCESS] = handle_cr,
8851 [EXIT_REASON_DR_ACCESS] = handle_dr,
8852 [EXIT_REASON_CPUID] = handle_cpuid,
8853 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8854 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8855 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8856 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008857 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008858 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008859 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008860 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008861 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008862 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008863 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008864 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008865 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008866 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008867 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008868 [EXIT_REASON_VMOFF] = handle_vmoff,
8869 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008870 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8871 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008872 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008873 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008874 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008875 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008876 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008877 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008878 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8879 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008880 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8881 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008882 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008883 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008884 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008885 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008886 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008887 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008888 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008889 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008890 [EXIT_REASON_XSAVES] = handle_xsaves,
8891 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008892 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008893 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008894 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008895};
8896
8897static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008898 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008899
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008900static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8901 struct vmcs12 *vmcs12)
8902{
8903 unsigned long exit_qualification;
8904 gpa_t bitmap, last_bitmap;
8905 unsigned int port;
8906 int size;
8907 u8 b;
8908
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008909 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008910 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008911
8912 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8913
8914 port = exit_qualification >> 16;
8915 size = (exit_qualification & 7) + 1;
8916
8917 last_bitmap = (gpa_t)-1;
8918 b = -1;
8919
8920 while (size > 0) {
8921 if (port < 0x8000)
8922 bitmap = vmcs12->io_bitmap_a;
8923 else if (port < 0x10000)
8924 bitmap = vmcs12->io_bitmap_b;
8925 else
Joe Perches1d804d02015-03-30 16:46:09 -07008926 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008927 bitmap += (port & 0x7fff) / 8;
8928
8929 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008930 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008931 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008932 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008933 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008934
8935 port++;
8936 size--;
8937 last_bitmap = bitmap;
8938 }
8939
Joe Perches1d804d02015-03-30 16:46:09 -07008940 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008941}
8942
Nadav Har'El644d7112011-05-25 23:12:35 +03008943/*
8944 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8945 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8946 * disinterest in the current event (read or write a specific MSR) by using an
8947 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
8948 */
8949static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
8950 struct vmcs12 *vmcs12, u32 exit_reason)
8951{
8952 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
8953 gpa_t bitmap;
8954
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01008955 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07008956 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008957
8958 /*
8959 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
8960 * for the four combinations of read/write and low/high MSR numbers.
8961 * First we need to figure out which of the four to use:
8962 */
8963 bitmap = vmcs12->msr_bitmap;
8964 if (exit_reason == EXIT_REASON_MSR_WRITE)
8965 bitmap += 2048;
8966 if (msr_index >= 0xc0000000) {
8967 msr_index -= 0xc0000000;
8968 bitmap += 1024;
8969 }
8970
8971 /* Then read the msr_index'th bit from this bitmap: */
8972 if (msr_index < 1024*8) {
8973 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008974 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008975 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008976 return 1 & (b >> (msr_index & 7));
8977 } else
Joe Perches1d804d02015-03-30 16:46:09 -07008978 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008979}
8980
8981/*
8982 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8983 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8984 * intercept (via guest_host_mask etc.) the current event.
8985 */
8986static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8987 struct vmcs12 *vmcs12)
8988{
8989 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8990 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008991 int reg;
8992 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008993
8994 switch ((exit_qualification >> 4) & 3) {
8995 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02008996 reg = (exit_qualification >> 8) & 15;
8997 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008998 switch (cr) {
8999 case 0:
9000 if (vmcs12->cr0_guest_host_mask &
9001 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009002 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009003 break;
9004 case 3:
9005 if ((vmcs12->cr3_target_count >= 1 &&
9006 vmcs12->cr3_target_value0 == val) ||
9007 (vmcs12->cr3_target_count >= 2 &&
9008 vmcs12->cr3_target_value1 == val) ||
9009 (vmcs12->cr3_target_count >= 3 &&
9010 vmcs12->cr3_target_value2 == val) ||
9011 (vmcs12->cr3_target_count >= 4 &&
9012 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009013 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009014 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009015 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009016 break;
9017 case 4:
9018 if (vmcs12->cr4_guest_host_mask &
9019 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009020 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009021 break;
9022 case 8:
9023 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009024 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009025 break;
9026 }
9027 break;
9028 case 2: /* clts */
9029 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9030 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009031 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009032 break;
9033 case 1: /* mov from cr */
9034 switch (cr) {
9035 case 3:
9036 if (vmcs12->cpu_based_vm_exec_control &
9037 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009038 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009039 break;
9040 case 8:
9041 if (vmcs12->cpu_based_vm_exec_control &
9042 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009043 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009044 break;
9045 }
9046 break;
9047 case 3: /* lmsw */
9048 /*
9049 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9050 * cr0. Other attempted changes are ignored, with no exit.
9051 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009052 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009053 if (vmcs12->cr0_guest_host_mask & 0xe &
9054 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009055 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009056 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9057 !(vmcs12->cr0_read_shadow & 0x1) &&
9058 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009059 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009060 break;
9061 }
Joe Perches1d804d02015-03-30 16:46:09 -07009062 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009063}
9064
9065/*
9066 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9067 * should handle it ourselves in L0 (and then continue L2). Only call this
9068 * when in is_guest_mode (L2).
9069 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009070static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009071{
Nadav Har'El644d7112011-05-25 23:12:35 +03009072 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9073 struct vcpu_vmx *vmx = to_vmx(vcpu);
9074 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9075
Jim Mattson4f350c62017-09-14 16:31:44 -07009076 if (vmx->nested.nested_run_pending)
9077 return false;
9078
9079 if (unlikely(vmx->fail)) {
9080 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9081 vmcs_read32(VM_INSTRUCTION_ERROR));
9082 return true;
9083 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009084
David Matlackc9f04402017-08-01 14:00:40 -07009085 /*
9086 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009087 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9088 * Page). The CPU may write to these pages via their host
9089 * physical address while L2 is running, bypassing any
9090 * address-translation-based dirty tracking (e.g. EPT write
9091 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009092 *
9093 * Mark them dirty on every exit from L2 to prevent them from
9094 * getting out of sync with dirty tracking.
9095 */
9096 nested_mark_vmcs12_pages_dirty(vcpu);
9097
Jim Mattson4f350c62017-09-14 16:31:44 -07009098 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9099 vmcs_readl(EXIT_QUALIFICATION),
9100 vmx->idt_vectoring_info,
9101 intr_info,
9102 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9103 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009104
9105 switch (exit_reason) {
9106 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009107 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009108 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009109 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009110 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009111 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009112 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009113 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009114 else if (is_debug(intr_info) &&
9115 vcpu->guest_debug &
9116 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9117 return false;
9118 else if (is_breakpoint(intr_info) &&
9119 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9120 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009121 return vmcs12->exception_bitmap &
9122 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9123 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009124 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009125 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009126 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009127 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009128 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009129 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009130 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009131 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009132 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009133 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009134 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009135 case EXIT_REASON_HLT:
9136 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9137 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009138 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009139 case EXIT_REASON_INVLPG:
9140 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9141 case EXIT_REASON_RDPMC:
9142 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009143 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009144 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009145 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009146 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009147 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009148 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9149 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9150 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9151 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9152 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9153 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009154 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009155 /*
9156 * VMX instructions trap unconditionally. This allows L1 to
9157 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9158 */
Joe Perches1d804d02015-03-30 16:46:09 -07009159 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009160 case EXIT_REASON_CR_ACCESS:
9161 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9162 case EXIT_REASON_DR_ACCESS:
9163 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9164 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009165 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009166 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9167 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009168 case EXIT_REASON_MSR_READ:
9169 case EXIT_REASON_MSR_WRITE:
9170 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9171 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009172 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009173 case EXIT_REASON_MWAIT_INSTRUCTION:
9174 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009175 case EXIT_REASON_MONITOR_TRAP_FLAG:
9176 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009177 case EXIT_REASON_MONITOR_INSTRUCTION:
9178 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9179 case EXIT_REASON_PAUSE_INSTRUCTION:
9180 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9181 nested_cpu_has2(vmcs12,
9182 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9183 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009184 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009185 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009186 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009187 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009188 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009189 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009190 /*
9191 * The controls for "virtualize APIC accesses," "APIC-
9192 * register virtualization," and "virtual-interrupt
9193 * delivery" only come from vmcs12.
9194 */
Joe Perches1d804d02015-03-30 16:46:09 -07009195 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009196 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009197 /*
9198 * L0 always deals with the EPT violation. If nested EPT is
9199 * used, and the nested mmu code discovers that the address is
9200 * missing in the guest EPT table (EPT12), the EPT violation
9201 * will be injected with nested_ept_inject_page_fault()
9202 */
Joe Perches1d804d02015-03-30 16:46:09 -07009203 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009204 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009205 /*
9206 * L2 never uses directly L1's EPT, but rather L0's own EPT
9207 * table (shadow on EPT) or a merged EPT table that L0 built
9208 * (EPT on EPT). So any problems with the structure of the
9209 * table is L0's fault.
9210 */
Joe Perches1d804d02015-03-30 16:46:09 -07009211 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009212 case EXIT_REASON_INVPCID:
9213 return
9214 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9215 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009216 case EXIT_REASON_WBINVD:
9217 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9218 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009219 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009220 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9221 /*
9222 * This should never happen, since it is not possible to
9223 * set XSS to a non-zero value---neither in L1 nor in L2.
9224 * If if it were, XSS would have to be checked against
9225 * the XSS exit bitmap in vmcs12.
9226 */
9227 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009228 case EXIT_REASON_PREEMPTION_TIMER:
9229 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009230 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009231 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009232 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009233 case EXIT_REASON_VMFUNC:
9234 /* VM functions are emulated through L2->L0 vmexits. */
9235 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009236 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009237 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009238 }
9239}
9240
Paolo Bonzini7313c692017-07-27 10:31:25 +02009241static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9242{
9243 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9244
9245 /*
9246 * At this point, the exit interruption info in exit_intr_info
9247 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9248 * we need to query the in-kernel LAPIC.
9249 */
9250 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9251 if ((exit_intr_info &
9252 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9253 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9254 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9255 vmcs12->vm_exit_intr_error_code =
9256 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9257 }
9258
9259 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9260 vmcs_readl(EXIT_QUALIFICATION));
9261 return 1;
9262}
9263
Avi Kivity586f9602010-11-18 13:09:54 +02009264static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9265{
9266 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9267 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9268}
9269
Kai Huanga3eaa862015-11-04 13:46:05 +08009270static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009271{
Kai Huanga3eaa862015-11-04 13:46:05 +08009272 if (vmx->pml_pg) {
9273 __free_page(vmx->pml_pg);
9274 vmx->pml_pg = NULL;
9275 }
Kai Huang843e4332015-01-28 10:54:28 +08009276}
9277
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009278static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009279{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009280 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009281 u64 *pml_buf;
9282 u16 pml_idx;
9283
9284 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9285
9286 /* Do nothing if PML buffer is empty */
9287 if (pml_idx == (PML_ENTITY_NUM - 1))
9288 return;
9289
9290 /* PML index always points to next available PML buffer entity */
9291 if (pml_idx >= PML_ENTITY_NUM)
9292 pml_idx = 0;
9293 else
9294 pml_idx++;
9295
9296 pml_buf = page_address(vmx->pml_pg);
9297 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9298 u64 gpa;
9299
9300 gpa = pml_buf[pml_idx];
9301 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009302 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009303 }
9304
9305 /* reset PML index */
9306 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9307}
9308
9309/*
9310 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9311 * Called before reporting dirty_bitmap to userspace.
9312 */
9313static void kvm_flush_pml_buffers(struct kvm *kvm)
9314{
9315 int i;
9316 struct kvm_vcpu *vcpu;
9317 /*
9318 * We only need to kick vcpu out of guest mode here, as PML buffer
9319 * is flushed at beginning of all VMEXITs, and it's obvious that only
9320 * vcpus running in guest are possible to have unflushed GPAs in PML
9321 * buffer.
9322 */
9323 kvm_for_each_vcpu(i, vcpu, kvm)
9324 kvm_vcpu_kick(vcpu);
9325}
9326
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009327static void vmx_dump_sel(char *name, uint32_t sel)
9328{
9329 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009330 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009331 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9332 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9333 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9334}
9335
9336static void vmx_dump_dtsel(char *name, uint32_t limit)
9337{
9338 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9339 name, vmcs_read32(limit),
9340 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9341}
9342
9343static void dump_vmcs(void)
9344{
9345 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9346 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9347 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9348 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9349 u32 secondary_exec_control = 0;
9350 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009351 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009352 int i, n;
9353
9354 if (cpu_has_secondary_exec_ctrls())
9355 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9356
9357 pr_err("*** Guest State ***\n");
9358 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9359 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9360 vmcs_readl(CR0_GUEST_HOST_MASK));
9361 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9362 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9363 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9364 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9365 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9366 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009367 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9368 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9369 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9370 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009371 }
9372 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9373 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9374 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9375 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9376 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9377 vmcs_readl(GUEST_SYSENTER_ESP),
9378 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9379 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9380 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9381 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9382 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9383 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9384 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9385 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9386 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9387 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9388 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9389 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9390 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009391 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9392 efer, vmcs_read64(GUEST_IA32_PAT));
9393 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9394 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009395 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009396 if (cpu_has_load_perf_global_ctrl &&
9397 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009398 pr_err("PerfGlobCtl = 0x%016llx\n",
9399 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009400 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009401 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009402 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9403 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9404 vmcs_read32(GUEST_ACTIVITY_STATE));
9405 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9406 pr_err("InterruptStatus = %04x\n",
9407 vmcs_read16(GUEST_INTR_STATUS));
9408
9409 pr_err("*** Host State ***\n");
9410 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9411 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9412 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9413 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9414 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9415 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9416 vmcs_read16(HOST_TR_SELECTOR));
9417 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9418 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9419 vmcs_readl(HOST_TR_BASE));
9420 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9421 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9422 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9423 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9424 vmcs_readl(HOST_CR4));
9425 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9426 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9427 vmcs_read32(HOST_IA32_SYSENTER_CS),
9428 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9429 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009430 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9431 vmcs_read64(HOST_IA32_EFER),
9432 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009433 if (cpu_has_load_perf_global_ctrl &&
9434 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009435 pr_err("PerfGlobCtl = 0x%016llx\n",
9436 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009437
9438 pr_err("*** Control State ***\n");
9439 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9440 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9441 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9442 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9443 vmcs_read32(EXCEPTION_BITMAP),
9444 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9445 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9446 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9447 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9448 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9449 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9450 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9451 vmcs_read32(VM_EXIT_INTR_INFO),
9452 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9453 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9454 pr_err(" reason=%08x qualification=%016lx\n",
9455 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9456 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9457 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9458 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009459 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009460 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009461 pr_err("TSC Multiplier = 0x%016llx\n",
9462 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009463 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9464 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9465 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9466 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9467 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009468 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009469 n = vmcs_read32(CR3_TARGET_COUNT);
9470 for (i = 0; i + 1 < n; i += 4)
9471 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9472 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9473 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9474 if (i < n)
9475 pr_err("CR3 target%u=%016lx\n",
9476 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9477 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9478 pr_err("PLE Gap=%08x Window=%08x\n",
9479 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9480 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9481 pr_err("Virtual processor ID = 0x%04x\n",
9482 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9483}
9484
Avi Kivity6aa8b732006-12-10 02:21:36 -08009485/*
9486 * The guest has exited. See if we can fix it or if we need userspace
9487 * assistance.
9488 */
Avi Kivity851ba692009-08-24 11:10:17 +03009489static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009490{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009491 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009492 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009493 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009494
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009495 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9496
Kai Huang843e4332015-01-28 10:54:28 +08009497 /*
9498 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9499 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9500 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9501 * mode as if vcpus is in root mode, the PML buffer must has been
9502 * flushed already.
9503 */
9504 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009505 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009506
Mohammed Gamal80ced182009-09-01 12:48:18 +02009507 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009508 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009509 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009510
Paolo Bonzini7313c692017-07-27 10:31:25 +02009511 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9512 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009513
Mohammed Gamal51207022010-05-31 22:40:54 +03009514 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009515 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009516 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9517 vcpu->run->fail_entry.hardware_entry_failure_reason
9518 = exit_reason;
9519 return 0;
9520 }
9521
Avi Kivity29bd8a72007-09-10 17:27:03 +03009522 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009523 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9524 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009525 = vmcs_read32(VM_INSTRUCTION_ERROR);
9526 return 0;
9527 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009528
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009529 /*
9530 * Note:
9531 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9532 * delivery event since it indicates guest is accessing MMIO.
9533 * The vm-exit can be triggered again after return to guest that
9534 * will cause infinite loop.
9535 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009536 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009537 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009538 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009539 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009540 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9541 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9542 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009543 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009544 vcpu->run->internal.data[0] = vectoring_info;
9545 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009546 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9547 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9548 vcpu->run->internal.ndata++;
9549 vcpu->run->internal.data[3] =
9550 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9551 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009552 return 0;
9553 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009554
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009555 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009556 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9557 if (vmx_interrupt_allowed(vcpu)) {
9558 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9559 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9560 vcpu->arch.nmi_pending) {
9561 /*
9562 * This CPU don't support us in finding the end of an
9563 * NMI-blocked window if the guest runs with IRQs
9564 * disabled. So we pull the trigger after 1 s of
9565 * futile waiting, but inform the user about this.
9566 */
9567 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9568 "state on VCPU %d after 1 s timeout\n",
9569 __func__, vcpu->vcpu_id);
9570 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9571 }
9572 }
9573
Avi Kivity6aa8b732006-12-10 02:21:36 -08009574 if (exit_reason < kvm_vmx_max_exit_handlers
9575 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009576 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009577 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009578 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9579 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009580 kvm_queue_exception(vcpu, UD_VECTOR);
9581 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009582 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009583}
9584
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009585/*
9586 * Software based L1D cache flush which is used when microcode providing
9587 * the cache control MSR is not loaded.
9588 *
9589 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9590 * flush it is required to read in 64 KiB because the replacement algorithm
9591 * is not exactly LRU. This could be sized at runtime via topology
9592 * information but as all relevant affected CPUs have 32KiB L1D cache size
9593 * there is no point in doing so.
9594 */
9595#define L1D_CACHE_ORDER 4
9596static void *vmx_l1d_flush_pages;
9597
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009598static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009599{
9600 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009601 bool always;
9602
9603 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02009604 * This code is only executed when the the flush mode is 'cond' or
9605 * 'always'
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04009606 *
Thomas Gleixner2f055942018-07-13 16:23:17 +02009607 * If 'flush always', keep the flush bit set, otherwise clear
9608 * it. The flush bit gets set again either from vcpu_run() or from
9609 * one of the unsafe VMEXIT handlers.
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009610 */
Thomas Gleixner2f055942018-07-13 16:23:17 +02009611 always = vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009612 vcpu->arch.l1tf_flush_l1d = always;
9613
9614 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009615
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009616 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9617 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9618 return;
9619 }
9620
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009621 asm volatile(
9622 /* First ensure the pages are in the TLB */
9623 "xorl %%eax, %%eax\n"
9624 ".Lpopulate_tlb:\n\t"
9625 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9626 "addl $4096, %%eax\n\t"
9627 "cmpl %%eax, %[size]\n\t"
9628 "jne .Lpopulate_tlb\n\t"
9629 "xorl %%eax, %%eax\n\t"
9630 "cpuid\n\t"
9631 /* Now fill the cache */
9632 "xorl %%eax, %%eax\n"
9633 ".Lfill_cache:\n"
9634 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9635 "addl $64, %%eax\n\t"
9636 "cmpl %%eax, %[size]\n\t"
9637 "jne .Lfill_cache\n\t"
9638 "lfence\n"
9639 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9640 [size] "r" (size)
9641 : "eax", "ebx", "ecx", "edx");
9642}
9643
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009644static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009645{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009646 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9647
9648 if (is_guest_mode(vcpu) &&
9649 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9650 return;
9651
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009652 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009653 vmcs_write32(TPR_THRESHOLD, 0);
9654 return;
9655 }
9656
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009657 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009658}
9659
Jim Mattson8d860bb2018-05-09 16:56:05 -04009660static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009661{
9662 u32 sec_exec_control;
9663
Jim Mattson8d860bb2018-05-09 16:56:05 -04009664 if (!lapic_in_kernel(vcpu))
9665 return;
9666
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009667 /* Postpone execution until vmcs01 is the current VMCS. */
9668 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009669 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009670 return;
9671 }
9672
Paolo Bonzini35754c92015-07-29 12:05:37 +02009673 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009674 return;
9675
9676 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009677 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9678 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009679
Jim Mattson8d860bb2018-05-09 16:56:05 -04009680 switch (kvm_get_apic_mode(vcpu)) {
9681 case LAPIC_MODE_INVALID:
9682 WARN_ONCE(true, "Invalid local APIC state");
9683 case LAPIC_MODE_DISABLED:
9684 break;
9685 case LAPIC_MODE_XAPIC:
9686 if (flexpriority_enabled) {
9687 sec_exec_control |=
9688 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9689 vmx_flush_tlb(vcpu, true);
9690 }
9691 break;
9692 case LAPIC_MODE_X2APIC:
9693 if (cpu_has_vmx_virtualize_x2apic_mode())
9694 sec_exec_control |=
9695 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9696 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009697 }
9698 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9699
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009700 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009701}
9702
Tang Chen38b99172014-09-24 15:57:54 +08009703static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9704{
Jim Mattsonab5df312018-05-09 17:02:03 -04009705 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009706 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009707 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009708 }
Tang Chen38b99172014-09-24 15:57:54 +08009709}
9710
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009711static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009712{
9713 u16 status;
9714 u8 old;
9715
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009716 if (max_isr == -1)
9717 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009718
9719 status = vmcs_read16(GUEST_INTR_STATUS);
9720 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009721 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009722 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009723 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009724 vmcs_write16(GUEST_INTR_STATUS, status);
9725 }
9726}
9727
9728static void vmx_set_rvi(int vector)
9729{
9730 u16 status;
9731 u8 old;
9732
Wei Wang4114c272014-11-05 10:53:43 +08009733 if (vector == -1)
9734 vector = 0;
9735
Yang Zhangc7c9c562013-01-25 10:18:51 +08009736 status = vmcs_read16(GUEST_INTR_STATUS);
9737 old = (u8)status & 0xff;
9738 if ((u8)vector != old) {
9739 status &= ~0xff;
9740 status |= (u8)vector;
9741 vmcs_write16(GUEST_INTR_STATUS, status);
9742 }
9743}
9744
9745static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9746{
Liran Alon851c1a182017-12-24 18:12:56 +02009747 /*
9748 * When running L2, updating RVI is only relevant when
9749 * vmcs12 virtual-interrupt-delivery enabled.
9750 * However, it can be enabled only when L1 also
9751 * intercepts external-interrupts and in that case
9752 * we should not update vmcs02 RVI but instead intercept
9753 * interrupt. Therefore, do nothing when running L2.
9754 */
9755 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009756 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009757}
9758
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009759static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009760{
9761 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009762 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009763 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009764
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009765 WARN_ON(!vcpu->arch.apicv_active);
9766 if (pi_test_on(&vmx->pi_desc)) {
9767 pi_clear_on(&vmx->pi_desc);
9768 /*
9769 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9770 * But on x86 this is just a compiler barrier anyway.
9771 */
9772 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009773 max_irr_updated =
9774 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9775
9776 /*
9777 * If we are running L2 and L1 has a new pending interrupt
9778 * which can be injected, we should re-evaluate
9779 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009780 * If L1 intercepts external-interrupts, we should
9781 * exit from L2 to L1. Otherwise, interrupt should be
9782 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009783 */
Liran Alon851c1a182017-12-24 18:12:56 +02009784 if (is_guest_mode(vcpu) && max_irr_updated) {
9785 if (nested_exit_on_intr(vcpu))
9786 kvm_vcpu_exiting_guest_mode(vcpu);
9787 else
9788 kvm_make_request(KVM_REQ_EVENT, vcpu);
9789 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009790 } else {
9791 max_irr = kvm_lapic_find_highest_irr(vcpu);
9792 }
9793 vmx_hwapic_irr_update(vcpu, max_irr);
9794 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009795}
9796
Andrey Smetanin63086302015-11-10 15:36:32 +03009797static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009798{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009799 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009800 return;
9801
Yang Zhangc7c9c562013-01-25 10:18:51 +08009802 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9803 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9804 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9805 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9806}
9807
Paolo Bonzini967235d2016-12-19 14:03:45 +01009808static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9809{
9810 struct vcpu_vmx *vmx = to_vmx(vcpu);
9811
9812 pi_clear_on(&vmx->pi_desc);
9813 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9814}
9815
Avi Kivity51aa01d2010-07-20 14:31:20 +03009816static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009817{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009818 u32 exit_intr_info = 0;
9819 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009820
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009821 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9822 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009823 return;
9824
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009825 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9826 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9827 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009828
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009829 /* if exit due to PF check for async PF */
9830 if (is_page_fault(exit_intr_info))
9831 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9832
Andi Kleena0861c02009-06-08 17:37:09 +08009833 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009834 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9835 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009836 kvm_machine_check();
9837
Gleb Natapov20f65982009-05-11 13:35:55 +03009838 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009839 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009840 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009841 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009842 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009843 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009844}
Gleb Natapov20f65982009-05-11 13:35:55 +03009845
Yang Zhanga547c6d2013-04-11 19:25:10 +08009846static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9847{
9848 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9849
Yang Zhanga547c6d2013-04-11 19:25:10 +08009850 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9851 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9852 unsigned int vector;
9853 unsigned long entry;
9854 gate_desc *desc;
9855 struct vcpu_vmx *vmx = to_vmx(vcpu);
9856#ifdef CONFIG_X86_64
9857 unsigned long tmp;
9858#endif
9859
9860 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9861 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009862 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009863 asm volatile(
9864#ifdef CONFIG_X86_64
9865 "mov %%" _ASM_SP ", %[sp]\n\t"
9866 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9867 "push $%c[ss]\n\t"
9868 "push %[sp]\n\t"
9869#endif
9870 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009871 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009872 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009873 :
9874#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009875 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009876#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009877 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009878 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009879 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009880 [ss]"i"(__KERNEL_DS),
9881 [cs]"i"(__KERNEL_CS)
9882 );
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009883 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009884 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009885}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009886STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009887
Tom Lendackybc226f02018-05-10 22:06:39 +02009888static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009889{
Tom Lendackybc226f02018-05-10 22:06:39 +02009890 switch (index) {
9891 case MSR_IA32_SMBASE:
9892 /*
9893 * We cannot do SMM unless we can run the guest in big
9894 * real mode.
9895 */
9896 return enable_unrestricted_guest || emulate_invalid_guest_state;
9897 case MSR_AMD64_VIRT_SPEC_CTRL:
9898 /* This is AMD only. */
9899 return false;
9900 default:
9901 return true;
9902 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009903}
9904
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009905static bool vmx_mpx_supported(void)
9906{
9907 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9908 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9909}
9910
Wanpeng Li55412b22014-12-02 19:21:30 +08009911static bool vmx_xsaves_supported(void)
9912{
9913 return vmcs_config.cpu_based_2nd_exec_ctrl &
9914 SECONDARY_EXEC_XSAVES;
9915}
9916
Avi Kivity51aa01d2010-07-20 14:31:20 +03009917static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9918{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009919 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009920 bool unblock_nmi;
9921 u8 vector;
9922 bool idtv_info_valid;
9923
9924 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009925
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009926 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009927 if (vmx->loaded_vmcs->nmi_known_unmasked)
9928 return;
9929 /*
9930 * Can't use vmx->exit_intr_info since we're not sure what
9931 * the exit reason is.
9932 */
9933 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9934 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9935 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9936 /*
9937 * SDM 3: 27.7.1.2 (September 2008)
9938 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9939 * a guest IRET fault.
9940 * SDM 3: 23.2.2 (September 2008)
9941 * Bit 12 is undefined in any of the following cases:
9942 * If the VM exit sets the valid bit in the IDT-vectoring
9943 * information field.
9944 * If the VM exit is due to a double fault.
9945 */
9946 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
9947 vector != DF_VECTOR && !idtv_info_valid)
9948 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9949 GUEST_INTR_STATE_NMI);
9950 else
9951 vmx->loaded_vmcs->nmi_known_unmasked =
9952 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
9953 & GUEST_INTR_STATE_NMI);
9954 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
9955 vmx->loaded_vmcs->vnmi_blocked_time +=
9956 ktime_to_ns(ktime_sub(ktime_get(),
9957 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03009958}
9959
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009960static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03009961 u32 idt_vectoring_info,
9962 int instr_len_field,
9963 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03009964{
Avi Kivity51aa01d2010-07-20 14:31:20 +03009965 u8 vector;
9966 int type;
9967 bool idtv_info_valid;
9968
9969 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03009970
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009971 vcpu->arch.nmi_injected = false;
9972 kvm_clear_exception_queue(vcpu);
9973 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009974
9975 if (!idtv_info_valid)
9976 return;
9977
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009978 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03009979
Avi Kivity668f6122008-07-02 09:28:55 +03009980 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
9981 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009982
Gleb Natapov64a7ec02009-03-30 16:03:29 +03009983 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03009984 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009985 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03009986 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03009987 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03009988 * Clear bit "block by NMI" before VM entry if a NMI
9989 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03009990 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009991 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03009992 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03009993 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01009994 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03009995 /* fall through */
9996 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03009997 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03009998 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03009999 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010000 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010001 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010002 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010003 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010004 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010005 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010006 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010007 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010008 break;
10009 default:
10010 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010011 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010012}
10013
Avi Kivity83422e12010-07-20 14:43:23 +030010014static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10015{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010016 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010017 VM_EXIT_INSTRUCTION_LEN,
10018 IDT_VECTORING_ERROR_CODE);
10019}
10020
Avi Kivityb463a6f2010-07-20 15:06:17 +030010021static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10022{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010023 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010024 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10025 VM_ENTRY_INSTRUCTION_LEN,
10026 VM_ENTRY_EXCEPTION_ERROR_CODE);
10027
10028 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10029}
10030
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010031static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10032{
10033 int i, nr_msrs;
10034 struct perf_guest_switch_msr *msrs;
10035
10036 msrs = perf_guest_get_msrs(&nr_msrs);
10037
10038 if (!msrs)
10039 return;
10040
10041 for (i = 0; i < nr_msrs; i++)
10042 if (msrs[i].host == msrs[i].guest)
10043 clear_atomic_switch_msr(vmx, msrs[i].msr);
10044 else
10045 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010046 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010047}
10048
Jiang Biao33365e72016-11-03 15:03:37 +080010049static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010050{
10051 struct vcpu_vmx *vmx = to_vmx(vcpu);
10052 u64 tscl;
10053 u32 delta_tsc;
10054
10055 if (vmx->hv_deadline_tsc == -1)
10056 return;
10057
10058 tscl = rdtsc();
10059 if (vmx->hv_deadline_tsc > tscl)
10060 /* sure to be 32 bit only because checked on set_hv_timer */
10061 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10062 cpu_preemption_timer_multi);
10063 else
10064 delta_tsc = 0;
10065
10066 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10067}
10068
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010069static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010070{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010071 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010072 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010073
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010074 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010075 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010076 vmx->loaded_vmcs->soft_vnmi_blocked))
10077 vmx->loaded_vmcs->entry_time = ktime_get();
10078
Avi Kivity104f2262010-11-18 13:12:52 +020010079 /* Don't enter VMX if guest state is invalid, let the exit handler
10080 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010081 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010082 return;
10083
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010084 if (vmx->ple_window_dirty) {
10085 vmx->ple_window_dirty = false;
10086 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10087 }
10088
Abel Gordon012f83c2013-04-18 14:39:25 +030010089 if (vmx->nested.sync_shadow_vmcs) {
10090 copy_vmcs12_to_shadow(vmx);
10091 vmx->nested.sync_shadow_vmcs = false;
10092 }
10093
Avi Kivity104f2262010-11-18 13:12:52 +020010094 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10095 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10096 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10097 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10098
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010099 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010100 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010101 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010102 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010103 }
10104
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010105 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010106 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010107 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010108 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010109 }
10110
Avi Kivity104f2262010-11-18 13:12:52 +020010111 /* When single-stepping over STI and MOV SS, we must clear the
10112 * corresponding interruptibility bits in the guest state. Otherwise
10113 * vmentry fails as it then expects bit 14 (BS) in pending debug
10114 * exceptions being set, but that's not correct for the guest debugging
10115 * case. */
10116 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10117 vmx_set_interrupt_shadow(vcpu, 0);
10118
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010119 if (static_cpu_has(X86_FEATURE_PKU) &&
10120 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10121 vcpu->arch.pkru != vmx->host_pkru)
10122 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010123
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010124 atomic_switch_perf_msrs(vmx);
10125
Yunhong Jiang64672c92016-06-13 14:19:59 -070010126 vmx_arm_hv_timer(vcpu);
10127
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010128 /*
10129 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10130 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10131 * is no need to worry about the conditional branch over the wrmsr
10132 * being speculatively taken.
10133 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010134 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010135
Nadav Har'Eld462b812011-05-24 15:26:10 +030010136 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010137
10138 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10139 (unsigned long)&current_evmcs->host_rsp : 0;
10140
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010141 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
10142 if (vcpu->arch.l1tf_flush_l1d)
10143 vmx_l1d_flush(vcpu);
10144 }
10145
Avi Kivity104f2262010-11-18 13:12:52 +020010146 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010147 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010148 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10149 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10150 "push %%" _ASM_CX " \n\t"
10151 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010152 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010153 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010154 /* Avoid VMWRITE when Enlightened VMCS is in use */
10155 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10156 "jz 2f \n\t"
10157 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10158 "jmp 1f \n\t"
10159 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010160 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010161 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010162 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010163 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10164 "mov %%cr2, %%" _ASM_DX " \n\t"
10165 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010166 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010167 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010168 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010169 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010170 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010171 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010172 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10173 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10174 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10175 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10176 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10177 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010178#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010179 "mov %c[r8](%0), %%r8 \n\t"
10180 "mov %c[r9](%0), %%r9 \n\t"
10181 "mov %c[r10](%0), %%r10 \n\t"
10182 "mov %c[r11](%0), %%r11 \n\t"
10183 "mov %c[r12](%0), %%r12 \n\t"
10184 "mov %c[r13](%0), %%r13 \n\t"
10185 "mov %c[r14](%0), %%r14 \n\t"
10186 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010187#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010188 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010189
Avi Kivity6aa8b732006-12-10 02:21:36 -080010190 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010191 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010192 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010193 "jmp 2f \n\t"
10194 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10195 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010196 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010197 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010198 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010199 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010200 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10201 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10202 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10203 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10204 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10205 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10206 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010207#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010208 "mov %%r8, %c[r8](%0) \n\t"
10209 "mov %%r9, %c[r9](%0) \n\t"
10210 "mov %%r10, %c[r10](%0) \n\t"
10211 "mov %%r11, %c[r11](%0) \n\t"
10212 "mov %%r12, %c[r12](%0) \n\t"
10213 "mov %%r13, %c[r13](%0) \n\t"
10214 "mov %%r14, %c[r14](%0) \n\t"
10215 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010216 "xor %%r8d, %%r8d \n\t"
10217 "xor %%r9d, %%r9d \n\t"
10218 "xor %%r10d, %%r10d \n\t"
10219 "xor %%r11d, %%r11d \n\t"
10220 "xor %%r12d, %%r12d \n\t"
10221 "xor %%r13d, %%r13d \n\t"
10222 "xor %%r14d, %%r14d \n\t"
10223 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010224#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010225 "mov %%cr2, %%" _ASM_AX " \n\t"
10226 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010227
Jim Mattson0cb5b302018-01-03 14:31:38 -080010228 "xor %%eax, %%eax \n\t"
10229 "xor %%ebx, %%ebx \n\t"
10230 "xor %%esi, %%esi \n\t"
10231 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010232 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010233 ".pushsection .rodata \n\t"
10234 ".global vmx_return \n\t"
10235 "vmx_return: " _ASM_PTR " 2b \n\t"
10236 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010237 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010238 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010239 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010240 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010241 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10242 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10243 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10244 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10245 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10246 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10247 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010248#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010249 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10250 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10251 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10252 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10253 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10254 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10255 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10256 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010257#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010258 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10259 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010260 : "cc", "memory"
10261#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010262 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010263 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010264#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010265 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010266#endif
10267 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010268
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010269 /*
10270 * We do not use IBRS in the kernel. If this vCPU has used the
10271 * SPEC_CTRL MSR it may have left it on; save the value and
10272 * turn it off. This is much more efficient than blindly adding
10273 * it to the atomic save/restore list. Especially as the former
10274 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10275 *
10276 * For non-nested case:
10277 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10278 * save it.
10279 *
10280 * For nested case:
10281 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10282 * save it.
10283 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010284 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010285 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010286
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010287 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010288
David Woodhouse117cc7a2018-01-12 11:11:27 +000010289 /* Eliminate branch target predictions from guest mode */
10290 vmexit_fill_RSB();
10291
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010292 /* All fields are clean at this point */
10293 if (static_branch_unlikely(&enable_evmcs))
10294 current_evmcs->hv_clean_fields |=
10295 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10296
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010297 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010298 if (vmx->host_debugctlmsr)
10299 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010300
Avi Kivityaa67f602012-08-01 16:48:03 +030010301#ifndef CONFIG_X86_64
10302 /*
10303 * The sysexit path does not restore ds/es, so we must set them to
10304 * a reasonable value ourselves.
10305 *
10306 * We can't defer this to vmx_load_host_state() since that function
10307 * may be executed in interrupt context, which saves and restore segments
10308 * around it, nullifying its effect.
10309 */
10310 loadsegment(ds, __USER_DS);
10311 loadsegment(es, __USER_DS);
10312#endif
10313
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010314 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010315 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010316 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010317 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010318 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010319 vcpu->arch.regs_dirty = 0;
10320
Gleb Natapove0b890d2013-09-25 12:51:33 +030010321 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010322 * eager fpu is enabled if PKEY is supported and CR4 is switched
10323 * back on host, so it is safe to read guest PKRU from current
10324 * XSAVE.
10325 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010326 if (static_cpu_has(X86_FEATURE_PKU) &&
10327 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10328 vcpu->arch.pkru = __read_pkru();
10329 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010330 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010331 }
10332
Gleb Natapove0b890d2013-09-25 12:51:33 +030010333 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010334 vmx->idt_vectoring_info = 0;
10335
10336 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10337 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10338 return;
10339
10340 vmx->loaded_vmcs->launched = 1;
10341 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010342
Avi Kivity51aa01d2010-07-20 14:31:20 +030010343 vmx_complete_atomic_exit(vmx);
10344 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010345 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010346}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010347STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010348
Sean Christopherson434a1e92018-03-20 12:17:18 -070010349static struct kvm *vmx_vm_alloc(void)
10350{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010351 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010352 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010353}
10354
10355static void vmx_vm_free(struct kvm *kvm)
10356{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010357 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010358}
10359
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010360static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010361{
10362 struct vcpu_vmx *vmx = to_vmx(vcpu);
10363 int cpu;
10364
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010365 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010366 return;
10367
10368 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010369 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010370 vmx_vcpu_put(vcpu);
10371 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010372 put_cpu();
10373}
10374
Jim Mattson2f1fe812016-07-08 15:36:06 -070010375/*
10376 * Ensure that the current vmcs of the logical processor is the
10377 * vmcs01 of the vcpu before calling free_nested().
10378 */
10379static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10380{
10381 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010382
Christoffer Dallec7660c2017-12-04 21:35:23 +010010383 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010384 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010385 free_nested(vmx);
10386 vcpu_put(vcpu);
10387}
10388
Avi Kivity6aa8b732006-12-10 02:21:36 -080010389static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10390{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010391 struct vcpu_vmx *vmx = to_vmx(vcpu);
10392
Kai Huang843e4332015-01-28 10:54:28 +080010393 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010394 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010395 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010396 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010397 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010398 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010399 kfree(vmx->guest_msrs);
10400 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010401 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010402}
10403
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010404static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010405{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010406 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010407 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010408 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010409 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010410
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010411 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010412 return ERR_PTR(-ENOMEM);
10413
Wanpeng Li991e7a02015-09-16 17:30:05 +080010414 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010415
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010416 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10417 if (err)
10418 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010419
Peter Feiner4e595162016-07-07 14:49:58 -070010420 err = -ENOMEM;
10421
10422 /*
10423 * If PML is turned on, failure on enabling PML just results in failure
10424 * of creating the vcpu, therefore we can simplify PML logic (by
10425 * avoiding dealing with cases, such as enabling PML partially on vcpus
10426 * for the guest, etc.
10427 */
10428 if (enable_pml) {
10429 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10430 if (!vmx->pml_pg)
10431 goto uninit_vcpu;
10432 }
10433
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010434 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010435 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10436 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010437
Peter Feiner4e595162016-07-07 14:49:58 -070010438 if (!vmx->guest_msrs)
10439 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010440
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010441 err = alloc_loaded_vmcs(&vmx->vmcs01);
10442 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010443 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010444
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010445 msr_bitmap = vmx->vmcs01.msr_bitmap;
10446 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10447 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10448 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10449 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10450 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10451 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10452 vmx->msr_bitmap_mode = 0;
10453
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010454 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010455 cpu = get_cpu();
10456 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010457 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010458 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010459 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010460 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010461 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010462 err = alloc_apic_access_page(kvm);
10463 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010464 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010465 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010466
Sean Christophersone90008d2018-03-05 12:04:37 -080010467 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010468 err = init_rmode_identity_map(kvm);
10469 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010470 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010471 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010472
Wanpeng Li5c614b32015-10-13 09:18:36 -070010473 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010474 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10475 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010476 vmx->nested.vpid02 = allocate_vpid();
10477 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010478
Wincy Van705699a2015-02-03 23:58:17 +080010479 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010480 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010481
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010482 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10483
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010484 /*
10485 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10486 * or POSTED_INTR_WAKEUP_VECTOR.
10487 */
10488 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10489 vmx->pi_desc.sn = 1;
10490
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010491 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010492
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010493free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010494 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010495 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010496free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010497 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010498free_pml:
10499 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010500uninit_vcpu:
10501 kvm_vcpu_uninit(&vmx->vcpu);
10502free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010503 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010504 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010505 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010506}
10507
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010508#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
10509
Wanpeng Lib31c1142018-03-12 04:53:04 -070010510static int vmx_vm_init(struct kvm *kvm)
10511{
10512 if (!ple_gap)
10513 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010514
10515 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
10516 if (nosmt) {
10517 pr_err(L1TF_MSG);
10518 return -EOPNOTSUPP;
10519 }
10520 pr_warn(L1TF_MSG);
10521 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010522 return 0;
10523}
10524
Yang, Sheng002c7f72007-07-31 14:23:01 +030010525static void __init vmx_check_processor_compat(void *rtn)
10526{
10527 struct vmcs_config vmcs_conf;
10528
10529 *(int *)rtn = 0;
10530 if (setup_vmcs_config(&vmcs_conf) < 0)
10531 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010532 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010533 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10534 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10535 smp_processor_id());
10536 *(int *)rtn = -EIO;
10537 }
10538}
10539
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010540static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010541{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010542 u8 cache;
10543 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010544
Sheng Yang522c68c2009-04-27 20:35:43 +080010545 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010546 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010547 * 2. EPT with VT-d:
10548 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010549 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010550 * b. VT-d with snooping control feature: snooping control feature of
10551 * VT-d engine can guarantee the cache correctness. Just set it
10552 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010553 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010554 * consistent with host MTRR
10555 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010556 if (is_mmio) {
10557 cache = MTRR_TYPE_UNCACHABLE;
10558 goto exit;
10559 }
10560
10561 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010562 ipat = VMX_EPT_IPAT_BIT;
10563 cache = MTRR_TYPE_WRBACK;
10564 goto exit;
10565 }
10566
10567 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10568 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010569 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010570 cache = MTRR_TYPE_WRBACK;
10571 else
10572 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010573 goto exit;
10574 }
10575
Xiao Guangrongff536042015-06-15 16:55:22 +080010576 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010577
10578exit:
10579 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010580}
10581
Sheng Yang17cc3932010-01-05 19:02:27 +080010582static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010583{
Sheng Yang878403b2010-01-05 19:02:29 +080010584 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10585 return PT_DIRECTORY_LEVEL;
10586 else
10587 /* For shadow and EPT supported 1GB page */
10588 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010589}
10590
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010591static void vmcs_set_secondary_exec_control(u32 new_ctl)
10592{
10593 /*
10594 * These bits in the secondary execution controls field
10595 * are dynamic, the others are mostly based on the hypervisor
10596 * architecture and the guest's CPUID. Do not touch the
10597 * dynamic bits.
10598 */
10599 u32 mask =
10600 SECONDARY_EXEC_SHADOW_VMCS |
10601 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010602 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10603 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010604
10605 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10606
10607 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10608 (new_ctl & ~mask) | (cur_ctl & mask));
10609}
10610
David Matlack8322ebb2016-11-29 18:14:09 -080010611/*
10612 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10613 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10614 */
10615static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10616{
10617 struct vcpu_vmx *vmx = to_vmx(vcpu);
10618 struct kvm_cpuid_entry2 *entry;
10619
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010620 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10621 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010622
10623#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10624 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010625 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010626} while (0)
10627
10628 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10629 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10630 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10631 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10632 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10633 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10634 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10635 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10636 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10637 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10638 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10639 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10640 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10641 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10642 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10643
10644 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10645 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10646 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10647 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10648 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010649 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010650
10651#undef cr4_fixed1_update
10652}
10653
Sheng Yang0e851882009-12-18 16:48:46 +080010654static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10655{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010656 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010657
Paolo Bonzini80154d72017-08-24 13:55:35 +020010658 if (cpu_has_secondary_exec_ctrls()) {
10659 vmx_compute_secondary_exec_control(vmx);
10660 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010661 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010662
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010663 if (nested_vmx_allowed(vcpu))
10664 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10665 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10666 else
10667 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10668 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010669
10670 if (nested_vmx_allowed(vcpu))
10671 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010672}
10673
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010674static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10675{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010676 if (func == 1 && nested)
10677 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010678}
10679
Yang Zhang25d92082013-08-06 12:00:32 +030010680static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10681 struct x86_exception *fault)
10682{
Jan Kiszka533558b2014-01-04 18:47:20 +010010683 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010684 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010685 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010686 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010687
Bandan Dasc5f983f2017-05-05 15:25:14 -040010688 if (vmx->nested.pml_full) {
10689 exit_reason = EXIT_REASON_PML_FULL;
10690 vmx->nested.pml_full = false;
10691 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10692 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010693 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010694 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010695 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010696
10697 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010698 vmcs12->guest_physical_address = fault->address;
10699}
10700
Peter Feiner995f00a2017-06-30 17:26:32 -070010701static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10702{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010703 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010704}
10705
Nadav Har'El155a97a2013-08-05 11:07:16 +030010706/* Callbacks for nested_ept_init_mmu_context: */
10707
10708static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10709{
10710 /* return the page table to be shadowed - in our case, EPT12 */
10711 return get_vmcs12(vcpu)->ept_pointer;
10712}
10713
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010714static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010715{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010716 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010717 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010718 return 1;
10719
10720 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010721 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010722 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010723 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010724 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010725 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10726 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10727 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10728
10729 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010730 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010731}
10732
10733static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10734{
10735 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10736}
10737
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010738static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10739 u16 error_code)
10740{
10741 bool inequality, bit;
10742
10743 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10744 inequality =
10745 (error_code & vmcs12->page_fault_error_code_mask) !=
10746 vmcs12->page_fault_error_code_match;
10747 return inequality ^ bit;
10748}
10749
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010750static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10751 struct x86_exception *fault)
10752{
10753 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10754
10755 WARN_ON(!is_guest_mode(vcpu));
10756
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010757 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10758 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010759 vmcs12->vm_exit_intr_error_code = fault->error_code;
10760 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10761 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10762 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10763 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010764 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010765 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010766 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010767}
10768
Paolo Bonzinic9923842017-12-13 14:16:30 +010010769static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10770 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010771
10772static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010773 struct vmcs12 *vmcs12)
10774{
10775 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010776 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010777 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010778
10779 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010780 /*
10781 * Translate L1 physical address to host physical
10782 * address for vmcs02. Keep the page pinned, so this
10783 * physical address remains valid. We keep a reference
10784 * to it so we can release it later.
10785 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010786 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010787 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010788 vmx->nested.apic_access_page = NULL;
10789 }
10790 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010791 /*
10792 * If translation failed, no matter: This feature asks
10793 * to exit when accessing the given address, and if it
10794 * can never be accessed, this feature won't do
10795 * anything anyway.
10796 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010797 if (!is_error_page(page)) {
10798 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010799 hpa = page_to_phys(vmx->nested.apic_access_page);
10800 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10801 } else {
10802 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10803 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10804 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010805 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010806
10807 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010808 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010809 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010810 vmx->nested.virtual_apic_page = NULL;
10811 }
10812 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010813
10814 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010815 * If translation failed, VM entry will fail because
10816 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10817 * Failing the vm entry is _not_ what the processor
10818 * does but it's basically the only possibility we
10819 * have. We could still enter the guest if CR8 load
10820 * exits are enabled, CR8 store exits are enabled, and
10821 * virtualize APIC access is disabled; in this case
10822 * the processor would never use the TPR shadow and we
10823 * could simply clear the bit from the execution
10824 * control. But such a configuration is useless, so
10825 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010826 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010827 if (!is_error_page(page)) {
10828 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010829 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10830 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10831 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010832 }
10833
Wincy Van705699a2015-02-03 23:58:17 +080010834 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010835 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10836 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010837 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010838 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010839 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010840 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10841 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010842 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010843 vmx->nested.pi_desc_page = page;
10844 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010845 vmx->nested.pi_desc =
10846 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10847 (unsigned long)(vmcs12->posted_intr_desc_addr &
10848 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010849 vmcs_write64(POSTED_INTR_DESC_ADDR,
10850 page_to_phys(vmx->nested.pi_desc_page) +
10851 (unsigned long)(vmcs12->posted_intr_desc_addr &
10852 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010853 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010854 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010855 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10856 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010857 else
10858 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10859 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010860}
10861
Jan Kiszkaf41245002014-03-07 20:03:13 +010010862static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10863{
10864 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10865 struct vcpu_vmx *vmx = to_vmx(vcpu);
10866
10867 if (vcpu->arch.virtual_tsc_khz == 0)
10868 return;
10869
10870 /* Make sure short timeouts reliably trigger an immediate vmexit.
10871 * hrtimer_start does not guarantee this. */
10872 if (preemption_timeout <= 1) {
10873 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10874 return;
10875 }
10876
10877 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10878 preemption_timeout *= 1000000;
10879 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10880 hrtimer_start(&vmx->nested.preemption_timer,
10881 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10882}
10883
Jim Mattson56a20512017-07-06 16:33:06 -070010884static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10885 struct vmcs12 *vmcs12)
10886{
10887 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10888 return 0;
10889
10890 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10891 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10892 return -EINVAL;
10893
10894 return 0;
10895}
10896
Wincy Van3af18d92015-02-03 23:49:31 +080010897static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10898 struct vmcs12 *vmcs12)
10899{
Wincy Van3af18d92015-02-03 23:49:31 +080010900 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10901 return 0;
10902
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010903 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010904 return -EINVAL;
10905
10906 return 0;
10907}
10908
Jim Mattson712b12d2017-08-24 13:24:47 -070010909static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10910 struct vmcs12 *vmcs12)
10911{
10912 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10913 return 0;
10914
10915 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10916 return -EINVAL;
10917
10918 return 0;
10919}
10920
Wincy Van3af18d92015-02-03 23:49:31 +080010921/*
10922 * Merge L0's and L1's MSR bitmap, return false to indicate that
10923 * we do not use the hardware.
10924 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010925static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10926 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010927{
Wincy Van82f0dd42015-02-03 23:57:18 +080010928 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010929 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010930 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010931 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010932 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010933 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010934 *
10935 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10936 * ensures that we do not accidentally generate an L02 MSR bitmap
10937 * from the L12 MSR bitmap that is too permissive.
10938 * 2. That L1 or L2s have actually used the MSR. This avoids
10939 * unnecessarily merging of the bitmap if the MSR is unused. This
10940 * works properly because we only update the L01 MSR bitmap lazily.
10941 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10942 * updated to reflect this when L1 (or its L2s) actually write to
10943 * the MSR.
10944 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010945 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10946 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080010947
Paolo Bonzinic9923842017-12-13 14:16:30 +010010948 /* Nothing to do if the MSR bitmap is not in use. */
10949 if (!cpu_has_vmx_msr_bitmap() ||
10950 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10951 return false;
10952
Ashok Raj15d45072018-02-01 22:59:43 +010010953 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010954 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080010955 return false;
10956
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010957 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
10958 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080010959 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010010960
Radim Krčmářd048c092016-08-08 20:16:22 +020010961 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010010962 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
10963 /*
10964 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
10965 * just lets the processor take the value from the virtual-APIC page;
10966 * take those 256 bits directly from the L1 bitmap.
10967 */
10968 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10969 unsigned word = msr / BITS_PER_LONG;
10970 msr_bitmap_l0[word] = msr_bitmap_l1[word];
10971 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080010972 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010010973 } else {
10974 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
10975 unsigned word = msr / BITS_PER_LONG;
10976 msr_bitmap_l0[word] = ~0;
10977 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
10978 }
10979 }
10980
10981 nested_vmx_disable_intercept_for_msr(
10982 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010983 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010984 MSR_TYPE_W);
10985
10986 if (nested_cpu_has_vid(vmcs12)) {
10987 nested_vmx_disable_intercept_for_msr(
10988 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010989 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010990 MSR_TYPE_W);
10991 nested_vmx_disable_intercept_for_msr(
10992 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010010993 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010010994 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080010995 }
Ashok Raj15d45072018-02-01 22:59:43 +010010996
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010997 if (spec_ctrl)
10998 nested_vmx_disable_intercept_for_msr(
10999 msr_bitmap_l1, msr_bitmap_l0,
11000 MSR_IA32_SPEC_CTRL,
11001 MSR_TYPE_R | MSR_TYPE_W);
11002
Ashok Raj15d45072018-02-01 22:59:43 +010011003 if (pred_cmd)
11004 nested_vmx_disable_intercept_for_msr(
11005 msr_bitmap_l1, msr_bitmap_l0,
11006 MSR_IA32_PRED_CMD,
11007 MSR_TYPE_W);
11008
Wincy Vanf2b93282015-02-03 23:56:03 +080011009 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011010 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011011
11012 return true;
11013}
11014
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011015static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11016 struct vmcs12 *vmcs12)
11017{
11018 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11019 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11020 return -EINVAL;
11021 else
11022 return 0;
11023}
11024
Wincy Vanf2b93282015-02-03 23:56:03 +080011025static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11026 struct vmcs12 *vmcs12)
11027{
Wincy Van82f0dd42015-02-03 23:57:18 +080011028 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011029 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011030 !nested_cpu_has_vid(vmcs12) &&
11031 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011032 return 0;
11033
11034 /*
11035 * If virtualize x2apic mode is enabled,
11036 * virtualize apic access must be disabled.
11037 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011038 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11039 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011040 return -EINVAL;
11041
Wincy Van608406e2015-02-03 23:57:51 +080011042 /*
11043 * If virtual interrupt delivery is enabled,
11044 * we must exit on external interrupts.
11045 */
11046 if (nested_cpu_has_vid(vmcs12) &&
11047 !nested_exit_on_intr(vcpu))
11048 return -EINVAL;
11049
Wincy Van705699a2015-02-03 23:58:17 +080011050 /*
11051 * bits 15:8 should be zero in posted_intr_nv,
11052 * the descriptor address has been already checked
11053 * in nested_get_vmcs12_pages.
11054 */
11055 if (nested_cpu_has_posted_intr(vmcs12) &&
11056 (!nested_cpu_has_vid(vmcs12) ||
11057 !nested_exit_intr_ack_set(vcpu) ||
11058 vmcs12->posted_intr_nv & 0xff00))
11059 return -EINVAL;
11060
Wincy Vanf2b93282015-02-03 23:56:03 +080011061 /* tpr shadow is needed by all apicv features. */
11062 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11063 return -EINVAL;
11064
11065 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011066}
11067
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011068static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11069 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011070 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011071{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011072 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011073 u64 count, addr;
11074
11075 if (vmcs12_read_any(vcpu, count_field, &count) ||
11076 vmcs12_read_any(vcpu, addr_field, &addr)) {
11077 WARN_ON(1);
11078 return -EINVAL;
11079 }
11080 if (count == 0)
11081 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011082 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011083 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11084 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011085 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011086 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11087 addr_field, maxphyaddr, count, addr);
11088 return -EINVAL;
11089 }
11090 return 0;
11091}
11092
11093static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11094 struct vmcs12 *vmcs12)
11095{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011096 if (vmcs12->vm_exit_msr_load_count == 0 &&
11097 vmcs12->vm_exit_msr_store_count == 0 &&
11098 vmcs12->vm_entry_msr_load_count == 0)
11099 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011100 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011101 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011102 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011103 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011104 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011105 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011106 return -EINVAL;
11107 return 0;
11108}
11109
Bandan Dasc5f983f2017-05-05 15:25:14 -040011110static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11111 struct vmcs12 *vmcs12)
11112{
11113 u64 address = vmcs12->pml_address;
11114 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11115
11116 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11117 if (!nested_cpu_has_ept(vmcs12) ||
11118 !IS_ALIGNED(address, 4096) ||
11119 address >> maxphyaddr)
11120 return -EINVAL;
11121 }
11122
11123 return 0;
11124}
11125
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011126static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11127 struct vmx_msr_entry *e)
11128{
11129 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011130 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011131 return -EINVAL;
11132 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11133 e->index == MSR_IA32_UCODE_REV)
11134 return -EINVAL;
11135 if (e->reserved != 0)
11136 return -EINVAL;
11137 return 0;
11138}
11139
11140static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11141 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011142{
11143 if (e->index == MSR_FS_BASE ||
11144 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011145 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11146 nested_vmx_msr_check_common(vcpu, e))
11147 return -EINVAL;
11148 return 0;
11149}
11150
11151static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11152 struct vmx_msr_entry *e)
11153{
11154 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11155 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011156 return -EINVAL;
11157 return 0;
11158}
11159
11160/*
11161 * Load guest's/host's msr at nested entry/exit.
11162 * return 0 for success, entry index for failure.
11163 */
11164static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11165{
11166 u32 i;
11167 struct vmx_msr_entry e;
11168 struct msr_data msr;
11169
11170 msr.host_initiated = false;
11171 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011172 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11173 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011174 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011175 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11176 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011177 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011178 }
11179 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011180 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011181 "%s check failed (%u, 0x%x, 0x%x)\n",
11182 __func__, i, e.index, e.reserved);
11183 goto fail;
11184 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011185 msr.index = e.index;
11186 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011187 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011188 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011189 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11190 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011191 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011192 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011193 }
11194 return 0;
11195fail:
11196 return i + 1;
11197}
11198
11199static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11200{
11201 u32 i;
11202 struct vmx_msr_entry e;
11203
11204 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011205 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011206 if (kvm_vcpu_read_guest(vcpu,
11207 gpa + i * sizeof(e),
11208 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011209 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011210 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11211 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011212 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011213 }
11214 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011215 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011216 "%s check failed (%u, 0x%x, 0x%x)\n",
11217 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011218 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011219 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011220 msr_info.host_initiated = false;
11221 msr_info.index = e.index;
11222 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011223 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011224 "%s cannot read MSR (%u, 0x%x)\n",
11225 __func__, i, e.index);
11226 return -EINVAL;
11227 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011228 if (kvm_vcpu_write_guest(vcpu,
11229 gpa + i * sizeof(e) +
11230 offsetof(struct vmx_msr_entry, value),
11231 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011232 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011233 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011234 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011235 return -EINVAL;
11236 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011237 }
11238 return 0;
11239}
11240
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011241static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11242{
11243 unsigned long invalid_mask;
11244
11245 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11246 return (val & invalid_mask) == 0;
11247}
11248
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011249/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011250 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11251 * emulating VM entry into a guest with EPT enabled.
11252 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11253 * is assigned to entry_failure_code on failure.
11254 */
11255static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011256 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011257{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011258 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011259 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011260 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11261 return 1;
11262 }
11263
11264 /*
11265 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11266 * must not be dereferenced.
11267 */
11268 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11269 !nested_ept) {
11270 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11271 *entry_failure_code = ENTRY_FAIL_PDPTE;
11272 return 1;
11273 }
11274 }
11275
11276 vcpu->arch.cr3 = cr3;
11277 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11278 }
11279
11280 kvm_mmu_reset_context(vcpu);
11281 return 0;
11282}
11283
Jim Mattson6514dc32018-04-26 16:09:12 -070011284static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011285{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011286 struct vcpu_vmx *vmx = to_vmx(vcpu);
11287
11288 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11289 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11290 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11291 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11292 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11293 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11294 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11295 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11296 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11297 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11298 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11299 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11300 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11301 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11302 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11303 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11304 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11305 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11306 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11307 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11308 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11309 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11310 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11311 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11312 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11313 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11314 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11315 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11316 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11317 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11318 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011319
11320 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11321 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11322 vmcs12->guest_pending_dbg_exceptions);
11323 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11324 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11325
11326 if (nested_cpu_has_xsaves(vmcs12))
11327 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11328 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11329
11330 if (cpu_has_vmx_posted_intr())
11331 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11332
11333 /*
11334 * Whether page-faults are trapped is determined by a combination of
11335 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11336 * If enable_ept, L0 doesn't care about page faults and we should
11337 * set all of these to L1's desires. However, if !enable_ept, L0 does
11338 * care about (at least some) page faults, and because it is not easy
11339 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11340 * to exit on each and every L2 page fault. This is done by setting
11341 * MASK=MATCH=0 and (see below) EB.PF=1.
11342 * Note that below we don't need special code to set EB.PF beyond the
11343 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11344 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11345 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11346 */
11347 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11348 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11349 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11350 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11351
11352 /* All VMFUNCs are currently emulated through L0 vmexits. */
11353 if (cpu_has_vmx_vmfunc())
11354 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11355
11356 if (cpu_has_vmx_apicv()) {
11357 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11358 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11359 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11360 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11361 }
11362
11363 /*
11364 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11365 * Some constant fields are set here by vmx_set_constant_host_state().
11366 * Other fields are different per CPU, and will be set later when
11367 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11368 */
11369 vmx_set_constant_host_state(vmx);
11370
11371 /*
11372 * Set the MSR load/store lists to match L0's settings.
11373 */
11374 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011375 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11376 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11377 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11378 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011379
11380 set_cr4_guest_host_mask(vmx);
11381
11382 if (vmx_mpx_supported())
11383 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11384
11385 if (enable_vpid) {
11386 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11387 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11388 else
11389 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11390 }
11391
11392 /*
11393 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11394 */
11395 if (enable_ept) {
11396 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11397 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11398 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11399 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11400 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011401
11402 if (cpu_has_vmx_msr_bitmap())
11403 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011404}
11405
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011406/*
11407 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11408 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011409 * 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 +030011410 * guest in a way that will both be appropriate to L1's requests, and our
11411 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11412 * function also has additional necessary side-effects, like setting various
11413 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011414 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11415 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011416 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011417static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011418 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011419{
11420 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011421 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011422
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011423 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011424 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011425 vmx->nested.dirty_vmcs12 = false;
11426 }
11427
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011428 /*
11429 * First, the fields that are shadowed. This must be kept in sync
11430 * with vmx_shadow_fields.h.
11431 */
11432
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011433 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011434 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011435 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011436 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11437 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011438
11439 /*
11440 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11441 * HOST_FS_BASE, HOST_GS_BASE.
11442 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011443
Jim Mattson6514dc32018-04-26 16:09:12 -070011444 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011445 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011446 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11447 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11448 } else {
11449 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11450 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11451 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011452 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011453 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11454 vmcs12->vm_entry_intr_info_field);
11455 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11456 vmcs12->vm_entry_exception_error_code);
11457 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11458 vmcs12->vm_entry_instruction_len);
11459 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11460 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011461 vmx->loaded_vmcs->nmi_known_unmasked =
11462 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011463 } else {
11464 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11465 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011466 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011467
Jan Kiszkaf41245002014-03-07 20:03:13 +010011468 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011469
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011470 /* Preemption timer setting is only taken from vmcs01. */
11471 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11472 exec_control |= vmcs_config.pin_based_exec_ctrl;
11473 if (vmx->hv_deadline_tsc == -1)
11474 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11475
11476 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011477 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011478 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11479 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011480 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011481 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011482 }
Wincy Van705699a2015-02-03 23:58:17 +080011483
Jan Kiszkaf41245002014-03-07 20:03:13 +010011484 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011485
Jan Kiszkaf41245002014-03-07 20:03:13 +010011486 vmx->nested.preemption_timer_expired = false;
11487 if (nested_cpu_has_preemption_timer(vmcs12))
11488 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011489
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011490 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011491 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011492
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011493 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011494 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011495 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011496 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011497 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011498 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011499 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11500 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011501 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011502 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11503 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11504 ~SECONDARY_EXEC_ENABLE_PML;
11505 exec_control |= vmcs12_exec_ctrl;
11506 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011507
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011508 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011509 vmcs_write16(GUEST_INTR_STATUS,
11510 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011511
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011512 /*
11513 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11514 * nested_get_vmcs12_pages will either fix it up or
11515 * remove the VM execution control.
11516 */
11517 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11518 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11519
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011520 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11521 }
11522
Jim Mattson83bafef2016-10-04 10:48:38 -070011523 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011524 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11525 * entry, but only if the current (host) sp changed from the value
11526 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11527 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11528 * here we just force the write to happen on entry.
11529 */
11530 vmx->host_rsp = 0;
11531
11532 exec_control = vmx_exec_control(vmx); /* L0's desires */
11533 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11534 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11535 exec_control &= ~CPU_BASED_TPR_SHADOW;
11536 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011537
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011538 /*
11539 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11540 * nested_get_vmcs12_pages can't fix it up, the illegal value
11541 * will result in a VM entry failure.
11542 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011543 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011544 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011545 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011546 } else {
11547#ifdef CONFIG_X86_64
11548 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11549 CPU_BASED_CR8_STORE_EXITING;
11550#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011551 }
11552
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011553 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011554 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11555 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011556 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011557 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11558 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11559
11560 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11561
11562 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11563 * bitwise-or of what L1 wants to trap for L2, and what we want to
11564 * trap. Note that CR0.TS also needs updating - we do this later.
11565 */
11566 update_exception_bitmap(vcpu);
11567 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11568 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11569
Nadav Har'El8049d652013-08-05 11:07:06 +030011570 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11571 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11572 * bits are further modified by vmx_set_efer() below.
11573 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011574 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011575
11576 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11577 * emulated by vmx_set_efer(), below.
11578 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011579 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011580 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11581 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011582 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11583
Jim Mattson6514dc32018-04-26 16:09:12 -070011584 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011585 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011586 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011587 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011588 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011589 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011590 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011591
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011592 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11593
Peter Feinerc95ba922016-08-17 09:36:47 -070011594 if (kvm_has_tsc_control)
11595 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011596
11597 if (enable_vpid) {
11598 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011599 * There is no direct mapping between vpid02 and vpid12, the
11600 * vpid02 is per-vCPU for L0 and reused while the value of
11601 * vpid12 is changed w/ one invvpid during nested vmentry.
11602 * The vpid12 is allocated by L1 for L2, so it will not
11603 * influence global bitmap(for vpid01 and vpid02 allocation)
11604 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011605 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011606 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011607 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11608 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011609 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011610 }
11611 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011612 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011613 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011614 }
11615
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011616 if (enable_pml) {
11617 /*
11618 * Conceptually we want to copy the PML address and index from
11619 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11620 * since we always flush the log on each vmexit, this happens
11621 * to be equivalent to simply resetting the fields in vmcs02.
11622 */
11623 ASSERT(vmx->pml_pg);
11624 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11625 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11626 }
11627
Nadav Har'El155a97a2013-08-05 11:07:16 +030011628 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011629 if (nested_ept_init_mmu_context(vcpu)) {
11630 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11631 return 1;
11632 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011633 } else if (nested_cpu_has2(vmcs12,
11634 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011635 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011636 }
11637
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011638 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011639 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11640 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011641 * The CR0_READ_SHADOW is what L2 should have expected to read given
11642 * the specifications by L1; It's not enough to take
11643 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11644 * have more bits than L1 expected.
11645 */
11646 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11647 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11648
11649 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11650 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11651
Jim Mattson6514dc32018-04-26 16:09:12 -070011652 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011653 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011654 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11655 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11656 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11657 else
11658 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11659 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11660 vmx_set_efer(vcpu, vcpu->arch.efer);
11661
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011662 /*
11663 * Guest state is invalid and unrestricted guest is disabled,
11664 * which means L1 attempted VMEntry to L2 with invalid state.
11665 * Fail the VMEntry.
11666 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011667 if (vmx->emulation_required) {
11668 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011669 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011670 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011671
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011672 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011673 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011674 entry_failure_code))
11675 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011676
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011677 if (!enable_ept)
11678 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11679
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011680 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11681 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011682 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011683}
11684
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011685static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11686{
11687 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11688 nested_cpu_has_virtual_nmis(vmcs12))
11689 return -EINVAL;
11690
11691 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11692 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11693 return -EINVAL;
11694
11695 return 0;
11696}
11697
Jim Mattsonca0bde22016-11-30 12:03:46 -080011698static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11699{
11700 struct vcpu_vmx *vmx = to_vmx(vcpu);
11701
11702 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11703 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11704 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11705
Jim Mattson56a20512017-07-06 16:33:06 -070011706 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11707 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11708
Jim Mattsonca0bde22016-11-30 12:03:46 -080011709 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11710 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11711
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011712 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11713 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11714
Jim Mattson712b12d2017-08-24 13:24:47 -070011715 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11716 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11717
Jim Mattsonca0bde22016-11-30 12:03:46 -080011718 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11719 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11720
11721 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11722 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11723
Bandan Dasc5f983f2017-05-05 15:25:14 -040011724 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11725 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11726
Jim Mattsonca0bde22016-11-30 12:03:46 -080011727 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011728 vmx->nested.msrs.procbased_ctls_low,
11729 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011730 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11731 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011732 vmx->nested.msrs.secondary_ctls_low,
11733 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011734 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011735 vmx->nested.msrs.pinbased_ctls_low,
11736 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011737 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011738 vmx->nested.msrs.exit_ctls_low,
11739 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011740 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011741 vmx->nested.msrs.entry_ctls_low,
11742 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011743 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11744
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011745 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011746 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11747
Bandan Das41ab9372017-08-03 15:54:43 -040011748 if (nested_cpu_has_vmfunc(vmcs12)) {
11749 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011750 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011751 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11752
11753 if (nested_cpu_has_eptp_switching(vmcs12)) {
11754 if (!nested_cpu_has_ept(vmcs12) ||
11755 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11756 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11757 }
11758 }
Bandan Das27c42a12017-08-03 15:54:42 -040011759
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070011760 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11761 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11762
Jim Mattsonca0bde22016-11-30 12:03:46 -080011763 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11764 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11765 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11766 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11767
11768 return 0;
11769}
11770
11771static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11772 u32 *exit_qual)
11773{
11774 bool ia32e;
11775
11776 *exit_qual = ENTRY_FAIL_DEFAULT;
11777
11778 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11779 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11780 return 1;
11781
11782 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11783 vmcs12->vmcs_link_pointer != -1ull) {
11784 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11785 return 1;
11786 }
11787
11788 /*
11789 * If the load IA32_EFER VM-entry control is 1, the following checks
11790 * are performed on the field for the IA32_EFER MSR:
11791 * - Bits reserved in the IA32_EFER MSR must be 0.
11792 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11793 * the IA-32e mode guest VM-exit control. It must also be identical
11794 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11795 * CR0.PG) is 1.
11796 */
11797 if (to_vmx(vcpu)->nested.nested_run_pending &&
11798 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11799 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11800 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11801 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11802 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11803 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11804 return 1;
11805 }
11806
11807 /*
11808 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11809 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11810 * the values of the LMA and LME bits in the field must each be that of
11811 * the host address-space size VM-exit control.
11812 */
11813 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11814 ia32e = (vmcs12->vm_exit_controls &
11815 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11816 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11817 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11818 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11819 return 1;
11820 }
11821
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011822 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11823 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11824 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11825 return 1;
11826
Jim Mattsonca0bde22016-11-30 12:03:46 -080011827 return 0;
11828}
11829
Jim Mattson6514dc32018-04-26 16:09:12 -070011830static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011831{
11832 struct vcpu_vmx *vmx = to_vmx(vcpu);
11833 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011834 u32 msr_entry_idx;
11835 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011836 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011837
Jim Mattson858e25c2016-11-30 12:03:47 -080011838 enter_guest_mode(vcpu);
11839
11840 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11841 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11842
Jim Mattsonde3a0022017-11-27 17:22:25 -060011843 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011844 vmx_segment_cache_clear(vmx);
11845
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011846 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11847 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11848
11849 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011850 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011851 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011852
11853 nested_get_vmcs12_pages(vcpu, vmcs12);
11854
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011855 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011856 msr_entry_idx = nested_vmx_load_msr(vcpu,
11857 vmcs12->vm_entry_msr_load_addr,
11858 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011859 if (msr_entry_idx)
11860 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011861
Jim Mattson858e25c2016-11-30 12:03:47 -080011862 /*
11863 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11864 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11865 * returned as far as L1 is concerned. It will only return (and set
11866 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11867 */
11868 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011869
11870fail:
11871 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11872 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11873 leave_guest_mode(vcpu);
11874 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11875 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11876 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011877}
11878
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011879/*
11880 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11881 * for running an L2 nested guest.
11882 */
11883static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11884{
11885 struct vmcs12 *vmcs12;
11886 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011887 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011888 u32 exit_qual;
11889 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011890
Kyle Hueyeb277562016-11-29 12:40:39 -080011891 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011892 return 1;
11893
Kyle Hueyeb277562016-11-29 12:40:39 -080011894 if (!nested_vmx_check_vmcs12(vcpu))
11895 goto out;
11896
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011897 vmcs12 = get_vmcs12(vcpu);
11898
Abel Gordon012f83c2013-04-18 14:39:25 +030011899 if (enable_shadow_vmcs)
11900 copy_shadow_to_vmcs12(vmx);
11901
Nadav Har'El7c177932011-05-25 23:12:04 +030011902 /*
11903 * The nested entry process starts with enforcing various prerequisites
11904 * on vmcs12 as required by the Intel SDM, and act appropriately when
11905 * they fail: As the SDM explains, some conditions should cause the
11906 * instruction to fail, while others will cause the instruction to seem
11907 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11908 * To speed up the normal (success) code path, we should avoid checking
11909 * for misconfigurations which will anyway be caught by the processor
11910 * when using the merged vmcs02.
11911 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011912 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11913 nested_vmx_failValid(vcpu,
11914 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11915 goto out;
11916 }
11917
Nadav Har'El7c177932011-05-25 23:12:04 +030011918 if (vmcs12->launch_state == launch) {
11919 nested_vmx_failValid(vcpu,
11920 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11921 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011922 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011923 }
11924
Jim Mattsonca0bde22016-11-30 12:03:46 -080011925 ret = check_vmentry_prereqs(vcpu, vmcs12);
11926 if (ret) {
11927 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011928 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011929 }
11930
Nadav Har'El7c177932011-05-25 23:12:04 +030011931 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011932 * After this point, the trap flag no longer triggers a singlestep trap
11933 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11934 * This is not 100% correct; for performance reasons, we delegate most
11935 * of the checks on host state to the processor. If those fail,
11936 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011937 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011938 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011939
Jim Mattsonca0bde22016-11-30 12:03:46 -080011940 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11941 if (ret) {
11942 nested_vmx_entry_failure(vcpu, vmcs12,
11943 EXIT_REASON_INVALID_STATE, exit_qual);
11944 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011945 }
11946
11947 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030011948 * We're finally done with prerequisite checking, and can start with
11949 * the nested entry.
11950 */
11951
Jim Mattson6514dc32018-04-26 16:09:12 -070011952 vmx->nested.nested_run_pending = 1;
11953 ret = enter_vmx_non_root_mode(vcpu);
11954 if (ret) {
11955 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080011956 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070011957 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011958
Paolo Bonzinic595cee2018-07-02 13:07:14 +020011959 /* Hide L1D cache contents from the nested guest. */
11960 vmx->vcpu.arch.l1tf_flush_l1d = true;
11961
Chao Gao135a06c2018-02-11 10:06:30 +080011962 /*
11963 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
11964 * by event injection, halt vcpu.
11965 */
11966 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070011967 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
11968 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060011969 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070011970 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011971 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080011972
11973out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080011974 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011975}
11976
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011977/*
11978 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
11979 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
11980 * This function returns the new value we should put in vmcs12.guest_cr0.
11981 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
11982 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
11983 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
11984 * didn't trap the bit, because if L1 did, so would L0).
11985 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
11986 * been modified by L2, and L1 knows it. So just leave the old value of
11987 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
11988 * isn't relevant, because if L0 traps this bit it can set it to anything.
11989 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
11990 * changed these bits, and therefore they need to be updated, but L0
11991 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
11992 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
11993 */
11994static inline unsigned long
11995vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11996{
11997 return
11998 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
11999 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12000 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12001 vcpu->arch.cr0_guest_owned_bits));
12002}
12003
12004static inline unsigned long
12005vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12006{
12007 return
12008 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12009 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12010 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12011 vcpu->arch.cr4_guest_owned_bits));
12012}
12013
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012014static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12015 struct vmcs12 *vmcs12)
12016{
12017 u32 idt_vectoring;
12018 unsigned int nr;
12019
Wanpeng Li664f8e22017-08-24 03:35:09 -070012020 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012021 nr = vcpu->arch.exception.nr;
12022 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12023
12024 if (kvm_exception_is_soft(nr)) {
12025 vmcs12->vm_exit_instruction_len =
12026 vcpu->arch.event_exit_inst_len;
12027 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12028 } else
12029 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12030
12031 if (vcpu->arch.exception.has_error_code) {
12032 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12033 vmcs12->idt_vectoring_error_code =
12034 vcpu->arch.exception.error_code;
12035 }
12036
12037 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012038 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012039 vmcs12->idt_vectoring_info_field =
12040 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012041 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012042 nr = vcpu->arch.interrupt.nr;
12043 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12044
12045 if (vcpu->arch.interrupt.soft) {
12046 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12047 vmcs12->vm_entry_instruction_len =
12048 vcpu->arch.event_exit_inst_len;
12049 } else
12050 idt_vectoring |= INTR_TYPE_EXT_INTR;
12051
12052 vmcs12->idt_vectoring_info_field = idt_vectoring;
12053 }
12054}
12055
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012056static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12057{
12058 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012059 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012060 bool block_nested_events =
12061 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012062
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012063 if (vcpu->arch.exception.pending &&
12064 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012065 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012066 return -EBUSY;
12067 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012068 return 0;
12069 }
12070
Jan Kiszkaf41245002014-03-07 20:03:13 +010012071 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12072 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012073 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012074 return -EBUSY;
12075 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12076 return 0;
12077 }
12078
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012079 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012080 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012081 return -EBUSY;
12082 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12083 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12084 INTR_INFO_VALID_MASK, 0);
12085 /*
12086 * The NMI-triggered VM exit counts as injection:
12087 * clear this one and block further NMIs.
12088 */
12089 vcpu->arch.nmi_pending = 0;
12090 vmx_set_nmi_mask(vcpu, true);
12091 return 0;
12092 }
12093
12094 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12095 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012096 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012097 return -EBUSY;
12098 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012099 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012100 }
12101
David Hildenbrand6342c502017-01-25 11:58:58 +010012102 vmx_complete_nested_posted_interrupt(vcpu);
12103 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012104}
12105
Jan Kiszkaf41245002014-03-07 20:03:13 +010012106static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12107{
12108 ktime_t remaining =
12109 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12110 u64 value;
12111
12112 if (ktime_to_ns(remaining) <= 0)
12113 return 0;
12114
12115 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12116 do_div(value, 1000000);
12117 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12118}
12119
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012120/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012121 * Update the guest state fields of vmcs12 to reflect changes that
12122 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12123 * VM-entry controls is also updated, since this is really a guest
12124 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012125 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012126static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012127{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012128 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12129 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12130
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012131 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12132 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12133 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12134
12135 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12136 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12137 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12138 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12139 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12140 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12141 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12142 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12143 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12144 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12145 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12146 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12147 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12148 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12149 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12150 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12151 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12152 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12153 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12154 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12155 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12156 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12157 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12158 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12159 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12160 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12161 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12162 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12163 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12164 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12165 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12166 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12167 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12168 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12169 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12170 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12171
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012172 vmcs12->guest_interruptibility_info =
12173 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12174 vmcs12->guest_pending_dbg_exceptions =
12175 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012176 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12177 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12178 else
12179 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012180
Jan Kiszkaf41245002014-03-07 20:03:13 +010012181 if (nested_cpu_has_preemption_timer(vmcs12)) {
12182 if (vmcs12->vm_exit_controls &
12183 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12184 vmcs12->vmx_preemption_timer_value =
12185 vmx_get_preemption_timer_value(vcpu);
12186 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12187 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012188
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012189 /*
12190 * In some cases (usually, nested EPT), L2 is allowed to change its
12191 * own CR3 without exiting. If it has changed it, we must keep it.
12192 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12193 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12194 *
12195 * Additionally, restore L2's PDPTR to vmcs12.
12196 */
12197 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012198 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012199 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12200 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12201 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12202 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12203 }
12204
Jim Mattsond281e132017-06-01 12:44:46 -070012205 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012206
Wincy Van608406e2015-02-03 23:57:51 +080012207 if (nested_cpu_has_vid(vmcs12))
12208 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12209
Jan Kiszkac18911a2013-03-13 16:06:41 +010012210 vmcs12->vm_entry_controls =
12211 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012212 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012213
Jan Kiszka2996fca2014-06-16 13:59:43 +020012214 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12215 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12216 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12217 }
12218
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012219 /* TODO: These cannot have changed unless we have MSR bitmaps and
12220 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012221 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012222 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012223 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12224 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012225 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12226 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12227 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012228 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012229 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012230}
12231
12232/*
12233 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12234 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12235 * and this function updates it to reflect the changes to the guest state while
12236 * L2 was running (and perhaps made some exits which were handled directly by L0
12237 * without going back to L1), and to reflect the exit reason.
12238 * Note that we do not have to copy here all VMCS fields, just those that
12239 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12240 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12241 * which already writes to vmcs12 directly.
12242 */
12243static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12244 u32 exit_reason, u32 exit_intr_info,
12245 unsigned long exit_qualification)
12246{
12247 /* update guest state fields: */
12248 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012249
12250 /* update exit information fields: */
12251
Jan Kiszka533558b2014-01-04 18:47:20 +010012252 vmcs12->vm_exit_reason = exit_reason;
12253 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012254 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012255
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012256 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012257 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12258 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12259
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012260 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012261 vmcs12->launch_state = 1;
12262
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012263 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12264 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012265 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012266
12267 /*
12268 * Transfer the event that L0 or L1 may wanted to inject into
12269 * L2 to IDT_VECTORING_INFO_FIELD.
12270 */
12271 vmcs12_save_pending_event(vcpu, vmcs12);
12272 }
12273
12274 /*
12275 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12276 * preserved above and would only end up incorrectly in L1.
12277 */
12278 vcpu->arch.nmi_injected = false;
12279 kvm_clear_exception_queue(vcpu);
12280 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012281}
12282
Wanpeng Li5af41572017-11-05 16:54:49 -080012283static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12284 struct vmcs12 *vmcs12)
12285{
12286 u32 entry_failure_code;
12287
12288 nested_ept_uninit_mmu_context(vcpu);
12289
12290 /*
12291 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12292 * couldn't have changed.
12293 */
12294 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12295 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12296
12297 if (!enable_ept)
12298 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12299}
12300
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012301/*
12302 * A part of what we need to when the nested L2 guest exits and we want to
12303 * run its L1 parent, is to reset L1's guest state to the host state specified
12304 * in vmcs12.
12305 * This function is to be called not only on normal nested exit, but also on
12306 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12307 * Failures During or After Loading Guest State").
12308 * This function should be called when the active VMCS is L1's (vmcs01).
12309 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012310static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12311 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012312{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012313 struct kvm_segment seg;
12314
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012315 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12316 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012317 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012318 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12319 else
12320 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12321 vmx_set_efer(vcpu, vcpu->arch.efer);
12322
12323 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12324 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012325 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012326 /*
12327 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012328 * actually changed, because vmx_set_cr0 refers to efer set above.
12329 *
12330 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12331 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012332 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012333 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020012334 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012335
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012336 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012337 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012338 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012339
Wanpeng Li5af41572017-11-05 16:54:49 -080012340 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012341
Liran Alon6f1e03b2018-05-22 17:16:14 +030012342 /*
12343 * If vmcs01 don't use VPID, CPU flushes TLB on every
12344 * VMEntry/VMExit. Thus, no need to flush TLB.
12345 *
12346 * If vmcs12 uses VPID, TLB entries populated by L2 are
12347 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12348 * with vmx->vpid. Thus, no need to flush TLB.
12349 *
12350 * Therefore, flush TLB only in case vmcs01 uses VPID and
12351 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12352 * are both tagged with vmx->vpid.
12353 */
12354 if (enable_vpid &&
12355 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012356 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012357 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012358
12359 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12360 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12361 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12362 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12363 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012364 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12365 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012366
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012367 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12368 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12369 vmcs_write64(GUEST_BNDCFGS, 0);
12370
Jan Kiszka44811c02013-08-04 17:17:27 +020012371 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012372 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012373 vcpu->arch.pat = vmcs12->host_ia32_pat;
12374 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012375 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12376 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12377 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012378
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012379 /* Set L1 segment info according to Intel SDM
12380 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12381 seg = (struct kvm_segment) {
12382 .base = 0,
12383 .limit = 0xFFFFFFFF,
12384 .selector = vmcs12->host_cs_selector,
12385 .type = 11,
12386 .present = 1,
12387 .s = 1,
12388 .g = 1
12389 };
12390 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12391 seg.l = 1;
12392 else
12393 seg.db = 1;
12394 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12395 seg = (struct kvm_segment) {
12396 .base = 0,
12397 .limit = 0xFFFFFFFF,
12398 .type = 3,
12399 .present = 1,
12400 .s = 1,
12401 .db = 1,
12402 .g = 1
12403 };
12404 seg.selector = vmcs12->host_ds_selector;
12405 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12406 seg.selector = vmcs12->host_es_selector;
12407 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12408 seg.selector = vmcs12->host_ss_selector;
12409 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12410 seg.selector = vmcs12->host_fs_selector;
12411 seg.base = vmcs12->host_fs_base;
12412 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12413 seg.selector = vmcs12->host_gs_selector;
12414 seg.base = vmcs12->host_gs_base;
12415 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12416 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012417 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012418 .limit = 0x67,
12419 .selector = vmcs12->host_tr_selector,
12420 .type = 11,
12421 .present = 1
12422 };
12423 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12424
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012425 kvm_set_dr(vcpu, 7, 0x400);
12426 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012427
Wincy Van3af18d92015-02-03 23:49:31 +080012428 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012429 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012430
Wincy Vanff651cb2014-12-11 08:52:58 +030012431 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12432 vmcs12->vm_exit_msr_load_count))
12433 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012434}
12435
12436/*
12437 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12438 * and modify vmcs12 to make it see what it would expect to see there if
12439 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12440 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012441static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12442 u32 exit_intr_info,
12443 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012444{
12445 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012446 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12447
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012448 /* trying to cancel vmlaunch/vmresume is a bug */
12449 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12450
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012451 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012452 * The only expected VM-instruction error is "VM entry with
12453 * invalid control field(s)." Anything else indicates a
12454 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012455 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012456 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12457 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12458
12459 leave_guest_mode(vcpu);
12460
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012461 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12462 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12463
Jim Mattson4f350c62017-09-14 16:31:44 -070012464 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012465 if (exit_reason == -1)
12466 sync_vmcs12(vcpu, vmcs12);
12467 else
12468 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12469 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012470
12471 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12472 vmcs12->vm_exit_msr_store_count))
12473 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012474 }
12475
Jim Mattson4f350c62017-09-14 16:31:44 -070012476 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012477 vm_entry_controls_reset_shadow(vmx);
12478 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012479 vmx_segment_cache_clear(vmx);
12480
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012481 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012482 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12483 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012484 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012485 if (vmx->hv_deadline_tsc == -1)
12486 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12487 PIN_BASED_VMX_PREEMPTION_TIMER);
12488 else
12489 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12490 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012491 if (kvm_has_tsc_control)
12492 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012493
Jim Mattson8d860bb2018-05-09 16:56:05 -040012494 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12495 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12496 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012497 } else if (!nested_cpu_has_ept(vmcs12) &&
12498 nested_cpu_has2(vmcs12,
12499 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012500 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012501 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012502
12503 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12504 vmx->host_rsp = 0;
12505
12506 /* Unpin physical memory we referred to in vmcs02 */
12507 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012508 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012509 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012510 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012511 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012512 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012513 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012514 }
Wincy Van705699a2015-02-03 23:58:17 +080012515 if (vmx->nested.pi_desc_page) {
12516 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012517 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012518 vmx->nested.pi_desc_page = NULL;
12519 vmx->nested.pi_desc = NULL;
12520 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012521
12522 /*
Tang Chen38b99172014-09-24 15:57:54 +080012523 * We are now running in L2, mmu_notifier will force to reload the
12524 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12525 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012526 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012527
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012528 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012529 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012530
12531 /* in case we halted in L2 */
12532 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012533
12534 if (likely(!vmx->fail)) {
12535 /*
12536 * TODO: SDM says that with acknowledge interrupt on
12537 * exit, bit 31 of the VM-exit interrupt information
12538 * (valid interrupt) is always set to 1 on
12539 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12540 * need kvm_cpu_has_interrupt(). See the commit
12541 * message for details.
12542 */
12543 if (nested_exit_intr_ack_set(vcpu) &&
12544 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12545 kvm_cpu_has_interrupt(vcpu)) {
12546 int irq = kvm_cpu_get_interrupt(vcpu);
12547 WARN_ON(irq < 0);
12548 vmcs12->vm_exit_intr_info = irq |
12549 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12550 }
12551
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012552 if (exit_reason != -1)
12553 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12554 vmcs12->exit_qualification,
12555 vmcs12->idt_vectoring_info_field,
12556 vmcs12->vm_exit_intr_info,
12557 vmcs12->vm_exit_intr_error_code,
12558 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012559
12560 load_vmcs12_host_state(vcpu, vmcs12);
12561
12562 return;
12563 }
12564
12565 /*
12566 * After an early L2 VM-entry failure, we're now back
12567 * in L1 which thinks it just finished a VMLAUNCH or
12568 * VMRESUME instruction, so we need to set the failure
12569 * flag and the VM-instruction error field of the VMCS
12570 * accordingly.
12571 */
12572 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012573
12574 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12575
Jim Mattson4f350c62017-09-14 16:31:44 -070012576 /*
12577 * The emulated instruction was already skipped in
12578 * nested_vmx_run, but the updated RIP was never
12579 * written back to the vmcs01.
12580 */
12581 skip_emulated_instruction(vcpu);
12582 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012583}
12584
Nadav Har'El7c177932011-05-25 23:12:04 +030012585/*
Jan Kiszka42124922014-01-04 18:47:19 +010012586 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12587 */
12588static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12589{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012590 if (is_guest_mode(vcpu)) {
12591 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012592 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012593 }
Jan Kiszka42124922014-01-04 18:47:19 +010012594 free_nested(to_vmx(vcpu));
12595}
12596
12597/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012598 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12599 * 23.7 "VM-entry failures during or after loading guest state" (this also
12600 * lists the acceptable exit-reason and exit-qualification parameters).
12601 * It should only be called before L2 actually succeeded to run, and when
12602 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12603 */
12604static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12605 struct vmcs12 *vmcs12,
12606 u32 reason, unsigned long qualification)
12607{
12608 load_vmcs12_host_state(vcpu, vmcs12);
12609 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12610 vmcs12->exit_qualification = qualification;
12611 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012612 if (enable_shadow_vmcs)
12613 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012614}
12615
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012616static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12617 struct x86_instruction_info *info,
12618 enum x86_intercept_stage stage)
12619{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012620 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12621 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12622
12623 /*
12624 * RDPID causes #UD if disabled through secondary execution controls.
12625 * Because it is marked as EmulateOnUD, we need to intercept it here.
12626 */
12627 if (info->intercept == x86_intercept_rdtscp &&
12628 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12629 ctxt->exception.vector = UD_VECTOR;
12630 ctxt->exception.error_code_valid = false;
12631 return X86EMUL_PROPAGATE_FAULT;
12632 }
12633
12634 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012635 return X86EMUL_CONTINUE;
12636}
12637
Yunhong Jiang64672c92016-06-13 14:19:59 -070012638#ifdef CONFIG_X86_64
12639/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12640static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12641 u64 divisor, u64 *result)
12642{
12643 u64 low = a << shift, high = a >> (64 - shift);
12644
12645 /* To avoid the overflow on divq */
12646 if (high >= divisor)
12647 return 1;
12648
12649 /* Low hold the result, high hold rem which is discarded */
12650 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12651 "rm" (divisor), "0" (low), "1" (high));
12652 *result = low;
12653
12654 return 0;
12655}
12656
12657static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12658{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012659 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012660 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012661
12662 if (kvm_mwait_in_guest(vcpu->kvm))
12663 return -EOPNOTSUPP;
12664
12665 vmx = to_vmx(vcpu);
12666 tscl = rdtsc();
12667 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12668 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012669 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12670
12671 if (delta_tsc > lapic_timer_advance_cycles)
12672 delta_tsc -= lapic_timer_advance_cycles;
12673 else
12674 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012675
12676 /* Convert to host delta tsc if tsc scaling is enabled */
12677 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12678 u64_shl_div_u64(delta_tsc,
12679 kvm_tsc_scaling_ratio_frac_bits,
12680 vcpu->arch.tsc_scaling_ratio,
12681 &delta_tsc))
12682 return -ERANGE;
12683
12684 /*
12685 * If the delta tsc can't fit in the 32 bit after the multi shift,
12686 * we can't use the preemption timer.
12687 * It's possible that it fits on later vmentries, but checking
12688 * on every vmentry is costly so we just use an hrtimer.
12689 */
12690 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12691 return -ERANGE;
12692
12693 vmx->hv_deadline_tsc = tscl + delta_tsc;
12694 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12695 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012696
12697 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012698}
12699
12700static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12701{
12702 struct vcpu_vmx *vmx = to_vmx(vcpu);
12703 vmx->hv_deadline_tsc = -1;
12704 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12705 PIN_BASED_VMX_PREEMPTION_TIMER);
12706}
12707#endif
12708
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012709static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012710{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012711 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012712 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012713}
12714
Kai Huang843e4332015-01-28 10:54:28 +080012715static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12716 struct kvm_memory_slot *slot)
12717{
12718 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12719 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12720}
12721
12722static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12723 struct kvm_memory_slot *slot)
12724{
12725 kvm_mmu_slot_set_dirty(kvm, slot);
12726}
12727
12728static void vmx_flush_log_dirty(struct kvm *kvm)
12729{
12730 kvm_flush_pml_buffers(kvm);
12731}
12732
Bandan Dasc5f983f2017-05-05 15:25:14 -040012733static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12734{
12735 struct vmcs12 *vmcs12;
12736 struct vcpu_vmx *vmx = to_vmx(vcpu);
12737 gpa_t gpa;
12738 struct page *page = NULL;
12739 u64 *pml_address;
12740
12741 if (is_guest_mode(vcpu)) {
12742 WARN_ON_ONCE(vmx->nested.pml_full);
12743
12744 /*
12745 * Check if PML is enabled for the nested guest.
12746 * Whether eptp bit 6 is set is already checked
12747 * as part of A/D emulation.
12748 */
12749 vmcs12 = get_vmcs12(vcpu);
12750 if (!nested_cpu_has_pml(vmcs12))
12751 return 0;
12752
Dan Carpenter47698862017-05-10 22:43:17 +030012753 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012754 vmx->nested.pml_full = true;
12755 return 1;
12756 }
12757
12758 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12759
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012760 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12761 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012762 return 0;
12763
12764 pml_address = kmap(page);
12765 pml_address[vmcs12->guest_pml_index--] = gpa;
12766 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012767 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012768 }
12769
12770 return 0;
12771}
12772
Kai Huang843e4332015-01-28 10:54:28 +080012773static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12774 struct kvm_memory_slot *memslot,
12775 gfn_t offset, unsigned long mask)
12776{
12777 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12778}
12779
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012780static void __pi_post_block(struct kvm_vcpu *vcpu)
12781{
12782 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12783 struct pi_desc old, new;
12784 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012785
12786 do {
12787 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012788 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12789 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012790
12791 dest = cpu_physical_id(vcpu->cpu);
12792
12793 if (x2apic_enabled())
12794 new.ndst = dest;
12795 else
12796 new.ndst = (dest << 8) & 0xFF00;
12797
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012798 /* set 'NV' to 'notification vector' */
12799 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012800 } while (cmpxchg64(&pi_desc->control, old.control,
12801 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012802
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012803 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12804 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012805 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012806 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012807 vcpu->pre_pcpu = -1;
12808 }
12809}
12810
Feng Wuefc64402015-09-18 22:29:51 +080012811/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012812 * This routine does the following things for vCPU which is going
12813 * to be blocked if VT-d PI is enabled.
12814 * - Store the vCPU to the wakeup list, so when interrupts happen
12815 * we can find the right vCPU to wake up.
12816 * - Change the Posted-interrupt descriptor as below:
12817 * 'NDST' <-- vcpu->pre_pcpu
12818 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12819 * - If 'ON' is set during this process, which means at least one
12820 * interrupt is posted for this vCPU, we cannot block it, in
12821 * this case, return 1, otherwise, return 0.
12822 *
12823 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012824static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012825{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012826 unsigned int dest;
12827 struct pi_desc old, new;
12828 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12829
12830 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012831 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12832 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012833 return 0;
12834
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012835 WARN_ON(irqs_disabled());
12836 local_irq_disable();
12837 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12838 vcpu->pre_pcpu = vcpu->cpu;
12839 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12840 list_add_tail(&vcpu->blocked_vcpu_list,
12841 &per_cpu(blocked_vcpu_on_cpu,
12842 vcpu->pre_pcpu));
12843 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12844 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012845
12846 do {
12847 old.control = new.control = pi_desc->control;
12848
Feng Wubf9f6ac2015-09-18 22:29:55 +080012849 WARN((pi_desc->sn == 1),
12850 "Warning: SN field of posted-interrupts "
12851 "is set before blocking\n");
12852
12853 /*
12854 * Since vCPU can be preempted during this process,
12855 * vcpu->cpu could be different with pre_pcpu, we
12856 * need to set pre_pcpu as the destination of wakeup
12857 * notification event, then we can find the right vCPU
12858 * to wakeup in wakeup handler if interrupts happen
12859 * when the vCPU is in blocked state.
12860 */
12861 dest = cpu_physical_id(vcpu->pre_pcpu);
12862
12863 if (x2apic_enabled())
12864 new.ndst = dest;
12865 else
12866 new.ndst = (dest << 8) & 0xFF00;
12867
12868 /* set 'NV' to 'wakeup vector' */
12869 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012870 } while (cmpxchg64(&pi_desc->control, old.control,
12871 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012872
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012873 /* We should not block the vCPU if an interrupt is posted for it. */
12874 if (pi_test_on(pi_desc) == 1)
12875 __pi_post_block(vcpu);
12876
12877 local_irq_enable();
12878 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012879}
12880
Yunhong Jiangbc225122016-06-13 14:19:58 -070012881static int vmx_pre_block(struct kvm_vcpu *vcpu)
12882{
12883 if (pi_pre_block(vcpu))
12884 return 1;
12885
Yunhong Jiang64672c92016-06-13 14:19:59 -070012886 if (kvm_lapic_hv_timer_in_use(vcpu))
12887 kvm_lapic_switch_to_sw_timer(vcpu);
12888
Yunhong Jiangbc225122016-06-13 14:19:58 -070012889 return 0;
12890}
12891
12892static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012893{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012894 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012895 return;
12896
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012897 WARN_ON(irqs_disabled());
12898 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012899 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012900 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012901}
12902
Yunhong Jiangbc225122016-06-13 14:19:58 -070012903static void vmx_post_block(struct kvm_vcpu *vcpu)
12904{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012905 if (kvm_x86_ops->set_hv_timer)
12906 kvm_lapic_switch_to_hv_timer(vcpu);
12907
Yunhong Jiangbc225122016-06-13 14:19:58 -070012908 pi_post_block(vcpu);
12909}
12910
Feng Wubf9f6ac2015-09-18 22:29:55 +080012911/*
Feng Wuefc64402015-09-18 22:29:51 +080012912 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12913 *
12914 * @kvm: kvm
12915 * @host_irq: host irq of the interrupt
12916 * @guest_irq: gsi of the interrupt
12917 * @set: set or unset PI
12918 * returns 0 on success, < 0 on failure
12919 */
12920static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12921 uint32_t guest_irq, bool set)
12922{
12923 struct kvm_kernel_irq_routing_entry *e;
12924 struct kvm_irq_routing_table *irq_rt;
12925 struct kvm_lapic_irq irq;
12926 struct kvm_vcpu *vcpu;
12927 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012928 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012929
12930 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012931 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12932 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012933 return 0;
12934
12935 idx = srcu_read_lock(&kvm->irq_srcu);
12936 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012937 if (guest_irq >= irq_rt->nr_rt_entries ||
12938 hlist_empty(&irq_rt->map[guest_irq])) {
12939 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12940 guest_irq, irq_rt->nr_rt_entries);
12941 goto out;
12942 }
Feng Wuefc64402015-09-18 22:29:51 +080012943
12944 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12945 if (e->type != KVM_IRQ_ROUTING_MSI)
12946 continue;
12947 /*
12948 * VT-d PI cannot support posting multicast/broadcast
12949 * interrupts to a vCPU, we still use interrupt remapping
12950 * for these kind of interrupts.
12951 *
12952 * For lowest-priority interrupts, we only support
12953 * those with single CPU as the destination, e.g. user
12954 * configures the interrupts via /proc/irq or uses
12955 * irqbalance to make the interrupts single-CPU.
12956 *
12957 * We will support full lowest-priority interrupt later.
12958 */
12959
Radim Krčmář371313132016-07-12 22:09:27 +020012960 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080012961 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
12962 /*
12963 * Make sure the IRTE is in remapped mode if
12964 * we don't handle it in posted mode.
12965 */
12966 ret = irq_set_vcpu_affinity(host_irq, NULL);
12967 if (ret < 0) {
12968 printk(KERN_INFO
12969 "failed to back to remapped mode, irq: %u\n",
12970 host_irq);
12971 goto out;
12972 }
12973
Feng Wuefc64402015-09-18 22:29:51 +080012974 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080012975 }
Feng Wuefc64402015-09-18 22:29:51 +080012976
12977 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
12978 vcpu_info.vector = irq.vector;
12979
hu huajun2698d822018-04-11 15:16:40 +080012980 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080012981 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
12982
12983 if (set)
12984 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080012985 else
Feng Wuefc64402015-09-18 22:29:51 +080012986 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080012987
12988 if (ret < 0) {
12989 printk(KERN_INFO "%s: failed to update PI IRTE\n",
12990 __func__);
12991 goto out;
12992 }
12993 }
12994
12995 ret = 0;
12996out:
12997 srcu_read_unlock(&kvm->irq_srcu, idx);
12998 return ret;
12999}
13000
Ashok Rajc45dcc72016-06-22 14:59:56 +080013001static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13002{
13003 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13004 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13005 FEATURE_CONTROL_LMCE;
13006 else
13007 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13008 ~FEATURE_CONTROL_LMCE;
13009}
13010
Ladi Prosek72d7b372017-10-11 16:54:41 +020013011static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13012{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013013 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13014 if (to_vmx(vcpu)->nested.nested_run_pending)
13015 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013016 return 1;
13017}
13018
Ladi Prosek0234bf82017-10-11 16:54:40 +020013019static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13020{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013021 struct vcpu_vmx *vmx = to_vmx(vcpu);
13022
13023 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13024 if (vmx->nested.smm.guest_mode)
13025 nested_vmx_vmexit(vcpu, -1, 0, 0);
13026
13027 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13028 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013029 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013030 return 0;
13031}
13032
13033static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13034{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013035 struct vcpu_vmx *vmx = to_vmx(vcpu);
13036 int ret;
13037
13038 if (vmx->nested.smm.vmxon) {
13039 vmx->nested.vmxon = true;
13040 vmx->nested.smm.vmxon = false;
13041 }
13042
13043 if (vmx->nested.smm.guest_mode) {
13044 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013045 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013046 vcpu->arch.hflags |= HF_SMM_MASK;
13047 if (ret)
13048 return ret;
13049
13050 vmx->nested.smm.guest_mode = false;
13051 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013052 return 0;
13053}
13054
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013055static int enable_smi_window(struct kvm_vcpu *vcpu)
13056{
13057 return 0;
13058}
13059
Kees Cook404f6aa2016-08-08 16:29:06 -070013060static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013061 .cpu_has_kvm_support = cpu_has_kvm_support,
13062 .disabled_by_bios = vmx_disabled_by_bios,
13063 .hardware_setup = hardware_setup,
13064 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013065 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013066 .hardware_enable = hardware_enable,
13067 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013068 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013069 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013070
Wanpeng Lib31c1142018-03-12 04:53:04 -070013071 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013072 .vm_alloc = vmx_vm_alloc,
13073 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013074
Avi Kivity6aa8b732006-12-10 02:21:36 -080013075 .vcpu_create = vmx_create_vcpu,
13076 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013077 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013078
Avi Kivity04d2cc72007-09-10 18:10:54 +030013079 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013080 .vcpu_load = vmx_vcpu_load,
13081 .vcpu_put = vmx_vcpu_put,
13082
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013083 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013084 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013085 .get_msr = vmx_get_msr,
13086 .set_msr = vmx_set_msr,
13087 .get_segment_base = vmx_get_segment_base,
13088 .get_segment = vmx_get_segment,
13089 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013090 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013091 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013092 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013093 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013094 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013095 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013096 .set_cr3 = vmx_set_cr3,
13097 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013098 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013099 .get_idt = vmx_get_idt,
13100 .set_idt = vmx_set_idt,
13101 .get_gdt = vmx_get_gdt,
13102 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013103 .get_dr6 = vmx_get_dr6,
13104 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013105 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013106 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013107 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013108 .get_rflags = vmx_get_rflags,
13109 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013110
Avi Kivity6aa8b732006-12-10 02:21:36 -080013111 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013112
Avi Kivity6aa8b732006-12-10 02:21:36 -080013113 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013114 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013115 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013116 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13117 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013118 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013119 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013120 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013121 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013122 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013123 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013124 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013125 .get_nmi_mask = vmx_get_nmi_mask,
13126 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013127 .enable_nmi_window = enable_nmi_window,
13128 .enable_irq_window = enable_irq_window,
13129 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013130 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013131 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013132 .get_enable_apicv = vmx_get_enable_apicv,
13133 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013134 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013135 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013136 .hwapic_irr_update = vmx_hwapic_irr_update,
13137 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013138 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13139 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013140
Izik Eiduscbc94022007-10-25 00:29:55 +020013141 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013142 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013143 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013144 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013145
Avi Kivity586f9602010-11-18 13:09:54 +020013146 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013147
Sheng Yang17cc3932010-01-05 19:02:27 +080013148 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013149
13150 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013151
13152 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013153 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013154
13155 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013156
13157 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013158
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013159 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013160 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013161
13162 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013163
13164 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013165 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013166 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013167 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013168 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013169
13170 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013171
13172 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013173
13174 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13175 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13176 .flush_log_dirty = vmx_flush_log_dirty,
13177 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013178 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013179
Feng Wubf9f6ac2015-09-18 22:29:55 +080013180 .pre_block = vmx_pre_block,
13181 .post_block = vmx_post_block,
13182
Wei Huang25462f72015-06-19 15:45:05 +020013183 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013184
13185 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013186
13187#ifdef CONFIG_X86_64
13188 .set_hv_timer = vmx_set_hv_timer,
13189 .cancel_hv_timer = vmx_cancel_hv_timer,
13190#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013191
13192 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013193
Ladi Prosek72d7b372017-10-11 16:54:41 +020013194 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013195 .pre_enter_smm = vmx_pre_enter_smm,
13196 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013197 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013198};
13199
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013200static int __init vmx_setup_l1d_flush(void)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013201{
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013202 struct page *page;
13203
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013204 if (!boot_cpu_has_bug(X86_BUG_L1TF))
13205 return 0;
13206
13207 l1tf_vmx_mitigation = vmentry_l1d_flush;
13208
Thomas Gleixner2f055942018-07-13 16:23:17 +020013209 if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013210 return 0;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013211
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020013212 if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
13213 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
13214 if (!page)
13215 return -ENOMEM;
13216 vmx_l1d_flush_pages = page_address(page);
13217 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013218
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013219 static_branch_enable(&vmx_l1d_should_flush);
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013220 return 0;
13221}
13222
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013223static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013224{
13225 if (vmx_l1d_flush_pages) {
13226 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13227 vmx_l1d_flush_pages = NULL;
13228 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013229 /* Restore state so sysfs ignores VMX */
13230 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013231}
13232
Avi Kivity6aa8b732006-12-10 02:21:36 -080013233static int __init vmx_init(void)
13234{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013235 int r;
13236
13237#if IS_ENABLED(CONFIG_HYPERV)
13238 /*
13239 * Enlightened VMCS usage should be recommended and the host needs
13240 * to support eVMCS v1 or above. We can also disable eVMCS support
13241 * with module parameter.
13242 */
13243 if (enlightened_vmcs &&
13244 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13245 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13246 KVM_EVMCS_VERSION) {
13247 int cpu;
13248
13249 /* Check that we have assist pages on all online CPUs */
13250 for_each_online_cpu(cpu) {
13251 if (!hv_get_vp_assist_page(cpu)) {
13252 enlightened_vmcs = false;
13253 break;
13254 }
13255 }
13256
13257 if (enlightened_vmcs) {
13258 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13259 static_branch_enable(&enable_evmcs);
13260 }
13261 } else {
13262 enlightened_vmcs = false;
13263 }
13264#endif
13265
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013266 r = vmx_setup_l1d_flush();
He, Qingfdef3ad2007-04-30 09:45:24 +030013267 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013268 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013269
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013270 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13271 __alignof__(struct vcpu_vmx), THIS_MODULE);
13272 if (r) {
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013273 vmx_cleanup_l1d_flush();
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013274 return r;
13275 }
13276
Dave Young2965faa2015-09-09 15:38:55 -070013277#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013278 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13279 crash_vmclear_local_loaded_vmcss);
13280#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013281 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013282
He, Qingfdef3ad2007-04-30 09:45:24 +030013283 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013284}
13285
13286static void __exit vmx_exit(void)
13287{
Dave Young2965faa2015-09-09 15:38:55 -070013288#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013289 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013290 synchronize_rcu();
13291#endif
13292
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013293 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013294
13295#if IS_ENABLED(CONFIG_HYPERV)
13296 if (static_branch_unlikely(&enable_evmcs)) {
13297 int cpu;
13298 struct hv_vp_assist_page *vp_ap;
13299 /*
13300 * Reset everything to support using non-enlightened VMCS
13301 * access later (e.g. when we reload the module with
13302 * enlightened_vmcs=0)
13303 */
13304 for_each_online_cpu(cpu) {
13305 vp_ap = hv_get_vp_assist_page(cpu);
13306
13307 if (!vp_ap)
13308 continue;
13309
13310 vp_ap->current_nested_vmcs = 0;
13311 vp_ap->enlighten_vmentry = 0;
13312 }
13313
13314 static_branch_disable(&enable_evmcs);
13315 }
13316#endif
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013317 vmx_cleanup_l1d_flush();
Avi Kivity6aa8b732006-12-10 02:21:36 -080013318}
13319
13320module_init(vmx_init)
13321module_exit(vmx_exit)