blob: 8fe2999fcb1e5e2a36cf1ff0bdfef7ac57c7a6c9 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
7 *
8 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Avi Kivity6aa8b732006-12-10 02:21:36 -080014 */
15
Sean Christopherson199b1182018-12-03 13:52:53 -080016#include <linux/frame.h>
17#include <linux/highmem.h>
18#include <linux/hrtimer.h>
19#include <linux/kernel.h>
Avi Kivityedf88412007-12-16 11:02:48 +020020#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080021#include <linux/module.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020022#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070023#include <linux/mod_devicetable.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080024#include <linux/mm.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080025#include <linux/sched.h>
Josh Poimboeufb2849092019-01-30 07:13:58 -060026#include <linux/sched/smt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040028#include <linux/tboot.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080029#include <linux/trace_events.h>
Avi Kivitye4956062007-06-28 14:15:57 -040030
Sean Christopherson199b1182018-12-03 13:52:53 -080031#include <asm/apic.h>
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020032#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080033#include <asm/cpu.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010034#include <asm/cpu_device_id.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010035#include <asm/debugreg.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080036#include <asm/desc.h>
37#include <asm/fpu/internal.h>
38#include <asm/io.h>
Feng Wuefc64402015-09-18 22:29:51 +080039#include <asm/irq_remapping.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080040#include <asm/kexec.h>
41#include <asm/perf_event.h>
42#include <asm/mce.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070043#include <asm/mmu_context.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010044#include <asm/mshyperv.h>
Benjamin Thielb10c3072020-01-23 18:29:45 +010045#include <asm/mwait.h>
Sean Christopherson199b1182018-12-03 13:52:53 -080046#include <asm/spec-ctrl.h>
47#include <asm/virtext.h>
48#include <asm/vmx.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080049
Sean Christopherson3077c192018-12-03 13:53:02 -080050#include "capabilities.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080051#include "cpuid.h"
Sean Christopherson4cebd742018-12-03 13:52:58 -080052#include "evmcs.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080053#include "irq.h"
54#include "kvm_cache_regs.h"
55#include "lapic.h"
56#include "mmu.h"
Sean Christopherson55d23752018-12-03 13:53:18 -080057#include "nested.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080058#include "ops.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080060#include "trace.h"
Sean Christophersoncb1d4742018-12-03 13:53:04 -080061#include "vmcs.h"
Sean Christopherson609363c2018-12-03 13:53:05 -080062#include "vmcs12.h"
Sean Christopherson89b0c9f2018-12-03 13:53:07 -080063#include "vmx.h"
Sean Christopherson199b1182018-12-03 13:52:53 -080064#include "x86.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Valdis Klētnieks575b2552020-02-27 21:49:52 -050069#ifdef MODULE
Josh Triplette9bda3b2012-03-20 23:33:51 -070070static const struct x86_cpu_id vmx_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010071 X86_MATCH_FEATURE(X86_FEATURE_VMX, NULL),
Josh Triplette9bda3b2012-03-20 23:33:51 -070072 {}
73};
74MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050075#endif
Josh Triplette9bda3b2012-03-20 23:33:51 -070076
Sean Christopherson2c4fd912018-12-03 13:53:03 -080077bool __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
Sean Christopherson2c4fd912018-12-03 13:53:03 -080083bool __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
Sean Christopherson2c4fd912018-12-03 13:53:03 -080086bool __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
Sean Christopherson2c4fd912018-12-03 13:53:03 -080089bool __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
Sean Christopherson2c4fd912018-12-03 13:53:03 -080093bool __read_mostly enable_ept_ad_bits = 1;
Xudong Hao83c3a332012-05-28 19:33:35 +080094module_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
Vitaly Kuznetsova4443262020-02-20 18:22:04 +0100102bool __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
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Paolo Bonzini1e58e5e2018-10-17 00:55:22 +0200110static bool __read_mostly nested = 1;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Sean Christopherson2c4fd912018-12-03 13:53:03 -0800113bool __read_mostly enable_pml = 1;
Kai Huang843e4332015-01-28 10:54:28 +0800114module_param_named(pml, enable_pml, bool, S_IRUGO);
115
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200116static bool __read_mostly dump_invalid_vmcs = 0;
117module_param(dump_invalid_vmcs, bool, 0644);
118
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100119#define MSR_BITMAP_MODE_X2APIC 1
120#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100121
Haozhong Zhang64903d62015-10-20 15:39:09 +0800122#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
123
Yunhong Jiang64672c92016-06-13 14:19:59 -0700124/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
125static int __read_mostly cpu_preemption_timer_multi;
126static bool __read_mostly enable_preemption_timer = 1;
127#ifdef CONFIG_X86_64
128module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
129#endif
130
Sean Christopherson3de63472018-07-13 08:42:30 -0700131#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800132#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
133#define KVM_VM_CR0_ALWAYS_ON \
134 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
135 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200136
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800137#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200138#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
139#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
140
Avi Kivity78ac8b42010-04-08 18:19:35 +0300141#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
142
Chao Pengbf8c55d2018-10-24 16:05:14 +0800143#define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
144 RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
145 RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
146 RTIT_STATUS_BYTECNT))
147
148#define MSR_IA32_RTIT_OUTPUT_BASE_MASK \
149 (~((1UL << cpuid_query_maxphyaddr(vcpu)) - 1) | 0x7f)
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
152 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
153 * ple_gap: upper bound on the amount of time between two successive
154 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500155 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800156 * ple_window: upper bound on the amount of time a guest is allowed to execute
157 * in a PAUSE loop. Tests indicate that most spinlocks are held for
158 * less than 2^12 cycles
159 * Time is measured based on a counter that runs at the same rate as the TSC,
160 * refer SDM volume 3b section 21.6.13 & 22.1.3.
161 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400162static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Luiz Capitulinoa87c99e2018-11-23 12:02:14 -0500163module_param(ple_gap, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200164
Babu Moger7fbc85a2018-03-16 16:37:22 -0400165static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
166module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800167
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200168/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400169static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400170module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200171
172/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400173static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200175
176/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400177static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
178module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200179
Chao Pengf99e3da2018-10-24 16:05:10 +0800180/* Default is SYSTEM mode, 1 for host-guest mode */
181int __read_mostly pt_mode = PT_MODE_SYSTEM;
182module_param(pt_mode, int, S_IRUGO);
183
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200184static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200185static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200186static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200187
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200188/* Storage for pre module init parameter parsing */
189static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200190
191static const struct {
192 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200193 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200195 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
196 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
197 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
198 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
199 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
200 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200201};
202
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200203#define L1D_CACHE_ORDER 4
204static void *vmx_l1d_flush_pages;
205
206static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
207{
208 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200209 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200210
Waiman Long19a36d32019-08-26 15:30:23 -0400211 if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
212 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
213 return 0;
214 }
215
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200216 if (!enable_ept) {
217 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
218 return 0;
219 }
220
Yi Wangd806afa2018-08-16 13:42:39 +0800221 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
222 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200223
Yi Wangd806afa2018-08-16 13:42:39 +0800224 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
225 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
226 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
227 return 0;
228 }
229 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200230
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200231 /* If set to auto use the default l1tf mitigation method */
232 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
233 switch (l1tf_mitigation) {
234 case L1TF_MITIGATION_OFF:
235 l1tf = VMENTER_L1D_FLUSH_NEVER;
236 break;
237 case L1TF_MITIGATION_FLUSH_NOWARN:
238 case L1TF_MITIGATION_FLUSH:
239 case L1TF_MITIGATION_FLUSH_NOSMT:
240 l1tf = VMENTER_L1D_FLUSH_COND;
241 break;
242 case L1TF_MITIGATION_FULL:
243 case L1TF_MITIGATION_FULL_FORCE:
244 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
245 break;
246 }
247 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
248 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
249 }
250
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200251 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
252 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Ben Gardon41836832019-02-11 11:02:52 -0800253 /*
254 * This allocation for vmx_l1d_flush_pages is not tied to a VM
255 * lifetime and so should not be charged to a memcg.
256 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200257 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
258 if (!page)
259 return -ENOMEM;
260 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200261
262 /*
263 * Initialize each page with a different pattern in
264 * order to protect against KSM in the nested
265 * virtualization case.
266 */
267 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
268 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
269 PAGE_SIZE);
270 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200271 }
272
273 l1tf_vmx_mitigation = l1tf;
274
Thomas Gleixner895ae472018-07-13 16:23:22 +0200275 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
276 static_branch_enable(&vmx_l1d_should_flush);
277 else
278 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200279
Nicolai Stange427362a2018-07-21 22:25:00 +0200280 if (l1tf == VMENTER_L1D_FLUSH_COND)
281 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200282 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200283 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200284 return 0;
285}
286
287static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200288{
289 unsigned int i;
290
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200291 if (s) {
292 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200293 if (vmentry_l1d_param[i].for_parse &&
294 sysfs_streq(s, vmentry_l1d_param[i].option))
295 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200296 }
297 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200298 return -EINVAL;
299}
300
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200301static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
302{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200303 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200304
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200305 l1tf = vmentry_l1d_flush_parse(s);
306 if (l1tf < 0)
307 return l1tf;
308
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200309 if (!boot_cpu_has(X86_BUG_L1TF))
310 return 0;
311
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200312 /*
313 * Has vmx_init() run already? If not then this is the pre init
314 * parameter parsing. In that case just store the value and let
315 * vmx_init() do the proper setup after enable_ept has been
316 * established.
317 */
318 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
319 vmentry_l1d_flush_param = l1tf;
320 return 0;
321 }
322
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200323 mutex_lock(&vmx_l1d_flush_mutex);
324 ret = vmx_setup_l1d_flush(l1tf);
325 mutex_unlock(&vmx_l1d_flush_mutex);
326 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200327}
328
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200329static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
330{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200331 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
332 return sprintf(s, "???\n");
333
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200334 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200335}
336
337static const struct kernel_param_ops vmentry_l1d_flush_ops = {
338 .set = vmentry_l1d_flush_set,
339 .get = vmentry_l1d_flush_get,
340};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200341module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200342
Gleb Natapovd99e4152012-12-20 16:57:45 +0200343static bool guest_state_valid(struct kvm_vcpu *vcpu);
344static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yi Wang1e4329ee2018-11-08 11:22:21 +0800345static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Ashok Raj15d45072018-02-01 22:59:43 +0100346 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300347
Sean Christopherson453eafb2018-12-20 12:25:17 -0800348void vmx_vmexit(void);
349
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700350#define vmx_insn_failed(fmt...) \
351do { \
352 WARN_ONCE(1, fmt); \
353 pr_warn_ratelimited(fmt); \
354} while (0)
355
Sean Christopherson6e202092019-07-19 13:41:08 -0700356asmlinkage void vmread_error(unsigned long field, bool fault)
357{
358 if (fault)
359 kvm_spurious_fault();
360 else
361 vmx_insn_failed("kvm: vmread failed: field=%lx\n", field);
362}
363
Sean Christopherson52a9fcb2019-07-19 13:41:07 -0700364noinline void vmwrite_error(unsigned long field, unsigned long value)
365{
366 vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%d\n",
367 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
368}
369
370noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr)
371{
372 vmx_insn_failed("kvm: vmclear failed: %p/%llx\n", vmcs, phys_addr);
373}
374
375noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr)
376{
377 vmx_insn_failed("kvm: vmptrld failed: %p/%llx\n", vmcs, phys_addr);
378}
379
380noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva)
381{
382 vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
383 ext, vpid, gva);
384}
385
386noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa)
387{
388 vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
389 ext, eptp, gpa);
390}
391
Avi Kivity6aa8b732006-12-10 02:21:36 -0800392static DEFINE_PER_CPU(struct vmcs *, vmxarea);
Sean Christopherson75edce82018-12-03 13:53:06 -0800393DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300394/*
395 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
396 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
397 */
398static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800399
Feng Wubf9f6ac2015-09-18 22:29:55 +0800400/*
401 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
402 * can find which vCPU should be waken up.
403 */
404static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
405static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
406
Sheng Yang2384d2b2008-01-17 15:14:33 +0800407static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
408static DEFINE_SPINLOCK(vmx_vpid_lock);
409
Sean Christopherson3077c192018-12-03 13:53:02 -0800410struct vmcs_config vmcs_config;
411struct vmx_capability vmx_capability;
Sheng Yangd56f5462008-04-25 10:13:16 +0800412
Avi Kivity6aa8b732006-12-10 02:21:36 -0800413#define VMX_SEGMENT_FIELD(seg) \
414 [VCPU_SREG_##seg] = { \
415 .selector = GUEST_##seg##_SELECTOR, \
416 .base = GUEST_##seg##_BASE, \
417 .limit = GUEST_##seg##_LIMIT, \
418 .ar_bytes = GUEST_##seg##_AR_BYTES, \
419 }
420
Mathias Krause772e0312012-08-30 01:30:19 +0200421static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800422 unsigned selector;
423 unsigned base;
424 unsigned limit;
425 unsigned ar_bytes;
426} kvm_vmx_segment_fields[] = {
427 VMX_SEGMENT_FIELD(CS),
428 VMX_SEGMENT_FIELD(DS),
429 VMX_SEGMENT_FIELD(ES),
430 VMX_SEGMENT_FIELD(FS),
431 VMX_SEGMENT_FIELD(GS),
432 VMX_SEGMENT_FIELD(SS),
433 VMX_SEGMENT_FIELD(TR),
434 VMX_SEGMENT_FIELD(LDTR),
435};
436
Sean Christophersonec0241f2020-04-15 13:34:52 -0700437static inline void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
438{
439 vmx->segment_cache.bitmask = 0;
440}
441
Sean Christopherson23420802019-04-19 22:50:57 -0700442static unsigned long host_idt_base;
Avi Kivity26bb0982009-09-07 11:14:12 +0300443
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300444/*
Jim Mattson898a8112018-12-05 15:28:59 -0800445 * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
446 * will emulate SYSCALL in legacy mode if the vendor string in guest
447 * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
448 * support this emulation, IA32_STAR must always be included in
449 * vmx_msr_index[], even in i386 builds.
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300450 */
Sean Christophersoncf3646e2018-12-03 13:53:15 -0800451const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800452#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300453 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400455 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Paolo Bonzinic11f83e2019-11-18 12:23:00 -0500456 MSR_IA32_TSX_CTRL,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100459#if IS_ENABLED(CONFIG_HYPERV)
460static bool __read_mostly enlightened_vmcs = true;
461module_param(enlightened_vmcs, bool, 0444);
462
Tianyu Lan877ad952018-07-19 08:40:23 +0000463/* check_ept_pointer() should be under protection of ept_pointer_lock. */
464static void check_ept_pointer_match(struct kvm *kvm)
465{
466 struct kvm_vcpu *vcpu;
467 u64 tmp_eptp = INVALID_PAGE;
468 int i;
469
470 kvm_for_each_vcpu(i, vcpu, kvm) {
471 if (!VALID_PAGE(tmp_eptp)) {
472 tmp_eptp = to_vmx(vcpu)->ept_pointer;
473 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
474 to_kvm_vmx(kvm)->ept_pointers_match
475 = EPT_POINTERS_MISMATCH;
476 return;
477 }
478 }
479
480 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
481}
482
Yi Wang8997f652019-01-21 15:27:05 +0800483static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800484 void *data)
485{
486 struct kvm_tlb_range *range = data;
487
488 return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
489 range->pages);
490}
491
492static inline int __hv_remote_flush_tlb_with_range(struct kvm *kvm,
493 struct kvm_vcpu *vcpu, struct kvm_tlb_range *range)
494{
495 u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
496
497 /*
498 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address
499 * of the base of EPT PML4 table, strip off EPT configuration
500 * information.
501 */
502 if (range)
503 return hyperv_flush_guest_mapping_range(ept_pointer & PAGE_MASK,
504 kvm_fill_hv_flush_list_func, (void *)range);
505 else
506 return hyperv_flush_guest_mapping(ept_pointer & PAGE_MASK);
507}
508
509static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
510 struct kvm_tlb_range *range)
Tianyu Lan877ad952018-07-19 08:40:23 +0000511{
Lan Tianyua5c214d2018-10-13 22:54:05 +0800512 struct kvm_vcpu *vcpu;
Lan Tianyub7c1c222019-01-04 15:20:44 +0800513 int ret = 0, i;
Tianyu Lan877ad952018-07-19 08:40:23 +0000514
515 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
516
517 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
518 check_ept_pointer_match(kvm);
519
520 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyu53963a72018-12-06 15:34:36 +0800521 kvm_for_each_vcpu(i, vcpu, kvm) {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800522 /* If ept_pointer is invalid pointer, bypass flush request. */
523 if (VALID_PAGE(to_vmx(vcpu)->ept_pointer))
524 ret |= __hv_remote_flush_tlb_with_range(
525 kvm, vcpu, range);
Lan Tianyu53963a72018-12-06 15:34:36 +0800526 }
Lan Tianyua5c214d2018-10-13 22:54:05 +0800527 } else {
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800528 ret = __hv_remote_flush_tlb_with_range(kvm,
529 kvm_get_vcpu(kvm, 0), range);
Tianyu Lan877ad952018-07-19 08:40:23 +0000530 }
Tianyu Lan877ad952018-07-19 08:40:23 +0000531
Tianyu Lan877ad952018-07-19 08:40:23 +0000532 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
533 return ret;
534}
Lan Tianyu1f3a3e42018-12-06 21:21:07 +0800535static int hv_remote_flush_tlb(struct kvm *kvm)
536{
537 return hv_remote_flush_tlb_with_range(kvm, NULL);
538}
539
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800540static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
541{
542 struct hv_enlightened_vmcs *evmcs;
543 struct hv_partition_assist_pg **p_hv_pa_pg =
544 &vcpu->kvm->arch.hyperv.hv_pa_pg;
545 /*
546 * Synthetic VM-Exit is not enabled in current code and so All
547 * evmcs in singe VM shares same assist page.
548 */
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200549 if (!*p_hv_pa_pg)
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800550 *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200551
552 if (!*p_hv_pa_pg)
553 return -ENOMEM;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800554
555 evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
556
557 evmcs->partition_assist_page =
558 __pa(*p_hv_pa_pg);
Vitaly Kuznetsovcab01852019-09-25 15:30:35 +0200559 evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800560 evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
561
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +0800562 return 0;
563}
564
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +0100565#endif /* IS_ENABLED(CONFIG_HYPERV) */
566
Yunhong Jiang64672c92016-06-13 14:19:59 -0700567/*
568 * Comment's format: document - errata name - stepping - processor name.
569 * Refer from
570 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
571 */
572static u32 vmx_preemption_cpu_tfms[] = {
573/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
5740x000206E6,
575/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
576/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
577/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
5780x00020652,
579/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
5800x00020655,
581/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
582/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
583/*
584 * 320767.pdf - AAP86 - B1 -
585 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
586 */
5870x000106E5,
588/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
5890x000106A0,
590/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
5910x000106A1,
592/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
5930x000106A4,
594 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
595 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
596 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
5970x000106A5,
Wei Huang3d82c562018-12-03 14:13:32 -0600598 /* Xeon E3-1220 V2 */
5990x000306A8,
Yunhong Jiang64672c92016-06-13 14:19:59 -0700600};
601
602static inline bool cpu_has_broken_vmx_preemption_timer(void)
603{
604 u32 eax = cpuid_eax(0x00000001), i;
605
606 /* Clear the reserved bits */
607 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +0000608 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -0700609 if (eax == vmx_preemption_cpu_tfms[i])
610 return true;
611
612 return false;
613}
614
Paolo Bonzini35754c92015-07-29 12:05:37 +0200615static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800616{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200617 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800618}
619
Sheng Yang04547152009-04-01 15:52:31 +0800620static inline bool report_flexpriority(void)
621{
622 return flexpriority_enabled;
623}
624
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800625static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800626{
627 int i;
628
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400629 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300630 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300631 return i;
632 return -1;
633}
634
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800635struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300636{
637 int i;
638
Rusty Russell8b9cf982007-07-30 16:31:43 +1000639 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300640 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400641 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000642 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800643}
644
Paolo Bonzinib07a5c52019-11-18 12:23:01 -0500645static int vmx_set_guest_msr(struct vcpu_vmx *vmx, struct shared_msr_entry *msr, u64 data)
646{
647 int ret = 0;
648
649 u64 old_msr_data = msr->data;
650 msr->data = data;
651 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
652 preempt_disable();
653 ret = kvm_set_shared_msr(msr->index, msr->data,
654 msr->mask);
655 preempt_enable();
656 if (ret)
657 msr->data = old_msr_data;
658 }
659 return ret;
660}
661
Dave Young2965faa2015-09-09 15:38:55 -0700662#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800663static void crash_vmclear_local_loaded_vmcss(void)
664{
665 int cpu = raw_smp_processor_id();
666 struct loaded_vmcs *v;
667
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800668 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
669 loaded_vmcss_on_cpu_link)
670 vmcs_clear(v->vmcs);
671}
Dave Young2965faa2015-09-09 15:38:55 -0700672#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +0800673
Nadav Har'Eld462b812011-05-24 15:26:10 +0300674static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300676 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800677 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800678
Nadav Har'Eld462b812011-05-24 15:26:10 +0300679 if (loaded_vmcs->cpu != cpu)
680 return; /* vcpu migration can race with cpu offline */
681 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800682 per_cpu(current_vmcs, cpu) = NULL;
Sean Christopherson31603d42020-03-21 12:37:49 -0700683
684 vmcs_clear(loaded_vmcs->vmcs);
685 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
686 vmcs_clear(loaded_vmcs->shadow_vmcs);
687
Nadav Har'Eld462b812011-05-24 15:26:10 +0300688 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800689
690 /*
Sean Christopherson31603d42020-03-21 12:37:49 -0700691 * Ensure all writes to loaded_vmcs, including deleting it from its
692 * current percpu list, complete before setting loaded_vmcs->vcpu to
693 * -1, otherwise a different cpu can see vcpu == -1 first and add
694 * loaded_vmcs to its percpu list before it's deleted from this cpu's
695 * list. Pairs with the smp_rmb() in vmx_vcpu_load_vmcs().
Xiao Guangrong5a560f82012-11-28 20:54:14 +0800696 */
697 smp_wmb();
698
Sean Christopherson31603d42020-03-21 12:37:49 -0700699 loaded_vmcs->cpu = -1;
700 loaded_vmcs->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701}
702
Sean Christopherson89b0c9f2018-12-03 13:53:07 -0800703void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800704{
Xiao Guangronge6c7d322012-11-28 20:53:15 +0800705 int cpu = loaded_vmcs->cpu;
706
707 if (cpu != -1)
708 smp_call_function_single(cpu,
709 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800710}
711
Avi Kivity2fb92db2011-04-27 19:42:18 +0300712static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
713 unsigned field)
714{
715 bool ret;
716 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
717
Sean Christophersoncb3c1e22019-09-27 14:45:22 -0700718 if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
719 kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
Avi Kivity2fb92db2011-04-27 19:42:18 +0300720 vmx->segment_cache.bitmask = 0;
721 }
722 ret = vmx->segment_cache.bitmask & mask;
723 vmx->segment_cache.bitmask |= mask;
724 return ret;
725}
726
727static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
728{
729 u16 *p = &vmx->segment_cache.seg[seg].selector;
730
731 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
732 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
733 return *p;
734}
735
736static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
737{
738 ulong *p = &vmx->segment_cache.seg[seg].base;
739
740 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
741 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
742 return *p;
743}
744
745static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
746{
747 u32 *p = &vmx->segment_cache.seg[seg].limit;
748
749 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
750 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
751 return *p;
752}
753
754static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
755{
756 u32 *p = &vmx->segment_cache.seg[seg].ar;
757
758 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
759 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
760 return *p;
761}
762
Sean Christopherson97b7ead2018-12-03 13:53:16 -0800763void update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300764{
765 u32 eb;
766
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100767 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -0800768 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +0200769 /*
770 * Guest access to VMware backdoor ports could legitimately
771 * trigger #GP because of TSS I/O permission bitmap.
772 * We intercept those #GP and allow access to them anyway
773 * as VMware does.
774 */
775 if (enable_vmware_backdoor)
776 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100777 if ((vcpu->guest_debug &
778 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
779 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
780 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300781 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300782 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200783 if (enable_ept)
Miaohe Lin49f933d2020-02-27 11:20:54 +0800784 eb &= ~(1u << PF_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +0300785
786 /* When we are running a nested L2 guest and L1 specified for it a
787 * certain exception bitmap, we must trap the same exceptions and pass
788 * them to L1. When running L2, we will only handle the exceptions
789 * specified above if L1 did not want them.
790 */
791 if (is_guest_mode(vcpu))
792 eb |= get_vmcs12(vcpu)->exception_bitmap;
793
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300794 vmcs_write32(EXCEPTION_BITMAP, eb);
795}
796
Ashok Raj15d45072018-02-01 22:59:43 +0100797/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100798 * Check if MSR is intercepted for currently loaded MSR bitmap.
799 */
800static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
801{
802 unsigned long *msr_bitmap;
803 int f = sizeof(unsigned long);
804
805 if (!cpu_has_vmx_msr_bitmap())
806 return true;
807
808 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
809
810 if (msr <= 0x1fff) {
811 return !!test_bit(msr, msr_bitmap + 0x800 / f);
812 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
813 msr &= 0x1fff;
814 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
815 }
816
817 return true;
818}
819
Gleb Natapov2961e8762013-11-25 15:37:13 +0200820static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
821 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200822{
Gleb Natapov2961e8762013-11-25 15:37:13 +0200823 vm_entry_controls_clearbit(vmx, entry);
824 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200825}
826
Aaron Lewis662f1d12019-11-07 21:14:39 -0800827int vmx_find_msr_index(struct vmx_msrs *m, u32 msr)
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400828{
829 unsigned int i;
830
831 for (i = 0; i < m->nr; ++i) {
832 if (m->val[i].index == msr)
833 return i;
834 }
835 return -ENOENT;
836}
837
Avi Kivity61d2ef22010-04-28 16:40:38 +0300838static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
839{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400840 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300841 struct msr_autoload *m = &vmx->msr_autoload;
842
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200843 switch (msr) {
844 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800845 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200846 clear_atomic_switch_msr_special(vmx,
847 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200848 VM_EXIT_LOAD_IA32_EFER);
849 return;
850 }
851 break;
852 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800853 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200854 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200855 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
856 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
857 return;
858 }
859 break;
Avi Kivity110312c2010-12-21 12:54:20 +0200860 }
Aaron Lewisef0fbca2019-11-07 21:14:38 -0800861 i = vmx_find_msr_index(&m->guest, msr);
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400862 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400863 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400864 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400865 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400866 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +0200867
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400868skip_guest:
Aaron Lewisef0fbca2019-11-07 21:14:38 -0800869 i = vmx_find_msr_index(&m->host, msr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400870 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300871 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400872
873 --m->host.nr;
874 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400875 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300876}
877
Gleb Natapov2961e8762013-11-25 15:37:13 +0200878static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
879 unsigned long entry, unsigned long exit,
880 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
881 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200882{
883 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -0700884 if (host_val_vmcs != HOST_IA32_EFER)
885 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +0200886 vm_entry_controls_setbit(vmx, entry);
887 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200888}
889
Avi Kivity61d2ef22010-04-28 16:40:38 +0300890static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400891 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +0300892{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400893 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300894 struct msr_autoload *m = &vmx->msr_autoload;
895
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200896 switch (msr) {
897 case MSR_EFER:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800898 if (cpu_has_load_ia32_efer()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200899 add_atomic_switch_msr_special(vmx,
900 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200901 VM_EXIT_LOAD_IA32_EFER,
902 GUEST_IA32_EFER,
903 HOST_IA32_EFER,
904 guest_val, host_val);
905 return;
906 }
907 break;
908 case MSR_CORE_PERF_GLOBAL_CTRL:
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800909 if (cpu_has_load_perf_global_ctrl()) {
Gleb Natapov2961e8762013-11-25 15:37:13 +0200910 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200911 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
912 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
913 GUEST_IA32_PERF_GLOBAL_CTRL,
914 HOST_IA32_PERF_GLOBAL_CTRL,
915 guest_val, host_val);
916 return;
917 }
918 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +0100919 case MSR_IA32_PEBS_ENABLE:
920 /* PEBS needs a quiescent period after being disabled (to write
921 * a record). Disabling PEBS through VMX MSR swapping doesn't
922 * provide that period, so a CPU could write host's record into
923 * guest's memory.
924 */
925 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +0200926 }
927
Aaron Lewisef0fbca2019-11-07 21:14:38 -0800928 i = vmx_find_msr_index(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400929 if (!entry_only)
Aaron Lewisef0fbca2019-11-07 21:14:38 -0800930 j = vmx_find_msr_index(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300931
Aaron Lewis7cfe0522019-11-07 21:14:37 -0800932 if ((i < 0 && m->guest.nr == NR_LOADSTORE_MSRS) ||
933 (j < 0 && m->host.nr == NR_LOADSTORE_MSRS)) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +0200934 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +0200935 "Can't add msr %x\n", msr);
936 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300937 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400938 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -0400939 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400940 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400941 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400942 m->guest.val[i].index = msr;
943 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300944
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400945 if (entry_only)
946 return;
947
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400948 if (j < 0) {
949 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400950 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +0300951 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -0400952 m->host.val[j].index = msr;
953 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300954}
955
Avi Kivity92c0d902009-10-29 11:00:16 +0200956static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300957{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100958 u64 guest_efer = vmx->vcpu.arch.efer;
959 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +0300960
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100961 /* Shadow paging assumes NX to be available. */
962 if (!enable_ept)
963 guest_efer |= EFER_NX;
Roel Kluin3a34a882009-08-04 02:08:45 -0700964
Avi Kivity51c6cf62007-08-29 03:48:05 +0300965 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100966 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +0300967 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100968 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300969#ifdef CONFIG_X86_64
970 ignore_bits |= EFER_LMA | EFER_LME;
971 /* SCE is meaningful only in long mode on Intel */
972 if (guest_efer & EFER_LMA)
973 ignore_bits &= ~(u64)EFER_SCE;
974#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +0300975
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800976 /*
977 * On EPT, we can't emulate NX, so we must switch EFER atomically.
978 * On CPUs that support "load IA32_EFER", always switch EFER
979 * atomically, since it's faster than switching it manually.
980 */
Sean Christophersonc73da3f2018-12-03 13:53:00 -0800981 if (cpu_has_load_ia32_efer() ||
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -0800982 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +0300983 if (!(guest_efer & EFER_LMA))
984 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -0800985 if (guest_efer != host_efer)
986 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -0400987 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -0700988 else
989 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +0300990 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100991 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -0700992 clear_atomic_switch_msr(vmx, MSR_EFER);
993
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100994 guest_efer &= ~ignore_bits;
995 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300996
Paolo Bonzini844a5fe2016-03-08 12:13:39 +0100997 vmx->guest_msrs[efer_offset].data = guest_efer;
998 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
999
1000 return true;
1001 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03001002}
1003
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001004#ifdef CONFIG_X86_32
1005/*
1006 * On 32-bit kernels, VM exits still load the FS and GS bases from the
1007 * VMCS rather than the segment table. KVM uses this helper to figure
1008 * out the current bases to poke them into the VMCS before entry.
1009 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001010static unsigned long segment_base(u16 selector)
1011{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001012 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001013 unsigned long v;
1014
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001015 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001016 return 0;
1017
Thomas Garnier45fc8752017-03-14 10:05:08 -07001018 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001019
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001020 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001021 u16 ldt_selector = kvm_read_ldt();
1022
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001023 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001024 return 0;
1025
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001026 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001027 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08001028 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001029 return v;
1030}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08001031#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001032
Sean Christophersone348ac72019-12-10 15:24:33 -08001033static inline bool pt_can_write_msr(struct vcpu_vmx *vmx)
1034{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001035 return vmx_pt_mode_is_host_guest() &&
Sean Christophersone348ac72019-12-10 15:24:33 -08001036 !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
1037}
1038
Chao Peng2ef444f2018-10-24 16:05:12 +08001039static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
1040{
1041 u32 i;
1042
1043 wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1044 wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1045 wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1046 wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1047 for (i = 0; i < addr_range; i++) {
1048 wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1049 wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1050 }
1051}
1052
1053static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
1054{
1055 u32 i;
1056
1057 rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1058 rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1059 rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1060 rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1061 for (i = 0; i < addr_range; i++) {
1062 rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1063 rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1064 }
1065}
1066
1067static void pt_guest_enter(struct vcpu_vmx *vmx)
1068{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001069 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001070 return;
1071
Chao Peng2ef444f2018-10-24 16:05:12 +08001072 /*
Chao Pengb08c2892018-10-24 16:05:15 +08001073 * GUEST_IA32_RTIT_CTL is already set in the VMCS.
1074 * Save host state before VM entry.
Chao Peng2ef444f2018-10-24 16:05:12 +08001075 */
Chao Pengb08c2892018-10-24 16:05:15 +08001076 rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
Chao Peng2ef444f2018-10-24 16:05:12 +08001077 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1078 wrmsrl(MSR_IA32_RTIT_CTL, 0);
1079 pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1080 pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1081 }
1082}
1083
1084static void pt_guest_exit(struct vcpu_vmx *vmx)
1085{
Sean Christopherson2ef76192020-03-02 15:56:22 -08001086 if (vmx_pt_mode_is_system())
Chao Peng2ef444f2018-10-24 16:05:12 +08001087 return;
1088
1089 if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1090 pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1091 pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1092 }
1093
1094 /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
1095 wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1096}
1097
Sean Christopherson13b964a2019-05-07 09:06:31 -07001098void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1099 unsigned long fs_base, unsigned long gs_base)
1100{
1101 if (unlikely(fs_sel != host->fs_sel)) {
1102 if (!(fs_sel & 7))
1103 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1104 else
1105 vmcs_write16(HOST_FS_SELECTOR, 0);
1106 host->fs_sel = fs_sel;
1107 }
1108 if (unlikely(gs_sel != host->gs_sel)) {
1109 if (!(gs_sel & 7))
1110 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1111 else
1112 vmcs_write16(HOST_GS_SELECTOR, 0);
1113 host->gs_sel = gs_sel;
1114 }
1115 if (unlikely(fs_base != host->fs_base)) {
1116 vmcs_writel(HOST_FS_BASE, fs_base);
1117 host->fs_base = fs_base;
1118 }
1119 if (unlikely(gs_base != host->gs_base)) {
1120 vmcs_writel(HOST_GS_BASE, gs_base);
1121 host->gs_base = gs_base;
1122 }
1123}
1124
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001125void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001126{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001127 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07001128 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001129#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01001130 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02001131#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001132 unsigned long fs_base, gs_base;
1133 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03001134 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001135
Sean Christophersond264ee02018-08-27 15:21:12 -07001136 vmx->req_immediate_exit = false;
1137
Liran Alonf48b4712018-11-20 18:03:25 +02001138 /*
1139 * Note that guest MSRs to be saved/restored can also be changed
1140 * when guest state is loaded. This happens when guest transitions
1141 * to/from long-mode by setting MSR_EFER.LMA.
1142 */
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001143 if (!vmx->guest_msrs_ready) {
1144 vmx->guest_msrs_ready = true;
Liran Alonf48b4712018-11-20 18:03:25 +02001145 for (i = 0; i < vmx->save_nmsrs; ++i)
1146 kvm_set_shared_msr(vmx->guest_msrs[i].index,
1147 vmx->guest_msrs[i].data,
1148 vmx->guest_msrs[i].mask);
1149
1150 }
wanpeng lic9dfd3f2020-02-17 18:37:43 +08001151
1152 if (vmx->nested.need_vmcs12_to_shadow_sync)
1153 nested_sync_vmcs12_to_shadow(vcpu);
1154
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001155 if (vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001156 return;
1157
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001158 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001159
Avi Kivity33ed6322007-05-02 16:54:03 +03001160 /*
1161 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1162 * allow segment selectors with cpl > 0 or ti == 1.
1163 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07001164 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01001165
1166#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001167 savesegment(ds, host_state->ds_sel);
1168 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07001169
1170 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001171 if (likely(is_64bit_mm(current->mm))) {
1172 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07001173 fs_sel = current->thread.fsindex;
1174 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001175 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07001176 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001177 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07001178 savesegment(fs, fs_sel);
1179 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02001180 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07001181 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03001182 }
1183
Paolo Bonzini4679b612018-09-24 17:23:01 +02001184 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03001185#else
Sean Christophersone368b872018-07-23 12:32:41 -07001186 savesegment(fs, fs_sel);
1187 savesegment(gs, gs_sel);
1188 fs_base = segment_base(fs_sel);
1189 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001190#endif
Sean Christophersone368b872018-07-23 12:32:41 -07001191
Sean Christopherson13b964a2019-05-07 09:06:31 -07001192 vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001193 vmx->guest_state_loaded = true;
Avi Kivity33ed6322007-05-02 16:54:03 +03001194}
1195
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001196static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001197{
Sean Christophersond7ee0392018-07-23 12:32:47 -07001198 struct vmcs_host_state *host_state;
1199
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001200 if (!vmx->guest_state_loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001201 return;
1202
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001203 host_state = &vmx->loaded_vmcs->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001204
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001205 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001206
Avi Kivityc8770e72010-11-11 12:37:26 +02001207#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02001208 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02001209#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07001210 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
1211 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001212#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001213 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001214#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07001215 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001216#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001217 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07001218 if (host_state->fs_sel & 7)
1219 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001220#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07001221 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
1222 loadsegment(ds, host_state->ds_sel);
1223 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001224 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001225#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08001226 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001227#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001228 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001229#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07001230 load_fixmap_gdt(raw_smp_processor_id());
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001231 vmx->guest_state_loaded = false;
1232 vmx->guest_msrs_ready = false;
Avi Kivity33ed6322007-05-02 16:54:03 +03001233}
1234
Sean Christopherson678e3152018-07-23 12:32:43 -07001235#ifdef CONFIG_X86_64
1236static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03001237{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001238 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001239 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001240 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1241 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001242 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03001243}
1244
Sean Christopherson678e3152018-07-23 12:32:43 -07001245static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
1246{
Paolo Bonzini4679b612018-09-24 17:23:01 +02001247 preempt_disable();
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001248 if (vmx->guest_state_loaded)
Paolo Bonzini4679b612018-09-24 17:23:01 +02001249 wrmsrl(MSR_KERNEL_GS_BASE, data);
1250 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07001251 vmx->msr_guest_kernel_gs_base = data;
1252}
1253#endif
1254
Feng Wu28b835d2015-09-18 22:29:54 +08001255static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
1256{
1257 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1258 struct pi_desc old, new;
1259 unsigned int dest;
1260
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001261 /*
1262 * In case of hot-plug or hot-unplug, we may have to undo
1263 * vmx_vcpu_pi_put even if there is no assigned device. And we
1264 * always keep PI.NDST up to date for simplicity: it makes the
1265 * code easier, and CPU migration is not a fast path.
1266 */
1267 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08001268 return;
1269
Joao Martins132194f2019-11-11 17:20:11 +00001270 /*
1271 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
1272 * PI.NDST: pi_post_block is the one expected to change PID.NDST and the
1273 * wakeup handler expects the vCPU to be on the blocked_vcpu_list that
1274 * matches PI.NDST. Otherwise, a vcpu may not be able to be woken up
1275 * correctly.
1276 */
1277 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || vcpu->cpu == cpu) {
1278 pi_clear_sn(pi_desc);
1279 goto after_clear_sn;
1280 }
1281
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001282 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08001283 do {
1284 old.control = new.control = pi_desc->control;
1285
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001286 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08001287
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02001288 if (x2apic_enabled())
1289 new.ndst = dest;
1290 else
1291 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08001292
Feng Wu28b835d2015-09-18 22:29:54 +08001293 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02001294 } while (cmpxchg64(&pi_desc->control, old.control,
1295 new.control) != old.control);
Luwei Kangc112b5f2019-02-14 10:48:07 +08001296
Joao Martins132194f2019-11-11 17:20:11 +00001297after_clear_sn:
1298
Luwei Kangc112b5f2019-02-14 10:48:07 +08001299 /*
1300 * Clear SN before reading the bitmap. The VT-d firmware
1301 * writes the bitmap and reads SN atomically (5.2.3 in the
1302 * spec), so it doesn't really have a memory barrier that
1303 * pairs with this, but we cannot do that and we need one.
1304 */
1305 smp_mb__after_atomic();
1306
Joao Martins29881b62019-11-11 17:20:12 +00001307 if (!pi_is_pir_empty(pi_desc))
Luwei Kangc112b5f2019-02-14 10:48:07 +08001308 pi_set_on(pi_desc);
Feng Wu28b835d2015-09-18 22:29:54 +08001309}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001310
Sean Christopherson5c911be2020-05-01 09:31:17 -07001311void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
1312 struct loaded_vmcs *buddy)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001314 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001315 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Sean Christopherson5c911be2020-05-01 09:31:17 -07001316 struct vmcs *prev;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001318 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01001319 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001320 local_irq_disable();
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001321
1322 /*
Sean Christopherson31603d42020-03-21 12:37:49 -07001323 * Ensure loaded_vmcs->cpu is read before adding loaded_vmcs to
1324 * this cpu's percpu list, otherwise it may not yet be deleted
1325 * from its previous cpu's percpu list. Pairs with the
1326 * smb_wmb() in __loaded_vmcs_clear().
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001327 */
1328 smp_rmb();
1329
Nadav Har'Eld462b812011-05-24 15:26:10 +03001330 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1331 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001332 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001333 }
1334
Sean Christopherson5c911be2020-05-01 09:31:17 -07001335 prev = per_cpu(current_vmcs, cpu);
1336 if (prev != vmx->loaded_vmcs->vmcs) {
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001337 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1338 vmcs_load(vmx->loaded_vmcs->vmcs);
Sean Christopherson5c911be2020-05-01 09:31:17 -07001339
1340 /*
1341 * No indirect branch prediction barrier needed when switching
1342 * the active VMCS within a guest, e.g. on nested VM-Enter.
1343 * The L1 VMM can protect itself with retpolines, IBPB or IBRS.
1344 */
1345 if (!buddy || WARN_ON_ONCE(buddy->vmcs != prev))
1346 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001347 }
1348
1349 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001350 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001351 unsigned long sysenter_esp;
1352
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07001353 /*
1354 * Flush all EPTP/VPID contexts, the new pCPU may have stale
1355 * TLB entries from its previous association with the vCPU.
1356 */
Jim Mattsonb80c76e2016-07-29 18:56:53 -07001357 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001358
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 /*
1360 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001361 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08001363 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01001364 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07001365 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366
1367 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1368 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08001369
Nadav Har'Eld462b812011-05-24 15:26:10 +03001370 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371 }
Feng Wu28b835d2015-09-18 22:29:54 +08001372
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001373 /* Setup TSC multiplier */
1374 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07001375 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
1376 decache_tsc_multiplier(vmx);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001377}
1378
1379/*
1380 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1381 * vcpu mutex is already taken.
1382 */
Sean Christopherson1af1bb02020-05-06 16:58:50 -07001383static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001384{
1385 struct vcpu_vmx *vmx = to_vmx(vcpu);
1386
Sean Christopherson5c911be2020-05-01 09:31:17 -07001387 vmx_vcpu_load_vmcs(vcpu, cpu, NULL);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001388
Feng Wu28b835d2015-09-18 22:29:54 +08001389 vmx_vcpu_pi_load(vcpu, cpu);
Sean Christopherson8ef863e2019-05-07 09:06:32 -07001390
Wanpeng Li74c55932017-11-29 01:31:20 -08001391 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08001392}
1393
1394static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
1395{
1396 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1397
1398 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08001399 !irq_remapping_cap(IRQ_POSTING_CAP) ||
1400 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08001401 return;
1402
1403 /* Set SN when the vCPU is preempted */
1404 if (vcpu->preempted)
1405 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001406}
1407
Sean Christopherson13b964a2019-05-07 09:06:31 -07001408static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409{
Feng Wu28b835d2015-09-18 22:29:54 +08001410 vmx_vcpu_pi_put(vcpu);
1411
Sean Christopherson6d6095b2018-07-23 12:32:44 -07001412 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413}
1414
Wanpeng Lif244dee2017-07-20 01:11:54 -07001415static bool emulation_required(struct kvm_vcpu *vcpu)
1416{
1417 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
1418}
1419
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001420unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001422 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001423 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001424
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001425 if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
1426 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Avi Kivity6de12732011-03-07 12:51:22 +02001427 rflags = vmcs_readl(GUEST_RFLAGS);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001428 if (vmx->rmode.vm86_active) {
Avi Kivity6de12732011-03-07 12:51:22 +02001429 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
Sean Christophersone7bddc52019-09-27 14:45:18 -07001430 save_rflags = vmx->rmode.save_rflags;
Avi Kivity6de12732011-03-07 12:51:22 +02001431 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1432 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001433 vmx->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001434 }
Sean Christophersone7bddc52019-09-27 14:45:18 -07001435 return vmx->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436}
1437
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001438void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439{
Sean Christophersone7bddc52019-09-27 14:45:18 -07001440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001441 unsigned long old_rflags;
Wanpeng Lif244dee2017-07-20 01:11:54 -07001442
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001443 if (enable_unrestricted_guest) {
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07001444 kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
Sean Christopherson491c1ad2019-09-27 14:45:19 -07001445 vmx->rflags = rflags;
1446 vmcs_writel(GUEST_RFLAGS, rflags);
1447 return;
1448 }
1449
1450 old_rflags = vmx_get_rflags(vcpu);
Sean Christophersone7bddc52019-09-27 14:45:18 -07001451 vmx->rflags = rflags;
1452 if (vmx->rmode.vm86_active) {
1453 vmx->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001454 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001455 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07001457
Sean Christophersone7bddc52019-09-27 14:45:18 -07001458 if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
1459 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460}
1461
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001462u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001463{
1464 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1465 int ret = 0;
1466
1467 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001468 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001469 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001470 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001471
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001472 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001473}
1474
Sean Christopherson97b7ead2018-12-03 13:53:16 -08001475void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001476{
1477 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1478 u32 interruptibility = interruptibility_old;
1479
1480 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1481
Jan Kiszka48005f62010-02-19 19:38:07 +01001482 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001483 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001484 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001485 interruptibility |= GUEST_INTR_STATE_STI;
1486
1487 if ((interruptibility != interruptibility_old))
1488 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1489}
1490
Chao Pengbf8c55d2018-10-24 16:05:14 +08001491static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
1492{
1493 struct vcpu_vmx *vmx = to_vmx(vcpu);
1494 unsigned long value;
1495
1496 /*
1497 * Any MSR write that attempts to change bits marked reserved will
1498 * case a #GP fault.
1499 */
1500 if (data & vmx->pt_desc.ctl_bitmask)
1501 return 1;
1502
1503 /*
1504 * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
1505 * result in a #GP unless the same write also clears TraceEn.
1506 */
1507 if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
1508 ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
1509 return 1;
1510
1511 /*
1512 * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
1513 * and FabricEn would cause #GP, if
1514 * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
1515 */
1516 if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
1517 !(data & RTIT_CTL_FABRIC_EN) &&
1518 !intel_pt_validate_cap(vmx->pt_desc.caps,
1519 PT_CAP_single_range_output))
1520 return 1;
1521
1522 /*
1523 * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
1524 * utilize encodings marked reserved will casue a #GP fault.
1525 */
1526 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
1527 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
1528 !test_bit((data & RTIT_CTL_MTC_RANGE) >>
1529 RTIT_CTL_MTC_RANGE_OFFSET, &value))
1530 return 1;
1531 value = intel_pt_validate_cap(vmx->pt_desc.caps,
1532 PT_CAP_cycle_thresholds);
1533 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1534 !test_bit((data & RTIT_CTL_CYC_THRESH) >>
1535 RTIT_CTL_CYC_THRESH_OFFSET, &value))
1536 return 1;
1537 value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
1538 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
1539 !test_bit((data & RTIT_CTL_PSB_FREQ) >>
1540 RTIT_CTL_PSB_FREQ_OFFSET, &value))
1541 return 1;
1542
1543 /*
1544 * If ADDRx_CFG is reserved or the encodings is >2 will
1545 * cause a #GP fault.
1546 */
1547 value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
1548 if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
1549 return 1;
1550 value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
1551 if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
1552 return 1;
1553 value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
1554 if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
1555 return 1;
1556 value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
1557 if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
1558 return 1;
1559
1560 return 0;
1561}
1562
Sean Christopherson1957aa62019-08-27 14:40:39 -07001563static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564{
Paolo Bonzinifede8072020-04-27 11:55:59 -04001565 unsigned long rip, orig_rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566
Sean Christopherson1957aa62019-08-27 14:40:39 -07001567 /*
1568 * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
1569 * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
1570 * set when EPT misconfig occurs. In practice, real hardware updates
1571 * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
1572 * (namely Hyper-V) don't set it due to it being undefined behavior,
1573 * i.e. we end up advancing IP with some random value.
1574 */
1575 if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
1576 to_vmx(vcpu)->exit_reason != EXIT_REASON_EPT_MISCONFIG) {
Paolo Bonzinifede8072020-04-27 11:55:59 -04001577 orig_rip = kvm_rip_read(vcpu);
1578 rip = orig_rip + vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
1579#ifdef CONFIG_X86_64
1580 /*
1581 * We need to mask out the high 32 bits of RIP if not in 64-bit
1582 * mode, but just finding out that we are in 64-bit mode is
1583 * quite expensive. Only do it if there was a carry.
1584 */
1585 if (unlikely(((rip ^ orig_rip) >> 31) == 3) && !is_64_bit_mode(vcpu))
1586 rip = (u32)rip;
1587#endif
Sean Christopherson1957aa62019-08-27 14:40:39 -07001588 kvm_rip_write(vcpu, rip);
1589 } else {
1590 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
1591 return 0;
1592 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593
Glauber Costa2809f5d2009-05-12 16:21:05 -04001594 /* skipping an emulated instruction also counts */
1595 vmx_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001596
Sean Christopherson60fc3d02019-08-27 14:40:38 -07001597 return 1;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02001598}
1599
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02001600/*
1601 * Handles kvm_read/write_guest_virt*() result and either injects #PF or returns
1602 * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value
1603 * indicates whether exit to userspace is needed.
1604 */
1605int vmx_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
1606 struct x86_exception *e)
1607{
1608 if (r == X86EMUL_PROPAGATE_FAULT) {
1609 kvm_inject_emulated_page_fault(vcpu, e);
1610 return 1;
1611 }
1612
1613 /*
1614 * In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED
1615 * while handling a VMX instruction KVM could've handled the request
1616 * correctly by exiting to userspace and performing I/O but there
1617 * doesn't seem to be a real use-case behind such requests, just return
1618 * KVM_EXIT_INTERNAL_ERROR for now.
1619 */
1620 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1621 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
1622 vcpu->run->internal.ndata = 0;
1623
1624 return 0;
1625}
Oliver Upton5ef8acb2020-02-07 02:36:07 -08001626
1627/*
1628 * Recognizes a pending MTF VM-exit and records the nested state for later
1629 * delivery.
1630 */
1631static void vmx_update_emulated_instruction(struct kvm_vcpu *vcpu)
1632{
1633 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1634 struct vcpu_vmx *vmx = to_vmx(vcpu);
1635
1636 if (!is_guest_mode(vcpu))
1637 return;
1638
1639 /*
1640 * Per the SDM, MTF takes priority over debug-trap exceptions besides
1641 * T-bit traps. As instruction emulation is completed (i.e. at the
1642 * instruction boundary), any #DB exception pending delivery must be a
1643 * debug-trap. Record the pending MTF state to be delivered in
1644 * vmx_check_nested_events().
1645 */
1646 if (nested_cpu_has_mtf(vmcs12) &&
1647 (!vcpu->arch.exception.pending ||
1648 vcpu->arch.exception.nr == DB_VECTOR))
1649 vmx->nested.mtf_pending = true;
1650 else
1651 vmx->nested.mtf_pending = false;
1652}
1653
1654static int vmx_skip_emulated_instruction(struct kvm_vcpu *vcpu)
1655{
1656 vmx_update_emulated_instruction(vcpu);
1657 return skip_emulated_instruction(vcpu);
1658}
1659
Wanpeng Licaa057a2018-03-12 04:53:03 -07001660static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1661{
1662 /*
1663 * Ensure that we clear the HLT state in the VMCS. We don't need to
1664 * explicitly skip the instruction because if the HLT state is set,
1665 * then the instruction is already executing and RIP has already been
1666 * advanced.
1667 */
1668 if (kvm_hlt_in_guest(vcpu->kvm) &&
1669 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1670 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1671}
1672
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001673static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02001674{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001675 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001676 unsigned nr = vcpu->arch.exception.nr;
1677 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07001678 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001679 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001680
Jim Mattsonda998b42018-10-16 14:29:22 -07001681 kvm_deliver_exception_payload(vcpu);
1682
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001683 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001684 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001685 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1686 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001687
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001688 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001689 int inc_eip = 0;
1690 if (kvm_exception_is_soft(nr))
1691 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07001692 kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001693 return;
1694 }
1695
Sean Christophersonadd5ff72018-03-23 09:34:00 -07001696 WARN_ON_ONCE(vmx->emulation_required);
1697
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001698 if (kvm_exception_is_soft(nr)) {
1699 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1700 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001701 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1702 } else
1703 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1704
1705 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001706
1707 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001708}
1709
Avi Kivity6aa8b732006-12-10 02:21:36 -08001710/*
Eddie Donga75beee2007-05-17 18:55:15 +03001711 * Swap MSR entry in host/guest MSR entry array.
1712 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001713static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001714{
Avi Kivity26bb0982009-09-07 11:14:12 +03001715 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001716
1717 tmp = vmx->guest_msrs[to];
1718 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1719 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001720}
1721
1722/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001723 * Set up the vmcs to automatically save and restore system
1724 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1725 * mode, as fiddling with msrs is very expensive.
1726 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001727static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001728{
Avi Kivity26bb0982009-09-07 11:14:12 +03001729 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001730
Eddie Donga75beee2007-05-17 18:55:15 +03001731 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001732#ifdef CONFIG_X86_64
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001733 /*
1734 * The SYSCALL MSRs are only needed on long mode guests, and only
1735 * when EFER.SCE is set.
1736 */
1737 if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
1738 index = __find_msr_index(vmx, MSR_STAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001739 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001740 move_msr_up(vmx, index, save_nmsrs++);
1741 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001742 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001743 move_msr_up(vmx, index, save_nmsrs++);
Jim Mattson84c8c5b2018-12-05 15:29:01 -08001744 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
1745 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001746 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001747 }
Eddie Donga75beee2007-05-17 18:55:15 +03001748#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001749 index = __find_msr_index(vmx, MSR_EFER);
1750 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001751 move_msr_up(vmx, index, save_nmsrs++);
Jim Mattson0023ef32018-12-05 15:28:58 -08001752 index = __find_msr_index(vmx, MSR_TSC_AUX);
1753 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
1754 move_msr_up(vmx, index, save_nmsrs++);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001755 index = __find_msr_index(vmx, MSR_IA32_TSX_CTRL);
1756 if (index >= 0)
1757 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001758
Avi Kivity26bb0982009-09-07 11:14:12 +03001759 vmx->save_nmsrs = save_nmsrs;
Paolo Bonzinib464f57e2019-06-07 19:00:14 +02001760 vmx->guest_msrs_ready = false;
Avi Kivity58972972009-02-24 22:26:47 +02001761
Yang Zhang8d146952013-01-25 10:18:50 +08001762 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001763 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001764}
1765
Leonid Shatz326e7422018-11-06 12:14:25 +02001766static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767{
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001768 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1769 u64 g_tsc_offset = 0;
Leonid Shatz326e7422018-11-06 12:14:25 +02001770
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001771 /*
1772 * We're here if L1 chose not to trap WRMSR to TSC. According
1773 * to the spec, this should set L1's TSC; The offset that L1
1774 * set for L2 remains unchanged, and still needs to be added
1775 * to the newly set TSC to get L2's TSC.
1776 */
1777 if (is_guest_mode(vcpu) &&
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08001778 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001779 g_tsc_offset = vmcs12->tsc_offset;
1780
1781 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1782 vcpu->arch.tsc_offset - g_tsc_offset,
1783 offset);
1784 vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
1785 return offset + g_tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786}
1787
Nadav Har'El801d3422011-05-25 23:02:23 +03001788/*
1789 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1790 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1791 * all guests if the "nested" module option is off, and can also be disabled
1792 * for a single guest by disabling its VMX cpuid bit.
1793 */
Sean Christopherson7c97fcb2018-12-03 13:53:17 -08001794bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
Nadav Har'El801d3422011-05-25 23:02:23 +03001795{
Radim Krčmářd6321d42017-08-05 00:12:49 +02001796 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03001797}
1798
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001799static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
1800 uint64_t val)
1801{
1802 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
1803
1804 return !(val & ~valid_bits);
1805}
1806
Tom Lendacky801e4592018-02-21 13:39:51 -06001807static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
1808{
Paolo Bonzini13893092018-02-26 13:40:09 +01001809 switch (msr->index) {
1810 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1811 if (!nested)
1812 return 1;
1813 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
Like Xu27461da32020-05-29 15:43:45 +08001814 case MSR_IA32_PERF_CAPABILITIES:
1815 msr->data = vmx_get_perf_capabilities();
1816 return 0;
Paolo Bonzini13893092018-02-26 13:40:09 +01001817 default:
Peter Xu12bc2132020-06-22 18:04:42 -04001818 return KVM_MSR_RET_INVALID;
Paolo Bonzini13893092018-02-26 13:40:09 +01001819 }
Tom Lendacky801e4592018-02-21 13:39:51 -06001820}
1821
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001822/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823 * Reads an msr value (of 'msr_index') into 'pdata'.
1824 * Returns 0 on success, non-0 otherwise.
1825 * Assumes vcpu_load() was already called.
1826 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001827static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828{
Borislav Petkova6cb0992017-12-20 12:50:28 +01001829 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001830 struct shared_msr_entry *msr;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001831 u32 index;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001833 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001834#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001836 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837 break;
1838 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001839 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001841 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07001842 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001843 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001844#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001846 return kvm_get_msr_common(vcpu, msr_info);
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001847 case MSR_IA32_TSX_CTRL:
1848 if (!msr_info->host_initiated &&
1849 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
1850 return 1;
1851 goto find_shared_msr;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08001852 case MSR_IA32_UMWAIT_CONTROL:
1853 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
1854 return 1;
1855
1856 msr_info->data = vmx->msr_ia32_umwait_control;
1857 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001858 case MSR_IA32_SPEC_CTRL:
1859 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01001860 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
1861 return 1;
1862
1863 msr_info->data = to_vmx(vcpu)->spec_ctrl;
1864 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001865 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001866 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867 break;
1868 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001869 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870 break;
1871 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001872 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001874 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08001875 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02001876 (!msr_info->host_initiated &&
1877 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01001878 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001879 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00001880 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001881 case MSR_IA32_MCG_EXT_CTL:
1882 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01001883 !(vmx->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001884 FEAT_CTL_LMCE_ENABLED))
Jan Kiszkacae50132014-01-04 18:47:22 +01001885 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08001886 msr_info->data = vcpu->arch.mcg_ext_ctl;
1887 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08001888 case MSR_IA32_FEAT_CTL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001889 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01001890 break;
1891 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
1892 if (!nested_vmx_allowed(vcpu))
1893 return 1;
Vitaly Kuznetsov31de3d22020-02-05 13:30:33 +01001894 if (vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
1895 &msr_info->data))
1896 return 1;
1897 /*
1898 * Enlightened VMCS v1 doesn't have certain fields, but buggy
1899 * Hyper-V versions are still trying to use corresponding
1900 * features when they are exposed. Filter out the essential
1901 * minimum.
1902 */
1903 if (!msr_info->host_initiated &&
1904 vmx->nested.enlightened_vmcs_enabled)
1905 nested_evmcs_filter_control_msr(msr_info->index,
1906 &msr_info->data);
1907 break;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001908 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001909 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001910 return 1;
1911 msr_info->data = vmx->pt_desc.guest.ctl;
1912 break;
1913 case MSR_IA32_RTIT_STATUS:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001914 if (!vmx_pt_mode_is_host_guest())
Chao Pengbf8c55d2018-10-24 16:05:14 +08001915 return 1;
1916 msr_info->data = vmx->pt_desc.guest.status;
1917 break;
1918 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001919 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001920 !intel_pt_validate_cap(vmx->pt_desc.caps,
1921 PT_CAP_cr3_filtering))
1922 return 1;
1923 msr_info->data = vmx->pt_desc.guest.cr3_match;
1924 break;
1925 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001926 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001927 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1928 PT_CAP_topa_output) &&
1929 !intel_pt_validate_cap(vmx->pt_desc.caps,
1930 PT_CAP_single_range_output)))
1931 return 1;
1932 msr_info->data = vmx->pt_desc.guest.output_base;
1933 break;
1934 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christopherson2ef76192020-03-02 15:56:22 -08001935 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001936 (!intel_pt_validate_cap(vmx->pt_desc.caps,
1937 PT_CAP_topa_output) &&
1938 !intel_pt_validate_cap(vmx->pt_desc.caps,
1939 PT_CAP_single_range_output)))
1940 return 1;
1941 msr_info->data = vmx->pt_desc.guest.output_mask;
1942 break;
1943 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
1944 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christopherson2ef76192020-03-02 15:56:22 -08001945 if (!vmx_pt_mode_is_host_guest() ||
Chao Pengbf8c55d2018-10-24 16:05:14 +08001946 (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
1947 PT_CAP_num_address_ranges)))
1948 return 1;
1949 if (index % 2)
1950 msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
1951 else
1952 msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
1953 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001954 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02001955 if (!msr_info->host_initiated &&
1956 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001957 return 1;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001958 goto find_shared_msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 default:
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05001960 find_shared_msr:
Borislav Petkova6cb0992017-12-20 12:50:28 +01001961 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001962 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001963 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08001964 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02001966 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967 }
1968
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969 return 0;
1970}
1971
Sean Christopherson24085002020-04-28 16:10:24 -07001972static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,
1973 u64 data)
1974{
1975#ifdef CONFIG_X86_64
1976 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
1977 return (u32)data;
1978#endif
1979 return (unsigned long)data;
1980}
1981
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982/*
Miaohe Lin311497e2019-12-11 14:26:25 +08001983 * Writes msr value into the appropriate "register".
Avi Kivity6aa8b732006-12-10 02:21:36 -08001984 * Returns 0 on success, non-0 otherwise.
1985 * Assumes vcpu_load() was already called.
1986 */
Will Auld8fe8ab42012-11-29 12:42:12 -08001987static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001989 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001990 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001991 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08001992 u32 msr_index = msr_info->index;
1993 u64 data = msr_info->data;
Chao Pengbf8c55d2018-10-24 16:05:14 +08001994 u32 index;
Eddie Dong2cc51562007-05-21 07:28:09 +03001995
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001997 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08001998 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03001999 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002000#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002001 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002002 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003 vmcs_writel(GUEST_FS_BASE, data);
2004 break;
2005 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002006 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002007 vmcs_writel(GUEST_GS_BASE, data);
2008 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002009 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07002010 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002011 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012#endif
2013 case MSR_IA32_SYSENTER_CS:
Sean Christophersonde70d272019-05-07 09:06:36 -07002014 if (is_guest_mode(vcpu))
2015 get_vmcs12(vcpu)->guest_sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016 vmcs_write32(GUEST_SYSENTER_CS, data);
2017 break;
2018 case MSR_IA32_SYSENTER_EIP:
Sean Christopherson24085002020-04-28 16:10:24 -07002019 if (is_guest_mode(vcpu)) {
2020 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002021 get_vmcs12(vcpu)->guest_sysenter_eip = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002022 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002023 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 break;
2025 case MSR_IA32_SYSENTER_ESP:
Sean Christopherson24085002020-04-28 16:10:24 -07002026 if (is_guest_mode(vcpu)) {
2027 data = nested_vmx_truncate_sysenter_addr(vcpu, data);
Sean Christophersonde70d272019-05-07 09:06:36 -07002028 get_vmcs12(vcpu)->guest_sysenter_esp = data;
Sean Christopherson24085002020-04-28 16:10:24 -07002029 }
Avi Kivityf5b42c32007-03-06 12:05:53 +02002030 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031 break;
Sean Christopherson699a1ac2019-05-07 09:06:37 -07002032 case MSR_IA32_DEBUGCTLMSR:
2033 if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
2034 VM_EXIT_SAVE_DEBUG_CONTROLS)
2035 get_vmcs12(vcpu)->guest_ia32_debugctl = data;
2036
2037 ret = kvm_set_msr_common(vcpu, msr_info);
2038 break;
2039
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002040 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08002041 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02002042 (!msr_info->host_initiated &&
2043 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002044 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08002045 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07002046 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002048 vmcs_write64(GUEST_BNDCFGS, data);
2049 break;
Tao Xu6e3ba4a2019-07-16 14:55:50 +08002050 case MSR_IA32_UMWAIT_CONTROL:
2051 if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
2052 return 1;
2053
2054 /* The reserved bit 1 and non-32 bit [63:32] should be zero */
2055 if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
2056 return 1;
2057
2058 vmx->msr_ia32_umwait_control = data;
2059 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002060 case MSR_IA32_SPEC_CTRL:
2061 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002062 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2063 return 1;
2064
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002065 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002066 return 1;
2067
2068 vmx->spec_ctrl = data;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002069 if (!data)
2070 break;
2071
2072 /*
2073 * For non-nested:
2074 * When it's written (to non-zero) for the first time, pass
2075 * it through.
2076 *
2077 * For nested:
2078 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002079 * nested_vmx_prepare_msr_bitmap. We should not touch the
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002080 * vmcs02.msr_bitmap here since it gets completely overwritten
2081 * in the merging. We update the vmcs01 here for L1 as well
2082 * since it will end up touching the MSR anyway now.
2083 */
2084 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
2085 MSR_IA32_SPEC_CTRL,
2086 MSR_TYPE_RW);
2087 break;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002088 case MSR_IA32_TSX_CTRL:
2089 if (!msr_info->host_initiated &&
2090 !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2091 return 1;
2092 if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
2093 return 1;
2094 goto find_shared_msr;
Ashok Raj15d45072018-02-01 22:59:43 +01002095 case MSR_IA32_PRED_CMD:
2096 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01002097 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2098 return 1;
2099
2100 if (data & ~PRED_CMD_IBPB)
2101 return 1;
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002102 if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL))
2103 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002104 if (!data)
2105 break;
2106
2107 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2108
2109 /*
2110 * For non-nested:
2111 * When it's written (to non-zero) for the first time, pass
2112 * it through.
2113 *
2114 * For nested:
2115 * The handling of the MSR bitmap for L2 guests is done in
Miaohe Lin4d516fe2019-12-11 14:26:21 +08002116 * nested_vmx_prepare_msr_bitmap. We should not touch the
Ashok Raj15d45072018-02-01 22:59:43 +01002117 * vmcs02.msr_bitmap here since it gets completely overwritten
2118 * in the merging.
2119 */
2120 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
2121 MSR_TYPE_W);
2122 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002123 case MSR_IA32_CR_PAT:
Sean Christophersond28f4292019-05-07 09:06:27 -07002124 if (!kvm_pat_valid(data))
2125 return 1;
2126
Sean Christopherson142e4be2019-05-07 09:06:35 -07002127 if (is_guest_mode(vcpu) &&
2128 get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
2129 get_vmcs12(vcpu)->guest_ia32_pat = data;
2130
Sheng Yang468d4722008-10-09 16:01:55 +08002131 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2132 vmcs_write64(GUEST_IA32_PAT, data);
2133 vcpu->arch.pat = data;
2134 break;
2135 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002136 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002137 break;
Will Auldba904632012-11-29 12:42:50 -08002138 case MSR_IA32_TSC_ADJUST:
2139 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002140 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002141 case MSR_IA32_MCG_EXT_CTL:
2142 if ((!msr_info->host_initiated &&
2143 !(to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002144 FEAT_CTL_LMCE_ENABLED)) ||
Ashok Rajc45dcc72016-06-22 14:59:56 +08002145 (data & ~MCG_EXT_CTL_LMCE_EN))
2146 return 1;
2147 vcpu->arch.mcg_ext_ctl = data;
2148 break;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002149 case MSR_IA32_FEAT_CTL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002150 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08002151 (to_vmx(vcpu)->msr_ia32_feature_control &
Sean Christopherson32ad73d2019-12-20 20:44:55 -08002152 FEAT_CTL_LOCKED && !msr_info->host_initiated))
Jan Kiszkacae50132014-01-04 18:47:22 +01002153 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08002154 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01002155 if (msr_info->host_initiated && data == 0)
2156 vmx_leave_nested(vcpu);
2157 break;
2158 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08002159 if (!msr_info->host_initiated)
2160 return 1; /* they are read-only */
2161 if (!nested_vmx_allowed(vcpu))
2162 return 1;
2163 return vmx_set_vmx_msr(vcpu, msr_index, data);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002164 case MSR_IA32_RTIT_CTL:
Sean Christopherson2ef76192020-03-02 15:56:22 -08002165 if (!vmx_pt_mode_is_host_guest() ||
Luwei Kangee85dec2018-10-24 16:05:16 +08002166 vmx_rtit_ctl_check(vcpu, data) ||
2167 vmx->nested.vmxon)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002168 return 1;
2169 vmcs_write64(GUEST_IA32_RTIT_CTL, data);
2170 vmx->pt_desc.guest.ctl = data;
Chao Pengb08c2892018-10-24 16:05:15 +08002171 pt_update_intercept_for_msr(vmx);
Chao Pengbf8c55d2018-10-24 16:05:14 +08002172 break;
2173 case MSR_IA32_RTIT_STATUS:
Sean Christophersone348ac72019-12-10 15:24:33 -08002174 if (!pt_can_write_msr(vmx))
2175 return 1;
2176 if (data & MSR_IA32_RTIT_STATUS_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002177 return 1;
2178 vmx->pt_desc.guest.status = data;
2179 break;
2180 case MSR_IA32_RTIT_CR3_MATCH:
Sean Christophersone348ac72019-12-10 15:24:33 -08002181 if (!pt_can_write_msr(vmx))
2182 return 1;
2183 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2184 PT_CAP_cr3_filtering))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002185 return 1;
2186 vmx->pt_desc.guest.cr3_match = data;
2187 break;
2188 case MSR_IA32_RTIT_OUTPUT_BASE:
Sean Christophersone348ac72019-12-10 15:24:33 -08002189 if (!pt_can_write_msr(vmx))
2190 return 1;
2191 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2192 PT_CAP_topa_output) &&
2193 !intel_pt_validate_cap(vmx->pt_desc.caps,
2194 PT_CAP_single_range_output))
2195 return 1;
2196 if (data & MSR_IA32_RTIT_OUTPUT_BASE_MASK)
Chao Pengbf8c55d2018-10-24 16:05:14 +08002197 return 1;
2198 vmx->pt_desc.guest.output_base = data;
2199 break;
2200 case MSR_IA32_RTIT_OUTPUT_MASK:
Sean Christophersone348ac72019-12-10 15:24:33 -08002201 if (!pt_can_write_msr(vmx))
2202 return 1;
2203 if (!intel_pt_validate_cap(vmx->pt_desc.caps,
2204 PT_CAP_topa_output) &&
2205 !intel_pt_validate_cap(vmx->pt_desc.caps,
2206 PT_CAP_single_range_output))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002207 return 1;
2208 vmx->pt_desc.guest.output_mask = data;
2209 break;
2210 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
Sean Christophersone348ac72019-12-10 15:24:33 -08002211 if (!pt_can_write_msr(vmx))
2212 return 1;
Chao Pengbf8c55d2018-10-24 16:05:14 +08002213 index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
Sean Christophersone348ac72019-12-10 15:24:33 -08002214 if (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2215 PT_CAP_num_address_ranges))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002216 return 1;
Sean Christophersonfe6ed362019-12-10 15:24:32 -08002217 if (is_noncanonical_address(data, vcpu))
Chao Pengbf8c55d2018-10-24 16:05:14 +08002218 return 1;
2219 if (index % 2)
2220 vmx->pt_desc.guest.addr_b[index / 2] = data;
2221 else
2222 vmx->pt_desc.guest.addr_a[index / 2] = data;
2223 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002224 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02002225 if (!msr_info->host_initiated &&
2226 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002227 return 1;
2228 /* Check reserved bit, higher 32 bits should be zero */
2229 if ((data >> 32) != 0)
2230 return 1;
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002231 goto find_shared_msr;
2232
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233 default:
Paolo Bonzinic11f83e2019-11-18 12:23:00 -05002234 find_shared_msr:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002235 msr = find_msr_entry(vmx, msr_index);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05002236 if (msr)
2237 ret = vmx_set_guest_msr(vmx, msr, data);
2238 else
2239 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 }
2241
Eddie Dong2cc51562007-05-21 07:28:09 +03002242 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243}
2244
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002245static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246{
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002247 unsigned long guest_owned_bits;
2248
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002249 kvm_register_mark_available(vcpu, reg);
2250
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002251 switch (reg) {
2252 case VCPU_REGS_RSP:
2253 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2254 break;
2255 case VCPU_REGS_RIP:
2256 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2257 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002258 case VCPU_EXREG_PDPTR:
2259 if (enable_ept)
2260 ept_save_pdptrs(vcpu);
2261 break;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07002262 case VCPU_EXREG_CR0:
2263 guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2264
2265 vcpu->arch.cr0 &= ~guest_owned_bits;
2266 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & guest_owned_bits;
2267 break;
Sean Christopherson34059c22019-09-27 14:45:23 -07002268 case VCPU_EXREG_CR3:
2269 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
2270 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2271 break;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07002272 case VCPU_EXREG_CR4:
2273 guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2274
2275 vcpu->arch.cr4 &= ~guest_owned_bits;
2276 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & guest_owned_bits;
2277 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002278 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07002279 WARN_ON_ONCE(1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002280 break;
2281 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282}
2283
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284static __init int cpu_has_kvm_support(void)
2285{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002286 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287}
2288
2289static __init int vmx_disabled_by_bios(void)
2290{
Sean Christophersona4d0b2f2019-12-20 20:45:09 -08002291 return !boot_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
2292 !boot_cpu_has(X86_FEATURE_VMX);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293}
2294
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002295static int kvm_cpu_vmxon(u64 vmxon_pointer)
Dongxiao Xu7725b892010-05-11 18:29:38 +08002296{
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002297 u64 msr;
2298
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002299 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002300 intel_pt_handle_vmx(1);
2301
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002302 asm_volatile_goto("1: vmxon %[vmxon_pointer]\n\t"
2303 _ASM_EXTABLE(1b, %l[fault])
2304 : : [vmxon_pointer] "m"(vmxon_pointer)
2305 : : fault);
2306 return 0;
2307
2308fault:
2309 WARN_ONCE(1, "VMXON faulted, MSR_IA32_FEAT_CTL (0x3a) = 0x%llx\n",
2310 rdmsrl_safe(MSR_IA32_FEAT_CTL, &msr) ? 0xdeadbeef : msr);
2311 intel_pt_handle_vmx(0);
2312 cr4_clear_bits(X86_CR4_VMXE);
2313
2314 return -EFAULT;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002315}
2316
Radim Krčmář13a34e02014-08-28 15:13:03 +02002317static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318{
2319 int cpu = raw_smp_processor_id();
2320 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002321 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002323 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02002324 return -EBUSY;
2325
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002326 /*
2327 * This can happen if we hot-added a CPU but failed to allocate
2328 * VP assist page for it.
2329 */
2330 if (static_branch_unlikely(&enable_evmcs) &&
2331 !hv_get_vp_assist_page(cpu))
2332 return -EFAULT;
2333
Sean Christopherson4f6ea0a2020-03-21 12:37:51 -07002334 r = kvm_cpu_vmxon(phys_addr);
2335 if (r)
2336 return r;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002337
David Hildenbrandfdf288b2017-08-24 20:51:29 +02002338 if (enable_ept)
2339 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02002340
2341 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342}
2343
Nadav Har'Eld462b812011-05-24 15:26:10 +03002344static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002345{
2346 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002347 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002348
Nadav Har'Eld462b812011-05-24 15:26:10 +03002349 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2350 loaded_vmcss_on_cpu_link)
2351 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002352}
2353
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002354
2355/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2356 * tricks.
2357 */
2358static void kvm_cpu_vmxoff(void)
2359{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002360 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03002361
2362 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002363 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002364}
2365
Radim Krčmář13a34e02014-08-28 15:13:03 +02002366static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002368 vmclear_local_loaded_vmcss();
2369 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370}
2371
Sean Christopherson7a57c092020-03-12 11:04:16 -07002372/*
2373 * There is no X86_FEATURE for SGX yet, but anyway we need to query CPUID
2374 * directly instead of going through cpu_has(), to ensure KVM is trapping
2375 * ENCLS whenever it's supported in hardware. It does not matter whether
2376 * the host OS supports or has enabled SGX.
2377 */
2378static bool cpu_has_sgx(void)
2379{
2380 return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0));
2381}
2382
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002383static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002384 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385{
2386 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002387 u32 ctl = ctl_min | ctl_opt;
2388
2389 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2390
2391 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2392 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2393
2394 /* Ensure minimum (required) set of control bits are supported. */
2395 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002396 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002397
2398 *result = ctl;
2399 return 0;
2400}
2401
Sean Christopherson7caaa712018-12-03 13:53:01 -08002402static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
2403 struct vmx_capability *vmx_cap)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002404{
2405 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002406 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002407 u32 _pin_based_exec_control = 0;
2408 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002409 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002410 u32 _vmexit_control = 0;
2411 u32 _vmentry_control = 0;
2412
Paolo Bonzini13893092018-02-26 13:40:09 +01002413 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05302414 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002415#ifdef CONFIG_X86_64
2416 CPU_BASED_CR8_LOAD_EXITING |
2417 CPU_BASED_CR8_STORE_EXITING |
2418#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002419 CPU_BASED_CR3_LOAD_EXITING |
2420 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08002421 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002422 CPU_BASED_MOV_DR_EXITING |
Xiaoyao Li5e3d3942019-12-06 16:45:26 +08002423 CPU_BASED_USE_TSC_OFFSETTING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07002424 CPU_BASED_MWAIT_EXITING |
2425 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002426 CPU_BASED_INVLPG_EXITING |
2427 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002428
Sheng Yangf78e0e22007-10-29 09:40:42 +08002429 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002430 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002431 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002432 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2433 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002434 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002435#ifdef CONFIG_X86_64
2436 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2437 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2438 ~CPU_BASED_CR8_STORE_EXITING;
2439#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002440 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002441 min2 = 0;
2442 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002443 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002444 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002445 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002446 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002447 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002448 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02002449 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00002450 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002451 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002452 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002453 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08002454 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08002455 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02002456 SECONDARY_EXEC_RDSEED_EXITING |
2457 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002458 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04002459 SECONDARY_EXEC_TSC_SCALING |
Tao Xue69e72fa2019-07-16 14:55:49 +08002460 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
Chao Pengf99e3da2018-10-24 16:05:10 +08002461 SECONDARY_EXEC_PT_USE_GPA |
2462 SECONDARY_EXEC_PT_CONCEAL_VMX |
Sean Christopherson7a57c092020-03-12 11:04:16 -07002463 SECONDARY_EXEC_ENABLE_VMFUNC;
2464 if (cpu_has_sgx())
2465 opt2 |= SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08002466 if (adjust_vmx_controls(min2, opt2,
2467 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002468 &_cpu_based_2nd_exec_control) < 0)
2469 return -EIO;
2470 }
2471#ifndef CONFIG_X86_64
2472 if (!(_cpu_based_2nd_exec_control &
2473 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2474 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2475#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002476
2477 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2478 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002479 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002480 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2481 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002482
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002483 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
Sean Christopherson7caaa712018-12-03 13:53:01 -08002484 &vmx_cap->ept, &vmx_cap->vpid);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002485
Sheng Yangd56f5462008-04-25 10:13:16 +08002486 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002487 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2488 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002489 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2490 CPU_BASED_CR3_STORE_EXITING |
2491 CPU_BASED_INVLPG_EXITING);
Sean Christopherson7caaa712018-12-03 13:53:01 -08002492 } else if (vmx_cap->ept) {
2493 vmx_cap->ept = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002494 pr_warn_once("EPT CAP should not exist if not support "
2495 "1-setting enable EPT VM-execution control\n");
2496 }
2497 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
Sean Christopherson7caaa712018-12-03 13:53:01 -08002498 vmx_cap->vpid) {
2499 vmx_cap->vpid = 0;
Wanpeng Li61f1dd92017-10-18 16:02:19 -07002500 pr_warn_once("VPID CAP should not exist if not support "
2501 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08002502 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002503
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002504 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002505#ifdef CONFIG_X86_64
2506 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2507#endif
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002508 opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002509 VM_EXIT_LOAD_IA32_PAT |
2510 VM_EXIT_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002511 VM_EXIT_CLEAR_BNDCFGS |
2512 VM_EXIT_PT_CONCEAL_PIP |
2513 VM_EXIT_CLEAR_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002514 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2515 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002516 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002517
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01002518 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2519 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
2520 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002521 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2522 &_pin_based_exec_control) < 0)
2523 return -EIO;
2524
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02002525 if (cpu_has_broken_vmx_preemption_timer())
2526 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08002527 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02002528 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08002529 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2530
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002531 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002532 opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
2533 VM_ENTRY_LOAD_IA32_PAT |
2534 VM_ENTRY_LOAD_IA32_EFER |
Chao Pengf99e3da2018-10-24 16:05:10 +08002535 VM_ENTRY_LOAD_BNDCFGS |
2536 VM_ENTRY_PT_CONCEAL_PIP |
2537 VM_ENTRY_LOAD_IA32_RTIT_CTL;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002538 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2539 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002540 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002542 /*
2543 * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
2544 * can't be used due to an errata where VM Exit may incorrectly clear
2545 * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
2546 * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2547 */
2548 if (boot_cpu_data.x86 == 0x6) {
2549 switch (boot_cpu_data.x86_model) {
2550 case 26: /* AAK155 */
2551 case 30: /* AAP115 */
2552 case 37: /* AAT100 */
2553 case 44: /* BC86,AAY89,BD102 */
2554 case 46: /* BA97 */
Sean Christopherson85ba2b12019-01-14 12:12:02 -08002555 _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
Sean Christophersonc73da3f2018-12-03 13:53:00 -08002556 _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
2557 pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2558 "does not work properly. Using workaround\n");
2559 break;
2560 default:
2561 break;
2562 }
2563 }
2564
2565
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002566 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002567
2568 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2569 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002570 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002571
2572#ifdef CONFIG_X86_64
2573 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2574 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002575 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002576#endif
2577
2578 /* Require Write-Back (WB) memory type for VMCS accesses. */
2579 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002580 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002581
Yang, Sheng002c7f72007-07-31 14:23:01 +03002582 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02002583 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002584 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002585
Liran Alon2307af12018-06-29 22:59:04 +03002586 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002587
Yang, Sheng002c7f72007-07-31 14:23:01 +03002588 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2589 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002590 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002591 vmcs_conf->vmexit_ctrl = _vmexit_control;
2592 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002593
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002594 if (static_branch_unlikely(&enable_evmcs))
2595 evmcs_sanitize_exec_ctrls(vmcs_conf);
2596
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002597 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002598}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599
Ben Gardon41836832019-02-11 11:02:52 -08002600struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601{
2602 int node = cpu_to_node(cpu);
2603 struct page *pages;
2604 struct vmcs *vmcs;
2605
Ben Gardon41836832019-02-11 11:02:52 -08002606 pages = __alloc_pages_node(node, flags, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 if (!pages)
2608 return NULL;
2609 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002610 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03002611
2612 /* KVM supports Enlightened VMCS v1 only */
2613 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002614 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03002615 else
Liran Alon392b2f22018-06-23 02:35:01 +03002616 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002617
Liran Alon491a6032018-06-23 02:35:12 +03002618 if (shadow)
2619 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 return vmcs;
2621}
2622
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002623void free_vmcs(struct vmcs *vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002625 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626}
2627
Nadav Har'Eld462b812011-05-24 15:26:10 +03002628/*
2629 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2630 */
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002631void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002632{
2633 if (!loaded_vmcs->vmcs)
2634 return;
2635 loaded_vmcs_clear(loaded_vmcs);
2636 free_vmcs(loaded_vmcs->vmcs);
2637 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002638 if (loaded_vmcs->msr_bitmap)
2639 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002640 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002641}
2642
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08002643int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002644{
Liran Alon491a6032018-06-23 02:35:12 +03002645 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002646 if (!loaded_vmcs->vmcs)
2647 return -ENOMEM;
2648
Sean Christophersond260f9e2020-03-21 12:37:50 -07002649 vmcs_clear(loaded_vmcs->vmcs);
2650
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002651 loaded_vmcs->shadow_vmcs = NULL;
Sean Christopherson804939e2019-05-07 12:18:05 -07002652 loaded_vmcs->hv_timer_soft_disabled = false;
Sean Christophersond260f9e2020-03-21 12:37:50 -07002653 loaded_vmcs->cpu = -1;
2654 loaded_vmcs->launched = 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002655
2656 if (cpu_has_vmx_msr_bitmap()) {
Ben Gardon41836832019-02-11 11:02:52 -08002657 loaded_vmcs->msr_bitmap = (unsigned long *)
2658 __get_free_page(GFP_KERNEL_ACCOUNT);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002659 if (!loaded_vmcs->msr_bitmap)
2660 goto out_vmcs;
2661 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002662
Arnd Bergmann1f008e12018-05-25 17:36:17 +02002663 if (IS_ENABLED(CONFIG_HYPERV) &&
2664 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02002665 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
2666 struct hv_enlightened_vmcs *evmcs =
2667 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
2668
2669 evmcs->hv_enlightenments_control.msr_bitmap = 1;
2670 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002671 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002672
2673 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
Sean Christopherson3af80fe2019-05-07 12:18:00 -07002674 memset(&loaded_vmcs->controls_shadow, 0,
2675 sizeof(struct vmcs_controls_shadow));
Sean Christophersond7ee0392018-07-23 12:32:47 -07002676
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002677 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01002678
2679out_vmcs:
2680 free_loaded_vmcs(loaded_vmcs);
2681 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01002682}
2683
Sam Ravnborg39959582007-06-01 00:47:13 -07002684static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002685{
2686 int cpu;
2687
Zachary Amsden3230bb42009-09-29 11:38:37 -10002688 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002690 per_cpu(vmxarea, cpu) = NULL;
2691 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692}
2693
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694static __init int alloc_kvm_area(void)
2695{
2696 int cpu;
2697
Zachary Amsden3230bb42009-09-29 11:38:37 -10002698 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 struct vmcs *vmcs;
2700
Ben Gardon41836832019-02-11 11:02:52 -08002701 vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 if (!vmcs) {
2703 free_kvm_area();
2704 return -ENOMEM;
2705 }
2706
Liran Alon2307af12018-06-29 22:59:04 +03002707 /*
2708 * When eVMCS is enabled, alloc_vmcs_cpu() sets
2709 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
2710 * revision_id reported by MSR_IA32_VMX_BASIC.
2711 *
Linus Torvalds312a4662018-12-26 17:03:51 -08002712 * However, even though not explicitly documented by
Liran Alon2307af12018-06-29 22:59:04 +03002713 * TLFS, VMXArea passed as VMXON argument should
2714 * still be marked with revision_id reported by
2715 * physical CPU.
2716 */
2717 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03002718 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03002719
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 per_cpu(vmxarea, cpu) = vmcs;
2721 }
2722 return 0;
2723}
2724
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002725static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002726 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002728 if (!emulate_invalid_guest_state) {
2729 /*
2730 * CS and SS RPL should be equal during guest entry according
2731 * to VMX spec, but in reality it is not always so. Since vcpu
2732 * is in the middle of the transition from real mode to
2733 * protected mode it is safe to assume that RPL 0 is a good
2734 * default value.
2735 */
2736 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03002737 save->selector &= ~SEGMENT_RPL_MASK;
2738 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02002739 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002741 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742}
2743
2744static void enter_pmode(struct kvm_vcpu *vcpu)
2745{
2746 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002747 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748
Gleb Natapovd99e4152012-12-20 16:57:45 +02002749 /*
2750 * Update real mode segment cache. It may be not up-to-date if sement
2751 * register was written while vcpu was in a guest mode.
2752 */
2753 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2754 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2755 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2756 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2757 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2758 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2759
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002760 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002762 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763
2764 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002765 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2766 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002767 vmcs_writel(GUEST_RFLAGS, flags);
2768
Rusty Russell66aee912007-07-17 23:34:16 +10002769 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2770 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771
2772 update_exception_bitmap(vcpu);
2773
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002774 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2775 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2776 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2777 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2778 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2779 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780}
2781
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002782static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783{
Mathias Krause772e0312012-08-30 01:30:19 +02002784 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002785 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786
Gleb Natapovd99e4152012-12-20 16:57:45 +02002787 var.dpl = 0x3;
2788 if (seg == VCPU_SREG_CS)
2789 var.type = 0x3;
2790
2791 if (!emulate_invalid_guest_state) {
2792 var.selector = var.base >> 4;
2793 var.base = var.base & 0xffff0;
2794 var.limit = 0xffff;
2795 var.g = 0;
2796 var.db = 0;
2797 var.present = 1;
2798 var.s = 1;
2799 var.l = 0;
2800 var.unusable = 0;
2801 var.type = 0x3;
2802 var.avl = 0;
2803 if (save->base & 0xf)
2804 printk_once(KERN_WARNING "kvm: segment base is not "
2805 "paragraph aligned when entering "
2806 "protected mode (seg=%d)", seg);
2807 }
2808
2809 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05002810 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02002811 vmcs_write32(sf->limit, var.limit);
2812 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813}
2814
2815static void enter_rmode(struct kvm_vcpu *vcpu)
2816{
2817 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002818 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002819 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002821 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2822 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2823 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2824 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2825 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002826 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2827 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002828
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002829 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830
Gleb Natapov776e58e2011-03-13 12:34:27 +02002831 /*
2832 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01002833 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02002834 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002835 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02002836 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2837 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02002838
Avi Kivity2fb92db2011-04-27 19:42:18 +03002839 vmx_segment_cache_clear(vmx);
2840
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07002841 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2844
2845 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002846 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002848 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849
2850 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002851 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002852 update_exception_bitmap(vcpu);
2853
Gleb Natapovd99e4152012-12-20 16:57:45 +02002854 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2855 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2856 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2857 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2858 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2859 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002860
Eddie Dong8668a3c2007-10-10 14:26:45 +08002861 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862}
2863
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002864void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Amit Shah401d10d2009-02-20 22:53:37 +05302865{
2866 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002867 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2868
2869 if (!msr)
2870 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302871
Avi Kivityf6801df2010-01-21 15:31:50 +02002872 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302873 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002874 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302875 msr->data = efer;
2876 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002877 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05302878
2879 msr->data = efer & ~EFER_LME;
2880 }
2881 setup_msrs(vmx);
2882}
2883
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002884#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885
2886static void enter_lmode(struct kvm_vcpu *vcpu)
2887{
2888 u32 guest_tr_ar;
2889
Avi Kivity2fb92db2011-04-27 19:42:18 +03002890 vmx_segment_cache_clear(to_vmx(vcpu));
2891
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002893 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002894 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2895 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07002897 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
2898 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 }
Avi Kivityda38f432010-07-06 11:30:49 +03002900 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901}
2902
2903static void exit_lmode(struct kvm_vcpu *vcpu)
2904{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002905 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002906 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907}
2908
2909#endif
2910
Sean Christopherson77809382020-03-20 14:28:18 -07002911static void vmx_flush_tlb_all(struct kvm_vcpu *vcpu)
Sean Christopherson5058b692020-03-20 14:28:14 -07002912{
2913 struct vcpu_vmx *vmx = to_vmx(vcpu);
2914
2915 /*
Sean Christopherson77809382020-03-20 14:28:18 -07002916 * INVEPT must be issued when EPT is enabled, irrespective of VPID, as
2917 * the CPU is not required to invalidate guest-physical mappings on
2918 * VM-Entry, even if VPID is disabled. Guest-physical mappings are
2919 * associated with the root EPT structure and not any particular VPID
2920 * (INVVPID also isn't required to invalidate guest-physical mappings).
Sean Christopherson5058b692020-03-20 14:28:14 -07002921 */
2922 if (enable_ept) {
2923 ept_sync_global();
2924 } else if (enable_vpid) {
2925 if (cpu_has_vmx_invvpid_global()) {
2926 vpid_sync_vcpu_global();
2927 } else {
2928 vpid_sync_vcpu_single(vmx->vpid);
2929 vpid_sync_vcpu_single(vmx->nested.vpid02);
2930 }
2931 }
2932}
2933
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002934static void vmx_flush_tlb_current(struct kvm_vcpu *vcpu)
2935{
2936 u64 root_hpa = vcpu->arch.mmu->root_hpa;
2937
2938 /* No flush required if the current context is invalid. */
2939 if (!VALID_PAGE(root_hpa))
2940 return;
2941
2942 if (enable_ept)
2943 ept_sync_context(construct_eptp(vcpu, root_hpa));
2944 else if (!is_guest_mode(vcpu))
2945 vpid_sync_context(to_vmx(vcpu)->vpid);
2946 else
2947 vpid_sync_context(nested_get_vpid02(vcpu));
2948}
2949
Junaid Shahidfaff8752018-06-29 13:10:05 -07002950static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2951{
Junaid Shahidfaff8752018-06-29 13:10:05 -07002952 /*
Sean Christophersonad104b52020-03-20 14:28:11 -07002953 * vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in
2954 * vmx_flush_tlb_guest() for an explanation of why this is ok.
Junaid Shahidfaff8752018-06-29 13:10:05 -07002955 */
Sean Christophersonad104b52020-03-20 14:28:11 -07002956 vpid_sync_vcpu_addr(to_vmx(vcpu)->vpid, addr);
Junaid Shahidfaff8752018-06-29 13:10:05 -07002957}
2958
Sean Christophersone64419d2020-03-20 14:28:10 -07002959static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu)
2960{
2961 /*
2962 * vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0
2963 * or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit
2964 * are required to flush GVA->{G,H}PA mappings from the TLB if vpid is
2965 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed),
2966 * i.e. no explicit INVVPID is necessary.
2967 */
2968 vpid_sync_context(to_vmx(vcpu)->vpid);
2969}
2970
Sheng Yang14394422008-04-28 12:24:45 +08002971static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2972{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002973 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2974
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002975 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002976 return;
2977
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02002978 if (is_pae_paging(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002979 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2980 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2981 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
2982 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002983 }
2984}
2985
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002986void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03002987{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002988 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2989
Sean Christopherson9932b492020-04-15 13:34:50 -07002990 if (WARN_ON_ONCE(!is_pae_paging(vcpu)))
2991 return;
2992
2993 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2994 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2995 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2996 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002997
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002998 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002999}
3000
Sheng Yang14394422008-04-28 12:24:45 +08003001static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3002 unsigned long cr0,
3003 struct kvm_vcpu *vcpu)
3004{
Sean Christopherson2183f562019-05-07 12:17:56 -07003005 struct vcpu_vmx *vmx = to_vmx(vcpu);
3006
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003007 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
Sean Christopherson34059c22019-09-27 14:45:23 -07003008 vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003009 if (!(cr0 & X86_CR0_PG)) {
3010 /* From paging/starting to nonpaging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003011 exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3012 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003013 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003014 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003015 } else if (!is_paging(vcpu)) {
3016 /* From nonpaging to paging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003017 exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3018 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003019 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003020 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003021 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003022
3023 if (!(cr0 & X86_CR0_WP))
3024 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003025}
3026
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003027void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003029 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003030 unsigned long hw_cr0;
3031
Sean Christopherson3de63472018-07-13 08:42:30 -07003032 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003033 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003034 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003035 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003036 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003037
Gleb Natapov218e7632013-01-21 15:36:45 +02003038 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3039 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040
Gleb Natapov218e7632013-01-21 15:36:45 +02003041 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3042 enter_rmode(vcpu);
3043 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003045#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003046 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003047 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003049 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 exit_lmode(vcpu);
3051 }
3052#endif
3053
Sean Christophersonb4d18512018-03-05 12:04:40 -08003054 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08003055 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3056
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003058 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003059 vcpu->arch.cr0 = cr0;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07003060 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0);
Gleb Natapov14168782013-01-21 15:36:49 +02003061
3062 /* depends on vcpu->arch.cr0 to be set to a new value */
3063 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064}
3065
Sean Christopherson0047fca2020-05-01 21:32:33 -07003066static int vmx_get_tdp_level(struct kvm_vcpu *vcpu)
3067{
Sean Christopherson0047fca2020-05-01 21:32:33 -07003068 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
3069 return 5;
3070 return 4;
3071}
3072
Yu Zhang855feb62017-08-24 20:27:55 +08003073static int get_ept_level(struct kvm_vcpu *vcpu)
3074{
Sean Christopherson148d735e2020-02-07 09:37:41 -08003075 if (is_guest_mode(vcpu) && nested_cpu_has_ept(get_vmcs12(vcpu)))
Sean Christophersonac69dfa2020-03-02 18:02:37 -08003076 return vmx_eptp_page_walk_level(nested_ept_get_eptp(vcpu));
Sean Christopherson0047fca2020-05-01 21:32:33 -07003077
3078 return vmx_get_tdp_level(vcpu);
Yu Zhang855feb62017-08-24 20:27:55 +08003079}
3080
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08003081u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08003082{
Yu Zhang855feb62017-08-24 20:27:55 +08003083 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08003084
Yu Zhang855feb62017-08-24 20:27:55 +08003085 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08003086
Peter Feiner995f00a2017-06-30 17:26:32 -07003087 if (enable_ept_ad_bits &&
3088 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02003089 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003090 eptp |= (root_hpa & PAGE_MASK);
3091
3092 return eptp;
3093}
3094
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003095void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096{
Tianyu Lan877ad952018-07-19 08:40:23 +00003097 struct kvm *kvm = vcpu->kvm;
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003098 bool update_guest_cr3 = true;
Sheng Yang14394422008-04-28 12:24:45 +08003099 unsigned long guest_cr3;
3100 u64 eptp;
3101
Avi Kivity089d0342009-03-23 18:26:32 +02003102 if (enable_ept) {
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003103 eptp = construct_eptp(vcpu, pgd);
Sheng Yang14394422008-04-28 12:24:45 +08003104 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00003105
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003106 if (kvm_x86_ops.tlb_remote_flush) {
Tianyu Lan877ad952018-07-19 08:40:23 +00003107 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3108 to_vmx(vcpu)->ept_pointer = eptp;
3109 to_kvm_vmx(kvm)->ept_pointers_match
3110 = EPT_POINTERS_CHECK;
3111 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3112 }
3113
Paolo Bonzinidf7e0682020-05-20 08:37:37 -04003114 if (!enable_unrestricted_guest && !is_paging(vcpu))
Tianyu Lan877ad952018-07-19 08:40:23 +00003115 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Sean Christophersonb17b7432019-09-27 14:45:17 -07003116 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3117 guest_cr3 = vcpu->arch.cr3;
3118 else /* vmcs01.GUEST_CR3 is already up-to-date. */
3119 update_guest_cr3 = false;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003120 ept_load_pdptrs(vcpu);
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003121 } else {
3122 guest_cr3 = pgd;
Sheng Yang14394422008-04-28 12:24:45 +08003123 }
3124
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003125 if (update_guest_cr3)
3126 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127}
3128
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003129int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003131 struct vcpu_vmx *vmx = to_vmx(vcpu);
Ben Serebrin085e68e2015-04-16 11:58:05 -07003132 /*
3133 * Pass through host's Machine Check Enable value to hw_cr4, which
3134 * is in force while we are in guest mode. Do not let guests control
3135 * this bit, even if host CR4.MCE == 0.
3136 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003137 unsigned long hw_cr4;
3138
3139 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
3140 if (enable_unrestricted_guest)
3141 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003142 else if (vmx->rmode.vm86_active)
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003143 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3144 else
3145 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003146
Sean Christopherson64f7a112018-04-30 10:01:06 -07003147 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3148 if (cr4 & X86_CR4_UMIP) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003149 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07003150 hw_cr4 &= ~X86_CR4_UMIP;
3151 } else if (!is_guest_mode(vcpu) ||
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003152 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3153 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3154 }
Sean Christopherson64f7a112018-04-30 10:01:06 -07003155 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02003156
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003157 if (cr4 & X86_CR4_VMXE) {
3158 /*
3159 * To use VMXON (and later other VMX instructions), a guest
3160 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3161 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003162 * is here. We operate under the default treatment of SMM,
3163 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003164 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003165 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003166 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003167 }
David Matlack38991522016-11-29 18:14:08 -08003168
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003169 if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003170 return 1;
3171
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003172 vcpu->arch.cr4 = cr4;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07003173 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4);
Sheng Yang14394422008-04-28 12:24:45 +08003174
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003175 if (!enable_unrestricted_guest) {
3176 if (enable_ept) {
3177 if (!is_paging(vcpu)) {
3178 hw_cr4 &= ~X86_CR4_PAE;
3179 hw_cr4 |= X86_CR4_PSE;
3180 } else if (!(cr4 & X86_CR4_PAE)) {
3181 hw_cr4 &= ~X86_CR4_PAE;
3182 }
3183 }
3184
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003185 /*
Huaitong Handdba2622016-03-22 16:51:15 +08003186 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3187 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3188 * to be manually disabled when guest switches to non-paging
3189 * mode.
3190 *
3191 * If !enable_unrestricted_guest, the CPU is always running
3192 * with CR0.PG=1 and CR4 needs to be modified.
3193 * If enable_unrestricted_guest, the CPU automatically
3194 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003195 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003196 if (!is_paging(vcpu))
3197 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3198 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003199
Sheng Yang14394422008-04-28 12:24:45 +08003200 vmcs_writel(CR4_READ_SHADOW, cr4);
3201 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003202 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203}
3204
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003205void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206{
Avi Kivitya9179492011-01-03 14:28:52 +02003207 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208 u32 ar;
3209
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003210 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003211 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003212 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003213 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003214 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003215 var->base = vmx_read_guest_seg_base(vmx, seg);
3216 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3217 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003218 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003219 var->base = vmx_read_guest_seg_base(vmx, seg);
3220 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3221 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3222 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003223 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 var->type = ar & 15;
3225 var->s = (ar >> 4) & 1;
3226 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003227 /*
3228 * Some userspaces do not preserve unusable property. Since usable
3229 * segment has to be present according to VMX spec we can use present
3230 * property to amend userspace bug by making unusable segment always
3231 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3232 * segment as unusable.
3233 */
3234 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 var->avl = (ar >> 12) & 1;
3236 var->l = (ar >> 13) & 1;
3237 var->db = (ar >> 14) & 1;
3238 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239}
3240
Avi Kivitya9179492011-01-03 14:28:52 +02003241static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3242{
Avi Kivitya9179492011-01-03 14:28:52 +02003243 struct kvm_segment s;
3244
3245 if (to_vmx(vcpu)->rmode.vm86_active) {
3246 vmx_get_segment(vcpu, &s, seg);
3247 return s.base;
3248 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003249 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003250}
3251
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003252int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003253{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003254 struct vcpu_vmx *vmx = to_vmx(vcpu);
3255
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003256 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003257 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003258 else {
3259 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003260 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003261 }
Avi Kivity69c73022011-03-07 15:26:44 +02003262}
3263
Avi Kivity653e3102007-05-07 10:55:37 +03003264static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266 u32 ar;
3267
Avi Kivityf0495f92012-06-07 17:06:10 +03003268 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 ar = 1 << 16;
3270 else {
3271 ar = var->type & 15;
3272 ar |= (var->s & 1) << 4;
3273 ar |= (var->dpl & 3) << 5;
3274 ar |= (var->present & 1) << 7;
3275 ar |= (var->avl & 1) << 12;
3276 ar |= (var->l & 1) << 13;
3277 ar |= (var->db & 1) << 14;
3278 ar |= (var->g & 1) << 15;
3279 }
Avi Kivity653e3102007-05-07 10:55:37 +03003280
3281 return ar;
3282}
3283
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003284void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03003285{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003286 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003287 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003288
Avi Kivity2fb92db2011-04-27 19:42:18 +03003289 vmx_segment_cache_clear(vmx);
3290
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003291 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3292 vmx->rmode.segs[seg] = *var;
3293 if (seg == VCPU_SREG_TR)
3294 vmcs_write16(sf->selector, var->selector);
3295 else if (var->s)
3296 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003297 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003298 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003299
Avi Kivity653e3102007-05-07 10:55:37 +03003300 vmcs_writel(sf->base, var->base);
3301 vmcs_write32(sf->limit, var->limit);
3302 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003303
3304 /*
3305 * Fix the "Accessed" bit in AR field of segment registers for older
3306 * qemu binaries.
3307 * IA32 arch specifies that at the time of processor reset the
3308 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003309 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003310 * state vmexit when "unrestricted guest" mode is turned on.
3311 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3312 * tree. Newer qemu binaries with that qemu fix would not need this
3313 * kvm hack.
3314 */
3315 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003316 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003317
Gleb Natapovf924d662012-12-12 19:10:55 +02003318 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003319
3320out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003321 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003322}
3323
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3325{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003326 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003327
3328 *db = (ar >> 14) & 1;
3329 *l = (ar >> 13) & 1;
3330}
3331
Gleb Natapov89a27f42010-02-16 10:51:48 +02003332static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003333{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003334 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3335 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003336}
3337
Gleb Natapov89a27f42010-02-16 10:51:48 +02003338static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003340 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3341 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342}
3343
Gleb Natapov89a27f42010-02-16 10:51:48 +02003344static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003346 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3347 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348}
3349
Gleb Natapov89a27f42010-02-16 10:51:48 +02003350static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003351{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003352 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3353 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354}
3355
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003356static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3357{
3358 struct kvm_segment var;
3359 u32 ar;
3360
3361 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003362 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003363 if (seg == VCPU_SREG_CS)
3364 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003365 ar = vmx_segment_access_rights(&var);
3366
3367 if (var.base != (var.selector << 4))
3368 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003369 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003370 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003371 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003372 return false;
3373
3374 return true;
3375}
3376
3377static bool code_segment_valid(struct kvm_vcpu *vcpu)
3378{
3379 struct kvm_segment cs;
3380 unsigned int cs_rpl;
3381
3382 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003383 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003384
Avi Kivity1872a3f2009-01-04 23:26:52 +02003385 if (cs.unusable)
3386 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003387 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003388 return false;
3389 if (!cs.s)
3390 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003391 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003392 if (cs.dpl > cs_rpl)
3393 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003394 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003395 if (cs.dpl != cs_rpl)
3396 return false;
3397 }
3398 if (!cs.present)
3399 return false;
3400
3401 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3402 return true;
3403}
3404
3405static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3406{
3407 struct kvm_segment ss;
3408 unsigned int ss_rpl;
3409
3410 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003411 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003412
Avi Kivity1872a3f2009-01-04 23:26:52 +02003413 if (ss.unusable)
3414 return true;
3415 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003416 return false;
3417 if (!ss.s)
3418 return false;
3419 if (ss.dpl != ss_rpl) /* DPL != RPL */
3420 return false;
3421 if (!ss.present)
3422 return false;
3423
3424 return true;
3425}
3426
3427static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3428{
3429 struct kvm_segment var;
3430 unsigned int rpl;
3431
3432 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003433 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003434
Avi Kivity1872a3f2009-01-04 23:26:52 +02003435 if (var.unusable)
3436 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003437 if (!var.s)
3438 return false;
3439 if (!var.present)
3440 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003441 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003442 if (var.dpl < rpl) /* DPL < RPL */
3443 return false;
3444 }
3445
3446 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3447 * rights flags
3448 */
3449 return true;
3450}
3451
3452static bool tr_valid(struct kvm_vcpu *vcpu)
3453{
3454 struct kvm_segment tr;
3455
3456 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3457
Avi Kivity1872a3f2009-01-04 23:26:52 +02003458 if (tr.unusable)
3459 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003460 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003461 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003462 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003463 return false;
3464 if (!tr.present)
3465 return false;
3466
3467 return true;
3468}
3469
3470static bool ldtr_valid(struct kvm_vcpu *vcpu)
3471{
3472 struct kvm_segment ldtr;
3473
3474 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3475
Avi Kivity1872a3f2009-01-04 23:26:52 +02003476 if (ldtr.unusable)
3477 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003478 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003479 return false;
3480 if (ldtr.type != 2)
3481 return false;
3482 if (!ldtr.present)
3483 return false;
3484
3485 return true;
3486}
3487
3488static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3489{
3490 struct kvm_segment cs, ss;
3491
3492 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3493 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3494
Nadav Amitb32a9912015-03-29 16:33:04 +03003495 return ((cs.selector & SEGMENT_RPL_MASK) ==
3496 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003497}
3498
3499/*
3500 * Check if guest state is valid. Returns true if valid, false if
3501 * not.
3502 * We assume that registers are always usable
3503 */
3504static bool guest_state_valid(struct kvm_vcpu *vcpu)
3505{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003506 if (enable_unrestricted_guest)
3507 return true;
3508
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003509 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003510 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003511 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3512 return false;
3513 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3514 return false;
3515 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3516 return false;
3517 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3518 return false;
3519 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3520 return false;
3521 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3522 return false;
3523 } else {
3524 /* protected mode guest state checks */
3525 if (!cs_ss_rpl_check(vcpu))
3526 return false;
3527 if (!code_segment_valid(vcpu))
3528 return false;
3529 if (!stack_segment_valid(vcpu))
3530 return false;
3531 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3532 return false;
3533 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3534 return false;
3535 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3536 return false;
3537 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3538 return false;
3539 if (!tr_valid(vcpu))
3540 return false;
3541 if (!ldtr_valid(vcpu))
3542 return false;
3543 }
3544 /* TODO:
3545 * - Add checks on RIP
3546 * - Add checks on RFLAGS
3547 */
3548
3549 return true;
3550}
3551
Mike Dayd77c26f2007-10-08 09:02:08 -04003552static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003554 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003555 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003556 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003558 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003559 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003560 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3561 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003562 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003563 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003564 r = kvm_write_guest_page(kvm, fn++, &data,
3565 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003566 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003567 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003568 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3569 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003570 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003571 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3572 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003573 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003574 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003575 r = kvm_write_guest_page(kvm, fn, &data,
3576 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3577 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003578out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003579 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003580 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003581}
3582
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003583static int init_rmode_identity_map(struct kvm *kvm)
3584{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003585 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Peter Xu2a5755b2020-01-09 09:57:14 -05003586 int i, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08003587 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003588 u32 tmp;
3589
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003590 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003591 mutex_lock(&kvm->slots_lock);
3592
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003593 if (likely(kvm_vmx->ept_identity_pagetable_done))
Peter Xu2a5755b2020-01-09 09:57:14 -05003594 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003595
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003596 if (!kvm_vmx->ept_identity_map_addr)
3597 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
3598 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003599
David Hildenbrandd8a6e362017-08-24 20:51:34 +02003600 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003601 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08003602 if (r < 0)
Peter Xu2a5755b2020-01-09 09:57:14 -05003603 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003604
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003605 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3606 if (r < 0)
3607 goto out;
3608 /* Set up identity-mapping pagetable for EPT in real mode */
3609 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3610 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3611 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3612 r = kvm_write_guest_page(kvm, identity_map_pfn,
3613 &tmp, i * sizeof(tmp), sizeof(tmp));
3614 if (r < 0)
3615 goto out;
3616 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003617 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003618
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003619out:
Tang Chena255d472014-09-16 18:41:58 +08003620 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003621 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003622}
3623
Avi Kivity6aa8b732006-12-10 02:21:36 -08003624static void seg_setup(int seg)
3625{
Mathias Krause772e0312012-08-30 01:30:19 +02003626 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003627 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628
3629 vmcs_write16(sf->selector, 0);
3630 vmcs_writel(sf->base, 0);
3631 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003632 ar = 0x93;
3633 if (seg == VCPU_SREG_CS)
3634 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003635
3636 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003637}
3638
Sheng Yangf78e0e22007-10-29 09:40:42 +08003639static int alloc_apic_access_page(struct kvm *kvm)
3640{
Xiao Guangrong44841412012-09-07 14:14:20 +08003641 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003642 int r = 0;
3643
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003644 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003645 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003646 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02003647 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3648 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003649 if (r)
3650 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003651
Tang Chen73a6d942014-09-11 13:38:00 +08003652 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003653 if (is_error_page(page)) {
3654 r = -EFAULT;
3655 goto out;
3656 }
3657
Tang Chenc24ae0d2014-09-24 15:57:58 +08003658 /*
3659 * Do not pin the page in memory, so that memory hot-unplug
3660 * is able to migrate it.
3661 */
3662 put_page(page);
3663 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003664out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003665 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003666 return r;
3667}
3668
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003669int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003670{
3671 int vpid;
3672
Avi Kivity919818a2009-03-23 18:01:29 +02003673 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003674 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003675 spin_lock(&vmx_vpid_lock);
3676 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003677 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003678 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003679 else
3680 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003681 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003682 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003683}
3684
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003685void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003686{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003687 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003688 return;
3689 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003690 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003691 spin_unlock(&vmx_vpid_lock);
3692}
3693
Yi Wang1e4329ee2018-11-08 11:22:21 +08003694static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003695 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003696{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003697 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003698
3699 if (!cpu_has_vmx_msr_bitmap())
3700 return;
3701
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003702 if (static_branch_unlikely(&enable_evmcs))
3703 evmcs_touch_msr_bitmap();
3704
Sheng Yang25c5f222008-03-28 13:18:56 +08003705 /*
3706 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3707 * have the write-low and read-high bitmap offsets the wrong way round.
3708 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3709 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003710 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003711 if (type & MSR_TYPE_R)
3712 /* read-low */
3713 __clear_bit(msr, msr_bitmap + 0x000 / f);
3714
3715 if (type & MSR_TYPE_W)
3716 /* write-low */
3717 __clear_bit(msr, msr_bitmap + 0x800 / f);
3718
Sheng Yang25c5f222008-03-28 13:18:56 +08003719 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3720 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003721 if (type & MSR_TYPE_R)
3722 /* read-high */
3723 __clear_bit(msr, msr_bitmap + 0x400 / f);
3724
3725 if (type & MSR_TYPE_W)
3726 /* write-high */
3727 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3728
3729 }
3730}
3731
Yi Wang1e4329ee2018-11-08 11:22:21 +08003732static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003733 u32 msr, int type)
3734{
3735 int f = sizeof(unsigned long);
3736
3737 if (!cpu_has_vmx_msr_bitmap())
3738 return;
3739
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003740 if (static_branch_unlikely(&enable_evmcs))
3741 evmcs_touch_msr_bitmap();
3742
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003743 /*
3744 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3745 * have the write-low and read-high bitmap offsets the wrong way round.
3746 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3747 */
3748 if (msr <= 0x1fff) {
3749 if (type & MSR_TYPE_R)
3750 /* read-low */
3751 __set_bit(msr, msr_bitmap + 0x000 / f);
3752
3753 if (type & MSR_TYPE_W)
3754 /* write-low */
3755 __set_bit(msr, msr_bitmap + 0x800 / f);
3756
3757 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3758 msr &= 0x1fff;
3759 if (type & MSR_TYPE_R)
3760 /* read-high */
3761 __set_bit(msr, msr_bitmap + 0x400 / f);
3762
3763 if (type & MSR_TYPE_W)
3764 /* write-high */
3765 __set_bit(msr, msr_bitmap + 0xc00 / f);
3766
3767 }
3768}
3769
Yi Wang1e4329ee2018-11-08 11:22:21 +08003770static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003771 u32 msr, int type, bool value)
3772{
3773 if (value)
3774 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
3775 else
3776 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
3777}
3778
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003779static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003780{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003781 u8 mode = 0;
3782
3783 if (cpu_has_secondary_exec_ctrls() &&
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003784 (secondary_exec_controls_get(to_vmx(vcpu)) &
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003785 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3786 mode |= MSR_BITMAP_MODE_X2APIC;
3787 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3788 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3789 }
3790
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003791 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003792}
3793
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003794static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
3795 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003796{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003797 int msr;
3798
3799 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3800 unsigned word = msr / BITS_PER_LONG;
3801 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
3802 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003803 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003804
3805 if (mode & MSR_BITMAP_MODE_X2APIC) {
3806 /*
3807 * TPR reads and writes can be virtualized even if virtual interrupt
3808 * delivery is not in use.
3809 */
3810 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
3811 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3812 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
3813 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3814 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
3815 }
3816 }
3817}
3818
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003819void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003820{
3821 struct vcpu_vmx *vmx = to_vmx(vcpu);
3822 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3823 u8 mode = vmx_msr_bitmap_mode(vcpu);
3824 u8 changed = mode ^ vmx->msr_bitmap_mode;
3825
3826 if (!changed)
3827 return;
3828
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003829 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
3830 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
3831
3832 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003833}
3834
Chao Pengb08c2892018-10-24 16:05:15 +08003835void pt_update_intercept_for_msr(struct vcpu_vmx *vmx)
3836{
3837 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3838 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
3839 u32 i;
3840
3841 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_STATUS,
3842 MSR_TYPE_RW, flag);
3843 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_BASE,
3844 MSR_TYPE_RW, flag);
3845 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_MASK,
3846 MSR_TYPE_RW, flag);
3847 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_CR3_MATCH,
3848 MSR_TYPE_RW, flag);
3849 for (i = 0; i < vmx->pt_desc.addr_range; i++) {
3850 vmx_set_intercept_for_msr(msr_bitmap,
3851 MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
3852 vmx_set_intercept_for_msr(msr_bitmap,
3853 MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
3854 }
3855}
3856
Liran Alone6c67d82018-09-04 10:56:52 +03003857static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3858{
3859 struct vcpu_vmx *vmx = to_vmx(vcpu);
3860 void *vapic_page;
3861 u32 vppr;
3862 int rvi;
3863
3864 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3865 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003866 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
Liran Alone6c67d82018-09-04 10:56:52 +03003867 return false;
3868
Paolo Bonzini7e712682018-10-03 13:44:26 +02003869 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003870
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003871 vapic_page = vmx->nested.virtual_apic_map.hva;
Liran Alone6c67d82018-09-04 10:56:52 +03003872 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
Liran Alone6c67d82018-09-04 10:56:52 +03003873
3874 return ((rvi & 0xf0) > (vppr & 0xf0));
3875}
3876
Wincy Van06a55242017-04-28 13:13:59 +08003877static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3878 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003879{
3880#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003881 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3882
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003883 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003884 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003885 * The vector of interrupt to be delivered to vcpu had
3886 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003887 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003888 * Following cases will be reached in this block, and
3889 * we always send a notification event in all cases as
3890 * explained below.
3891 *
3892 * Case 1: vcpu keeps in non-root mode. Sending a
3893 * notification event posts the interrupt to vcpu.
3894 *
3895 * Case 2: vcpu exits to root mode and is still
3896 * runnable. PIR will be synced to vIRR before the
3897 * next vcpu entry. Sending a notification event in
3898 * this case has no effect, as vcpu is not in root
3899 * mode.
3900 *
3901 * Case 3: vcpu exits to root mode and is blocked.
3902 * vcpu_block() has already synced PIR to vIRR and
3903 * never blocks vcpu if vIRR is not cleared. Therefore,
3904 * a blocked vcpu here does not wait for any requested
3905 * interrupts in PIR, and sending a notification event
3906 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003907 */
Feng Wu28b835d2015-09-18 22:29:54 +08003908
Wincy Van06a55242017-04-28 13:13:59 +08003909 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003910 return true;
3911 }
3912#endif
3913 return false;
3914}
3915
Wincy Van705699a2015-02-03 23:58:17 +08003916static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3917 int vector)
3918{
3919 struct vcpu_vmx *vmx = to_vmx(vcpu);
3920
3921 if (is_guest_mode(vcpu) &&
3922 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003923 /*
3924 * If a posted intr is not recognized by hardware,
3925 * we will accomplish it in the next vmentry.
3926 */
3927 vmx->nested.pi_pending = true;
3928 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02003929 /* the PIR and ON have been set by L1. */
3930 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
3931 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08003932 return 0;
3933 }
3934 return -1;
3935}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936/*
Yang Zhanga20ed542013-04-11 19:25:15 +08003937 * Send interrupt to vcpu via posted interrupt way.
3938 * 1. If target vcpu is running(non-root mode), send posted interrupt
3939 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3940 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3941 * interrupt from PIR in next vmentry.
3942 */
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003943static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
Yang Zhanga20ed542013-04-11 19:25:15 +08003944{
3945 struct vcpu_vmx *vmx = to_vmx(vcpu);
3946 int r;
3947
Wincy Van705699a2015-02-03 23:58:17 +08003948 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
3949 if (!r)
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003950 return 0;
3951
3952 if (!vcpu->arch.apicv_active)
3953 return -1;
Wincy Van705699a2015-02-03 23:58:17 +08003954
Yang Zhanga20ed542013-04-11 19:25:15 +08003955 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003956 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003957
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003958 /* If a previous notification has sent the IPI, nothing to do. */
3959 if (pi_test_and_set_on(&vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003960 return 0;
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003961
Wanpeng Li379a3c82020-04-28 14:23:27 +08003962 if (vcpu != kvm_get_running_vcpu() &&
3963 !kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08003964 kvm_vcpu_kick(vcpu);
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003965
3966 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003967}
3968
Avi Kivity6aa8b732006-12-10 02:21:36 -08003969/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003970 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3971 * will not change in the lifetime of the guest.
3972 * Note that host-state that does change is set elsewhere. E.g., host-state
3973 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3974 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003975void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003976{
3977 u32 low32, high32;
3978 unsigned long tmpl;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003979 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003980
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07003981 cr0 = read_cr0();
3982 WARN_ON(cr0 & X86_CR0_TS);
3983 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003984
3985 /*
3986 * Save the most likely value for this task's CR3 in the VMCS.
3987 * We can't use __get_current_cr3_fast() because we're not atomic.
3988 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07003989 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003990 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003991 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003992
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003993 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003994 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003995 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003996 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003997
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003998 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003999#ifdef CONFIG_X86_64
4000 /*
4001 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07004002 * vmx_prepare_switch_to_host(), in case userspace uses
4003 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03004004 */
4005 vmcs_write16(HOST_DS_SELECTOR, 0);
4006 vmcs_write16(HOST_ES_SELECTOR, 0);
4007#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004008 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4009 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004010#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004011 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4012 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4013
Sean Christopherson23420802019-04-19 22:50:57 -07004014 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004015
Sean Christopherson453eafb2018-12-20 12:25:17 -08004016 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004017
4018 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4019 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4020 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4021 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4022
4023 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4024 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4025 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4026 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004027
Sean Christophersonc73da3f2018-12-03 13:53:00 -08004028 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004029 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004030}
4031
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004032void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004033{
Sean Christophersonfa71e952020-07-02 21:04:22 -07004034 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS;
4035 if (!enable_ept)
4036 vmx->vcpu.arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004037 if (is_guest_mode(&vmx->vcpu))
4038 vmx->vcpu.arch.cr4_guest_owned_bits &=
4039 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004040 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4041}
4042
Sean Christophersonc075c3e2019-05-07 12:17:53 -07004043u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
Yang Zhang01e439b2013-04-11 19:25:12 +08004044{
4045 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4046
Andrey Smetanind62caab2015-11-10 15:36:33 +03004047 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004048 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004049
4050 if (!enable_vnmi)
4051 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4052
Sean Christopherson804939e2019-05-07 12:18:05 -07004053 if (!enable_preemption_timer)
4054 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4055
Yang Zhang01e439b2013-04-11 19:25:12 +08004056 return pin_based_exec_ctrl;
4057}
4058
Andrey Smetanind62caab2015-11-10 15:36:33 +03004059static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4060{
4061 struct vcpu_vmx *vmx = to_vmx(vcpu);
4062
Sean Christophersonc5f2c762019-05-07 12:17:55 -07004063 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004064 if (cpu_has_secondary_exec_ctrls()) {
4065 if (kvm_vcpu_apicv_active(vcpu))
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004066 secondary_exec_controls_setbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004067 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4068 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4069 else
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004070 secondary_exec_controls_clearbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004071 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4072 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4073 }
4074
4075 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004076 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004077}
4078
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004079u32 vmx_exec_control(struct vcpu_vmx *vmx)
4080{
4081 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4082
4083 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4084 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4085
4086 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4087 exec_control &= ~CPU_BASED_TPR_SHADOW;
4088#ifdef CONFIG_X86_64
4089 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4090 CPU_BASED_CR8_LOAD_EXITING;
4091#endif
4092 }
4093 if (!enable_ept)
4094 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4095 CPU_BASED_CR3_LOAD_EXITING |
4096 CPU_BASED_INVLPG_EXITING;
4097 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4098 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4099 CPU_BASED_MONITOR_EXITING);
4100 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4101 exec_control &= ~CPU_BASED_HLT_EXITING;
4102 return exec_control;
4103}
4104
4105
Paolo Bonzini80154d72017-08-24 13:55:35 +02004106static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004107{
Paolo Bonzini80154d72017-08-24 13:55:35 +02004108 struct kvm_vcpu *vcpu = &vmx->vcpu;
4109
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004110 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004111
Sean Christopherson2ef76192020-03-02 15:56:22 -08004112 if (vmx_pt_mode_is_system())
Chao Pengf99e3da2018-10-24 16:05:10 +08004113 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004114 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004115 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4116 if (vmx->vpid == 0)
4117 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4118 if (!enable_ept) {
4119 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4120 enable_unrestricted_guest = 0;
4121 }
4122 if (!enable_unrestricted_guest)
4123 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004124 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004125 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02004126 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004127 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4128 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004129 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004130
4131 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4132 * in vmx_set_cr4. */
4133 exec_control &= ~SECONDARY_EXEC_DESC;
4134
Abel Gordonabc4fc52013-04-18 14:35:25 +03004135 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4136 (handle_vmptrld).
4137 We can NOT enable shadow_vmcs here because we don't have yet
4138 a current VMCS12
4139 */
4140 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004141
4142 if (!enable_pml)
4143 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004144
Paolo Bonzini3db13482017-08-24 14:48:03 +02004145 if (vmx_xsaves_supported()) {
4146 /* Exposing XSAVES only when XSAVE is exposed */
4147 bool xsaves_enabled =
Sean Christopherson96be4e02019-12-10 14:44:15 -08004148 boot_cpu_has(X86_FEATURE_XSAVE) &&
Paolo Bonzini3db13482017-08-24 14:48:03 +02004149 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4150 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4151
Aaron Lewis72041602019-10-21 16:30:20 -07004152 vcpu->arch.xsaves_enabled = xsaves_enabled;
4153
Paolo Bonzini3db13482017-08-24 14:48:03 +02004154 if (!xsaves_enabled)
4155 exec_control &= ~SECONDARY_EXEC_XSAVES;
4156
4157 if (nested) {
4158 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004159 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02004160 SECONDARY_EXEC_XSAVES;
4161 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004162 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02004163 ~SECONDARY_EXEC_XSAVES;
4164 }
4165 }
4166
Sean Christophersona7a200e2020-03-02 15:56:58 -08004167 if (cpu_has_vmx_rdtscp()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004168 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
4169 if (!rdtscp_enabled)
4170 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4171
4172 if (nested) {
4173 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004174 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004175 SECONDARY_EXEC_RDTSCP;
4176 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004177 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004178 ~SECONDARY_EXEC_RDTSCP;
4179 }
4180 }
4181
Sean Christopherson5ffec6f2020-03-02 15:56:34 -08004182 if (cpu_has_vmx_invpcid()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004183 /* Exposing INVPCID only when PCID is exposed */
4184 bool invpcid_enabled =
4185 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
4186 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
4187
4188 if (!invpcid_enabled) {
4189 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
4190 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
4191 }
4192
4193 if (nested) {
4194 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004195 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004196 SECONDARY_EXEC_ENABLE_INVPCID;
4197 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004198 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004199 ~SECONDARY_EXEC_ENABLE_INVPCID;
4200 }
4201 }
4202
Jim Mattson45ec3682017-08-23 16:32:04 -07004203 if (vmx_rdrand_supported()) {
4204 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
4205 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02004206 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004207
4208 if (nested) {
4209 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004210 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004211 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004212 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004213 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004214 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004215 }
4216 }
4217
Jim Mattson75f4fc82017-08-23 16:32:03 -07004218 if (vmx_rdseed_supported()) {
4219 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
4220 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02004221 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004222
4223 if (nested) {
4224 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004225 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004226 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004227 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004228 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004229 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004230 }
4231 }
4232
Tao Xue69e72fa2019-07-16 14:55:49 +08004233 if (vmx_waitpkg_supported()) {
4234 bool waitpkg_enabled =
4235 guest_cpuid_has(vcpu, X86_FEATURE_WAITPKG);
4236
4237 if (!waitpkg_enabled)
4238 exec_control &= ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4239
4240 if (nested) {
4241 if (waitpkg_enabled)
4242 vmx->nested.msrs.secondary_ctls_high |=
4243 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4244 else
4245 vmx->nested.msrs.secondary_ctls_high &=
4246 ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4247 }
4248 }
4249
Paolo Bonzini80154d72017-08-24 13:55:35 +02004250 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004251}
4252
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004253static void ept_set_mmio_spte_mask(void)
4254{
4255 /*
4256 * EPT Misconfigurations can be generated if the value of bits 2:0
4257 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004258 */
Paolo Bonzinie7581ca2020-05-19 05:04:49 -04004259 kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE, 0);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004260}
4261
Wanpeng Lif53cd632014-12-02 19:14:58 +08004262#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263
Sean Christopherson944c3462018-12-03 13:53:09 -08004264/*
Xiaoyao Li1b842922019-10-20 17:11:01 +08004265 * Noting that the initialization of Guest-state Area of VMCS is in
4266 * vmx_vcpu_reset().
Sean Christopherson944c3462018-12-03 13:53:09 -08004267 */
Xiaoyao Li1b842922019-10-20 17:11:01 +08004268static void init_vmcs(struct vcpu_vmx *vmx)
Sean Christopherson944c3462018-12-03 13:53:09 -08004269{
Sean Christopherson944c3462018-12-03 13:53:09 -08004270 if (nested)
Xiaoyao Li1b842922019-10-20 17:11:01 +08004271 nested_vmx_set_vmcs_shadowing_bitmap();
Sean Christopherson944c3462018-12-03 13:53:09 -08004272
Sheng Yang25c5f222008-03-28 13:18:56 +08004273 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004274 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004275
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4277
Avi Kivity6aa8b732006-12-10 02:21:36 -08004278 /* Control */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004279 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004280
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004281 exec_controls_set(vmx, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004282
Dan Williamsdfa169b2016-06-02 11:17:24 -07004283 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004284 vmx_compute_secondary_exec_control(vmx);
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004285 secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004286 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004287
Andrey Smetanind62caab2015-11-10 15:36:33 +03004288 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004289 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4290 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4291 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4292 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4293
4294 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004295
Li RongQing0bcf2612015-12-03 13:29:34 +08004296 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004297 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004298 }
4299
Wanpeng Lib31c1142018-03-12 04:53:04 -07004300 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004301 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004302 vmx->ple_window = ple_window;
4303 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004304 }
4305
Xiao Guangrongc3707952011-07-12 03:28:04 +08004306 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4307 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004308 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4309
Avi Kivity9581d442010-10-19 16:46:55 +02004310 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4311 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004312 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4314 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004315
Bandan Das2a499e42017-08-03 15:54:41 -04004316 if (cpu_has_vmx_vmfunc())
4317 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4318
Eddie Dong2cc51562007-05-21 07:28:09 +03004319 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4320 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004321 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004322 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004323 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324
Radim Krčmář74545702015-04-27 15:11:25 +02004325 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4326 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004327
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004328 vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329
4330 /* 22.2.1, 20.8.1 */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004331 vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004332
Sean Christophersonfa71e952020-07-02 21:04:22 -07004333 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4334 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004335
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004336 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004337
Xiaoyao Li35fbe0d2019-10-20 17:10:58 +08004338 if (vmx->vpid != 0)
4339 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4340
Wanpeng Lif53cd632014-12-02 19:14:58 +08004341 if (vmx_xsaves_supported())
4342 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4343
Peter Feiner4e595162016-07-07 14:49:58 -07004344 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004345 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4346 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4347 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004348
4349 if (cpu_has_vmx_encls_vmexit())
4350 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Chao Peng2ef444f2018-10-24 16:05:12 +08004351
Sean Christopherson2ef76192020-03-02 15:56:22 -08004352 if (vmx_pt_mode_is_host_guest()) {
Chao Peng2ef444f2018-10-24 16:05:12 +08004353 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
4354 /* Bit[6~0] are forced to 1, writes are ignored. */
4355 vmx->pt_desc.guest.output_mask = 0x7F;
4356 vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
4357 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004358}
4359
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004360static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004361{
4362 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004363 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004364 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004365
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004366 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004367 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004368
Tao Xu6e3ba4a2019-07-16 14:55:50 +08004369 vmx->msr_ia32_umwait_control = 0;
4370
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004371 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Wanpeng Li95c06542019-09-05 14:26:28 +08004372 vmx->hv_deadline_tsc = -1;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004373 kvm_set_cr8(vcpu, 0);
4374
4375 if (!init_event) {
4376 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4377 MSR_IA32_APICBASE_ENABLE;
4378 if (kvm_vcpu_is_reset_bsp(vcpu))
4379 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4380 apic_base_msr.host_initiated = true;
4381 kvm_set_apic_base(vcpu, &apic_base_msr);
4382 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004383
Avi Kivity2fb92db2011-04-27 19:42:18 +03004384 vmx_segment_cache_clear(vmx);
4385
Avi Kivity5706be02008-08-20 15:07:31 +03004386 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004387 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004388 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004389
4390 seg_setup(VCPU_SREG_DS);
4391 seg_setup(VCPU_SREG_ES);
4392 seg_setup(VCPU_SREG_FS);
4393 seg_setup(VCPU_SREG_GS);
4394 seg_setup(VCPU_SREG_SS);
4395
4396 vmcs_write16(GUEST_TR_SELECTOR, 0);
4397 vmcs_writel(GUEST_TR_BASE, 0);
4398 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4399 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4400
4401 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4402 vmcs_writel(GUEST_LDTR_BASE, 0);
4403 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4404 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4405
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004406 if (!init_event) {
4407 vmcs_write32(GUEST_SYSENTER_CS, 0);
4408 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4409 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4410 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4411 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004412
Wanpeng Lic37c2872017-11-20 14:52:21 -08004413 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004414 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004415
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004416 vmcs_writel(GUEST_GDTR_BASE, 0);
4417 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4418
4419 vmcs_writel(GUEST_IDTR_BASE, 0);
4420 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4421
Anthony Liguori443381a2010-12-06 10:53:38 -06004422 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004423 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004424 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004425 if (kvm_mpx_supported())
4426 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004427
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004428 setup_msrs(vmx);
4429
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4431
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004432 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004433 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004434 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004435 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004436 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004437 vmcs_write32(TPR_THRESHOLD, 0);
4438 }
4439
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004440 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004442 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004443 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004444 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004445 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004446 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004447
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004448 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004450 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004451 if (init_event)
4452 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004453}
4454
Jan Kiszkac9a79532014-03-07 20:03:15 +01004455static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004456{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08004457 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004458}
4459
Jan Kiszkac9a79532014-03-07 20:03:15 +01004460static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004461{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004462 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004463 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01004464 enable_irq_window(vcpu);
4465 return;
4466 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004467
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08004468 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004469}
4470
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004471static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004472{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004473 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004474 uint32_t intr;
4475 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004476
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004477 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004478
Avi Kivityfa89a812008-09-01 15:57:51 +03004479 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004480 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004481 int inc_eip = 0;
4482 if (vcpu->arch.interrupt.soft)
4483 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004484 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
Eddie Dong85f455f2007-07-06 12:20:49 +03004485 return;
4486 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004487 intr = irq | INTR_INFO_VALID_MASK;
4488 if (vcpu->arch.interrupt.soft) {
4489 intr |= INTR_TYPE_SOFT_INTR;
4490 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4491 vmx->vcpu.arch.event_exit_inst_len);
4492 } else
4493 intr |= INTR_TYPE_EXT_INTR;
4494 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004495
4496 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004497}
4498
Sheng Yangf08864b2008-05-15 18:23:25 +08004499static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4500{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004501 struct vcpu_vmx *vmx = to_vmx(vcpu);
4502
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004503 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004504 /*
4505 * Tracking the NMI-blocked state in software is built upon
4506 * finding the next open IRQ window. This, in turn, depends on
4507 * well-behaving guests: They have to keep IRQs disabled at
4508 * least as long as the NMI handler runs. Otherwise we may
4509 * cause NMI nesting, maybe breaking the guest. But as this is
4510 * highly unlikely, we can live with the residual risk.
4511 */
4512 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
4513 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4514 }
4515
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004516 ++vcpu->stat.nmi_injections;
4517 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004518
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004519 if (vmx->rmode.vm86_active) {
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004520 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004521 return;
4522 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08004523
Sheng Yangf08864b2008-05-15 18:23:25 +08004524 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4525 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004526
4527 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004528}
4529
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004530bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004531{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004532 struct vcpu_vmx *vmx = to_vmx(vcpu);
4533 bool masked;
4534
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004535 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004536 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004537 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004538 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004539 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4540 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4541 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004542}
4543
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004544void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004545{
4546 struct vcpu_vmx *vmx = to_vmx(vcpu);
4547
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004548 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004549 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4550 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4551 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4552 }
4553 } else {
4554 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4555 if (masked)
4556 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4557 GUEST_INTR_STATE_NMI);
4558 else
4559 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4560 GUEST_INTR_STATE_NMI);
4561 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004562}
4563
Sean Christopherson1b660b62020-04-22 19:25:44 -07004564bool vmx_nmi_blocked(struct kvm_vcpu *vcpu)
4565{
4566 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
4567 return false;
4568
4569 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4570 return true;
4571
4572 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4573 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
4574 GUEST_INTR_STATE_NMI));
4575}
4576
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004577static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Jan Kiszka2505dc92013-04-14 12:12:47 +02004578{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004579 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004580 return -EBUSY;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004581
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004582 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
4583 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004584 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004585
Sean Christopherson1b660b62020-04-22 19:25:44 -07004586 return !vmx_nmi_blocked(vcpu);
4587}
Sean Christopherson429ab572020-04-22 19:25:42 -07004588
Sean Christopherson1b660b62020-04-22 19:25:44 -07004589bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
4590{
4591 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Sean Christopherson88c604b2020-04-22 19:25:41 -07004592 return false;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004593
Sean Christopherson7ab0abd2020-04-22 19:25:50 -07004594 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) ||
Sean Christopherson1b660b62020-04-22 19:25:44 -07004595 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4596 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Jan Kiszka2505dc92013-04-14 12:12:47 +02004597}
4598
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004599static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Gleb Natapov78646122009-03-23 12:12:11 +02004600{
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004601 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004602 return -EBUSY;
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004603
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004604 /*
4605 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
4606 * e.g. if the IRQ arrived asynchronously after checking nested events.
4607 */
4608 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004609 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004610
Sean Christopherson1b660b62020-04-22 19:25:44 -07004611 return !vmx_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02004612}
4613
Izik Eiduscbc94022007-10-25 00:29:55 +02004614static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4615{
4616 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004617
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004618 if (enable_unrestricted_guest)
4619 return 0;
4620
Peter Xu6a3c6232020-01-09 09:57:16 -05004621 mutex_lock(&kvm->slots_lock);
4622 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4623 PAGE_SIZE * 3);
4624 mutex_unlock(&kvm->slots_lock);
4625
Izik Eiduscbc94022007-10-25 00:29:55 +02004626 if (ret)
4627 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004628 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004629 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004630}
4631
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004632static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4633{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004634 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004635 return 0;
4636}
4637
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004638static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004639{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004640 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004641 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004642 /*
4643 * Update instruction length as we may reinject the exception
4644 * from user space while in guest debugging mode.
4645 */
4646 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4647 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004648 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004649 return false;
4650 /* fall through */
4651 case DB_VECTOR:
Miaohe Lina8cfbae2020-02-19 10:45:48 +08004652 return !(vcpu->guest_debug &
4653 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP));
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004654 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004655 case OF_VECTOR:
4656 case BR_VECTOR:
4657 case UD_VECTOR:
4658 case DF_VECTOR:
4659 case SS_VECTOR:
4660 case GP_VECTOR:
4661 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004662 return true;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004663 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004664 return false;
4665}
4666
4667static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4668 int vec, u32 err_code)
4669{
4670 /*
4671 * Instruction with address size override prefix opcode 0x67
4672 * Cause the #SS fault with 0 error code in VM86 mode.
4673 */
4674 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004675 if (kvm_emulate_instruction(vcpu, 0)) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004676 if (vcpu->arch.halt_request) {
4677 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004678 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004679 }
4680 return 1;
4681 }
4682 return 0;
4683 }
4684
4685 /*
4686 * Forward all other exceptions that are valid in real mode.
4687 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4688 * the required debugging infrastructure rework.
4689 */
4690 kvm_queue_exception(vcpu, vec);
4691 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004692}
4693
Andi Kleena0861c02009-06-08 17:37:09 +08004694/*
4695 * Trigger machine check on the host. We assume all the MSRs are already set up
4696 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4697 * We pass a fake environment to the machine check handler because we want
4698 * the guest to be always treated like user space, no matter what context
4699 * it used internally.
4700 */
4701static void kvm_machine_check(void)
4702{
Uros Bizjakfb56baa2020-04-14 09:14:14 +02004703#if defined(CONFIG_X86_MCE)
Andi Kleena0861c02009-06-08 17:37:09 +08004704 struct pt_regs regs = {
4705 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4706 .flags = X86_EFLAGS_IF,
4707 };
4708
Thomas Gleixner8cd501c2020-02-25 23:33:23 +01004709 do_machine_check(&regs);
Andi Kleena0861c02009-06-08 17:37:09 +08004710#endif
4711}
4712
Avi Kivity851ba692009-08-24 11:10:17 +03004713static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004714{
Sean Christopherson95b5a482019-04-19 22:50:59 -07004715 /* handled by vmx_vcpu_run() */
Andi Kleena0861c02009-06-08 17:37:09 +08004716 return 1;
4717}
4718
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004719/*
4720 * If the host has split lock detection disabled, then #AC is
4721 * unconditionally injected into the guest, which is the pre split lock
4722 * detection behaviour.
4723 *
4724 * If the host has split lock detection enabled then #AC is
4725 * only injected into the guest when:
4726 * - Guest CPL == 3 (user mode)
4727 * - Guest has #AC detection enabled in CR0
4728 * - Guest EFLAGS has AC bit set
4729 */
4730static inline bool guest_inject_ac(struct kvm_vcpu *vcpu)
4731{
4732 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
4733 return true;
4734
4735 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
4736 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
4737}
4738
Sean Christopherson95b5a482019-04-19 22:50:59 -07004739static int handle_exception_nmi(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740{
Avi Kivity1155f762007-11-22 11:30:47 +02004741 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004742 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004743 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004744 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004745 u32 vect_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746
Avi Kivity1155f762007-11-22 11:30:47 +02004747 vect_info = vmx->idt_vectoring_info;
Sean Christophersonf27ad732020-04-27 10:18:37 -07004748 intr_info = vmx_get_intr_info(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004749
Paolo Bonzini2ea72032019-06-06 14:57:25 +02004750 if (is_machine_check(intr_info) || is_nmi(intr_info))
Sean Christopherson95b5a482019-04-19 22:50:59 -07004751 return 1; /* handled by handle_exception_nmi_irqoff() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004752
Wanpeng Li082d06e2018-04-03 16:28:48 -07004753 if (is_invalid_opcode(intr_info))
4754 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004755
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004757 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004758 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004759
Liran Alon9e869482018-03-12 13:12:51 +02004760 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4761 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christophersona6c6ed12019-08-27 14:40:30 -07004762
4763 /*
4764 * VMware backdoor emulation on #GP interception only handles
4765 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
4766 * error code on #GP.
4767 */
4768 if (error_code) {
4769 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4770 return 1;
4771 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004772 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon9e869482018-03-12 13:12:51 +02004773 }
4774
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004775 /*
4776 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4777 * MMIO, it is better to report an internal error.
4778 * See the comments in vmx_handle_exit.
4779 */
4780 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4781 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4782 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4783 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Jim Mattson1aa561b2020-06-03 16:56:21 -07004784 vcpu->run->internal.ndata = 4;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004785 vcpu->run->internal.data[0] = vect_info;
4786 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004787 vcpu->run->internal.data[2] = error_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07004788 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004789 return 0;
4790 }
4791
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792 if (is_page_fault(intr_info)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07004793 cr2 = vmx_get_exit_qual(vcpu);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07004794 /* EPT won't cause page fault directly */
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02004795 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_flags && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02004796 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797 }
4798
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004799 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004800
4801 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4802 return handle_rmode_exception(vcpu, ex_no, error_code);
4803
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004804 switch (ex_no) {
4805 case DB_VECTOR:
Sean Christopherson5addc232020-04-15 13:34:53 -07004806 dr6 = vmx_get_exit_qual(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004807 if (!(vcpu->guest_debug &
4808 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004809 if (is_icebp(intr_info))
Sean Christopherson1957aa62019-08-27 14:40:39 -07004810 WARN_ON(!skip_emulated_instruction(vcpu));
Huw Daviesfd2a4452014-04-16 10:02:51 +01004811
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004812 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004813 return 1;
4814 }
Peter Xu13196632020-05-05 16:49:58 -04004815 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004816 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4817 /* fall through */
4818 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004819 /*
4820 * Update instruction length as we may reinject #BP from
4821 * user space while in guest debugging mode. Reading it for
4822 * #DB as well causes no harm, it is not used in that case.
4823 */
4824 vmx->vcpu.arch.event_exit_inst_len =
4825 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004826 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004827 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004828 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4829 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004830 break;
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004831 case AC_VECTOR:
4832 if (guest_inject_ac(vcpu)) {
4833 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4834 return 1;
4835 }
4836
4837 /*
4838 * Handle split lock. Depending on detection mode this will
4839 * either warn and disable split lock detection for this
4840 * task or force SIGBUS on it.
4841 */
4842 if (handle_guest_split_lock(kvm_rip_read(vcpu)))
4843 return 1;
4844 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004845 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004846 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4847 kvm_run->ex.exception = ex_no;
4848 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004849 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851 return 0;
4852}
4853
Andrea Arcangelif399e602019-11-04 17:59:58 -05004854static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004855{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004856 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004857 return 1;
4858}
4859
Avi Kivity851ba692009-08-24 11:10:17 +03004860static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004861{
Avi Kivity851ba692009-08-24 11:10:17 +03004862 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004863 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004864 return 0;
4865}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866
Avi Kivity851ba692009-08-24 11:10:17 +03004867static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004868{
He, Qingbfdaab02007-09-12 14:18:28 +08004869 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004870 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004871 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872
Sean Christopherson5addc232020-04-15 13:34:53 -07004873 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02004874 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004875
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004876 ++vcpu->stat.io_exits;
4877
Sean Christopherson432baf62018-03-08 08:57:26 -08004878 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004879 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004880
4881 port = exit_qualification >> 16;
4882 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004883 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004884
Sean Christophersondca7f122018-03-08 08:57:27 -08004885 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004886}
4887
Ingo Molnar102d8322007-02-19 14:37:47 +02004888static void
4889vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4890{
4891 /*
4892 * Patch in the VMCALL instruction:
4893 */
4894 hypercall[0] = 0x0f;
4895 hypercall[1] = 0x01;
4896 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004897}
4898
Guo Chao0fa06072012-06-28 15:16:19 +08004899/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004900static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4901{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004902 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004903 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4904 unsigned long orig_val = val;
4905
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004906 /*
4907 * We get here when L2 changed cr0 in a way that did not change
4908 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004909 * but did change L0 shadowed bits. So we first calculate the
4910 * effective cr0 value that L1 would like to write into the
4911 * hardware. It consists of the L2-owned bits from the new
4912 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004913 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004914 val = (val & ~vmcs12->cr0_guest_host_mask) |
4915 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4916
David Matlack38991522016-11-29 18:14:08 -08004917 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004918 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004919
4920 if (kvm_set_cr0(vcpu, val))
4921 return 1;
4922 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004923 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004924 } else {
4925 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004926 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004927 return 1;
David Matlack38991522016-11-29 18:14:08 -08004928
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004929 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004930 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004931}
4932
4933static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4934{
4935 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004936 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4937 unsigned long orig_val = val;
4938
4939 /* analogously to handle_set_cr0 */
4940 val = (val & ~vmcs12->cr4_guest_host_mask) |
4941 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4942 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004943 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004944 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004945 return 0;
4946 } else
4947 return kvm_set_cr4(vcpu, val);
4948}
4949
Paolo Bonzini0367f202016-07-12 10:44:55 +02004950static int handle_desc(struct kvm_vcpu *vcpu)
4951{
4952 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004953 return kvm_emulate_instruction(vcpu, 0);
Paolo Bonzini0367f202016-07-12 10:44:55 +02004954}
4955
Avi Kivity851ba692009-08-24 11:10:17 +03004956static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004958 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004959 int cr;
4960 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004961 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004962 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963
Sean Christopherson5addc232020-04-15 13:34:53 -07004964 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004965 cr = exit_qualification & 15;
4966 reg = (exit_qualification >> 8) & 15;
4967 switch ((exit_qualification >> 4) & 3) {
4968 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03004969 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004970 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971 switch (cr) {
4972 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004973 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004974 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004976 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03004977 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004978 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004980 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004981 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004982 case 8: {
4983 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03004984 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004985 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004986 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004987 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08004988 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004989 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08004990 return ret;
4991 /*
4992 * TODO: we might be squashing a
4993 * KVM_GUESTDBG_SINGLESTEP-triggered
4994 * KVM_EXIT_DEBUG here.
4995 */
Avi Kivity851ba692009-08-24 11:10:17 +03004996 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004997 return 0;
4998 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004999 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005001 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005002 WARN_ONCE(1, "Guest should always own CR0.TS");
5003 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005004 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005005 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006 case 1: /*mov from cr*/
5007 switch (cr) {
5008 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005009 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02005010 val = kvm_read_cr3(vcpu);
5011 kvm_register_write(vcpu, reg, val);
5012 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005013 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005015 val = kvm_get_cr8(vcpu);
5016 kvm_register_write(vcpu, reg, val);
5017 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005018 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005019 }
5020 break;
5021 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005022 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005023 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005024 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005025
Kyle Huey6affcbe2016-11-29 12:40:40 -08005026 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005027 default:
5028 break;
5029 }
Avi Kivity851ba692009-08-24 11:10:17 +03005030 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005031 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032 (int)(exit_qualification >> 4) & 3, cr);
5033 return 0;
5034}
5035
Avi Kivity851ba692009-08-24 11:10:17 +03005036static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037{
He, Qingbfdaab02007-09-12 14:18:28 +08005038 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005039 int dr, dr7, reg;
5040
Sean Christopherson5addc232020-04-15 13:34:53 -07005041 exit_qualification = vmx_get_exit_qual(vcpu);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005042 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5043
5044 /* First, if DR does not exist, trigger UD */
5045 if (!kvm_require_dr(vcpu, dr))
5046 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005047
Jan Kiszkaf2483412010-01-20 18:20:20 +01005048 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005049 if (!kvm_require_cpl(vcpu, 0))
5050 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005051 dr7 = vmcs_readl(GUEST_DR7);
5052 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005053 /*
5054 * As the vm-exit takes precedence over the debug trap, we
5055 * need to emulate the latter, either for the host or the
5056 * guest debugging itself.
5057 */
5058 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Paolo Bonzini45981de2020-05-06 05:59:39 -04005059 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_RTM | DR6_FIXED_1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005060 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005061 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005062 vcpu->run->debug.arch.exception = DB_VECTOR;
5063 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005064 return 0;
5065 } else {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04005066 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005067 return 1;
5068 }
5069 }
5070
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005071 if (vcpu->guest_debug == 0) {
Sean Christopherson2183f562019-05-07 12:17:56 -07005072 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005073
5074 /*
5075 * No more DR vmexits; force a reload of the debug registers
5076 * and reenter on this instruction. The next vmexit will
5077 * retrieve the full state of the debug registers.
5078 */
5079 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5080 return 1;
5081 }
5082
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005083 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5084 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005085 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005086
5087 if (kvm_get_dr(vcpu, dr, &val))
5088 return 1;
5089 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005090 } else
Nadav Amit57773922014-06-18 17:19:23 +03005091 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005092 return 1;
5093
Kyle Huey6affcbe2016-11-29 12:40:40 -08005094 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095}
5096
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005097static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5098{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005099 get_debugreg(vcpu->arch.db[0], 0);
5100 get_debugreg(vcpu->arch.db[1], 1);
5101 get_debugreg(vcpu->arch.db[2], 2);
5102 get_debugreg(vcpu->arch.db[3], 3);
5103 get_debugreg(vcpu->arch.dr6, 6);
5104 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5105
5106 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Sean Christopherson2183f562019-05-07 12:17:56 -07005107 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005108}
5109
Gleb Natapov020df072010-04-13 10:05:23 +03005110static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5111{
5112 vmcs_writel(GUEST_DR7, val);
5113}
5114
Avi Kivity851ba692009-08-24 11:10:17 +03005115static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005116{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005117 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005118 return 1;
5119}
5120
Avi Kivity851ba692009-08-24 11:10:17 +03005121static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005123 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005124
Avi Kivity3842d132010-07-27 12:30:24 +03005125 kvm_make_request(KVM_REQ_EVENT, vcpu);
5126
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005127 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005128 return 1;
5129}
5130
Avi Kivity851ba692009-08-24 11:10:17 +03005131static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005132{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005133 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005134}
5135
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005136static int handle_invd(struct kvm_vcpu *vcpu)
5137{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005138 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005139}
5140
Avi Kivity851ba692009-08-24 11:10:17 +03005141static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005142{
Sean Christopherson5addc232020-04-15 13:34:53 -07005143 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005144
5145 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005146 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005147}
5148
Avi Kivityfee84b02011-11-10 14:57:25 +02005149static int handle_rdpmc(struct kvm_vcpu *vcpu)
5150{
5151 int err;
5152
5153 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005154 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02005155}
5156
Avi Kivity851ba692009-08-24 11:10:17 +03005157static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005158{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005159 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005160}
5161
Dexuan Cui2acf9232010-06-10 11:27:12 +08005162static int handle_xsetbv(struct kvm_vcpu *vcpu)
5163{
5164 u64 new_bv = kvm_read_edx_eax(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07005165 u32 index = kvm_rcx_read(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005166
5167 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005168 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005169 return 1;
5170}
5171
Avi Kivity851ba692009-08-24 11:10:17 +03005172static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005173{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005174 if (likely(fasteoi)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07005175 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005176 int access_type, offset;
5177
5178 access_type = exit_qualification & APIC_ACCESS_TYPE;
5179 offset = exit_qualification & APIC_ACCESS_OFFSET;
5180 /*
5181 * Sane guest uses MOV to write EOI, with written value
5182 * not cared. So make a short-circuit here by avoiding
5183 * heavy instruction emulation.
5184 */
5185 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5186 (offset == APIC_EOI)) {
5187 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005188 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005189 }
5190 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005191 return kvm_emulate_instruction(vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005192}
5193
Yang Zhangc7c9c562013-01-25 10:18:51 +08005194static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5195{
Sean Christopherson5addc232020-04-15 13:34:53 -07005196 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005197 int vector = exit_qualification & 0xff;
5198
5199 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5200 kvm_apic_set_eoi_accelerated(vcpu, vector);
5201 return 1;
5202}
5203
Yang Zhang83d4c282013-01-25 10:18:49 +08005204static int handle_apic_write(struct kvm_vcpu *vcpu)
5205{
Sean Christopherson5addc232020-04-15 13:34:53 -07005206 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhang83d4c282013-01-25 10:18:49 +08005207 u32 offset = exit_qualification & 0xfff;
5208
5209 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5210 kvm_apic_write_nodecode(vcpu, offset);
5211 return 1;
5212}
5213
Avi Kivity851ba692009-08-24 11:10:17 +03005214static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005215{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005216 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005217 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005218 bool has_error_code = false;
5219 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005220 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005221 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005222
5223 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005224 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005225 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005226
Sean Christopherson5addc232020-04-15 13:34:53 -07005227 exit_qualification = vmx_get_exit_qual(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005228
5229 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005230 if (reason == TASK_SWITCH_GATE && idt_v) {
5231 switch (type) {
5232 case INTR_TYPE_NMI_INTR:
5233 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005234 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005235 break;
5236 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005237 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005238 kvm_clear_interrupt_queue(vcpu);
5239 break;
5240 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005241 if (vmx->idt_vectoring_info &
5242 VECTORING_INFO_DELIVER_CODE_MASK) {
5243 has_error_code = true;
5244 error_code =
5245 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5246 }
5247 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005248 case INTR_TYPE_SOFT_EXCEPTION:
5249 kvm_clear_exception_queue(vcpu);
5250 break;
5251 default:
5252 break;
5253 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005254 }
Izik Eidus37817f22008-03-24 23:14:53 +02005255 tss_selector = exit_qualification;
5256
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005257 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5258 type != INTR_TYPE_EXT_INTR &&
5259 type != INTR_TYPE_NMI_INTR))
Sean Christopherson1957aa62019-08-27 14:40:39 -07005260 WARN_ON(!skip_emulated_instruction(vcpu));
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005261
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005262 /*
5263 * TODO: What about debug traps on tss switch?
5264 * Are we supposed to inject them and update dr6?
5265 */
Sean Christopherson10517782019-08-27 14:40:35 -07005266 return kvm_task_switch(vcpu, tss_selector,
5267 type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005268 reason, has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005269}
5270
Avi Kivity851ba692009-08-24 11:10:17 +03005271static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005272{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005273 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005274 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005275 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005276
Sean Christopherson5addc232020-04-15 13:34:53 -07005277 exit_qualification = vmx_get_exit_qual(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005278
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005279 /*
5280 * EPT violation happened while executing iret from NMI,
5281 * "blocked by NMI" bit has to be set before next VM entry.
5282 * There are errata that may cause this bit to not be set:
5283 * AAK134, BY25.
5284 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005285 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005286 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005287 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005288 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5289
Sheng Yang14394422008-04-28 12:24:45 +08005290 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005291 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005292
Junaid Shahid27959a42016-12-06 16:46:10 -08005293 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005294 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005295 ? PFERR_USER_MASK : 0;
5296 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005297 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005298 ? PFERR_WRITE_MASK : 0;
5299 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005300 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005301 ? PFERR_FETCH_MASK : 0;
5302 /* ept page table entry is present? */
5303 error_code |= (exit_qualification &
5304 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5305 EPT_VIOLATION_EXECUTABLE))
5306 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005307
Paolo Bonzinieebed242016-11-28 14:39:58 +01005308 error_code |= (exit_qualification & 0x100) != 0 ?
5309 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005310
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005311 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005312 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005313}
5314
Avi Kivity851ba692009-08-24 11:10:17 +03005315static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005316{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005317 gpa_t gpa;
5318
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005319 /*
5320 * A nested guest cannot optimize MMIO vmexits, because we have an
5321 * nGPA here instead of the required GPA.
5322 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005323 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005324 if (!is_guest_mode(vcpu) &&
5325 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005326 trace_kvm_fast_mmio(gpa);
Sean Christopherson1957aa62019-08-27 14:40:39 -07005327 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005328 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005329
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005330 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005331}
5332
Avi Kivity851ba692009-08-24 11:10:17 +03005333static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005334{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005335 WARN_ON_ONCE(!enable_vnmi);
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005336 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005337 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005338 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005339
5340 return 1;
5341}
5342
Mohammed Gamal80ced182009-09-01 12:48:18 +02005343static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005344{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005345 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity49e9d552010-09-19 14:34:08 +02005346 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005347 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005348
Sean Christopherson2183f562019-05-07 12:17:56 -07005349 intr_window_requested = exec_controls_get(vmx) &
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005350 CPU_BASED_INTR_WINDOW_EXITING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005351
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005352 while (vmx->emulation_required && count-- != 0) {
Sean Christophersondb438592020-04-22 19:25:48 -07005353 if (intr_window_requested && !vmx_interrupt_blocked(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005354 return handle_interrupt_window(&vmx->vcpu);
5355
Radim Krčmář72875d82017-04-26 22:32:19 +02005356 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005357 return 1;
5358
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005359 if (!kvm_emulate_instruction(vcpu, 0))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005360 return 0;
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005361
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005362 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
Sean Christopherson8fff2712019-08-27 14:40:37 -07005363 vcpu->arch.exception.pending) {
5364 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5365 vcpu->run->internal.suberror =
5366 KVM_INTERNAL_ERROR_EMULATION;
5367 vcpu->run->internal.ndata = 0;
5368 return 0;
5369 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005370
Gleb Natapov8d76c492013-05-08 18:38:44 +03005371 if (vcpu->arch.halt_request) {
5372 vcpu->arch.halt_request = 0;
Sean Christopherson8fff2712019-08-27 14:40:37 -07005373 return kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005374 }
5375
Sean Christopherson8fff2712019-08-27 14:40:37 -07005376 /*
5377 * Note, return 1 and not 0, vcpu_run() is responsible for
5378 * morphing the pending signal into the proper return code.
5379 */
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005380 if (signal_pending(current))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005381 return 1;
5382
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005383 if (need_resched())
5384 schedule();
5385 }
5386
Sean Christopherson8fff2712019-08-27 14:40:37 -07005387 return 1;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005388}
5389
5390static void grow_ple_window(struct kvm_vcpu *vcpu)
5391{
5392 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005393 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005394
Babu Mogerc8e88712018-03-16 16:37:24 -04005395 vmx->ple_window = __grow_ple_window(old, ple_window,
5396 ple_window_grow,
5397 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005398
Peter Xu4f75bcc2019-09-06 10:17:22 +08005399 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005400 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005401 trace_kvm_ple_window_update(vcpu->vcpu_id,
5402 vmx->ple_window, old);
5403 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005404}
5405
5406static void shrink_ple_window(struct kvm_vcpu *vcpu)
5407{
5408 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005409 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005410
Babu Mogerc8e88712018-03-16 16:37:24 -04005411 vmx->ple_window = __shrink_ple_window(old, ple_window,
5412 ple_window_shrink,
5413 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005414
Peter Xu4f75bcc2019-09-06 10:17:22 +08005415 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005416 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005417 trace_kvm_ple_window_update(vcpu->vcpu_id,
5418 vmx->ple_window, old);
5419 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005420}
5421
5422/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08005423 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
5424 */
5425static void wakeup_handler(void)
5426{
5427 struct kvm_vcpu *vcpu;
5428 int cpu = smp_processor_id();
5429
5430 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
5431 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
5432 blocked_vcpu_list) {
5433 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
5434
5435 if (pi_test_on(pi_desc) == 1)
5436 kvm_vcpu_kick(vcpu);
5437 }
5438 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
5439}
5440
Peng Haoe01bca22018-04-07 05:47:32 +08005441static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005442{
5443 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
5444 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
5445 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
5446 0ull, VMX_EPT_EXECUTABLE_MASK,
5447 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005448 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005449
5450 ept_set_mmio_spte_mask();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005451}
5452
Avi Kivity6aa8b732006-12-10 02:21:36 -08005453/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005454 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5455 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5456 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005457static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005458{
Wanpeng Lib31c1142018-03-12 04:53:04 -07005459 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005460 grow_ple_window(vcpu);
5461
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08005462 /*
5463 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
5464 * VM-execution control is ignored if CPL > 0. OTOH, KVM
5465 * never set PAUSE_EXITING and just set PLE if supported,
5466 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
5467 */
5468 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005469 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005470}
5471
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005472static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08005473{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005474 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08005475}
5476
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005477static int handle_mwait(struct kvm_vcpu *vcpu)
5478{
5479 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
5480 return handle_nop(vcpu);
5481}
5482
Jim Mattson45ec3682017-08-23 16:32:04 -07005483static int handle_invalid_op(struct kvm_vcpu *vcpu)
5484{
5485 kvm_queue_exception(vcpu, UD_VECTOR);
5486 return 1;
5487}
5488
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005489static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5490{
5491 return 1;
5492}
5493
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005494static int handle_monitor(struct kvm_vcpu *vcpu)
5495{
5496 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5497 return handle_nop(vcpu);
5498}
5499
Junaid Shahideb4b2482018-06-27 14:59:14 -07005500static int handle_invpcid(struct kvm_vcpu *vcpu)
5501{
5502 u32 vmx_instruction_info;
5503 unsigned long type;
5504 bool pcid_enabled;
5505 gva_t gva;
5506 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005507 unsigned i;
5508 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005509 struct {
5510 u64 pcid;
5511 u64 gla;
5512 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005513 int r;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005514
5515 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5516 kvm_queue_exception(vcpu, UD_VECTOR);
5517 return 1;
5518 }
5519
5520 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5521 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5522
5523 if (type > 3) {
5524 kvm_inject_gp(vcpu, 0);
5525 return 1;
5526 }
5527
5528 /* According to the Intel instruction reference, the memory operand
5529 * is read even if it isn't needed (e.g., for type==all)
5530 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005531 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005532 vmx_instruction_info, false,
5533 sizeof(operand), &gva))
Junaid Shahideb4b2482018-06-27 14:59:14 -07005534 return 1;
5535
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005536 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5537 if (r != X86EMUL_CONTINUE)
5538 return vmx_handle_memory_failure(vcpu, r, &e);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005539
5540 if (operand.pcid >> 12 != 0) {
5541 kvm_inject_gp(vcpu, 0);
5542 return 1;
5543 }
5544
5545 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
5546
5547 switch (type) {
5548 case INVPCID_TYPE_INDIV_ADDR:
5549 if ((!pcid_enabled && (operand.pcid != 0)) ||
5550 is_noncanonical_address(operand.gla, vcpu)) {
5551 kvm_inject_gp(vcpu, 0);
5552 return 1;
5553 }
5554 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
5555 return kvm_skip_emulated_instruction(vcpu);
5556
5557 case INVPCID_TYPE_SINGLE_CTXT:
5558 if (!pcid_enabled && (operand.pcid != 0)) {
5559 kvm_inject_gp(vcpu, 0);
5560 return 1;
5561 }
5562
5563 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
5564 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07005565 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005566 }
5567
Junaid Shahidb94742c2018-06-27 14:59:20 -07005568 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005569 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd)
Junaid Shahidb94742c2018-06-27 14:59:20 -07005570 == operand.pcid)
5571 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07005572
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02005573 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005574 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005575 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07005576 * given PCID, then nothing needs to be done here because a
5577 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005578 */
5579
5580 return kvm_skip_emulated_instruction(vcpu);
5581
5582 case INVPCID_TYPE_ALL_NON_GLOBAL:
5583 /*
5584 * Currently, KVM doesn't mark global entries in the shadow
5585 * page tables, so a non-global flush just degenerates to a
5586 * global flush. If needed, we could optimize this later by
5587 * keeping track of global entries in shadow page tables.
5588 */
5589
5590 /* fall-through */
5591 case INVPCID_TYPE_ALL_INCL_GLOBAL:
5592 kvm_mmu_unload(vcpu);
5593 return kvm_skip_emulated_instruction(vcpu);
5594
5595 default:
5596 BUG(); /* We have already checked above that type <= 3 */
5597 }
5598}
5599
Kai Huang843e4332015-01-28 10:54:28 +08005600static int handle_pml_full(struct kvm_vcpu *vcpu)
5601{
5602 unsigned long exit_qualification;
5603
5604 trace_kvm_pml_full(vcpu->vcpu_id);
5605
Sean Christopherson5addc232020-04-15 13:34:53 -07005606 exit_qualification = vmx_get_exit_qual(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005607
5608 /*
5609 * PML buffer FULL happened while executing iret from NMI,
5610 * "blocked by NMI" bit has to be set before next VM entry.
5611 */
5612 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005613 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005614 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5615 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5616 GUEST_INTR_STATE_NMI);
5617
5618 /*
5619 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5620 * here.., and there's no userspace involvement needed for PML.
5621 */
5622 return 1;
5623}
5624
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005625static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07005626{
Sean Christopherson804939e2019-05-07 12:18:05 -07005627 struct vcpu_vmx *vmx = to_vmx(vcpu);
5628
5629 if (!vmx->req_immediate_exit &&
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005630 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) {
Sean Christophersond264ee02018-08-27 15:21:12 -07005631 kvm_lapic_expired_hv_timer(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005632 return EXIT_FASTPATH_REENTER_GUEST;
5633 }
Sean Christopherson804939e2019-05-07 12:18:05 -07005634
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005635 return EXIT_FASTPATH_NONE;
5636}
5637
5638static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5639{
5640 handle_fastpath_preemption_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005641 return 1;
5642}
5643
Sean Christophersone4027cf2018-12-03 13:53:12 -08005644/*
5645 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5646 * are overwritten by nested_vmx_setup() when nested=1.
5647 */
5648static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5649{
5650 kvm_queue_exception(vcpu, UD_VECTOR);
5651 return 1;
5652}
5653
Sean Christopherson0b665d32018-08-14 09:33:34 -07005654static int handle_encls(struct kvm_vcpu *vcpu)
5655{
5656 /*
5657 * SGX virtualization is not yet supported. There is no software
5658 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5659 * to prevent the guest from executing ENCLS.
5660 */
5661 kvm_queue_exception(vcpu, UD_VECTOR);
5662 return 1;
5663}
5664
Nadav Har'El0140cae2011-05-25 23:06:28 +03005665/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005666 * The exit handlers return 1 if the exit was handled fully and guest execution
5667 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5668 * to be done to userspace and return 0.
5669 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005670static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Sean Christopherson95b5a482019-04-19 22:50:59 -07005671 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005672 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005673 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005674 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005675 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005676 [EXIT_REASON_CR_ACCESS] = handle_cr,
5677 [EXIT_REASON_DR_ACCESS] = handle_dr,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005678 [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
5679 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
5680 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005681 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005682 [EXIT_REASON_HLT] = kvm_emulate_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005683 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005684 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005685 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005686 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005687 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5688 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5689 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5690 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5691 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5692 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5693 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5694 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5695 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005696 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5697 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005698 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005699 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005700 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005701 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005702 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005703 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005704 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5705 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005706 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5707 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005708 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005709 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005710 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005711 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005712 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5713 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005714 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005715 [EXIT_REASON_RDSEED] = handle_invalid_op,
Kai Huang843e4332015-01-28 10:54:28 +08005716 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005717 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005718 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005719 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005720 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005721};
5722
5723static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005724 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005725
Avi Kivity586f9602010-11-18 13:09:54 +02005726static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
5727{
Sean Christopherson5addc232020-04-15 13:34:53 -07005728 *info1 = vmx_get_exit_qual(vcpu);
Sean Christopherson87915852020-04-15 13:34:54 -07005729 *info2 = vmx_get_intr_info(vcpu);
Avi Kivity586f9602010-11-18 13:09:54 +02005730}
5731
Kai Huanga3eaa862015-11-04 13:46:05 +08005732static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005733{
Kai Huanga3eaa862015-11-04 13:46:05 +08005734 if (vmx->pml_pg) {
5735 __free_page(vmx->pml_pg);
5736 vmx->pml_pg = NULL;
5737 }
Kai Huang843e4332015-01-28 10:54:28 +08005738}
5739
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005740static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005741{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005742 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005743 u64 *pml_buf;
5744 u16 pml_idx;
5745
5746 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5747
5748 /* Do nothing if PML buffer is empty */
5749 if (pml_idx == (PML_ENTITY_NUM - 1))
5750 return;
5751
5752 /* PML index always points to next available PML buffer entity */
5753 if (pml_idx >= PML_ENTITY_NUM)
5754 pml_idx = 0;
5755 else
5756 pml_idx++;
5757
5758 pml_buf = page_address(vmx->pml_pg);
5759 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5760 u64 gpa;
5761
5762 gpa = pml_buf[pml_idx];
5763 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005764 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005765 }
5766
5767 /* reset PML index */
5768 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5769}
5770
5771/*
5772 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5773 * Called before reporting dirty_bitmap to userspace.
5774 */
5775static void kvm_flush_pml_buffers(struct kvm *kvm)
5776{
5777 int i;
5778 struct kvm_vcpu *vcpu;
5779 /*
5780 * We only need to kick vcpu out of guest mode here, as PML buffer
5781 * is flushed at beginning of all VMEXITs, and it's obvious that only
5782 * vcpus running in guest are possible to have unflushed GPAs in PML
5783 * buffer.
5784 */
5785 kvm_for_each_vcpu(i, vcpu, kvm)
5786 kvm_vcpu_kick(vcpu);
5787}
5788
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005789static void vmx_dump_sel(char *name, uint32_t sel)
5790{
5791 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005792 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005793 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5794 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5795 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5796}
5797
5798static void vmx_dump_dtsel(char *name, uint32_t limit)
5799{
5800 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5801 name, vmcs_read32(limit),
5802 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5803}
5804
Paolo Bonzini69090812019-04-15 15:16:17 +02005805void dump_vmcs(void)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005806{
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005807 u32 vmentry_ctl, vmexit_ctl;
5808 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
5809 unsigned long cr4;
5810 u64 efer;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005811
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005812 if (!dump_invalid_vmcs) {
5813 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
5814 return;
5815 }
5816
5817 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5818 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5819 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5820 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5821 cr4 = vmcs_readl(GUEST_CR4);
5822 efer = vmcs_read64(GUEST_IA32_EFER);
5823 secondary_exec_control = 0;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005824 if (cpu_has_secondary_exec_ctrls())
5825 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5826
5827 pr_err("*** Guest State ***\n");
5828 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5829 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5830 vmcs_readl(CR0_GUEST_HOST_MASK));
5831 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5832 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5833 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5834 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5835 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5836 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005837 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5838 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5839 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5840 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005841 }
5842 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5843 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5844 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5845 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5846 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5847 vmcs_readl(GUEST_SYSENTER_ESP),
5848 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5849 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5850 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5851 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5852 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5853 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5854 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5855 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5856 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5857 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5858 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5859 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5860 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005861 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5862 efer, vmcs_read64(GUEST_IA32_PAT));
5863 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5864 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005865 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005866 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005867 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005868 pr_err("PerfGlobCtl = 0x%016llx\n",
5869 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005870 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005871 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005872 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5873 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5874 vmcs_read32(GUEST_ACTIVITY_STATE));
5875 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5876 pr_err("InterruptStatus = %04x\n",
5877 vmcs_read16(GUEST_INTR_STATUS));
5878
5879 pr_err("*** Host State ***\n");
5880 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5881 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5882 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5883 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5884 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5885 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5886 vmcs_read16(HOST_TR_SELECTOR));
5887 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5888 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5889 vmcs_readl(HOST_TR_BASE));
5890 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5891 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5892 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5893 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5894 vmcs_readl(HOST_CR4));
5895 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5896 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5897 vmcs_read32(HOST_IA32_SYSENTER_CS),
5898 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5899 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005900 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5901 vmcs_read64(HOST_IA32_EFER),
5902 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005903 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005904 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005905 pr_err("PerfGlobCtl = 0x%016llx\n",
5906 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005907
5908 pr_err("*** Control State ***\n");
5909 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5910 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5911 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5912 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5913 vmcs_read32(EXCEPTION_BITMAP),
5914 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5915 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5916 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5917 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5918 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5919 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5920 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5921 vmcs_read32(VM_EXIT_INTR_INFO),
5922 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5923 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5924 pr_err(" reason=%08x qualification=%016lx\n",
5925 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5926 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5927 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5928 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005929 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005930 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005931 pr_err("TSC Multiplier = 0x%016llx\n",
5932 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005933 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
5934 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
5935 u16 status = vmcs_read16(GUEST_INTR_STATUS);
5936 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
5937 }
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005938 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005939 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
5940 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005941 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005942 }
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005943 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5944 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5945 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005946 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005947 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5948 pr_err("PLE Gap=%08x Window=%08x\n",
5949 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5950 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5951 pr_err("Virtual processor ID = 0x%04x\n",
5952 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5953}
5954
Avi Kivity6aa8b732006-12-10 02:21:36 -08005955/*
5956 * The guest has exited. See if we can fix it or if we need userspace
5957 * assistance.
5958 */
Wanpeng Li404d5d72020-04-28 14:23:25 +08005959static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005960{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005961 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005962 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005963 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005964
Kai Huang843e4332015-01-28 10:54:28 +08005965 /*
5966 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5967 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5968 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5969 * mode as if vcpus is in root mode, the PML buffer must has been
5970 * flushed already.
5971 */
5972 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005973 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005974
Sean Christophersondb438592020-04-22 19:25:48 -07005975 /*
5976 * We should never reach this point with a pending nested VM-Enter, and
5977 * more specifically emulation of L2 due to invalid guest state (see
5978 * below) should never happen as that means we incorrectly allowed a
5979 * nested VM-Enter with an invalid vmcs12.
5980 */
5981 WARN_ON_ONCE(vmx->nested.nested_run_pending);
5982
Mohammed Gamal80ced182009-09-01 12:48:18 +02005983 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02005984 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02005985 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005986
Paolo Bonzini96b100c2020-03-17 18:32:50 +01005987 if (is_guest_mode(vcpu)) {
5988 /*
5989 * The host physical addresses of some pages of guest memory
5990 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
5991 * Page). The CPU may write to these pages via their host
5992 * physical address while L2 is running, bypassing any
5993 * address-translation-based dirty tracking (e.g. EPT write
5994 * protection).
5995 *
5996 * Mark them dirty on every exit from L2 to prevent them from
5997 * getting out of sync with dirty tracking.
5998 */
5999 nested_mark_vmcs12_pages_dirty(vcpu);
6000
Sean Christophersonf47baae2020-04-15 10:55:16 -07006001 if (nested_vmx_reflect_vmexit(vcpu))
Sean Christopherson789afc52020-04-15 10:55:10 -07006002 return 1;
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006003 }
Nadav Har'El644d7112011-05-25 23:12:35 +03006004
Mohammed Gamal51207022010-05-31 22:40:54 +03006005 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02006006 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03006007 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6008 vcpu->run->fail_entry.hardware_entry_failure_reason
6009 = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006010 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Mohammed Gamal51207022010-05-31 22:40:54 +03006011 return 0;
6012 }
6013
Avi Kivity29bd8a72007-09-10 17:27:03 +03006014 if (unlikely(vmx->fail)) {
Paolo Bonzini3b20e032019-07-19 18:15:08 +02006015 dump_vmcs();
Avi Kivity851ba692009-08-24 11:10:17 +03006016 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6017 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006018 = vmcs_read32(VM_INSTRUCTION_ERROR);
Jim Mattson8a14fe42020-06-03 16:56:22 -07006019 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006020 return 0;
6021 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006022
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006023 /*
6024 * Note:
6025 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6026 * delivery event since it indicates guest is accessing MMIO.
6027 * The vm-exit can be triggered again after return to guest that
6028 * will cause infinite loop.
6029 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006030 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006031 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006032 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00006033 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006034 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6035 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6036 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006037 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006038 vcpu->run->internal.data[0] = vectoring_info;
6039 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006040 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
6041 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
6042 vcpu->run->internal.ndata++;
6043 vcpu->run->internal.data[3] =
6044 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6045 }
Jim Mattson1aa561b2020-06-03 16:56:21 -07006046 vcpu->run->internal.data[vcpu->run->internal.ndata++] =
Jim Mattson8a14fe42020-06-03 16:56:22 -07006047 vcpu->arch.last_vmentry_cpu;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006048 return 0;
6049 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006050
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006051 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006052 vmx->loaded_vmcs->soft_vnmi_blocked)) {
Sean Christophersondb438592020-04-22 19:25:48 -07006053 if (!vmx_interrupt_blocked(vcpu)) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006054 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6055 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
6056 vcpu->arch.nmi_pending) {
6057 /*
6058 * This CPU don't support us in finding the end of an
6059 * NMI-blocked window if the guest runs with IRQs
6060 * disabled. So we pull the trigger after 1 s of
6061 * futile waiting, but inform the user about this.
6062 */
6063 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6064 "state on VCPU %d after 1 s timeout\n",
6065 __func__, vcpu->vcpu_id);
6066 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6067 }
6068 }
6069
Wanpeng Li404d5d72020-04-28 14:23:25 +08006070 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08006071 return 1;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006072
6073 if (exit_reason >= kvm_vmx_max_exit_handlers)
6074 goto unexpected_vmexit;
6075#ifdef CONFIG_RETPOLINE
6076 if (exit_reason == EXIT_REASON_MSR_WRITE)
6077 return kvm_emulate_wrmsr(vcpu);
6078 else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER)
6079 return handle_preemption_timer(vcpu);
6080 else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW)
6081 return handle_interrupt_window(vcpu);
6082 else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6083 return handle_external_interrupt(vcpu);
6084 else if (exit_reason == EXIT_REASON_HLT)
6085 return kvm_emulate_halt(vcpu);
6086 else if (exit_reason == EXIT_REASON_EPT_MISCONFIG)
6087 return handle_ept_misconfig(vcpu);
6088#endif
6089
6090 exit_reason = array_index_nospec(exit_reason,
6091 kvm_vmx_max_exit_handlers);
6092 if (!kvm_vmx_exit_handlers[exit_reason])
6093 goto unexpected_vmexit;
6094
6095 return kvm_vmx_exit_handlers[exit_reason](vcpu);
6096
6097unexpected_vmexit:
6098 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", exit_reason);
6099 dump_vmcs();
6100 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6101 vcpu->run->internal.suberror =
6102 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
Jim Mattson1aa561b2020-06-03 16:56:21 -07006103 vcpu->run->internal.ndata = 2;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006104 vcpu->run->internal.data[0] = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006105 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006106 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006107}
6108
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006109/*
6110 * Software based L1D cache flush which is used when microcode providing
6111 * the cache control MSR is not loaded.
6112 *
6113 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
6114 * flush it is required to read in 64 KiB because the replacement algorithm
6115 * is not exactly LRU. This could be sized at runtime via topology
6116 * information but as all relevant affected CPUs have 32KiB L1D cache size
6117 * there is no point in doing so.
6118 */
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006119static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006120{
6121 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006122
6123 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02006124 * This code is only executed when the the flush mode is 'cond' or
6125 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006126 */
Nicolai Stange427362a2018-07-21 22:25:00 +02006127 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02006128 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006129
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006130 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02006131 * Clear the per-vcpu flush bit, it gets set again
6132 * either from vcpu_run() or from one of the unsafe
6133 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006134 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02006135 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02006136 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02006137
6138 /*
6139 * Clear the per-cpu flush bit, it gets set again from
6140 * the interrupt handlers.
6141 */
6142 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
6143 kvm_clear_cpu_l1tf_flush_l1d();
6144
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006145 if (!flush_l1d)
6146 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006147 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006148
6149 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006150
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006151 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006152 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006153 return;
6154 }
6155
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006156 asm volatile(
6157 /* First ensure the pages are in the TLB */
6158 "xorl %%eax, %%eax\n"
6159 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02006160 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006161 "addl $4096, %%eax\n\t"
6162 "cmpl %%eax, %[size]\n\t"
6163 "jne .Lpopulate_tlb\n\t"
6164 "xorl %%eax, %%eax\n\t"
6165 "cpuid\n\t"
6166 /* Now fill the cache */
6167 "xorl %%eax, %%eax\n"
6168 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006169 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006170 "addl $64, %%eax\n\t"
6171 "cmpl %%eax, %[size]\n\t"
6172 "jne .Lfill_cache\n\t"
6173 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006174 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006175 [size] "r" (size)
6176 : "eax", "ebx", "ecx", "edx");
6177}
6178
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006179static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006180{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006181 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Liran Alon132f4f72019-11-11 14:30:54 +02006182 int tpr_threshold;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006183
6184 if (is_guest_mode(vcpu) &&
6185 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6186 return;
6187
Liran Alon132f4f72019-11-11 14:30:54 +02006188 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
Liran Alon02d496cf2019-11-11 14:30:55 +02006189 if (is_guest_mode(vcpu))
6190 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6191 else
6192 vmcs_write32(TPR_THRESHOLD, tpr_threshold);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006193}
6194
Sean Christopherson97b7ead2018-12-03 13:53:16 -08006195void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08006196{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006197 struct vcpu_vmx *vmx = to_vmx(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006198 u32 sec_exec_control;
6199
Jim Mattson8d860bb2018-05-09 16:56:05 -04006200 if (!lapic_in_kernel(vcpu))
6201 return;
6202
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07006203 if (!flexpriority_enabled &&
6204 !cpu_has_vmx_virtualize_x2apic_mode())
6205 return;
6206
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006207 /* Postpone execution until vmcs01 is the current VMCS. */
6208 if (is_guest_mode(vcpu)) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006209 vmx->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006210 return;
6211 }
6212
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006213 sec_exec_control = secondary_exec_controls_get(vmx);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006214 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6215 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08006216
Jim Mattson8d860bb2018-05-09 16:56:05 -04006217 switch (kvm_get_apic_mode(vcpu)) {
6218 case LAPIC_MODE_INVALID:
6219 WARN_ONCE(true, "Invalid local APIC state");
6220 case LAPIC_MODE_DISABLED:
6221 break;
6222 case LAPIC_MODE_XAPIC:
6223 if (flexpriority_enabled) {
6224 sec_exec_control |=
6225 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006226 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6227
6228 /*
6229 * Flush the TLB, reloading the APIC access page will
6230 * only do so if its physical address has changed, but
6231 * the guest may have inserted a non-APIC mapping into
6232 * the TLB while the APIC access page was disabled.
6233 */
6234 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006235 }
6236 break;
6237 case LAPIC_MODE_X2APIC:
6238 if (cpu_has_vmx_virtualize_x2apic_mode())
6239 sec_exec_control |=
6240 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6241 break;
Yang Zhang8d146952013-01-25 10:18:50 +08006242 }
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006243 secondary_exec_controls_set(vmx, sec_exec_control);
Yang Zhang8d146952013-01-25 10:18:50 +08006244
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006245 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006246}
6247
Sean Christophersona4148b72020-03-20 14:28:24 -07006248static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
Tang Chen38b99172014-09-24 15:57:54 +08006249{
Sean Christophersona4148b72020-03-20 14:28:24 -07006250 struct page *page;
6251
Sean Christopherson1196cb92020-03-20 14:28:23 -07006252 /* Defer reload until vmcs01 is the current VMCS. */
6253 if (is_guest_mode(vcpu)) {
6254 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true;
6255 return;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07006256 }
Sean Christopherson1196cb92020-03-20 14:28:23 -07006257
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006258 if (!(secondary_exec_controls_get(to_vmx(vcpu)) &
6259 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
6260 return;
6261
Sean Christophersona4148b72020-03-20 14:28:24 -07006262 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
6263 if (is_error_page(page))
6264 return;
6265
6266 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
Sean Christopherson1196cb92020-03-20 14:28:23 -07006267 vmx_flush_tlb_current(vcpu);
Sean Christophersona4148b72020-03-20 14:28:24 -07006268
6269 /*
6270 * Do not pin apic access page in memory, the MMU notifier
6271 * will call us again if it is migrated or swapped out.
6272 */
6273 put_page(page);
Tang Chen38b99172014-09-24 15:57:54 +08006274}
6275
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006276static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006277{
6278 u16 status;
6279 u8 old;
6280
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006281 if (max_isr == -1)
6282 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006283
6284 status = vmcs_read16(GUEST_INTR_STATUS);
6285 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006286 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006287 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006288 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006289 vmcs_write16(GUEST_INTR_STATUS, status);
6290 }
6291}
6292
6293static void vmx_set_rvi(int vector)
6294{
6295 u16 status;
6296 u8 old;
6297
Wei Wang4114c272014-11-05 10:53:43 +08006298 if (vector == -1)
6299 vector = 0;
6300
Yang Zhangc7c9c562013-01-25 10:18:51 +08006301 status = vmcs_read16(GUEST_INTR_STATUS);
6302 old = (u8)status & 0xff;
6303 if ((u8)vector != old) {
6304 status &= ~0xff;
6305 status |= (u8)vector;
6306 vmcs_write16(GUEST_INTR_STATUS, status);
6307 }
6308}
6309
6310static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6311{
Liran Alon851c1a182017-12-24 18:12:56 +02006312 /*
6313 * When running L2, updating RVI is only relevant when
6314 * vmcs12 virtual-interrupt-delivery enabled.
6315 * However, it can be enabled only when L1 also
6316 * intercepts external-interrupts and in that case
6317 * we should not update vmcs02 RVI but instead intercept
6318 * interrupt. Therefore, do nothing when running L2.
6319 */
6320 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08006321 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006322}
6323
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006324static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006325{
6326 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006327 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02006328 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006329
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006330 WARN_ON(!vcpu->arch.apicv_active);
6331 if (pi_test_on(&vmx->pi_desc)) {
6332 pi_clear_on(&vmx->pi_desc);
6333 /*
Liran Alond9ff2742019-11-11 14:25:25 +02006334 * IOMMU can write to PID.ON, so the barrier matters even on UP.
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006335 * But on x86 this is just a compiler barrier anyway.
6336 */
6337 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02006338 max_irr_updated =
6339 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6340
6341 /*
6342 * If we are running L2 and L1 has a new pending interrupt
6343 * which can be injected, we should re-evaluate
6344 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02006345 * If L1 intercepts external-interrupts, we should
6346 * exit from L2 to L1. Otherwise, interrupt should be
6347 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02006348 */
Liran Alon851c1a182017-12-24 18:12:56 +02006349 if (is_guest_mode(vcpu) && max_irr_updated) {
6350 if (nested_exit_on_intr(vcpu))
6351 kvm_vcpu_exiting_guest_mode(vcpu);
6352 else
6353 kvm_make_request(KVM_REQ_EVENT, vcpu);
6354 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006355 } else {
6356 max_irr = kvm_lapic_find_highest_irr(vcpu);
6357 }
6358 vmx_hwapic_irr_update(vcpu, max_irr);
6359 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006360}
6361
Wanpeng Li17e433b2019-08-05 10:03:19 +08006362static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
6363{
Joao Martins9482ae42019-11-11 17:20:10 +00006364 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6365
6366 return pi_test_on(pi_desc) ||
Joao Martins29881b62019-11-11 17:20:12 +00006367 (pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));
Wanpeng Li17e433b2019-08-05 10:03:19 +08006368}
6369
Andrey Smetanin63086302015-11-10 15:36:32 +03006370static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006371{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006372 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08006373 return;
6374
Yang Zhangc7c9c562013-01-25 10:18:51 +08006375 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6376 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6377 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6378 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6379}
6380
Paolo Bonzini967235d2016-12-19 14:03:45 +01006381static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
6382{
6383 struct vcpu_vmx *vmx = to_vmx(vcpu);
6384
6385 pi_clear_on(&vmx->pi_desc);
6386 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
6387}
6388
Sean Christopherson95b5a482019-04-19 22:50:59 -07006389static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006390{
Sean Christopherson87915852020-04-15 13:34:54 -07006391 u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006392
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006393 /* if exit due to PF check for async PF */
Sean Christopherson87915852020-04-15 13:34:54 -07006394 if (is_page_fault(intr_info)) {
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02006395 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags();
Andi Kleena0861c02009-06-08 17:37:09 +08006396 /* Handle machine checks before interrupts are enabled */
Sean Christopherson87915852020-04-15 13:34:54 -07006397 } else if (is_machine_check(intr_info)) {
Andi Kleena0861c02009-06-08 17:37:09 +08006398 kvm_machine_check();
Gleb Natapov20f65982009-05-11 13:35:55 +03006399 /* We need to handle NMIs before interrupts are enabled */
Sean Christopherson87915852020-04-15 13:34:54 -07006400 } else if (is_nmi(intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07006401 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03006402 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07006403 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006404 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03006405}
Gleb Natapov20f65982009-05-11 13:35:55 +03006406
Sean Christopherson95b5a482019-04-19 22:50:59 -07006407static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006408{
Sean Christopherson49def502019-04-19 22:50:56 -07006409 unsigned int vector;
6410 unsigned long entry;
6411#ifdef CONFIG_X86_64
6412 unsigned long tmp;
6413#endif
6414 gate_desc *desc;
Sean Christopherson87915852020-04-15 13:34:54 -07006415 u32 intr_info = vmx_get_intr_info(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006416
Sean Christopherson49def502019-04-19 22:50:56 -07006417 if (WARN_ONCE(!is_external_intr(intr_info),
6418 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
6419 return;
6420
6421 vector = intr_info & INTR_INFO_VECTOR_MASK;
Sean Christopherson23420802019-04-19 22:50:57 -07006422 desc = (gate_desc *)host_idt_base + vector;
Sean Christopherson49def502019-04-19 22:50:56 -07006423 entry = gate_offset(desc);
6424
Sean Christopherson165072b2019-04-19 22:50:58 -07006425 kvm_before_interrupt(vcpu);
6426
Sean Christopherson49def502019-04-19 22:50:56 -07006427 asm volatile(
Yang Zhanga547c6d2013-04-11 19:25:10 +08006428#ifdef CONFIG_X86_64
Uros Bizjak551896e2020-05-04 17:57:06 +02006429 "mov %%rsp, %[sp]\n\t"
6430 "and $-16, %%rsp\n\t"
6431 "push %[ss]\n\t"
Sean Christopherson49def502019-04-19 22:50:56 -07006432 "push %[sp]\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08006433#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006434 "pushf\n\t"
Uros Bizjak551896e2020-05-04 17:57:06 +02006435 "push %[cs]\n\t"
Sean Christopherson49def502019-04-19 22:50:56 -07006436 CALL_NOSPEC
6437 :
Yang Zhanga547c6d2013-04-11 19:25:10 +08006438#ifdef CONFIG_X86_64
Sean Christopherson49def502019-04-19 22:50:56 -07006439 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08006440#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006441 ASM_CALL_CONSTRAINT
6442 :
Nick Desaulniers428b8f12020-03-23 12:12:43 -07006443 [thunk_target]"r"(entry),
Uros Bizjak551896e2020-05-04 17:57:06 +02006444#ifdef CONFIG_X86_64
Sean Christopherson49def502019-04-19 22:50:56 -07006445 [ss]"i"(__KERNEL_DS),
Uros Bizjak551896e2020-05-04 17:57:06 +02006446#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006447 [cs]"i"(__KERNEL_CS)
6448 );
Sean Christopherson165072b2019-04-19 22:50:58 -07006449
6450 kvm_after_interrupt(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006451}
Sean Christopherson95b5a482019-04-19 22:50:59 -07006452STACK_FRAME_NON_STANDARD(handle_external_interrupt_irqoff);
6453
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006454static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006455{
6456 struct vcpu_vmx *vmx = to_vmx(vcpu);
6457
6458 if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6459 handle_external_interrupt_irqoff(vcpu);
6460 else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)
6461 handle_exception_nmi_irqoff(vmx);
6462}
Yang Zhanga547c6d2013-04-11 19:25:10 +08006463
Sean Christophersoncb97c2d2020-02-18 15:40:11 -08006464static bool vmx_has_emulated_msr(u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006465{
Tom Lendackybc226f02018-05-10 22:06:39 +02006466 switch (index) {
6467 case MSR_IA32_SMBASE:
6468 /*
6469 * We cannot do SMM unless we can run the guest in big
6470 * real mode.
6471 */
6472 return enable_unrestricted_guest || emulate_invalid_guest_state;
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02006473 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
6474 return nested;
Tom Lendackybc226f02018-05-10 22:06:39 +02006475 case MSR_AMD64_VIRT_SPEC_CTRL:
6476 /* This is AMD only. */
6477 return false;
6478 default:
6479 return true;
6480 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006481}
6482
Avi Kivity51aa01d2010-07-20 14:31:20 +03006483static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6484{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006485 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006486 bool unblock_nmi;
6487 u8 vector;
6488 bool idtv_info_valid;
6489
6490 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006491
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006492 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006493 if (vmx->loaded_vmcs->nmi_known_unmasked)
6494 return;
Sean Christopherson87915852020-04-15 13:34:54 -07006495
6496 exit_intr_info = vmx_get_intr_info(&vmx->vcpu);
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006497 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6498 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6499 /*
6500 * SDM 3: 27.7.1.2 (September 2008)
6501 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6502 * a guest IRET fault.
6503 * SDM 3: 23.2.2 (September 2008)
6504 * Bit 12 is undefined in any of the following cases:
6505 * If the VM exit sets the valid bit in the IDT-vectoring
6506 * information field.
6507 * If the VM exit is due to a double fault.
6508 */
6509 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6510 vector != DF_VECTOR && !idtv_info_valid)
6511 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6512 GUEST_INTR_STATE_NMI);
6513 else
6514 vmx->loaded_vmcs->nmi_known_unmasked =
6515 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6516 & GUEST_INTR_STATE_NMI);
6517 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
6518 vmx->loaded_vmcs->vnmi_blocked_time +=
6519 ktime_to_ns(ktime_sub(ktime_get(),
6520 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006521}
6522
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006523static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006524 u32 idt_vectoring_info,
6525 int instr_len_field,
6526 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006527{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006528 u8 vector;
6529 int type;
6530 bool idtv_info_valid;
6531
6532 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006533
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006534 vcpu->arch.nmi_injected = false;
6535 kvm_clear_exception_queue(vcpu);
6536 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006537
6538 if (!idtv_info_valid)
6539 return;
6540
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006541 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006542
Avi Kivity668f6122008-07-02 09:28:55 +03006543 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6544 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006545
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006546 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006547 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006548 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006549 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006550 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006551 * Clear bit "block by NMI" before VM entry if a NMI
6552 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006553 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006554 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006555 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006556 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006557 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006558 /* fall through */
6559 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006560 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006561 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03006562 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006563 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03006564 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006565 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006566 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006567 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006568 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03006569 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006570 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006571 break;
6572 default:
6573 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006574 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006575}
6576
Avi Kivity83422e12010-07-20 14:43:23 +03006577static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6578{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006579 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006580 VM_EXIT_INSTRUCTION_LEN,
6581 IDT_VECTORING_ERROR_CODE);
6582}
6583
Avi Kivityb463a6f2010-07-20 15:06:17 +03006584static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6585{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006586 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006587 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6588 VM_ENTRY_INSTRUCTION_LEN,
6589 VM_ENTRY_EXCEPTION_ERROR_CODE);
6590
6591 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6592}
6593
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006594static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6595{
6596 int i, nr_msrs;
6597 struct perf_guest_switch_msr *msrs;
6598
6599 msrs = perf_guest_get_msrs(&nr_msrs);
6600
6601 if (!msrs)
6602 return;
6603
6604 for (i = 0; i < nr_msrs; i++)
6605 if (msrs[i].host == msrs[i].guest)
6606 clear_atomic_switch_msr(vmx, msrs[i].msr);
6607 else
6608 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006609 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006610}
6611
Sean Christophersonf459a702018-08-27 15:21:11 -07006612static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006613{
6614 struct vcpu_vmx *vmx = to_vmx(vcpu);
6615 u64 tscl;
6616 u32 delta_tsc;
6617
Sean Christophersond264ee02018-08-27 15:21:12 -07006618 if (vmx->req_immediate_exit) {
Sean Christopherson804939e2019-05-07 12:18:05 -07006619 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
6620 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6621 } else if (vmx->hv_deadline_tsc != -1) {
Sean Christophersonf459a702018-08-27 15:21:11 -07006622 tscl = rdtsc();
6623 if (vmx->hv_deadline_tsc > tscl)
6624 /* set_hv_timer ensures the delta fits in 32-bits */
6625 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6626 cpu_preemption_timer_multi);
6627 else
6628 delta_tsc = 0;
6629
Sean Christopherson804939e2019-05-07 12:18:05 -07006630 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
6631 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6632 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
6633 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
6634 vmx->loaded_vmcs->hv_timer_soft_disabled = true;
Sean Christophersonf459a702018-08-27 15:21:11 -07006635 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006636}
6637
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006638void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006639{
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006640 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
6641 vmx->loaded_vmcs->host_state.rsp = host_rsp;
6642 vmcs_writel(HOST_RSP, host_rsp);
6643 }
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006644}
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006645
Wanpeng Li404d5d72020-04-28 14:23:25 +08006646static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006647{
6648 switch (to_vmx(vcpu)->exit_reason) {
6649 case EXIT_REASON_MSR_WRITE:
6650 return handle_fastpath_set_msr_irqoff(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04006651 case EXIT_REASON_PREEMPTION_TIMER:
6652 return handle_fastpath_preemption_timer(vcpu);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006653 default:
6654 return EXIT_FASTPATH_NONE;
6655 }
6656}
6657
Sean Christophersonfc2ba5a2019-01-25 07:41:19 -08006658bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006659
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006660static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
6661 struct vcpu_vmx *vmx)
6662{
6663 /*
6664 * VMENTER enables interrupts (host state), but the kernel state is
6665 * interrupts disabled when this is invoked. Also tell RCU about
6666 * it. This is the same logic as for exit_to_user_mode().
6667 *
6668 * This ensures that e.g. latency analysis on the host observes
6669 * guest mode as interrupt enabled.
6670 *
6671 * guest_enter_irqoff() informs context tracking about the
6672 * transition to guest mode and if enabled adjusts RCU state
6673 * accordingly.
6674 */
6675 instrumentation_begin();
6676 trace_hardirqs_on_prepare();
6677 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
6678 instrumentation_end();
6679
6680 guest_enter_irqoff();
6681 lockdep_hardirqs_on(CALLER_ADDR0);
6682
6683 /* L1D Flush includes CPU buffer clear to mitigate MDS */
6684 if (static_branch_unlikely(&vmx_l1d_should_flush))
6685 vmx_l1d_flush(vcpu);
6686 else if (static_branch_unlikely(&mds_user_clear))
6687 mds_clear_cpu_buffers();
6688
Thomas Gleixner2245d392020-07-08 21:52:00 +02006689 if (vcpu->arch.cr2 != native_read_cr2())
6690 native_write_cr2(vcpu->arch.cr2);
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006691
6692 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
6693 vmx->loaded_vmcs->launched);
6694
Thomas Gleixner2245d392020-07-08 21:52:00 +02006695 vcpu->arch.cr2 = native_read_cr2();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006696
6697 /*
6698 * VMEXIT disables interrupts (host state), but tracing and lockdep
6699 * have them in state 'on' as recorded before entering guest mode.
6700 * Same as enter_from_user_mode().
6701 *
6702 * guest_exit_irqoff() restores host context and reinstates RCU if
6703 * enabled and required.
6704 *
6705 * This needs to be done before the below as native_read_msr()
6706 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
6707 * into world and some more.
6708 */
6709 lockdep_hardirqs_off(CALLER_ADDR0);
6710 guest_exit_irqoff();
6711
6712 instrumentation_begin();
6713 trace_hardirqs_off_finish();
6714 instrumentation_end();
6715}
6716
Wanpeng Li404d5d72020-04-28 14:23:25 +08006717static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006718{
Wanpeng Li404d5d72020-04-28 14:23:25 +08006719 fastpath_t exit_fastpath;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006720 struct vcpu_vmx *vmx = to_vmx(vcpu);
6721 unsigned long cr3, cr4;
6722
Wanpeng Li404d5d72020-04-28 14:23:25 +08006723reenter_guest:
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006724 /* Record the guest's net vcpu time for enforced NMI injections. */
6725 if (unlikely(!enable_vnmi &&
6726 vmx->loaded_vmcs->soft_vnmi_blocked))
6727 vmx->loaded_vmcs->entry_time = ktime_get();
6728
6729 /* Don't enter VMX if guest state is invalid, let the exit handler
6730 start emulation until we arrive back to a valid state */
6731 if (vmx->emulation_required)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006732 return EXIT_FASTPATH_NONE;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006733
6734 if (vmx->ple_window_dirty) {
6735 vmx->ple_window_dirty = false;
6736 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6737 }
6738
wanpeng lic9dfd3f2020-02-17 18:37:43 +08006739 /*
6740 * We did this in prepare_switch_to_guest, because it needs to
6741 * be within srcu_read_lock.
6742 */
6743 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006744
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006745 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006746 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006747 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006748 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6749
6750 cr3 = __get_current_cr3_fast();
6751 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6752 vmcs_writel(HOST_CR3, cr3);
6753 vmx->loaded_vmcs->host_state.cr3 = cr3;
6754 }
6755
6756 cr4 = cr4_read_shadow();
6757 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
6758 vmcs_writel(HOST_CR4, cr4);
6759 vmx->loaded_vmcs->host_state.cr4 = cr4;
6760 }
6761
6762 /* When single-stepping over STI and MOV SS, we must clear the
6763 * corresponding interruptibility bits in the guest state. Otherwise
6764 * vmentry fails as it then expects bit 14 (BS) in pending debug
6765 * exceptions being set, but that's not correct for the guest debugging
6766 * case. */
6767 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6768 vmx_set_interrupt_shadow(vcpu, 0);
6769
Aaron Lewis139a12c2019-10-21 16:30:25 -07006770 kvm_load_guest_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006771
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006772 pt_guest_enter(vmx);
6773
Vitaly Kuznetsov49097762020-06-19 11:40:46 +02006774 atomic_switch_perf_msrs(vmx);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006775
Sean Christopherson804939e2019-05-07 12:18:05 -07006776 if (enable_preemption_timer)
6777 vmx_update_hv_timer(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006778
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08006779 if (lapic_in_kernel(vcpu) &&
6780 vcpu->arch.apic->lapic_timer.timer_advance_ns)
6781 kvm_wait_lapic_expire(vcpu);
6782
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006783 /*
6784 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6785 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6786 * is no need to worry about the conditional branch over the wrmsr
6787 * being speculatively taken.
6788 */
6789 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
6790
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006791 /* The actual VMENTER/EXIT is in the .noinstr.text section. */
6792 vmx_vcpu_enter_exit(vcpu, vmx);
Thomas Gleixner87fa7f32020-07-08 21:51:54 +02006793
6794 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006795 * We do not use IBRS in the kernel. If this vCPU has used the
6796 * SPEC_CTRL MSR it may have left it on; save the value and
6797 * turn it off. This is much more efficient than blindly adding
6798 * it to the atomic save/restore list. Especially as the former
6799 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6800 *
6801 * For non-nested case:
6802 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6803 * save it.
6804 *
6805 * For nested case:
6806 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6807 * save it.
6808 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006809 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006810 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006811
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006812 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006813
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006814 /* All fields are clean at this point */
6815 if (static_branch_unlikely(&enable_evmcs))
6816 current_evmcs->hv_clean_fields |=
6817 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6818
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08006819 if (static_branch_unlikely(&enable_evmcs))
6820 current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
6821
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006822 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006823 if (vmx->host_debugctlmsr)
6824 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006825
Avi Kivityaa67f602012-08-01 16:48:03 +03006826#ifndef CONFIG_X86_64
6827 /*
6828 * The sysexit path does not restore ds/es, so we must set them to
6829 * a reasonable value ourselves.
6830 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006831 * We can't defer this to vmx_prepare_switch_to_host() since that
6832 * function may be executed in interrupt context, which saves and
6833 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006834 */
6835 loadsegment(ds, __USER_DS);
6836 loadsegment(es, __USER_DS);
6837#endif
6838
Sean Christophersone5d03de2020-04-15 13:34:51 -07006839 vmx_register_cache_reset(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006840
Chao Peng2ef444f2018-10-24 16:05:12 +08006841 pt_guest_exit(vmx);
6842
Aaron Lewis139a12c2019-10-21 16:30:25 -07006843 kvm_load_host_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006844
Gleb Natapove0b890d2013-09-25 12:51:33 +03006845 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006846 vmx->idt_vectoring_info = 0;
6847
Sean Christopherson873e1da2020-04-10 10:47:02 -07006848 if (unlikely(vmx->fail)) {
6849 vmx->exit_reason = 0xdead;
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006850 return EXIT_FASTPATH_NONE;
Sean Christopherson873e1da2020-04-10 10:47:02 -07006851 }
6852
6853 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
6854 if (unlikely((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY))
Sean Christophersonbeb8d932019-04-19 22:50:55 -07006855 kvm_machine_check();
6856
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006857 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
6858
Sean Christopherson873e1da2020-04-10 10:47:02 -07006859 if (unlikely(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006860 return EXIT_FASTPATH_NONE;
6861
Jim Mattsonb060ca32017-09-14 16:31:42 -07006862 vmx->loaded_vmcs->launched = 1;
6863 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006864
Avi Kivity51aa01d2010-07-20 14:31:20 +03006865 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006866 vmx_complete_interrupts(vmx);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006867
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006868 if (is_guest_mode(vcpu))
6869 return EXIT_FASTPATH_NONE;
6870
6871 exit_fastpath = vmx_exit_handlers_fastpath(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006872 if (exit_fastpath == EXIT_FASTPATH_REENTER_GUEST) {
6873 if (!kvm_vcpu_exit_request(vcpu)) {
6874 /*
6875 * FIXME: this goto should be a loop in vcpu_enter_guest,
6876 * but it would incur the cost of a retpoline for now.
6877 * Revisit once static calls are available.
6878 */
Wanpeng Li379a3c82020-04-28 14:23:27 +08006879 if (vcpu->arch.apicv_active)
6880 vmx_sync_pir_to_irr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006881 goto reenter_guest;
6882 }
6883 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
6884 }
6885
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006886 return exit_fastpath;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006887}
6888
Avi Kivity6aa8b732006-12-10 02:21:36 -08006889static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6890{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006891 struct vcpu_vmx *vmx = to_vmx(vcpu);
6892
Kai Huang843e4332015-01-28 10:54:28 +08006893 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006894 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006895 free_vpid(vmx->vpid);
Sean Christopherson55d23752018-12-03 13:53:18 -08006896 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006897 free_loaded_vmcs(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006898}
6899
Sean Christopherson987b2592019-12-18 13:54:55 -08006900static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006901{
Ben Gardon41836832019-02-11 11:02:52 -08006902 struct vcpu_vmx *vmx;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006903 unsigned long *msr_bitmap;
Sean Christopherson34109c02019-12-18 13:54:50 -08006904 int i, cpu, err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006905
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006906 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0);
6907 vmx = to_vmx(vcpu);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006908
Peter Feiner4e595162016-07-07 14:49:58 -07006909 err = -ENOMEM;
6910
Sean Christopherson034d8e22019-12-18 13:54:49 -08006911 vmx->vpid = allocate_vpid();
6912
Peter Feiner4e595162016-07-07 14:49:58 -07006913 /*
6914 * If PML is turned on, failure on enabling PML just results in failure
6915 * of creating the vcpu, therefore we can simplify PML logic (by
6916 * avoiding dealing with cases, such as enabling PML partially on vcpus
Miaohe Lin67b0ae42019-12-11 14:26:22 +08006917 * for the guest), etc.
Peter Feiner4e595162016-07-07 14:49:58 -07006918 */
6919 if (enable_pml) {
Ben Gardon41836832019-02-11 11:02:52 -08006920 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Peter Feiner4e595162016-07-07 14:49:58 -07006921 if (!vmx->pml_pg)
Sean Christopherson987b2592019-12-18 13:54:55 -08006922 goto free_vpid;
Peter Feiner4e595162016-07-07 14:49:58 -07006923 }
6924
Jim Mattson7d737102019-12-03 16:24:42 -08006925 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) != NR_SHARED_MSRS);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006926
Xiaoyao Li4be53412019-10-20 17:11:00 +08006927 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6928 u32 index = vmx_msr_index[i];
6929 u32 data_low, data_high;
6930 int j = vmx->nmsrs;
6931
6932 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6933 continue;
6934 if (wrmsr_safe(index, data_low, data_high) < 0)
6935 continue;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006936
Xiaoyao Li4be53412019-10-20 17:11:00 +08006937 vmx->guest_msrs[j].index = i;
6938 vmx->guest_msrs[j].data = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006939 switch (index) {
6940 case MSR_IA32_TSX_CTRL:
6941 /*
6942 * No need to pass TSX_CTRL_CPUID_CLEAR through, so
6943 * let's avoid changing CPUID bits under the host
6944 * kernel's feet.
6945 */
6946 vmx->guest_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
6947 break;
6948 default:
6949 vmx->guest_msrs[j].mask = -1ull;
6950 break;
6951 }
Xiaoyao Li4be53412019-10-20 17:11:00 +08006952 ++vmx->nmsrs;
6953 }
6954
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006955 err = alloc_loaded_vmcs(&vmx->vmcs01);
6956 if (err < 0)
Jim Mattson7d737102019-12-03 16:24:42 -08006957 goto free_pml;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006958
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006959 msr_bitmap = vmx->vmcs01.msr_bitmap;
Jim Mattson788fc1e2018-11-09 09:35:11 -08006960 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006961 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
6962 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
6963 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6964 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6965 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6966 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
Sean Christopherson987b2592019-12-18 13:54:55 -08006967 if (kvm_cstate_in_guest(vcpu->kvm)) {
Wanpeng Lib5170062019-05-21 14:06:53 +08006968 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
6969 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
6970 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
6971 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
6972 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006973 vmx->msr_bitmap_mode = 0;
6974
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006975 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03006976 cpu = get_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006977 vmx_vcpu_load(vcpu, cpu);
6978 vcpu->cpu = cpu;
Xiaoyao Li1b842922019-10-20 17:11:01 +08006979 init_vmcs(vmx);
Sean Christopherson34109c02019-12-18 13:54:50 -08006980 vmx_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006981 put_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08006982 if (cpu_need_virtualize_apic_accesses(vcpu)) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006983 err = alloc_apic_access_page(vcpu->kvm);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006984 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006985 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02006986 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006987
Sean Christophersone90008d2018-03-05 12:04:37 -08006988 if (enable_ept && !enable_unrestricted_guest) {
Sean Christopherson987b2592019-12-18 13:54:55 -08006989 err = init_rmode_identity_map(vcpu->kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08006990 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02006991 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006992 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006993
Roman Kagan63aff652018-07-19 21:59:07 +03006994 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006995 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01006996 vmx_capability.ept);
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08006997 else
6998 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08006999
Wincy Van705699a2015-02-03 23:58:17 +08007000 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007001 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007002
Paolo Bonzinibab0c312020-02-11 18:40:58 +01007003 vcpu->arch.microcode_version = 0x100000000ULL;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007004 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007005
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02007006 /*
7007 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
7008 * or POSTED_INTR_WAKEUP_VECTOR.
7009 */
7010 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
7011 vmx->pi_desc.sn = 1;
7012
Lan Tianyu53963a72018-12-06 15:34:36 +08007013 vmx->ept_pointer = INVALID_PAGE;
7014
Sean Christophersona9dd6f02019-12-18 13:54:52 -08007015 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007016
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007017free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007018 free_loaded_vmcs(vmx->loaded_vmcs);
Peter Feiner4e595162016-07-07 14:49:58 -07007019free_pml:
7020 vmx_destroy_pml_buffer(vmx);
Sean Christopherson987b2592019-12-18 13:54:55 -08007021free_vpid:
Wanpeng Li991e7a02015-09-16 17:30:05 +08007022 free_vpid(vmx->vpid);
Sean Christophersona9dd6f02019-12-18 13:54:52 -08007023 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007024}
7025
Thomas Gleixner65fd4cb2019-02-19 11:10:49 +01007026#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
7027#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007028
Wanpeng Lib31c1142018-03-12 04:53:04 -07007029static int vmx_vm_init(struct kvm *kvm)
7030{
Tianyu Lan877ad952018-07-19 08:40:23 +00007031 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
7032
Wanpeng Lib31c1142018-03-12 04:53:04 -07007033 if (!ple_gap)
7034 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007035
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007036 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
7037 switch (l1tf_mitigation) {
7038 case L1TF_MITIGATION_OFF:
7039 case L1TF_MITIGATION_FLUSH_NOWARN:
7040 /* 'I explicitly don't care' is set */
7041 break;
7042 case L1TF_MITIGATION_FLUSH:
7043 case L1TF_MITIGATION_FLUSH_NOSMT:
7044 case L1TF_MITIGATION_FULL:
7045 /*
7046 * Warn upon starting the first VM in a potentially
7047 * insecure environment.
7048 */
Josh Poimboeufb2849092019-01-30 07:13:58 -06007049 if (sched_smt_active())
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007050 pr_warn_once(L1TF_MSG_SMT);
7051 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
7052 pr_warn_once(L1TF_MSG_L1D);
7053 break;
7054 case L1TF_MITIGATION_FULL_FORCE:
7055 /* Flush is enforced */
7056 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007057 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007058 }
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007059 kvm_apicv_init(kvm, enable_apicv);
Wanpeng Lib31c1142018-03-12 04:53:04 -07007060 return 0;
7061}
7062
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007063static int __init vmx_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03007064{
7065 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08007066 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007067
Sean Christophersonff10e222019-12-20 20:45:10 -08007068 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
7069 !this_cpu_has(X86_FEATURE_VMX)) {
7070 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id());
7071 return -EIO;
7072 }
7073
Sean Christopherson7caaa712018-12-03 13:53:01 -08007074 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007075 return -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007076 if (nested)
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01007077 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept);
Yang, Sheng002c7f72007-07-31 14:23:01 +03007078 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7079 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7080 smp_processor_id());
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007081 return -EIO;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007082 }
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007083 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007084}
7085
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007086static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007087{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007088 u8 cache;
7089 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007090
Chia-I Wu222f06e2020-02-13 13:30:34 -08007091 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in
7092 * memory aliases with conflicting memory types and sometimes MCEs.
7093 * We have to be careful as to what are honored and when.
7094 *
7095 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to
7096 * UC. The effective memory type is UC or WC depending on guest PAT.
7097 * This was historically the source of MCEs and we want to be
7098 * conservative.
7099 *
7100 * When there is no need to deal with noncoherent DMA (e.g., no VT-d
7101 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The
7102 * EPT memory type is set to WB. The effective memory type is forced
7103 * WB.
7104 *
7105 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The
7106 * EPT memory type is used to emulate guest CD/MTRR.
Sheng Yang522c68c2009-04-27 20:35:43 +08007107 */
Chia-I Wu222f06e2020-02-13 13:30:34 -08007108
Paolo Bonzini606decd2015-10-01 13:12:47 +02007109 if (is_mmio) {
7110 cache = MTRR_TYPE_UNCACHABLE;
7111 goto exit;
7112 }
7113
7114 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007115 ipat = VMX_EPT_IPAT_BIT;
7116 cache = MTRR_TYPE_WRBACK;
7117 goto exit;
7118 }
7119
7120 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7121 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02007122 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08007123 cache = MTRR_TYPE_WRBACK;
7124 else
7125 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007126 goto exit;
7127 }
7128
Xiao Guangrongff536042015-06-15 16:55:22 +08007129 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007130
7131exit:
7132 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08007133}
7134
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007135static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007136{
7137 /*
7138 * These bits in the secondary execution controls field
7139 * are dynamic, the others are mostly based on the hypervisor
7140 * architecture and the guest's CPUID. Do not touch the
7141 * dynamic bits.
7142 */
7143 u32 mask =
7144 SECONDARY_EXEC_SHADOW_VMCS |
7145 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02007146 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7147 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007148
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007149 u32 new_ctl = vmx->secondary_exec_control;
7150 u32 cur_ctl = secondary_exec_controls_get(vmx);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007151
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007152 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007153}
7154
David Matlack8322ebb2016-11-29 18:14:09 -08007155/*
7156 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
7157 * (indicating "allowed-1") if they are supported in the guest's CPUID.
7158 */
7159static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
7160{
7161 struct vcpu_vmx *vmx = to_vmx(vcpu);
7162 struct kvm_cpuid_entry2 *entry;
7163
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007164 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
7165 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08007166
7167#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
7168 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007169 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08007170} while (0)
7171
7172 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007173 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME));
7174 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME));
7175 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC));
7176 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE));
7177 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE));
7178 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE));
7179 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE));
7180 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE));
7181 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR));
7182 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM));
7183 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX));
7184 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX));
7185 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID));
7186 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE));
David Matlack8322ebb2016-11-29 18:14:09 -08007187
7188 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007189 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE));
7190 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP));
7191 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP));
7192 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU));
7193 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP));
7194 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57));
David Matlack8322ebb2016-11-29 18:14:09 -08007195
7196#undef cr4_fixed1_update
7197}
7198
Liran Alon5f76f6f2018-09-14 03:25:52 +03007199static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7200{
7201 struct vcpu_vmx *vmx = to_vmx(vcpu);
7202
7203 if (kvm_mpx_supported()) {
7204 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7205
7206 if (mpx_enabled) {
7207 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7208 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7209 } else {
7210 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7211 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7212 }
7213 }
7214}
7215
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007216static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7217{
7218 struct vcpu_vmx *vmx = to_vmx(vcpu);
7219 struct kvm_cpuid_entry2 *best = NULL;
7220 int i;
7221
7222 for (i = 0; i < PT_CPUID_LEAVES; i++) {
7223 best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7224 if (!best)
7225 return;
7226 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7227 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7228 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7229 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7230 }
7231
7232 /* Get the number of configurable Address Ranges for filtering */
7233 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7234 PT_CAP_num_address_ranges);
7235
7236 /* Initialize and clear the no dependency bits */
7237 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7238 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7239
7240 /*
7241 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7242 * will inject an #GP
7243 */
7244 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7245 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7246
7247 /*
7248 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7249 * PSBFreq can be set
7250 */
7251 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7252 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7253 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7254
7255 /*
7256 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7257 * MTCFreq can be set
7258 */
7259 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7260 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7261 RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7262
7263 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7264 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7265 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7266 RTIT_CTL_PTW_EN);
7267
7268 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7269 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7270 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7271
7272 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7273 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7274 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7275
7276 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
7277 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7278 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7279
7280 /* unmask address range configure area */
7281 for (i = 0; i < vmx->pt_desc.addr_range; i++)
Gustavo A. R. Silvad14eff12018-12-26 14:40:59 -06007282 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007283}
7284
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007285static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08007286{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007287 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007288
Aaron Lewis72041602019-10-21 16:30:20 -07007289 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7290 vcpu->arch.xsaves_enabled = false;
7291
Paolo Bonzini80154d72017-08-24 13:55:35 +02007292 if (cpu_has_secondary_exec_ctrls()) {
7293 vmx_compute_secondary_exec_control(vmx);
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007294 vmcs_set_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007295 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007296
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007297 if (nested_vmx_allowed(vcpu))
7298 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007299 FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7300 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007301 else
7302 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007303 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7304 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
David Matlack8322ebb2016-11-29 18:14:09 -08007305
Liran Alon5f76f6f2018-09-14 03:25:52 +03007306 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08007307 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03007308 nested_vmx_entry_exit_ctls_update(vcpu);
7309 }
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007310
7311 if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7312 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7313 update_intel_pt_cfg(vcpu);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007314
7315 if (boot_cpu_has(X86_FEATURE_RTM)) {
7316 struct shared_msr_entry *msr;
7317 msr = find_msr_entry(vmx, MSR_IA32_TSX_CTRL);
7318 if (msr) {
7319 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
7320 vmx_set_guest_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
7321 }
7322 }
Sheng Yang0e851882009-12-18 16:48:46 +08007323}
7324
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007325static __init void vmx_set_cpu_caps(void)
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007326{
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007327 kvm_set_cpu_caps();
7328
7329 /* CPUID 0x1 */
7330 if (nested)
7331 kvm_cpu_cap_set(X86_FEATURE_VMX);
7332
7333 /* CPUID 0x7 */
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007334 if (kvm_mpx_supported())
7335 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX);
7336 if (cpu_has_vmx_invpcid())
7337 kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID);
7338 if (vmx_pt_mode_is_host_guest())
7339 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT);
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007340
Sean Christopherson90d2f602020-03-02 15:56:47 -08007341 if (vmx_umip_emulated())
7342 kvm_cpu_cap_set(X86_FEATURE_UMIP);
7343
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007344 /* CPUID 0xD.1 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01007345 supported_xss = 0;
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007346 if (!vmx_xsaves_supported())
7347 kvm_cpu_cap_clear(X86_FEATURE_XSAVES);
7348
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007349 /* CPUID 0x80000001 */
7350 if (!cpu_has_vmx_rdtscp())
7351 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007352
7353 if (vmx_waitpkg_supported())
7354 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007355}
7356
Sean Christophersond264ee02018-08-27 15:21:12 -07007357static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7358{
7359 to_vmx(vcpu)->req_immediate_exit = true;
7360}
7361
Oliver Upton35a57132020-02-04 15:26:31 -08007362static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7363 struct x86_instruction_info *info)
7364{
7365 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7366 unsigned short port;
7367 bool intercept;
7368 int size;
7369
7370 if (info->intercept == x86_intercept_in ||
7371 info->intercept == x86_intercept_ins) {
7372 port = info->src_val;
7373 size = info->dst_bytes;
7374 } else {
7375 port = info->dst_val;
7376 size = info->src_bytes;
7377 }
7378
7379 /*
7380 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7381 * VM-exits depend on the 'unconditional IO exiting' VM-execution
7382 * control.
7383 *
7384 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7385 */
7386 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7387 intercept = nested_cpu_has(vmcs12,
7388 CPU_BASED_UNCOND_IO_EXITING);
7389 else
7390 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7391
Oliver Upton86f7e902020-02-29 11:30:14 -08007392 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
Oliver Upton35a57132020-02-04 15:26:31 -08007393 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7394}
7395
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007396static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7397 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007398 enum x86_intercept_stage stage,
7399 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007400{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007401 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007402
Oliver Upton35a57132020-02-04 15:26:31 -08007403 switch (info->intercept) {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007404 /*
7405 * RDPID causes #UD if disabled through secondary execution controls.
7406 * Because it is marked as EmulateOnUD, we need to intercept it here.
7407 */
Oliver Upton35a57132020-02-04 15:26:31 -08007408 case x86_intercept_rdtscp:
7409 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007410 exception->vector = UD_VECTOR;
7411 exception->error_code_valid = false;
Oliver Upton35a57132020-02-04 15:26:31 -08007412 return X86EMUL_PROPAGATE_FAULT;
7413 }
7414 break;
7415
7416 case x86_intercept_in:
7417 case x86_intercept_ins:
7418 case x86_intercept_out:
7419 case x86_intercept_outs:
7420 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007421
Oliver Upton86f7e902020-02-29 11:30:14 -08007422 case x86_intercept_lgdt:
7423 case x86_intercept_lidt:
7424 case x86_intercept_lldt:
7425 case x86_intercept_ltr:
7426 case x86_intercept_sgdt:
7427 case x86_intercept_sidt:
7428 case x86_intercept_sldt:
7429 case x86_intercept_str:
7430 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
7431 return X86EMUL_CONTINUE;
7432
7433 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
7434 break;
7435
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007436 /* TODO: check more intercepts... */
Oliver Upton35a57132020-02-04 15:26:31 -08007437 default:
7438 break;
7439 }
7440
Paolo Bonzini07721fe2020-02-04 15:26:29 -08007441 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007442}
7443
Yunhong Jiang64672c92016-06-13 14:19:59 -07007444#ifdef CONFIG_X86_64
7445/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
7446static inline int u64_shl_div_u64(u64 a, unsigned int shift,
7447 u64 divisor, u64 *result)
7448{
7449 u64 low = a << shift, high = a >> (64 - shift);
7450
7451 /* To avoid the overflow on divq */
7452 if (high >= divisor)
7453 return 1;
7454
7455 /* Low hold the result, high hold rem which is discarded */
7456 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
7457 "rm" (divisor), "0" (low), "1" (high));
7458 *result = low;
7459
7460 return 0;
7461}
7462
Sean Christophersonf9927982019-04-16 13:32:46 -07007463static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
7464 bool *expired)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007465{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007466 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007467 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
Sean Christopherson39497d72019-04-17 10:15:32 -07007468 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007469
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007470 vmx = to_vmx(vcpu);
7471 tscl = rdtsc();
7472 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
7473 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Sean Christopherson39497d72019-04-17 10:15:32 -07007474 lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7475 ktimer->timer_advance_ns);
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007476
7477 if (delta_tsc > lapic_timer_advance_cycles)
7478 delta_tsc -= lapic_timer_advance_cycles;
7479 else
7480 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007481
7482 /* Convert to host delta tsc if tsc scaling is enabled */
7483 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
Sean Christopherson0967fa12019-04-16 13:32:48 -07007484 delta_tsc && u64_shl_div_u64(delta_tsc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007485 kvm_tsc_scaling_ratio_frac_bits,
Sean Christopherson0967fa12019-04-16 13:32:48 -07007486 vcpu->arch.tsc_scaling_ratio, &delta_tsc))
Yunhong Jiang64672c92016-06-13 14:19:59 -07007487 return -ERANGE;
7488
7489 /*
7490 * If the delta tsc can't fit in the 32 bit after the multi shift,
7491 * we can't use the preemption timer.
7492 * It's possible that it fits on later vmentries, but checking
7493 * on every vmentry is costly so we just use an hrtimer.
7494 */
7495 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
7496 return -ERANGE;
7497
7498 vmx->hv_deadline_tsc = tscl + delta_tsc;
Sean Christophersonf9927982019-04-16 13:32:46 -07007499 *expired = !delta_tsc;
7500 return 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007501}
7502
7503static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
7504{
Sean Christophersonf459a702018-08-27 15:21:11 -07007505 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007506}
7507#endif
7508
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007509static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007510{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007511 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007512 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007513}
7514
Kai Huang843e4332015-01-28 10:54:28 +08007515static void vmx_slot_enable_log_dirty(struct kvm *kvm,
7516 struct kvm_memory_slot *slot)
7517{
Jay Zhou3c9bd402020-02-27 09:32:27 +08007518 if (!kvm_dirty_log_manual_protect_and_init_set(kvm))
7519 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
Kai Huang843e4332015-01-28 10:54:28 +08007520 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
7521}
7522
7523static void vmx_slot_disable_log_dirty(struct kvm *kvm,
7524 struct kvm_memory_slot *slot)
7525{
7526 kvm_mmu_slot_set_dirty(kvm, slot);
7527}
7528
7529static void vmx_flush_log_dirty(struct kvm *kvm)
7530{
7531 kvm_flush_pml_buffers(kvm);
7532}
7533
7534static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
7535 struct kvm_memory_slot *memslot,
7536 gfn_t offset, unsigned long mask)
7537{
7538 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
7539}
7540
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007541static void __pi_post_block(struct kvm_vcpu *vcpu)
7542{
7543 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7544 struct pi_desc old, new;
7545 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007546
7547 do {
7548 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007549 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
7550 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007551
7552 dest = cpu_physical_id(vcpu->cpu);
7553
7554 if (x2apic_enabled())
7555 new.ndst = dest;
7556 else
7557 new.ndst = (dest << 8) & 0xFF00;
7558
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007559 /* set 'NV' to 'notification vector' */
7560 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02007561 } while (cmpxchg64(&pi_desc->control, old.control,
7562 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007563
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007564 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
7565 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007566 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007567 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007568 vcpu->pre_pcpu = -1;
7569 }
7570}
7571
Feng Wuefc64402015-09-18 22:29:51 +08007572/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007573 * This routine does the following things for vCPU which is going
7574 * to be blocked if VT-d PI is enabled.
7575 * - Store the vCPU to the wakeup list, so when interrupts happen
7576 * we can find the right vCPU to wake up.
7577 * - Change the Posted-interrupt descriptor as below:
7578 * 'NDST' <-- vcpu->pre_pcpu
7579 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
7580 * - If 'ON' is set during this process, which means at least one
7581 * interrupt is posted for this vCPU, we cannot block it, in
7582 * this case, return 1, otherwise, return 0.
7583 *
7584 */
Yunhong Jiangbc225122016-06-13 14:19:58 -07007585static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007586{
Feng Wubf9f6ac2015-09-18 22:29:55 +08007587 unsigned int dest;
7588 struct pi_desc old, new;
7589 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7590
7591 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08007592 !irq_remapping_cap(IRQ_POSTING_CAP) ||
7593 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +08007594 return 0;
7595
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007596 WARN_ON(irqs_disabled());
7597 local_irq_disable();
7598 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
7599 vcpu->pre_pcpu = vcpu->cpu;
7600 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
7601 list_add_tail(&vcpu->blocked_vcpu_list,
7602 &per_cpu(blocked_vcpu_on_cpu,
7603 vcpu->pre_pcpu));
7604 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
7605 }
Feng Wubf9f6ac2015-09-18 22:29:55 +08007606
7607 do {
7608 old.control = new.control = pi_desc->control;
7609
Feng Wubf9f6ac2015-09-18 22:29:55 +08007610 WARN((pi_desc->sn == 1),
7611 "Warning: SN field of posted-interrupts "
7612 "is set before blocking\n");
7613
7614 /*
7615 * Since vCPU can be preempted during this process,
7616 * vcpu->cpu could be different with pre_pcpu, we
7617 * need to set pre_pcpu as the destination of wakeup
7618 * notification event, then we can find the right vCPU
7619 * to wakeup in wakeup handler if interrupts happen
7620 * when the vCPU is in blocked state.
7621 */
7622 dest = cpu_physical_id(vcpu->pre_pcpu);
7623
7624 if (x2apic_enabled())
7625 new.ndst = dest;
7626 else
7627 new.ndst = (dest << 8) & 0xFF00;
7628
7629 /* set 'NV' to 'wakeup vector' */
7630 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02007631 } while (cmpxchg64(&pi_desc->control, old.control,
7632 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007633
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007634 /* We should not block the vCPU if an interrupt is posted for it. */
7635 if (pi_test_on(pi_desc) == 1)
7636 __pi_post_block(vcpu);
7637
7638 local_irq_enable();
7639 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007640}
7641
Yunhong Jiangbc225122016-06-13 14:19:58 -07007642static int vmx_pre_block(struct kvm_vcpu *vcpu)
7643{
7644 if (pi_pre_block(vcpu))
7645 return 1;
7646
Yunhong Jiang64672c92016-06-13 14:19:59 -07007647 if (kvm_lapic_hv_timer_in_use(vcpu))
7648 kvm_lapic_switch_to_sw_timer(vcpu);
7649
Yunhong Jiangbc225122016-06-13 14:19:58 -07007650 return 0;
7651}
7652
7653static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007654{
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007655 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007656 return;
7657
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007658 WARN_ON(irqs_disabled());
7659 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007660 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007661 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +08007662}
7663
Yunhong Jiangbc225122016-06-13 14:19:58 -07007664static void vmx_post_block(struct kvm_vcpu *vcpu)
7665{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007666 if (kvm_x86_ops.set_hv_timer)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007667 kvm_lapic_switch_to_hv_timer(vcpu);
7668
Yunhong Jiangbc225122016-06-13 14:19:58 -07007669 pi_post_block(vcpu);
7670}
7671
Feng Wubf9f6ac2015-09-18 22:29:55 +08007672/*
Feng Wuefc64402015-09-18 22:29:51 +08007673 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
7674 *
7675 * @kvm: kvm
7676 * @host_irq: host irq of the interrupt
7677 * @guest_irq: gsi of the interrupt
7678 * @set: set or unset PI
7679 * returns 0 on success, < 0 on failure
7680 */
7681static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
7682 uint32_t guest_irq, bool set)
7683{
7684 struct kvm_kernel_irq_routing_entry *e;
7685 struct kvm_irq_routing_table *irq_rt;
7686 struct kvm_lapic_irq irq;
7687 struct kvm_vcpu *vcpu;
7688 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007689 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +08007690
7691 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08007692 !irq_remapping_cap(IRQ_POSTING_CAP) ||
7693 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +08007694 return 0;
7695
7696 idx = srcu_read_lock(&kvm->irq_srcu);
7697 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007698 if (guest_irq >= irq_rt->nr_rt_entries ||
7699 hlist_empty(&irq_rt->map[guest_irq])) {
7700 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
7701 guest_irq, irq_rt->nr_rt_entries);
7702 goto out;
7703 }
Feng Wuefc64402015-09-18 22:29:51 +08007704
7705 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
7706 if (e->type != KVM_IRQ_ROUTING_MSI)
7707 continue;
7708 /*
7709 * VT-d PI cannot support posting multicast/broadcast
7710 * interrupts to a vCPU, we still use interrupt remapping
7711 * for these kind of interrupts.
7712 *
7713 * For lowest-priority interrupts, we only support
7714 * those with single CPU as the destination, e.g. user
7715 * configures the interrupts via /proc/irq or uses
7716 * irqbalance to make the interrupts single-CPU.
7717 *
7718 * We will support full lowest-priority interrupt later.
Alexander Graffdcf7562019-09-05 14:58:18 +02007719 *
7720 * In addition, we can only inject generic interrupts using
7721 * the PI mechanism, refuse to route others through it.
Feng Wuefc64402015-09-18 22:29:51 +08007722 */
7723
Radim Krčmář371313132016-07-12 22:09:27 +02007724 kvm_set_msi_irq(kvm, e, &irq);
Alexander Graffdcf7562019-09-05 14:58:18 +02007725 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
7726 !kvm_irq_is_postable(&irq)) {
Feng Wu23a1c252016-01-25 16:53:32 +08007727 /*
7728 * Make sure the IRTE is in remapped mode if
7729 * we don't handle it in posted mode.
7730 */
7731 ret = irq_set_vcpu_affinity(host_irq, NULL);
7732 if (ret < 0) {
7733 printk(KERN_INFO
7734 "failed to back to remapped mode, irq: %u\n",
7735 host_irq);
7736 goto out;
7737 }
7738
Feng Wuefc64402015-09-18 22:29:51 +08007739 continue;
Feng Wu23a1c252016-01-25 16:53:32 +08007740 }
Feng Wuefc64402015-09-18 22:29:51 +08007741
7742 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
7743 vcpu_info.vector = irq.vector;
7744
hu huajun2698d822018-04-11 15:16:40 +08007745 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +08007746 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
7747
7748 if (set)
7749 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +08007750 else
Feng Wuefc64402015-09-18 22:29:51 +08007751 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +08007752
7753 if (ret < 0) {
7754 printk(KERN_INFO "%s: failed to update PI IRTE\n",
7755 __func__);
7756 goto out;
7757 }
7758 }
7759
7760 ret = 0;
7761out:
7762 srcu_read_unlock(&kvm->irq_srcu, idx);
7763 return ret;
7764}
7765
Ashok Rajc45dcc72016-06-22 14:59:56 +08007766static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7767{
7768 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7769 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007770 FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007771 else
7772 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007773 ~FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007774}
7775
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007776static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Ladi Prosek72d7b372017-10-11 16:54:41 +02007777{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007778 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7779 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007780 return -EBUSY;
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -04007781 return !is_smm(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02007782}
7783
Ladi Prosek0234bf82017-10-11 16:54:40 +02007784static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7785{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007786 struct vcpu_vmx *vmx = to_vmx(vcpu);
7787
7788 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7789 if (vmx->nested.smm.guest_mode)
7790 nested_vmx_vmexit(vcpu, -1, 0, 0);
7791
7792 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7793 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007794 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007795 return 0;
7796}
7797
Sean Christophersoned193212019-04-02 08:03:09 -07007798static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007799{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007800 struct vcpu_vmx *vmx = to_vmx(vcpu);
7801 int ret;
7802
7803 if (vmx->nested.smm.vmxon) {
7804 vmx->nested.vmxon = true;
7805 vmx->nested.smm.vmxon = false;
7806 }
7807
7808 if (vmx->nested.smm.guest_mode) {
Sean Christophersona633e412018-09-26 09:23:47 -07007809 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007810 if (ret)
7811 return ret;
7812
7813 vmx->nested.smm.guest_mode = false;
7814 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007815 return 0;
7816}
7817
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007818static void enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007819{
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007820 /* RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007821}
7822
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007823static bool vmx_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
7824{
Yi Wang9481b7f2019-07-15 12:35:17 +08007825 return false;
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007826}
7827
Liran Alon4b9852f2019-08-26 13:24:49 +03007828static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7829{
7830 return to_vmx(vcpu)->nested.vmxon;
7831}
7832
Jim Mattson93dff2f2020-05-08 13:36:43 -07007833static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
7834{
7835 if (is_guest_mode(vcpu)) {
7836 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer;
7837
7838 if (hrtimer_try_to_cancel(timer) == 1)
7839 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
7840 }
7841}
7842
Sean Christopherson6e4fd062020-03-21 13:26:01 -07007843static void hardware_unsetup(void)
Sean Christophersona3203382018-12-03 13:53:11 -08007844{
7845 if (nested)
7846 nested_vmx_hardware_unsetup();
7847
7848 free_kvm_area();
7849}
7850
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007851static bool vmx_check_apicv_inhibit_reasons(ulong bit)
7852{
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -06007853 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
7854 BIT(APICV_INHIBIT_REASON_HYPERV);
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007855
7856 return supported & BIT(bit);
7857}
7858
Sean Christophersone286ac02020-03-21 13:26:02 -07007859static struct kvm_x86_ops vmx_x86_ops __initdata = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007860 .hardware_unsetup = hardware_unsetup,
Sean Christopherson484014f2020-03-21 13:25:57 -07007861
Avi Kivity6aa8b732006-12-10 02:21:36 -08007862 .hardware_enable = hardware_enable,
7863 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007864 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007865 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007866
Sean Christopherson484014f2020-03-21 13:25:57 -07007867 .vm_size = sizeof(struct kvm_vmx),
Wanpeng Lib31c1142018-03-12 04:53:04 -07007868 .vm_init = vmx_vm_init,
7869
Avi Kivity6aa8b732006-12-10 02:21:36 -08007870 .vcpu_create = vmx_create_vcpu,
7871 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007872 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007873
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007874 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007875 .vcpu_load = vmx_vcpu_load,
7876 .vcpu_put = vmx_vcpu_put,
7877
Paolo Bonzini69869822020-07-10 17:48:06 +02007878 .update_exception_bitmap = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007879 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007880 .get_msr = vmx_get_msr,
7881 .set_msr = vmx_set_msr,
7882 .get_segment_base = vmx_get_segment_base,
7883 .get_segment = vmx_get_segment,
7884 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007885 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007886 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7887 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007888 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007889 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007890 .get_idt = vmx_get_idt,
7891 .set_idt = vmx_set_idt,
7892 .get_gdt = vmx_get_gdt,
7893 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007894 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007895 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007896 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007897 .get_rflags = vmx_get_rflags,
7898 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007899
Sean Christopherson77809382020-03-20 14:28:18 -07007900 .tlb_flush_all = vmx_flush_tlb_all,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07007901 .tlb_flush_current = vmx_flush_tlb_current,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007902 .tlb_flush_gva = vmx_flush_tlb_gva,
Sean Christophersone64419d2020-03-20 14:28:10 -07007903 .tlb_flush_guest = vmx_flush_tlb_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007904
Avi Kivity6aa8b732006-12-10 02:21:36 -08007905 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007906 .handle_exit = vmx_handle_exit,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08007907 .skip_emulated_instruction = vmx_skip_emulated_instruction,
7908 .update_emulated_instruction = vmx_update_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007909 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7910 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007911 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007912 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007913 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007914 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007915 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007916 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007917 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007918 .get_nmi_mask = vmx_get_nmi_mask,
7919 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007920 .enable_nmi_window = enable_nmi_window,
7921 .enable_irq_window = enable_irq_window,
7922 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007923 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007924 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007925 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007926 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007927 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007928 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007929 .hwapic_irr_update = vmx_hwapic_irr_update,
7930 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007931 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007932 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7933 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Wanpeng Li17e433b2019-08-05 10:03:19 +08007934 .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007935
Izik Eiduscbc94022007-10-25 00:29:55 +02007936 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007937 .set_identity_map_addr = vmx_set_identity_map_addr,
Sean Christopherson0047fca2020-05-01 21:32:33 -07007938 .get_tdp_level = vmx_get_tdp_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007939 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007940
Avi Kivity586f9602010-11-18 13:09:54 +02007941 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007942
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007943 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007944
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007945 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007946
Leonid Shatz326e7422018-11-06 12:14:25 +02007947 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007948
Sean Christopherson484014f2020-03-21 13:25:57 -07007949 .load_mmu_pgd = vmx_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007950
7951 .check_intercept = vmx_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007952 .handle_exit_irqoff = vmx_handle_exit_irqoff,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007953
Sean Christophersond264ee02018-08-27 15:21:12 -07007954 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007955
7956 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007957
7958 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7959 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7960 .flush_log_dirty = vmx_flush_log_dirty,
7961 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +02007962
Feng Wubf9f6ac2015-09-18 22:29:55 +08007963 .pre_block = vmx_pre_block,
7964 .post_block = vmx_post_block,
7965
Wei Huang25462f72015-06-19 15:45:05 +02007966 .pmu_ops = &intel_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04007967 .nested_ops = &vmx_nested_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007968
7969 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007970
7971#ifdef CONFIG_X86_64
7972 .set_hv_timer = vmx_set_hv_timer,
7973 .cancel_hv_timer = vmx_cancel_hv_timer,
7974#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007975
7976 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007977
Ladi Prosek72d7b372017-10-11 16:54:41 +02007978 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02007979 .pre_enter_smm = vmx_pre_enter_smm,
7980 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007981 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02007982
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007983 .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
Liran Alon4b9852f2019-08-26 13:24:49 +03007984 .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
Jim Mattson93dff2f2020-05-08 13:36:43 -07007985 .migrate_timers = vmx_migrate_timers,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007986};
7987
Avi Kivity6aa8b732006-12-10 02:21:36 -08007988static __init int hardware_setup(void)
7989{
7990 unsigned long host_bndcfgs;
7991 struct desc_ptr dt;
Sean Christopherson703c3352020-03-02 15:57:03 -08007992 int r, i, ept_lpage_level;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007993
Avi Kivity6aa8b732006-12-10 02:21:36 -08007994 store_idt(&dt);
7995 host_idt_base = dt.address;
7996
7997 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7998 kvm_define_shared_msr(i, vmx_msr_index[i]);
7999
8000 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
8001 return -EIO;
8002
8003 if (boot_cpu_has(X86_FEATURE_NX))
8004 kvm_enable_efer_bits(EFER_NX);
8005
8006 if (boot_cpu_has(X86_FEATURE_MPX)) {
8007 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
8008 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
8009 }
8010
Sean Christopherson7f5581f2020-03-02 15:56:24 -08008011 if (!cpu_has_vmx_mpx())
Sean Christophersoncfc48182020-03-02 15:56:23 -08008012 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
8013 XFEATURE_MASK_BNDCSR);
8014
Avi Kivity6aa8b732006-12-10 02:21:36 -08008015 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
8016 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
8017 enable_vpid = 0;
8018
8019 if (!cpu_has_vmx_ept() ||
8020 !cpu_has_vmx_ept_4levels() ||
8021 !cpu_has_vmx_ept_mt_wb() ||
8022 !cpu_has_vmx_invept_global())
8023 enable_ept = 0;
8024
8025 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
8026 enable_ept_ad_bits = 0;
8027
8028 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Avi Kivity873a7c42006-12-13 00:34:14 -08008029 enable_unrestricted_guest = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008030
8031 if (!cpu_has_vmx_flexpriority())
8032 flexpriority_enabled = 0;
8033
8034 if (!cpu_has_virtual_nmis())
8035 enable_vnmi = 0;
8036
8037 /*
8038 * set_apic_access_page_addr() is used to reload apic access
8039 * page upon invalidation. No need to do anything if not
8040 * using the APIC_ACCESS_ADDR VMCS field.
8041 */
8042 if (!flexpriority_enabled)
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008043 vmx_x86_ops.set_apic_access_page_addr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008044
8045 if (!cpu_has_vmx_tpr_shadow())
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008046 vmx_x86_ops.update_cr8_intercept = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008047
Avi Kivity6aa8b732006-12-10 02:21:36 -08008048#if IS_ENABLED(CONFIG_HYPERV)
8049 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
8050 && enable_ept) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008051 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
8052 vmx_x86_ops.tlb_remote_flush_with_range =
Avi Kivity6aa8b732006-12-10 02:21:36 -08008053 hv_remote_flush_tlb_with_range;
8054 }
8055#endif
8056
8057 if (!cpu_has_vmx_ple()) {
8058 ple_gap = 0;
8059 ple_window = 0;
8060 ple_window_grow = 0;
8061 ple_window_max = 0;
8062 ple_window_shrink = 0;
8063 }
8064
8065 if (!cpu_has_vmx_apicv()) {
8066 enable_apicv = 0;
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008067 vmx_x86_ops.sync_pir_to_irr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008068 }
8069
8070 if (cpu_has_vmx_tsc_scaling()) {
8071 kvm_has_tsc_control = true;
8072 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
8073 kvm_tsc_scaling_ratio_frac_bits = 48;
8074 }
8075
8076 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8077
8078 if (enable_ept)
8079 vmx_enable_tdp();
Sean Christopherson703c3352020-03-02 15:57:03 -08008080
8081 if (!enable_ept)
8082 ept_lpage_level = 0;
8083 else if (cpu_has_vmx_ept_1g_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07008084 ept_lpage_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08008085 else if (cpu_has_vmx_ept_2m_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07008086 ept_lpage_level = PG_LEVEL_2M;
Sean Christopherson703c3352020-03-02 15:57:03 -08008087 else
Sean Christopherson3bae0452020-04-27 17:54:22 -07008088 ept_lpage_level = PG_LEVEL_4K;
Sean Christopherson703c3352020-03-02 15:57:03 -08008089 kvm_configure_mmu(enable_ept, ept_lpage_level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008090
8091 /*
8092 * Only enable PML when hardware supports PML feature, and both EPT
8093 * and EPT A/D bit features are enabled -- PML depends on them to work.
8094 */
8095 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
8096 enable_pml = 0;
8097
8098 if (!enable_pml) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008099 vmx_x86_ops.slot_enable_log_dirty = NULL;
8100 vmx_x86_ops.slot_disable_log_dirty = NULL;
8101 vmx_x86_ops.flush_log_dirty = NULL;
8102 vmx_x86_ops.enable_log_dirty_pt_masked = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008103 }
8104
8105 if (!cpu_has_vmx_preemption_timer())
8106 enable_preemption_timer = false;
8107
8108 if (enable_preemption_timer) {
8109 u64 use_timer_freq = 5000ULL * 1000 * 1000;
8110 u64 vmx_msr;
8111
8112 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8113 cpu_preemption_timer_multi =
8114 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8115
8116 if (tsc_khz)
8117 use_timer_freq = (u64)tsc_khz * 1000;
8118 use_timer_freq >>= cpu_preemption_timer_multi;
8119
8120 /*
8121 * KVM "disables" the preemption timer by setting it to its max
8122 * value. Don't use the timer if it might cause spurious exits
8123 * at a rate faster than 0.1 Hz (of uninterrupted guest time).
8124 */
8125 if (use_timer_freq > 0xffffffffu / 10)
8126 enable_preemption_timer = false;
8127 }
8128
8129 if (!enable_preemption_timer) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008130 vmx_x86_ops.set_hv_timer = NULL;
8131 vmx_x86_ops.cancel_hv_timer = NULL;
8132 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008133 }
8134
8135 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
8136
8137 kvm_mce_cap_supported |= MCG_LMCE_P;
8138
8139 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
8140 return -EINVAL;
8141 if (!enable_ept || !cpu_has_vmx_intel_pt())
8142 pt_mode = PT_MODE_SYSTEM;
8143
8144 if (nested) {
8145 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
8146 vmx_capability.ept);
8147
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07008148 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008149 if (r)
8150 return r;
8151 }
8152
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08008153 vmx_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08008154
Avi Kivity6aa8b732006-12-10 02:21:36 -08008155 r = alloc_kvm_area();
8156 if (r)
8157 nested_vmx_hardware_unsetup();
8158 return r;
8159}
8160
Sean Christophersond008dfd2020-03-21 13:25:56 -07008161static struct kvm_x86_init_ops vmx_init_ops __initdata = {
8162 .cpu_has_kvm_support = cpu_has_kvm_support,
8163 .disabled_by_bios = vmx_disabled_by_bios,
8164 .check_processor_compatibility = vmx_check_processor_compat,
8165 .hardware_setup = hardware_setup,
8166
8167 .runtime_ops = &vmx_x86_ops,
8168};
8169
Avi Kivity6aa8b732006-12-10 02:21:36 -08008170static void vmx_cleanup_l1d_flush(void)
8171{
8172 if (vmx_l1d_flush_pages) {
8173 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
8174 vmx_l1d_flush_pages = NULL;
8175 }
8176 /* Restore state so sysfs ignores VMX */
8177 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
8178}
8179
8180static void vmx_exit(void)
8181{
8182#ifdef CONFIG_KEXEC_CORE
8183 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
8184 synchronize_rcu();
8185#endif
8186
8187 kvm_exit();
8188
8189#if IS_ENABLED(CONFIG_HYPERV)
8190 if (static_branch_unlikely(&enable_evmcs)) {
8191 int cpu;
8192 struct hv_vp_assist_page *vp_ap;
8193 /*
8194 * Reset everything to support using non-enlightened VMCS
8195 * access later (e.g. when we reload the module with
8196 * enlightened_vmcs=0)
8197 */
8198 for_each_online_cpu(cpu) {
8199 vp_ap = hv_get_vp_assist_page(cpu);
8200
8201 if (!vp_ap)
8202 continue;
8203
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008204 vp_ap->nested_control.features.directhypercall = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008205 vp_ap->current_nested_vmcs = 0;
8206 vp_ap->enlighten_vmentry = 0;
8207 }
8208
8209 static_branch_disable(&enable_evmcs);
8210 }
8211#endif
8212 vmx_cleanup_l1d_flush();
8213}
8214module_exit(vmx_exit);
8215
8216static int __init vmx_init(void)
8217{
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008218 int r, cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008219
8220#if IS_ENABLED(CONFIG_HYPERV)
8221 /*
8222 * Enlightened VMCS usage should be recommended and the host needs
8223 * to support eVMCS v1 or above. We can also disable eVMCS support
8224 * with module parameter.
8225 */
8226 if (enlightened_vmcs &&
8227 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
8228 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
8229 KVM_EVMCS_VERSION) {
8230 int cpu;
8231
8232 /* Check that we have assist pages on all online CPUs */
8233 for_each_online_cpu(cpu) {
8234 if (!hv_get_vp_assist_page(cpu)) {
8235 enlightened_vmcs = false;
8236 break;
8237 }
8238 }
8239
8240 if (enlightened_vmcs) {
8241 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
8242 static_branch_enable(&enable_evmcs);
8243 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008244
8245 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
8246 vmx_x86_ops.enable_direct_tlbflush
8247 = hv_enable_direct_tlbflush;
8248
Avi Kivity6aa8b732006-12-10 02:21:36 -08008249 } else {
8250 enlightened_vmcs = false;
8251 }
8252#endif
8253
Sean Christophersond008dfd2020-03-21 13:25:56 -07008254 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008255 __alignof__(struct vcpu_vmx), THIS_MODULE);
8256 if (r)
8257 return r;
8258
8259 /*
8260 * Must be called after kvm_init() so enable_ept is properly set
8261 * up. Hand the parameter mitigation value in which was stored in
8262 * the pre module init parser. If no parameter was given, it will
8263 * contain 'auto' which will be turned into the default 'cond'
8264 * mitigation mode.
8265 */
Waiman Long19a36d32019-08-26 15:30:23 -04008266 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
8267 if (r) {
8268 vmx_exit();
8269 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008270 }
8271
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008272 for_each_possible_cpu(cpu) {
8273 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
8274 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
8275 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
8276 }
8277
Avi Kivity6aa8b732006-12-10 02:21:36 -08008278#ifdef CONFIG_KEXEC_CORE
8279 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8280 crash_vmclear_local_loaded_vmcss);
8281#endif
8282 vmx_check_vmcs12_offsets();
8283
8284 return 0;
8285}
8286module_init(vmx_init);