blob: a70d8f6d8aba77fabebcf4fa4b171e46648f206d [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;
Paolo Bonzinia0c13432020-07-10 17:48:08 +0200783 if (!vmx_need_pf_intercept(vcpu))
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{
Sean Christopherson2a40b902020-07-15 20:41:18 -07002936 struct kvm_mmu *mmu = vcpu->arch.mmu;
2937 u64 root_hpa = mmu->root_hpa;
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002938
2939 /* No flush required if the current context is invalid. */
2940 if (!VALID_PAGE(root_hpa))
2941 return;
2942
2943 if (enable_ept)
Sean Christopherson2a40b902020-07-15 20:41:18 -07002944 ept_sync_context(construct_eptp(vcpu, root_hpa,
2945 mmu->shadow_root_level));
Sean Christopherson33d19ec2020-03-20 14:28:16 -07002946 else if (!is_guest_mode(vcpu))
2947 vpid_sync_context(to_vmx(vcpu)->vpid);
2948 else
2949 vpid_sync_context(nested_get_vpid02(vcpu));
2950}
2951
Junaid Shahidfaff8752018-06-29 13:10:05 -07002952static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
2953{
Junaid Shahidfaff8752018-06-29 13:10:05 -07002954 /*
Sean Christophersonad104b52020-03-20 14:28:11 -07002955 * vpid_sync_vcpu_addr() is a nop if vmx->vpid==0, see the comment in
2956 * vmx_flush_tlb_guest() for an explanation of why this is ok.
Junaid Shahidfaff8752018-06-29 13:10:05 -07002957 */
Sean Christophersonad104b52020-03-20 14:28:11 -07002958 vpid_sync_vcpu_addr(to_vmx(vcpu)->vpid, addr);
Junaid Shahidfaff8752018-06-29 13:10:05 -07002959}
2960
Sean Christophersone64419d2020-03-20 14:28:10 -07002961static void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu)
2962{
2963 /*
2964 * vpid_sync_context() is a nop if vmx->vpid==0, e.g. if enable_vpid==0
2965 * or a vpid couldn't be allocated for this vCPU. VM-Enter and VM-Exit
2966 * are required to flush GVA->{G,H}PA mappings from the TLB if vpid is
2967 * disabled (VM-Enter with vpid enabled and vpid==0 is disallowed),
2968 * i.e. no explicit INVVPID is necessary.
2969 */
2970 vpid_sync_context(to_vmx(vcpu)->vpid);
2971}
2972
Sheng Yang14394422008-04-28 12:24:45 +08002973static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2974{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002975 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2976
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07002977 if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002978 return;
2979
Paolo Bonzinibf03d4f2019-06-06 18:52:44 +02002980 if (is_pae_paging(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002981 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
2982 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
2983 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
2984 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002985 }
2986}
2987
Sean Christopherson97b7ead2018-12-03 13:53:16 -08002988void ept_save_pdptrs(struct kvm_vcpu *vcpu)
Avi Kivity8f5d5492009-05-31 18:41:29 +03002989{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03002990 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
2991
Sean Christopherson9932b492020-04-15 13:34:50 -07002992 if (WARN_ON_ONCE(!is_pae_paging(vcpu)))
2993 return;
2994
2995 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2996 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2997 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2998 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002999
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003000 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003001}
3002
Sheng Yang14394422008-04-28 12:24:45 +08003003static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3004 unsigned long cr0,
3005 struct kvm_vcpu *vcpu)
3006{
Sean Christopherson2183f562019-05-07 12:17:56 -07003007 struct vcpu_vmx *vmx = to_vmx(vcpu);
3008
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07003009 if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
Sean Christopherson34059c22019-09-27 14:45:23 -07003010 vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003011 if (!(cr0 & X86_CR0_PG)) {
3012 /* From paging/starting to nonpaging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003013 exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3014 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003015 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003016 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003017 } else if (!is_paging(vcpu)) {
3018 /* From nonpaging to paging */
Sean Christopherson2183f562019-05-07 12:17:56 -07003019 exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3020 CPU_BASED_CR3_STORE_EXITING);
Sheng Yang14394422008-04-28 12:24:45 +08003021 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003022 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003023 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003024
3025 if (!(cr0 & X86_CR0_WP))
3026 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003027}
3028
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003029void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003031 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003032 unsigned long hw_cr0;
3033
Sean Christopherson3de63472018-07-13 08:42:30 -07003034 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003035 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003036 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003037 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003038 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003039
Gleb Natapov218e7632013-01-21 15:36:45 +02003040 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3041 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042
Gleb Natapov218e7632013-01-21 15:36:45 +02003043 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3044 enter_rmode(vcpu);
3045 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003047#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003048 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003049 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003051 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052 exit_lmode(vcpu);
3053 }
3054#endif
3055
Sean Christophersonb4d18512018-03-05 12:04:40 -08003056 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08003057 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3058
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003060 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003061 vcpu->arch.cr0 = cr0;
Sean Christophersonbd31fe42020-05-01 21:32:31 -07003062 kvm_register_mark_available(vcpu, VCPU_EXREG_CR0);
Gleb Natapov14168782013-01-21 15:36:49 +02003063
3064 /* depends on vcpu->arch.cr0 to be set to a new value */
3065 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066}
3067
Sean Christophersond468d942020-07-15 20:41:20 -07003068static int vmx_get_max_tdp_level(void)
Sean Christopherson0047fca2020-05-01 21:32:33 -07003069{
Sean Christophersond468d942020-07-15 20:41:20 -07003070 if (cpu_has_vmx_ept_5levels())
Sean Christopherson0047fca2020-05-01 21:32:33 -07003071 return 5;
3072 return 4;
3073}
3074
Sean Christopherson2a40b902020-07-15 20:41:18 -07003075u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa,
3076 int root_level)
Sheng Yang14394422008-04-28 12:24:45 +08003077{
Yu Zhang855feb62017-08-24 20:27:55 +08003078 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08003079
Sean Christopherson2a40b902020-07-15 20:41:18 -07003080 eptp |= (root_level == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08003081
Peter Feiner995f00a2017-06-30 17:26:32 -07003082 if (enable_ept_ad_bits &&
3083 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02003084 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003085 eptp |= (root_hpa & PAGE_MASK);
3086
3087 return eptp;
3088}
3089
Sean Christopherson2a40b902020-07-15 20:41:18 -07003090static void vmx_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long pgd,
3091 int pgd_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092{
Tianyu Lan877ad952018-07-19 08:40:23 +00003093 struct kvm *kvm = vcpu->kvm;
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003094 bool update_guest_cr3 = true;
Sheng Yang14394422008-04-28 12:24:45 +08003095 unsigned long guest_cr3;
3096 u64 eptp;
3097
Avi Kivity089d0342009-03-23 18:26:32 +02003098 if (enable_ept) {
Sean Christopherson2a40b902020-07-15 20:41:18 -07003099 eptp = construct_eptp(vcpu, pgd, pgd_level);
Sheng Yang14394422008-04-28 12:24:45 +08003100 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00003101
Sean Christophersonafaf0b22020-03-21 13:26:00 -07003102 if (kvm_x86_ops.tlb_remote_flush) {
Tianyu Lan877ad952018-07-19 08:40:23 +00003103 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3104 to_vmx(vcpu)->ept_pointer = eptp;
3105 to_kvm_vmx(kvm)->ept_pointers_match
3106 = EPT_POINTERS_CHECK;
3107 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3108 }
3109
Paolo Bonzinidf7e0682020-05-20 08:37:37 -04003110 if (!enable_unrestricted_guest && !is_paging(vcpu))
Tianyu Lan877ad952018-07-19 08:40:23 +00003111 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Sean Christophersonb17b7432019-09-27 14:45:17 -07003112 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3113 guest_cr3 = vcpu->arch.cr3;
3114 else /* vmcs01.GUEST_CR3 is already up-to-date. */
3115 update_guest_cr3 = false;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003116 ept_load_pdptrs(vcpu);
Sean Christophersonbe100ef2020-03-20 14:28:33 -07003117 } else {
3118 guest_cr3 = pgd;
Sheng Yang14394422008-04-28 12:24:45 +08003119 }
3120
Sean Christopherson04f11ef2019-09-27 14:45:16 -07003121 if (update_guest_cr3)
3122 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123}
3124
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003125int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003127 struct vcpu_vmx *vmx = to_vmx(vcpu);
Ben Serebrin085e68e2015-04-16 11:58:05 -07003128 /*
3129 * Pass through host's Machine Check Enable value to hw_cr4, which
3130 * is in force while we are in guest mode. Do not let guests control
3131 * this bit, even if host CR4.MCE == 0.
3132 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003133 unsigned long hw_cr4;
3134
3135 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
3136 if (enable_unrestricted_guest)
3137 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003138 else if (vmx->rmode.vm86_active)
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003139 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3140 else
3141 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003142
Sean Christopherson64f7a112018-04-30 10:01:06 -07003143 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3144 if (cr4 & X86_CR4_UMIP) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003145 secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07003146 hw_cr4 &= ~X86_CR4_UMIP;
3147 } else if (!is_guest_mode(vcpu) ||
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003148 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3149 secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3150 }
Sean Christopherson64f7a112018-04-30 10:01:06 -07003151 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02003152
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003153 if (cr4 & X86_CR4_VMXE) {
3154 /*
3155 * To use VMXON (and later other VMX instructions), a guest
3156 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3157 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003158 * is here. We operate under the default treatment of SMM,
3159 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003160 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02003161 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003162 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003163 }
David Matlack38991522016-11-29 18:14:08 -08003164
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003165 if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003166 return 1;
3167
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003168 vcpu->arch.cr4 = cr4;
Sean Christophersonf98c1e72020-05-01 21:32:30 -07003169 kvm_register_mark_available(vcpu, VCPU_EXREG_CR4);
Sheng Yang14394422008-04-28 12:24:45 +08003170
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003171 if (!enable_unrestricted_guest) {
3172 if (enable_ept) {
3173 if (!is_paging(vcpu)) {
3174 hw_cr4 &= ~X86_CR4_PAE;
3175 hw_cr4 |= X86_CR4_PSE;
3176 } else if (!(cr4 & X86_CR4_PAE)) {
3177 hw_cr4 &= ~X86_CR4_PAE;
3178 }
3179 }
3180
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003181 /*
Huaitong Handdba2622016-03-22 16:51:15 +08003182 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3183 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3184 * to be manually disabled when guest switches to non-paging
3185 * mode.
3186 *
3187 * If !enable_unrestricted_guest, the CPU is always running
3188 * with CR0.PG=1 and CR4 needs to be modified.
3189 * If enable_unrestricted_guest, the CPU automatically
3190 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003191 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08003192 if (!is_paging(vcpu))
3193 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3194 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003195
Sheng Yang14394422008-04-28 12:24:45 +08003196 vmcs_writel(CR4_READ_SHADOW, cr4);
3197 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003198 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199}
3200
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003201void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202{
Avi Kivitya9179492011-01-03 14:28:52 +02003203 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 u32 ar;
3205
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003206 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003207 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003208 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003209 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003210 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003211 var->base = vmx_read_guest_seg_base(vmx, seg);
3212 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3213 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003214 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003215 var->base = vmx_read_guest_seg_base(vmx, seg);
3216 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3217 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3218 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003219 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 var->type = ar & 15;
3221 var->s = (ar >> 4) & 1;
3222 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003223 /*
3224 * Some userspaces do not preserve unusable property. Since usable
3225 * segment has to be present according to VMX spec we can use present
3226 * property to amend userspace bug by making unusable segment always
3227 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3228 * segment as unusable.
3229 */
3230 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 var->avl = (ar >> 12) & 1;
3232 var->l = (ar >> 13) & 1;
3233 var->db = (ar >> 14) & 1;
3234 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235}
3236
Avi Kivitya9179492011-01-03 14:28:52 +02003237static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3238{
Avi Kivitya9179492011-01-03 14:28:52 +02003239 struct kvm_segment s;
3240
3241 if (to_vmx(vcpu)->rmode.vm86_active) {
3242 vmx_get_segment(vcpu, &s, seg);
3243 return s.base;
3244 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003245 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003246}
3247
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003248int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003249{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003250 struct vcpu_vmx *vmx = to_vmx(vcpu);
3251
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003252 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003253 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003254 else {
3255 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003256 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003257 }
Avi Kivity69c73022011-03-07 15:26:44 +02003258}
3259
Avi Kivity653e3102007-05-07 10:55:37 +03003260static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 u32 ar;
3263
Avi Kivityf0495f92012-06-07 17:06:10 +03003264 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265 ar = 1 << 16;
3266 else {
3267 ar = var->type & 15;
3268 ar |= (var->s & 1) << 4;
3269 ar |= (var->dpl & 3) << 5;
3270 ar |= (var->present & 1) << 7;
3271 ar |= (var->avl & 1) << 12;
3272 ar |= (var->l & 1) << 13;
3273 ar |= (var->db & 1) << 14;
3274 ar |= (var->g & 1) << 15;
3275 }
Avi Kivity653e3102007-05-07 10:55:37 +03003276
3277 return ar;
3278}
3279
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003280void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
Avi Kivity653e3102007-05-07 10:55:37 +03003281{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003282 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003283 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003284
Avi Kivity2fb92db2011-04-27 19:42:18 +03003285 vmx_segment_cache_clear(vmx);
3286
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003287 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3288 vmx->rmode.segs[seg] = *var;
3289 if (seg == VCPU_SREG_TR)
3290 vmcs_write16(sf->selector, var->selector);
3291 else if (var->s)
3292 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003293 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003294 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003295
Avi Kivity653e3102007-05-07 10:55:37 +03003296 vmcs_writel(sf->base, var->base);
3297 vmcs_write32(sf->limit, var->limit);
3298 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003299
3300 /*
3301 * Fix the "Accessed" bit in AR field of segment registers for older
3302 * qemu binaries.
3303 * IA32 arch specifies that at the time of processor reset the
3304 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003305 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003306 * state vmexit when "unrestricted guest" mode is turned on.
3307 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3308 * tree. Newer qemu binaries with that qemu fix would not need this
3309 * kvm hack.
3310 */
3311 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003312 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003313
Gleb Natapovf924d662012-12-12 19:10:55 +02003314 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003315
3316out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003317 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003318}
3319
Avi Kivity6aa8b732006-12-10 02:21:36 -08003320static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3321{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003322 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323
3324 *db = (ar >> 14) & 1;
3325 *l = (ar >> 13) & 1;
3326}
3327
Gleb Natapov89a27f42010-02-16 10:51:48 +02003328static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003330 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3331 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332}
3333
Gleb Natapov89a27f42010-02-16 10:51:48 +02003334static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003336 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3337 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003338}
3339
Gleb Natapov89a27f42010-02-16 10:51:48 +02003340static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003341{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003342 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3343 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344}
3345
Gleb Natapov89a27f42010-02-16 10:51:48 +02003346static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003348 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3349 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350}
3351
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003352static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3353{
3354 struct kvm_segment var;
3355 u32 ar;
3356
3357 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003358 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003359 if (seg == VCPU_SREG_CS)
3360 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003361 ar = vmx_segment_access_rights(&var);
3362
3363 if (var.base != (var.selector << 4))
3364 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003365 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003366 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003367 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003368 return false;
3369
3370 return true;
3371}
3372
3373static bool code_segment_valid(struct kvm_vcpu *vcpu)
3374{
3375 struct kvm_segment cs;
3376 unsigned int cs_rpl;
3377
3378 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003379 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003380
Avi Kivity1872a3f2009-01-04 23:26:52 +02003381 if (cs.unusable)
3382 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003383 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003384 return false;
3385 if (!cs.s)
3386 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003387 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003388 if (cs.dpl > cs_rpl)
3389 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003390 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003391 if (cs.dpl != cs_rpl)
3392 return false;
3393 }
3394 if (!cs.present)
3395 return false;
3396
3397 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3398 return true;
3399}
3400
3401static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3402{
3403 struct kvm_segment ss;
3404 unsigned int ss_rpl;
3405
3406 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003407 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003408
Avi Kivity1872a3f2009-01-04 23:26:52 +02003409 if (ss.unusable)
3410 return true;
3411 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003412 return false;
3413 if (!ss.s)
3414 return false;
3415 if (ss.dpl != ss_rpl) /* DPL != RPL */
3416 return false;
3417 if (!ss.present)
3418 return false;
3419
3420 return true;
3421}
3422
3423static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3424{
3425 struct kvm_segment var;
3426 unsigned int rpl;
3427
3428 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003429 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003430
Avi Kivity1872a3f2009-01-04 23:26:52 +02003431 if (var.unusable)
3432 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003433 if (!var.s)
3434 return false;
3435 if (!var.present)
3436 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003437 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003438 if (var.dpl < rpl) /* DPL < RPL */
3439 return false;
3440 }
3441
3442 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3443 * rights flags
3444 */
3445 return true;
3446}
3447
3448static bool tr_valid(struct kvm_vcpu *vcpu)
3449{
3450 struct kvm_segment tr;
3451
3452 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3453
Avi Kivity1872a3f2009-01-04 23:26:52 +02003454 if (tr.unusable)
3455 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003456 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003457 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003458 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003459 return false;
3460 if (!tr.present)
3461 return false;
3462
3463 return true;
3464}
3465
3466static bool ldtr_valid(struct kvm_vcpu *vcpu)
3467{
3468 struct kvm_segment ldtr;
3469
3470 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3471
Avi Kivity1872a3f2009-01-04 23:26:52 +02003472 if (ldtr.unusable)
3473 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003474 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003475 return false;
3476 if (ldtr.type != 2)
3477 return false;
3478 if (!ldtr.present)
3479 return false;
3480
3481 return true;
3482}
3483
3484static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3485{
3486 struct kvm_segment cs, ss;
3487
3488 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3489 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3490
Nadav Amitb32a9912015-03-29 16:33:04 +03003491 return ((cs.selector & SEGMENT_RPL_MASK) ==
3492 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003493}
3494
3495/*
3496 * Check if guest state is valid. Returns true if valid, false if
3497 * not.
3498 * We assume that registers are always usable
3499 */
3500static bool guest_state_valid(struct kvm_vcpu *vcpu)
3501{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003502 if (enable_unrestricted_guest)
3503 return true;
3504
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003505 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003506 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003507 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3508 return false;
3509 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3510 return false;
3511 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3512 return false;
3513 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3514 return false;
3515 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3516 return false;
3517 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3518 return false;
3519 } else {
3520 /* protected mode guest state checks */
3521 if (!cs_ss_rpl_check(vcpu))
3522 return false;
3523 if (!code_segment_valid(vcpu))
3524 return false;
3525 if (!stack_segment_valid(vcpu))
3526 return false;
3527 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3528 return false;
3529 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3530 return false;
3531 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3532 return false;
3533 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3534 return false;
3535 if (!tr_valid(vcpu))
3536 return false;
3537 if (!ldtr_valid(vcpu))
3538 return false;
3539 }
3540 /* TODO:
3541 * - Add checks on RIP
3542 * - Add checks on RFLAGS
3543 */
3544
3545 return true;
3546}
3547
Mike Dayd77c26f2007-10-08 09:02:08 -04003548static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003549{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003550 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003551 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003552 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003554 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003555 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003556 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3557 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003558 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003559 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003560 r = kvm_write_guest_page(kvm, fn++, &data,
3561 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003562 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003563 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003564 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3565 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003566 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003567 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3568 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003569 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003570 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003571 r = kvm_write_guest_page(kvm, fn, &data,
3572 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3573 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003574out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003575 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003576 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003577}
3578
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003579static int init_rmode_identity_map(struct kvm *kvm)
3580{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003581 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Peter Xu2a5755b2020-01-09 09:57:14 -05003582 int i, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08003583 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003584 u32 tmp;
3585
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003586 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08003587 mutex_lock(&kvm->slots_lock);
3588
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003589 if (likely(kvm_vmx->ept_identity_pagetable_done))
Peter Xu2a5755b2020-01-09 09:57:14 -05003590 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003591
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003592 if (!kvm_vmx->ept_identity_map_addr)
3593 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
3594 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003595
David Hildenbrandd8a6e362017-08-24 20:51:34 +02003596 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003597 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08003598 if (r < 0)
Peter Xu2a5755b2020-01-09 09:57:14 -05003599 goto out;
Tang Chena255d472014-09-16 18:41:58 +08003600
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003601 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3602 if (r < 0)
3603 goto out;
3604 /* Set up identity-mapping pagetable for EPT in real mode */
3605 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3606 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3607 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3608 r = kvm_write_guest_page(kvm, identity_map_pfn,
3609 &tmp, i * sizeof(tmp), sizeof(tmp));
3610 if (r < 0)
3611 goto out;
3612 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07003613 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003614
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003615out:
Tang Chena255d472014-09-16 18:41:58 +08003616 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08003617 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003618}
3619
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620static void seg_setup(int seg)
3621{
Mathias Krause772e0312012-08-30 01:30:19 +02003622 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003623 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003624
3625 vmcs_write16(sf->selector, 0);
3626 vmcs_writel(sf->base, 0);
3627 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003628 ar = 0x93;
3629 if (seg == VCPU_SREG_CS)
3630 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003631
3632 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633}
3634
Sheng Yangf78e0e22007-10-29 09:40:42 +08003635static int alloc_apic_access_page(struct kvm *kvm)
3636{
Xiao Guangrong44841412012-09-07 14:14:20 +08003637 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003638 int r = 0;
3639
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003640 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08003641 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003642 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02003643 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
3644 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003645 if (r)
3646 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003647
Tang Chen73a6d942014-09-11 13:38:00 +08003648 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08003649 if (is_error_page(page)) {
3650 r = -EFAULT;
3651 goto out;
3652 }
3653
Tang Chenc24ae0d2014-09-24 15:57:58 +08003654 /*
3655 * Do not pin the page in memory, so that memory hot-unplug
3656 * is able to migrate it.
3657 */
3658 put_page(page);
3659 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003660out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003661 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003662 return r;
3663}
3664
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003665int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003666{
3667 int vpid;
3668
Avi Kivity919818a2009-03-23 18:01:29 +02003669 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08003670 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003671 spin_lock(&vmx_vpid_lock);
3672 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003673 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003674 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003675 else
3676 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003677 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003678 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08003679}
3680
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003681void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003682{
Wanpeng Li991e7a02015-09-16 17:30:05 +08003683 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003684 return;
3685 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08003686 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003687 spin_unlock(&vmx_vpid_lock);
3688}
3689
Yi Wang1e4329ee2018-11-08 11:22:21 +08003690static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003691 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003692{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003693 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003694
3695 if (!cpu_has_vmx_msr_bitmap())
3696 return;
3697
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003698 if (static_branch_unlikely(&enable_evmcs))
3699 evmcs_touch_msr_bitmap();
3700
Sheng Yang25c5f222008-03-28 13:18:56 +08003701 /*
3702 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3703 * have the write-low and read-high bitmap offsets the wrong way round.
3704 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3705 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003706 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003707 if (type & MSR_TYPE_R)
3708 /* read-low */
3709 __clear_bit(msr, msr_bitmap + 0x000 / f);
3710
3711 if (type & MSR_TYPE_W)
3712 /* write-low */
3713 __clear_bit(msr, msr_bitmap + 0x800 / f);
3714
Sheng Yang25c5f222008-03-28 13:18:56 +08003715 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3716 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003717 if (type & MSR_TYPE_R)
3718 /* read-high */
3719 __clear_bit(msr, msr_bitmap + 0x400 / f);
3720
3721 if (type & MSR_TYPE_W)
3722 /* write-high */
3723 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3724
3725 }
3726}
3727
Yi Wang1e4329ee2018-11-08 11:22:21 +08003728static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003729 u32 msr, int type)
3730{
3731 int f = sizeof(unsigned long);
3732
3733 if (!cpu_has_vmx_msr_bitmap())
3734 return;
3735
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02003736 if (static_branch_unlikely(&enable_evmcs))
3737 evmcs_touch_msr_bitmap();
3738
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003739 /*
3740 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3741 * have the write-low and read-high bitmap offsets the wrong way round.
3742 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3743 */
3744 if (msr <= 0x1fff) {
3745 if (type & MSR_TYPE_R)
3746 /* read-low */
3747 __set_bit(msr, msr_bitmap + 0x000 / f);
3748
3749 if (type & MSR_TYPE_W)
3750 /* write-low */
3751 __set_bit(msr, msr_bitmap + 0x800 / f);
3752
3753 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3754 msr &= 0x1fff;
3755 if (type & MSR_TYPE_R)
3756 /* read-high */
3757 __set_bit(msr, msr_bitmap + 0x400 / f);
3758
3759 if (type & MSR_TYPE_W)
3760 /* write-high */
3761 __set_bit(msr, msr_bitmap + 0xc00 / f);
3762
3763 }
3764}
3765
Yi Wang1e4329ee2018-11-08 11:22:21 +08003766static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003767 u32 msr, int type, bool value)
3768{
3769 if (value)
3770 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
3771 else
3772 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
3773}
3774
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003775static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02003776{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003777 u8 mode = 0;
3778
3779 if (cpu_has_secondary_exec_ctrls() &&
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07003780 (secondary_exec_controls_get(to_vmx(vcpu)) &
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003781 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
3782 mode |= MSR_BITMAP_MODE_X2APIC;
3783 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
3784 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
3785 }
3786
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003787 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08003788}
3789
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003790static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
3791 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08003792{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003793 int msr;
3794
3795 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
3796 unsigned word = msr / BITS_PER_LONG;
3797 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
3798 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08003799 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003800
3801 if (mode & MSR_BITMAP_MODE_X2APIC) {
3802 /*
3803 * TPR reads and writes can be virtualized even if virtual interrupt
3804 * delivery is not in use.
3805 */
3806 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
3807 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
3808 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
3809 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
3810 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
3811 }
3812 }
3813}
3814
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003815void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003816{
3817 struct vcpu_vmx *vmx = to_vmx(vcpu);
3818 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3819 u8 mode = vmx_msr_bitmap_mode(vcpu);
3820 u8 changed = mode ^ vmx->msr_bitmap_mode;
3821
3822 if (!changed)
3823 return;
3824
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003825 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
3826 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
3827
3828 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02003829}
3830
Chao Pengb08c2892018-10-24 16:05:15 +08003831void pt_update_intercept_for_msr(struct vcpu_vmx *vmx)
3832{
3833 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
3834 bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
3835 u32 i;
3836
3837 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_STATUS,
3838 MSR_TYPE_RW, flag);
3839 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_BASE,
3840 MSR_TYPE_RW, flag);
3841 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_MASK,
3842 MSR_TYPE_RW, flag);
3843 vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_CR3_MATCH,
3844 MSR_TYPE_RW, flag);
3845 for (i = 0; i < vmx->pt_desc.addr_range; i++) {
3846 vmx_set_intercept_for_msr(msr_bitmap,
3847 MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
3848 vmx_set_intercept_for_msr(msr_bitmap,
3849 MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
3850 }
3851}
3852
Liran Alone6c67d82018-09-04 10:56:52 +03003853static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
3854{
3855 struct vcpu_vmx *vmx = to_vmx(vcpu);
3856 void *vapic_page;
3857 u32 vppr;
3858 int rvi;
3859
3860 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
3861 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003862 WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
Liran Alone6c67d82018-09-04 10:56:52 +03003863 return false;
3864
Paolo Bonzini7e712682018-10-03 13:44:26 +02003865 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03003866
KarimAllah Ahmed96c66e82019-01-31 21:24:37 +01003867 vapic_page = vmx->nested.virtual_apic_map.hva;
Liran Alone6c67d82018-09-04 10:56:52 +03003868 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
Liran Alone6c67d82018-09-04 10:56:52 +03003869
3870 return ((rvi & 0xf0) > (vppr & 0xf0));
3871}
3872
Wincy Van06a55242017-04-28 13:13:59 +08003873static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
3874 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003875{
3876#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08003877 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
3878
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003879 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08003880 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003881 * The vector of interrupt to be delivered to vcpu had
3882 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08003883 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08003884 * Following cases will be reached in this block, and
3885 * we always send a notification event in all cases as
3886 * explained below.
3887 *
3888 * Case 1: vcpu keeps in non-root mode. Sending a
3889 * notification event posts the interrupt to vcpu.
3890 *
3891 * Case 2: vcpu exits to root mode and is still
3892 * runnable. PIR will be synced to vIRR before the
3893 * next vcpu entry. Sending a notification event in
3894 * this case has no effect, as vcpu is not in root
3895 * mode.
3896 *
3897 * Case 3: vcpu exits to root mode and is blocked.
3898 * vcpu_block() has already synced PIR to vIRR and
3899 * never blocks vcpu if vIRR is not cleared. Therefore,
3900 * a blocked vcpu here does not wait for any requested
3901 * interrupts in PIR, and sending a notification event
3902 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08003903 */
Feng Wu28b835d2015-09-18 22:29:54 +08003904
Wincy Van06a55242017-04-28 13:13:59 +08003905 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01003906 return true;
3907 }
3908#endif
3909 return false;
3910}
3911
Wincy Van705699a2015-02-03 23:58:17 +08003912static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
3913 int vector)
3914{
3915 struct vcpu_vmx *vmx = to_vmx(vcpu);
3916
3917 if (is_guest_mode(vcpu) &&
3918 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08003919 /*
3920 * If a posted intr is not recognized by hardware,
3921 * we will accomplish it in the next vmentry.
3922 */
3923 vmx->nested.pi_pending = true;
3924 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02003925 /* the PIR and ON have been set by L1. */
3926 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
3927 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08003928 return 0;
3929 }
3930 return -1;
3931}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932/*
Yang Zhanga20ed542013-04-11 19:25:15 +08003933 * Send interrupt to vcpu via posted interrupt way.
3934 * 1. If target vcpu is running(non-root mode), send posted interrupt
3935 * notification to vcpu and hardware will sync PIR to vIRR atomically.
3936 * 2. If target vcpu isn't running(root mode), kick it to pick up the
3937 * interrupt from PIR in next vmentry.
3938 */
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003939static int vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
Yang Zhanga20ed542013-04-11 19:25:15 +08003940{
3941 struct vcpu_vmx *vmx = to_vmx(vcpu);
3942 int r;
3943
Wincy Van705699a2015-02-03 23:58:17 +08003944 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
3945 if (!r)
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003946 return 0;
3947
3948 if (!vcpu->arch.apicv_active)
3949 return -1;
Wincy Van705699a2015-02-03 23:58:17 +08003950
Yang Zhanga20ed542013-04-11 19:25:15 +08003951 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003952 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003953
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003954 /* If a previous notification has sent the IPI, nothing to do. */
3955 if (pi_test_and_set_on(&vmx->pi_desc))
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003956 return 0;
Paolo Bonzinib95234c2016-12-19 13:57:33 +01003957
Wanpeng Li379a3c82020-04-28 14:23:27 +08003958 if (vcpu != kvm_get_running_vcpu() &&
3959 !kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08003960 kvm_vcpu_kick(vcpu);
Vitaly Kuznetsov91a5f412020-02-20 18:22:05 +01003961
3962 return 0;
Yang Zhanga20ed542013-04-11 19:25:15 +08003963}
3964
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003966 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3967 * will not change in the lifetime of the guest.
3968 * Note that host-state that does change is set elsewhere. E.g., host-state
3969 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3970 */
Sean Christopherson97b7ead2018-12-03 13:53:16 -08003971void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003972{
3973 u32 low32, high32;
3974 unsigned long tmpl;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003975 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003976
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07003977 cr0 = read_cr0();
3978 WARN_ON(cr0 & X86_CR0_TS);
3979 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003980
3981 /*
3982 * Save the most likely value for this task's CR3 in the VMCS.
3983 * We can't use __get_current_cr3_fast() because we're not atomic.
3984 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07003985 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07003986 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003987 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003988
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003989 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003990 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003991 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07003992 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07003993
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003994 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003995#ifdef CONFIG_X86_64
3996 /*
3997 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003998 * vmx_prepare_switch_to_host(), in case userspace uses
3999 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03004000 */
4001 vmcs_write16(HOST_DS_SELECTOR, 0);
4002 vmcs_write16(HOST_ES_SELECTOR, 0);
4003#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004004 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4005 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004006#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004007 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4008 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4009
Sean Christopherson23420802019-04-19 22:50:57 -07004010 vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004011
Sean Christopherson453eafb2018-12-20 12:25:17 -08004012 vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004013
4014 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4015 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4016 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4017 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4018
4019 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4020 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4021 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4022 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004023
Sean Christophersonc73da3f2018-12-03 13:53:00 -08004024 if (cpu_has_load_ia32_efer())
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07004025 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004026}
4027
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004028void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004029{
Sean Christophersonfa71e952020-07-02 21:04:22 -07004030 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_POSSIBLE_CR4_GUEST_BITS;
4031 if (!enable_ept)
4032 vmx->vcpu.arch.cr4_guest_owned_bits &= ~X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004033 if (is_guest_mode(&vmx->vcpu))
4034 vmx->vcpu.arch.cr4_guest_owned_bits &=
4035 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004036 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4037}
4038
Sean Christophersonc075c3e2019-05-07 12:17:53 -07004039u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
Yang Zhang01e439b2013-04-11 19:25:12 +08004040{
4041 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4042
Andrey Smetanind62caab2015-11-10 15:36:33 +03004043 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004044 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004045
4046 if (!enable_vnmi)
4047 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4048
Sean Christopherson804939e2019-05-07 12:18:05 -07004049 if (!enable_preemption_timer)
4050 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4051
Yang Zhang01e439b2013-04-11 19:25:12 +08004052 return pin_based_exec_ctrl;
4053}
4054
Andrey Smetanind62caab2015-11-10 15:36:33 +03004055static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4056{
4057 struct vcpu_vmx *vmx = to_vmx(vcpu);
4058
Sean Christophersonc5f2c762019-05-07 12:17:55 -07004059 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004060 if (cpu_has_secondary_exec_ctrls()) {
4061 if (kvm_vcpu_apicv_active(vcpu))
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004062 secondary_exec_controls_setbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004063 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4064 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4065 else
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07004066 secondary_exec_controls_clearbit(vmx,
Roman Kagan3ce424e2016-05-18 17:48:20 +03004067 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4068 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4069 }
4070
4071 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004072 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004073}
4074
Sean Christopherson89b0c9f2018-12-03 13:53:07 -08004075u32 vmx_exec_control(struct vcpu_vmx *vmx)
4076{
4077 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4078
4079 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4080 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4081
4082 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4083 exec_control &= ~CPU_BASED_TPR_SHADOW;
4084#ifdef CONFIG_X86_64
4085 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4086 CPU_BASED_CR8_LOAD_EXITING;
4087#endif
4088 }
4089 if (!enable_ept)
4090 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4091 CPU_BASED_CR3_LOAD_EXITING |
4092 CPU_BASED_INVLPG_EXITING;
4093 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4094 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4095 CPU_BASED_MONITOR_EXITING);
4096 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4097 exec_control &= ~CPU_BASED_HLT_EXITING;
4098 return exec_control;
4099}
4100
4101
Paolo Bonzini80154d72017-08-24 13:55:35 +02004102static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004103{
Paolo Bonzini80154d72017-08-24 13:55:35 +02004104 struct kvm_vcpu *vcpu = &vmx->vcpu;
4105
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004106 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004107
Sean Christopherson2ef76192020-03-02 15:56:22 -08004108 if (vmx_pt_mode_is_system())
Chao Pengf99e3da2018-10-24 16:05:10 +08004109 exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
Paolo Bonzini80154d72017-08-24 13:55:35 +02004110 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004111 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4112 if (vmx->vpid == 0)
4113 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4114 if (!enable_ept) {
4115 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4116 enable_unrestricted_guest = 0;
4117 }
4118 if (!enable_unrestricted_guest)
4119 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07004120 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004121 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02004122 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004123 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4124 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004125 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02004126
4127 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4128 * in vmx_set_cr4. */
4129 exec_control &= ~SECONDARY_EXEC_DESC;
4130
Abel Gordonabc4fc52013-04-18 14:35:25 +03004131 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4132 (handle_vmptrld).
4133 We can NOT enable shadow_vmcs here because we don't have yet
4134 a current VMCS12
4135 */
4136 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004137
4138 if (!enable_pml)
4139 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004140
Paolo Bonzini3db13482017-08-24 14:48:03 +02004141 if (vmx_xsaves_supported()) {
4142 /* Exposing XSAVES only when XSAVE is exposed */
4143 bool xsaves_enabled =
Sean Christopherson96be4e02019-12-10 14:44:15 -08004144 boot_cpu_has(X86_FEATURE_XSAVE) &&
Paolo Bonzini3db13482017-08-24 14:48:03 +02004145 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4146 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4147
Aaron Lewis72041602019-10-21 16:30:20 -07004148 vcpu->arch.xsaves_enabled = xsaves_enabled;
4149
Paolo Bonzini3db13482017-08-24 14:48:03 +02004150 if (!xsaves_enabled)
4151 exec_control &= ~SECONDARY_EXEC_XSAVES;
4152
4153 if (nested) {
4154 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004155 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02004156 SECONDARY_EXEC_XSAVES;
4157 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004158 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02004159 ~SECONDARY_EXEC_XSAVES;
4160 }
4161 }
4162
Sean Christophersona7a200e2020-03-02 15:56:58 -08004163 if (cpu_has_vmx_rdtscp()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004164 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
4165 if (!rdtscp_enabled)
4166 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4167
4168 if (nested) {
4169 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004170 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004171 SECONDARY_EXEC_RDTSCP;
4172 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004173 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004174 ~SECONDARY_EXEC_RDTSCP;
4175 }
4176 }
4177
Sean Christopherson5ffec6f2020-03-02 15:56:34 -08004178 if (cpu_has_vmx_invpcid()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004179 /* Exposing INVPCID only when PCID is exposed */
4180 bool invpcid_enabled =
4181 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
4182 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
4183
4184 if (!invpcid_enabled) {
4185 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
4186 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
4187 }
4188
4189 if (nested) {
4190 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004191 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004192 SECONDARY_EXEC_ENABLE_INVPCID;
4193 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004194 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02004195 ~SECONDARY_EXEC_ENABLE_INVPCID;
4196 }
4197 }
4198
Jim Mattson45ec3682017-08-23 16:32:04 -07004199 if (vmx_rdrand_supported()) {
4200 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
4201 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02004202 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004203
4204 if (nested) {
4205 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004206 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004207 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004208 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004209 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004210 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07004211 }
4212 }
4213
Jim Mattson75f4fc82017-08-23 16:32:03 -07004214 if (vmx_rdseed_supported()) {
4215 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
4216 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02004217 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004218
4219 if (nested) {
4220 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004221 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004222 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004223 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004224 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02004225 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07004226 }
4227 }
4228
Tao Xue69e72fa2019-07-16 14:55:49 +08004229 if (vmx_waitpkg_supported()) {
4230 bool waitpkg_enabled =
4231 guest_cpuid_has(vcpu, X86_FEATURE_WAITPKG);
4232
4233 if (!waitpkg_enabled)
4234 exec_control &= ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4235
4236 if (nested) {
4237 if (waitpkg_enabled)
4238 vmx->nested.msrs.secondary_ctls_high |=
4239 SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4240 else
4241 vmx->nested.msrs.secondary_ctls_high &=
4242 ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4243 }
4244 }
4245
Paolo Bonzini80154d72017-08-24 13:55:35 +02004246 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004247}
4248
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004249static void ept_set_mmio_spte_mask(void)
4250{
4251 /*
4252 * EPT Misconfigurations can be generated if the value of bits 2:0
4253 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004254 */
Paolo Bonzinie7581ca2020-05-19 05:04:49 -04004255 kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE, 0);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004256}
4257
Wanpeng Lif53cd632014-12-02 19:14:58 +08004258#define VMX_XSS_EXIT_BITMAP 0
Avi Kivity6aa8b732006-12-10 02:21:36 -08004259
Sean Christopherson944c3462018-12-03 13:53:09 -08004260/*
Xiaoyao Li1b842922019-10-20 17:11:01 +08004261 * Noting that the initialization of Guest-state Area of VMCS is in
4262 * vmx_vcpu_reset().
Sean Christopherson944c3462018-12-03 13:53:09 -08004263 */
Xiaoyao Li1b842922019-10-20 17:11:01 +08004264static void init_vmcs(struct vcpu_vmx *vmx)
Sean Christopherson944c3462018-12-03 13:53:09 -08004265{
Sean Christopherson944c3462018-12-03 13:53:09 -08004266 if (nested)
Xiaoyao Li1b842922019-10-20 17:11:01 +08004267 nested_vmx_set_vmcs_shadowing_bitmap();
Sean Christopherson944c3462018-12-03 13:53:09 -08004268
Sheng Yang25c5f222008-03-28 13:18:56 +08004269 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004270 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08004271
Avi Kivity6aa8b732006-12-10 02:21:36 -08004272 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4273
Avi Kivity6aa8b732006-12-10 02:21:36 -08004274 /* Control */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004275 pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004276
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004277 exec_controls_set(vmx, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004278
Dan Williamsdfa169b2016-06-02 11:17:24 -07004279 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02004280 vmx_compute_secondary_exec_control(vmx);
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004281 secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07004282 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004283
Andrey Smetanind62caab2015-11-10 15:36:33 +03004284 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004285 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4286 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4287 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4288 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4289
4290 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004291
Li RongQing0bcf2612015-12-03 13:29:34 +08004292 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004293 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004294 }
4295
Wanpeng Lib31c1142018-03-12 04:53:04 -07004296 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004297 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004298 vmx->ple_window = ple_window;
4299 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004300 }
4301
Xiao Guangrongc3707952011-07-12 03:28:04 +08004302 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4303 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004304 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4305
Avi Kivity9581d442010-10-19 16:46:55 +02004306 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4307 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004308 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004309 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4310 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004311
Bandan Das2a499e42017-08-03 15:54:41 -04004312 if (cpu_has_vmx_vmfunc())
4313 vmcs_write64(VM_FUNCTION_CONTROL, 0);
4314
Eddie Dong2cc51562007-05-21 07:28:09 +03004315 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4316 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004317 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03004318 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04004319 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004320
Radim Krčmář74545702015-04-27 15:11:25 +02004321 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4322 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004323
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004324 vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325
4326 /* 22.2.1, 20.8.1 */
Sean Christopherson3af80fe2019-05-07 12:18:00 -07004327 vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004328
Sean Christophersonfa71e952020-07-02 21:04:22 -07004329 vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4330 vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004331
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004332 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004333
Xiaoyao Li35fbe0d2019-10-20 17:10:58 +08004334 if (vmx->vpid != 0)
4335 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4336
Wanpeng Lif53cd632014-12-02 19:14:58 +08004337 if (vmx_xsaves_supported())
4338 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4339
Peter Feiner4e595162016-07-07 14:49:58 -07004340 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07004341 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
4342 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
4343 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07004344
4345 if (cpu_has_vmx_encls_vmexit())
4346 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Chao Peng2ef444f2018-10-24 16:05:12 +08004347
Sean Christopherson2ef76192020-03-02 15:56:22 -08004348 if (vmx_pt_mode_is_host_guest()) {
Chao Peng2ef444f2018-10-24 16:05:12 +08004349 memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
4350 /* Bit[6~0] are forced to 1, writes are ignored. */
4351 vmx->pt_desc.guest.output_mask = 0x7F;
4352 vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
4353 }
Paolo Bonzini8c4182b2020-07-10 17:48:10 +02004354
4355 /*
4356 * If EPT is enabled, #PF is only trapped if MAXPHYADDR is mismatched
4357 * between guest and host. In that case we only care about present
4358 * faults.
4359 */
4360 if (enable_ept) {
4361 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, PFERR_PRESENT_MASK);
4362 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, PFERR_PRESENT_MASK);
4363 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004364}
4365
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004366static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004367{
4368 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004369 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004370 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004371
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004372 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004373 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004374
Tao Xu6e3ba4a2019-07-16 14:55:50 +08004375 vmx->msr_ia32_umwait_control = 0;
4376
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004377 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Wanpeng Li95c06542019-09-05 14:26:28 +08004378 vmx->hv_deadline_tsc = -1;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004379 kvm_set_cr8(vcpu, 0);
4380
4381 if (!init_event) {
4382 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4383 MSR_IA32_APICBASE_ENABLE;
4384 if (kvm_vcpu_is_reset_bsp(vcpu))
4385 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4386 apic_base_msr.host_initiated = true;
4387 kvm_set_apic_base(vcpu, &apic_base_msr);
4388 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004389
Avi Kivity2fb92db2011-04-27 19:42:18 +03004390 vmx_segment_cache_clear(vmx);
4391
Avi Kivity5706be02008-08-20 15:07:31 +03004392 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004393 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004394 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004395
4396 seg_setup(VCPU_SREG_DS);
4397 seg_setup(VCPU_SREG_ES);
4398 seg_setup(VCPU_SREG_FS);
4399 seg_setup(VCPU_SREG_GS);
4400 seg_setup(VCPU_SREG_SS);
4401
4402 vmcs_write16(GUEST_TR_SELECTOR, 0);
4403 vmcs_writel(GUEST_TR_BASE, 0);
4404 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4405 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4406
4407 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4408 vmcs_writel(GUEST_LDTR_BASE, 0);
4409 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4410 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4411
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004412 if (!init_event) {
4413 vmcs_write32(GUEST_SYSENTER_CS, 0);
4414 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4415 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4416 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4417 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004418
Wanpeng Lic37c2872017-11-20 14:52:21 -08004419 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01004420 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004421
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004422 vmcs_writel(GUEST_GDTR_BASE, 0);
4423 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4424
4425 vmcs_writel(GUEST_IDTR_BASE, 0);
4426 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4427
Anthony Liguori443381a2010-12-06 10:53:38 -06004428 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004429 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01004430 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07004431 if (kvm_mpx_supported())
4432 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004433
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004434 setup_msrs(vmx);
4435
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4437
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004438 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004439 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004440 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004441 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004442 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004443 vmcs_write32(TPR_THRESHOLD, 0);
4444 }
4445
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004446 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004447
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004448 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004449 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06004450 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004451 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004452 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08004453
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004454 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004455
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004456 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004457 if (init_event)
4458 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004459}
4460
Jan Kiszkac9a79532014-03-07 20:03:15 +01004461static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004462{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08004463 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004464}
4465
Jan Kiszkac9a79532014-03-07 20:03:15 +01004466static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004467{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004468 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004469 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01004470 enable_irq_window(vcpu);
4471 return;
4472 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004473
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08004474 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004475}
4476
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004477static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004478{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004479 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004480 uint32_t intr;
4481 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004482
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004483 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004484
Avi Kivityfa89a812008-09-01 15:57:51 +03004485 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004486 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004487 int inc_eip = 0;
4488 if (vcpu->arch.interrupt.soft)
4489 inc_eip = vcpu->arch.event_exit_inst_len;
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004490 kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
Eddie Dong85f455f2007-07-06 12:20:49 +03004491 return;
4492 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004493 intr = irq | INTR_INFO_VALID_MASK;
4494 if (vcpu->arch.interrupt.soft) {
4495 intr |= INTR_TYPE_SOFT_INTR;
4496 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4497 vmx->vcpu.arch.event_exit_inst_len);
4498 } else
4499 intr |= INTR_TYPE_EXT_INTR;
4500 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004501
4502 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004503}
4504
Sheng Yangf08864b2008-05-15 18:23:25 +08004505static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4506{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004507 struct vcpu_vmx *vmx = to_vmx(vcpu);
4508
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004509 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004510 /*
4511 * Tracking the NMI-blocked state in software is built upon
4512 * finding the next open IRQ window. This, in turn, depends on
4513 * well-behaving guests: They have to keep IRQs disabled at
4514 * least as long as the NMI handler runs. Otherwise we may
4515 * cause NMI nesting, maybe breaking the guest. But as this is
4516 * highly unlikely, we can live with the residual risk.
4517 */
4518 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
4519 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4520 }
4521
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004522 ++vcpu->stat.nmi_injections;
4523 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004524
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004525 if (vmx->rmode.vm86_active) {
Sean Christopherson9497e1f2019-08-27 14:40:36 -07004526 kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004527 return;
4528 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08004529
Sheng Yangf08864b2008-05-15 18:23:25 +08004530 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4531 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07004532
4533 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004534}
4535
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004536bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004537{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004538 struct vcpu_vmx *vmx = to_vmx(vcpu);
4539 bool masked;
4540
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004541 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004542 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004543 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02004544 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02004545 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
4546 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4547 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004548}
4549
Sean Christopherson97b7ead2018-12-03 13:53:16 -08004550void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004551{
4552 struct vcpu_vmx *vmx = to_vmx(vcpu);
4553
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01004554 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004555 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
4556 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
4557 vmx->loaded_vmcs->vnmi_blocked_time = 0;
4558 }
4559 } else {
4560 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
4561 if (masked)
4562 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4563 GUEST_INTR_STATE_NMI);
4564 else
4565 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4566 GUEST_INTR_STATE_NMI);
4567 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004568}
4569
Sean Christopherson1b660b62020-04-22 19:25:44 -07004570bool vmx_nmi_blocked(struct kvm_vcpu *vcpu)
4571{
4572 if (is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
4573 return false;
4574
4575 if (!enable_vnmi && to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
4576 return true;
4577
4578 return (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4579 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
4580 GUEST_INTR_STATE_NMI));
4581}
4582
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004583static int vmx_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Jan Kiszka2505dc92013-04-14 12:12:47 +02004584{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004585 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004586 return -EBUSY;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004587
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004588 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
4589 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004590 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004591
Sean Christopherson1b660b62020-04-22 19:25:44 -07004592 return !vmx_nmi_blocked(vcpu);
4593}
Sean Christopherson429ab572020-04-22 19:25:42 -07004594
Sean Christopherson1b660b62020-04-22 19:25:44 -07004595bool vmx_interrupt_blocked(struct kvm_vcpu *vcpu)
4596{
4597 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Sean Christopherson88c604b2020-04-22 19:25:41 -07004598 return false;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004599
Sean Christopherson7ab0abd2020-04-22 19:25:50 -07004600 return !(vmx_get_rflags(vcpu) & X86_EFLAGS_IF) ||
Sean Christopherson1b660b62020-04-22 19:25:44 -07004601 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4602 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Jan Kiszka2505dc92013-04-14 12:12:47 +02004603}
4604
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004605static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Gleb Natapov78646122009-03-23 12:12:11 +02004606{
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004607 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004608 return -EBUSY;
Sean Christophersona1c77ab2020-03-02 22:27:35 -08004609
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004610 /*
4611 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
4612 * e.g. if the IRQ arrived asynchronously after checking nested events.
4613 */
4614 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004615 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004616
Sean Christopherson1b660b62020-04-22 19:25:44 -07004617 return !vmx_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02004618}
4619
Izik Eiduscbc94022007-10-25 00:29:55 +02004620static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4621{
4622 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02004623
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08004624 if (enable_unrestricted_guest)
4625 return 0;
4626
Peter Xu6a3c6232020-01-09 09:57:16 -05004627 mutex_lock(&kvm->slots_lock);
4628 ret = __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
4629 PAGE_SIZE * 3);
4630 mutex_unlock(&kvm->slots_lock);
4631
Izik Eiduscbc94022007-10-25 00:29:55 +02004632 if (ret)
4633 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004634 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004635 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004636}
4637
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004638static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
4639{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004640 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004641 return 0;
4642}
4643
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004644static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004645{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004646 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004647 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004648 /*
4649 * Update instruction length as we may reinject the exception
4650 * from user space while in guest debugging mode.
4651 */
4652 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4653 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004654 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004655 return false;
4656 /* fall through */
4657 case DB_VECTOR:
Miaohe Lina8cfbae2020-02-19 10:45:48 +08004658 return !(vcpu->guest_debug &
4659 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP));
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004660 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004661 case OF_VECTOR:
4662 case BR_VECTOR:
4663 case UD_VECTOR:
4664 case DF_VECTOR:
4665 case SS_VECTOR:
4666 case GP_VECTOR:
4667 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004668 return true;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004669 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004670 return false;
4671}
4672
4673static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4674 int vec, u32 err_code)
4675{
4676 /*
4677 * Instruction with address size override prefix opcode 0x67
4678 * Cause the #SS fault with 0 error code in VM86 mode.
4679 */
4680 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004681 if (kvm_emulate_instruction(vcpu, 0)) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004682 if (vcpu->arch.halt_request) {
4683 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06004684 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004685 }
4686 return 1;
4687 }
4688 return 0;
4689 }
4690
4691 /*
4692 * Forward all other exceptions that are valid in real mode.
4693 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4694 * the required debugging infrastructure rework.
4695 */
4696 kvm_queue_exception(vcpu, vec);
4697 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004698}
4699
Andi Kleena0861c02009-06-08 17:37:09 +08004700/*
4701 * Trigger machine check on the host. We assume all the MSRs are already set up
4702 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4703 * We pass a fake environment to the machine check handler because we want
4704 * the guest to be always treated like user space, no matter what context
4705 * it used internally.
4706 */
4707static void kvm_machine_check(void)
4708{
Uros Bizjakfb56baa2020-04-14 09:14:14 +02004709#if defined(CONFIG_X86_MCE)
Andi Kleena0861c02009-06-08 17:37:09 +08004710 struct pt_regs regs = {
4711 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4712 .flags = X86_EFLAGS_IF,
4713 };
4714
Thomas Gleixner8cd501c2020-02-25 23:33:23 +01004715 do_machine_check(&regs);
Andi Kleena0861c02009-06-08 17:37:09 +08004716#endif
4717}
4718
Avi Kivity851ba692009-08-24 11:10:17 +03004719static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004720{
Sean Christopherson95b5a482019-04-19 22:50:59 -07004721 /* handled by vmx_vcpu_run() */
Andi Kleena0861c02009-06-08 17:37:09 +08004722 return 1;
4723}
4724
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004725/*
4726 * If the host has split lock detection disabled, then #AC is
4727 * unconditionally injected into the guest, which is the pre split lock
4728 * detection behaviour.
4729 *
4730 * If the host has split lock detection enabled then #AC is
4731 * only injected into the guest when:
4732 * - Guest CPL == 3 (user mode)
4733 * - Guest has #AC detection enabled in CR0
4734 * - Guest EFLAGS has AC bit set
4735 */
4736static inline bool guest_inject_ac(struct kvm_vcpu *vcpu)
4737{
4738 if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
4739 return true;
4740
4741 return vmx_get_cpl(vcpu) == 3 && kvm_read_cr0_bits(vcpu, X86_CR0_AM) &&
4742 (kvm_get_rflags(vcpu) & X86_EFLAGS_AC);
4743}
4744
Sean Christopherson95b5a482019-04-19 22:50:59 -07004745static int handle_exception_nmi(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746{
Avi Kivity1155f762007-11-22 11:30:47 +02004747 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004748 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004749 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004750 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751 u32 vect_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752
Avi Kivity1155f762007-11-22 11:30:47 +02004753 vect_info = vmx->idt_vectoring_info;
Sean Christophersonf27ad732020-04-27 10:18:37 -07004754 intr_info = vmx_get_intr_info(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004755
Paolo Bonzini2ea72032019-06-06 14:57:25 +02004756 if (is_machine_check(intr_info) || is_nmi(intr_info))
Sean Christopherson95b5a482019-04-19 22:50:59 -07004757 return 1; /* handled by handle_exception_nmi_irqoff() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004758
Wanpeng Li082d06e2018-04-03 16:28:48 -07004759 if (is_invalid_opcode(intr_info))
4760 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004761
Avi Kivity6aa8b732006-12-10 02:21:36 -08004762 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004763 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004764 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004765
Liran Alon9e869482018-03-12 13:12:51 +02004766 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
4767 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christophersona6c6ed12019-08-27 14:40:30 -07004768
4769 /*
4770 * VMware backdoor emulation on #GP interception only handles
4771 * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
4772 * error code on #GP.
4773 */
4774 if (error_code) {
4775 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
4776 return 1;
4777 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004778 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon9e869482018-03-12 13:12:51 +02004779 }
4780
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004781 /*
4782 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4783 * MMIO, it is better to report an internal error.
4784 * See the comments in vmx_handle_exit.
4785 */
4786 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4787 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4788 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4789 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Jim Mattson1aa561b2020-06-03 16:56:21 -07004790 vcpu->run->internal.ndata = 4;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004791 vcpu->run->internal.data[0] = vect_info;
4792 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02004793 vcpu->run->internal.data[2] = error_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07004794 vcpu->run->internal.data[3] = vcpu->arch.last_vmentry_cpu;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004795 return 0;
4796 }
4797
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798 if (is_page_fault(intr_info)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07004799 cr2 = vmx_get_exit_qual(vcpu);
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02004800 if (enable_ept && !vcpu->arch.apf.host_apf_flags) {
4801 /*
4802 * EPT will cause page fault only if we need to
4803 * detect illegal GPAs.
4804 */
4805 kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
4806 return 1;
4807 } else
4808 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809 }
4810
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004811 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004812
4813 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4814 return handle_rmode_exception(vcpu, ex_no, error_code);
4815
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004816 switch (ex_no) {
4817 case DB_VECTOR:
Sean Christopherson5addc232020-04-15 13:34:53 -07004818 dr6 = vmx_get_exit_qual(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004819 if (!(vcpu->guest_debug &
4820 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Linus Torvalds32d43cd2018-03-20 12:16:59 -07004821 if (is_icebp(intr_info))
Sean Christopherson1957aa62019-08-27 14:40:39 -07004822 WARN_ON(!skip_emulated_instruction(vcpu));
Huw Daviesfd2a4452014-04-16 10:02:51 +01004823
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04004824 kvm_queue_exception_p(vcpu, DB_VECTOR, dr6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004825 return 1;
4826 }
Peter Xu13196632020-05-05 16:49:58 -04004827 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004828 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4829 /* fall through */
4830 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004831 /*
4832 * Update instruction length as we may reinject #BP from
4833 * user space while in guest debugging mode. Reading it for
4834 * #DB as well causes no harm, it is not used in that case.
4835 */
4836 vmx->vcpu.arch.event_exit_inst_len =
4837 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004838 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004839 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004840 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4841 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004842 break;
Xiaoyao Lie6f8b6c2020-04-10 13:54:02 +02004843 case AC_VECTOR:
4844 if (guest_inject_ac(vcpu)) {
4845 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
4846 return 1;
4847 }
4848
4849 /*
4850 * Handle split lock. Depending on detection mode this will
4851 * either warn and disable split lock detection for this
4852 * task or force SIGBUS on it.
4853 */
4854 if (handle_guest_split_lock(kvm_rip_read(vcpu)))
4855 return 1;
4856 fallthrough;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004857 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004858 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4859 kvm_run->ex.exception = ex_no;
4860 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 return 0;
4864}
4865
Andrea Arcangelif399e602019-11-04 17:59:58 -05004866static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004868 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 return 1;
4870}
4871
Avi Kivity851ba692009-08-24 11:10:17 +03004872static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004873{
Avi Kivity851ba692009-08-24 11:10:17 +03004874 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07004875 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08004876 return 0;
4877}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878
Avi Kivity851ba692009-08-24 11:10:17 +03004879static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880{
He, Qingbfdaab02007-09-12 14:18:28 +08004881 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08004882 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004883 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884
Sean Christopherson5addc232020-04-15 13:34:53 -07004885 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity039576c2007-03-20 12:46:50 +02004886 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004887
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004888 ++vcpu->stat.io_exits;
4889
Sean Christopherson432baf62018-03-08 08:57:26 -08004890 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004891 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004892
4893 port = exit_qualification >> 16;
4894 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08004895 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004896
Sean Christophersondca7f122018-03-08 08:57:27 -08004897 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004898}
4899
Ingo Molnar102d8322007-02-19 14:37:47 +02004900static void
4901vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4902{
4903 /*
4904 * Patch in the VMCALL instruction:
4905 */
4906 hypercall[0] = 0x0f;
4907 hypercall[1] = 0x01;
4908 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004909}
4910
Guo Chao0fa06072012-06-28 15:16:19 +08004911/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004912static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4913{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004914 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004915 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4916 unsigned long orig_val = val;
4917
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004918 /*
4919 * We get here when L2 changed cr0 in a way that did not change
4920 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004921 * but did change L0 shadowed bits. So we first calculate the
4922 * effective cr0 value that L1 would like to write into the
4923 * hardware. It consists of the L2-owned bits from the new
4924 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004925 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004926 val = (val & ~vmcs12->cr0_guest_host_mask) |
4927 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4928
David Matlack38991522016-11-29 18:14:08 -08004929 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004930 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004931
4932 if (kvm_set_cr0(vcpu, val))
4933 return 1;
4934 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004935 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004936 } else {
4937 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08004938 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004939 return 1;
David Matlack38991522016-11-29 18:14:08 -08004940
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004941 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004942 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004943}
4944
4945static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4946{
4947 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004948 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4949 unsigned long orig_val = val;
4950
4951 /* analogously to handle_set_cr0 */
4952 val = (val & ~vmcs12->cr4_guest_host_mask) |
4953 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4954 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004955 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004956 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004957 return 0;
4958 } else
4959 return kvm_set_cr4(vcpu, val);
4960}
4961
Paolo Bonzini0367f202016-07-12 10:44:55 +02004962static int handle_desc(struct kvm_vcpu *vcpu)
4963{
4964 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson60fc3d02019-08-27 14:40:38 -07004965 return kvm_emulate_instruction(vcpu, 0);
Paolo Bonzini0367f202016-07-12 10:44:55 +02004966}
4967
Avi Kivity851ba692009-08-24 11:10:17 +03004968static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004970 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971 int cr;
4972 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004973 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08004974 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975
Sean Christopherson5addc232020-04-15 13:34:53 -07004976 exit_qualification = vmx_get_exit_qual(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977 cr = exit_qualification & 15;
4978 reg = (exit_qualification >> 8) & 15;
4979 switch ((exit_qualification >> 4) & 3) {
4980 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03004981 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004982 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983 switch (cr) {
4984 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004985 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004986 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004987 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08004988 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03004989 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004990 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004991 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004992 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004993 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004994 case 8: {
4995 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03004996 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004997 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08004998 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004999 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005000 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005001 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005002 return ret;
5003 /*
5004 * TODO: we might be squashing a
5005 * KVM_GUESTDBG_SINGLESTEP-triggered
5006 * KVM_EXIT_DEBUG here.
5007 */
Avi Kivity851ba692009-08-24 11:10:17 +03005008 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005009 return 0;
5010 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005011 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005013 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005014 WARN_ONCE(1, "Guest should always own CR0.TS");
5015 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005016 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005017 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018 case 1: /*mov from cr*/
5019 switch (cr) {
5020 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08005021 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02005022 val = kvm_read_cr3(vcpu);
5023 kvm_register_write(vcpu, reg, val);
5024 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005025 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005026 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005027 val = kvm_get_cr8(vcpu);
5028 kvm_register_write(vcpu, reg, val);
5029 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005030 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005031 }
5032 break;
5033 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005034 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005035 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005036 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037
Kyle Huey6affcbe2016-11-29 12:40:40 -08005038 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005039 default:
5040 break;
5041 }
Avi Kivity851ba692009-08-24 11:10:17 +03005042 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005043 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044 (int)(exit_qualification >> 4) & 3, cr);
5045 return 0;
5046}
5047
Avi Kivity851ba692009-08-24 11:10:17 +03005048static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005049{
He, Qingbfdaab02007-09-12 14:18:28 +08005050 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005051 int dr, dr7, reg;
5052
Sean Christopherson5addc232020-04-15 13:34:53 -07005053 exit_qualification = vmx_get_exit_qual(vcpu);
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005054 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5055
5056 /* First, if DR does not exist, trigger UD */
5057 if (!kvm_require_dr(vcpu, dr))
5058 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005059
Jan Kiszkaf2483412010-01-20 18:20:20 +01005060 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005061 if (!kvm_require_cpl(vcpu, 0))
5062 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005063 dr7 = vmcs_readl(GUEST_DR7);
5064 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005065 /*
5066 * As the vm-exit takes precedence over the debug trap, we
5067 * need to emulate the latter, either for the host or the
5068 * guest debugging itself.
5069 */
5070 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Paolo Bonzini45981de2020-05-06 05:59:39 -04005071 vcpu->run->debug.arch.dr6 = DR6_BD | DR6_RTM | DR6_FIXED_1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005072 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005073 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005074 vcpu->run->debug.arch.exception = DB_VECTOR;
5075 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005076 return 0;
5077 } else {
Paolo Bonzini4d5523c2020-05-05 07:33:20 -04005078 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BD);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005079 return 1;
5080 }
5081 }
5082
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005083 if (vcpu->guest_debug == 0) {
Sean Christopherson2183f562019-05-07 12:17:56 -07005084 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005085
5086 /*
5087 * No more DR vmexits; force a reload of the debug registers
5088 * and reenter on this instruction. The next vmexit will
5089 * retrieve the full state of the debug registers.
5090 */
5091 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5092 return 1;
5093 }
5094
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005095 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5096 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005097 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005098
5099 if (kvm_get_dr(vcpu, dr, &val))
5100 return 1;
5101 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005102 } else
Nadav Amit57773922014-06-18 17:19:23 +03005103 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005104 return 1;
5105
Kyle Huey6affcbe2016-11-29 12:40:40 -08005106 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005107}
5108
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005109static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5110{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005111 get_debugreg(vcpu->arch.db[0], 0);
5112 get_debugreg(vcpu->arch.db[1], 1);
5113 get_debugreg(vcpu->arch.db[2], 2);
5114 get_debugreg(vcpu->arch.db[3], 3);
5115 get_debugreg(vcpu->arch.dr6, 6);
5116 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5117
5118 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Sean Christopherson2183f562019-05-07 12:17:56 -07005119 exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005120}
5121
Gleb Natapov020df072010-04-13 10:05:23 +03005122static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5123{
5124 vmcs_writel(GUEST_DR7, val);
5125}
5126
Avi Kivity851ba692009-08-24 11:10:17 +03005127static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005128{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01005129 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005130 return 1;
5131}
5132
Avi Kivity851ba692009-08-24 11:10:17 +03005133static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005134{
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005135 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005136
Avi Kivity3842d132010-07-27 12:30:24 +03005137 kvm_make_request(KVM_REQ_EVENT, vcpu);
5138
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005139 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005140 return 1;
5141}
5142
Avi Kivity851ba692009-08-24 11:10:17 +03005143static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005144{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005145 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005146}
5147
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005148static int handle_invd(struct kvm_vcpu *vcpu)
5149{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005150 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005151}
5152
Avi Kivity851ba692009-08-24 11:10:17 +03005153static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005154{
Sean Christopherson5addc232020-04-15 13:34:53 -07005155 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005156
5157 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005158 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005159}
5160
Avi Kivityfee84b02011-11-10 14:57:25 +02005161static int handle_rdpmc(struct kvm_vcpu *vcpu)
5162{
5163 int err;
5164
5165 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005166 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02005167}
5168
Avi Kivity851ba692009-08-24 11:10:17 +03005169static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005170{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005171 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005172}
5173
Dexuan Cui2acf9232010-06-10 11:27:12 +08005174static int handle_xsetbv(struct kvm_vcpu *vcpu)
5175{
5176 u64 new_bv = kvm_read_edx_eax(vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07005177 u32 index = kvm_rcx_read(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005178
5179 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005180 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08005181 return 1;
5182}
5183
Avi Kivity851ba692009-08-24 11:10:17 +03005184static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005185{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005186 if (likely(fasteoi)) {
Sean Christopherson5addc232020-04-15 13:34:53 -07005187 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005188 int access_type, offset;
5189
5190 access_type = exit_qualification & APIC_ACCESS_TYPE;
5191 offset = exit_qualification & APIC_ACCESS_OFFSET;
5192 /*
5193 * Sane guest uses MOV to write EOI, with written value
5194 * not cared. So make a short-circuit here by avoiding
5195 * heavy instruction emulation.
5196 */
5197 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5198 (offset == APIC_EOI)) {
5199 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005200 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03005201 }
5202 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005203 return kvm_emulate_instruction(vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005204}
5205
Yang Zhangc7c9c562013-01-25 10:18:51 +08005206static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5207{
Sean Christopherson5addc232020-04-15 13:34:53 -07005208 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhangc7c9c562013-01-25 10:18:51 +08005209 int vector = exit_qualification & 0xff;
5210
5211 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5212 kvm_apic_set_eoi_accelerated(vcpu, vector);
5213 return 1;
5214}
5215
Yang Zhang83d4c282013-01-25 10:18:49 +08005216static int handle_apic_write(struct kvm_vcpu *vcpu)
5217{
Sean Christopherson5addc232020-04-15 13:34:53 -07005218 unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
Yang Zhang83d4c282013-01-25 10:18:49 +08005219 u32 offset = exit_qualification & 0xfff;
5220
5221 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5222 kvm_apic_write_nodecode(vcpu, offset);
5223 return 1;
5224}
5225
Avi Kivity851ba692009-08-24 11:10:17 +03005226static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005227{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005228 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005229 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005230 bool has_error_code = false;
5231 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005232 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005233 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005234
5235 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005236 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005237 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005238
Sean Christopherson5addc232020-04-15 13:34:53 -07005239 exit_qualification = vmx_get_exit_qual(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005240
5241 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005242 if (reason == TASK_SWITCH_GATE && idt_v) {
5243 switch (type) {
5244 case INTR_TYPE_NMI_INTR:
5245 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005246 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005247 break;
5248 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005249 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005250 kvm_clear_interrupt_queue(vcpu);
5251 break;
5252 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005253 if (vmx->idt_vectoring_info &
5254 VECTORING_INFO_DELIVER_CODE_MASK) {
5255 has_error_code = true;
5256 error_code =
5257 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5258 }
5259 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005260 case INTR_TYPE_SOFT_EXCEPTION:
5261 kvm_clear_exception_queue(vcpu);
5262 break;
5263 default:
5264 break;
5265 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005266 }
Izik Eidus37817f22008-03-24 23:14:53 +02005267 tss_selector = exit_qualification;
5268
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005269 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5270 type != INTR_TYPE_EXT_INTR &&
5271 type != INTR_TYPE_NMI_INTR))
Sean Christopherson1957aa62019-08-27 14:40:39 -07005272 WARN_ON(!skip_emulated_instruction(vcpu));
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005273
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005274 /*
5275 * TODO: What about debug traps on tss switch?
5276 * Are we supposed to inject them and update dr6?
5277 */
Sean Christopherson10517782019-08-27 14:40:35 -07005278 return kvm_task_switch(vcpu, tss_selector,
5279 type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005280 reason, has_error_code, error_code);
Izik Eidus37817f22008-03-24 23:14:53 +02005281}
5282
Avi Kivity851ba692009-08-24 11:10:17 +03005283static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005284{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005285 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005286 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01005287 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005288
Sean Christopherson5addc232020-04-15 13:34:53 -07005289 exit_qualification = vmx_get_exit_qual(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005290
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005291 /*
5292 * EPT violation happened while executing iret from NMI,
5293 * "blocked by NMI" bit has to be set before next VM entry.
5294 * There are errata that may cause this bit to not be set:
5295 * AAK134, BY25.
5296 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005297 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005298 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005299 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005300 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5301
Sheng Yang14394422008-04-28 12:24:45 +08005302 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005303 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005304
Junaid Shahid27959a42016-12-06 16:46:10 -08005305 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005306 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08005307 ? PFERR_USER_MASK : 0;
5308 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005309 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08005310 ? PFERR_WRITE_MASK : 0;
5311 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08005312 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08005313 ? PFERR_FETCH_MASK : 0;
5314 /* ept page table entry is present? */
5315 error_code |= (exit_qualification &
5316 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
5317 EPT_VIOLATION_EXECUTABLE))
5318 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005319
Paolo Bonzinieebed242016-11-28 14:39:58 +01005320 error_code |= (exit_qualification & 0x100) != 0 ?
5321 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005322
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005323 vcpu->arch.exit_qualification = exit_qualification;
Mohammed Gamal1dbf5d682020-07-10 17:48:09 +02005324
5325 /*
5326 * Check that the GPA doesn't exceed physical memory limits, as that is
5327 * a guest page fault. We have to emulate the instruction here, because
5328 * if the illegal address is that of a paging structure, then
5329 * EPT_VIOLATION_ACC_WRITE bit is set. Alternatively, if supported we
5330 * would also use advanced VM-exit information for EPT violations to
5331 * reconstruct the page fault error code.
5332 */
5333 if (unlikely(kvm_mmu_is_illegal_gpa(vcpu, gpa)))
5334 return kvm_emulate_instruction(vcpu, 0);
5335
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005336 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005337}
5338
Avi Kivity851ba692009-08-24 11:10:17 +03005339static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005340{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005341 gpa_t gpa;
5342
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005343 /*
5344 * A nested guest cannot optimize MMIO vmexits, because we have an
5345 * nGPA here instead of the required GPA.
5346 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005347 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02005348 if (!is_guest_mode(vcpu) &&
5349 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08005350 trace_kvm_fast_mmio(gpa);
Sean Christopherson1957aa62019-08-27 14:40:39 -07005351 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005352 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005353
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07005354 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005355}
5356
Avi Kivity851ba692009-08-24 11:10:17 +03005357static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005358{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005359 WARN_ON_ONCE(!enable_vnmi);
Xiaoyao Li4e2a0bc2019-12-06 16:45:25 +08005360 exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
Sheng Yangf08864b2008-05-15 18:23:25 +08005361 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005362 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005363
5364 return 1;
5365}
5366
Mohammed Gamal80ced182009-09-01 12:48:18 +02005367static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005368{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005369 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity49e9d552010-09-19 14:34:08 +02005370 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005371 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005372
Sean Christopherson2183f562019-05-07 12:17:56 -07005373 intr_window_requested = exec_controls_get(vmx) &
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005374 CPU_BASED_INTR_WINDOW_EXITING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005375
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005376 while (vmx->emulation_required && count-- != 0) {
Sean Christophersondb438592020-04-22 19:25:48 -07005377 if (intr_window_requested && !vmx_interrupt_blocked(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005378 return handle_interrupt_window(&vmx->vcpu);
5379
Radim Krčmář72875d82017-04-26 22:32:19 +02005380 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005381 return 1;
5382
Sean Christopherson60fc3d02019-08-27 14:40:38 -07005383 if (!kvm_emulate_instruction(vcpu, 0))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005384 return 0;
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005385
Sean Christophersonadd5ff72018-03-23 09:34:00 -07005386 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
Sean Christopherson8fff2712019-08-27 14:40:37 -07005387 vcpu->arch.exception.pending) {
5388 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5389 vcpu->run->internal.suberror =
5390 KVM_INTERNAL_ERROR_EMULATION;
5391 vcpu->run->internal.ndata = 0;
5392 return 0;
5393 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005394
Gleb Natapov8d76c492013-05-08 18:38:44 +03005395 if (vcpu->arch.halt_request) {
5396 vcpu->arch.halt_request = 0;
Sean Christopherson8fff2712019-08-27 14:40:37 -07005397 return kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005398 }
5399
Sean Christopherson8fff2712019-08-27 14:40:37 -07005400 /*
5401 * Note, return 1 and not 0, vcpu_run() is responsible for
5402 * morphing the pending signal into the proper return code.
5403 */
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005404 if (signal_pending(current))
Sean Christopherson8fff2712019-08-27 14:40:37 -07005405 return 1;
5406
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005407 if (need_resched())
5408 schedule();
5409 }
5410
Sean Christopherson8fff2712019-08-27 14:40:37 -07005411 return 1;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005412}
5413
5414static void grow_ple_window(struct kvm_vcpu *vcpu)
5415{
5416 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005417 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005418
Babu Mogerc8e88712018-03-16 16:37:24 -04005419 vmx->ple_window = __grow_ple_window(old, ple_window,
5420 ple_window_grow,
5421 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005422
Peter Xu4f75bcc2019-09-06 10:17:22 +08005423 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005424 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005425 trace_kvm_ple_window_update(vcpu->vcpu_id,
5426 vmx->ple_window, old);
5427 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005428}
5429
5430static void shrink_ple_window(struct kvm_vcpu *vcpu)
5431{
5432 struct vcpu_vmx *vmx = to_vmx(vcpu);
Peter Xuc5c5d6f2019-09-06 10:17:21 +08005433 unsigned int old = vmx->ple_window;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005434
Babu Mogerc8e88712018-03-16 16:37:24 -04005435 vmx->ple_window = __shrink_ple_window(old, ple_window,
5436 ple_window_shrink,
5437 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005438
Peter Xu4f75bcc2019-09-06 10:17:22 +08005439 if (vmx->ple_window != old) {
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005440 vmx->ple_window_dirty = true;
Peter Xu4f75bcc2019-09-06 10:17:22 +08005441 trace_kvm_ple_window_update(vcpu->vcpu_id,
5442 vmx->ple_window, old);
5443 }
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005444}
5445
5446/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08005447 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
5448 */
5449static void wakeup_handler(void)
5450{
5451 struct kvm_vcpu *vcpu;
5452 int cpu = smp_processor_id();
5453
5454 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
5455 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
5456 blocked_vcpu_list) {
5457 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
5458
5459 if (pi_test_on(pi_desc) == 1)
5460 kvm_vcpu_kick(vcpu);
5461 }
5462 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
5463}
5464
Peng Haoe01bca22018-04-07 05:47:32 +08005465static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005466{
5467 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
5468 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
5469 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
5470 0ull, VMX_EPT_EXECUTABLE_MASK,
5471 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05005472 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005473
5474 ept_set_mmio_spte_mask();
Junaid Shahidf160c7b2016-12-06 16:46:16 -08005475}
5476
Avi Kivity6aa8b732006-12-10 02:21:36 -08005477/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005478 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5479 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5480 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005481static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005482{
Wanpeng Lib31c1142018-03-12 04:53:04 -07005483 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005484 grow_ple_window(vcpu);
5485
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08005486 /*
5487 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
5488 * VM-execution control is ignored if CPL > 0. OTOH, KVM
5489 * never set PAUSE_EXITING and just set PLE if supported,
5490 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
5491 */
5492 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005493 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005494}
5495
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005496static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08005497{
Kyle Huey6affcbe2016-11-29 12:40:40 -08005498 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08005499}
5500
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005501static int handle_mwait(struct kvm_vcpu *vcpu)
5502{
5503 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
5504 return handle_nop(vcpu);
5505}
5506
Jim Mattson45ec3682017-08-23 16:32:04 -07005507static int handle_invalid_op(struct kvm_vcpu *vcpu)
5508{
5509 kvm_queue_exception(vcpu, UD_VECTOR);
5510 return 1;
5511}
5512
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005513static int handle_monitor_trap(struct kvm_vcpu *vcpu)
5514{
5515 return 1;
5516}
5517
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005518static int handle_monitor(struct kvm_vcpu *vcpu)
5519{
5520 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5521 return handle_nop(vcpu);
5522}
5523
Junaid Shahideb4b2482018-06-27 14:59:14 -07005524static int handle_invpcid(struct kvm_vcpu *vcpu)
5525{
5526 u32 vmx_instruction_info;
5527 unsigned long type;
5528 bool pcid_enabled;
5529 gva_t gva;
5530 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07005531 unsigned i;
5532 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005533 struct {
5534 u64 pcid;
5535 u64 gla;
5536 } operand;
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005537 int r;
Junaid Shahideb4b2482018-06-27 14:59:14 -07005538
5539 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
5540 kvm_queue_exception(vcpu, UD_VECTOR);
5541 return 1;
5542 }
5543
5544 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5545 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
5546
5547 if (type > 3) {
5548 kvm_inject_gp(vcpu, 0);
5549 return 1;
5550 }
5551
5552 /* According to the Intel instruction reference, the memory operand
5553 * is read even if it isn't needed (e.g., for type==all)
5554 */
Sean Christopherson5addc232020-04-15 13:34:53 -07005555 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu),
Eugene Korenevskyfdb28612019-06-06 00:19:16 +03005556 vmx_instruction_info, false,
5557 sizeof(operand), &gva))
Junaid Shahideb4b2482018-06-27 14:59:14 -07005558 return 1;
5559
Vitaly Kuznetsov7a35e512020-06-05 13:59:05 +02005560 r = kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e);
5561 if (r != X86EMUL_CONTINUE)
5562 return vmx_handle_memory_failure(vcpu, r, &e);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005563
5564 if (operand.pcid >> 12 != 0) {
5565 kvm_inject_gp(vcpu, 0);
5566 return 1;
5567 }
5568
5569 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
5570
5571 switch (type) {
5572 case INVPCID_TYPE_INDIV_ADDR:
5573 if ((!pcid_enabled && (operand.pcid != 0)) ||
5574 is_noncanonical_address(operand.gla, vcpu)) {
5575 kvm_inject_gp(vcpu, 0);
5576 return 1;
5577 }
5578 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
5579 return kvm_skip_emulated_instruction(vcpu);
5580
5581 case INVPCID_TYPE_SINGLE_CTXT:
5582 if (!pcid_enabled && (operand.pcid != 0)) {
5583 kvm_inject_gp(vcpu, 0);
5584 return 1;
5585 }
5586
5587 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
5588 kvm_mmu_sync_roots(vcpu);
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07005589 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005590 }
5591
Junaid Shahidb94742c2018-06-27 14:59:20 -07005592 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Sean Christophersonbe01e8e2020-03-20 14:28:32 -07005593 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].pgd)
Junaid Shahidb94742c2018-06-27 14:59:20 -07005594 == operand.pcid)
5595 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07005596
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02005597 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07005598 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07005599 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07005600 * given PCID, then nothing needs to be done here because a
5601 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07005602 */
5603
5604 return kvm_skip_emulated_instruction(vcpu);
5605
5606 case INVPCID_TYPE_ALL_NON_GLOBAL:
5607 /*
5608 * Currently, KVM doesn't mark global entries in the shadow
5609 * page tables, so a non-global flush just degenerates to a
5610 * global flush. If needed, we could optimize this later by
5611 * keeping track of global entries in shadow page tables.
5612 */
5613
5614 /* fall-through */
5615 case INVPCID_TYPE_ALL_INCL_GLOBAL:
5616 kvm_mmu_unload(vcpu);
5617 return kvm_skip_emulated_instruction(vcpu);
5618
5619 default:
5620 BUG(); /* We have already checked above that type <= 3 */
5621 }
5622}
5623
Kai Huang843e4332015-01-28 10:54:28 +08005624static int handle_pml_full(struct kvm_vcpu *vcpu)
5625{
5626 unsigned long exit_qualification;
5627
5628 trace_kvm_pml_full(vcpu->vcpu_id);
5629
Sean Christopherson5addc232020-04-15 13:34:53 -07005630 exit_qualification = vmx_get_exit_qual(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005631
5632 /*
5633 * PML buffer FULL happened while executing iret from NMI,
5634 * "blocked by NMI" bit has to be set before next VM entry.
5635 */
5636 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01005637 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08005638 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5639 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5640 GUEST_INTR_STATE_NMI);
5641
5642 /*
5643 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
5644 * here.., and there's no userspace involvement needed for PML.
5645 */
5646 return 1;
5647}
5648
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005649static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07005650{
Sean Christopherson804939e2019-05-07 12:18:05 -07005651 struct vcpu_vmx *vmx = to_vmx(vcpu);
5652
5653 if (!vmx->req_immediate_exit &&
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005654 !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) {
Sean Christophersond264ee02018-08-27 15:21:12 -07005655 kvm_lapic_expired_hv_timer(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005656 return EXIT_FASTPATH_REENTER_GUEST;
5657 }
Sean Christopherson804939e2019-05-07 12:18:05 -07005658
Wanpeng Li26efe2f2020-05-06 11:44:01 -04005659 return EXIT_FASTPATH_NONE;
5660}
5661
5662static int handle_preemption_timer(struct kvm_vcpu *vcpu)
5663{
5664 handle_fastpath_preemption_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07005665 return 1;
5666}
5667
Sean Christophersone4027cf2018-12-03 13:53:12 -08005668/*
5669 * When nested=0, all VMX instruction VM Exits filter here. The handlers
5670 * are overwritten by nested_vmx_setup() when nested=1.
5671 */
5672static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
5673{
5674 kvm_queue_exception(vcpu, UD_VECTOR);
5675 return 1;
5676}
5677
Sean Christopherson0b665d32018-08-14 09:33:34 -07005678static int handle_encls(struct kvm_vcpu *vcpu)
5679{
5680 /*
5681 * SGX virtualization is not yet supported. There is no software
5682 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
5683 * to prevent the guest from executing ENCLS.
5684 */
5685 kvm_queue_exception(vcpu, UD_VECTOR);
5686 return 1;
5687}
5688
Nadav Har'El0140cae2011-05-25 23:06:28 +03005689/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005690 * The exit handlers return 1 if the exit was handled fully and guest execution
5691 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5692 * to be done to userspace and return 0.
5693 */
Sean Christophersone4027cf2018-12-03 13:53:12 -08005694static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Sean Christopherson95b5a482019-04-19 22:50:59 -07005695 [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005696 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005697 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005698 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005699 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005700 [EXIT_REASON_CR_ACCESS] = handle_cr,
5701 [EXIT_REASON_DR_ACCESS] = handle_dr,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005702 [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
5703 [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
5704 [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
Xiaoyao Li9dadc2f2019-12-06 16:45:24 +08005705 [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
Andrea Arcangelif399e602019-11-04 17:59:58 -05005706 [EXIT_REASON_HLT] = kvm_emulate_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005707 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005708 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005709 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005710 [EXIT_REASON_VMCALL] = handle_vmcall,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005711 [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
5712 [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
5713 [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
5714 [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
5715 [EXIT_REASON_VMREAD] = handle_vmx_instruction,
5716 [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
5717 [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
5718 [EXIT_REASON_VMOFF] = handle_vmx_instruction,
5719 [EXIT_REASON_VMON] = handle_vmx_instruction,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005720 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5721 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005722 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005723 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005724 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005725 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005726 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005727 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005728 [EXIT_REASON_GDTR_IDTR] = handle_desc,
5729 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005730 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5731 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005732 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005733 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03005734 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005735 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005736 [EXIT_REASON_INVEPT] = handle_vmx_instruction,
5737 [EXIT_REASON_INVVPID] = handle_vmx_instruction,
Jim Mattson45ec3682017-08-23 16:32:04 -07005738 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07005739 [EXIT_REASON_RDSEED] = handle_invalid_op,
Kai Huang843e4332015-01-28 10:54:28 +08005740 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07005741 [EXIT_REASON_INVPCID] = handle_invpcid,
Sean Christophersone4027cf2018-12-03 13:53:12 -08005742 [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
Yunhong Jiang64672c92016-06-13 14:19:59 -07005743 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07005744 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005745};
5746
5747static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005748 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005749
Avi Kivity586f9602010-11-18 13:09:54 +02005750static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
5751{
Sean Christopherson5addc232020-04-15 13:34:53 -07005752 *info1 = vmx_get_exit_qual(vcpu);
Sean Christopherson87915852020-04-15 13:34:54 -07005753 *info2 = vmx_get_intr_info(vcpu);
Avi Kivity586f9602010-11-18 13:09:54 +02005754}
5755
Kai Huanga3eaa862015-11-04 13:46:05 +08005756static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08005757{
Kai Huanga3eaa862015-11-04 13:46:05 +08005758 if (vmx->pml_pg) {
5759 __free_page(vmx->pml_pg);
5760 vmx->pml_pg = NULL;
5761 }
Kai Huang843e4332015-01-28 10:54:28 +08005762}
5763
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005764static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08005765{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005766 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005767 u64 *pml_buf;
5768 u16 pml_idx;
5769
5770 pml_idx = vmcs_read16(GUEST_PML_INDEX);
5771
5772 /* Do nothing if PML buffer is empty */
5773 if (pml_idx == (PML_ENTITY_NUM - 1))
5774 return;
5775
5776 /* PML index always points to next available PML buffer entity */
5777 if (pml_idx >= PML_ENTITY_NUM)
5778 pml_idx = 0;
5779 else
5780 pml_idx++;
5781
5782 pml_buf = page_address(vmx->pml_pg);
5783 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
5784 u64 gpa;
5785
5786 gpa = pml_buf[pml_idx];
5787 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005788 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08005789 }
5790
5791 /* reset PML index */
5792 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5793}
5794
5795/*
5796 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
5797 * Called before reporting dirty_bitmap to userspace.
5798 */
5799static void kvm_flush_pml_buffers(struct kvm *kvm)
5800{
5801 int i;
5802 struct kvm_vcpu *vcpu;
5803 /*
5804 * We only need to kick vcpu out of guest mode here, as PML buffer
5805 * is flushed at beginning of all VMEXITs, and it's obvious that only
5806 * vcpus running in guest are possible to have unflushed GPAs in PML
5807 * buffer.
5808 */
5809 kvm_for_each_vcpu(i, vcpu, kvm)
5810 kvm_vcpu_kick(vcpu);
5811}
5812
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005813static void vmx_dump_sel(char *name, uint32_t sel)
5814{
5815 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05005816 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005817 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
5818 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
5819 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
5820}
5821
5822static void vmx_dump_dtsel(char *name, uint32_t limit)
5823{
5824 pr_err("%s limit=0x%08x, base=0x%016lx\n",
5825 name, vmcs_read32(limit),
5826 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
5827}
5828
Paolo Bonzini69090812019-04-15 15:16:17 +02005829void dump_vmcs(void)
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005830{
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005831 u32 vmentry_ctl, vmexit_ctl;
5832 u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
5833 unsigned long cr4;
5834 u64 efer;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005835
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02005836 if (!dump_invalid_vmcs) {
5837 pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
5838 return;
5839 }
5840
5841 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
5842 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
5843 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5844 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
5845 cr4 = vmcs_readl(GUEST_CR4);
5846 efer = vmcs_read64(GUEST_IA32_EFER);
5847 secondary_exec_control = 0;
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005848 if (cpu_has_secondary_exec_ctrls())
5849 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5850
5851 pr_err("*** Guest State ***\n");
5852 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5853 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
5854 vmcs_readl(CR0_GUEST_HOST_MASK));
5855 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
5856 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
5857 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
5858 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
5859 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
5860 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005861 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
5862 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
5863 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
5864 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005865 }
5866 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
5867 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
5868 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
5869 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
5870 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5871 vmcs_readl(GUEST_SYSENTER_ESP),
5872 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
5873 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
5874 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
5875 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
5876 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
5877 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
5878 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
5879 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
5880 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
5881 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
5882 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
5883 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
5884 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005885 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5886 efer, vmcs_read64(GUEST_IA32_PAT));
5887 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
5888 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005889 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005890 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005891 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005892 pr_err("PerfGlobCtl = 0x%016llx\n",
5893 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005894 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005895 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005896 pr_err("Interruptibility = %08x ActivityState = %08x\n",
5897 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
5898 vmcs_read32(GUEST_ACTIVITY_STATE));
5899 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
5900 pr_err("InterruptStatus = %04x\n",
5901 vmcs_read16(GUEST_INTR_STATUS));
5902
5903 pr_err("*** Host State ***\n");
5904 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
5905 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
5906 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
5907 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
5908 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
5909 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
5910 vmcs_read16(HOST_TR_SELECTOR));
5911 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
5912 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
5913 vmcs_readl(HOST_TR_BASE));
5914 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
5915 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
5916 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
5917 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
5918 vmcs_readl(HOST_CR4));
5919 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
5920 vmcs_readl(HOST_IA32_SYSENTER_ESP),
5921 vmcs_read32(HOST_IA32_SYSENTER_CS),
5922 vmcs_readl(HOST_IA32_SYSENTER_EIP));
5923 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005924 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
5925 vmcs_read64(HOST_IA32_EFER),
5926 vmcs_read64(HOST_IA32_PAT));
Sean Christophersonc73da3f2018-12-03 13:53:00 -08005927 if (cpu_has_load_perf_global_ctrl() &&
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01005928 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005929 pr_err("PerfGlobCtl = 0x%016llx\n",
5930 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005931
5932 pr_err("*** Control State ***\n");
5933 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
5934 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
5935 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
5936 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
5937 vmcs_read32(EXCEPTION_BITMAP),
5938 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
5939 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
5940 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
5941 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
5942 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
5943 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
5944 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
5945 vmcs_read32(VM_EXIT_INTR_INFO),
5946 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
5947 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
5948 pr_err(" reason=%08x qualification=%016lx\n",
5949 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
5950 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
5951 vmcs_read32(IDT_VECTORING_INFO_FIELD),
5952 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005953 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08005954 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005955 pr_err("TSC Multiplier = 0x%016llx\n",
5956 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005957 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
5958 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
5959 u16 status = vmcs_read16(GUEST_INTR_STATUS);
5960 pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
5961 }
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005962 pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005963 if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
5964 pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
Dan Carpenterd6a85c32019-04-24 13:15:08 +03005965 pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
Paolo Bonzini9d609642019-04-15 15:14:32 +02005966 }
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005967 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
5968 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
5969 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01005970 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02005971 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
5972 pr_err("PLE Gap=%08x Window=%08x\n",
5973 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
5974 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
5975 pr_err("Virtual processor ID = 0x%04x\n",
5976 vmcs_read16(VIRTUAL_PROCESSOR_ID));
5977}
5978
Avi Kivity6aa8b732006-12-10 02:21:36 -08005979/*
5980 * The guest has exited. See if we can fix it or if we need userspace
5981 * assistance.
5982 */
Wanpeng Li404d5d72020-04-28 14:23:25 +08005983static int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005984{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005985 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005986 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005987 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005988
Kai Huang843e4332015-01-28 10:54:28 +08005989 /*
5990 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
5991 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
5992 * querying dirty_bitmap, we only need to kick all vcpus out of guest
5993 * mode as if vcpus is in root mode, the PML buffer must has been
5994 * flushed already.
5995 */
5996 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02005997 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08005998
Sean Christophersondb438592020-04-22 19:25:48 -07005999 /*
6000 * We should never reach this point with a pending nested VM-Enter, and
6001 * more specifically emulation of L2 due to invalid guest state (see
6002 * below) should never happen as that means we incorrectly allowed a
6003 * nested VM-Enter with an invalid vmcs12.
6004 */
6005 WARN_ON_ONCE(vmx->nested.nested_run_pending);
6006
Mohammed Gamal80ced182009-09-01 12:48:18 +02006007 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006008 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006009 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006010
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006011 if (is_guest_mode(vcpu)) {
6012 /*
6013 * The host physical addresses of some pages of guest memory
6014 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
6015 * Page). The CPU may write to these pages via their host
6016 * physical address while L2 is running, bypassing any
6017 * address-translation-based dirty tracking (e.g. EPT write
6018 * protection).
6019 *
6020 * Mark them dirty on every exit from L2 to prevent them from
6021 * getting out of sync with dirty tracking.
6022 */
6023 nested_mark_vmcs12_pages_dirty(vcpu);
6024
Sean Christophersonf47baae2020-04-15 10:55:16 -07006025 if (nested_vmx_reflect_vmexit(vcpu))
Sean Christopherson789afc52020-04-15 10:55:10 -07006026 return 1;
Paolo Bonzini96b100c2020-03-17 18:32:50 +01006027 }
Nadav Har'El644d7112011-05-25 23:12:35 +03006028
Mohammed Gamal51207022010-05-31 22:40:54 +03006029 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02006030 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03006031 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6032 vcpu->run->fail_entry.hardware_entry_failure_reason
6033 = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006034 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Mohammed Gamal51207022010-05-31 22:40:54 +03006035 return 0;
6036 }
6037
Avi Kivity29bd8a72007-09-10 17:27:03 +03006038 if (unlikely(vmx->fail)) {
Paolo Bonzini3b20e032019-07-19 18:15:08 +02006039 dump_vmcs();
Avi Kivity851ba692009-08-24 11:10:17 +03006040 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6041 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006042 = vmcs_read32(VM_INSTRUCTION_ERROR);
Jim Mattson8a14fe42020-06-03 16:56:22 -07006043 vcpu->run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006044 return 0;
6045 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006046
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006047 /*
6048 * Note:
6049 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6050 * delivery event since it indicates guest is accessing MMIO.
6051 * The vm-exit can be triggered again after return to guest that
6052 * will cause infinite loop.
6053 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006054 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006055 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006056 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00006057 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006058 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6059 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6060 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006061 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006062 vcpu->run->internal.data[0] = vectoring_info;
6063 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02006064 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
6065 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
6066 vcpu->run->internal.ndata++;
6067 vcpu->run->internal.data[3] =
6068 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6069 }
Jim Mattson1aa561b2020-06-03 16:56:21 -07006070 vcpu->run->internal.data[vcpu->run->internal.ndata++] =
Jim Mattson8a14fe42020-06-03 16:56:22 -07006071 vcpu->arch.last_vmentry_cpu;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006072 return 0;
6073 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006074
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006075 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006076 vmx->loaded_vmcs->soft_vnmi_blocked)) {
Sean Christophersondb438592020-04-22 19:25:48 -07006077 if (!vmx_interrupt_blocked(vcpu)) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006078 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6079 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
6080 vcpu->arch.nmi_pending) {
6081 /*
6082 * This CPU don't support us in finding the end of an
6083 * NMI-blocked window if the guest runs with IRQs
6084 * disabled. So we pull the trigger after 1 s of
6085 * futile waiting, but inform the user about this.
6086 */
6087 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6088 "state on VCPU %d after 1 s timeout\n",
6089 __func__, vcpu->vcpu_id);
6090 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6091 }
6092 }
6093
Wanpeng Li404d5d72020-04-28 14:23:25 +08006094 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08006095 return 1;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006096
6097 if (exit_reason >= kvm_vmx_max_exit_handlers)
6098 goto unexpected_vmexit;
6099#ifdef CONFIG_RETPOLINE
6100 if (exit_reason == EXIT_REASON_MSR_WRITE)
6101 return kvm_emulate_wrmsr(vcpu);
6102 else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER)
6103 return handle_preemption_timer(vcpu);
6104 else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW)
6105 return handle_interrupt_window(vcpu);
6106 else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6107 return handle_external_interrupt(vcpu);
6108 else if (exit_reason == EXIT_REASON_HLT)
6109 return kvm_emulate_halt(vcpu);
6110 else if (exit_reason == EXIT_REASON_EPT_MISCONFIG)
6111 return handle_ept_misconfig(vcpu);
6112#endif
6113
6114 exit_reason = array_index_nospec(exit_reason,
6115 kvm_vmx_max_exit_handlers);
6116 if (!kvm_vmx_exit_handlers[exit_reason])
6117 goto unexpected_vmexit;
6118
6119 return kvm_vmx_exit_handlers[exit_reason](vcpu);
6120
6121unexpected_vmexit:
6122 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", exit_reason);
6123 dump_vmcs();
6124 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6125 vcpu->run->internal.suberror =
6126 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
Jim Mattson1aa561b2020-06-03 16:56:21 -07006127 vcpu->run->internal.ndata = 2;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006128 vcpu->run->internal.data[0] = exit_reason;
Jim Mattson8a14fe42020-06-03 16:56:22 -07006129 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Marios Pomonisc926f2f2019-12-11 12:47:51 -08006130 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006131}
6132
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006133/*
6134 * Software based L1D cache flush which is used when microcode providing
6135 * the cache control MSR is not loaded.
6136 *
6137 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
6138 * flush it is required to read in 64 KiB because the replacement algorithm
6139 * is not exactly LRU. This could be sized at runtime via topology
6140 * information but as all relevant affected CPUs have 32KiB L1D cache size
6141 * there is no point in doing so.
6142 */
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006143static noinstr void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006144{
6145 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006146
6147 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02006148 * This code is only executed when the the flush mode is 'cond' or
6149 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006150 */
Nicolai Stange427362a2018-07-21 22:25:00 +02006151 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +02006152 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006153
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006154 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +02006155 * Clear the per-vcpu flush bit, it gets set again
6156 * either from vcpu_run() or from one of the unsafe
6157 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006158 */
Nicolai Stange45b575c2018-07-27 13:22:16 +02006159 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +02006160 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +02006161
6162 /*
6163 * Clear the per-cpu flush bit, it gets set again from
6164 * the interrupt handlers.
6165 */
6166 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
6167 kvm_clear_cpu_l1tf_flush_l1d();
6168
Nicolai Stange5b6ccc62018-07-21 22:35:28 +02006169 if (!flush_l1d)
6170 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +02006171 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +02006172
6173 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006174
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006175 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006176 native_wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02006177 return;
6178 }
6179
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006180 asm volatile(
6181 /* First ensure the pages are in the TLB */
6182 "xorl %%eax, %%eax\n"
6183 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +02006184 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006185 "addl $4096, %%eax\n\t"
6186 "cmpl %%eax, %[size]\n\t"
6187 "jne .Lpopulate_tlb\n\t"
6188 "xorl %%eax, %%eax\n\t"
6189 "cpuid\n\t"
6190 /* Now fill the cache */
6191 "xorl %%eax, %%eax\n"
6192 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006193 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006194 "addl $64, %%eax\n\t"
6195 "cmpl %%eax, %[size]\n\t"
6196 "jne .Lfill_cache\n\t"
6197 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +02006198 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02006199 [size] "r" (size)
6200 : "eax", "ebx", "ecx", "edx");
6201}
6202
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006203static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006204{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006205 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Liran Alon132f4f72019-11-11 14:30:54 +02006206 int tpr_threshold;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006207
6208 if (is_guest_mode(vcpu) &&
6209 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6210 return;
6211
Liran Alon132f4f72019-11-11 14:30:54 +02006212 tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
Liran Alon02d496cf2019-11-11 14:30:55 +02006213 if (is_guest_mode(vcpu))
6214 to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6215 else
6216 vmcs_write32(TPR_THRESHOLD, tpr_threshold);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006217}
6218
Sean Christopherson97b7ead2018-12-03 13:53:16 -08006219void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08006220{
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006221 struct vcpu_vmx *vmx = to_vmx(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006222 u32 sec_exec_control;
6223
Jim Mattson8d860bb2018-05-09 16:56:05 -04006224 if (!lapic_in_kernel(vcpu))
6225 return;
6226
Sean Christophersonfd6b6d92018-10-01 14:25:34 -07006227 if (!flexpriority_enabled &&
6228 !cpu_has_vmx_virtualize_x2apic_mode())
6229 return;
6230
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006231 /* Postpone execution until vmcs01 is the current VMCS. */
6232 if (is_guest_mode(vcpu)) {
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006233 vmx->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02006234 return;
6235 }
6236
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006237 sec_exec_control = secondary_exec_controls_get(vmx);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006238 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6239 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08006240
Jim Mattson8d860bb2018-05-09 16:56:05 -04006241 switch (kvm_get_apic_mode(vcpu)) {
6242 case LAPIC_MODE_INVALID:
6243 WARN_ONCE(true, "Invalid local APIC state");
6244 case LAPIC_MODE_DISABLED:
6245 break;
6246 case LAPIC_MODE_XAPIC:
6247 if (flexpriority_enabled) {
6248 sec_exec_control |=
6249 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006250 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
6251
6252 /*
6253 * Flush the TLB, reloading the APIC access page will
6254 * only do so if its physical address has changed, but
6255 * the guest may have inserted a non-APIC mapping into
6256 * the TLB while the APIC access page was disabled.
6257 */
6258 kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu);
Jim Mattson8d860bb2018-05-09 16:56:05 -04006259 }
6260 break;
6261 case LAPIC_MODE_X2APIC:
6262 if (cpu_has_vmx_virtualize_x2apic_mode())
6263 sec_exec_control |=
6264 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6265 break;
Yang Zhang8d146952013-01-25 10:18:50 +08006266 }
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07006267 secondary_exec_controls_set(vmx, sec_exec_control);
Yang Zhang8d146952013-01-25 10:18:50 +08006268
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006269 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08006270}
6271
Sean Christophersona4148b72020-03-20 14:28:24 -07006272static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
Tang Chen38b99172014-09-24 15:57:54 +08006273{
Sean Christophersona4148b72020-03-20 14:28:24 -07006274 struct page *page;
6275
Sean Christopherson1196cb92020-03-20 14:28:23 -07006276 /* Defer reload until vmcs01 is the current VMCS. */
6277 if (is_guest_mode(vcpu)) {
6278 to_vmx(vcpu)->nested.reload_vmcs01_apic_access_page = true;
6279 return;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07006280 }
Sean Christopherson1196cb92020-03-20 14:28:23 -07006281
Sean Christopherson4de1f9d2020-03-20 14:28:25 -07006282 if (!(secondary_exec_controls_get(to_vmx(vcpu)) &
6283 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
6284 return;
6285
Sean Christophersona4148b72020-03-20 14:28:24 -07006286 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
6287 if (is_error_page(page))
6288 return;
6289
6290 vmcs_write64(APIC_ACCESS_ADDR, page_to_phys(page));
Sean Christopherson1196cb92020-03-20 14:28:23 -07006291 vmx_flush_tlb_current(vcpu);
Sean Christophersona4148b72020-03-20 14:28:24 -07006292
6293 /*
6294 * Do not pin apic access page in memory, the MMU notifier
6295 * will call us again if it is migrated or swapped out.
6296 */
6297 put_page(page);
Tang Chen38b99172014-09-24 15:57:54 +08006298}
6299
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006300static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006301{
6302 u16 status;
6303 u8 old;
6304
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006305 if (max_isr == -1)
6306 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006307
6308 status = vmcs_read16(GUEST_INTR_STATUS);
6309 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006310 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006311 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02006312 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08006313 vmcs_write16(GUEST_INTR_STATUS, status);
6314 }
6315}
6316
6317static void vmx_set_rvi(int vector)
6318{
6319 u16 status;
6320 u8 old;
6321
Wei Wang4114c272014-11-05 10:53:43 +08006322 if (vector == -1)
6323 vector = 0;
6324
Yang Zhangc7c9c562013-01-25 10:18:51 +08006325 status = vmcs_read16(GUEST_INTR_STATUS);
6326 old = (u8)status & 0xff;
6327 if ((u8)vector != old) {
6328 status &= ~0xff;
6329 status |= (u8)vector;
6330 vmcs_write16(GUEST_INTR_STATUS, status);
6331 }
6332}
6333
6334static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6335{
Liran Alon851c1a182017-12-24 18:12:56 +02006336 /*
6337 * When running L2, updating RVI is only relevant when
6338 * vmcs12 virtual-interrupt-delivery enabled.
6339 * However, it can be enabled only when L1 also
6340 * intercepts external-interrupts and in that case
6341 * we should not update vmcs02 RVI but instead intercept
6342 * interrupt. Therefore, do nothing when running L2.
6343 */
6344 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08006345 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08006346}
6347
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006348static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006349{
6350 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006351 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02006352 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006353
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006354 WARN_ON(!vcpu->arch.apicv_active);
6355 if (pi_test_on(&vmx->pi_desc)) {
6356 pi_clear_on(&vmx->pi_desc);
6357 /*
Liran Alond9ff2742019-11-11 14:25:25 +02006358 * IOMMU can write to PID.ON, so the barrier matters even on UP.
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006359 * But on x86 this is just a compiler barrier anyway.
6360 */
6361 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02006362 max_irr_updated =
6363 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6364
6365 /*
6366 * If we are running L2 and L1 has a new pending interrupt
6367 * which can be injected, we should re-evaluate
6368 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02006369 * If L1 intercepts external-interrupts, we should
6370 * exit from L2 to L1. Otherwise, interrupt should be
6371 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02006372 */
Liran Alon851c1a182017-12-24 18:12:56 +02006373 if (is_guest_mode(vcpu) && max_irr_updated) {
6374 if (nested_exit_on_intr(vcpu))
6375 kvm_vcpu_exiting_guest_mode(vcpu);
6376 else
6377 kvm_make_request(KVM_REQ_EVENT, vcpu);
6378 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006379 } else {
6380 max_irr = kvm_lapic_find_highest_irr(vcpu);
6381 }
6382 vmx_hwapic_irr_update(vcpu, max_irr);
6383 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01006384}
6385
Wanpeng Li17e433b2019-08-05 10:03:19 +08006386static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
6387{
Joao Martins9482ae42019-11-11 17:20:10 +00006388 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6389
6390 return pi_test_on(pi_desc) ||
Joao Martins29881b62019-11-11 17:20:12 +00006391 (pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));
Wanpeng Li17e433b2019-08-05 10:03:19 +08006392}
6393
Andrey Smetanin63086302015-11-10 15:36:32 +03006394static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08006395{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006396 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08006397 return;
6398
Yang Zhangc7c9c562013-01-25 10:18:51 +08006399 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6400 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6401 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6402 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6403}
6404
Paolo Bonzini967235d2016-12-19 14:03:45 +01006405static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
6406{
6407 struct vcpu_vmx *vmx = to_vmx(vcpu);
6408
6409 pi_clear_on(&vmx->pi_desc);
6410 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
6411}
6412
Sean Christopherson95b5a482019-04-19 22:50:59 -07006413static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006414{
Sean Christopherson87915852020-04-15 13:34:54 -07006415 u32 intr_info = vmx_get_intr_info(&vmx->vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006416
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006417 /* if exit due to PF check for async PF */
Sean Christopherson87915852020-04-15 13:34:54 -07006418 if (is_page_fault(intr_info)) {
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02006419 vmx->vcpu.arch.apf.host_apf_flags = kvm_read_and_reset_apf_flags();
Andi Kleena0861c02009-06-08 17:37:09 +08006420 /* Handle machine checks before interrupts are enabled */
Sean Christopherson87915852020-04-15 13:34:54 -07006421 } else if (is_machine_check(intr_info)) {
Andi Kleena0861c02009-06-08 17:37:09 +08006422 kvm_machine_check();
Gleb Natapov20f65982009-05-11 13:35:55 +03006423 /* We need to handle NMIs before interrupts are enabled */
Sean Christopherson87915852020-04-15 13:34:54 -07006424 } else if (is_nmi(intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07006425 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03006426 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07006427 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006428 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03006429}
Gleb Natapov20f65982009-05-11 13:35:55 +03006430
Sean Christopherson95b5a482019-04-19 22:50:59 -07006431static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08006432{
Sean Christopherson49def502019-04-19 22:50:56 -07006433 unsigned int vector;
6434 unsigned long entry;
6435#ifdef CONFIG_X86_64
6436 unsigned long tmp;
6437#endif
6438 gate_desc *desc;
Sean Christopherson87915852020-04-15 13:34:54 -07006439 u32 intr_info = vmx_get_intr_info(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006440
Sean Christopherson49def502019-04-19 22:50:56 -07006441 if (WARN_ONCE(!is_external_intr(intr_info),
6442 "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
6443 return;
6444
6445 vector = intr_info & INTR_INFO_VECTOR_MASK;
Sean Christopherson23420802019-04-19 22:50:57 -07006446 desc = (gate_desc *)host_idt_base + vector;
Sean Christopherson49def502019-04-19 22:50:56 -07006447 entry = gate_offset(desc);
6448
Sean Christopherson165072b2019-04-19 22:50:58 -07006449 kvm_before_interrupt(vcpu);
6450
Sean Christopherson49def502019-04-19 22:50:56 -07006451 asm volatile(
Yang Zhanga547c6d2013-04-11 19:25:10 +08006452#ifdef CONFIG_X86_64
Uros Bizjak551896e2020-05-04 17:57:06 +02006453 "mov %%rsp, %[sp]\n\t"
6454 "and $-16, %%rsp\n\t"
6455 "push %[ss]\n\t"
Sean Christopherson49def502019-04-19 22:50:56 -07006456 "push %[sp]\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08006457#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006458 "pushf\n\t"
Uros Bizjak551896e2020-05-04 17:57:06 +02006459 "push %[cs]\n\t"
Sean Christopherson49def502019-04-19 22:50:56 -07006460 CALL_NOSPEC
6461 :
Yang Zhanga547c6d2013-04-11 19:25:10 +08006462#ifdef CONFIG_X86_64
Sean Christopherson49def502019-04-19 22:50:56 -07006463 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08006464#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006465 ASM_CALL_CONSTRAINT
6466 :
Nick Desaulniers428b8f12020-03-23 12:12:43 -07006467 [thunk_target]"r"(entry),
Uros Bizjak551896e2020-05-04 17:57:06 +02006468#ifdef CONFIG_X86_64
Sean Christopherson49def502019-04-19 22:50:56 -07006469 [ss]"i"(__KERNEL_DS),
Uros Bizjak551896e2020-05-04 17:57:06 +02006470#endif
Sean Christopherson49def502019-04-19 22:50:56 -07006471 [cs]"i"(__KERNEL_CS)
6472 );
Sean Christopherson165072b2019-04-19 22:50:58 -07006473
6474 kvm_after_interrupt(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08006475}
Sean Christopherson95b5a482019-04-19 22:50:59 -07006476STACK_FRAME_NON_STANDARD(handle_external_interrupt_irqoff);
6477
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006478static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Sean Christopherson95b5a482019-04-19 22:50:59 -07006479{
6480 struct vcpu_vmx *vmx = to_vmx(vcpu);
6481
6482 if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6483 handle_external_interrupt_irqoff(vcpu);
6484 else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)
6485 handle_exception_nmi_irqoff(vmx);
6486}
Yang Zhanga547c6d2013-04-11 19:25:10 +08006487
Sean Christophersoncb97c2d2020-02-18 15:40:11 -08006488static bool vmx_has_emulated_msr(u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006489{
Tom Lendackybc226f02018-05-10 22:06:39 +02006490 switch (index) {
6491 case MSR_IA32_SMBASE:
6492 /*
6493 * We cannot do SMM unless we can run the guest in big
6494 * real mode.
6495 */
6496 return enable_unrestricted_guest || emulate_invalid_guest_state;
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02006497 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
6498 return nested;
Tom Lendackybc226f02018-05-10 22:06:39 +02006499 case MSR_AMD64_VIRT_SPEC_CTRL:
6500 /* This is AMD only. */
6501 return false;
6502 default:
6503 return true;
6504 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02006505}
6506
Avi Kivity51aa01d2010-07-20 14:31:20 +03006507static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6508{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006509 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006510 bool unblock_nmi;
6511 u8 vector;
6512 bool idtv_info_valid;
6513
6514 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006515
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006516 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006517 if (vmx->loaded_vmcs->nmi_known_unmasked)
6518 return;
Sean Christopherson87915852020-04-15 13:34:54 -07006519
6520 exit_intr_info = vmx_get_intr_info(&vmx->vcpu);
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006521 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6522 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6523 /*
6524 * SDM 3: 27.7.1.2 (September 2008)
6525 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6526 * a guest IRET fault.
6527 * SDM 3: 23.2.2 (September 2008)
6528 * Bit 12 is undefined in any of the following cases:
6529 * If the VM exit sets the valid bit in the IDT-vectoring
6530 * information field.
6531 * If the VM exit is due to a double fault.
6532 */
6533 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6534 vector != DF_VECTOR && !idtv_info_valid)
6535 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6536 GUEST_INTR_STATE_NMI);
6537 else
6538 vmx->loaded_vmcs->nmi_known_unmasked =
6539 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6540 & GUEST_INTR_STATE_NMI);
6541 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
6542 vmx->loaded_vmcs->vnmi_blocked_time +=
6543 ktime_to_ns(ktime_sub(ktime_get(),
6544 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006545}
6546
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006547static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006548 u32 idt_vectoring_info,
6549 int instr_len_field,
6550 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006551{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006552 u8 vector;
6553 int type;
6554 bool idtv_info_valid;
6555
6556 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006557
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006558 vcpu->arch.nmi_injected = false;
6559 kvm_clear_exception_queue(vcpu);
6560 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006561
6562 if (!idtv_info_valid)
6563 return;
6564
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006565 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006566
Avi Kivity668f6122008-07-02 09:28:55 +03006567 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6568 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006569
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006570 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006571 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006572 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006573 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006574 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006575 * Clear bit "block by NMI" before VM entry if a NMI
6576 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006577 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006578 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006579 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006580 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006581 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006582 /* fall through */
6583 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006584 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006585 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03006586 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006587 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03006588 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006589 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006590 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006591 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006592 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03006593 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006594 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006595 break;
6596 default:
6597 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006598 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006599}
6600
Avi Kivity83422e12010-07-20 14:43:23 +03006601static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6602{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006603 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006604 VM_EXIT_INSTRUCTION_LEN,
6605 IDT_VECTORING_ERROR_CODE);
6606}
6607
Avi Kivityb463a6f2010-07-20 15:06:17 +03006608static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6609{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006610 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006611 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6612 VM_ENTRY_INSTRUCTION_LEN,
6613 VM_ENTRY_EXCEPTION_ERROR_CODE);
6614
6615 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6616}
6617
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006618static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6619{
6620 int i, nr_msrs;
6621 struct perf_guest_switch_msr *msrs;
6622
6623 msrs = perf_guest_get_msrs(&nr_msrs);
6624
6625 if (!msrs)
6626 return;
6627
6628 for (i = 0; i < nr_msrs; i++)
6629 if (msrs[i].host == msrs[i].guest)
6630 clear_atomic_switch_msr(vmx, msrs[i].msr);
6631 else
6632 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04006633 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006634}
6635
Sean Christophersonf459a702018-08-27 15:21:11 -07006636static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07006637{
6638 struct vcpu_vmx *vmx = to_vmx(vcpu);
6639 u64 tscl;
6640 u32 delta_tsc;
6641
Sean Christophersond264ee02018-08-27 15:21:12 -07006642 if (vmx->req_immediate_exit) {
Sean Christopherson804939e2019-05-07 12:18:05 -07006643 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
6644 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6645 } else if (vmx->hv_deadline_tsc != -1) {
Sean Christophersonf459a702018-08-27 15:21:11 -07006646 tscl = rdtsc();
6647 if (vmx->hv_deadline_tsc > tscl)
6648 /* set_hv_timer ensures the delta fits in 32-bits */
6649 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
6650 cpu_preemption_timer_multi);
6651 else
6652 delta_tsc = 0;
6653
Sean Christopherson804939e2019-05-07 12:18:05 -07006654 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
6655 vmx->loaded_vmcs->hv_timer_soft_disabled = false;
6656 } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
6657 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
6658 vmx->loaded_vmcs->hv_timer_soft_disabled = true;
Sean Christophersonf459a702018-08-27 15:21:11 -07006659 }
Yunhong Jiang64672c92016-06-13 14:19:59 -07006660}
6661
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006662void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006663{
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006664 if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
6665 vmx->loaded_vmcs->host_state.rsp = host_rsp;
6666 vmcs_writel(HOST_RSP, host_rsp);
6667 }
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006668}
Sean Christophersonc09b03e2019-01-25 07:41:04 -08006669
Wanpeng Li404d5d72020-04-28 14:23:25 +08006670static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006671{
6672 switch (to_vmx(vcpu)->exit_reason) {
6673 case EXIT_REASON_MSR_WRITE:
6674 return handle_fastpath_set_msr_irqoff(vcpu);
Wanpeng Li26efe2f2020-05-06 11:44:01 -04006675 case EXIT_REASON_PREEMPTION_TIMER:
6676 return handle_fastpath_preemption_timer(vcpu);
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006677 default:
6678 return EXIT_FASTPATH_NONE;
6679 }
6680}
6681
Sean Christophersonfc2ba5a2019-01-25 07:41:19 -08006682bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006683
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006684static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
6685 struct vcpu_vmx *vmx)
6686{
6687 /*
6688 * VMENTER enables interrupts (host state), but the kernel state is
6689 * interrupts disabled when this is invoked. Also tell RCU about
6690 * it. This is the same logic as for exit_to_user_mode().
6691 *
6692 * This ensures that e.g. latency analysis on the host observes
6693 * guest mode as interrupt enabled.
6694 *
6695 * guest_enter_irqoff() informs context tracking about the
6696 * transition to guest mode and if enabled adjusts RCU state
6697 * accordingly.
6698 */
6699 instrumentation_begin();
6700 trace_hardirqs_on_prepare();
6701 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
6702 instrumentation_end();
6703
6704 guest_enter_irqoff();
6705 lockdep_hardirqs_on(CALLER_ADDR0);
6706
6707 /* L1D Flush includes CPU buffer clear to mitigate MDS */
6708 if (static_branch_unlikely(&vmx_l1d_should_flush))
6709 vmx_l1d_flush(vcpu);
6710 else if (static_branch_unlikely(&mds_user_clear))
6711 mds_clear_cpu_buffers();
6712
Thomas Gleixner2245d392020-07-08 21:52:00 +02006713 if (vcpu->arch.cr2 != native_read_cr2())
6714 native_write_cr2(vcpu->arch.cr2);
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006715
6716 vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
6717 vmx->loaded_vmcs->launched);
6718
Thomas Gleixner2245d392020-07-08 21:52:00 +02006719 vcpu->arch.cr2 = native_read_cr2();
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006720
6721 /*
6722 * VMEXIT disables interrupts (host state), but tracing and lockdep
6723 * have them in state 'on' as recorded before entering guest mode.
6724 * Same as enter_from_user_mode().
6725 *
6726 * guest_exit_irqoff() restores host context and reinstates RCU if
6727 * enabled and required.
6728 *
6729 * This needs to be done before the below as native_read_msr()
6730 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
6731 * into world and some more.
6732 */
6733 lockdep_hardirqs_off(CALLER_ADDR0);
6734 guest_exit_irqoff();
6735
6736 instrumentation_begin();
6737 trace_hardirqs_off_finish();
6738 instrumentation_end();
6739}
6740
Wanpeng Li404d5d72020-04-28 14:23:25 +08006741static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006742{
Wanpeng Li404d5d72020-04-28 14:23:25 +08006743 fastpath_t exit_fastpath;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006744 struct vcpu_vmx *vmx = to_vmx(vcpu);
6745 unsigned long cr3, cr4;
6746
Wanpeng Li404d5d72020-04-28 14:23:25 +08006747reenter_guest:
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006748 /* Record the guest's net vcpu time for enforced NMI injections. */
6749 if (unlikely(!enable_vnmi &&
6750 vmx->loaded_vmcs->soft_vnmi_blocked))
6751 vmx->loaded_vmcs->entry_time = ktime_get();
6752
6753 /* Don't enter VMX if guest state is invalid, let the exit handler
6754 start emulation until we arrive back to a valid state */
6755 if (vmx->emulation_required)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006756 return EXIT_FASTPATH_NONE;
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006757
6758 if (vmx->ple_window_dirty) {
6759 vmx->ple_window_dirty = false;
6760 vmcs_write32(PLE_WINDOW, vmx->ple_window);
6761 }
6762
wanpeng lic9dfd3f2020-02-17 18:37:43 +08006763 /*
6764 * We did this in prepare_switch_to_guest, because it needs to
6765 * be within srcu_read_lock.
6766 */
6767 WARN_ON_ONCE(vmx->nested.need_vmcs12_to_shadow_sync);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006768
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006769 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006770 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
Sean Christophersoncb3c1e22019-09-27 14:45:22 -07006771 if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006772 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6773
6774 cr3 = __get_current_cr3_fast();
6775 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
6776 vmcs_writel(HOST_CR3, cr3);
6777 vmx->loaded_vmcs->host_state.cr3 = cr3;
6778 }
6779
6780 cr4 = cr4_read_shadow();
6781 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
6782 vmcs_writel(HOST_CR4, cr4);
6783 vmx->loaded_vmcs->host_state.cr4 = cr4;
6784 }
6785
6786 /* When single-stepping over STI and MOV SS, we must clear the
6787 * corresponding interruptibility bits in the guest state. Otherwise
6788 * vmentry fails as it then expects bit 14 (BS) in pending debug
6789 * exceptions being set, but that's not correct for the guest debugging
6790 * case. */
6791 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6792 vmx_set_interrupt_shadow(vcpu, 0);
6793
Aaron Lewis139a12c2019-10-21 16:30:25 -07006794 kvm_load_guest_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006795
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006796 pt_guest_enter(vmx);
6797
Vitaly Kuznetsov49097762020-06-19 11:40:46 +02006798 atomic_switch_perf_msrs(vmx);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006799
Sean Christopherson804939e2019-05-07 12:18:05 -07006800 if (enable_preemption_timer)
6801 vmx_update_hv_timer(vcpu);
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006802
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08006803 if (lapic_in_kernel(vcpu) &&
6804 vcpu->arch.apic->lapic_timer.timer_advance_ns)
6805 kvm_wait_lapic_expire(vcpu);
6806
Sean Christopherson5ad6ece82019-01-15 17:10:53 -08006807 /*
6808 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
6809 * it's non-zero. Since vmentry is serialising on affected CPUs, there
6810 * is no need to worry about the conditional branch over the wrmsr
6811 * being speculatively taken.
6812 */
6813 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
6814
Thomas Gleixner3ebccdf2020-07-08 21:51:57 +02006815 /* The actual VMENTER/EXIT is in the .noinstr.text section. */
6816 vmx_vcpu_enter_exit(vcpu, vmx);
Thomas Gleixner87fa7f32020-07-08 21:51:54 +02006817
6818 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006819 * We do not use IBRS in the kernel. If this vCPU has used the
6820 * SPEC_CTRL MSR it may have left it on; save the value and
6821 * turn it off. This is much more efficient than blindly adding
6822 * it to the atomic save/restore list. Especially as the former
6823 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
6824 *
6825 * For non-nested case:
6826 * If the L01 MSR bitmap does not intercept the MSR, then we need to
6827 * save it.
6828 *
6829 * For nested case:
6830 * If the L02 MSR bitmap does not intercept the MSR, then we need to
6831 * save it.
6832 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01006833 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01006834 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006835
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02006836 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006837
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01006838 /* All fields are clean at this point */
6839 if (static_branch_unlikely(&enable_evmcs))
6840 current_evmcs->hv_clean_fields |=
6841 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
6842
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08006843 if (static_branch_unlikely(&enable_evmcs))
6844 current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
6845
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006846 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -08006847 if (vmx->host_debugctlmsr)
6848 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006849
Avi Kivityaa67f602012-08-01 16:48:03 +03006850#ifndef CONFIG_X86_64
6851 /*
6852 * The sysexit path does not restore ds/es, so we must set them to
6853 * a reasonable value ourselves.
6854 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006855 * We can't defer this to vmx_prepare_switch_to_host() since that
6856 * function may be executed in interrupt context, which saves and
6857 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +03006858 */
6859 loadsegment(ds, __USER_DS);
6860 loadsegment(es, __USER_DS);
6861#endif
6862
Sean Christophersone5d03de2020-04-15 13:34:51 -07006863 vmx_register_cache_reset(vcpu);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006864
Chao Peng2ef444f2018-10-24 16:05:12 +08006865 pt_guest_exit(vmx);
6866
Aaron Lewis139a12c2019-10-21 16:30:25 -07006867 kvm_load_host_xsave_state(vcpu);
WANG Chao1811d972019-04-12 15:55:39 +08006868
Gleb Natapove0b890d2013-09-25 12:51:33 +03006869 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -07006870 vmx->idt_vectoring_info = 0;
6871
Sean Christopherson873e1da2020-04-10 10:47:02 -07006872 if (unlikely(vmx->fail)) {
6873 vmx->exit_reason = 0xdead;
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006874 return EXIT_FASTPATH_NONE;
Sean Christopherson873e1da2020-04-10 10:47:02 -07006875 }
6876
6877 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
6878 if (unlikely((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY))
Sean Christophersonbeb8d932019-04-19 22:50:55 -07006879 kvm_machine_check();
6880
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006881 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
6882
Sean Christopherson873e1da2020-04-10 10:47:02 -07006883 if (unlikely(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006884 return EXIT_FASTPATH_NONE;
6885
Jim Mattsonb060ca32017-09-14 16:31:42 -07006886 vmx->loaded_vmcs->launched = 1;
6887 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +03006888
Avi Kivity51aa01d2010-07-20 14:31:20 +03006889 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006890 vmx_complete_interrupts(vmx);
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006891
Wanpeng Lidcf068d2020-04-28 14:23:23 +08006892 if (is_guest_mode(vcpu))
6893 return EXIT_FASTPATH_NONE;
6894
6895 exit_fastpath = vmx_exit_handlers_fastpath(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006896 if (exit_fastpath == EXIT_FASTPATH_REENTER_GUEST) {
6897 if (!kvm_vcpu_exit_request(vcpu)) {
6898 /*
6899 * FIXME: this goto should be a loop in vcpu_enter_guest,
6900 * but it would incur the cost of a retpoline for now.
6901 * Revisit once static calls are available.
6902 */
Wanpeng Li379a3c82020-04-28 14:23:27 +08006903 if (vcpu->arch.apicv_active)
6904 vmx_sync_pir_to_irr(vcpu);
Wanpeng Li404d5d72020-04-28 14:23:25 +08006905 goto reenter_guest;
6906 }
6907 exit_fastpath = EXIT_FASTPATH_EXIT_HANDLED;
6908 }
6909
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07006910 return exit_fastpath;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006911}
6912
Avi Kivity6aa8b732006-12-10 02:21:36 -08006913static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6914{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006915 struct vcpu_vmx *vmx = to_vmx(vcpu);
6916
Kai Huang843e4332015-01-28 10:54:28 +08006917 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08006918 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08006919 free_vpid(vmx->vpid);
Sean Christopherson55d23752018-12-03 13:53:18 -08006920 nested_vmx_free_vcpu(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006921 free_loaded_vmcs(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006922}
6923
Sean Christopherson987b2592019-12-18 13:54:55 -08006924static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006925{
Ben Gardon41836832019-02-11 11:02:52 -08006926 struct vcpu_vmx *vmx;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006927 unsigned long *msr_bitmap;
Sean Christopherson34109c02019-12-18 13:54:50 -08006928 int i, cpu, err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006929
Sean Christophersona9dd6f02019-12-18 13:54:52 -08006930 BUILD_BUG_ON(offsetof(struct vcpu_vmx, vcpu) != 0);
6931 vmx = to_vmx(vcpu);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006932
Peter Feiner4e595162016-07-07 14:49:58 -07006933 err = -ENOMEM;
6934
Sean Christopherson034d8e22019-12-18 13:54:49 -08006935 vmx->vpid = allocate_vpid();
6936
Peter Feiner4e595162016-07-07 14:49:58 -07006937 /*
6938 * If PML is turned on, failure on enabling PML just results in failure
6939 * of creating the vcpu, therefore we can simplify PML logic (by
6940 * avoiding dealing with cases, such as enabling PML partially on vcpus
Miaohe Lin67b0ae42019-12-11 14:26:22 +08006941 * for the guest), etc.
Peter Feiner4e595162016-07-07 14:49:58 -07006942 */
6943 if (enable_pml) {
Ben Gardon41836832019-02-11 11:02:52 -08006944 vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Peter Feiner4e595162016-07-07 14:49:58 -07006945 if (!vmx->pml_pg)
Sean Christopherson987b2592019-12-18 13:54:55 -08006946 goto free_vpid;
Peter Feiner4e595162016-07-07 14:49:58 -07006947 }
6948
Jim Mattson7d737102019-12-03 16:24:42 -08006949 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) != NR_SHARED_MSRS);
Ingo Molnar965b58a2007-01-05 16:36:23 -08006950
Xiaoyao Li4be53412019-10-20 17:11:00 +08006951 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
6952 u32 index = vmx_msr_index[i];
6953 u32 data_low, data_high;
6954 int j = vmx->nmsrs;
6955
6956 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6957 continue;
6958 if (wrmsr_safe(index, data_low, data_high) < 0)
6959 continue;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006960
Xiaoyao Li4be53412019-10-20 17:11:00 +08006961 vmx->guest_msrs[j].index = i;
6962 vmx->guest_msrs[j].data = 0;
Paolo Bonzini46f4f0a2019-11-21 10:01:51 +01006963 switch (index) {
6964 case MSR_IA32_TSX_CTRL:
6965 /*
6966 * No need to pass TSX_CTRL_CPUID_CLEAR through, so
6967 * let's avoid changing CPUID bits under the host
6968 * kernel's feet.
6969 */
6970 vmx->guest_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
6971 break;
6972 default:
6973 vmx->guest_msrs[j].mask = -1ull;
6974 break;
6975 }
Xiaoyao Li4be53412019-10-20 17:11:00 +08006976 ++vmx->nmsrs;
6977 }
6978
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006979 err = alloc_loaded_vmcs(&vmx->vmcs01);
6980 if (err < 0)
Jim Mattson7d737102019-12-03 16:24:42 -08006981 goto free_pml;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006982
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006983 msr_bitmap = vmx->vmcs01.msr_bitmap;
Jim Mattson788fc1e2018-11-09 09:35:11 -08006984 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006985 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
6986 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
6987 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
6988 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
6989 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
6990 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
Sean Christopherson987b2592019-12-18 13:54:55 -08006991 if (kvm_cstate_in_guest(vcpu->kvm)) {
Wanpeng Lib5170062019-05-21 14:06:53 +08006992 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
6993 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
6994 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
6995 vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
6996 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006997 vmx->msr_bitmap_mode = 0;
6998
Paolo Bonzinif21f1652018-01-11 12:16:15 +01006999 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03007000 cpu = get_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08007001 vmx_vcpu_load(vcpu, cpu);
7002 vcpu->cpu = cpu;
Xiaoyao Li1b842922019-10-20 17:11:01 +08007003 init_vmcs(vmx);
Sean Christopherson34109c02019-12-18 13:54:50 -08007004 vmx_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03007005 put_cpu();
Sean Christopherson34109c02019-12-18 13:54:50 -08007006 if (cpu_need_virtualize_apic_accesses(vcpu)) {
Sean Christopherson987b2592019-12-18 13:54:55 -08007007 err = alloc_apic_access_page(vcpu->kvm);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007008 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02007009 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007010 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007011
Sean Christophersone90008d2018-03-05 12:04:37 -08007012 if (enable_ept && !enable_unrestricted_guest) {
Sean Christopherson987b2592019-12-18 13:54:55 -08007013 err = init_rmode_identity_map(vcpu->kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08007014 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02007015 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08007016 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007017
Roman Kagan63aff652018-07-19 21:59:07 +03007018 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007019 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01007020 vmx_capability.ept);
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007021 else
7022 memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
Wincy Vanb9c237b2015-02-03 23:56:30 +08007023
Wincy Van705699a2015-02-03 23:58:17 +08007024 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007025 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007026
Paolo Bonzinibab0c312020-02-11 18:40:58 +01007027 vcpu->arch.microcode_version = 0x100000000ULL;
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007028 vmx->msr_ia32_feature_control_valid_bits = FEAT_CTL_LOCKED;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007029
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02007030 /*
7031 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
7032 * or POSTED_INTR_WAKEUP_VECTOR.
7033 */
7034 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
7035 vmx->pi_desc.sn = 1;
7036
Lan Tianyu53963a72018-12-06 15:34:36 +08007037 vmx->ept_pointer = INVALID_PAGE;
7038
Sean Christophersona9dd6f02019-12-18 13:54:52 -08007039 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007040
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007041free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007042 free_loaded_vmcs(vmx->loaded_vmcs);
Peter Feiner4e595162016-07-07 14:49:58 -07007043free_pml:
7044 vmx_destroy_pml_buffer(vmx);
Sean Christopherson987b2592019-12-18 13:54:55 -08007045free_vpid:
Wanpeng Li991e7a02015-09-16 17:30:05 +08007046 free_vpid(vmx->vpid);
Sean Christophersona9dd6f02019-12-18 13:54:52 -08007047 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007048}
7049
Thomas Gleixner65fd4cb2019-02-19 11:10:49 +01007050#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"
7051#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 -04007052
Wanpeng Lib31c1142018-03-12 04:53:04 -07007053static int vmx_vm_init(struct kvm *kvm)
7054{
Tianyu Lan877ad952018-07-19 08:40:23 +00007055 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
7056
Wanpeng Lib31c1142018-03-12 04:53:04 -07007057 if (!ple_gap)
7058 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007059
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007060 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
7061 switch (l1tf_mitigation) {
7062 case L1TF_MITIGATION_OFF:
7063 case L1TF_MITIGATION_FLUSH_NOWARN:
7064 /* 'I explicitly don't care' is set */
7065 break;
7066 case L1TF_MITIGATION_FLUSH:
7067 case L1TF_MITIGATION_FLUSH_NOSMT:
7068 case L1TF_MITIGATION_FULL:
7069 /*
7070 * Warn upon starting the first VM in a potentially
7071 * insecure environment.
7072 */
Josh Poimboeufb2849092019-01-30 07:13:58 -06007073 if (sched_smt_active())
Jiri Kosinad90a7a02018-07-13 16:23:25 +02007074 pr_warn_once(L1TF_MSG_SMT);
7075 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
7076 pr_warn_once(L1TF_MSG_L1D);
7077 break;
7078 case L1TF_MITIGATION_FULL_FORCE:
7079 /* Flush is enforced */
7080 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007081 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -04007082 }
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06007083 kvm_apicv_init(kvm, enable_apicv);
Wanpeng Lib31c1142018-03-12 04:53:04 -07007084 return 0;
7085}
7086
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007087static int __init vmx_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03007088{
7089 struct vmcs_config vmcs_conf;
Sean Christopherson7caaa712018-12-03 13:53:01 -08007090 struct vmx_capability vmx_cap;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007091
Sean Christophersonff10e222019-12-20 20:45:10 -08007092 if (!this_cpu_has(X86_FEATURE_MSR_IA32_FEAT_CTL) ||
7093 !this_cpu_has(X86_FEATURE_VMX)) {
7094 pr_err("kvm: VMX is disabled on CPU %d\n", smp_processor_id());
7095 return -EIO;
7096 }
7097
Sean Christopherson7caaa712018-12-03 13:53:01 -08007098 if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007099 return -EIO;
Sean Christopherson3e8eacc2018-12-03 13:53:13 -08007100 if (nested)
Vitaly Kuznetsova4443262020-02-20 18:22:04 +01007101 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept);
Yang, Sheng002c7f72007-07-31 14:23:01 +03007102 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7103 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7104 smp_processor_id());
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007105 return -EIO;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007106 }
Sean Christophersonf257d6d2019-04-19 22:18:17 -07007107 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03007108}
7109
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007110static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007111{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007112 u8 cache;
7113 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007114
Chia-I Wu222f06e2020-02-13 13:30:34 -08007115 /* We wanted to honor guest CD/MTRR/PAT, but doing so could result in
7116 * memory aliases with conflicting memory types and sometimes MCEs.
7117 * We have to be careful as to what are honored and when.
7118 *
7119 * For MMIO, guest CD/MTRR are ignored. The EPT memory type is set to
7120 * UC. The effective memory type is UC or WC depending on guest PAT.
7121 * This was historically the source of MCEs and we want to be
7122 * conservative.
7123 *
7124 * When there is no need to deal with noncoherent DMA (e.g., no VT-d
7125 * or VT-d has snoop control), guest CD/MTRR/PAT are all ignored. The
7126 * EPT memory type is set to WB. The effective memory type is forced
7127 * WB.
7128 *
7129 * Otherwise, we trust guest. Guest CD/MTRR/PAT are all honored. The
7130 * EPT memory type is used to emulate guest CD/MTRR.
Sheng Yang522c68c2009-04-27 20:35:43 +08007131 */
Chia-I Wu222f06e2020-02-13 13:30:34 -08007132
Paolo Bonzini606decd2015-10-01 13:12:47 +02007133 if (is_mmio) {
7134 cache = MTRR_TYPE_UNCACHABLE;
7135 goto exit;
7136 }
7137
7138 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007139 ipat = VMX_EPT_IPAT_BIT;
7140 cache = MTRR_TYPE_WRBACK;
7141 goto exit;
7142 }
7143
7144 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7145 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02007146 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08007147 cache = MTRR_TYPE_WRBACK;
7148 else
7149 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007150 goto exit;
7151 }
7152
Xiao Guangrongff536042015-06-15 16:55:22 +08007153 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08007154
7155exit:
7156 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08007157}
7158
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007159static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007160{
7161 /*
7162 * These bits in the secondary execution controls field
7163 * are dynamic, the others are mostly based on the hypervisor
7164 * architecture and the guest's CPUID. Do not touch the
7165 * dynamic bits.
7166 */
7167 u32 mask =
7168 SECONDARY_EXEC_SHADOW_VMCS |
7169 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +02007170 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7171 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007172
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007173 u32 new_ctl = vmx->secondary_exec_control;
7174 u32 cur_ctl = secondary_exec_controls_get(vmx);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007175
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007176 secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
Xiao Guangrongfeda8052015-09-09 14:05:55 +08007177}
7178
David Matlack8322ebb2016-11-29 18:14:09 -08007179/*
7180 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
7181 * (indicating "allowed-1") if they are supported in the guest's CPUID.
7182 */
7183static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
7184{
7185 struct vcpu_vmx *vmx = to_vmx(vcpu);
7186 struct kvm_cpuid_entry2 *entry;
7187
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007188 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
7189 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -08007190
7191#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
7192 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01007193 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -08007194} while (0)
7195
7196 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007197 cr4_fixed1_update(X86_CR4_VME, edx, feature_bit(VME));
7198 cr4_fixed1_update(X86_CR4_PVI, edx, feature_bit(VME));
7199 cr4_fixed1_update(X86_CR4_TSD, edx, feature_bit(TSC));
7200 cr4_fixed1_update(X86_CR4_DE, edx, feature_bit(DE));
7201 cr4_fixed1_update(X86_CR4_PSE, edx, feature_bit(PSE));
7202 cr4_fixed1_update(X86_CR4_PAE, edx, feature_bit(PAE));
7203 cr4_fixed1_update(X86_CR4_MCE, edx, feature_bit(MCE));
7204 cr4_fixed1_update(X86_CR4_PGE, edx, feature_bit(PGE));
7205 cr4_fixed1_update(X86_CR4_OSFXSR, edx, feature_bit(FXSR));
7206 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, feature_bit(XMM));
7207 cr4_fixed1_update(X86_CR4_VMXE, ecx, feature_bit(VMX));
7208 cr4_fixed1_update(X86_CR4_SMXE, ecx, feature_bit(SMX));
7209 cr4_fixed1_update(X86_CR4_PCIDE, ecx, feature_bit(PCID));
7210 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, feature_bit(XSAVE));
David Matlack8322ebb2016-11-29 18:14:09 -08007211
7212 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
Sean Christopherson87382002019-12-17 13:32:42 -08007213 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, feature_bit(FSGSBASE));
7214 cr4_fixed1_update(X86_CR4_SMEP, ebx, feature_bit(SMEP));
7215 cr4_fixed1_update(X86_CR4_SMAP, ebx, feature_bit(SMAP));
7216 cr4_fixed1_update(X86_CR4_PKE, ecx, feature_bit(PKU));
7217 cr4_fixed1_update(X86_CR4_UMIP, ecx, feature_bit(UMIP));
7218 cr4_fixed1_update(X86_CR4_LA57, ecx, feature_bit(LA57));
David Matlack8322ebb2016-11-29 18:14:09 -08007219
7220#undef cr4_fixed1_update
7221}
7222
Liran Alon5f76f6f2018-09-14 03:25:52 +03007223static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7224{
7225 struct vcpu_vmx *vmx = to_vmx(vcpu);
7226
7227 if (kvm_mpx_supported()) {
7228 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7229
7230 if (mpx_enabled) {
7231 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7232 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7233 } else {
7234 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7235 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7236 }
7237 }
7238}
7239
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007240static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7241{
7242 struct vcpu_vmx *vmx = to_vmx(vcpu);
7243 struct kvm_cpuid_entry2 *best = NULL;
7244 int i;
7245
7246 for (i = 0; i < PT_CPUID_LEAVES; i++) {
7247 best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7248 if (!best)
7249 return;
7250 vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7251 vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7252 vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7253 vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7254 }
7255
7256 /* Get the number of configurable Address Ranges for filtering */
7257 vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7258 PT_CAP_num_address_ranges);
7259
7260 /* Initialize and clear the no dependency bits */
7261 vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7262 RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7263
7264 /*
7265 * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7266 * will inject an #GP
7267 */
7268 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7269 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7270
7271 /*
7272 * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7273 * PSBFreq can be set
7274 */
7275 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7276 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7277 RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7278
7279 /*
7280 * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7281 * MTCFreq can be set
7282 */
7283 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7284 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7285 RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7286
7287 /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7288 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7289 vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7290 RTIT_CTL_PTW_EN);
7291
7292 /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7293 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7294 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7295
7296 /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7297 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7298 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7299
7300 /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
7301 if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7302 vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7303
7304 /* unmask address range configure area */
7305 for (i = 0; i < vmx->pt_desc.addr_range; i++)
Gustavo A. R. Silvad14eff12018-12-26 14:40:59 -06007306 vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007307}
7308
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007309static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08007310{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007311 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007312
Aaron Lewis72041602019-10-21 16:30:20 -07007313 /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7314 vcpu->arch.xsaves_enabled = false;
7315
Paolo Bonzini80154d72017-08-24 13:55:35 +02007316 if (cpu_has_secondary_exec_ctrls()) {
7317 vmx_compute_secondary_exec_control(vmx);
Sean Christophersonfe7f895d2019-05-07 12:17:57 -07007318 vmcs_set_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007319 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007320
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007321 if (nested_vmx_allowed(vcpu))
7322 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007323 FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7324 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08007325 else
7326 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007327 ~(FEAT_CTL_VMX_ENABLED_INSIDE_SMX |
7328 FEAT_CTL_VMX_ENABLED_OUTSIDE_SMX);
David Matlack8322ebb2016-11-29 18:14:09 -08007329
Liran Alon5f76f6f2018-09-14 03:25:52 +03007330 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -08007331 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +03007332 nested_vmx_entry_exit_ctls_update(vcpu);
7333 }
Luwei Kang6c0f0bb2018-10-24 16:05:13 +08007334
7335 if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7336 guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7337 update_intel_pt_cfg(vcpu);
Paolo Bonzinib07a5c52019-11-18 12:23:01 -05007338
7339 if (boot_cpu_has(X86_FEATURE_RTM)) {
7340 struct shared_msr_entry *msr;
7341 msr = find_msr_entry(vmx, MSR_IA32_TSX_CTRL);
7342 if (msr) {
7343 bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
7344 vmx_set_guest_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
7345 }
7346 }
Sheng Yang0e851882009-12-18 16:48:46 +08007347}
7348
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007349static __init void vmx_set_cpu_caps(void)
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007350{
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007351 kvm_set_cpu_caps();
7352
7353 /* CPUID 0x1 */
7354 if (nested)
7355 kvm_cpu_cap_set(X86_FEATURE_VMX);
7356
7357 /* CPUID 0x7 */
Sean Christopherson8721f5b2020-03-02 15:56:45 -08007358 if (kvm_mpx_supported())
7359 kvm_cpu_cap_check_and_set(X86_FEATURE_MPX);
7360 if (cpu_has_vmx_invpcid())
7361 kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID);
7362 if (vmx_pt_mode_is_host_guest())
7363 kvm_cpu_cap_check_and_set(X86_FEATURE_INTEL_PT);
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007364
Sean Christopherson90d2f602020-03-02 15:56:47 -08007365 if (vmx_umip_emulated())
7366 kvm_cpu_cap_set(X86_FEATURE_UMIP);
7367
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007368 /* CPUID 0xD.1 */
Paolo Bonzini408e9a32020-03-05 16:11:56 +01007369 supported_xss = 0;
Sean Christophersonb3d895d52020-03-02 15:56:44 -08007370 if (!vmx_xsaves_supported())
7371 kvm_cpu_cap_clear(X86_FEATURE_XSAVES);
7372
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08007373 /* CPUID 0x80000001 */
7374 if (!cpu_has_vmx_rdtscp())
7375 kvm_cpu_cap_clear(X86_FEATURE_RDTSCP);
Maxim Levitsky0abcc8f2020-05-23 19:14:54 +03007376
7377 if (vmx_waitpkg_supported())
7378 kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007379}
7380
Sean Christophersond264ee02018-08-27 15:21:12 -07007381static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7382{
7383 to_vmx(vcpu)->req_immediate_exit = true;
7384}
7385
Oliver Upton35a57132020-02-04 15:26:31 -08007386static int vmx_check_intercept_io(struct kvm_vcpu *vcpu,
7387 struct x86_instruction_info *info)
7388{
7389 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7390 unsigned short port;
7391 bool intercept;
7392 int size;
7393
7394 if (info->intercept == x86_intercept_in ||
7395 info->intercept == x86_intercept_ins) {
7396 port = info->src_val;
7397 size = info->dst_bytes;
7398 } else {
7399 port = info->dst_val;
7400 size = info->src_bytes;
7401 }
7402
7403 /*
7404 * If the 'use IO bitmaps' VM-execution control is 0, IO instruction
7405 * VM-exits depend on the 'unconditional IO exiting' VM-execution
7406 * control.
7407 *
7408 * Otherwise, IO instruction VM-exits are controlled by the IO bitmaps.
7409 */
7410 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
7411 intercept = nested_cpu_has(vmcs12,
7412 CPU_BASED_UNCOND_IO_EXITING);
7413 else
7414 intercept = nested_vmx_check_io_bitmaps(vcpu, port, size);
7415
Oliver Upton86f7e902020-02-29 11:30:14 -08007416 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
Oliver Upton35a57132020-02-04 15:26:31 -08007417 return intercept ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
7418}
7419
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007420static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7421 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007422 enum x86_intercept_stage stage,
7423 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007424{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007425 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007426
Oliver Upton35a57132020-02-04 15:26:31 -08007427 switch (info->intercept) {
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007428 /*
7429 * RDPID causes #UD if disabled through secondary execution controls.
7430 * Because it is marked as EmulateOnUD, we need to intercept it here.
7431 */
Oliver Upton35a57132020-02-04 15:26:31 -08007432 case x86_intercept_rdtscp:
7433 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08007434 exception->vector = UD_VECTOR;
7435 exception->error_code_valid = false;
Oliver Upton35a57132020-02-04 15:26:31 -08007436 return X86EMUL_PROPAGATE_FAULT;
7437 }
7438 break;
7439
7440 case x86_intercept_in:
7441 case x86_intercept_ins:
7442 case x86_intercept_out:
7443 case x86_intercept_outs:
7444 return vmx_check_intercept_io(vcpu, info);
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007445
Oliver Upton86f7e902020-02-29 11:30:14 -08007446 case x86_intercept_lgdt:
7447 case x86_intercept_lidt:
7448 case x86_intercept_lldt:
7449 case x86_intercept_ltr:
7450 case x86_intercept_sgdt:
7451 case x86_intercept_sidt:
7452 case x86_intercept_sldt:
7453 case x86_intercept_str:
7454 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC))
7455 return X86EMUL_CONTINUE;
7456
7457 /* FIXME: produce nested vmexit and return X86EMUL_INTERCEPTED. */
7458 break;
7459
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +02007460 /* TODO: check more intercepts... */
Oliver Upton35a57132020-02-04 15:26:31 -08007461 default:
7462 break;
7463 }
7464
Paolo Bonzini07721fe2020-02-04 15:26:29 -08007465 return X86EMUL_UNHANDLEABLE;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007466}
7467
Yunhong Jiang64672c92016-06-13 14:19:59 -07007468#ifdef CONFIG_X86_64
7469/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
7470static inline int u64_shl_div_u64(u64 a, unsigned int shift,
7471 u64 divisor, u64 *result)
7472{
7473 u64 low = a << shift, high = a >> (64 - shift);
7474
7475 /* To avoid the overflow on divq */
7476 if (high >= divisor)
7477 return 1;
7478
7479 /* Low hold the result, high hold rem which is discarded */
7480 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
7481 "rm" (divisor), "0" (low), "1" (high));
7482 *result = low;
7483
7484 return 0;
7485}
7486
Sean Christophersonf9927982019-04-16 13:32:46 -07007487static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
7488 bool *expired)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007489{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007490 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007491 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
Sean Christopherson39497d72019-04-17 10:15:32 -07007492 struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007493
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +02007494 vmx = to_vmx(vcpu);
7495 tscl = rdtsc();
7496 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
7497 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Sean Christopherson39497d72019-04-17 10:15:32 -07007498 lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
7499 ktimer->timer_advance_ns);
Wanpeng Lic5ce8232018-05-29 14:53:17 +08007500
7501 if (delta_tsc > lapic_timer_advance_cycles)
7502 delta_tsc -= lapic_timer_advance_cycles;
7503 else
7504 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007505
7506 /* Convert to host delta tsc if tsc scaling is enabled */
7507 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
Sean Christopherson0967fa12019-04-16 13:32:48 -07007508 delta_tsc && u64_shl_div_u64(delta_tsc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007509 kvm_tsc_scaling_ratio_frac_bits,
Sean Christopherson0967fa12019-04-16 13:32:48 -07007510 vcpu->arch.tsc_scaling_ratio, &delta_tsc))
Yunhong Jiang64672c92016-06-13 14:19:59 -07007511 return -ERANGE;
7512
7513 /*
7514 * If the delta tsc can't fit in the 32 bit after the multi shift,
7515 * we can't use the preemption timer.
7516 * It's possible that it fits on later vmentries, but checking
7517 * on every vmentry is costly so we just use an hrtimer.
7518 */
7519 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
7520 return -ERANGE;
7521
7522 vmx->hv_deadline_tsc = tscl + delta_tsc;
Sean Christophersonf9927982019-04-16 13:32:46 -07007523 *expired = !delta_tsc;
7524 return 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007525}
7526
7527static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
7528{
Sean Christophersonf459a702018-08-27 15:21:11 -07007529 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -07007530}
7531#endif
7532
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007533static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007534{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007535 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007536 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007537}
7538
Kai Huang843e4332015-01-28 10:54:28 +08007539static void vmx_slot_enable_log_dirty(struct kvm *kvm,
7540 struct kvm_memory_slot *slot)
7541{
Jay Zhou3c9bd402020-02-27 09:32:27 +08007542 if (!kvm_dirty_log_manual_protect_and_init_set(kvm))
7543 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
Kai Huang843e4332015-01-28 10:54:28 +08007544 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
7545}
7546
7547static void vmx_slot_disable_log_dirty(struct kvm *kvm,
7548 struct kvm_memory_slot *slot)
7549{
7550 kvm_mmu_slot_set_dirty(kvm, slot);
7551}
7552
7553static void vmx_flush_log_dirty(struct kvm *kvm)
7554{
7555 kvm_flush_pml_buffers(kvm);
7556}
7557
7558static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
7559 struct kvm_memory_slot *memslot,
7560 gfn_t offset, unsigned long mask)
7561{
7562 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
7563}
7564
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007565static void __pi_post_block(struct kvm_vcpu *vcpu)
7566{
7567 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7568 struct pi_desc old, new;
7569 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007570
7571 do {
7572 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007573 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
7574 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007575
7576 dest = cpu_physical_id(vcpu->cpu);
7577
7578 if (x2apic_enabled())
7579 new.ndst = dest;
7580 else
7581 new.ndst = (dest << 8) & 0xFF00;
7582
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007583 /* set 'NV' to 'notification vector' */
7584 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02007585 } while (cmpxchg64(&pi_desc->control, old.control,
7586 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007587
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007588 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
7589 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007590 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007591 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007592 vcpu->pre_pcpu = -1;
7593 }
7594}
7595
Feng Wuefc64402015-09-18 22:29:51 +08007596/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007597 * This routine does the following things for vCPU which is going
7598 * to be blocked if VT-d PI is enabled.
7599 * - Store the vCPU to the wakeup list, so when interrupts happen
7600 * we can find the right vCPU to wake up.
7601 * - Change the Posted-interrupt descriptor as below:
7602 * 'NDST' <-- vcpu->pre_pcpu
7603 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
7604 * - If 'ON' is set during this process, which means at least one
7605 * interrupt is posted for this vCPU, we cannot block it, in
7606 * this case, return 1, otherwise, return 0.
7607 *
7608 */
Yunhong Jiangbc225122016-06-13 14:19:58 -07007609static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007610{
Feng Wubf9f6ac2015-09-18 22:29:55 +08007611 unsigned int dest;
7612 struct pi_desc old, new;
7613 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7614
7615 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08007616 !irq_remapping_cap(IRQ_POSTING_CAP) ||
7617 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +08007618 return 0;
7619
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007620 WARN_ON(irqs_disabled());
7621 local_irq_disable();
7622 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
7623 vcpu->pre_pcpu = vcpu->cpu;
7624 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
7625 list_add_tail(&vcpu->blocked_vcpu_list,
7626 &per_cpu(blocked_vcpu_on_cpu,
7627 vcpu->pre_pcpu));
7628 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
7629 }
Feng Wubf9f6ac2015-09-18 22:29:55 +08007630
7631 do {
7632 old.control = new.control = pi_desc->control;
7633
Feng Wubf9f6ac2015-09-18 22:29:55 +08007634 WARN((pi_desc->sn == 1),
7635 "Warning: SN field of posted-interrupts "
7636 "is set before blocking\n");
7637
7638 /*
7639 * Since vCPU can be preempted during this process,
7640 * vcpu->cpu could be different with pre_pcpu, we
7641 * need to set pre_pcpu as the destination of wakeup
7642 * notification event, then we can find the right vCPU
7643 * to wakeup in wakeup handler if interrupts happen
7644 * when the vCPU is in blocked state.
7645 */
7646 dest = cpu_physical_id(vcpu->pre_pcpu);
7647
7648 if (x2apic_enabled())
7649 new.ndst = dest;
7650 else
7651 new.ndst = (dest << 8) & 0xFF00;
7652
7653 /* set 'NV' to 'wakeup vector' */
7654 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02007655 } while (cmpxchg64(&pi_desc->control, old.control,
7656 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007657
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007658 /* We should not block the vCPU if an interrupt is posted for it. */
7659 if (pi_test_on(pi_desc) == 1)
7660 __pi_post_block(vcpu);
7661
7662 local_irq_enable();
7663 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007664}
7665
Yunhong Jiangbc225122016-06-13 14:19:58 -07007666static int vmx_pre_block(struct kvm_vcpu *vcpu)
7667{
7668 if (pi_pre_block(vcpu))
7669 return 1;
7670
Yunhong Jiang64672c92016-06-13 14:19:59 -07007671 if (kvm_lapic_hv_timer_in_use(vcpu))
7672 kvm_lapic_switch_to_sw_timer(vcpu);
7673
Yunhong Jiangbc225122016-06-13 14:19:58 -07007674 return 0;
7675}
7676
7677static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007678{
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007679 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +08007680 return;
7681
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007682 WARN_ON(irqs_disabled());
7683 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +02007684 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +02007685 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +08007686}
7687
Yunhong Jiangbc225122016-06-13 14:19:58 -07007688static void vmx_post_block(struct kvm_vcpu *vcpu)
7689{
Sean Christophersonafaf0b22020-03-21 13:26:00 -07007690 if (kvm_x86_ops.set_hv_timer)
Yunhong Jiang64672c92016-06-13 14:19:59 -07007691 kvm_lapic_switch_to_hv_timer(vcpu);
7692
Yunhong Jiangbc225122016-06-13 14:19:58 -07007693 pi_post_block(vcpu);
7694}
7695
Feng Wubf9f6ac2015-09-18 22:29:55 +08007696/*
Feng Wuefc64402015-09-18 22:29:51 +08007697 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
7698 *
7699 * @kvm: kvm
7700 * @host_irq: host irq of the interrupt
7701 * @guest_irq: gsi of the interrupt
7702 * @set: set or unset PI
7703 * returns 0 on success, < 0 on failure
7704 */
7705static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
7706 uint32_t guest_irq, bool set)
7707{
7708 struct kvm_kernel_irq_routing_entry *e;
7709 struct kvm_irq_routing_table *irq_rt;
7710 struct kvm_lapic_irq irq;
7711 struct kvm_vcpu *vcpu;
7712 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007713 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +08007714
7715 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08007716 !irq_remapping_cap(IRQ_POSTING_CAP) ||
7717 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +08007718 return 0;
7719
7720 idx = srcu_read_lock(&kvm->irq_srcu);
7721 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +01007722 if (guest_irq >= irq_rt->nr_rt_entries ||
7723 hlist_empty(&irq_rt->map[guest_irq])) {
7724 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
7725 guest_irq, irq_rt->nr_rt_entries);
7726 goto out;
7727 }
Feng Wuefc64402015-09-18 22:29:51 +08007728
7729 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
7730 if (e->type != KVM_IRQ_ROUTING_MSI)
7731 continue;
7732 /*
7733 * VT-d PI cannot support posting multicast/broadcast
7734 * interrupts to a vCPU, we still use interrupt remapping
7735 * for these kind of interrupts.
7736 *
7737 * For lowest-priority interrupts, we only support
7738 * those with single CPU as the destination, e.g. user
7739 * configures the interrupts via /proc/irq or uses
7740 * irqbalance to make the interrupts single-CPU.
7741 *
7742 * We will support full lowest-priority interrupt later.
Alexander Graffdcf7562019-09-05 14:58:18 +02007743 *
7744 * In addition, we can only inject generic interrupts using
7745 * the PI mechanism, refuse to route others through it.
Feng Wuefc64402015-09-18 22:29:51 +08007746 */
7747
Radim Krčmář371313132016-07-12 22:09:27 +02007748 kvm_set_msi_irq(kvm, e, &irq);
Alexander Graffdcf7562019-09-05 14:58:18 +02007749 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
7750 !kvm_irq_is_postable(&irq)) {
Feng Wu23a1c252016-01-25 16:53:32 +08007751 /*
7752 * Make sure the IRTE is in remapped mode if
7753 * we don't handle it in posted mode.
7754 */
7755 ret = irq_set_vcpu_affinity(host_irq, NULL);
7756 if (ret < 0) {
7757 printk(KERN_INFO
7758 "failed to back to remapped mode, irq: %u\n",
7759 host_irq);
7760 goto out;
7761 }
7762
Feng Wuefc64402015-09-18 22:29:51 +08007763 continue;
Feng Wu23a1c252016-01-25 16:53:32 +08007764 }
Feng Wuefc64402015-09-18 22:29:51 +08007765
7766 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
7767 vcpu_info.vector = irq.vector;
7768
hu huajun2698d822018-04-11 15:16:40 +08007769 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +08007770 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
7771
7772 if (set)
7773 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +08007774 else
Feng Wuefc64402015-09-18 22:29:51 +08007775 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +08007776
7777 if (ret < 0) {
7778 printk(KERN_INFO "%s: failed to update PI IRTE\n",
7779 __func__);
7780 goto out;
7781 }
7782 }
7783
7784 ret = 0;
7785out:
7786 srcu_read_unlock(&kvm->irq_srcu, idx);
7787 return ret;
7788}
7789
Ashok Rajc45dcc72016-06-22 14:59:56 +08007790static void vmx_setup_mce(struct kvm_vcpu *vcpu)
7791{
7792 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
7793 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007794 FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007795 else
7796 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
Sean Christopherson32ad73d2019-12-20 20:44:55 -08007797 ~FEAT_CTL_LMCE_ENABLED;
Ashok Rajc45dcc72016-06-22 14:59:56 +08007798}
7799
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007800static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Ladi Prosek72d7b372017-10-11 16:54:41 +02007801{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007802 /* we need a nested vmexit to enter SMM, postpone if run is pending */
7803 if (to_vmx(vcpu)->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007804 return -EBUSY;
Paolo Bonzinia9fa7cb2020-04-23 11:02:36 -04007805 return !is_smm(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02007806}
7807
Ladi Prosek0234bf82017-10-11 16:54:40 +02007808static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
7809{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007810 struct vcpu_vmx *vmx = to_vmx(vcpu);
7811
7812 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
7813 if (vmx->nested.smm.guest_mode)
7814 nested_vmx_vmexit(vcpu, -1, 0, 0);
7815
7816 vmx->nested.smm.vmxon = vmx->nested.vmxon;
7817 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -07007818 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +02007819 return 0;
7820}
7821
Sean Christophersoned193212019-04-02 08:03:09 -07007822static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02007823{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007824 struct vcpu_vmx *vmx = to_vmx(vcpu);
7825 int ret;
7826
7827 if (vmx->nested.smm.vmxon) {
7828 vmx->nested.vmxon = true;
7829 vmx->nested.smm.vmxon = false;
7830 }
7831
7832 if (vmx->nested.smm.guest_mode) {
Sean Christophersona633e412018-09-26 09:23:47 -07007833 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +02007834 if (ret)
7835 return ret;
7836
7837 vmx->nested.smm.guest_mode = false;
7838 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02007839 return 0;
7840}
7841
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007842static void enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007843{
Paolo Bonzinic9d40912020-05-22 11:21:49 -04007844 /* RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02007845}
7846
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007847static bool vmx_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
7848{
Yi Wang9481b7f2019-07-15 12:35:17 +08007849 return false;
Singh, Brijesh05d5a482019-02-15 17:24:12 +00007850}
7851
Liran Alon4b9852f2019-08-26 13:24:49 +03007852static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
7853{
7854 return to_vmx(vcpu)->nested.vmxon;
7855}
7856
Jim Mattson93dff2f2020-05-08 13:36:43 -07007857static void vmx_migrate_timers(struct kvm_vcpu *vcpu)
7858{
7859 if (is_guest_mode(vcpu)) {
7860 struct hrtimer *timer = &to_vmx(vcpu)->nested.preemption_timer;
7861
7862 if (hrtimer_try_to_cancel(timer) == 1)
7863 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
7864 }
7865}
7866
Sean Christopherson6e4fd062020-03-21 13:26:01 -07007867static void hardware_unsetup(void)
Sean Christophersona3203382018-12-03 13:53:11 -08007868{
7869 if (nested)
7870 nested_vmx_hardware_unsetup();
7871
7872 free_kvm_area();
7873}
7874
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007875static bool vmx_check_apicv_inhibit_reasons(ulong bit)
7876{
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -06007877 ulong supported = BIT(APICV_INHIBIT_REASON_DISABLE) |
7878 BIT(APICV_INHIBIT_REASON_HYPERV);
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007879
7880 return supported & BIT(bit);
7881}
7882
Sean Christophersone286ac02020-03-21 13:26:02 -07007883static struct kvm_x86_ops vmx_x86_ops __initdata = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007884 .hardware_unsetup = hardware_unsetup,
Sean Christopherson484014f2020-03-21 13:25:57 -07007885
Avi Kivity6aa8b732006-12-10 02:21:36 -08007886 .hardware_enable = hardware_enable,
7887 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007888 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +02007889 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007890
Sean Christopherson484014f2020-03-21 13:25:57 -07007891 .vm_size = sizeof(struct kvm_vmx),
Wanpeng Lib31c1142018-03-12 04:53:04 -07007892 .vm_init = vmx_vm_init,
7893
Avi Kivity6aa8b732006-12-10 02:21:36 -08007894 .vcpu_create = vmx_create_vcpu,
7895 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007896 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007897
Sean Christopherson6d6095b2018-07-23 12:32:44 -07007898 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007899 .vcpu_load = vmx_vcpu_load,
7900 .vcpu_put = vmx_vcpu_put,
7901
Paolo Bonzini69869822020-07-10 17:48:06 +02007902 .update_exception_bitmap = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06007903 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007904 .get_msr = vmx_get_msr,
7905 .set_msr = vmx_set_msr,
7906 .get_segment_base = vmx_get_segment_base,
7907 .get_segment = vmx_get_segment,
7908 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007909 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007910 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
7911 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007912 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007913 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007914 .get_idt = vmx_get_idt,
7915 .set_idt = vmx_set_idt,
7916 .get_gdt = vmx_get_gdt,
7917 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007918 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007919 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007920 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007921 .get_rflags = vmx_get_rflags,
7922 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08007923
Sean Christopherson77809382020-03-20 14:28:18 -07007924 .tlb_flush_all = vmx_flush_tlb_all,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07007925 .tlb_flush_current = vmx_flush_tlb_current,
Junaid Shahidfaff8752018-06-29 13:10:05 -07007926 .tlb_flush_gva = vmx_flush_tlb_gva,
Sean Christophersone64419d2020-03-20 14:28:10 -07007927 .tlb_flush_guest = vmx_flush_tlb_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007928
Avi Kivity6aa8b732006-12-10 02:21:36 -08007929 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007930 .handle_exit = vmx_handle_exit,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08007931 .skip_emulated_instruction = vmx_skip_emulated_instruction,
7932 .update_emulated_instruction = vmx_update_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007933 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7934 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007935 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007936 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007937 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007938 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007939 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007940 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007941 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007942 .get_nmi_mask = vmx_get_nmi_mask,
7943 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007944 .enable_nmi_window = enable_nmi_window,
7945 .enable_irq_window = enable_irq_window,
7946 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04007947 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08007948 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +03007949 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007950 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +01007951 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06007952 .check_apicv_inhibit_reasons = vmx_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007953 .hwapic_irr_update = vmx_hwapic_irr_update,
7954 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +03007955 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +08007956 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7957 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Wanpeng Li17e433b2019-08-05 10:03:19 +08007958 .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007959
Izik Eiduscbc94022007-10-25 00:29:55 +02007960 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07007961 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007962 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007963
Avi Kivity586f9602010-11-18 13:09:54 +02007964 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007965
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08007966 .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007967
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007968 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007969
Leonid Shatz326e7422018-11-06 12:14:25 +02007970 .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007971
Sean Christopherson484014f2020-03-21 13:25:57 -07007972 .load_mmu_pgd = vmx_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007973
7974 .check_intercept = vmx_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07007975 .handle_exit_irqoff = vmx_handle_exit_irqoff,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01007976
Sean Christophersond264ee02018-08-27 15:21:12 -07007977 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02007978
7979 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08007980
7981 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
7982 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
7983 .flush_log_dirty = vmx_flush_log_dirty,
7984 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +02007985
Feng Wubf9f6ac2015-09-18 22:29:55 +08007986 .pre_block = vmx_pre_block,
7987 .post_block = vmx_post_block,
7988
Wei Huang25462f72015-06-19 15:45:05 +02007989 .pmu_ops = &intel_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04007990 .nested_ops = &vmx_nested_ops,
Feng Wuefc64402015-09-18 22:29:51 +08007991
7992 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007993
7994#ifdef CONFIG_X86_64
7995 .set_hv_timer = vmx_set_hv_timer,
7996 .cancel_hv_timer = vmx_cancel_hv_timer,
7997#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +08007998
7999 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02008000
Ladi Prosek72d7b372017-10-11 16:54:41 +02008001 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02008002 .pre_enter_smm = vmx_pre_enter_smm,
8003 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02008004 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02008005
Singh, Brijesh05d5a482019-02-15 17:24:12 +00008006 .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
Liran Alon4b9852f2019-08-26 13:24:49 +03008007 .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
Jim Mattson93dff2f2020-05-08 13:36:43 -07008008 .migrate_timers = vmx_migrate_timers,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008009};
8010
Avi Kivity6aa8b732006-12-10 02:21:36 -08008011static __init int hardware_setup(void)
8012{
8013 unsigned long host_bndcfgs;
8014 struct desc_ptr dt;
Sean Christopherson703c3352020-03-02 15:57:03 -08008015 int r, i, ept_lpage_level;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008016
Avi Kivity6aa8b732006-12-10 02:21:36 -08008017 store_idt(&dt);
8018 host_idt_base = dt.address;
8019
8020 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
8021 kvm_define_shared_msr(i, vmx_msr_index[i]);
8022
8023 if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
8024 return -EIO;
8025
8026 if (boot_cpu_has(X86_FEATURE_NX))
8027 kvm_enable_efer_bits(EFER_NX);
8028
8029 if (boot_cpu_has(X86_FEATURE_MPX)) {
8030 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
8031 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
8032 }
8033
Sean Christopherson7f5581f2020-03-02 15:56:24 -08008034 if (!cpu_has_vmx_mpx())
Sean Christophersoncfc48182020-03-02 15:56:23 -08008035 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS |
8036 XFEATURE_MASK_BNDCSR);
8037
Avi Kivity6aa8b732006-12-10 02:21:36 -08008038 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
8039 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
8040 enable_vpid = 0;
8041
8042 if (!cpu_has_vmx_ept() ||
8043 !cpu_has_vmx_ept_4levels() ||
8044 !cpu_has_vmx_ept_mt_wb() ||
8045 !cpu_has_vmx_invept_global())
8046 enable_ept = 0;
8047
8048 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
8049 enable_ept_ad_bits = 0;
8050
8051 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Avi Kivity873a7c42006-12-13 00:34:14 -08008052 enable_unrestricted_guest = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008053
8054 if (!cpu_has_vmx_flexpriority())
8055 flexpriority_enabled = 0;
8056
8057 if (!cpu_has_virtual_nmis())
8058 enable_vnmi = 0;
8059
8060 /*
8061 * set_apic_access_page_addr() is used to reload apic access
8062 * page upon invalidation. No need to do anything if not
8063 * using the APIC_ACCESS_ADDR VMCS field.
8064 */
8065 if (!flexpriority_enabled)
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008066 vmx_x86_ops.set_apic_access_page_addr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008067
8068 if (!cpu_has_vmx_tpr_shadow())
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008069 vmx_x86_ops.update_cr8_intercept = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008070
Avi Kivity6aa8b732006-12-10 02:21:36 -08008071#if IS_ENABLED(CONFIG_HYPERV)
8072 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
8073 && enable_ept) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008074 vmx_x86_ops.tlb_remote_flush = hv_remote_flush_tlb;
8075 vmx_x86_ops.tlb_remote_flush_with_range =
Avi Kivity6aa8b732006-12-10 02:21:36 -08008076 hv_remote_flush_tlb_with_range;
8077 }
8078#endif
8079
8080 if (!cpu_has_vmx_ple()) {
8081 ple_gap = 0;
8082 ple_window = 0;
8083 ple_window_grow = 0;
8084 ple_window_max = 0;
8085 ple_window_shrink = 0;
8086 }
8087
8088 if (!cpu_has_vmx_apicv()) {
8089 enable_apicv = 0;
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008090 vmx_x86_ops.sync_pir_to_irr = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008091 }
8092
8093 if (cpu_has_vmx_tsc_scaling()) {
8094 kvm_has_tsc_control = true;
8095 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
8096 kvm_tsc_scaling_ratio_frac_bits = 48;
8097 }
8098
8099 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8100
8101 if (enable_ept)
8102 vmx_enable_tdp();
Sean Christopherson703c3352020-03-02 15:57:03 -08008103
8104 if (!enable_ept)
8105 ept_lpage_level = 0;
8106 else if (cpu_has_vmx_ept_1g_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07008107 ept_lpage_level = PG_LEVEL_1G;
Sean Christopherson703c3352020-03-02 15:57:03 -08008108 else if (cpu_has_vmx_ept_2m_page())
Sean Christopherson3bae0452020-04-27 17:54:22 -07008109 ept_lpage_level = PG_LEVEL_2M;
Sean Christopherson703c3352020-03-02 15:57:03 -08008110 else
Sean Christopherson3bae0452020-04-27 17:54:22 -07008111 ept_lpage_level = PG_LEVEL_4K;
Sean Christopherson83013052020-07-15 20:41:22 -07008112 kvm_configure_mmu(enable_ept, vmx_get_max_tdp_level(), ept_lpage_level);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008113
8114 /*
8115 * Only enable PML when hardware supports PML feature, and both EPT
8116 * and EPT A/D bit features are enabled -- PML depends on them to work.
8117 */
8118 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
8119 enable_pml = 0;
8120
8121 if (!enable_pml) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008122 vmx_x86_ops.slot_enable_log_dirty = NULL;
8123 vmx_x86_ops.slot_disable_log_dirty = NULL;
8124 vmx_x86_ops.flush_log_dirty = NULL;
8125 vmx_x86_ops.enable_log_dirty_pt_masked = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008126 }
8127
8128 if (!cpu_has_vmx_preemption_timer())
8129 enable_preemption_timer = false;
8130
8131 if (enable_preemption_timer) {
8132 u64 use_timer_freq = 5000ULL * 1000 * 1000;
8133 u64 vmx_msr;
8134
8135 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8136 cpu_preemption_timer_multi =
8137 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8138
8139 if (tsc_khz)
8140 use_timer_freq = (u64)tsc_khz * 1000;
8141 use_timer_freq >>= cpu_preemption_timer_multi;
8142
8143 /*
8144 * KVM "disables" the preemption timer by setting it to its max
8145 * value. Don't use the timer if it might cause spurious exits
8146 * at a rate faster than 0.1 Hz (of uninterrupted guest time).
8147 */
8148 if (use_timer_freq > 0xffffffffu / 10)
8149 enable_preemption_timer = false;
8150 }
8151
8152 if (!enable_preemption_timer) {
Sean Christopherson72b0eaa2020-03-21 13:25:58 -07008153 vmx_x86_ops.set_hv_timer = NULL;
8154 vmx_x86_ops.cancel_hv_timer = NULL;
8155 vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008156 }
8157
8158 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
8159
8160 kvm_mce_cap_supported |= MCG_LMCE_P;
8161
8162 if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
8163 return -EINVAL;
8164 if (!enable_ept || !cpu_has_vmx_intel_pt())
8165 pt_mode = PT_MODE_SYSTEM;
8166
8167 if (nested) {
8168 nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
8169 vmx_capability.ept);
8170
Sean Christopherson6c1c6e52020-05-06 13:46:53 -07008171 r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008172 if (r)
8173 return r;
8174 }
8175
Sean Christopherson3ec6fd82020-03-02 15:56:43 -08008176 vmx_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08008177
Avi Kivity6aa8b732006-12-10 02:21:36 -08008178 r = alloc_kvm_area();
8179 if (r)
8180 nested_vmx_hardware_unsetup();
8181 return r;
8182}
8183
Sean Christophersond008dfd2020-03-21 13:25:56 -07008184static struct kvm_x86_init_ops vmx_init_ops __initdata = {
8185 .cpu_has_kvm_support = cpu_has_kvm_support,
8186 .disabled_by_bios = vmx_disabled_by_bios,
8187 .check_processor_compatibility = vmx_check_processor_compat,
8188 .hardware_setup = hardware_setup,
8189
8190 .runtime_ops = &vmx_x86_ops,
8191};
8192
Avi Kivity6aa8b732006-12-10 02:21:36 -08008193static void vmx_cleanup_l1d_flush(void)
8194{
8195 if (vmx_l1d_flush_pages) {
8196 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
8197 vmx_l1d_flush_pages = NULL;
8198 }
8199 /* Restore state so sysfs ignores VMX */
8200 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
8201}
8202
8203static void vmx_exit(void)
8204{
8205#ifdef CONFIG_KEXEC_CORE
8206 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
8207 synchronize_rcu();
8208#endif
8209
8210 kvm_exit();
8211
8212#if IS_ENABLED(CONFIG_HYPERV)
8213 if (static_branch_unlikely(&enable_evmcs)) {
8214 int cpu;
8215 struct hv_vp_assist_page *vp_ap;
8216 /*
8217 * Reset everything to support using non-enlightened VMCS
8218 * access later (e.g. when we reload the module with
8219 * enlightened_vmcs=0)
8220 */
8221 for_each_online_cpu(cpu) {
8222 vp_ap = hv_get_vp_assist_page(cpu);
8223
8224 if (!vp_ap)
8225 continue;
8226
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008227 vp_ap->nested_control.features.directhypercall = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008228 vp_ap->current_nested_vmcs = 0;
8229 vp_ap->enlighten_vmentry = 0;
8230 }
8231
8232 static_branch_disable(&enable_evmcs);
8233 }
8234#endif
8235 vmx_cleanup_l1d_flush();
8236}
8237module_exit(vmx_exit);
8238
8239static int __init vmx_init(void)
8240{
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008241 int r, cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008242
8243#if IS_ENABLED(CONFIG_HYPERV)
8244 /*
8245 * Enlightened VMCS usage should be recommended and the host needs
8246 * to support eVMCS v1 or above. We can also disable eVMCS support
8247 * with module parameter.
8248 */
8249 if (enlightened_vmcs &&
8250 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
8251 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
8252 KVM_EVMCS_VERSION) {
8253 int cpu;
8254
8255 /* Check that we have assist pages on all online CPUs */
8256 for_each_online_cpu(cpu) {
8257 if (!hv_get_vp_assist_page(cpu)) {
8258 enlightened_vmcs = false;
8259 break;
8260 }
8261 }
8262
8263 if (enlightened_vmcs) {
8264 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
8265 static_branch_enable(&enable_evmcs);
8266 }
Vitaly Kuznetsov6f6a6572019-08-22 22:30:21 +08008267
8268 if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
8269 vmx_x86_ops.enable_direct_tlbflush
8270 = hv_enable_direct_tlbflush;
8271
Avi Kivity6aa8b732006-12-10 02:21:36 -08008272 } else {
8273 enlightened_vmcs = false;
8274 }
8275#endif
8276
Sean Christophersond008dfd2020-03-21 13:25:56 -07008277 r = kvm_init(&vmx_init_ops, sizeof(struct vcpu_vmx),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008278 __alignof__(struct vcpu_vmx), THIS_MODULE);
8279 if (r)
8280 return r;
8281
8282 /*
8283 * Must be called after kvm_init() so enable_ept is properly set
8284 * up. Hand the parameter mitigation value in which was stored in
8285 * the pre module init parser. If no parameter was given, it will
8286 * contain 'auto' which will be turned into the default 'cond'
8287 * mitigation mode.
8288 */
Waiman Long19a36d32019-08-26 15:30:23 -04008289 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
8290 if (r) {
8291 vmx_exit();
8292 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008293 }
8294
Vitaly Kuznetsovdbef2802020-04-01 10:13:48 +02008295 for_each_possible_cpu(cpu) {
8296 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
8297 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
8298 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
8299 }
8300
Avi Kivity6aa8b732006-12-10 02:21:36 -08008301#ifdef CONFIG_KEXEC_CORE
8302 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8303 crash_vmclear_local_loaded_vmcss);
8304#endif
8305 vmx_check_vmcs12_offsets();
8306
Mohammed Gamal3edd6832020-07-10 17:48:11 +02008307 /*
8308 * Intel processors don't have problems with
8309 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR so enable
8310 * it for VMX by default
8311 */
8312 allow_smaller_maxphyaddr = true;
8313
Avi Kivity6aa8b732006-12-10 02:21:36 -08008314 return 0;
8315}
8316module_init(vmx_init);