blob: ce06701916ae6acacda0f4e179505251c6d819f1 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040074static bool __read_mostly nosmt;
75module_param(nosmt, bool, S_IRUGO);
76
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020078module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080079
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010080static bool __read_mostly enable_vnmi = 1;
81module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
82
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020087module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080088
Rusty Russell476bc002012-01-13 09:32:18 +103089static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070090module_param_named(unrestricted_guest,
91 enable_unrestricted_guest, bool, S_IRUGO);
92
Xudong Hao83c3a332012-05-28 19:33:35 +080093static bool __read_mostly enable_ept_ad_bits = 1;
94module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
95
Avi Kivitya27685c2012-06-12 20:30:18 +030096static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020097module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030098
Rusty Russell476bc002012-01-13 09:32:18 +103099static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +0300100module_param(fasteoi, bool, S_IRUGO);
101
Yang Zhang5a717852013-04-11 19:25:16 +0800102static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800103module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800104
Abel Gordonabc4fc52013-04-18 14:35:25 +0300105static bool __read_mostly enable_shadow_vmcs = 1;
106module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300107/*
108 * If nested=1, nested virtualization is supported, i.e., guests may use
109 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
110 * use VMX instructions.
111 */
Rusty Russell476bc002012-01-13 09:32:18 +1030112static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300113module_param(nested, bool, S_IRUGO);
114
Wanpeng Li20300092014-12-02 19:14:59 +0800115static u64 __read_mostly host_xss;
116
Kai Huang843e4332015-01-28 10:54:28 +0800117static bool __read_mostly enable_pml = 1;
118module_param_named(pml, enable_pml, bool, S_IRUGO);
119
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100120#define MSR_TYPE_R 1
121#define MSR_TYPE_W 2
122#define MSR_TYPE_RW 3
123
124#define MSR_BITMAP_MODE_X2APIC 1
125#define MSR_BITMAP_MODE_X2APIC_APICV 2
126#define MSR_BITMAP_MODE_LM 4
127
Haozhong Zhang64903d62015-10-20 15:39:09 +0800128#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
129
Yunhong Jiang64672c92016-06-13 14:19:59 -0700130/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
131static int __read_mostly cpu_preemption_timer_multi;
132static bool __read_mostly enable_preemption_timer = 1;
133#ifdef CONFIG_X86_64
134module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
135#endif
136
Gleb Natapov50378782013-02-04 16:00:28 +0200137#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800138#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
139#define KVM_VM_CR0_ALWAYS_ON \
140 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
141 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200142#define KVM_CR4_GUEST_OWNED_BITS \
143 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800144 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200145
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800146#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200147#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
148#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
149
Avi Kivity78ac8b42010-04-08 18:19:35 +0300150#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
151
Jan Kiszkaf4124502014-03-07 20:03:13 +0100152#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800154/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300155 * Hyper-V requires all of these, so mark them as supported even though
156 * they are just treated the same as all-context.
157 */
158#define VMX_VPID_EXTENT_SUPPORTED_MASK \
159 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
160 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
161 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164/*
165 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
166 * ple_gap: upper bound on the amount of time between two successive
167 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500168 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169 * ple_window: upper bound on the amount of time a guest is allowed to execute
170 * in a PAUSE loop. Tests indicate that most spinlocks are held for
171 * less than 2^12 cycles
172 * Time is measured based on a counter that runs at the same rate as the TSC,
173 * refer SDM volume 3b section 21.6.13 & 22.1.3.
174 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400175static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176
Babu Moger7fbc85a2018-03-16 16:37:22 -0400177static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
178module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800179
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400181static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400182module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200183
184/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400185static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200187
188/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400189static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
190module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200191
Avi Kivity83287ea422012-09-16 15:10:57 +0300192extern const ulong vmx_return;
193
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
195
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200196/* Storage for pre module init parameter parsing */
197static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198
199static const struct {
200 const char *option;
201 enum vmx_l1d_flush_state cmd;
202} vmentry_l1d_param[] = {
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +0200203 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200204 {"never", VMENTER_L1D_FLUSH_NEVER},
205 {"cond", VMENTER_L1D_FLUSH_COND},
206 {"always", VMENTER_L1D_FLUSH_ALWAYS},
207};
208
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200209#define L1D_CACHE_ORDER 4
210static void *vmx_l1d_flush_pages;
211
212static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
213{
214 struct page *page;
215
216 /* If set to 'auto' select 'cond' */
217 if (l1tf == VMENTER_L1D_FLUSH_AUTO)
218 l1tf = VMENTER_L1D_FLUSH_COND;
219
220 if (!enable_ept) {
221 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
222 return 0;
223 }
224
225 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
226 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
227 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
228 if (!page)
229 return -ENOMEM;
230 vmx_l1d_flush_pages = page_address(page);
231 }
232
233 l1tf_vmx_mitigation = l1tf;
234
235 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
236 static_branch_enable(&vmx_l1d_should_flush);
237 return 0;
238}
239
240static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200241{
242 unsigned int i;
243
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200244 if (s) {
245 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
246 if (!strcmp(s, vmentry_l1d_param[i].option))
247 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200248 }
249 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200250 return -EINVAL;
251}
252
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200253static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
254{
255 int l1tf;
256
257 if (!boot_cpu_has(X86_BUG_L1TF))
258 return 0;
259
260 l1tf = vmentry_l1d_flush_parse(s);
261 if (l1tf < 0)
262 return l1tf;
263
264 /*
265 * Has vmx_init() run already? If not then this is the pre init
266 * parameter parsing. In that case just store the value and let
267 * vmx_init() do the proper setup after enable_ept has been
268 * established.
269 */
270 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
271 vmentry_l1d_flush_param = l1tf;
272 return 0;
273 }
274
275 return vmx_setup_l1d_flush(l1tf);
276}
277
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200278static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
279{
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200280 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200281}
282
283static const struct kernel_param_ops vmentry_l1d_flush_ops = {
284 .set = vmentry_l1d_flush_set,
285 .get = vmentry_l1d_flush_get,
286};
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200287module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, S_IRUGO);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200288
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700289struct kvm_vmx {
290 struct kvm kvm;
291
292 unsigned int tss_addr;
293 bool ept_identity_pagetable_done;
294 gpa_t ept_identity_map_addr;
295};
296
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200297#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300298
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400299struct vmcs {
300 u32 revision_id;
301 u32 abort;
302 char data[0];
303};
304
Nadav Har'Eld462b812011-05-24 15:26:10 +0300305/*
306 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
307 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
308 * loaded on this CPU (so we can clear them if the CPU goes down).
309 */
310struct loaded_vmcs {
311 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700312 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300313 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200314 bool launched;
315 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200316 unsigned long vmcs_host_cr3; /* May not match real cr3 */
317 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100318 /* Support for vnmi-less CPUs */
319 int soft_vnmi_blocked;
320 ktime_t entry_time;
321 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100322 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300323 struct list_head loaded_vmcss_on_cpu_link;
324};
325
Avi Kivity26bb0982009-09-07 11:14:12 +0300326struct shared_msr_entry {
327 unsigned index;
328 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200329 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300330};
331
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300332/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300333 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
334 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
335 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
336 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
337 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
338 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600339 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300340 * underlying hardware which will be used to run L2.
341 * This structure is packed to ensure that its layout is identical across
342 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700343 *
344 * IMPORTANT: Changing the layout of existing fields in this structure
345 * will break save/restore compatibility with older kvm releases. When
346 * adding new fields, either use space in the reserved padding* arrays
347 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300348 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300349typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300350struct __packed vmcs12 {
351 /* According to the Intel spec, a VMCS region must start with the
352 * following two fields. Then follow implementation-specific data.
353 */
354 u32 revision_id;
355 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300356
Nadav Har'El27d6c862011-05-25 23:06:59 +0300357 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
358 u32 padding[7]; /* room for future expansion */
359
Nadav Har'El22bd0352011-05-25 23:05:57 +0300360 u64 io_bitmap_a;
361 u64 io_bitmap_b;
362 u64 msr_bitmap;
363 u64 vm_exit_msr_store_addr;
364 u64 vm_exit_msr_load_addr;
365 u64 vm_entry_msr_load_addr;
366 u64 tsc_offset;
367 u64 virtual_apic_page_addr;
368 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800369 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300370 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800371 u64 eoi_exit_bitmap0;
372 u64 eoi_exit_bitmap1;
373 u64 eoi_exit_bitmap2;
374 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800375 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300376 u64 guest_physical_address;
377 u64 vmcs_link_pointer;
378 u64 guest_ia32_debugctl;
379 u64 guest_ia32_pat;
380 u64 guest_ia32_efer;
381 u64 guest_ia32_perf_global_ctrl;
382 u64 guest_pdptr0;
383 u64 guest_pdptr1;
384 u64 guest_pdptr2;
385 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100386 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300387 u64 host_ia32_pat;
388 u64 host_ia32_efer;
389 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700390 u64 vmread_bitmap;
391 u64 vmwrite_bitmap;
392 u64 vm_function_control;
393 u64 eptp_list_address;
394 u64 pml_address;
395 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300396 /*
397 * To allow migration of L1 (complete with its L2 guests) between
398 * machines of different natural widths (32 or 64 bit), we cannot have
399 * unsigned long fields with no explict size. We use u64 (aliased
400 * natural_width) instead. Luckily, x86 is little-endian.
401 */
402 natural_width cr0_guest_host_mask;
403 natural_width cr4_guest_host_mask;
404 natural_width cr0_read_shadow;
405 natural_width cr4_read_shadow;
406 natural_width cr3_target_value0;
407 natural_width cr3_target_value1;
408 natural_width cr3_target_value2;
409 natural_width cr3_target_value3;
410 natural_width exit_qualification;
411 natural_width guest_linear_address;
412 natural_width guest_cr0;
413 natural_width guest_cr3;
414 natural_width guest_cr4;
415 natural_width guest_es_base;
416 natural_width guest_cs_base;
417 natural_width guest_ss_base;
418 natural_width guest_ds_base;
419 natural_width guest_fs_base;
420 natural_width guest_gs_base;
421 natural_width guest_ldtr_base;
422 natural_width guest_tr_base;
423 natural_width guest_gdtr_base;
424 natural_width guest_idtr_base;
425 natural_width guest_dr7;
426 natural_width guest_rsp;
427 natural_width guest_rip;
428 natural_width guest_rflags;
429 natural_width guest_pending_dbg_exceptions;
430 natural_width guest_sysenter_esp;
431 natural_width guest_sysenter_eip;
432 natural_width host_cr0;
433 natural_width host_cr3;
434 natural_width host_cr4;
435 natural_width host_fs_base;
436 natural_width host_gs_base;
437 natural_width host_tr_base;
438 natural_width host_gdtr_base;
439 natural_width host_idtr_base;
440 natural_width host_ia32_sysenter_esp;
441 natural_width host_ia32_sysenter_eip;
442 natural_width host_rsp;
443 natural_width host_rip;
444 natural_width paddingl[8]; /* room for future expansion */
445 u32 pin_based_vm_exec_control;
446 u32 cpu_based_vm_exec_control;
447 u32 exception_bitmap;
448 u32 page_fault_error_code_mask;
449 u32 page_fault_error_code_match;
450 u32 cr3_target_count;
451 u32 vm_exit_controls;
452 u32 vm_exit_msr_store_count;
453 u32 vm_exit_msr_load_count;
454 u32 vm_entry_controls;
455 u32 vm_entry_msr_load_count;
456 u32 vm_entry_intr_info_field;
457 u32 vm_entry_exception_error_code;
458 u32 vm_entry_instruction_len;
459 u32 tpr_threshold;
460 u32 secondary_vm_exec_control;
461 u32 vm_instruction_error;
462 u32 vm_exit_reason;
463 u32 vm_exit_intr_info;
464 u32 vm_exit_intr_error_code;
465 u32 idt_vectoring_info_field;
466 u32 idt_vectoring_error_code;
467 u32 vm_exit_instruction_len;
468 u32 vmx_instruction_info;
469 u32 guest_es_limit;
470 u32 guest_cs_limit;
471 u32 guest_ss_limit;
472 u32 guest_ds_limit;
473 u32 guest_fs_limit;
474 u32 guest_gs_limit;
475 u32 guest_ldtr_limit;
476 u32 guest_tr_limit;
477 u32 guest_gdtr_limit;
478 u32 guest_idtr_limit;
479 u32 guest_es_ar_bytes;
480 u32 guest_cs_ar_bytes;
481 u32 guest_ss_ar_bytes;
482 u32 guest_ds_ar_bytes;
483 u32 guest_fs_ar_bytes;
484 u32 guest_gs_ar_bytes;
485 u32 guest_ldtr_ar_bytes;
486 u32 guest_tr_ar_bytes;
487 u32 guest_interruptibility_info;
488 u32 guest_activity_state;
489 u32 guest_sysenter_cs;
490 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100491 u32 vmx_preemption_timer_value;
492 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300493 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800494 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300495 u16 guest_es_selector;
496 u16 guest_cs_selector;
497 u16 guest_ss_selector;
498 u16 guest_ds_selector;
499 u16 guest_fs_selector;
500 u16 guest_gs_selector;
501 u16 guest_ldtr_selector;
502 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800503 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300504 u16 host_es_selector;
505 u16 host_cs_selector;
506 u16 host_ss_selector;
507 u16 host_ds_selector;
508 u16 host_fs_selector;
509 u16 host_gs_selector;
510 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700511 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300512};
513
514/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700515 * For save/restore compatibility, the vmcs12 field offsets must not change.
516 */
517#define CHECK_OFFSET(field, loc) \
518 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
519 "Offset of " #field " in struct vmcs12 has changed.")
520
521static inline void vmx_check_vmcs12_offsets(void) {
522 CHECK_OFFSET(revision_id, 0);
523 CHECK_OFFSET(abort, 4);
524 CHECK_OFFSET(launch_state, 8);
525 CHECK_OFFSET(io_bitmap_a, 40);
526 CHECK_OFFSET(io_bitmap_b, 48);
527 CHECK_OFFSET(msr_bitmap, 56);
528 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
529 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
530 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
531 CHECK_OFFSET(tsc_offset, 88);
532 CHECK_OFFSET(virtual_apic_page_addr, 96);
533 CHECK_OFFSET(apic_access_addr, 104);
534 CHECK_OFFSET(posted_intr_desc_addr, 112);
535 CHECK_OFFSET(ept_pointer, 120);
536 CHECK_OFFSET(eoi_exit_bitmap0, 128);
537 CHECK_OFFSET(eoi_exit_bitmap1, 136);
538 CHECK_OFFSET(eoi_exit_bitmap2, 144);
539 CHECK_OFFSET(eoi_exit_bitmap3, 152);
540 CHECK_OFFSET(xss_exit_bitmap, 160);
541 CHECK_OFFSET(guest_physical_address, 168);
542 CHECK_OFFSET(vmcs_link_pointer, 176);
543 CHECK_OFFSET(guest_ia32_debugctl, 184);
544 CHECK_OFFSET(guest_ia32_pat, 192);
545 CHECK_OFFSET(guest_ia32_efer, 200);
546 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
547 CHECK_OFFSET(guest_pdptr0, 216);
548 CHECK_OFFSET(guest_pdptr1, 224);
549 CHECK_OFFSET(guest_pdptr2, 232);
550 CHECK_OFFSET(guest_pdptr3, 240);
551 CHECK_OFFSET(guest_bndcfgs, 248);
552 CHECK_OFFSET(host_ia32_pat, 256);
553 CHECK_OFFSET(host_ia32_efer, 264);
554 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
555 CHECK_OFFSET(vmread_bitmap, 280);
556 CHECK_OFFSET(vmwrite_bitmap, 288);
557 CHECK_OFFSET(vm_function_control, 296);
558 CHECK_OFFSET(eptp_list_address, 304);
559 CHECK_OFFSET(pml_address, 312);
560 CHECK_OFFSET(cr0_guest_host_mask, 344);
561 CHECK_OFFSET(cr4_guest_host_mask, 352);
562 CHECK_OFFSET(cr0_read_shadow, 360);
563 CHECK_OFFSET(cr4_read_shadow, 368);
564 CHECK_OFFSET(cr3_target_value0, 376);
565 CHECK_OFFSET(cr3_target_value1, 384);
566 CHECK_OFFSET(cr3_target_value2, 392);
567 CHECK_OFFSET(cr3_target_value3, 400);
568 CHECK_OFFSET(exit_qualification, 408);
569 CHECK_OFFSET(guest_linear_address, 416);
570 CHECK_OFFSET(guest_cr0, 424);
571 CHECK_OFFSET(guest_cr3, 432);
572 CHECK_OFFSET(guest_cr4, 440);
573 CHECK_OFFSET(guest_es_base, 448);
574 CHECK_OFFSET(guest_cs_base, 456);
575 CHECK_OFFSET(guest_ss_base, 464);
576 CHECK_OFFSET(guest_ds_base, 472);
577 CHECK_OFFSET(guest_fs_base, 480);
578 CHECK_OFFSET(guest_gs_base, 488);
579 CHECK_OFFSET(guest_ldtr_base, 496);
580 CHECK_OFFSET(guest_tr_base, 504);
581 CHECK_OFFSET(guest_gdtr_base, 512);
582 CHECK_OFFSET(guest_idtr_base, 520);
583 CHECK_OFFSET(guest_dr7, 528);
584 CHECK_OFFSET(guest_rsp, 536);
585 CHECK_OFFSET(guest_rip, 544);
586 CHECK_OFFSET(guest_rflags, 552);
587 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
588 CHECK_OFFSET(guest_sysenter_esp, 568);
589 CHECK_OFFSET(guest_sysenter_eip, 576);
590 CHECK_OFFSET(host_cr0, 584);
591 CHECK_OFFSET(host_cr3, 592);
592 CHECK_OFFSET(host_cr4, 600);
593 CHECK_OFFSET(host_fs_base, 608);
594 CHECK_OFFSET(host_gs_base, 616);
595 CHECK_OFFSET(host_tr_base, 624);
596 CHECK_OFFSET(host_gdtr_base, 632);
597 CHECK_OFFSET(host_idtr_base, 640);
598 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
599 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
600 CHECK_OFFSET(host_rsp, 664);
601 CHECK_OFFSET(host_rip, 672);
602 CHECK_OFFSET(pin_based_vm_exec_control, 744);
603 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
604 CHECK_OFFSET(exception_bitmap, 752);
605 CHECK_OFFSET(page_fault_error_code_mask, 756);
606 CHECK_OFFSET(page_fault_error_code_match, 760);
607 CHECK_OFFSET(cr3_target_count, 764);
608 CHECK_OFFSET(vm_exit_controls, 768);
609 CHECK_OFFSET(vm_exit_msr_store_count, 772);
610 CHECK_OFFSET(vm_exit_msr_load_count, 776);
611 CHECK_OFFSET(vm_entry_controls, 780);
612 CHECK_OFFSET(vm_entry_msr_load_count, 784);
613 CHECK_OFFSET(vm_entry_intr_info_field, 788);
614 CHECK_OFFSET(vm_entry_exception_error_code, 792);
615 CHECK_OFFSET(vm_entry_instruction_len, 796);
616 CHECK_OFFSET(tpr_threshold, 800);
617 CHECK_OFFSET(secondary_vm_exec_control, 804);
618 CHECK_OFFSET(vm_instruction_error, 808);
619 CHECK_OFFSET(vm_exit_reason, 812);
620 CHECK_OFFSET(vm_exit_intr_info, 816);
621 CHECK_OFFSET(vm_exit_intr_error_code, 820);
622 CHECK_OFFSET(idt_vectoring_info_field, 824);
623 CHECK_OFFSET(idt_vectoring_error_code, 828);
624 CHECK_OFFSET(vm_exit_instruction_len, 832);
625 CHECK_OFFSET(vmx_instruction_info, 836);
626 CHECK_OFFSET(guest_es_limit, 840);
627 CHECK_OFFSET(guest_cs_limit, 844);
628 CHECK_OFFSET(guest_ss_limit, 848);
629 CHECK_OFFSET(guest_ds_limit, 852);
630 CHECK_OFFSET(guest_fs_limit, 856);
631 CHECK_OFFSET(guest_gs_limit, 860);
632 CHECK_OFFSET(guest_ldtr_limit, 864);
633 CHECK_OFFSET(guest_tr_limit, 868);
634 CHECK_OFFSET(guest_gdtr_limit, 872);
635 CHECK_OFFSET(guest_idtr_limit, 876);
636 CHECK_OFFSET(guest_es_ar_bytes, 880);
637 CHECK_OFFSET(guest_cs_ar_bytes, 884);
638 CHECK_OFFSET(guest_ss_ar_bytes, 888);
639 CHECK_OFFSET(guest_ds_ar_bytes, 892);
640 CHECK_OFFSET(guest_fs_ar_bytes, 896);
641 CHECK_OFFSET(guest_gs_ar_bytes, 900);
642 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
643 CHECK_OFFSET(guest_tr_ar_bytes, 908);
644 CHECK_OFFSET(guest_interruptibility_info, 912);
645 CHECK_OFFSET(guest_activity_state, 916);
646 CHECK_OFFSET(guest_sysenter_cs, 920);
647 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
648 CHECK_OFFSET(vmx_preemption_timer_value, 928);
649 CHECK_OFFSET(virtual_processor_id, 960);
650 CHECK_OFFSET(posted_intr_nv, 962);
651 CHECK_OFFSET(guest_es_selector, 964);
652 CHECK_OFFSET(guest_cs_selector, 966);
653 CHECK_OFFSET(guest_ss_selector, 968);
654 CHECK_OFFSET(guest_ds_selector, 970);
655 CHECK_OFFSET(guest_fs_selector, 972);
656 CHECK_OFFSET(guest_gs_selector, 974);
657 CHECK_OFFSET(guest_ldtr_selector, 976);
658 CHECK_OFFSET(guest_tr_selector, 978);
659 CHECK_OFFSET(guest_intr_status, 980);
660 CHECK_OFFSET(host_es_selector, 982);
661 CHECK_OFFSET(host_cs_selector, 984);
662 CHECK_OFFSET(host_ss_selector, 986);
663 CHECK_OFFSET(host_ds_selector, 988);
664 CHECK_OFFSET(host_fs_selector, 990);
665 CHECK_OFFSET(host_gs_selector, 992);
666 CHECK_OFFSET(host_tr_selector, 994);
667 CHECK_OFFSET(guest_pml_index, 996);
668}
669
670/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300671 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
672 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
673 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700674 *
675 * IMPORTANT: Changing this value will break save/restore compatibility with
676 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300677 */
678#define VMCS12_REVISION 0x11e57ed0
679
680/*
681 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
682 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
683 * current implementation, 4K are reserved to avoid future complications.
684 */
685#define VMCS12_SIZE 0x1000
686
687/*
Jim Mattson5b157062017-12-22 12:11:12 -0800688 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
689 * supported VMCS12 field encoding.
690 */
691#define VMCS12_MAX_FIELD_INDEX 0x17
692
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100693struct nested_vmx_msrs {
694 /*
695 * We only store the "true" versions of the VMX capability MSRs. We
696 * generate the "non-true" versions by setting the must-be-1 bits
697 * according to the SDM.
698 */
699 u32 procbased_ctls_low;
700 u32 procbased_ctls_high;
701 u32 secondary_ctls_low;
702 u32 secondary_ctls_high;
703 u32 pinbased_ctls_low;
704 u32 pinbased_ctls_high;
705 u32 exit_ctls_low;
706 u32 exit_ctls_high;
707 u32 entry_ctls_low;
708 u32 entry_ctls_high;
709 u32 misc_low;
710 u32 misc_high;
711 u32 ept_caps;
712 u32 vpid_caps;
713 u64 basic;
714 u64 cr0_fixed0;
715 u64 cr0_fixed1;
716 u64 cr4_fixed0;
717 u64 cr4_fixed1;
718 u64 vmcs_enum;
719 u64 vmfunc_controls;
720};
721
Jim Mattson5b157062017-12-22 12:11:12 -0800722/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300723 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
724 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
725 */
726struct nested_vmx {
727 /* Has the level1 guest done vmxon? */
728 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400729 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400730 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300731
732 /* The guest-physical address of the current VMCS L1 keeps for L2 */
733 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700734 /*
735 * Cache of the guest's VMCS, existing outside of guest memory.
736 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700737 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700738 */
739 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300740 /*
741 * Indicates if the shadow vmcs must be updated with the
742 * data hold by vmcs12
743 */
744 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100745 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300746
Jim Mattson8d860bb2018-05-09 16:56:05 -0400747 bool change_vmcs01_virtual_apic_mode;
748
Nadav Har'El644d7112011-05-25 23:12:35 +0300749 /* L2 must run next, and mustn't decide to exit to L1. */
750 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600751
752 struct loaded_vmcs vmcs02;
753
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300754 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600755 * Guest pages referred to in the vmcs02 with host-physical
756 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300757 */
758 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800759 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800760 struct page *pi_desc_page;
761 struct pi_desc *pi_desc;
762 bool pi_pending;
763 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100764
765 struct hrtimer preemption_timer;
766 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200767
768 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
769 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800770
Wanpeng Li5c614b32015-10-13 09:18:36 -0700771 u16 vpid02;
772 u16 last_vpid;
773
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100774 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200775
776 /* SMM related state */
777 struct {
778 /* in VMX operation on SMM entry? */
779 bool vmxon;
780 /* in guest mode on SMM entry? */
781 bool guest_mode;
782 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300783};
784
Yang Zhang01e439b2013-04-11 19:25:12 +0800785#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800786#define POSTED_INTR_SN 1
787
Yang Zhang01e439b2013-04-11 19:25:12 +0800788/* Posted-Interrupt Descriptor */
789struct pi_desc {
790 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800791 union {
792 struct {
793 /* bit 256 - Outstanding Notification */
794 u16 on : 1,
795 /* bit 257 - Suppress Notification */
796 sn : 1,
797 /* bit 271:258 - Reserved */
798 rsvd_1 : 14;
799 /* bit 279:272 - Notification Vector */
800 u8 nv;
801 /* bit 287:280 - Reserved */
802 u8 rsvd_2;
803 /* bit 319:288 - Notification Destination */
804 u32 ndst;
805 };
806 u64 control;
807 };
808 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800809} __aligned(64);
810
Yang Zhanga20ed542013-04-11 19:25:15 +0800811static bool pi_test_and_set_on(struct pi_desc *pi_desc)
812{
813 return test_and_set_bit(POSTED_INTR_ON,
814 (unsigned long *)&pi_desc->control);
815}
816
817static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
818{
819 return test_and_clear_bit(POSTED_INTR_ON,
820 (unsigned long *)&pi_desc->control);
821}
822
823static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
824{
825 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
826}
827
Feng Wuebbfc762015-09-18 22:29:46 +0800828static inline void pi_clear_sn(struct pi_desc *pi_desc)
829{
830 return clear_bit(POSTED_INTR_SN,
831 (unsigned long *)&pi_desc->control);
832}
833
834static inline void pi_set_sn(struct pi_desc *pi_desc)
835{
836 return set_bit(POSTED_INTR_SN,
837 (unsigned long *)&pi_desc->control);
838}
839
Paolo Bonziniad361092016-09-20 16:15:05 +0200840static inline void pi_clear_on(struct pi_desc *pi_desc)
841{
842 clear_bit(POSTED_INTR_ON,
843 (unsigned long *)&pi_desc->control);
844}
845
Feng Wuebbfc762015-09-18 22:29:46 +0800846static inline int pi_test_on(struct pi_desc *pi_desc)
847{
848 return test_bit(POSTED_INTR_ON,
849 (unsigned long *)&pi_desc->control);
850}
851
852static inline int pi_test_sn(struct pi_desc *pi_desc)
853{
854 return test_bit(POSTED_INTR_SN,
855 (unsigned long *)&pi_desc->control);
856}
857
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400858struct vmx_msrs {
859 unsigned int nr;
860 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
861};
862
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400863struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000864 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300865 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300866 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100867 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300868 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200869 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200870 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300871 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400872 int nmsrs;
873 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800874 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400875#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300876 u64 msr_host_kernel_gs_base;
877 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400878#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100879
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100880 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100881 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100882
Gleb Natapov2961e8762013-11-25 15:37:13 +0200883 u32 vm_entry_controls_shadow;
884 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200885 u32 secondary_exec_control;
886
Nadav Har'Eld462b812011-05-24 15:26:10 +0300887 /*
888 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
889 * non-nested (L1) guest, it always points to vmcs01. For a nested
890 * guest (L2), it points to a different VMCS.
891 */
892 struct loaded_vmcs vmcs01;
893 struct loaded_vmcs *loaded_vmcs;
894 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300895 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400896 struct vmx_msrs guest;
897 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300898 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400899 struct {
900 int loaded;
901 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300902#ifdef CONFIG_X86_64
903 u16 ds_sel, es_sel;
904#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200905 int gs_ldt_reload_needed;
906 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000907 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400908 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200909 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300910 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300911 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300912 struct kvm_segment segs[8];
913 } rmode;
914 struct {
915 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300916 struct kvm_save_segment {
917 u16 selector;
918 unsigned long base;
919 u32 limit;
920 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300921 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300922 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800923 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300924 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200925
Andi Kleena0861c02009-06-08 17:37:09 +0800926 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800927
Yang Zhang01e439b2013-04-11 19:25:12 +0800928 /* Posted interrupt descriptor */
929 struct pi_desc pi_desc;
930
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300931 /* Support for a guest hypervisor (nested VMX) */
932 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200933
934 /* Dynamic PLE window. */
935 int ple_window;
936 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800937
938 /* Support for PML */
939#define PML_ENTITY_NUM 512
940 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800941
Yunhong Jiang64672c92016-06-13 14:19:59 -0700942 /* apic deadline value in host tsc */
943 u64 hv_deadline_tsc;
944
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800945 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800946
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800947 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800948
Wanpeng Li74c55932017-11-29 01:31:20 -0800949 unsigned long host_debugctlmsr;
950
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800951 /*
952 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
953 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
954 * in msr_ia32_feature_control_valid_bits.
955 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800956 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800957 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400958};
959
Avi Kivity2fb92db2011-04-27 19:42:18 +0300960enum segment_cache_field {
961 SEG_FIELD_SEL = 0,
962 SEG_FIELD_BASE = 1,
963 SEG_FIELD_LIMIT = 2,
964 SEG_FIELD_AR = 3,
965
966 SEG_FIELD_NR = 4
967};
968
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700969static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
970{
971 return container_of(kvm, struct kvm_vmx, kvm);
972}
973
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400974static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
975{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000976 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977}
978
Feng Wuefc64402015-09-18 22:29:51 +0800979static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
980{
981 return &(to_vmx(vcpu)->pi_desc);
982}
983
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800984#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300985#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800986#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
987#define FIELD64(number, name) \
988 FIELD(number, name), \
989 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300990
Abel Gordon4607c2d2013-04-18 14:35:55 +0300991
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100992static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100993#define SHADOW_FIELD_RO(x) x,
994#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300995};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400996static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300997 ARRAY_SIZE(shadow_read_only_fields);
998
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100999static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001000#define SHADOW_FIELD_RW(x) x,
1001#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001002};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001003static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001004 ARRAY_SIZE(shadow_read_write_fields);
1005
Mathias Krause772e0312012-08-30 01:30:19 +02001006static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001007 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001008 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001009 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1010 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1011 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1012 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1013 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1014 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1015 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1016 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001017 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001018 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001019 FIELD(HOST_ES_SELECTOR, host_es_selector),
1020 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1021 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1022 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1023 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1024 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1025 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1026 FIELD64(IO_BITMAP_A, io_bitmap_a),
1027 FIELD64(IO_BITMAP_B, io_bitmap_b),
1028 FIELD64(MSR_BITMAP, msr_bitmap),
1029 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1030 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1031 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001032 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001033 FIELD64(TSC_OFFSET, tsc_offset),
1034 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1035 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001036 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001037 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001038 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001039 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1040 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1041 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1042 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001043 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001044 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1045 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001046 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001047 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1048 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1049 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1050 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1051 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1052 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1053 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1054 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1055 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1056 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001057 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001058 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1059 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1060 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1061 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1062 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1063 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1064 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1065 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1066 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1067 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1068 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1069 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1070 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1071 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1072 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1073 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1074 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1075 FIELD(TPR_THRESHOLD, tpr_threshold),
1076 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1077 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1078 FIELD(VM_EXIT_REASON, vm_exit_reason),
1079 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1080 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1081 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1082 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1083 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1084 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1085 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1086 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1087 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1088 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1089 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1090 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1091 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1092 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1093 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1094 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1095 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1096 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1097 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1098 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1099 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1100 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1101 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1102 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1103 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1104 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1105 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1106 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001107 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001108 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1109 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1110 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1111 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1112 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1113 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1114 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1115 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1116 FIELD(EXIT_QUALIFICATION, exit_qualification),
1117 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1118 FIELD(GUEST_CR0, guest_cr0),
1119 FIELD(GUEST_CR3, guest_cr3),
1120 FIELD(GUEST_CR4, guest_cr4),
1121 FIELD(GUEST_ES_BASE, guest_es_base),
1122 FIELD(GUEST_CS_BASE, guest_cs_base),
1123 FIELD(GUEST_SS_BASE, guest_ss_base),
1124 FIELD(GUEST_DS_BASE, guest_ds_base),
1125 FIELD(GUEST_FS_BASE, guest_fs_base),
1126 FIELD(GUEST_GS_BASE, guest_gs_base),
1127 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1128 FIELD(GUEST_TR_BASE, guest_tr_base),
1129 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1130 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1131 FIELD(GUEST_DR7, guest_dr7),
1132 FIELD(GUEST_RSP, guest_rsp),
1133 FIELD(GUEST_RIP, guest_rip),
1134 FIELD(GUEST_RFLAGS, guest_rflags),
1135 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1136 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1137 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1138 FIELD(HOST_CR0, host_cr0),
1139 FIELD(HOST_CR3, host_cr3),
1140 FIELD(HOST_CR4, host_cr4),
1141 FIELD(HOST_FS_BASE, host_fs_base),
1142 FIELD(HOST_GS_BASE, host_gs_base),
1143 FIELD(HOST_TR_BASE, host_tr_base),
1144 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1145 FIELD(HOST_IDTR_BASE, host_idtr_base),
1146 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1147 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1148 FIELD(HOST_RSP, host_rsp),
1149 FIELD(HOST_RIP, host_rip),
1150};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001151
1152static inline short vmcs_field_to_offset(unsigned long field)
1153{
Dan Williams085331d2018-01-31 17:47:03 -08001154 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1155 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001156 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001157
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001158 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001159 return -ENOENT;
1160
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001161 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001162 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001163 return -ENOENT;
1164
Linus Torvalds15303ba2018-02-10 13:16:35 -08001165 index = array_index_nospec(index, size);
1166 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001167 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001168 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001169 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001170}
1171
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001172static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1173{
David Matlack4f2777b2016-07-13 17:16:37 -07001174 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001175}
1176
Peter Feiner995f00a2017-06-30 17:26:32 -07001177static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001178static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001179static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001180static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001181static void vmx_set_segment(struct kvm_vcpu *vcpu,
1182 struct kvm_segment *var, int seg);
1183static void vmx_get_segment(struct kvm_vcpu *vcpu,
1184 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001185static bool guest_state_valid(struct kvm_vcpu *vcpu);
1186static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001187static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001188static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1189static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1190static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1191 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001192static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001193static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1194 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001195
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1197static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001198/*
1199 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1200 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1201 */
1202static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001203
Feng Wubf9f6ac2015-09-18 22:29:55 +08001204/*
1205 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1206 * can find which vCPU should be waken up.
1207 */
1208static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1209static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1210
Radim Krčmář23611332016-09-29 22:41:33 +02001211enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001212 VMX_VMREAD_BITMAP,
1213 VMX_VMWRITE_BITMAP,
1214 VMX_BITMAP_NR
1215};
1216
1217static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1218
Radim Krčmář23611332016-09-29 22:41:33 +02001219#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1220#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001221
Avi Kivity110312c2010-12-21 12:54:20 +02001222static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001223static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001224
Sheng Yang2384d2b2008-01-17 15:14:33 +08001225static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1226static DEFINE_SPINLOCK(vmx_vpid_lock);
1227
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001228static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229 int size;
1230 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001231 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001232 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001233 u32 pin_based_exec_ctrl;
1234 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001235 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001236 u32 vmexit_ctrl;
1237 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001238 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001239} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240
Hannes Ederefff9e52008-11-28 17:02:06 +01001241static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001242 u32 ept;
1243 u32 vpid;
1244} vmx_capability;
1245
Avi Kivity6aa8b732006-12-10 02:21:36 -08001246#define VMX_SEGMENT_FIELD(seg) \
1247 [VCPU_SREG_##seg] = { \
1248 .selector = GUEST_##seg##_SELECTOR, \
1249 .base = GUEST_##seg##_BASE, \
1250 .limit = GUEST_##seg##_LIMIT, \
1251 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1252 }
1253
Mathias Krause772e0312012-08-30 01:30:19 +02001254static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255 unsigned selector;
1256 unsigned base;
1257 unsigned limit;
1258 unsigned ar_bytes;
1259} kvm_vmx_segment_fields[] = {
1260 VMX_SEGMENT_FIELD(CS),
1261 VMX_SEGMENT_FIELD(DS),
1262 VMX_SEGMENT_FIELD(ES),
1263 VMX_SEGMENT_FIELD(FS),
1264 VMX_SEGMENT_FIELD(GS),
1265 VMX_SEGMENT_FIELD(SS),
1266 VMX_SEGMENT_FIELD(TR),
1267 VMX_SEGMENT_FIELD(LDTR),
1268};
1269
Avi Kivity26bb0982009-09-07 11:14:12 +03001270static u64 host_efer;
1271
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001272static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1273
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001274/*
Brian Gerst8c065852010-07-17 09:03:26 -04001275 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001276 * away by decrementing the array size.
1277 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001279#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001280 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001282 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001285DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1286
1287#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1288
1289#define KVM_EVMCS_VERSION 1
1290
1291#if IS_ENABLED(CONFIG_HYPERV)
1292static bool __read_mostly enlightened_vmcs = true;
1293module_param(enlightened_vmcs, bool, 0444);
1294
1295static inline void evmcs_write64(unsigned long field, u64 value)
1296{
1297 u16 clean_field;
1298 int offset = get_evmcs_offset(field, &clean_field);
1299
1300 if (offset < 0)
1301 return;
1302
1303 *(u64 *)((char *)current_evmcs + offset) = value;
1304
1305 current_evmcs->hv_clean_fields &= ~clean_field;
1306}
1307
1308static inline void evmcs_write32(unsigned long field, u32 value)
1309{
1310 u16 clean_field;
1311 int offset = get_evmcs_offset(field, &clean_field);
1312
1313 if (offset < 0)
1314 return;
1315
1316 *(u32 *)((char *)current_evmcs + offset) = value;
1317 current_evmcs->hv_clean_fields &= ~clean_field;
1318}
1319
1320static inline void evmcs_write16(unsigned long field, u16 value)
1321{
1322 u16 clean_field;
1323 int offset = get_evmcs_offset(field, &clean_field);
1324
1325 if (offset < 0)
1326 return;
1327
1328 *(u16 *)((char *)current_evmcs + offset) = value;
1329 current_evmcs->hv_clean_fields &= ~clean_field;
1330}
1331
1332static inline u64 evmcs_read64(unsigned long field)
1333{
1334 int offset = get_evmcs_offset(field, NULL);
1335
1336 if (offset < 0)
1337 return 0;
1338
1339 return *(u64 *)((char *)current_evmcs + offset);
1340}
1341
1342static inline u32 evmcs_read32(unsigned long field)
1343{
1344 int offset = get_evmcs_offset(field, NULL);
1345
1346 if (offset < 0)
1347 return 0;
1348
1349 return *(u32 *)((char *)current_evmcs + offset);
1350}
1351
1352static inline u16 evmcs_read16(unsigned long field)
1353{
1354 int offset = get_evmcs_offset(field, NULL);
1355
1356 if (offset < 0)
1357 return 0;
1358
1359 return *(u16 *)((char *)current_evmcs + offset);
1360}
1361
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001362static inline void evmcs_touch_msr_bitmap(void)
1363{
1364 if (unlikely(!current_evmcs))
1365 return;
1366
1367 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1368 current_evmcs->hv_clean_fields &=
1369 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1370}
1371
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001372static void evmcs_load(u64 phys_addr)
1373{
1374 struct hv_vp_assist_page *vp_ap =
1375 hv_get_vp_assist_page(smp_processor_id());
1376
1377 vp_ap->current_nested_vmcs = phys_addr;
1378 vp_ap->enlighten_vmentry = 1;
1379}
1380
1381static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1382{
1383 /*
1384 * Enlightened VMCSv1 doesn't support these:
1385 *
1386 * POSTED_INTR_NV = 0x00000002,
1387 * GUEST_INTR_STATUS = 0x00000810,
1388 * APIC_ACCESS_ADDR = 0x00002014,
1389 * POSTED_INTR_DESC_ADDR = 0x00002016,
1390 * EOI_EXIT_BITMAP0 = 0x0000201c,
1391 * EOI_EXIT_BITMAP1 = 0x0000201e,
1392 * EOI_EXIT_BITMAP2 = 0x00002020,
1393 * EOI_EXIT_BITMAP3 = 0x00002022,
1394 */
1395 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1396 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1397 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1398 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1399 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1400 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1401 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1402
1403 /*
1404 * GUEST_PML_INDEX = 0x00000812,
1405 * PML_ADDRESS = 0x0000200e,
1406 */
1407 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1408
1409 /* VM_FUNCTION_CONTROL = 0x00002018, */
1410 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1411
1412 /*
1413 * EPTP_LIST_ADDRESS = 0x00002024,
1414 * VMREAD_BITMAP = 0x00002026,
1415 * VMWRITE_BITMAP = 0x00002028,
1416 */
1417 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1418
1419 /*
1420 * TSC_MULTIPLIER = 0x00002032,
1421 */
1422 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1423
1424 /*
1425 * PLE_GAP = 0x00004020,
1426 * PLE_WINDOW = 0x00004022,
1427 */
1428 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1429
1430 /*
1431 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1432 */
1433 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1434
1435 /*
1436 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1437 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1438 */
1439 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1440 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1441
1442 /*
1443 * Currently unsupported in KVM:
1444 * GUEST_IA32_RTIT_CTL = 0x00002814,
1445 */
1446}
1447#else /* !IS_ENABLED(CONFIG_HYPERV) */
1448static inline void evmcs_write64(unsigned long field, u64 value) {}
1449static inline void evmcs_write32(unsigned long field, u32 value) {}
1450static inline void evmcs_write16(unsigned long field, u16 value) {}
1451static inline u64 evmcs_read64(unsigned long field) { return 0; }
1452static inline u32 evmcs_read32(unsigned long field) { return 0; }
1453static inline u16 evmcs_read16(unsigned long field) { return 0; }
1454static inline void evmcs_load(u64 phys_addr) {}
1455static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001456static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001457#endif /* IS_ENABLED(CONFIG_HYPERV) */
1458
Jan Kiszka5bb16012016-02-09 20:14:21 +01001459static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460{
1461 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1462 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001463 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1464}
1465
Jan Kiszka6f054852016-02-09 20:15:18 +01001466static inline bool is_debug(u32 intr_info)
1467{
1468 return is_exception_n(intr_info, DB_VECTOR);
1469}
1470
1471static inline bool is_breakpoint(u32 intr_info)
1472{
1473 return is_exception_n(intr_info, BP_VECTOR);
1474}
1475
Jan Kiszka5bb16012016-02-09 20:14:21 +01001476static inline bool is_page_fault(u32 intr_info)
1477{
1478 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479}
1480
Gui Jianfeng31299942010-03-15 17:29:09 +08001481static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001482{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001483 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001484}
1485
Gui Jianfeng31299942010-03-15 17:29:09 +08001486static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001487{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001488 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001489}
1490
Liran Alon9e869482018-03-12 13:12:51 +02001491static inline bool is_gp_fault(u32 intr_info)
1492{
1493 return is_exception_n(intr_info, GP_VECTOR);
1494}
1495
Gui Jianfeng31299942010-03-15 17:29:09 +08001496static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497{
1498 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1499 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1500}
1501
Gui Jianfeng31299942010-03-15 17:29:09 +08001502static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001503{
1504 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1505 INTR_INFO_VALID_MASK)) ==
1506 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1507}
1508
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001509/* Undocumented: icebp/int1 */
1510static inline bool is_icebp(u32 intr_info)
1511{
1512 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1513 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1514}
1515
Gui Jianfeng31299942010-03-15 17:29:09 +08001516static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001517{
Sheng Yang04547152009-04-01 15:52:31 +08001518 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001519}
1520
Gui Jianfeng31299942010-03-15 17:29:09 +08001521static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001522{
Sheng Yang04547152009-04-01 15:52:31 +08001523 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001524}
1525
Paolo Bonzini35754c92015-07-29 12:05:37 +02001526static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001527{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001528 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001529}
1530
Gui Jianfeng31299942010-03-15 17:29:09 +08001531static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001532{
Sheng Yang04547152009-04-01 15:52:31 +08001533 return vmcs_config.cpu_based_exec_ctrl &
1534 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001535}
1536
Avi Kivity774ead32007-12-26 13:57:04 +02001537static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001538{
Sheng Yang04547152009-04-01 15:52:31 +08001539 return vmcs_config.cpu_based_2nd_exec_ctrl &
1540 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1541}
1542
Yang Zhang8d146952013-01-25 10:18:50 +08001543static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1544{
1545 return vmcs_config.cpu_based_2nd_exec_ctrl &
1546 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1547}
1548
Yang Zhang83d4c282013-01-25 10:18:49 +08001549static inline bool cpu_has_vmx_apic_register_virt(void)
1550{
1551 return vmcs_config.cpu_based_2nd_exec_ctrl &
1552 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1553}
1554
Yang Zhangc7c9c562013-01-25 10:18:51 +08001555static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1556{
1557 return vmcs_config.cpu_based_2nd_exec_ctrl &
1558 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1559}
1560
Yunhong Jiang64672c92016-06-13 14:19:59 -07001561/*
1562 * Comment's format: document - errata name - stepping - processor name.
1563 * Refer from
1564 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1565 */
1566static u32 vmx_preemption_cpu_tfms[] = {
1567/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15680x000206E6,
1569/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1570/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1571/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15720x00020652,
1573/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15740x00020655,
1575/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1576/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1577/*
1578 * 320767.pdf - AAP86 - B1 -
1579 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1580 */
15810x000106E5,
1582/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15830x000106A0,
1584/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15850x000106A1,
1586/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15870x000106A4,
1588 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1589 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1590 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15910x000106A5,
1592};
1593
1594static inline bool cpu_has_broken_vmx_preemption_timer(void)
1595{
1596 u32 eax = cpuid_eax(0x00000001), i;
1597
1598 /* Clear the reserved bits */
1599 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001600 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001601 if (eax == vmx_preemption_cpu_tfms[i])
1602 return true;
1603
1604 return false;
1605}
1606
1607static inline bool cpu_has_vmx_preemption_timer(void)
1608{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001609 return vmcs_config.pin_based_exec_ctrl &
1610 PIN_BASED_VMX_PREEMPTION_TIMER;
1611}
1612
Yang Zhang01e439b2013-04-11 19:25:12 +08001613static inline bool cpu_has_vmx_posted_intr(void)
1614{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001615 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1616 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001617}
1618
1619static inline bool cpu_has_vmx_apicv(void)
1620{
1621 return cpu_has_vmx_apic_register_virt() &&
1622 cpu_has_vmx_virtual_intr_delivery() &&
1623 cpu_has_vmx_posted_intr();
1624}
1625
Sheng Yang04547152009-04-01 15:52:31 +08001626static inline bool cpu_has_vmx_flexpriority(void)
1627{
1628 return cpu_has_vmx_tpr_shadow() &&
1629 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001630}
1631
Marcelo Tosattie7997942009-06-11 12:07:40 -03001632static inline bool cpu_has_vmx_ept_execute_only(void)
1633{
Gui Jianfeng31299942010-03-15 17:29:09 +08001634 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001635}
1636
Marcelo Tosattie7997942009-06-11 12:07:40 -03001637static inline bool cpu_has_vmx_ept_2m_page(void)
1638{
Gui Jianfeng31299942010-03-15 17:29:09 +08001639 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001640}
1641
Sheng Yang878403b2010-01-05 19:02:29 +08001642static inline bool cpu_has_vmx_ept_1g_page(void)
1643{
Gui Jianfeng31299942010-03-15 17:29:09 +08001644 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001645}
1646
Sheng Yang4bc9b982010-06-02 14:05:24 +08001647static inline bool cpu_has_vmx_ept_4levels(void)
1648{
1649 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1650}
1651
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001652static inline bool cpu_has_vmx_ept_mt_wb(void)
1653{
1654 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1655}
1656
Yu Zhang855feb62017-08-24 20:27:55 +08001657static inline bool cpu_has_vmx_ept_5levels(void)
1658{
1659 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1660}
1661
Xudong Hao83c3a332012-05-28 19:33:35 +08001662static inline bool cpu_has_vmx_ept_ad_bits(void)
1663{
1664 return vmx_capability.ept & VMX_EPT_AD_BIT;
1665}
1666
Gui Jianfeng31299942010-03-15 17:29:09 +08001667static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001668{
Gui Jianfeng31299942010-03-15 17:29:09 +08001669 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001670}
1671
Gui Jianfeng31299942010-03-15 17:29:09 +08001672static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001673{
Gui Jianfeng31299942010-03-15 17:29:09 +08001674 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001675}
1676
Liran Aloncd9a4912018-05-22 17:16:15 +03001677static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1678{
1679 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1680}
1681
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001682static inline bool cpu_has_vmx_invvpid_single(void)
1683{
1684 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1685}
1686
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001687static inline bool cpu_has_vmx_invvpid_global(void)
1688{
1689 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1690}
1691
Wanpeng Li08d839c2017-03-23 05:30:08 -07001692static inline bool cpu_has_vmx_invvpid(void)
1693{
1694 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1695}
1696
Gui Jianfeng31299942010-03-15 17:29:09 +08001697static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001698{
Sheng Yang04547152009-04-01 15:52:31 +08001699 return vmcs_config.cpu_based_2nd_exec_ctrl &
1700 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001701}
1702
Gui Jianfeng31299942010-03-15 17:29:09 +08001703static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001704{
1705 return vmcs_config.cpu_based_2nd_exec_ctrl &
1706 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1707}
1708
Gui Jianfeng31299942010-03-15 17:29:09 +08001709static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001710{
1711 return vmcs_config.cpu_based_2nd_exec_ctrl &
1712 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1713}
1714
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001715static inline bool cpu_has_vmx_basic_inout(void)
1716{
1717 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1718}
1719
Paolo Bonzini35754c92015-07-29 12:05:37 +02001720static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001721{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001722 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001723}
1724
Gui Jianfeng31299942010-03-15 17:29:09 +08001725static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001726{
Sheng Yang04547152009-04-01 15:52:31 +08001727 return vmcs_config.cpu_based_2nd_exec_ctrl &
1728 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001729}
1730
Gui Jianfeng31299942010-03-15 17:29:09 +08001731static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001732{
1733 return vmcs_config.cpu_based_2nd_exec_ctrl &
1734 SECONDARY_EXEC_RDTSCP;
1735}
1736
Mao, Junjiead756a12012-07-02 01:18:48 +00001737static inline bool cpu_has_vmx_invpcid(void)
1738{
1739 return vmcs_config.cpu_based_2nd_exec_ctrl &
1740 SECONDARY_EXEC_ENABLE_INVPCID;
1741}
1742
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001743static inline bool cpu_has_virtual_nmis(void)
1744{
1745 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1746}
1747
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001748static inline bool cpu_has_vmx_wbinvd_exit(void)
1749{
1750 return vmcs_config.cpu_based_2nd_exec_ctrl &
1751 SECONDARY_EXEC_WBINVD_EXITING;
1752}
1753
Abel Gordonabc4fc52013-04-18 14:35:25 +03001754static inline bool cpu_has_vmx_shadow_vmcs(void)
1755{
1756 u64 vmx_msr;
1757 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1758 /* check if the cpu supports writing r/o exit information fields */
1759 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1760 return false;
1761
1762 return vmcs_config.cpu_based_2nd_exec_ctrl &
1763 SECONDARY_EXEC_SHADOW_VMCS;
1764}
1765
Kai Huang843e4332015-01-28 10:54:28 +08001766static inline bool cpu_has_vmx_pml(void)
1767{
1768 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1769}
1770
Haozhong Zhang64903d62015-10-20 15:39:09 +08001771static inline bool cpu_has_vmx_tsc_scaling(void)
1772{
1773 return vmcs_config.cpu_based_2nd_exec_ctrl &
1774 SECONDARY_EXEC_TSC_SCALING;
1775}
1776
Bandan Das2a499e42017-08-03 15:54:41 -04001777static inline bool cpu_has_vmx_vmfunc(void)
1778{
1779 return vmcs_config.cpu_based_2nd_exec_ctrl &
1780 SECONDARY_EXEC_ENABLE_VMFUNC;
1781}
1782
Sean Christopherson64f7a112018-04-30 10:01:06 -07001783static bool vmx_umip_emulated(void)
1784{
1785 return vmcs_config.cpu_based_2nd_exec_ctrl &
1786 SECONDARY_EXEC_DESC;
1787}
1788
Sheng Yang04547152009-04-01 15:52:31 +08001789static inline bool report_flexpriority(void)
1790{
1791 return flexpriority_enabled;
1792}
1793
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001794static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1795{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001796 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001797}
1798
Jim Mattsonf4160e42018-05-29 09:11:33 -07001799/*
1800 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1801 * to modify any valid field of the VMCS, or are the VM-exit
1802 * information fields read-only?
1803 */
1804static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1805{
1806 return to_vmx(vcpu)->nested.msrs.misc_low &
1807 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1808}
1809
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001810static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1811{
1812 return vmcs12->cpu_based_vm_exec_control & bit;
1813}
1814
1815static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1816{
1817 return (vmcs12->cpu_based_vm_exec_control &
1818 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1819 (vmcs12->secondary_vm_exec_control & bit);
1820}
1821
Jan Kiszkaf4124502014-03-07 20:03:13 +01001822static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1823{
1824 return vmcs12->pin_based_vm_exec_control &
1825 PIN_BASED_VMX_PREEMPTION_TIMER;
1826}
1827
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001828static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1829{
1830 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1831}
1832
1833static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1834{
1835 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1836}
1837
Nadav Har'El155a97a2013-08-05 11:07:16 +03001838static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1839{
1840 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1841}
1842
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001843static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1844{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001845 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001846}
1847
Bandan Dasc5f983f2017-05-05 15:25:14 -04001848static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1849{
1850 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1851}
1852
Wincy Vanf2b93282015-02-03 23:56:03 +08001853static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1854{
1855 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1856}
1857
Wanpeng Li5c614b32015-10-13 09:18:36 -07001858static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1859{
1860 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1861}
1862
Wincy Van82f0dd42015-02-03 23:57:18 +08001863static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1864{
1865 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1866}
1867
Wincy Van608406e2015-02-03 23:57:51 +08001868static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1869{
1870 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1871}
1872
Wincy Van705699a2015-02-03 23:58:17 +08001873static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1874{
1875 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1876}
1877
Bandan Das27c42a12017-08-03 15:54:42 -04001878static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1879{
1880 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1881}
1882
Bandan Das41ab9372017-08-03 15:54:43 -04001883static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1884{
1885 return nested_cpu_has_vmfunc(vmcs12) &&
1886 (vmcs12->vm_function_control &
1887 VMX_VMFUNC_EPTP_SWITCHING);
1888}
1889
Jim Mattsonef85b672016-12-12 11:01:37 -08001890static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001891{
1892 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001893 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001894}
1895
Jan Kiszka533558b2014-01-04 18:47:20 +01001896static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1897 u32 exit_intr_info,
1898 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001899static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1900 struct vmcs12 *vmcs12,
1901 u32 reason, unsigned long qualification);
1902
Rusty Russell8b9cf982007-07-30 16:31:43 +10001903static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001904{
1905 int i;
1906
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001907 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001908 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001909 return i;
1910 return -1;
1911}
1912
Sheng Yang2384d2b2008-01-17 15:14:33 +08001913static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1914{
1915 struct {
1916 u64 vpid : 16;
1917 u64 rsvd : 48;
1918 u64 gva;
1919 } operand = { vpid, 0, gva };
1920
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001921 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001922 /* CF==1 or ZF==1 --> rc = -1 */
1923 "; ja 1f ; ud2 ; 1:"
1924 : : "a"(&operand), "c"(ext) : "cc", "memory");
1925}
1926
Sheng Yang14394422008-04-28 12:24:45 +08001927static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1928{
1929 struct {
1930 u64 eptp, gpa;
1931 } operand = {eptp, gpa};
1932
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001933 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001934 /* CF==1 or ZF==1 --> rc = -1 */
1935 "; ja 1f ; ud2 ; 1:\n"
1936 : : "a" (&operand), "c" (ext) : "cc", "memory");
1937}
1938
Avi Kivity26bb0982009-09-07 11:14:12 +03001939static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001940{
1941 int i;
1942
Rusty Russell8b9cf982007-07-30 16:31:43 +10001943 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001944 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001945 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001946 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001947}
1948
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949static void vmcs_clear(struct vmcs *vmcs)
1950{
1951 u64 phys_addr = __pa(vmcs);
1952 u8 error;
1953
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001954 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001955 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 : "cc", "memory");
1957 if (error)
1958 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1959 vmcs, phys_addr);
1960}
1961
Nadav Har'Eld462b812011-05-24 15:26:10 +03001962static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1963{
1964 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001965 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1966 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001967 loaded_vmcs->cpu = -1;
1968 loaded_vmcs->launched = 0;
1969}
1970
Dongxiao Xu7725b892010-05-11 18:29:38 +08001971static void vmcs_load(struct vmcs *vmcs)
1972{
1973 u64 phys_addr = __pa(vmcs);
1974 u8 error;
1975
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001976 if (static_branch_unlikely(&enable_evmcs))
1977 return evmcs_load(phys_addr);
1978
Dongxiao Xu7725b892010-05-11 18:29:38 +08001979 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001980 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001981 : "cc", "memory");
1982 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001983 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001984 vmcs, phys_addr);
1985}
1986
Dave Young2965faa2015-09-09 15:38:55 -07001987#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001988/*
1989 * This bitmap is used to indicate whether the vmclear
1990 * operation is enabled on all cpus. All disabled by
1991 * default.
1992 */
1993static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1994
1995static inline void crash_enable_local_vmclear(int cpu)
1996{
1997 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1998}
1999
2000static inline void crash_disable_local_vmclear(int cpu)
2001{
2002 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2003}
2004
2005static inline int crash_local_vmclear_enabled(int cpu)
2006{
2007 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2008}
2009
2010static void crash_vmclear_local_loaded_vmcss(void)
2011{
2012 int cpu = raw_smp_processor_id();
2013 struct loaded_vmcs *v;
2014
2015 if (!crash_local_vmclear_enabled(cpu))
2016 return;
2017
2018 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2019 loaded_vmcss_on_cpu_link)
2020 vmcs_clear(v->vmcs);
2021}
2022#else
2023static inline void crash_enable_local_vmclear(int cpu) { }
2024static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002025#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002026
Nadav Har'Eld462b812011-05-24 15:26:10 +03002027static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002029 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002030 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031
Nadav Har'Eld462b812011-05-24 15:26:10 +03002032 if (loaded_vmcs->cpu != cpu)
2033 return; /* vcpu migration can race with cpu offline */
2034 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002036 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002037 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002038
2039 /*
2040 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2041 * is before setting loaded_vmcs->vcpu to -1 which is done in
2042 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2043 * then adds the vmcs into percpu list before it is deleted.
2044 */
2045 smp_wmb();
2046
Nadav Har'Eld462b812011-05-24 15:26:10 +03002047 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002048 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002049}
2050
Nadav Har'Eld462b812011-05-24 15:26:10 +03002051static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002052{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002053 int cpu = loaded_vmcs->cpu;
2054
2055 if (cpu != -1)
2056 smp_call_function_single(cpu,
2057 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002058}
2059
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002060static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002061{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002062 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002063 return;
2064
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002065 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002066 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002067}
2068
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002069static inline void vpid_sync_vcpu_global(void)
2070{
2071 if (cpu_has_vmx_invvpid_global())
2072 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2073}
2074
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002075static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002076{
2077 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002078 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002079 else
2080 vpid_sync_vcpu_global();
2081}
2082
Sheng Yang14394422008-04-28 12:24:45 +08002083static inline void ept_sync_global(void)
2084{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002085 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002086}
2087
2088static inline void ept_sync_context(u64 eptp)
2089{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002090 if (cpu_has_vmx_invept_context())
2091 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2092 else
2093 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002094}
2095
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002096static __always_inline void vmcs_check16(unsigned long field)
2097{
2098 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2099 "16-bit accessor invalid for 64-bit field");
2100 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2101 "16-bit accessor invalid for 64-bit high field");
2102 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2103 "16-bit accessor invalid for 32-bit high field");
2104 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2105 "16-bit accessor invalid for natural width field");
2106}
2107
2108static __always_inline void vmcs_check32(unsigned long field)
2109{
2110 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2111 "32-bit accessor invalid for 16-bit field");
2112 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2113 "32-bit accessor invalid for natural width field");
2114}
2115
2116static __always_inline void vmcs_check64(unsigned long field)
2117{
2118 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2119 "64-bit accessor invalid for 16-bit field");
2120 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2121 "64-bit accessor invalid for 64-bit high field");
2122 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2123 "64-bit accessor invalid for 32-bit field");
2124 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2125 "64-bit accessor invalid for natural width field");
2126}
2127
2128static __always_inline void vmcs_checkl(unsigned long field)
2129{
2130 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2131 "Natural width accessor invalid for 16-bit field");
2132 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2133 "Natural width accessor invalid for 64-bit field");
2134 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2135 "Natural width accessor invalid for 64-bit high field");
2136 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2137 "Natural width accessor invalid for 32-bit field");
2138}
2139
2140static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141{
Avi Kivity5e520e62011-05-15 10:13:12 -04002142 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143
Avi Kivity5e520e62011-05-15 10:13:12 -04002144 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2145 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002146 return value;
2147}
2148
Avi Kivity96304212011-05-15 10:13:13 -04002149static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002151 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002152 if (static_branch_unlikely(&enable_evmcs))
2153 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002154 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155}
2156
Avi Kivity96304212011-05-15 10:13:13 -04002157static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002159 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002160 if (static_branch_unlikely(&enable_evmcs))
2161 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002162 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163}
2164
Avi Kivity96304212011-05-15 10:13:13 -04002165static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002167 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002168 if (static_branch_unlikely(&enable_evmcs))
2169 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002170#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002171 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002173 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002174#endif
2175}
2176
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002177static __always_inline unsigned long vmcs_readl(unsigned long field)
2178{
2179 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002180 if (static_branch_unlikely(&enable_evmcs))
2181 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002182 return __vmcs_readl(field);
2183}
2184
Avi Kivitye52de1b2007-01-05 16:36:56 -08002185static noinline void vmwrite_error(unsigned long field, unsigned long value)
2186{
2187 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2188 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2189 dump_stack();
2190}
2191
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002192static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193{
2194 u8 error;
2195
Avi Kivity4ecac3f2008-05-13 13:23:38 +03002196 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04002197 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08002198 if (unlikely(error))
2199 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200}
2201
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002202static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002204 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002205 if (static_branch_unlikely(&enable_evmcs))
2206 return evmcs_write16(field, value);
2207
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002208 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209}
2210
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002211static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002213 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002214 if (static_branch_unlikely(&enable_evmcs))
2215 return evmcs_write32(field, value);
2216
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002217 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218}
2219
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002220static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002222 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002223 if (static_branch_unlikely(&enable_evmcs))
2224 return evmcs_write64(field, value);
2225
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002226 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002227#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002229 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230#endif
2231}
2232
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002233static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002234{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002235 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002236 if (static_branch_unlikely(&enable_evmcs))
2237 return evmcs_write64(field, value);
2238
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002239 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002240}
2241
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002242static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002243{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002244 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2245 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002246 if (static_branch_unlikely(&enable_evmcs))
2247 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2248
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002249 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2250}
2251
2252static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2253{
2254 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2255 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002256 if (static_branch_unlikely(&enable_evmcs))
2257 return evmcs_write32(field, evmcs_read32(field) | mask);
2258
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002259 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002260}
2261
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002262static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2263{
2264 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2265}
2266
Gleb Natapov2961e8762013-11-25 15:37:13 +02002267static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2268{
2269 vmcs_write32(VM_ENTRY_CONTROLS, val);
2270 vmx->vm_entry_controls_shadow = val;
2271}
2272
2273static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2274{
2275 if (vmx->vm_entry_controls_shadow != val)
2276 vm_entry_controls_init(vmx, val);
2277}
2278
2279static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2280{
2281 return vmx->vm_entry_controls_shadow;
2282}
2283
2284
2285static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2286{
2287 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2288}
2289
2290static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2291{
2292 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2293}
2294
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002295static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2296{
2297 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2298}
2299
Gleb Natapov2961e8762013-11-25 15:37:13 +02002300static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2301{
2302 vmcs_write32(VM_EXIT_CONTROLS, val);
2303 vmx->vm_exit_controls_shadow = val;
2304}
2305
2306static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2307{
2308 if (vmx->vm_exit_controls_shadow != val)
2309 vm_exit_controls_init(vmx, val);
2310}
2311
2312static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2313{
2314 return vmx->vm_exit_controls_shadow;
2315}
2316
2317
2318static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2319{
2320 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2321}
2322
2323static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2324{
2325 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2326}
2327
Avi Kivity2fb92db2011-04-27 19:42:18 +03002328static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2329{
2330 vmx->segment_cache.bitmask = 0;
2331}
2332
2333static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2334 unsigned field)
2335{
2336 bool ret;
2337 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2338
2339 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2340 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2341 vmx->segment_cache.bitmask = 0;
2342 }
2343 ret = vmx->segment_cache.bitmask & mask;
2344 vmx->segment_cache.bitmask |= mask;
2345 return ret;
2346}
2347
2348static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2349{
2350 u16 *p = &vmx->segment_cache.seg[seg].selector;
2351
2352 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2353 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2354 return *p;
2355}
2356
2357static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2358{
2359 ulong *p = &vmx->segment_cache.seg[seg].base;
2360
2361 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2362 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2363 return *p;
2364}
2365
2366static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2367{
2368 u32 *p = &vmx->segment_cache.seg[seg].limit;
2369
2370 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2371 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2372 return *p;
2373}
2374
2375static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2376{
2377 u32 *p = &vmx->segment_cache.seg[seg].ar;
2378
2379 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2380 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2381 return *p;
2382}
2383
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002384static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2385{
2386 u32 eb;
2387
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002388 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002389 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002390 /*
2391 * Guest access to VMware backdoor ports could legitimately
2392 * trigger #GP because of TSS I/O permission bitmap.
2393 * We intercept those #GP and allow access to them anyway
2394 * as VMware does.
2395 */
2396 if (enable_vmware_backdoor)
2397 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002398 if ((vcpu->guest_debug &
2399 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2400 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2401 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002402 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002403 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002404 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002405 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002406
2407 /* When we are running a nested L2 guest and L1 specified for it a
2408 * certain exception bitmap, we must trap the same exceptions and pass
2409 * them to L1. When running L2, we will only handle the exceptions
2410 * specified above if L1 did not want them.
2411 */
2412 if (is_guest_mode(vcpu))
2413 eb |= get_vmcs12(vcpu)->exception_bitmap;
2414
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002415 vmcs_write32(EXCEPTION_BITMAP, eb);
2416}
2417
Ashok Raj15d45072018-02-01 22:59:43 +01002418/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002419 * Check if MSR is intercepted for currently loaded MSR bitmap.
2420 */
2421static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2422{
2423 unsigned long *msr_bitmap;
2424 int f = sizeof(unsigned long);
2425
2426 if (!cpu_has_vmx_msr_bitmap())
2427 return true;
2428
2429 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2430
2431 if (msr <= 0x1fff) {
2432 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2433 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2434 msr &= 0x1fff;
2435 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2436 }
2437
2438 return true;
2439}
2440
2441/*
Ashok Raj15d45072018-02-01 22:59:43 +01002442 * Check if MSR is intercepted for L01 MSR bitmap.
2443 */
2444static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2445{
2446 unsigned long *msr_bitmap;
2447 int f = sizeof(unsigned long);
2448
2449 if (!cpu_has_vmx_msr_bitmap())
2450 return true;
2451
2452 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2453
2454 if (msr <= 0x1fff) {
2455 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2456 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2457 msr &= 0x1fff;
2458 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2459 }
2460
2461 return true;
2462}
2463
Gleb Natapov2961e8762013-11-25 15:37:13 +02002464static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2465 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002466{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002467 vm_entry_controls_clearbit(vmx, entry);
2468 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002469}
2470
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002471static int find_msr(struct vmx_msrs *m, unsigned int msr)
2472{
2473 unsigned int i;
2474
2475 for (i = 0; i < m->nr; ++i) {
2476 if (m->val[i].index == msr)
2477 return i;
2478 }
2479 return -ENOENT;
2480}
2481
Avi Kivity61d2ef22010-04-28 16:40:38 +03002482static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2483{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002484 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002485 struct msr_autoload *m = &vmx->msr_autoload;
2486
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002487 switch (msr) {
2488 case MSR_EFER:
2489 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002490 clear_atomic_switch_msr_special(vmx,
2491 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002492 VM_EXIT_LOAD_IA32_EFER);
2493 return;
2494 }
2495 break;
2496 case MSR_CORE_PERF_GLOBAL_CTRL:
2497 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002498 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002499 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2500 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2501 return;
2502 }
2503 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002504 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002505 i = find_msr(&m->guest, msr);
2506 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002507 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002508 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002509 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002510 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002511
2512skip_guest:
2513 i = find_msr(&m->host, msr);
2514 if (i < 0)
2515 return;
2516
2517 --m->host.nr;
2518 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002519 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002520}
2521
Gleb Natapov2961e8762013-11-25 15:37:13 +02002522static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2523 unsigned long entry, unsigned long exit,
2524 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2525 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002526{
2527 vmcs_write64(guest_val_vmcs, guest_val);
2528 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002529 vm_entry_controls_setbit(vmx, entry);
2530 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002531}
2532
Avi Kivity61d2ef22010-04-28 16:40:38 +03002533static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002534 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002535{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002536 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002537 struct msr_autoload *m = &vmx->msr_autoload;
2538
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002539 switch (msr) {
2540 case MSR_EFER:
2541 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002542 add_atomic_switch_msr_special(vmx,
2543 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002544 VM_EXIT_LOAD_IA32_EFER,
2545 GUEST_IA32_EFER,
2546 HOST_IA32_EFER,
2547 guest_val, host_val);
2548 return;
2549 }
2550 break;
2551 case MSR_CORE_PERF_GLOBAL_CTRL:
2552 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002553 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002554 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2555 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2556 GUEST_IA32_PERF_GLOBAL_CTRL,
2557 HOST_IA32_PERF_GLOBAL_CTRL,
2558 guest_val, host_val);
2559 return;
2560 }
2561 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002562 case MSR_IA32_PEBS_ENABLE:
2563 /* PEBS needs a quiescent period after being disabled (to write
2564 * a record). Disabling PEBS through VMX MSR swapping doesn't
2565 * provide that period, so a CPU could write host's record into
2566 * guest's memory.
2567 */
2568 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002569 }
2570
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002571 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002572 if (!entry_only)
2573 j = find_msr(&m->host, msr);
2574
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002575 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002576 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002577 "Can't add msr %x\n", msr);
2578 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002579 }
2580 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002581 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002582 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002583 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002584 m->guest.val[i].index = msr;
2585 m->guest.val[i].value = guest_val;
2586
2587 if (entry_only)
2588 return;
2589
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002590 if (j < 0) {
2591 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002592 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002593 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002594 m->host.val[j].index = msr;
2595 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002596}
2597
Avi Kivity92c0d902009-10-29 11:00:16 +02002598static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002599{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002600 u64 guest_efer = vmx->vcpu.arch.efer;
2601 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002602
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002603 if (!enable_ept) {
2604 /*
2605 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2606 * host CPUID is more efficient than testing guest CPUID
2607 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2608 */
2609 if (boot_cpu_has(X86_FEATURE_SMEP))
2610 guest_efer |= EFER_NX;
2611 else if (!(guest_efer & EFER_NX))
2612 ignore_bits |= EFER_NX;
2613 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002614
Avi Kivity51c6cf62007-08-29 03:48:05 +03002615 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002616 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002617 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002618 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002619#ifdef CONFIG_X86_64
2620 ignore_bits |= EFER_LMA | EFER_LME;
2621 /* SCE is meaningful only in long mode on Intel */
2622 if (guest_efer & EFER_LMA)
2623 ignore_bits &= ~(u64)EFER_SCE;
2624#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002625
2626 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002627
2628 /*
2629 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2630 * On CPUs that support "load IA32_EFER", always switch EFER
2631 * atomically, since it's faster than switching it manually.
2632 */
2633 if (cpu_has_load_ia32_efer ||
2634 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002635 if (!(guest_efer & EFER_LMA))
2636 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002637 if (guest_efer != host_efer)
2638 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002639 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002640 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002641 } else {
2642 guest_efer &= ~ignore_bits;
2643 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002644
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002645 vmx->guest_msrs[efer_offset].data = guest_efer;
2646 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2647
2648 return true;
2649 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002650}
2651
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002652#ifdef CONFIG_X86_32
2653/*
2654 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2655 * VMCS rather than the segment table. KVM uses this helper to figure
2656 * out the current bases to poke them into the VMCS before entry.
2657 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002658static unsigned long segment_base(u16 selector)
2659{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002660 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002661 unsigned long v;
2662
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002663 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002664 return 0;
2665
Thomas Garnier45fc8752017-03-14 10:05:08 -07002666 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002667
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002668 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002669 u16 ldt_selector = kvm_read_ldt();
2670
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002671 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002672 return 0;
2673
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002674 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002675 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002676 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002677 return v;
2678}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002679#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002680
Avi Kivity04d2cc72007-09-10 18:10:54 +03002681static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002682{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002683 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002684#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002685 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002686#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002687 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002688
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002689 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002690 return;
2691
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002692 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002693 /*
2694 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2695 * allow segment selectors with cpl > 0 or ti == 1.
2696 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002697 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002698 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002699
2700#ifdef CONFIG_X86_64
2701 save_fsgs_for_kvm();
2702 vmx->host_state.fs_sel = current->thread.fsindex;
2703 vmx->host_state.gs_sel = current->thread.gsindex;
2704#else
Avi Kivity9581d442010-10-19 16:46:55 +02002705 savesegment(fs, vmx->host_state.fs_sel);
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002706 savesegment(gs, vmx->host_state.gs_sel);
2707#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002708 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002709 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002710 vmx->host_state.fs_reload_needed = 0;
2711 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002712 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002713 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002714 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002715 if (!(vmx->host_state.gs_sel & 7))
2716 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002717 else {
2718 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002719 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002720 }
2721
2722#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002723 savesegment(ds, vmx->host_state.ds_sel);
2724 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002725
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002726 vmcs_writel(HOST_FS_BASE, current->thread.fsbase);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002727 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002728
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002729 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Avi Kivityc8770e72010-11-11 12:37:26 +02002730 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002731 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002732#else
2733 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2734 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2735#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002736 if (boot_cpu_has(X86_FEATURE_MPX))
2737 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002738 for (i = 0; i < vmx->save_nmsrs; ++i)
2739 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002740 vmx->guest_msrs[i].data,
2741 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002742}
2743
Avi Kivitya9b21b62008-06-24 11:48:49 +03002744static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002745{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002746 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002747 return;
2748
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002749 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002750 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002751#ifdef CONFIG_X86_64
2752 if (is_long_mode(&vmx->vcpu))
2753 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2754#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002755 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002756 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002757#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002758 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002759#else
2760 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002761#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002762 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002763 if (vmx->host_state.fs_reload_needed)
2764 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002765#ifdef CONFIG_X86_64
2766 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2767 loadsegment(ds, vmx->host_state.ds_sel);
2768 loadsegment(es, vmx->host_state.es_sel);
2769 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002770#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002771 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002772#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002773 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002774#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002775 if (vmx->host_state.msr_host_bndcfgs)
2776 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Thomas Garnier45fc8752017-03-14 10:05:08 -07002777 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002778}
2779
Avi Kivitya9b21b62008-06-24 11:48:49 +03002780static void vmx_load_host_state(struct vcpu_vmx *vmx)
2781{
2782 preempt_disable();
2783 __vmx_load_host_state(vmx);
2784 preempt_enable();
2785}
2786
Feng Wu28b835d2015-09-18 22:29:54 +08002787static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2788{
2789 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2790 struct pi_desc old, new;
2791 unsigned int dest;
2792
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002793 /*
2794 * In case of hot-plug or hot-unplug, we may have to undo
2795 * vmx_vcpu_pi_put even if there is no assigned device. And we
2796 * always keep PI.NDST up to date for simplicity: it makes the
2797 * code easier, and CPU migration is not a fast path.
2798 */
2799 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002800 return;
2801
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002802 /*
2803 * First handle the simple case where no cmpxchg is necessary; just
2804 * allow posting non-urgent interrupts.
2805 *
2806 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2807 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2808 * expects the VCPU to be on the blocked_vcpu_list that matches
2809 * PI.NDST.
2810 */
2811 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2812 vcpu->cpu == cpu) {
2813 pi_clear_sn(pi_desc);
2814 return;
2815 }
2816
2817 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002818 do {
2819 old.control = new.control = pi_desc->control;
2820
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002821 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002822
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002823 if (x2apic_enabled())
2824 new.ndst = dest;
2825 else
2826 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002827
Feng Wu28b835d2015-09-18 22:29:54 +08002828 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002829 } while (cmpxchg64(&pi_desc->control, old.control,
2830 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002831}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002832
Peter Feinerc95ba922016-08-17 09:36:47 -07002833static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2834{
2835 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2836 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2837}
2838
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839/*
2840 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2841 * vcpu mutex is already taken.
2842 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002843static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002845 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002846 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002848 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002849 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002850 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002851 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002852
2853 /*
2854 * Read loaded_vmcs->cpu should be before fetching
2855 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2856 * See the comments in __loaded_vmcs_clear().
2857 */
2858 smp_rmb();
2859
Nadav Har'Eld462b812011-05-24 15:26:10 +03002860 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2861 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002862 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002863 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002864 }
2865
2866 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2867 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2868 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002869 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002870 }
2871
2872 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002873 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002874 unsigned long sysenter_esp;
2875
2876 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002877
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 /*
2879 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002880 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002882 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002883 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002884 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002886 /*
2887 * VM exits change the host TR limit to 0x67 after a VM
2888 * exit. This is okay, since 0x67 covers everything except
2889 * the IO bitmap and have have code to handle the IO bitmap
2890 * being lost after a VM exit.
2891 */
2892 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2893
Avi Kivity6aa8b732006-12-10 02:21:36 -08002894 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2895 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002896
Nadav Har'Eld462b812011-05-24 15:26:10 +03002897 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 }
Feng Wu28b835d2015-09-18 22:29:54 +08002899
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002900 /* Setup TSC multiplier */
2901 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002902 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2903 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002904
Feng Wu28b835d2015-09-18 22:29:54 +08002905 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002906 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002907 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002908}
2909
2910static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2911{
2912 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2913
2914 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002915 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2916 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002917 return;
2918
2919 /* Set SN when the vCPU is preempted */
2920 if (vcpu->preempted)
2921 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922}
2923
2924static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2925{
Feng Wu28b835d2015-09-18 22:29:54 +08002926 vmx_vcpu_pi_put(vcpu);
2927
Avi Kivitya9b21b62008-06-24 11:48:49 +03002928 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929}
2930
Wanpeng Lif244dee2017-07-20 01:11:54 -07002931static bool emulation_required(struct kvm_vcpu *vcpu)
2932{
2933 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2934}
2935
Avi Kivityedcafe32009-12-30 18:07:40 +02002936static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2937
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002938/*
2939 * Return the cr0 value that a nested guest would read. This is a combination
2940 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2941 * its hypervisor (cr0_read_shadow).
2942 */
2943static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2944{
2945 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2946 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2947}
2948static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2949{
2950 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2951 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2952}
2953
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2955{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002956 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002957
Avi Kivity6de12732011-03-07 12:51:22 +02002958 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2959 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2960 rflags = vmcs_readl(GUEST_RFLAGS);
2961 if (to_vmx(vcpu)->rmode.vm86_active) {
2962 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2963 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2964 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2965 }
2966 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002967 }
Avi Kivity6de12732011-03-07 12:51:22 +02002968 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969}
2970
2971static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2972{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002973 unsigned long old_rflags = vmx_get_rflags(vcpu);
2974
Avi Kivity6de12732011-03-07 12:51:22 +02002975 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2976 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002977 if (to_vmx(vcpu)->rmode.vm86_active) {
2978 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002979 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002980 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002982
2983 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2984 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985}
2986
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002987static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002988{
2989 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2990 int ret = 0;
2991
2992 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002993 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002994 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002995 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002996
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002997 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002998}
2999
3000static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3001{
3002 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3003 u32 interruptibility = interruptibility_old;
3004
3005 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3006
Jan Kiszka48005f62010-02-19 19:38:07 +01003007 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003008 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003009 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003010 interruptibility |= GUEST_INTR_STATE_STI;
3011
3012 if ((interruptibility != interruptibility_old))
3013 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3014}
3015
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3017{
3018 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003020 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003022 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003023
Glauber Costa2809f5d2009-05-12 16:21:05 -04003024 /* skipping an emulated instruction also counts */
3025 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026}
3027
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003028static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3029 unsigned long exit_qual)
3030{
3031 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3032 unsigned int nr = vcpu->arch.exception.nr;
3033 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3034
3035 if (vcpu->arch.exception.has_error_code) {
3036 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3037 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3038 }
3039
3040 if (kvm_exception_is_soft(nr))
3041 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3042 else
3043 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3044
3045 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3046 vmx_get_nmi_mask(vcpu))
3047 intr_info |= INTR_INFO_UNBLOCK_NMI;
3048
3049 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3050}
3051
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003052/*
3053 * KVM wants to inject page-faults which it got to the guest. This function
3054 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003055 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003056static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003057{
3058 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003059 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003060
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003061 if (nr == PF_VECTOR) {
3062 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003063 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003064 return 1;
3065 }
3066 /*
3067 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3068 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3069 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3070 * can be written only when inject_pending_event runs. This should be
3071 * conditional on a new capability---if the capability is disabled,
3072 * kvm_multiple_exception would write the ancillary information to
3073 * CR2 or DR6, for backwards ABI-compatibility.
3074 */
3075 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3076 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003077 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003078 return 1;
3079 }
3080 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003081 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003082 if (nr == DB_VECTOR)
3083 *exit_qual = vcpu->arch.dr6;
3084 else
3085 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003086 return 1;
3087 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003088 }
3089
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003090 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003091}
3092
Wanpeng Licaa057a2018-03-12 04:53:03 -07003093static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3094{
3095 /*
3096 * Ensure that we clear the HLT state in the VMCS. We don't need to
3097 * explicitly skip the instruction because if the HLT state is set,
3098 * then the instruction is already executing and RIP has already been
3099 * advanced.
3100 */
3101 if (kvm_hlt_in_guest(vcpu->kvm) &&
3102 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3103 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3104}
3105
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003106static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003107{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003108 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003109 unsigned nr = vcpu->arch.exception.nr;
3110 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003111 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003112 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003113
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003114 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003115 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003116 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3117 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003118
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003119 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003120 int inc_eip = 0;
3121 if (kvm_exception_is_soft(nr))
3122 inc_eip = vcpu->arch.event_exit_inst_len;
3123 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003124 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003125 return;
3126 }
3127
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003128 WARN_ON_ONCE(vmx->emulation_required);
3129
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003130 if (kvm_exception_is_soft(nr)) {
3131 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3132 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003133 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3134 } else
3135 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3136
3137 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003138
3139 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003140}
3141
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003142static bool vmx_rdtscp_supported(void)
3143{
3144 return cpu_has_vmx_rdtscp();
3145}
3146
Mao, Junjiead756a12012-07-02 01:18:48 +00003147static bool vmx_invpcid_supported(void)
3148{
3149 return cpu_has_vmx_invpcid() && enable_ept;
3150}
3151
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152/*
Eddie Donga75beee2007-05-17 18:55:15 +03003153 * Swap MSR entry in host/guest MSR entry array.
3154 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003155static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003156{
Avi Kivity26bb0982009-09-07 11:14:12 +03003157 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003158
3159 tmp = vmx->guest_msrs[to];
3160 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3161 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003162}
3163
3164/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003165 * Set up the vmcs to automatically save and restore system
3166 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3167 * mode, as fiddling with msrs is very expensive.
3168 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003169static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003170{
Avi Kivity26bb0982009-09-07 11:14:12 +03003171 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003172
Eddie Donga75beee2007-05-17 18:55:15 +03003173 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003174#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003175 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003176 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003177 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003178 move_msr_up(vmx, index, save_nmsrs++);
3179 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003180 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003181 move_msr_up(vmx, index, save_nmsrs++);
3182 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003183 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003184 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003185 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003186 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003187 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003188 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003189 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003190 * if efer.sce is enabled.
3191 */
Brian Gerst8c065852010-07-17 09:03:26 -04003192 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003193 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003194 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003195 }
Eddie Donga75beee2007-05-17 18:55:15 +03003196#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003197 index = __find_msr_index(vmx, MSR_EFER);
3198 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003199 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003200
Avi Kivity26bb0982009-09-07 11:14:12 +03003201 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003202
Yang Zhang8d146952013-01-25 10:18:50 +08003203 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003204 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003205}
3206
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003207static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003209 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003211 if (is_guest_mode(vcpu) &&
3212 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3213 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3214
3215 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216}
3217
3218/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003219 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003221static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003223 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003224 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003225 * We're here if L1 chose not to trap WRMSR to TSC. According
3226 * to the spec, this should set L1's TSC; The offset that L1
3227 * set for L2 remains unchanged, and still needs to be added
3228 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003229 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003230 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003231 /* recalculate vmcs02.TSC_OFFSET: */
3232 vmcs12 = get_vmcs12(vcpu);
3233 vmcs_write64(TSC_OFFSET, offset +
3234 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3235 vmcs12->tsc_offset : 0));
3236 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003237 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3238 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003239 vmcs_write64(TSC_OFFSET, offset);
3240 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241}
3242
Nadav Har'El801d3422011-05-25 23:02:23 +03003243/*
3244 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3245 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3246 * all guests if the "nested" module option is off, and can also be disabled
3247 * for a single guest by disabling its VMX cpuid bit.
3248 */
3249static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3250{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003251 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003252}
3253
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003255 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3256 * returned for the various VMX controls MSRs when nested VMX is enabled.
3257 * The same values should also be used to verify that vmcs12 control fields are
3258 * valid during nested entry from L1 to L2.
3259 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3260 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3261 * bit in the high half is on if the corresponding bit in the control field
3262 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003263 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003264static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003265{
Paolo Bonzini13893092018-02-26 13:40:09 +01003266 if (!nested) {
3267 memset(msrs, 0, sizeof(*msrs));
3268 return;
3269 }
3270
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003271 /*
3272 * Note that as a general rule, the high half of the MSRs (bits in
3273 * the control fields which may be 1) should be initialized by the
3274 * intersection of the underlying hardware's MSR (i.e., features which
3275 * can be supported) and the list of features we want to expose -
3276 * because they are known to be properly supported in our code.
3277 * Also, usually, the low half of the MSRs (bits which must be 1) can
3278 * be set to 0, meaning that L1 may turn off any of these bits. The
3279 * reason is that if one of these bits is necessary, it will appear
3280 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3281 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003282 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003283 * These rules have exceptions below.
3284 */
3285
3286 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003287 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003288 msrs->pinbased_ctls_low,
3289 msrs->pinbased_ctls_high);
3290 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003291 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003292 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003293 PIN_BASED_EXT_INTR_MASK |
3294 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003295 PIN_BASED_VIRTUAL_NMIS |
3296 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003297 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003298 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003299 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003300
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003301 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003302 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003303 msrs->exit_ctls_low,
3304 msrs->exit_ctls_high);
3305 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003306 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003307
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003308 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003309#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003310 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003311#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003312 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003313 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003314 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003315 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003316 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3317
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003318 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003319 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003320
Jan Kiszka2996fca2014-06-16 13:59:43 +02003321 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003323
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003324 /* entry controls */
3325 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003326 msrs->entry_ctls_low,
3327 msrs->entry_ctls_high);
3328 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003329 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003330 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003331#ifdef CONFIG_X86_64
3332 VM_ENTRY_IA32E_MODE |
3333#endif
3334 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003335 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003336 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003337 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003338 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003339
Jan Kiszka2996fca2014-06-16 13:59:43 +02003340 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003341 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003342
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003343 /* cpu-based controls */
3344 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003345 msrs->procbased_ctls_low,
3346 msrs->procbased_ctls_high);
3347 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003348 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003349 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003350 CPU_BASED_VIRTUAL_INTR_PENDING |
3351 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003352 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3353 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3354 CPU_BASED_CR3_STORE_EXITING |
3355#ifdef CONFIG_X86_64
3356 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3357#endif
3358 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003359 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3360 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3361 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3362 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003363 /*
3364 * We can allow some features even when not supported by the
3365 * hardware. For example, L1 can specify an MSR bitmap - and we
3366 * can use it to avoid exits to L1 - even when L0 runs L2
3367 * without MSR bitmaps.
3368 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003369 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003370 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003371 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003372
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003373 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003374 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003375 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3376
Paolo Bonzini80154d72017-08-24 13:55:35 +02003377 /*
3378 * secondary cpu-based controls. Do not include those that
3379 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3380 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003381 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003382 msrs->secondary_ctls_low,
3383 msrs->secondary_ctls_high);
3384 msrs->secondary_ctls_low = 0;
3385 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003386 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003387 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003388 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003389 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003390 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003391 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003392
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003393 if (enable_ept) {
3394 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003395 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003396 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003397 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003398 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003399 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003400 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003401 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003402 msrs->ept_caps &= vmx_capability.ept;
3403 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003404 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3405 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003406 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003407 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003408 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003409 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003410 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003411 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003412
Bandan Das27c42a12017-08-03 15:54:42 -04003413 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003414 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003415 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003416 /*
3417 * Advertise EPTP switching unconditionally
3418 * since we emulate it
3419 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003420 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003421 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003422 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003423 }
3424
Paolo Bonzinief697a72016-03-18 16:58:38 +01003425 /*
3426 * Old versions of KVM use the single-context version without
3427 * checking for support, so declare that it is supported even
3428 * though it is treated as global context. The alternative is
3429 * not failing the single-context invvpid, and it is worse.
3430 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003431 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003432 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003433 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003434 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003435 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003436 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003437
Radim Krčmář0790ec12015-03-17 14:02:32 +01003438 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003439 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003440 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3441
Jan Kiszkac18911a2013-03-13 16:06:41 +01003442 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003443 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003444 msrs->misc_low,
3445 msrs->misc_high);
3446 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3447 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003448 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003449 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003450 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003451 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003452
3453 /*
3454 * This MSR reports some information about VMX support. We
3455 * should return information about the VMX we emulate for the
3456 * guest, and the VMCS structure we give it - not about the
3457 * VMX support of the underlying hardware.
3458 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003459 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003460 VMCS12_REVISION |
3461 VMX_BASIC_TRUE_CTLS |
3462 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3463 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3464
3465 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003466 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003467
3468 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003469 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003470 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3471 * We picked the standard core2 setting.
3472 */
3473#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3474#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003475 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3476 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003477
3478 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003479 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3480 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003481
3482 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003483 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003484}
3485
David Matlack38991522016-11-29 18:14:08 -08003486/*
3487 * if fixed0[i] == 1: val[i] must be 1
3488 * if fixed1[i] == 0: val[i] must be 0
3489 */
3490static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3491{
3492 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003493}
3494
3495static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3496{
David Matlack38991522016-11-29 18:14:08 -08003497 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003498}
3499
3500static inline u64 vmx_control_msr(u32 low, u32 high)
3501{
3502 return low | ((u64)high << 32);
3503}
3504
David Matlack62cc6b9d2016-11-29 18:14:07 -08003505static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3506{
3507 superset &= mask;
3508 subset &= mask;
3509
3510 return (superset | subset) == superset;
3511}
3512
3513static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3514{
3515 const u64 feature_and_reserved =
3516 /* feature (except bit 48; see below) */
3517 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3518 /* reserved */
3519 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003520 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003521
3522 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3523 return -EINVAL;
3524
3525 /*
3526 * KVM does not emulate a version of VMX that constrains physical
3527 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3528 */
3529 if (data & BIT_ULL(48))
3530 return -EINVAL;
3531
3532 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3533 vmx_basic_vmcs_revision_id(data))
3534 return -EINVAL;
3535
3536 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3537 return -EINVAL;
3538
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003539 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003540 return 0;
3541}
3542
3543static int
3544vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3545{
3546 u64 supported;
3547 u32 *lowp, *highp;
3548
3549 switch (msr_index) {
3550 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003551 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3552 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003553 break;
3554 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003555 lowp = &vmx->nested.msrs.procbased_ctls_low;
3556 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003557 break;
3558 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003559 lowp = &vmx->nested.msrs.exit_ctls_low;
3560 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003561 break;
3562 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003563 lowp = &vmx->nested.msrs.entry_ctls_low;
3564 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003565 break;
3566 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003567 lowp = &vmx->nested.msrs.secondary_ctls_low;
3568 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003569 break;
3570 default:
3571 BUG();
3572 }
3573
3574 supported = vmx_control_msr(*lowp, *highp);
3575
3576 /* Check must-be-1 bits are still 1. */
3577 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3578 return -EINVAL;
3579
3580 /* Check must-be-0 bits are still 0. */
3581 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3582 return -EINVAL;
3583
3584 *lowp = data;
3585 *highp = data >> 32;
3586 return 0;
3587}
3588
3589static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3590{
3591 const u64 feature_and_reserved_bits =
3592 /* feature */
3593 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3594 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3595 /* reserved */
3596 GENMASK_ULL(13, 9) | BIT_ULL(31);
3597 u64 vmx_misc;
3598
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003599 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3600 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003601
3602 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3603 return -EINVAL;
3604
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003605 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003606 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3607 vmx_misc_preemption_timer_rate(data) !=
3608 vmx_misc_preemption_timer_rate(vmx_misc))
3609 return -EINVAL;
3610
3611 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3612 return -EINVAL;
3613
3614 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3615 return -EINVAL;
3616
3617 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3618 return -EINVAL;
3619
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003620 vmx->nested.msrs.misc_low = data;
3621 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003622
3623 /*
3624 * If L1 has read-only VM-exit information fields, use the
3625 * less permissive vmx_vmwrite_bitmap to specify write
3626 * permissions for the shadow VMCS.
3627 */
3628 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3629 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3630
David Matlack62cc6b9d2016-11-29 18:14:07 -08003631 return 0;
3632}
3633
3634static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3635{
3636 u64 vmx_ept_vpid_cap;
3637
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003638 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3639 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003640
3641 /* Every bit is either reserved or a feature bit. */
3642 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3643 return -EINVAL;
3644
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003645 vmx->nested.msrs.ept_caps = data;
3646 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003647 return 0;
3648}
3649
3650static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3651{
3652 u64 *msr;
3653
3654 switch (msr_index) {
3655 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003656 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003657 break;
3658 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003659 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003660 break;
3661 default:
3662 BUG();
3663 }
3664
3665 /*
3666 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3667 * must be 1 in the restored value.
3668 */
3669 if (!is_bitwise_subset(data, *msr, -1ULL))
3670 return -EINVAL;
3671
3672 *msr = data;
3673 return 0;
3674}
3675
3676/*
3677 * Called when userspace is restoring VMX MSRs.
3678 *
3679 * Returns 0 on success, non-0 otherwise.
3680 */
3681static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3682{
3683 struct vcpu_vmx *vmx = to_vmx(vcpu);
3684
Jim Mattsona943ac52018-05-29 09:11:32 -07003685 /*
3686 * Don't allow changes to the VMX capability MSRs while the vCPU
3687 * is in VMX operation.
3688 */
3689 if (vmx->nested.vmxon)
3690 return -EBUSY;
3691
David Matlack62cc6b9d2016-11-29 18:14:07 -08003692 switch (msr_index) {
3693 case MSR_IA32_VMX_BASIC:
3694 return vmx_restore_vmx_basic(vmx, data);
3695 case MSR_IA32_VMX_PINBASED_CTLS:
3696 case MSR_IA32_VMX_PROCBASED_CTLS:
3697 case MSR_IA32_VMX_EXIT_CTLS:
3698 case MSR_IA32_VMX_ENTRY_CTLS:
3699 /*
3700 * The "non-true" VMX capability MSRs are generated from the
3701 * "true" MSRs, so we do not support restoring them directly.
3702 *
3703 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3704 * should restore the "true" MSRs with the must-be-1 bits
3705 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3706 * DEFAULT SETTINGS".
3707 */
3708 return -EINVAL;
3709 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3710 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3711 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3712 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3713 case MSR_IA32_VMX_PROCBASED_CTLS2:
3714 return vmx_restore_control_msr(vmx, msr_index, data);
3715 case MSR_IA32_VMX_MISC:
3716 return vmx_restore_vmx_misc(vmx, data);
3717 case MSR_IA32_VMX_CR0_FIXED0:
3718 case MSR_IA32_VMX_CR4_FIXED0:
3719 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3720 case MSR_IA32_VMX_CR0_FIXED1:
3721 case MSR_IA32_VMX_CR4_FIXED1:
3722 /*
3723 * These MSRs are generated based on the vCPU's CPUID, so we
3724 * do not support restoring them directly.
3725 */
3726 return -EINVAL;
3727 case MSR_IA32_VMX_EPT_VPID_CAP:
3728 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3729 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003730 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003731 return 0;
3732 default:
3733 /*
3734 * The rest of the VMX capability MSRs do not support restore.
3735 */
3736 return -EINVAL;
3737 }
3738}
3739
Jan Kiszkacae50132014-01-04 18:47:22 +01003740/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003741static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003742{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003743 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003744 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003745 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003746 break;
3747 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3748 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003749 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003750 msrs->pinbased_ctls_low,
3751 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003752 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3753 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003754 break;
3755 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3756 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003757 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003758 msrs->procbased_ctls_low,
3759 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003760 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3761 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003762 break;
3763 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3764 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003765 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003766 msrs->exit_ctls_low,
3767 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003768 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3769 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003770 break;
3771 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3772 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003773 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003774 msrs->entry_ctls_low,
3775 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003776 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3777 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003778 break;
3779 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003780 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003781 msrs->misc_low,
3782 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003783 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003784 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003785 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003786 break;
3787 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003788 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003789 break;
3790 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003791 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003792 break;
3793 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003794 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003795 break;
3796 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003797 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003798 break;
3799 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003800 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003801 msrs->secondary_ctls_low,
3802 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003803 break;
3804 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003805 *pdata = msrs->ept_caps |
3806 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003807 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003808 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003809 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003810 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003811 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003812 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003813 }
3814
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003815 return 0;
3816}
3817
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003818static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3819 uint64_t val)
3820{
3821 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3822
3823 return !(val & ~valid_bits);
3824}
3825
Tom Lendacky801e4592018-02-21 13:39:51 -06003826static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3827{
Paolo Bonzini13893092018-02-26 13:40:09 +01003828 switch (msr->index) {
3829 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3830 if (!nested)
3831 return 1;
3832 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3833 default:
3834 return 1;
3835 }
3836
3837 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003838}
3839
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003840/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841 * Reads an msr value (of 'msr_index') into 'pdata'.
3842 * Returns 0 on success, non-0 otherwise.
3843 * Assumes vcpu_load() was already called.
3844 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003845static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003846{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003847 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003848 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003850 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003851#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003853 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854 break;
3855 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003856 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003858 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003859 vmx_load_host_state(vmx);
3860 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003861 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003862#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003863 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003864 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003865 case MSR_IA32_SPEC_CTRL:
3866 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003867 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3868 return 1;
3869
3870 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3871 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003872 case MSR_IA32_ARCH_CAPABILITIES:
3873 if (!msr_info->host_initiated &&
3874 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3875 return 1;
3876 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3877 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003879 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880 break;
3881 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003882 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003883 break;
3884 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003885 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003886 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003887 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003888 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003889 (!msr_info->host_initiated &&
3890 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003891 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003892 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003893 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003894 case MSR_IA32_MCG_EXT_CTL:
3895 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003896 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003897 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003898 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003899 msr_info->data = vcpu->arch.mcg_ext_ctl;
3900 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003901 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003902 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003903 break;
3904 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3905 if (!nested_vmx_allowed(vcpu))
3906 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003907 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3908 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003909 case MSR_IA32_XSS:
3910 if (!vmx_xsaves_supported())
3911 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003912 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003913 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003914 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003915 if (!msr_info->host_initiated &&
3916 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003917 return 1;
3918 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003920 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003921 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003922 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003923 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003925 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003926 }
3927
Avi Kivity6aa8b732006-12-10 02:21:36 -08003928 return 0;
3929}
3930
Jan Kiszkacae50132014-01-04 18:47:22 +01003931static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3932
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933/*
3934 * Writes msr value into into the appropriate "register".
3935 * Returns 0 on success, non-0 otherwise.
3936 * Assumes vcpu_load() was already called.
3937 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003938static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003940 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003941 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003942 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003943 u32 msr_index = msr_info->index;
3944 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003945
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003947 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003948 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003949 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003950#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003952 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953 vmcs_writel(GUEST_FS_BASE, data);
3954 break;
3955 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003956 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957 vmcs_writel(GUEST_GS_BASE, data);
3958 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003959 case MSR_KERNEL_GS_BASE:
3960 vmx_load_host_state(vmx);
3961 vmx->msr_guest_kernel_gs_base = data;
3962 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003963#endif
3964 case MSR_IA32_SYSENTER_CS:
3965 vmcs_write32(GUEST_SYSENTER_CS, data);
3966 break;
3967 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003968 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003969 break;
3970 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003971 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003972 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003973 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003974 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003975 (!msr_info->host_initiated &&
3976 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003977 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003978 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003979 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003980 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003981 vmcs_write64(GUEST_BNDCFGS, data);
3982 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003983 case MSR_IA32_SPEC_CTRL:
3984 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003985 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3986 return 1;
3987
3988 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003989 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003990 return 1;
3991
3992 vmx->spec_ctrl = data;
3993
3994 if (!data)
3995 break;
3996
3997 /*
3998 * For non-nested:
3999 * When it's written (to non-zero) for the first time, pass
4000 * it through.
4001 *
4002 * For nested:
4003 * The handling of the MSR bitmap for L2 guests is done in
4004 * nested_vmx_merge_msr_bitmap. We should not touch the
4005 * vmcs02.msr_bitmap here since it gets completely overwritten
4006 * in the merging. We update the vmcs01 here for L1 as well
4007 * since it will end up touching the MSR anyway now.
4008 */
4009 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4010 MSR_IA32_SPEC_CTRL,
4011 MSR_TYPE_RW);
4012 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004013 case MSR_IA32_PRED_CMD:
4014 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004015 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4016 return 1;
4017
4018 if (data & ~PRED_CMD_IBPB)
4019 return 1;
4020
4021 if (!data)
4022 break;
4023
4024 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4025
4026 /*
4027 * For non-nested:
4028 * When it's written (to non-zero) for the first time, pass
4029 * it through.
4030 *
4031 * For nested:
4032 * The handling of the MSR bitmap for L2 guests is done in
4033 * nested_vmx_merge_msr_bitmap. We should not touch the
4034 * vmcs02.msr_bitmap here since it gets completely overwritten
4035 * in the merging.
4036 */
4037 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4038 MSR_TYPE_W);
4039 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004040 case MSR_IA32_ARCH_CAPABILITIES:
4041 if (!msr_info->host_initiated)
4042 return 1;
4043 vmx->arch_capabilities = data;
4044 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004045 case MSR_IA32_CR_PAT:
4046 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004047 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4048 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004049 vmcs_write64(GUEST_IA32_PAT, data);
4050 vcpu->arch.pat = data;
4051 break;
4052 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004053 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004054 break;
Will Auldba904632012-11-29 12:42:50 -08004055 case MSR_IA32_TSC_ADJUST:
4056 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004057 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004058 case MSR_IA32_MCG_EXT_CTL:
4059 if ((!msr_info->host_initiated &&
4060 !(to_vmx(vcpu)->msr_ia32_feature_control &
4061 FEATURE_CONTROL_LMCE)) ||
4062 (data & ~MCG_EXT_CTL_LMCE_EN))
4063 return 1;
4064 vcpu->arch.mcg_ext_ctl = data;
4065 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004066 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004067 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004068 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004069 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4070 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004071 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004072 if (msr_info->host_initiated && data == 0)
4073 vmx_leave_nested(vcpu);
4074 break;
4075 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004076 if (!msr_info->host_initiated)
4077 return 1; /* they are read-only */
4078 if (!nested_vmx_allowed(vcpu))
4079 return 1;
4080 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004081 case MSR_IA32_XSS:
4082 if (!vmx_xsaves_supported())
4083 return 1;
4084 /*
4085 * The only supported bit as of Skylake is bit 8, but
4086 * it is not supported on KVM.
4087 */
4088 if (data != 0)
4089 return 1;
4090 vcpu->arch.ia32_xss = data;
4091 if (vcpu->arch.ia32_xss != host_xss)
4092 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004093 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004094 else
4095 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4096 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004097 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004098 if (!msr_info->host_initiated &&
4099 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004100 return 1;
4101 /* Check reserved bit, higher 32 bits should be zero */
4102 if ((data >> 32) != 0)
4103 return 1;
4104 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004106 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004107 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004108 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004109 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004110 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4111 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004112 ret = kvm_set_shared_msr(msr->index, msr->data,
4113 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004114 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004115 if (ret)
4116 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004117 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004118 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004120 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004121 }
4122
Eddie Dong2cc51562007-05-21 07:28:09 +03004123 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004124}
4125
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004126static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004127{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004128 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4129 switch (reg) {
4130 case VCPU_REGS_RSP:
4131 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4132 break;
4133 case VCPU_REGS_RIP:
4134 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4135 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004136 case VCPU_EXREG_PDPTR:
4137 if (enable_ept)
4138 ept_save_pdptrs(vcpu);
4139 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004140 default:
4141 break;
4142 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143}
4144
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145static __init int cpu_has_kvm_support(void)
4146{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004147 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148}
4149
4150static __init int vmx_disabled_by_bios(void)
4151{
4152 u64 msr;
4153
4154 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004155 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004156 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004157 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4158 && tboot_enabled())
4159 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004160 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004161 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004162 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004163 && !tboot_enabled()) {
4164 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004165 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004166 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004167 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004168 /* launched w/o TXT and VMX disabled */
4169 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4170 && !tboot_enabled())
4171 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004172 }
4173
4174 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175}
4176
Dongxiao Xu7725b892010-05-11 18:29:38 +08004177static void kvm_cpu_vmxon(u64 addr)
4178{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004179 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004180 intel_pt_handle_vmx(1);
4181
Dongxiao Xu7725b892010-05-11 18:29:38 +08004182 asm volatile (ASM_VMX_VMXON_RAX
4183 : : "a"(&addr), "m"(addr)
4184 : "memory", "cc");
4185}
4186
Radim Krčmář13a34e02014-08-28 15:13:03 +02004187static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188{
4189 int cpu = raw_smp_processor_id();
4190 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004191 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004192
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004193 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004194 return -EBUSY;
4195
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004196 /*
4197 * This can happen if we hot-added a CPU but failed to allocate
4198 * VP assist page for it.
4199 */
4200 if (static_branch_unlikely(&enable_evmcs) &&
4201 !hv_get_vp_assist_page(cpu))
4202 return -EFAULT;
4203
Nadav Har'Eld462b812011-05-24 15:26:10 +03004204 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004205 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4206 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004207
4208 /*
4209 * Now we can enable the vmclear operation in kdump
4210 * since the loaded_vmcss_on_cpu list on this cpu
4211 * has been initialized.
4212 *
4213 * Though the cpu is not in VMX operation now, there
4214 * is no problem to enable the vmclear operation
4215 * for the loaded_vmcss_on_cpu list is empty!
4216 */
4217 crash_enable_local_vmclear(cpu);
4218
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004220
4221 test_bits = FEATURE_CONTROL_LOCKED;
4222 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4223 if (tboot_enabled())
4224 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4225
4226 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004227 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004228 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4229 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004230 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004231 if (enable_ept)
4232 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004233
4234 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235}
4236
Nadav Har'Eld462b812011-05-24 15:26:10 +03004237static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004238{
4239 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004240 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004241
Nadav Har'Eld462b812011-05-24 15:26:10 +03004242 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4243 loaded_vmcss_on_cpu_link)
4244 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004245}
4246
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004247
4248/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4249 * tricks.
4250 */
4251static void kvm_cpu_vmxoff(void)
4252{
4253 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004254
4255 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004256 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004257}
4258
Radim Krčmář13a34e02014-08-28 15:13:03 +02004259static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004260{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004261 vmclear_local_loaded_vmcss();
4262 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263}
4264
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004265static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004266 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004267{
4268 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004269 u32 ctl = ctl_min | ctl_opt;
4270
4271 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4272
4273 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4274 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4275
4276 /* Ensure minimum (required) set of control bits are supported. */
4277 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004278 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004279
4280 *result = ctl;
4281 return 0;
4282}
4283
Avi Kivity110312c2010-12-21 12:54:20 +02004284static __init bool allow_1_setting(u32 msr, u32 ctl)
4285{
4286 u32 vmx_msr_low, vmx_msr_high;
4287
4288 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4289 return vmx_msr_high & ctl;
4290}
4291
Yang, Sheng002c7f72007-07-31 14:23:01 +03004292static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004293{
4294 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004295 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004296 u32 _pin_based_exec_control = 0;
4297 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004298 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004299 u32 _vmexit_control = 0;
4300 u32 _vmentry_control = 0;
4301
Paolo Bonzini13893092018-02-26 13:40:09 +01004302 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304303 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004304#ifdef CONFIG_X86_64
4305 CPU_BASED_CR8_LOAD_EXITING |
4306 CPU_BASED_CR8_STORE_EXITING |
4307#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004308 CPU_BASED_CR3_LOAD_EXITING |
4309 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004310 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004311 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004312 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004313 CPU_BASED_MWAIT_EXITING |
4314 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004315 CPU_BASED_INVLPG_EXITING |
4316 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004317
Sheng Yangf78e0e22007-10-29 09:40:42 +08004318 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004319 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004320 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004321 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4322 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004323 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004324#ifdef CONFIG_X86_64
4325 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4326 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4327 ~CPU_BASED_CR8_STORE_EXITING;
4328#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004329 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004330 min2 = 0;
4331 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004332 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004333 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004334 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004335 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004336 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004337 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004338 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004339 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004340 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004341 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004342 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004343 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004344 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004345 SECONDARY_EXEC_RDSEED_EXITING |
4346 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004347 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004348 SECONDARY_EXEC_TSC_SCALING |
4349 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004350 if (adjust_vmx_controls(min2, opt2,
4351 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004352 &_cpu_based_2nd_exec_control) < 0)
4353 return -EIO;
4354 }
4355#ifndef CONFIG_X86_64
4356 if (!(_cpu_based_2nd_exec_control &
4357 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4358 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4359#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004360
4361 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4362 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004363 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004364 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4365 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004366
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004367 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4368 &vmx_capability.ept, &vmx_capability.vpid);
4369
Sheng Yangd56f5462008-04-25 10:13:16 +08004370 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004371 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4372 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004373 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4374 CPU_BASED_CR3_STORE_EXITING |
4375 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004376 } else if (vmx_capability.ept) {
4377 vmx_capability.ept = 0;
4378 pr_warn_once("EPT CAP should not exist if not support "
4379 "1-setting enable EPT VM-execution control\n");
4380 }
4381 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4382 vmx_capability.vpid) {
4383 vmx_capability.vpid = 0;
4384 pr_warn_once("VPID CAP should not exist if not support "
4385 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004386 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004387
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004388 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004389#ifdef CONFIG_X86_64
4390 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4391#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004392 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004393 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004394 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4395 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004396 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004397
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004398 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4399 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4400 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004401 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4402 &_pin_based_exec_control) < 0)
4403 return -EIO;
4404
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004405 if (cpu_has_broken_vmx_preemption_timer())
4406 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004407 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004408 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004409 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4410
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004411 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004412 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004413 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4414 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004415 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004416
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004417 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004418
4419 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4420 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004421 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004422
4423#ifdef CONFIG_X86_64
4424 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4425 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004426 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004427#endif
4428
4429 /* Require Write-Back (WB) memory type for VMCS accesses. */
4430 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004431 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004432
Yang, Sheng002c7f72007-07-31 14:23:01 +03004433 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004434 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004435 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004436
4437 /* KVM supports Enlightened VMCS v1 only */
4438 if (static_branch_unlikely(&enable_evmcs))
4439 vmcs_conf->revision_id = KVM_EVMCS_VERSION;
4440 else
4441 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004442
Yang, Sheng002c7f72007-07-31 14:23:01 +03004443 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4444 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004445 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004446 vmcs_conf->vmexit_ctrl = _vmexit_control;
4447 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004448
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004449 if (static_branch_unlikely(&enable_evmcs))
4450 evmcs_sanitize_exec_ctrls(vmcs_conf);
4451
Avi Kivity110312c2010-12-21 12:54:20 +02004452 cpu_has_load_ia32_efer =
4453 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4454 VM_ENTRY_LOAD_IA32_EFER)
4455 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4456 VM_EXIT_LOAD_IA32_EFER);
4457
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004458 cpu_has_load_perf_global_ctrl =
4459 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4460 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4461 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4462 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4463
4464 /*
4465 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004466 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004467 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4468 *
4469 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4470 *
4471 * AAK155 (model 26)
4472 * AAP115 (model 30)
4473 * AAT100 (model 37)
4474 * BC86,AAY89,BD102 (model 44)
4475 * BA97 (model 46)
4476 *
4477 */
4478 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4479 switch (boot_cpu_data.x86_model) {
4480 case 26:
4481 case 30:
4482 case 37:
4483 case 44:
4484 case 46:
4485 cpu_has_load_perf_global_ctrl = false;
4486 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4487 "does not work properly. Using workaround\n");
4488 break;
4489 default:
4490 break;
4491 }
4492 }
4493
Borislav Petkov782511b2016-04-04 22:25:03 +02004494 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004495 rdmsrl(MSR_IA32_XSS, host_xss);
4496
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004497 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004498}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499
4500static struct vmcs *alloc_vmcs_cpu(int cpu)
4501{
4502 int node = cpu_to_node(cpu);
4503 struct page *pages;
4504 struct vmcs *vmcs;
4505
Vlastimil Babka96db8002015-09-08 15:03:50 -07004506 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004507 if (!pages)
4508 return NULL;
4509 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004510 memset(vmcs, 0, vmcs_config.size);
4511 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512 return vmcs;
4513}
4514
Avi Kivity6aa8b732006-12-10 02:21:36 -08004515static void free_vmcs(struct vmcs *vmcs)
4516{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004517 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004518}
4519
Nadav Har'Eld462b812011-05-24 15:26:10 +03004520/*
4521 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4522 */
4523static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4524{
4525 if (!loaded_vmcs->vmcs)
4526 return;
4527 loaded_vmcs_clear(loaded_vmcs);
4528 free_vmcs(loaded_vmcs->vmcs);
4529 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004530 if (loaded_vmcs->msr_bitmap)
4531 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004532 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004533}
4534
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004535static struct vmcs *alloc_vmcs(void)
4536{
4537 return alloc_vmcs_cpu(raw_smp_processor_id());
4538}
4539
4540static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4541{
4542 loaded_vmcs->vmcs = alloc_vmcs();
4543 if (!loaded_vmcs->vmcs)
4544 return -ENOMEM;
4545
4546 loaded_vmcs->shadow_vmcs = NULL;
4547 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004548
4549 if (cpu_has_vmx_msr_bitmap()) {
4550 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4551 if (!loaded_vmcs->msr_bitmap)
4552 goto out_vmcs;
4553 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004554
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004555 if (IS_ENABLED(CONFIG_HYPERV) &&
4556 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004557 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4558 struct hv_enlightened_vmcs *evmcs =
4559 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4560
4561 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4562 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004563 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004564 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004565
4566out_vmcs:
4567 free_loaded_vmcs(loaded_vmcs);
4568 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004569}
4570
Sam Ravnborg39959582007-06-01 00:47:13 -07004571static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004572{
4573 int cpu;
4574
Zachary Amsden3230bb42009-09-29 11:38:37 -10004575 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004577 per_cpu(vmxarea, cpu) = NULL;
4578 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004579}
4580
Jim Mattsond37f4262017-12-22 12:12:16 -08004581enum vmcs_field_width {
4582 VMCS_FIELD_WIDTH_U16 = 0,
4583 VMCS_FIELD_WIDTH_U64 = 1,
4584 VMCS_FIELD_WIDTH_U32 = 2,
4585 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004586};
4587
Jim Mattsond37f4262017-12-22 12:12:16 -08004588static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004589{
4590 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004591 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004592 return (field >> 13) & 0x3 ;
4593}
4594
4595static inline int vmcs_field_readonly(unsigned long field)
4596{
4597 return (((field >> 10) & 0x3) == 1);
4598}
4599
Bandan Dasfe2b2012014-04-21 15:20:14 -04004600static void init_vmcs_shadow_fields(void)
4601{
4602 int i, j;
4603
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004604 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4605 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004606 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004607 (i + 1 == max_shadow_read_only_fields ||
4608 shadow_read_only_fields[i + 1] != field + 1))
4609 pr_err("Missing field from shadow_read_only_field %x\n",
4610 field + 1);
4611
4612 clear_bit(field, vmx_vmread_bitmap);
4613#ifdef CONFIG_X86_64
4614 if (field & 1)
4615 continue;
4616#endif
4617 if (j < i)
4618 shadow_read_only_fields[j] = field;
4619 j++;
4620 }
4621 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004622
4623 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004624 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004625 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004626 (i + 1 == max_shadow_read_write_fields ||
4627 shadow_read_write_fields[i + 1] != field + 1))
4628 pr_err("Missing field from shadow_read_write_field %x\n",
4629 field + 1);
4630
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004631 /*
4632 * PML and the preemption timer can be emulated, but the
4633 * processor cannot vmwrite to fields that don't exist
4634 * on bare metal.
4635 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004636 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004637 case GUEST_PML_INDEX:
4638 if (!cpu_has_vmx_pml())
4639 continue;
4640 break;
4641 case VMX_PREEMPTION_TIMER_VALUE:
4642 if (!cpu_has_vmx_preemption_timer())
4643 continue;
4644 break;
4645 case GUEST_INTR_STATUS:
4646 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004647 continue;
4648 break;
4649 default:
4650 break;
4651 }
4652
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004653 clear_bit(field, vmx_vmwrite_bitmap);
4654 clear_bit(field, vmx_vmread_bitmap);
4655#ifdef CONFIG_X86_64
4656 if (field & 1)
4657 continue;
4658#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004659 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004660 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004661 j++;
4662 }
4663 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004664}
4665
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666static __init int alloc_kvm_area(void)
4667{
4668 int cpu;
4669
Zachary Amsden3230bb42009-09-29 11:38:37 -10004670 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004671 struct vmcs *vmcs;
4672
4673 vmcs = alloc_vmcs_cpu(cpu);
4674 if (!vmcs) {
4675 free_kvm_area();
4676 return -ENOMEM;
4677 }
4678
4679 per_cpu(vmxarea, cpu) = vmcs;
4680 }
4681 return 0;
4682}
4683
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004684static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004685 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004687 if (!emulate_invalid_guest_state) {
4688 /*
4689 * CS and SS RPL should be equal during guest entry according
4690 * to VMX spec, but in reality it is not always so. Since vcpu
4691 * is in the middle of the transition from real mode to
4692 * protected mode it is safe to assume that RPL 0 is a good
4693 * default value.
4694 */
4695 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004696 save->selector &= ~SEGMENT_RPL_MASK;
4697 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004698 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004699 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004700 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701}
4702
4703static void enter_pmode(struct kvm_vcpu *vcpu)
4704{
4705 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004706 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707
Gleb Natapovd99e4152012-12-20 16:57:45 +02004708 /*
4709 * Update real mode segment cache. It may be not up-to-date if sement
4710 * register was written while vcpu was in a guest mode.
4711 */
4712 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4713 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4714 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4715 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4716 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4717 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4718
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004719 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004720
Avi Kivity2fb92db2011-04-27 19:42:18 +03004721 vmx_segment_cache_clear(vmx);
4722
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004723 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004724
4725 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004726 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4727 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728 vmcs_writel(GUEST_RFLAGS, flags);
4729
Rusty Russell66aee912007-07-17 23:34:16 +10004730 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4731 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732
4733 update_exception_bitmap(vcpu);
4734
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004735 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4736 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4737 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4738 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4739 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4740 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004741}
4742
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004743static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004744{
Mathias Krause772e0312012-08-30 01:30:19 +02004745 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004746 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004747
Gleb Natapovd99e4152012-12-20 16:57:45 +02004748 var.dpl = 0x3;
4749 if (seg == VCPU_SREG_CS)
4750 var.type = 0x3;
4751
4752 if (!emulate_invalid_guest_state) {
4753 var.selector = var.base >> 4;
4754 var.base = var.base & 0xffff0;
4755 var.limit = 0xffff;
4756 var.g = 0;
4757 var.db = 0;
4758 var.present = 1;
4759 var.s = 1;
4760 var.l = 0;
4761 var.unusable = 0;
4762 var.type = 0x3;
4763 var.avl = 0;
4764 if (save->base & 0xf)
4765 printk_once(KERN_WARNING "kvm: segment base is not "
4766 "paragraph aligned when entering "
4767 "protected mode (seg=%d)", seg);
4768 }
4769
4770 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004771 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004772 vmcs_write32(sf->limit, var.limit);
4773 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004774}
4775
4776static void enter_rmode(struct kvm_vcpu *vcpu)
4777{
4778 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004779 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004780 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004782 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4783 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4784 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4785 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4786 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004787 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4788 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004789
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004790 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791
Gleb Natapov776e58e2011-03-13 12:34:27 +02004792 /*
4793 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004794 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004795 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004796 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004797 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4798 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004799
Avi Kivity2fb92db2011-04-27 19:42:18 +03004800 vmx_segment_cache_clear(vmx);
4801
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004802 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004803 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4805
4806 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004807 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004808
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004809 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004810
4811 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004812 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813 update_exception_bitmap(vcpu);
4814
Gleb Natapovd99e4152012-12-20 16:57:45 +02004815 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4816 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4817 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4818 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4819 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4820 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004821
Eddie Dong8668a3c2007-10-10 14:26:45 +08004822 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004823}
4824
Amit Shah401d10d2009-02-20 22:53:37 +05304825static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4826{
4827 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004828 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4829
4830 if (!msr)
4831 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304832
Avi Kivity44ea2b12009-09-06 15:55:37 +03004833 /*
4834 * Force kernel_gs_base reloading before EFER changes, as control
4835 * of this msr depends on is_long_mode().
4836 */
4837 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004838 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304839 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004840 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304841 msr->data = efer;
4842 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004843 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304844
4845 msr->data = efer & ~EFER_LME;
4846 }
4847 setup_msrs(vmx);
4848}
4849
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004850#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004851
4852static void enter_lmode(struct kvm_vcpu *vcpu)
4853{
4854 u32 guest_tr_ar;
4855
Avi Kivity2fb92db2011-04-27 19:42:18 +03004856 vmx_segment_cache_clear(to_vmx(vcpu));
4857
Avi Kivity6aa8b732006-12-10 02:21:36 -08004858 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004859 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004860 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4861 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004863 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4864 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 }
Avi Kivityda38f432010-07-06 11:30:49 +03004866 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867}
4868
4869static void exit_lmode(struct kvm_vcpu *vcpu)
4870{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004871 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004872 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873}
4874
4875#endif
4876
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004877static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4878 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004879{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004880 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004881 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4882 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004883 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004884 } else {
4885 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004886 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004887}
4888
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004889static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004890{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004891 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004892}
4893
Avi Kivitye8467fd2009-12-29 18:43:06 +02004894static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4895{
4896 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4897
4898 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4899 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4900}
4901
Avi Kivityaff48ba2010-12-05 18:56:11 +02004902static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4903{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004904 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004905 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4906 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4907}
4908
Anthony Liguori25c4c272007-04-27 09:29:21 +03004909static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004910{
Avi Kivityfc78f512009-12-07 12:16:48 +02004911 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4912
4913 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4914 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004915}
4916
Sheng Yang14394422008-04-28 12:24:45 +08004917static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4918{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004919 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4920
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004921 if (!test_bit(VCPU_EXREG_PDPTR,
4922 (unsigned long *)&vcpu->arch.regs_dirty))
4923 return;
4924
Sheng Yang14394422008-04-28 12:24:45 +08004925 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004926 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4927 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4928 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4929 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004930 }
4931}
4932
Avi Kivity8f5d5492009-05-31 18:41:29 +03004933static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4934{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004935 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4936
Avi Kivity8f5d5492009-05-31 18:41:29 +03004937 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004938 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4939 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4940 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4941 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004942 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004943
4944 __set_bit(VCPU_EXREG_PDPTR,
4945 (unsigned long *)&vcpu->arch.regs_avail);
4946 __set_bit(VCPU_EXREG_PDPTR,
4947 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004948}
4949
David Matlack38991522016-11-29 18:14:08 -08004950static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4951{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004952 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4953 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004954 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4955
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004956 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004957 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4958 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4959 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4960
4961 return fixed_bits_valid(val, fixed0, fixed1);
4962}
4963
4964static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4965{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004966 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4967 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004968
4969 return fixed_bits_valid(val, fixed0, fixed1);
4970}
4971
4972static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4973{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004974 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4975 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004976
4977 return fixed_bits_valid(val, fixed0, fixed1);
4978}
4979
4980/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4981#define nested_guest_cr4_valid nested_cr4_valid
4982#define nested_host_cr4_valid nested_cr4_valid
4983
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004984static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004985
4986static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4987 unsigned long cr0,
4988 struct kvm_vcpu *vcpu)
4989{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004990 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4991 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004992 if (!(cr0 & X86_CR0_PG)) {
4993 /* From paging/starting to nonpaging */
4994 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004995 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004996 (CPU_BASED_CR3_LOAD_EXITING |
4997 CPU_BASED_CR3_STORE_EXITING));
4998 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004999 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005000 } else if (!is_paging(vcpu)) {
5001 /* From nonpaging to paging */
5002 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005003 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005004 ~(CPU_BASED_CR3_LOAD_EXITING |
5005 CPU_BASED_CR3_STORE_EXITING));
5006 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005007 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005008 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005009
5010 if (!(cr0 & X86_CR0_WP))
5011 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005012}
5013
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5015{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005016 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005017 unsigned long hw_cr0;
5018
Gleb Natapov50378782013-02-04 16:00:28 +02005019 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005020 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005021 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005022 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005023 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005024
Gleb Natapov218e7632013-01-21 15:36:45 +02005025 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5026 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005027
Gleb Natapov218e7632013-01-21 15:36:45 +02005028 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5029 enter_rmode(vcpu);
5030 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005031
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005032#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005033 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005034 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005036 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037 exit_lmode(vcpu);
5038 }
5039#endif
5040
Sean Christophersonb4d18512018-03-05 12:04:40 -08005041 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005042 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5043
Avi Kivity6aa8b732006-12-10 02:21:36 -08005044 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005045 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005046 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005047
5048 /* depends on vcpu->arch.cr0 to be set to a new value */
5049 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005050}
5051
Yu Zhang855feb62017-08-24 20:27:55 +08005052static int get_ept_level(struct kvm_vcpu *vcpu)
5053{
5054 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5055 return 5;
5056 return 4;
5057}
5058
Peter Feiner995f00a2017-06-30 17:26:32 -07005059static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005060{
Yu Zhang855feb62017-08-24 20:27:55 +08005061 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005062
Yu Zhang855feb62017-08-24 20:27:55 +08005063 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005064
Peter Feiner995f00a2017-06-30 17:26:32 -07005065 if (enable_ept_ad_bits &&
5066 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005067 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005068 eptp |= (root_hpa & PAGE_MASK);
5069
5070 return eptp;
5071}
5072
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5074{
Sheng Yang14394422008-04-28 12:24:45 +08005075 unsigned long guest_cr3;
5076 u64 eptp;
5077
5078 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005079 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005080 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005081 vmcs_write64(EPT_POINTER, eptp);
Sean Christophersone90008d2018-03-05 12:04:37 -08005082 if (enable_unrestricted_guest || is_paging(vcpu) ||
5083 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005084 guest_cr3 = kvm_read_cr3(vcpu);
5085 else
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005086 guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005087 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005088 }
5089
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005090 vmx_flush_tlb(vcpu, true);
Sheng Yang14394422008-04-28 12:24:45 +08005091 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005092}
5093
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005094static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005096 /*
5097 * Pass through host's Machine Check Enable value to hw_cr4, which
5098 * is in force while we are in guest mode. Do not let guests control
5099 * this bit, even if host CR4.MCE == 0.
5100 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005101 unsigned long hw_cr4;
5102
5103 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5104 if (enable_unrestricted_guest)
5105 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5106 else if (to_vmx(vcpu)->rmode.vm86_active)
5107 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5108 else
5109 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005110
Sean Christopherson64f7a112018-04-30 10:01:06 -07005111 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5112 if (cr4 & X86_CR4_UMIP) {
5113 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005114 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005115 hw_cr4 &= ~X86_CR4_UMIP;
5116 } else if (!is_guest_mode(vcpu) ||
5117 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5118 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5119 SECONDARY_EXEC_DESC);
5120 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005121
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005122 if (cr4 & X86_CR4_VMXE) {
5123 /*
5124 * To use VMXON (and later other VMX instructions), a guest
5125 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5126 * So basically the check on whether to allow nested VMX
5127 * is here.
5128 */
5129 if (!nested_vmx_allowed(vcpu))
5130 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005131 }
David Matlack38991522016-11-29 18:14:08 -08005132
5133 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005134 return 1;
5135
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005136 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005137
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005138 if (!enable_unrestricted_guest) {
5139 if (enable_ept) {
5140 if (!is_paging(vcpu)) {
5141 hw_cr4 &= ~X86_CR4_PAE;
5142 hw_cr4 |= X86_CR4_PSE;
5143 } else if (!(cr4 & X86_CR4_PAE)) {
5144 hw_cr4 &= ~X86_CR4_PAE;
5145 }
5146 }
5147
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005148 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005149 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5150 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5151 * to be manually disabled when guest switches to non-paging
5152 * mode.
5153 *
5154 * If !enable_unrestricted_guest, the CPU is always running
5155 * with CR0.PG=1 and CR4 needs to be modified.
5156 * If enable_unrestricted_guest, the CPU automatically
5157 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005158 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005159 if (!is_paging(vcpu))
5160 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5161 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005162
Sheng Yang14394422008-04-28 12:24:45 +08005163 vmcs_writel(CR4_READ_SHADOW, cr4);
5164 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005165 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166}
5167
Avi Kivity6aa8b732006-12-10 02:21:36 -08005168static void vmx_get_segment(struct kvm_vcpu *vcpu,
5169 struct kvm_segment *var, int seg)
5170{
Avi Kivitya9179492011-01-03 14:28:52 +02005171 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172 u32 ar;
5173
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005174 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005175 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005176 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005177 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005178 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005179 var->base = vmx_read_guest_seg_base(vmx, seg);
5180 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5181 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005182 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005183 var->base = vmx_read_guest_seg_base(vmx, seg);
5184 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5185 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5186 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005187 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005188 var->type = ar & 15;
5189 var->s = (ar >> 4) & 1;
5190 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005191 /*
5192 * Some userspaces do not preserve unusable property. Since usable
5193 * segment has to be present according to VMX spec we can use present
5194 * property to amend userspace bug by making unusable segment always
5195 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5196 * segment as unusable.
5197 */
5198 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005199 var->avl = (ar >> 12) & 1;
5200 var->l = (ar >> 13) & 1;
5201 var->db = (ar >> 14) & 1;
5202 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005203}
5204
Avi Kivitya9179492011-01-03 14:28:52 +02005205static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5206{
Avi Kivitya9179492011-01-03 14:28:52 +02005207 struct kvm_segment s;
5208
5209 if (to_vmx(vcpu)->rmode.vm86_active) {
5210 vmx_get_segment(vcpu, &s, seg);
5211 return s.base;
5212 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005213 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005214}
5215
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005216static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005217{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005218 struct vcpu_vmx *vmx = to_vmx(vcpu);
5219
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005220 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005221 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005222 else {
5223 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005224 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005225 }
Avi Kivity69c73022011-03-07 15:26:44 +02005226}
5227
Avi Kivity653e3102007-05-07 10:55:37 +03005228static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005229{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005230 u32 ar;
5231
Avi Kivityf0495f92012-06-07 17:06:10 +03005232 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005233 ar = 1 << 16;
5234 else {
5235 ar = var->type & 15;
5236 ar |= (var->s & 1) << 4;
5237 ar |= (var->dpl & 3) << 5;
5238 ar |= (var->present & 1) << 7;
5239 ar |= (var->avl & 1) << 12;
5240 ar |= (var->l & 1) << 13;
5241 ar |= (var->db & 1) << 14;
5242 ar |= (var->g & 1) << 15;
5243 }
Avi Kivity653e3102007-05-07 10:55:37 +03005244
5245 return ar;
5246}
5247
5248static void vmx_set_segment(struct kvm_vcpu *vcpu,
5249 struct kvm_segment *var, int seg)
5250{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005252 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005253
Avi Kivity2fb92db2011-04-27 19:42:18 +03005254 vmx_segment_cache_clear(vmx);
5255
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005256 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5257 vmx->rmode.segs[seg] = *var;
5258 if (seg == VCPU_SREG_TR)
5259 vmcs_write16(sf->selector, var->selector);
5260 else if (var->s)
5261 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005262 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005263 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005264
Avi Kivity653e3102007-05-07 10:55:37 +03005265 vmcs_writel(sf->base, var->base);
5266 vmcs_write32(sf->limit, var->limit);
5267 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005268
5269 /*
5270 * Fix the "Accessed" bit in AR field of segment registers for older
5271 * qemu binaries.
5272 * IA32 arch specifies that at the time of processor reset the
5273 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005274 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005275 * state vmexit when "unrestricted guest" mode is turned on.
5276 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5277 * tree. Newer qemu binaries with that qemu fix would not need this
5278 * kvm hack.
5279 */
5280 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005281 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005282
Gleb Natapovf924d662012-12-12 19:10:55 +02005283 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005284
5285out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005286 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005287}
5288
Avi Kivity6aa8b732006-12-10 02:21:36 -08005289static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5290{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005291 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005292
5293 *db = (ar >> 14) & 1;
5294 *l = (ar >> 13) & 1;
5295}
5296
Gleb Natapov89a27f42010-02-16 10:51:48 +02005297static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005298{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005299 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5300 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005301}
5302
Gleb Natapov89a27f42010-02-16 10:51:48 +02005303static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005304{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005305 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5306 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005307}
5308
Gleb Natapov89a27f42010-02-16 10:51:48 +02005309static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005310{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005311 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5312 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005313}
5314
Gleb Natapov89a27f42010-02-16 10:51:48 +02005315static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005316{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005317 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5318 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005319}
5320
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005321static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5322{
5323 struct kvm_segment var;
5324 u32 ar;
5325
5326 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005327 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005328 if (seg == VCPU_SREG_CS)
5329 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005330 ar = vmx_segment_access_rights(&var);
5331
5332 if (var.base != (var.selector << 4))
5333 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005334 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005335 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005336 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005337 return false;
5338
5339 return true;
5340}
5341
5342static bool code_segment_valid(struct kvm_vcpu *vcpu)
5343{
5344 struct kvm_segment cs;
5345 unsigned int cs_rpl;
5346
5347 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005348 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005349
Avi Kivity1872a3f2009-01-04 23:26:52 +02005350 if (cs.unusable)
5351 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005352 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005353 return false;
5354 if (!cs.s)
5355 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005356 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005357 if (cs.dpl > cs_rpl)
5358 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005359 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005360 if (cs.dpl != cs_rpl)
5361 return false;
5362 }
5363 if (!cs.present)
5364 return false;
5365
5366 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5367 return true;
5368}
5369
5370static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5371{
5372 struct kvm_segment ss;
5373 unsigned int ss_rpl;
5374
5375 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005376 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005377
Avi Kivity1872a3f2009-01-04 23:26:52 +02005378 if (ss.unusable)
5379 return true;
5380 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005381 return false;
5382 if (!ss.s)
5383 return false;
5384 if (ss.dpl != ss_rpl) /* DPL != RPL */
5385 return false;
5386 if (!ss.present)
5387 return false;
5388
5389 return true;
5390}
5391
5392static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5393{
5394 struct kvm_segment var;
5395 unsigned int rpl;
5396
5397 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005398 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005399
Avi Kivity1872a3f2009-01-04 23:26:52 +02005400 if (var.unusable)
5401 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005402 if (!var.s)
5403 return false;
5404 if (!var.present)
5405 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005406 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005407 if (var.dpl < rpl) /* DPL < RPL */
5408 return false;
5409 }
5410
5411 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5412 * rights flags
5413 */
5414 return true;
5415}
5416
5417static bool tr_valid(struct kvm_vcpu *vcpu)
5418{
5419 struct kvm_segment tr;
5420
5421 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5422
Avi Kivity1872a3f2009-01-04 23:26:52 +02005423 if (tr.unusable)
5424 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005425 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005426 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005427 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005428 return false;
5429 if (!tr.present)
5430 return false;
5431
5432 return true;
5433}
5434
5435static bool ldtr_valid(struct kvm_vcpu *vcpu)
5436{
5437 struct kvm_segment ldtr;
5438
5439 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5440
Avi Kivity1872a3f2009-01-04 23:26:52 +02005441 if (ldtr.unusable)
5442 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005443 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005444 return false;
5445 if (ldtr.type != 2)
5446 return false;
5447 if (!ldtr.present)
5448 return false;
5449
5450 return true;
5451}
5452
5453static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5454{
5455 struct kvm_segment cs, ss;
5456
5457 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5458 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5459
Nadav Amitb32a9912015-03-29 16:33:04 +03005460 return ((cs.selector & SEGMENT_RPL_MASK) ==
5461 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005462}
5463
5464/*
5465 * Check if guest state is valid. Returns true if valid, false if
5466 * not.
5467 * We assume that registers are always usable
5468 */
5469static bool guest_state_valid(struct kvm_vcpu *vcpu)
5470{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005471 if (enable_unrestricted_guest)
5472 return true;
5473
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005474 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005475 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005476 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5477 return false;
5478 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5479 return false;
5480 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5481 return false;
5482 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5483 return false;
5484 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5485 return false;
5486 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5487 return false;
5488 } else {
5489 /* protected mode guest state checks */
5490 if (!cs_ss_rpl_check(vcpu))
5491 return false;
5492 if (!code_segment_valid(vcpu))
5493 return false;
5494 if (!stack_segment_valid(vcpu))
5495 return false;
5496 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5497 return false;
5498 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5499 return false;
5500 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5501 return false;
5502 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5503 return false;
5504 if (!tr_valid(vcpu))
5505 return false;
5506 if (!ldtr_valid(vcpu))
5507 return false;
5508 }
5509 /* TODO:
5510 * - Add checks on RIP
5511 * - Add checks on RFLAGS
5512 */
5513
5514 return true;
5515}
5516
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005517static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5518{
5519 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5520}
5521
Mike Dayd77c26f2007-10-08 09:02:08 -04005522static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005523{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005524 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005525 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005526 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005527
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005528 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005529 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005530 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5531 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005532 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005533 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005534 r = kvm_write_guest_page(kvm, fn++, &data,
5535 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005536 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005537 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005538 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5539 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005540 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005541 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5542 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005543 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005544 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005545 r = kvm_write_guest_page(kvm, fn, &data,
5546 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5547 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005548out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005549 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005550 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005551}
5552
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005553static int init_rmode_identity_map(struct kvm *kvm)
5554{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005555 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005556 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005557 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005558 u32 tmp;
5559
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005560 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005561 mutex_lock(&kvm->slots_lock);
5562
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005563 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005564 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005565
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005566 if (!kvm_vmx->ept_identity_map_addr)
5567 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5568 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005569
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005570 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005571 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005572 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005573 goto out2;
5574
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005575 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005576 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5577 if (r < 0)
5578 goto out;
5579 /* Set up identity-mapping pagetable for EPT in real mode */
5580 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5581 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5582 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5583 r = kvm_write_guest_page(kvm, identity_map_pfn,
5584 &tmp, i * sizeof(tmp), sizeof(tmp));
5585 if (r < 0)
5586 goto out;
5587 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005588 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005589
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005590out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005591 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005592
5593out2:
5594 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005595 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005596}
5597
Avi Kivity6aa8b732006-12-10 02:21:36 -08005598static void seg_setup(int seg)
5599{
Mathias Krause772e0312012-08-30 01:30:19 +02005600 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005601 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005602
5603 vmcs_write16(sf->selector, 0);
5604 vmcs_writel(sf->base, 0);
5605 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005606 ar = 0x93;
5607 if (seg == VCPU_SREG_CS)
5608 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005609
5610 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005611}
5612
Sheng Yangf78e0e22007-10-29 09:40:42 +08005613static int alloc_apic_access_page(struct kvm *kvm)
5614{
Xiao Guangrong44841412012-09-07 14:14:20 +08005615 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005616 int r = 0;
5617
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005618 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005619 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005620 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005621 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5622 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005623 if (r)
5624 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005625
Tang Chen73a6d942014-09-11 13:38:00 +08005626 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005627 if (is_error_page(page)) {
5628 r = -EFAULT;
5629 goto out;
5630 }
5631
Tang Chenc24ae0d2014-09-24 15:57:58 +08005632 /*
5633 * Do not pin the page in memory, so that memory hot-unplug
5634 * is able to migrate it.
5635 */
5636 put_page(page);
5637 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005638out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005639 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005640 return r;
5641}
5642
Wanpeng Li991e7a02015-09-16 17:30:05 +08005643static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005644{
5645 int vpid;
5646
Avi Kivity919818a2009-03-23 18:01:29 +02005647 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005648 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005649 spin_lock(&vmx_vpid_lock);
5650 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005651 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005652 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005653 else
5654 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005655 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005656 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005657}
5658
Wanpeng Li991e7a02015-09-16 17:30:05 +08005659static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005660{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005661 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005662 return;
5663 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005664 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005665 spin_unlock(&vmx_vpid_lock);
5666}
5667
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005668static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5669 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005670{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005671 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005672
5673 if (!cpu_has_vmx_msr_bitmap())
5674 return;
5675
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005676 if (static_branch_unlikely(&enable_evmcs))
5677 evmcs_touch_msr_bitmap();
5678
Sheng Yang25c5f222008-03-28 13:18:56 +08005679 /*
5680 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5681 * have the write-low and read-high bitmap offsets the wrong way round.
5682 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5683 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005684 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005685 if (type & MSR_TYPE_R)
5686 /* read-low */
5687 __clear_bit(msr, msr_bitmap + 0x000 / f);
5688
5689 if (type & MSR_TYPE_W)
5690 /* write-low */
5691 __clear_bit(msr, msr_bitmap + 0x800 / f);
5692
Sheng Yang25c5f222008-03-28 13:18:56 +08005693 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5694 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005695 if (type & MSR_TYPE_R)
5696 /* read-high */
5697 __clear_bit(msr, msr_bitmap + 0x400 / f);
5698
5699 if (type & MSR_TYPE_W)
5700 /* write-high */
5701 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5702
5703 }
5704}
5705
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005706static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5707 u32 msr, int type)
5708{
5709 int f = sizeof(unsigned long);
5710
5711 if (!cpu_has_vmx_msr_bitmap())
5712 return;
5713
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005714 if (static_branch_unlikely(&enable_evmcs))
5715 evmcs_touch_msr_bitmap();
5716
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005717 /*
5718 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5719 * have the write-low and read-high bitmap offsets the wrong way round.
5720 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5721 */
5722 if (msr <= 0x1fff) {
5723 if (type & MSR_TYPE_R)
5724 /* read-low */
5725 __set_bit(msr, msr_bitmap + 0x000 / f);
5726
5727 if (type & MSR_TYPE_W)
5728 /* write-low */
5729 __set_bit(msr, msr_bitmap + 0x800 / f);
5730
5731 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5732 msr &= 0x1fff;
5733 if (type & MSR_TYPE_R)
5734 /* read-high */
5735 __set_bit(msr, msr_bitmap + 0x400 / f);
5736
5737 if (type & MSR_TYPE_W)
5738 /* write-high */
5739 __set_bit(msr, msr_bitmap + 0xc00 / f);
5740
5741 }
5742}
5743
5744static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5745 u32 msr, int type, bool value)
5746{
5747 if (value)
5748 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5749 else
5750 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5751}
5752
Wincy Vanf2b93282015-02-03 23:56:03 +08005753/*
5754 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5755 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5756 */
5757static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5758 unsigned long *msr_bitmap_nested,
5759 u32 msr, int type)
5760{
5761 int f = sizeof(unsigned long);
5762
Wincy Vanf2b93282015-02-03 23:56:03 +08005763 /*
5764 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5765 * have the write-low and read-high bitmap offsets the wrong way round.
5766 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5767 */
5768 if (msr <= 0x1fff) {
5769 if (type & MSR_TYPE_R &&
5770 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5771 /* read-low */
5772 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5773
5774 if (type & MSR_TYPE_W &&
5775 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5776 /* write-low */
5777 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5778
5779 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5780 msr &= 0x1fff;
5781 if (type & MSR_TYPE_R &&
5782 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5783 /* read-high */
5784 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5785
5786 if (type & MSR_TYPE_W &&
5787 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5788 /* write-high */
5789 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5790
5791 }
5792}
5793
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005794static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005795{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005796 u8 mode = 0;
5797
5798 if (cpu_has_secondary_exec_ctrls() &&
5799 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5800 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5801 mode |= MSR_BITMAP_MODE_X2APIC;
5802 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5803 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5804 }
5805
5806 if (is_long_mode(vcpu))
5807 mode |= MSR_BITMAP_MODE_LM;
5808
5809 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005810}
5811
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005812#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5813
5814static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5815 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005816{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005817 int msr;
5818
5819 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5820 unsigned word = msr / BITS_PER_LONG;
5821 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5822 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005823 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005824
5825 if (mode & MSR_BITMAP_MODE_X2APIC) {
5826 /*
5827 * TPR reads and writes can be virtualized even if virtual interrupt
5828 * delivery is not in use.
5829 */
5830 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5831 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5832 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5833 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5834 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5835 }
5836 }
5837}
5838
5839static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5840{
5841 struct vcpu_vmx *vmx = to_vmx(vcpu);
5842 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5843 u8 mode = vmx_msr_bitmap_mode(vcpu);
5844 u8 changed = mode ^ vmx->msr_bitmap_mode;
5845
5846 if (!changed)
5847 return;
5848
5849 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5850 !(mode & MSR_BITMAP_MODE_LM));
5851
5852 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5853 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5854
5855 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005856}
5857
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005858static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005859{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005860 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005861}
5862
David Matlackc9f04402017-08-01 14:00:40 -07005863static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5864{
5865 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5866 gfn_t gfn;
5867
5868 /*
5869 * Don't need to mark the APIC access page dirty; it is never
5870 * written to by the CPU during APIC virtualization.
5871 */
5872
5873 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5874 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5875 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5876 }
5877
5878 if (nested_cpu_has_posted_intr(vmcs12)) {
5879 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5880 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5881 }
5882}
5883
5884
David Hildenbrand6342c502017-01-25 11:58:58 +01005885static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005886{
5887 struct vcpu_vmx *vmx = to_vmx(vcpu);
5888 int max_irr;
5889 void *vapic_page;
5890 u16 status;
5891
David Matlackc9f04402017-08-01 14:00:40 -07005892 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5893 return;
Wincy Van705699a2015-02-03 23:58:17 +08005894
David Matlackc9f04402017-08-01 14:00:40 -07005895 vmx->nested.pi_pending = false;
5896 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5897 return;
Wincy Van705699a2015-02-03 23:58:17 +08005898
David Matlackc9f04402017-08-01 14:00:40 -07005899 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5900 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005901 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005902 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5903 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005904 kunmap(vmx->nested.virtual_apic_page);
5905
5906 status = vmcs_read16(GUEST_INTR_STATUS);
5907 if ((u8)max_irr > ((u8)status & 0xff)) {
5908 status &= ~0xff;
5909 status |= (u8)max_irr;
5910 vmcs_write16(GUEST_INTR_STATUS, status);
5911 }
5912 }
David Matlackc9f04402017-08-01 14:00:40 -07005913
5914 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005915}
5916
Wincy Van06a55242017-04-28 13:13:59 +08005917static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5918 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005919{
5920#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005921 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5922
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005923 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005924 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005925 * The vector of interrupt to be delivered to vcpu had
5926 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005927 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005928 * Following cases will be reached in this block, and
5929 * we always send a notification event in all cases as
5930 * explained below.
5931 *
5932 * Case 1: vcpu keeps in non-root mode. Sending a
5933 * notification event posts the interrupt to vcpu.
5934 *
5935 * Case 2: vcpu exits to root mode and is still
5936 * runnable. PIR will be synced to vIRR before the
5937 * next vcpu entry. Sending a notification event in
5938 * this case has no effect, as vcpu is not in root
5939 * mode.
5940 *
5941 * Case 3: vcpu exits to root mode and is blocked.
5942 * vcpu_block() has already synced PIR to vIRR and
5943 * never blocks vcpu if vIRR is not cleared. Therefore,
5944 * a blocked vcpu here does not wait for any requested
5945 * interrupts in PIR, and sending a notification event
5946 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005947 */
Feng Wu28b835d2015-09-18 22:29:54 +08005948
Wincy Van06a55242017-04-28 13:13:59 +08005949 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005950 return true;
5951 }
5952#endif
5953 return false;
5954}
5955
Wincy Van705699a2015-02-03 23:58:17 +08005956static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5957 int vector)
5958{
5959 struct vcpu_vmx *vmx = to_vmx(vcpu);
5960
5961 if (is_guest_mode(vcpu) &&
5962 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005963 /*
5964 * If a posted intr is not recognized by hardware,
5965 * we will accomplish it in the next vmentry.
5966 */
5967 vmx->nested.pi_pending = true;
5968 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005969 /* the PIR and ON have been set by L1. */
5970 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
5971 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005972 return 0;
5973 }
5974 return -1;
5975}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005976/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005977 * Send interrupt to vcpu via posted interrupt way.
5978 * 1. If target vcpu is running(non-root mode), send posted interrupt
5979 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5980 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5981 * interrupt from PIR in next vmentry.
5982 */
5983static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5984{
5985 struct vcpu_vmx *vmx = to_vmx(vcpu);
5986 int r;
5987
Wincy Van705699a2015-02-03 23:58:17 +08005988 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5989 if (!r)
5990 return;
5991
Yang Zhanga20ed542013-04-11 19:25:15 +08005992 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5993 return;
5994
Paolo Bonzinib95234c2016-12-19 13:57:33 +01005995 /* If a previous notification has sent the IPI, nothing to do. */
5996 if (pi_test_and_set_on(&vmx->pi_desc))
5997 return;
5998
Wincy Van06a55242017-04-28 13:13:59 +08005999 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006000 kvm_vcpu_kick(vcpu);
6001}
6002
Avi Kivity6aa8b732006-12-10 02:21:36 -08006003/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006004 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6005 * will not change in the lifetime of the guest.
6006 * Note that host-state that does change is set elsewhere. E.g., host-state
6007 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6008 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006009static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006010{
6011 u32 low32, high32;
6012 unsigned long tmpl;
6013 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006014 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006015
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006016 cr0 = read_cr0();
6017 WARN_ON(cr0 & X86_CR0_TS);
6018 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006019
6020 /*
6021 * Save the most likely value for this task's CR3 in the VMCS.
6022 * We can't use __get_current_cr3_fast() because we're not atomic.
6023 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006024 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006025 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006026 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006027
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006028 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006029 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006030 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006031 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006032
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006033 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006034#ifdef CONFIG_X86_64
6035 /*
6036 * Load null selectors, so we can avoid reloading them in
6037 * __vmx_load_host_state(), in case userspace uses the null selectors
6038 * too (the expected case).
6039 */
6040 vmcs_write16(HOST_DS_SELECTOR, 0);
6041 vmcs_write16(HOST_ES_SELECTOR, 0);
6042#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006043 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6044 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006045#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006046 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6047 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6048
Juergen Gross87930012017-09-04 12:25:27 +02006049 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006050 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006051 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006052
Avi Kivity83287ea422012-09-16 15:10:57 +03006053 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006054
6055 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6056 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6057 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6058 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6059
6060 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6061 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6062 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6063 }
6064}
6065
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006066static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6067{
6068 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6069 if (enable_ept)
6070 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006071 if (is_guest_mode(&vmx->vcpu))
6072 vmx->vcpu.arch.cr4_guest_owned_bits &=
6073 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006074 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6075}
6076
Yang Zhang01e439b2013-04-11 19:25:12 +08006077static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6078{
6079 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6080
Andrey Smetanind62caab2015-11-10 15:36:33 +03006081 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006082 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006083
6084 if (!enable_vnmi)
6085 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6086
Yunhong Jiang64672c92016-06-13 14:19:59 -07006087 /* Enable the preemption timer dynamically */
6088 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006089 return pin_based_exec_ctrl;
6090}
6091
Andrey Smetanind62caab2015-11-10 15:36:33 +03006092static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6093{
6094 struct vcpu_vmx *vmx = to_vmx(vcpu);
6095
6096 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006097 if (cpu_has_secondary_exec_ctrls()) {
6098 if (kvm_vcpu_apicv_active(vcpu))
6099 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6100 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6101 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6102 else
6103 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6104 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6105 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6106 }
6107
6108 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006109 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006110}
6111
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006112static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6113{
6114 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006115
6116 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6117 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6118
Paolo Bonzini35754c92015-07-29 12:05:37 +02006119 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006120 exec_control &= ~CPU_BASED_TPR_SHADOW;
6121#ifdef CONFIG_X86_64
6122 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6123 CPU_BASED_CR8_LOAD_EXITING;
6124#endif
6125 }
6126 if (!enable_ept)
6127 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6128 CPU_BASED_CR3_LOAD_EXITING |
6129 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006130 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6131 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6132 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006133 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6134 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006135 return exec_control;
6136}
6137
Jim Mattson45ec3682017-08-23 16:32:04 -07006138static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006139{
Jim Mattson45ec3682017-08-23 16:32:04 -07006140 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006141 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006142}
6143
Jim Mattson75f4fc82017-08-23 16:32:03 -07006144static bool vmx_rdseed_supported(void)
6145{
6146 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006147 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006148}
6149
Paolo Bonzini80154d72017-08-24 13:55:35 +02006150static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006151{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006152 struct kvm_vcpu *vcpu = &vmx->vcpu;
6153
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006154 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006155
Paolo Bonzini80154d72017-08-24 13:55:35 +02006156 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006157 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6158 if (vmx->vpid == 0)
6159 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6160 if (!enable_ept) {
6161 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6162 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00006163 /* Enable INVPCID for non-ept guests may cause performance regression. */
6164 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006165 }
6166 if (!enable_unrestricted_guest)
6167 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006168 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006169 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006170 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006171 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6172 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006173 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006174
6175 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6176 * in vmx_set_cr4. */
6177 exec_control &= ~SECONDARY_EXEC_DESC;
6178
Abel Gordonabc4fc52013-04-18 14:35:25 +03006179 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6180 (handle_vmptrld).
6181 We can NOT enable shadow_vmcs here because we don't have yet
6182 a current VMCS12
6183 */
6184 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006185
6186 if (!enable_pml)
6187 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006188
Paolo Bonzini3db13482017-08-24 14:48:03 +02006189 if (vmx_xsaves_supported()) {
6190 /* Exposing XSAVES only when XSAVE is exposed */
6191 bool xsaves_enabled =
6192 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6193 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6194
6195 if (!xsaves_enabled)
6196 exec_control &= ~SECONDARY_EXEC_XSAVES;
6197
6198 if (nested) {
6199 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006200 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006201 SECONDARY_EXEC_XSAVES;
6202 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006203 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006204 ~SECONDARY_EXEC_XSAVES;
6205 }
6206 }
6207
Paolo Bonzini80154d72017-08-24 13:55:35 +02006208 if (vmx_rdtscp_supported()) {
6209 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6210 if (!rdtscp_enabled)
6211 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6212
6213 if (nested) {
6214 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006215 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006216 SECONDARY_EXEC_RDTSCP;
6217 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006218 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006219 ~SECONDARY_EXEC_RDTSCP;
6220 }
6221 }
6222
6223 if (vmx_invpcid_supported()) {
6224 /* Exposing INVPCID only when PCID is exposed */
6225 bool invpcid_enabled =
6226 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6227 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6228
6229 if (!invpcid_enabled) {
6230 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6231 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6232 }
6233
6234 if (nested) {
6235 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006236 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006237 SECONDARY_EXEC_ENABLE_INVPCID;
6238 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006239 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006240 ~SECONDARY_EXEC_ENABLE_INVPCID;
6241 }
6242 }
6243
Jim Mattson45ec3682017-08-23 16:32:04 -07006244 if (vmx_rdrand_supported()) {
6245 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6246 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006247 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006248
6249 if (nested) {
6250 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006251 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006252 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006253 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006254 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006255 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006256 }
6257 }
6258
Jim Mattson75f4fc82017-08-23 16:32:03 -07006259 if (vmx_rdseed_supported()) {
6260 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6261 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006262 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006263
6264 if (nested) {
6265 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006266 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006267 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006268 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006269 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006270 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006271 }
6272 }
6273
Paolo Bonzini80154d72017-08-24 13:55:35 +02006274 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006275}
6276
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006277static void ept_set_mmio_spte_mask(void)
6278{
6279 /*
6280 * EPT Misconfigurations can be generated if the value of bits 2:0
6281 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006282 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006283 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6284 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006285}
6286
Wanpeng Lif53cd632014-12-02 19:14:58 +08006287#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006288/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006289 * Sets up the vmcs for emulated real mode.
6290 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006291static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006292{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006293#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006294 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006295#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006296 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006297
Abel Gordon4607c2d2013-04-18 14:35:55 +03006298 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006299 /*
6300 * At vCPU creation, "VMWRITE to any supported field
6301 * in the VMCS" is supported, so use the more
6302 * permissive vmx_vmread_bitmap to specify both read
6303 * and write permissions for the shadow VMCS.
6304 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006305 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006306 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006307 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006308 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006309 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006310
Avi Kivity6aa8b732006-12-10 02:21:36 -08006311 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6312
Avi Kivity6aa8b732006-12-10 02:21:36 -08006313 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006314 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006315 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006316
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006317 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006318
Dan Williamsdfa169b2016-06-02 11:17:24 -07006319 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006320 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006321 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006322 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006323 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006324
Andrey Smetanind62caab2015-11-10 15:36:33 +03006325 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006326 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6327 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6328 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6329 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6330
6331 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006332
Li RongQing0bcf2612015-12-03 13:29:34 +08006333 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006334 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006335 }
6336
Wanpeng Lib31c1142018-03-12 04:53:04 -07006337 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006338 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006339 vmx->ple_window = ple_window;
6340 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006341 }
6342
Xiao Guangrongc3707952011-07-12 03:28:04 +08006343 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6344 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006345 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6346
Avi Kivity9581d442010-10-19 16:46:55 +02006347 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6348 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006349 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006350#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006351 rdmsrl(MSR_FS_BASE, a);
6352 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6353 rdmsrl(MSR_GS_BASE, a);
6354 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6355#else
6356 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6357 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6358#endif
6359
Bandan Das2a499e42017-08-03 15:54:41 -04006360 if (cpu_has_vmx_vmfunc())
6361 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6362
Eddie Dong2cc51562007-05-21 07:28:09 +03006363 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6364 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006365 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006366 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006367 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006368
Radim Krčmář74545702015-04-27 15:11:25 +02006369 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6370 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006371
Paolo Bonzini03916db2014-07-24 14:21:57 +02006372 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006373 u32 index = vmx_msr_index[i];
6374 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006375 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006376
6377 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6378 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006379 if (wrmsr_safe(index, data_low, data_high) < 0)
6380 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006381 vmx->guest_msrs[j].index = i;
6382 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006383 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006384 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006385 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006386
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006387 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6388 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006389
6390 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006391
6392 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006393 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006394
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006395 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6396 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6397
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006398 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006399
Wanpeng Lif53cd632014-12-02 19:14:58 +08006400 if (vmx_xsaves_supported())
6401 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6402
Peter Feiner4e595162016-07-07 14:49:58 -07006403 if (enable_pml) {
6404 ASSERT(vmx->pml_pg);
6405 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6406 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6407 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006408}
6409
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006410static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006411{
6412 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006413 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006414 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006415
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006416 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006417 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006418
Wanpeng Li518e7b92018-02-28 14:03:31 +08006419 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006420 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006421 kvm_set_cr8(vcpu, 0);
6422
6423 if (!init_event) {
6424 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6425 MSR_IA32_APICBASE_ENABLE;
6426 if (kvm_vcpu_is_reset_bsp(vcpu))
6427 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6428 apic_base_msr.host_initiated = true;
6429 kvm_set_apic_base(vcpu, &apic_base_msr);
6430 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006431
Avi Kivity2fb92db2011-04-27 19:42:18 +03006432 vmx_segment_cache_clear(vmx);
6433
Avi Kivity5706be02008-08-20 15:07:31 +03006434 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006435 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006436 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006437
6438 seg_setup(VCPU_SREG_DS);
6439 seg_setup(VCPU_SREG_ES);
6440 seg_setup(VCPU_SREG_FS);
6441 seg_setup(VCPU_SREG_GS);
6442 seg_setup(VCPU_SREG_SS);
6443
6444 vmcs_write16(GUEST_TR_SELECTOR, 0);
6445 vmcs_writel(GUEST_TR_BASE, 0);
6446 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6447 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6448
6449 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6450 vmcs_writel(GUEST_LDTR_BASE, 0);
6451 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6452 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6453
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006454 if (!init_event) {
6455 vmcs_write32(GUEST_SYSENTER_CS, 0);
6456 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6457 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6458 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6459 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006460
Wanpeng Lic37c2872017-11-20 14:52:21 -08006461 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006462 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006463
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006464 vmcs_writel(GUEST_GDTR_BASE, 0);
6465 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6466
6467 vmcs_writel(GUEST_IDTR_BASE, 0);
6468 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6469
Anthony Liguori443381a2010-12-06 10:53:38 -06006470 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006471 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006472 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006473 if (kvm_mpx_supported())
6474 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006475
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006476 setup_msrs(vmx);
6477
Avi Kivity6aa8b732006-12-10 02:21:36 -08006478 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6479
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006480 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006481 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006482 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006483 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006484 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006485 vmcs_write32(TPR_THRESHOLD, 0);
6486 }
6487
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006488 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006489
Sheng Yang2384d2b2008-01-17 15:14:33 +08006490 if (vmx->vpid != 0)
6491 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6492
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006493 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006494 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006495 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006496 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006497 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006498
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006499 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006500
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006501 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006502 if (init_event)
6503 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006504}
6505
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006506/*
6507 * In nested virtualization, check if L1 asked to exit on external interrupts.
6508 * For most existing hypervisors, this will always return true.
6509 */
6510static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6511{
6512 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6513 PIN_BASED_EXT_INTR_MASK;
6514}
6515
Bandan Das77b0f5d2014-04-19 18:17:45 -04006516/*
6517 * In nested virtualization, check if L1 has set
6518 * VM_EXIT_ACK_INTR_ON_EXIT
6519 */
6520static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6521{
6522 return get_vmcs12(vcpu)->vm_exit_controls &
6523 VM_EXIT_ACK_INTR_ON_EXIT;
6524}
6525
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006526static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6527{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006528 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006529}
6530
Jan Kiszkac9a79532014-03-07 20:03:15 +01006531static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006532{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006533 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6534 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006535}
6536
Jan Kiszkac9a79532014-03-07 20:03:15 +01006537static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006538{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006539 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006540 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006541 enable_irq_window(vcpu);
6542 return;
6543 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006544
Paolo Bonzini47c01522016-12-19 11:44:07 +01006545 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6546 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006547}
6548
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006549static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006550{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006551 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006552 uint32_t intr;
6553 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006554
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006555 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006556
Avi Kivityfa89a812008-09-01 15:57:51 +03006557 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006558 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006559 int inc_eip = 0;
6560 if (vcpu->arch.interrupt.soft)
6561 inc_eip = vcpu->arch.event_exit_inst_len;
6562 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006563 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006564 return;
6565 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006566 intr = irq | INTR_INFO_VALID_MASK;
6567 if (vcpu->arch.interrupt.soft) {
6568 intr |= INTR_TYPE_SOFT_INTR;
6569 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6570 vmx->vcpu.arch.event_exit_inst_len);
6571 } else
6572 intr |= INTR_TYPE_EXT_INTR;
6573 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006574
6575 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006576}
6577
Sheng Yangf08864b2008-05-15 18:23:25 +08006578static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6579{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006580 struct vcpu_vmx *vmx = to_vmx(vcpu);
6581
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006582 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006583 /*
6584 * Tracking the NMI-blocked state in software is built upon
6585 * finding the next open IRQ window. This, in turn, depends on
6586 * well-behaving guests: They have to keep IRQs disabled at
6587 * least as long as the NMI handler runs. Otherwise we may
6588 * cause NMI nesting, maybe breaking the guest. But as this is
6589 * highly unlikely, we can live with the residual risk.
6590 */
6591 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6592 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6593 }
6594
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006595 ++vcpu->stat.nmi_injections;
6596 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006597
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006598 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006599 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006600 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006601 return;
6602 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006603
Sheng Yangf08864b2008-05-15 18:23:25 +08006604 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6605 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006606
6607 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006608}
6609
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006610static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6611{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006612 struct vcpu_vmx *vmx = to_vmx(vcpu);
6613 bool masked;
6614
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006615 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006616 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006617 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006618 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006619 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6620 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6621 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006622}
6623
6624static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6625{
6626 struct vcpu_vmx *vmx = to_vmx(vcpu);
6627
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006628 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006629 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6630 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6631 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6632 }
6633 } else {
6634 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6635 if (masked)
6636 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6637 GUEST_INTR_STATE_NMI);
6638 else
6639 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6640 GUEST_INTR_STATE_NMI);
6641 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006642}
6643
Jan Kiszka2505dc92013-04-14 12:12:47 +02006644static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6645{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006646 if (to_vmx(vcpu)->nested.nested_run_pending)
6647 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006648
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006649 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006650 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6651 return 0;
6652
Jan Kiszka2505dc92013-04-14 12:12:47 +02006653 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6654 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6655 | GUEST_INTR_STATE_NMI));
6656}
6657
Gleb Natapov78646122009-03-23 12:12:11 +02006658static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6659{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006660 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6661 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006662 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6663 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006664}
6665
Izik Eiduscbc94022007-10-25 00:29:55 +02006666static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6667{
6668 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006669
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006670 if (enable_unrestricted_guest)
6671 return 0;
6672
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006673 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6674 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006675 if (ret)
6676 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006677 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006678 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006679}
6680
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006681static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6682{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006683 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006684 return 0;
6685}
6686
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006687static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006688{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006689 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006690 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006691 /*
6692 * Update instruction length as we may reinject the exception
6693 * from user space while in guest debugging mode.
6694 */
6695 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6696 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006697 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006698 return false;
6699 /* fall through */
6700 case DB_VECTOR:
6701 if (vcpu->guest_debug &
6702 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6703 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006704 /* fall through */
6705 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006706 case OF_VECTOR:
6707 case BR_VECTOR:
6708 case UD_VECTOR:
6709 case DF_VECTOR:
6710 case SS_VECTOR:
6711 case GP_VECTOR:
6712 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006713 return true;
6714 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006715 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006716 return false;
6717}
6718
6719static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6720 int vec, u32 err_code)
6721{
6722 /*
6723 * Instruction with address size override prefix opcode 0x67
6724 * Cause the #SS fault with 0 error code in VM86 mode.
6725 */
6726 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6727 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6728 if (vcpu->arch.halt_request) {
6729 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006730 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006731 }
6732 return 1;
6733 }
6734 return 0;
6735 }
6736
6737 /*
6738 * Forward all other exceptions that are valid in real mode.
6739 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6740 * the required debugging infrastructure rework.
6741 */
6742 kvm_queue_exception(vcpu, vec);
6743 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006744}
6745
Andi Kleena0861c02009-06-08 17:37:09 +08006746/*
6747 * Trigger machine check on the host. We assume all the MSRs are already set up
6748 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6749 * We pass a fake environment to the machine check handler because we want
6750 * the guest to be always treated like user space, no matter what context
6751 * it used internally.
6752 */
6753static void kvm_machine_check(void)
6754{
6755#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6756 struct pt_regs regs = {
6757 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6758 .flags = X86_EFLAGS_IF,
6759 };
6760
6761 do_machine_check(&regs, 0);
6762#endif
6763}
6764
Avi Kivity851ba692009-08-24 11:10:17 +03006765static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006766{
6767 /* already handled by vcpu_run */
6768 return 1;
6769}
6770
Avi Kivity851ba692009-08-24 11:10:17 +03006771static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006772{
Avi Kivity1155f762007-11-22 11:30:47 +02006773 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006774 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006775 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006776 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006777 u32 vect_info;
6778 enum emulation_result er;
6779
Avi Kivity1155f762007-11-22 11:30:47 +02006780 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006781 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006782
Andi Kleena0861c02009-06-08 17:37:09 +08006783 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006784 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006785
Jim Mattsonef85b672016-12-12 11:01:37 -08006786 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006787 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006788
Wanpeng Li082d06e2018-04-03 16:28:48 -07006789 if (is_invalid_opcode(intr_info))
6790 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006791
Avi Kivity6aa8b732006-12-10 02:21:36 -08006792 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006793 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006794 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006795
Liran Alon9e869482018-03-12 13:12:51 +02006796 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6797 WARN_ON_ONCE(!enable_vmware_backdoor);
6798 er = emulate_instruction(vcpu,
6799 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6800 if (er == EMULATE_USER_EXIT)
6801 return 0;
6802 else if (er != EMULATE_DONE)
6803 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6804 return 1;
6805 }
6806
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006807 /*
6808 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6809 * MMIO, it is better to report an internal error.
6810 * See the comments in vmx_handle_exit.
6811 */
6812 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6813 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6814 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6815 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006816 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006817 vcpu->run->internal.data[0] = vect_info;
6818 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006819 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006820 return 0;
6821 }
6822
Avi Kivity6aa8b732006-12-10 02:21:36 -08006823 if (is_page_fault(intr_info)) {
6824 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006825 /* EPT won't cause page fault directly */
6826 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006827 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006828 }
6829
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006830 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006831
6832 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6833 return handle_rmode_exception(vcpu, ex_no, error_code);
6834
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006835 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006836 case AC_VECTOR:
6837 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6838 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006839 case DB_VECTOR:
6840 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6841 if (!(vcpu->guest_debug &
6842 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006843 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006844 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006845 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006846 skip_emulated_instruction(vcpu);
6847
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006848 kvm_queue_exception(vcpu, DB_VECTOR);
6849 return 1;
6850 }
6851 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6852 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6853 /* fall through */
6854 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006855 /*
6856 * Update instruction length as we may reinject #BP from
6857 * user space while in guest debugging mode. Reading it for
6858 * #DB as well causes no harm, it is not used in that case.
6859 */
6860 vmx->vcpu.arch.event_exit_inst_len =
6861 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006862 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006863 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006864 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6865 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006866 break;
6867 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006868 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6869 kvm_run->ex.exception = ex_no;
6870 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006871 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006872 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006873 return 0;
6874}
6875
Avi Kivity851ba692009-08-24 11:10:17 +03006876static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006877{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006878 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006879 return 1;
6880}
6881
Avi Kivity851ba692009-08-24 11:10:17 +03006882static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006883{
Avi Kivity851ba692009-08-24 11:10:17 +03006884 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006885 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006886 return 0;
6887}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006888
Avi Kivity851ba692009-08-24 11:10:17 +03006889static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006890{
He, Qingbfdaab02007-09-12 14:18:28 +08006891 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006892 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006893 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006894
He, Qingbfdaab02007-09-12 14:18:28 +08006895 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006896 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006897
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006898 ++vcpu->stat.io_exits;
6899
Sean Christopherson432baf62018-03-08 08:57:26 -08006900 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006901 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006902
6903 port = exit_qualification >> 16;
6904 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006905 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006906
Sean Christophersondca7f122018-03-08 08:57:27 -08006907 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006908}
6909
Ingo Molnar102d8322007-02-19 14:37:47 +02006910static void
6911vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6912{
6913 /*
6914 * Patch in the VMCALL instruction:
6915 */
6916 hypercall[0] = 0x0f;
6917 hypercall[1] = 0x01;
6918 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006919}
6920
Guo Chao0fa06072012-06-28 15:16:19 +08006921/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006922static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6923{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006924 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006925 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6926 unsigned long orig_val = val;
6927
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006928 /*
6929 * We get here when L2 changed cr0 in a way that did not change
6930 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006931 * but did change L0 shadowed bits. So we first calculate the
6932 * effective cr0 value that L1 would like to write into the
6933 * hardware. It consists of the L2-owned bits from the new
6934 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006935 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006936 val = (val & ~vmcs12->cr0_guest_host_mask) |
6937 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6938
David Matlack38991522016-11-29 18:14:08 -08006939 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006940 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006941
6942 if (kvm_set_cr0(vcpu, val))
6943 return 1;
6944 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006945 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006946 } else {
6947 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006948 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006949 return 1;
David Matlack38991522016-11-29 18:14:08 -08006950
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006951 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006952 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006953}
6954
6955static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6956{
6957 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006958 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6959 unsigned long orig_val = val;
6960
6961 /* analogously to handle_set_cr0 */
6962 val = (val & ~vmcs12->cr4_guest_host_mask) |
6963 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6964 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006965 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006966 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006967 return 0;
6968 } else
6969 return kvm_set_cr4(vcpu, val);
6970}
6971
Paolo Bonzini0367f202016-07-12 10:44:55 +02006972static int handle_desc(struct kvm_vcpu *vcpu)
6973{
6974 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
6975 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
6976}
6977
Avi Kivity851ba692009-08-24 11:10:17 +03006978static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006979{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006980 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006981 int cr;
6982 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03006983 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08006984 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006985
He, Qingbfdaab02007-09-12 14:18:28 +08006986 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006987 cr = exit_qualification & 15;
6988 reg = (exit_qualification >> 8) & 15;
6989 switch ((exit_qualification >> 4) & 3) {
6990 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03006991 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006992 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006993 switch (cr) {
6994 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006995 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006996 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006997 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08006998 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03006999 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007000 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007001 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007002 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007003 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007004 case 8: {
7005 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007006 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007007 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007008 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007009 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007010 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007011 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007012 return ret;
7013 /*
7014 * TODO: we might be squashing a
7015 * KVM_GUESTDBG_SINGLESTEP-triggered
7016 * KVM_EXIT_DEBUG here.
7017 */
Avi Kivity851ba692009-08-24 11:10:17 +03007018 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007019 return 0;
7020 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007021 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007022 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007023 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007024 WARN_ONCE(1, "Guest should always own CR0.TS");
7025 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007026 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007027 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007028 case 1: /*mov from cr*/
7029 switch (cr) {
7030 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007031 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007032 val = kvm_read_cr3(vcpu);
7033 kvm_register_write(vcpu, reg, val);
7034 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007035 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007036 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007037 val = kvm_get_cr8(vcpu);
7038 kvm_register_write(vcpu, reg, val);
7039 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007040 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007041 }
7042 break;
7043 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007044 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007045 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007046 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007047
Kyle Huey6affcbe2016-11-29 12:40:40 -08007048 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007049 default:
7050 break;
7051 }
Avi Kivity851ba692009-08-24 11:10:17 +03007052 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007053 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007054 (int)(exit_qualification >> 4) & 3, cr);
7055 return 0;
7056}
7057
Avi Kivity851ba692009-08-24 11:10:17 +03007058static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007059{
He, Qingbfdaab02007-09-12 14:18:28 +08007060 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007061 int dr, dr7, reg;
7062
7063 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7064 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7065
7066 /* First, if DR does not exist, trigger UD */
7067 if (!kvm_require_dr(vcpu, dr))
7068 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007069
Jan Kiszkaf2483412010-01-20 18:20:20 +01007070 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007071 if (!kvm_require_cpl(vcpu, 0))
7072 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007073 dr7 = vmcs_readl(GUEST_DR7);
7074 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007075 /*
7076 * As the vm-exit takes precedence over the debug trap, we
7077 * need to emulate the latter, either for the host or the
7078 * guest debugging itself.
7079 */
7080 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007081 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007082 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007083 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007084 vcpu->run->debug.arch.exception = DB_VECTOR;
7085 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007086 return 0;
7087 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007088 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007089 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007090 kvm_queue_exception(vcpu, DB_VECTOR);
7091 return 1;
7092 }
7093 }
7094
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007095 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007096 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7097 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007098
7099 /*
7100 * No more DR vmexits; force a reload of the debug registers
7101 * and reenter on this instruction. The next vmexit will
7102 * retrieve the full state of the debug registers.
7103 */
7104 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7105 return 1;
7106 }
7107
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007108 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7109 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007110 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007111
7112 if (kvm_get_dr(vcpu, dr, &val))
7113 return 1;
7114 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007115 } else
Nadav Amit57773922014-06-18 17:19:23 +03007116 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007117 return 1;
7118
Kyle Huey6affcbe2016-11-29 12:40:40 -08007119 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007120}
7121
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007122static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7123{
7124 return vcpu->arch.dr6;
7125}
7126
7127static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7128{
7129}
7130
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007131static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7132{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007133 get_debugreg(vcpu->arch.db[0], 0);
7134 get_debugreg(vcpu->arch.db[1], 1);
7135 get_debugreg(vcpu->arch.db[2], 2);
7136 get_debugreg(vcpu->arch.db[3], 3);
7137 get_debugreg(vcpu->arch.dr6, 6);
7138 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7139
7140 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007141 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007142}
7143
Gleb Natapov020df072010-04-13 10:05:23 +03007144static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7145{
7146 vmcs_writel(GUEST_DR7, val);
7147}
7148
Avi Kivity851ba692009-08-24 11:10:17 +03007149static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007150{
Kyle Huey6a908b62016-11-29 12:40:37 -08007151 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007152}
7153
Avi Kivity851ba692009-08-24 11:10:17 +03007154static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007155{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007156 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007157 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007158
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007159 msr_info.index = ecx;
7160 msr_info.host_initiated = false;
7161 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007162 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007163 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007164 return 1;
7165 }
7166
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007167 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007168
Avi Kivity6aa8b732006-12-10 02:21:36 -08007169 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007170 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7171 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007172 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007173}
7174
Avi Kivity851ba692009-08-24 11:10:17 +03007175static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007176{
Will Auld8fe8ab42012-11-29 12:42:12 -08007177 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007178 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7179 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7180 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007181
Will Auld8fe8ab42012-11-29 12:42:12 -08007182 msr.data = data;
7183 msr.index = ecx;
7184 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007185 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007186 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007187 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007188 return 1;
7189 }
7190
Avi Kivity59200272010-01-25 19:47:02 +02007191 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007192 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007193}
7194
Avi Kivity851ba692009-08-24 11:10:17 +03007195static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007196{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007197 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007198 return 1;
7199}
7200
Avi Kivity851ba692009-08-24 11:10:17 +03007201static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007202{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007203 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7204 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007205
Avi Kivity3842d132010-07-27 12:30:24 +03007206 kvm_make_request(KVM_REQ_EVENT, vcpu);
7207
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007208 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007209 return 1;
7210}
7211
Avi Kivity851ba692009-08-24 11:10:17 +03007212static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007213{
Avi Kivityd3bef152007-06-05 15:53:05 +03007214 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007215}
7216
Avi Kivity851ba692009-08-24 11:10:17 +03007217static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007218{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007219 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007220}
7221
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007222static int handle_invd(struct kvm_vcpu *vcpu)
7223{
Andre Przywara51d8b662010-12-21 11:12:02 +01007224 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007225}
7226
Avi Kivity851ba692009-08-24 11:10:17 +03007227static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007228{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007229 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007230
7231 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007232 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007233}
7234
Avi Kivityfee84b02011-11-10 14:57:25 +02007235static int handle_rdpmc(struct kvm_vcpu *vcpu)
7236{
7237 int err;
7238
7239 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007240 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007241}
7242
Avi Kivity851ba692009-08-24 11:10:17 +03007243static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007244{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007245 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007246}
7247
Dexuan Cui2acf9232010-06-10 11:27:12 +08007248static int handle_xsetbv(struct kvm_vcpu *vcpu)
7249{
7250 u64 new_bv = kvm_read_edx_eax(vcpu);
7251 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7252
7253 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007254 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007255 return 1;
7256}
7257
Wanpeng Lif53cd632014-12-02 19:14:58 +08007258static int handle_xsaves(struct kvm_vcpu *vcpu)
7259{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007260 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007261 WARN(1, "this should never happen\n");
7262 return 1;
7263}
7264
7265static int handle_xrstors(struct kvm_vcpu *vcpu)
7266{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007267 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007268 WARN(1, "this should never happen\n");
7269 return 1;
7270}
7271
Avi Kivity851ba692009-08-24 11:10:17 +03007272static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007273{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007274 if (likely(fasteoi)) {
7275 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7276 int access_type, offset;
7277
7278 access_type = exit_qualification & APIC_ACCESS_TYPE;
7279 offset = exit_qualification & APIC_ACCESS_OFFSET;
7280 /*
7281 * Sane guest uses MOV to write EOI, with written value
7282 * not cared. So make a short-circuit here by avoiding
7283 * heavy instruction emulation.
7284 */
7285 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7286 (offset == APIC_EOI)) {
7287 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007288 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007289 }
7290 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007291 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007292}
7293
Yang Zhangc7c9c562013-01-25 10:18:51 +08007294static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7295{
7296 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7297 int vector = exit_qualification & 0xff;
7298
7299 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7300 kvm_apic_set_eoi_accelerated(vcpu, vector);
7301 return 1;
7302}
7303
Yang Zhang83d4c282013-01-25 10:18:49 +08007304static int handle_apic_write(struct kvm_vcpu *vcpu)
7305{
7306 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7307 u32 offset = exit_qualification & 0xfff;
7308
7309 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7310 kvm_apic_write_nodecode(vcpu, offset);
7311 return 1;
7312}
7313
Avi Kivity851ba692009-08-24 11:10:17 +03007314static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007315{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007316 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007317 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007318 bool has_error_code = false;
7319 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007320 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007321 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007322
7323 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007324 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007325 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007326
7327 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7328
7329 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007330 if (reason == TASK_SWITCH_GATE && idt_v) {
7331 switch (type) {
7332 case INTR_TYPE_NMI_INTR:
7333 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007334 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007335 break;
7336 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007337 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007338 kvm_clear_interrupt_queue(vcpu);
7339 break;
7340 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007341 if (vmx->idt_vectoring_info &
7342 VECTORING_INFO_DELIVER_CODE_MASK) {
7343 has_error_code = true;
7344 error_code =
7345 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7346 }
7347 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007348 case INTR_TYPE_SOFT_EXCEPTION:
7349 kvm_clear_exception_queue(vcpu);
7350 break;
7351 default:
7352 break;
7353 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007354 }
Izik Eidus37817f22008-03-24 23:14:53 +02007355 tss_selector = exit_qualification;
7356
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007357 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7358 type != INTR_TYPE_EXT_INTR &&
7359 type != INTR_TYPE_NMI_INTR))
7360 skip_emulated_instruction(vcpu);
7361
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007362 if (kvm_task_switch(vcpu, tss_selector,
7363 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7364 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007365 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7366 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7367 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007368 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007369 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007370
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007371 /*
7372 * TODO: What about debug traps on tss switch?
7373 * Are we supposed to inject them and update dr6?
7374 */
7375
7376 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007377}
7378
Avi Kivity851ba692009-08-24 11:10:17 +03007379static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007380{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007381 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007382 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007383 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007384
Sheng Yangf9c617f2009-03-25 10:08:52 +08007385 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007386
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007387 /*
7388 * EPT violation happened while executing iret from NMI,
7389 * "blocked by NMI" bit has to be set before next VM entry.
7390 * There are errata that may cause this bit to not be set:
7391 * AAK134, BY25.
7392 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007393 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007394 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007395 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007396 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7397
Sheng Yang14394422008-04-28 12:24:45 +08007398 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007399 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007400
Junaid Shahid27959a42016-12-06 16:46:10 -08007401 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007402 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007403 ? PFERR_USER_MASK : 0;
7404 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007405 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007406 ? PFERR_WRITE_MASK : 0;
7407 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007408 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007409 ? PFERR_FETCH_MASK : 0;
7410 /* ept page table entry is present? */
7411 error_code |= (exit_qualification &
7412 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7413 EPT_VIOLATION_EXECUTABLE))
7414 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007415
Paolo Bonzinieebed242016-11-28 14:39:58 +01007416 error_code |= (exit_qualification & 0x100) != 0 ?
7417 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007418
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007419 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007420 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007421}
7422
Avi Kivity851ba692009-08-24 11:10:17 +03007423static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007424{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007425 gpa_t gpa;
7426
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007427 /*
7428 * A nested guest cannot optimize MMIO vmexits, because we have an
7429 * nGPA here instead of the required GPA.
7430 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007431 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007432 if (!is_guest_mode(vcpu) &&
7433 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007434 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007435 /*
7436 * Doing kvm_skip_emulated_instruction() depends on undefined
7437 * behavior: Intel's manual doesn't mandate
7438 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7439 * occurs and while on real hardware it was observed to be set,
7440 * other hypervisors (namely Hyper-V) don't set it, we end up
7441 * advancing IP with some random value. Disable fast mmio when
7442 * running nested and keep it for real hardware in hope that
7443 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7444 */
7445 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7446 return kvm_skip_emulated_instruction(vcpu);
7447 else
7448 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7449 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007450 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007451
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007452 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007453}
7454
Avi Kivity851ba692009-08-24 11:10:17 +03007455static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007456{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007457 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007458 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7459 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007460 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007461 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007462
7463 return 1;
7464}
7465
Mohammed Gamal80ced182009-09-01 12:48:18 +02007466static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007467{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007468 struct vcpu_vmx *vmx = to_vmx(vcpu);
7469 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007470 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007471 u32 cpu_exec_ctrl;
7472 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007473 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007474
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007475 /*
7476 * We should never reach the point where we are emulating L2
7477 * due to invalid guest state as that means we incorrectly
7478 * allowed a nested VMEntry with an invalid vmcs12.
7479 */
7480 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7481
Avi Kivity49e9d552010-09-19 14:34:08 +02007482 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7483 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007484
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007485 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007486 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007487 return handle_interrupt_window(&vmx->vcpu);
7488
Radim Krčmář72875d82017-04-26 22:32:19 +02007489 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007490 return 1;
7491
Liran Alon9b8ae632017-11-05 16:56:34 +02007492 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007493
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007494 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007495 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007496 ret = 0;
7497 goto out;
7498 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007499
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007500 if (err != EMULATE_DONE)
7501 goto emulation_error;
7502
7503 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7504 vcpu->arch.exception.pending)
7505 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007506
Gleb Natapov8d76c492013-05-08 18:38:44 +03007507 if (vcpu->arch.halt_request) {
7508 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007509 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007510 goto out;
7511 }
7512
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007513 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007514 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007515 if (need_resched())
7516 schedule();
7517 }
7518
Mohammed Gamal80ced182009-09-01 12:48:18 +02007519out:
7520 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007521
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007522emulation_error:
7523 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7524 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7525 vcpu->run->internal.ndata = 0;
7526 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007527}
7528
7529static void grow_ple_window(struct kvm_vcpu *vcpu)
7530{
7531 struct vcpu_vmx *vmx = to_vmx(vcpu);
7532 int old = vmx->ple_window;
7533
Babu Mogerc8e88712018-03-16 16:37:24 -04007534 vmx->ple_window = __grow_ple_window(old, ple_window,
7535 ple_window_grow,
7536 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007537
7538 if (vmx->ple_window != old)
7539 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007540
7541 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007542}
7543
7544static void shrink_ple_window(struct kvm_vcpu *vcpu)
7545{
7546 struct vcpu_vmx *vmx = to_vmx(vcpu);
7547 int old = vmx->ple_window;
7548
Babu Mogerc8e88712018-03-16 16:37:24 -04007549 vmx->ple_window = __shrink_ple_window(old, ple_window,
7550 ple_window_shrink,
7551 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007552
7553 if (vmx->ple_window != old)
7554 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007555
7556 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007557}
7558
7559/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007560 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7561 */
7562static void wakeup_handler(void)
7563{
7564 struct kvm_vcpu *vcpu;
7565 int cpu = smp_processor_id();
7566
7567 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7568 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7569 blocked_vcpu_list) {
7570 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7571
7572 if (pi_test_on(pi_desc) == 1)
7573 kvm_vcpu_kick(vcpu);
7574 }
7575 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7576}
7577
Peng Haoe01bca22018-04-07 05:47:32 +08007578static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007579{
7580 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7581 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7582 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7583 0ull, VMX_EPT_EXECUTABLE_MASK,
7584 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007585 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007586
7587 ept_set_mmio_spte_mask();
7588 kvm_enable_tdp();
7589}
7590
Tiejun Chenf2c76482014-10-28 10:14:47 +08007591static __init int hardware_setup(void)
7592{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007593 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007594
7595 rdmsrl_safe(MSR_EFER, &host_efer);
7596
7597 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7598 kvm_define_shared_msr(i, vmx_msr_index[i]);
7599
Radim Krčmář23611332016-09-29 22:41:33 +02007600 for (i = 0; i < VMX_BITMAP_NR; i++) {
7601 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7602 if (!vmx_bitmap[i])
7603 goto out;
7604 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007605
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007606 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7607 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7608
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007609 if (setup_vmcs_config(&vmcs_config) < 0) {
7610 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007611 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007612 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007613
7614 if (boot_cpu_has(X86_FEATURE_NX))
7615 kvm_enable_efer_bits(EFER_NX);
7616
Wanpeng Li08d839c2017-03-23 05:30:08 -07007617 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7618 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007619 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007620
Tiejun Chenf2c76482014-10-28 10:14:47 +08007621 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007622 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007623 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007624 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007625 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007626
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007627 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007628 enable_ept_ad_bits = 0;
7629
Wanpeng Li8ad81822017-10-09 15:51:53 -07007630 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007631 enable_unrestricted_guest = 0;
7632
Paolo Bonziniad15a292015-01-30 16:18:49 +01007633 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007634 flexpriority_enabled = 0;
7635
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007636 if (!cpu_has_virtual_nmis())
7637 enable_vnmi = 0;
7638
Paolo Bonziniad15a292015-01-30 16:18:49 +01007639 /*
7640 * set_apic_access_page_addr() is used to reload apic access
7641 * page upon invalidation. No need to do anything if not
7642 * using the APIC_ACCESS_ADDR VMCS field.
7643 */
7644 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007645 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007646
7647 if (!cpu_has_vmx_tpr_shadow())
7648 kvm_x86_ops->update_cr8_intercept = NULL;
7649
7650 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7651 kvm_disable_largepages();
7652
Wanpeng Li0f107682017-09-28 18:06:24 -07007653 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007654 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007655 ple_window = 0;
7656 ple_window_grow = 0;
7657 ple_window_max = 0;
7658 ple_window_shrink = 0;
7659 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007660
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007661 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007662 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007663 kvm_x86_ops->sync_pir_to_irr = NULL;
7664 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007665
Haozhong Zhang64903d62015-10-20 15:39:09 +08007666 if (cpu_has_vmx_tsc_scaling()) {
7667 kvm_has_tsc_control = true;
7668 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7669 kvm_tsc_scaling_ratio_frac_bits = 48;
7670 }
7671
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007672 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7673
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007674 if (enable_ept)
7675 vmx_enable_tdp();
7676 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007677 kvm_disable_tdp();
7678
Kai Huang843e4332015-01-28 10:54:28 +08007679 /*
7680 * Only enable PML when hardware supports PML feature, and both EPT
7681 * and EPT A/D bit features are enabled -- PML depends on them to work.
7682 */
7683 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7684 enable_pml = 0;
7685
7686 if (!enable_pml) {
7687 kvm_x86_ops->slot_enable_log_dirty = NULL;
7688 kvm_x86_ops->slot_disable_log_dirty = NULL;
7689 kvm_x86_ops->flush_log_dirty = NULL;
7690 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7691 }
7692
Yunhong Jiang64672c92016-06-13 14:19:59 -07007693 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7694 u64 vmx_msr;
7695
7696 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7697 cpu_preemption_timer_multi =
7698 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7699 } else {
7700 kvm_x86_ops->set_hv_timer = NULL;
7701 kvm_x86_ops->cancel_hv_timer = NULL;
7702 }
7703
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007704 if (!cpu_has_vmx_shadow_vmcs())
7705 enable_shadow_vmcs = 0;
7706 if (enable_shadow_vmcs)
7707 init_vmcs_shadow_fields();
7708
Feng Wubf9f6ac2015-09-18 22:29:55 +08007709 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007710 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007711
Ashok Rajc45dcc72016-06-22 14:59:56 +08007712 kvm_mce_cap_supported |= MCG_LMCE_P;
7713
Tiejun Chenf2c76482014-10-28 10:14:47 +08007714 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007715
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007716out:
Radim Krčmář23611332016-09-29 22:41:33 +02007717 for (i = 0; i < VMX_BITMAP_NR; i++)
7718 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007719
7720 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007721}
7722
7723static __exit void hardware_unsetup(void)
7724{
Radim Krčmář23611332016-09-29 22:41:33 +02007725 int i;
7726
7727 for (i = 0; i < VMX_BITMAP_NR; i++)
7728 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007729
Tiejun Chenf2c76482014-10-28 10:14:47 +08007730 free_kvm_area();
7731}
7732
Avi Kivity6aa8b732006-12-10 02:21:36 -08007733/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007734 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7735 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7736 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007737static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007738{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007739 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007740 grow_ple_window(vcpu);
7741
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007742 /*
7743 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7744 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7745 * never set PAUSE_EXITING and just set PLE if supported,
7746 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7747 */
7748 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007749 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007750}
7751
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007752static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007753{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007754 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007755}
7756
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007757static int handle_mwait(struct kvm_vcpu *vcpu)
7758{
7759 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7760 return handle_nop(vcpu);
7761}
7762
Jim Mattson45ec3682017-08-23 16:32:04 -07007763static int handle_invalid_op(struct kvm_vcpu *vcpu)
7764{
7765 kvm_queue_exception(vcpu, UD_VECTOR);
7766 return 1;
7767}
7768
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007769static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7770{
7771 return 1;
7772}
7773
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007774static int handle_monitor(struct kvm_vcpu *vcpu)
7775{
7776 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7777 return handle_nop(vcpu);
7778}
7779
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007780/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007781 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7782 * set the success or error code of an emulated VMX instruction, as specified
7783 * by Vol 2B, VMX Instruction Reference, "Conventions".
7784 */
7785static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7786{
7787 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7788 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7789 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7790}
7791
7792static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7793{
7794 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7795 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7796 X86_EFLAGS_SF | X86_EFLAGS_OF))
7797 | X86_EFLAGS_CF);
7798}
7799
Abel Gordon145c28d2013-04-18 14:36:55 +03007800static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007801 u32 vm_instruction_error)
7802{
7803 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7804 /*
7805 * failValid writes the error number to the current VMCS, which
7806 * can't be done there isn't a current VMCS.
7807 */
7808 nested_vmx_failInvalid(vcpu);
7809 return;
7810 }
7811 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7812 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7813 X86_EFLAGS_SF | X86_EFLAGS_OF))
7814 | X86_EFLAGS_ZF);
7815 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7816 /*
7817 * We don't need to force a shadow sync because
7818 * VM_INSTRUCTION_ERROR is not shadowed
7819 */
7820}
Abel Gordon145c28d2013-04-18 14:36:55 +03007821
Wincy Vanff651cb2014-12-11 08:52:58 +03007822static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7823{
7824 /* TODO: not to reset guest simply here. */
7825 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007826 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007827}
7828
Jan Kiszkaf4124502014-03-07 20:03:13 +01007829static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7830{
7831 struct vcpu_vmx *vmx =
7832 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7833
7834 vmx->nested.preemption_timer_expired = true;
7835 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7836 kvm_vcpu_kick(&vmx->vcpu);
7837
7838 return HRTIMER_NORESTART;
7839}
7840
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007841/*
Bandan Das19677e32014-05-06 02:19:15 -04007842 * Decode the memory-address operand of a vmx instruction, as recorded on an
7843 * exit caused by such an instruction (run by a guest hypervisor).
7844 * On success, returns 0. When the operand is invalid, returns 1 and throws
7845 * #UD or #GP.
7846 */
7847static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7848 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007849 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007850{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007851 gva_t off;
7852 bool exn;
7853 struct kvm_segment s;
7854
Bandan Das19677e32014-05-06 02:19:15 -04007855 /*
7856 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7857 * Execution", on an exit, vmx_instruction_info holds most of the
7858 * addressing components of the operand. Only the displacement part
7859 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7860 * For how an actual address is calculated from all these components,
7861 * refer to Vol. 1, "Operand Addressing".
7862 */
7863 int scaling = vmx_instruction_info & 3;
7864 int addr_size = (vmx_instruction_info >> 7) & 7;
7865 bool is_reg = vmx_instruction_info & (1u << 10);
7866 int seg_reg = (vmx_instruction_info >> 15) & 7;
7867 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7868 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7869 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7870 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7871
7872 if (is_reg) {
7873 kvm_queue_exception(vcpu, UD_VECTOR);
7874 return 1;
7875 }
7876
7877 /* Addr = segment_base + offset */
7878 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007879 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007880 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007881 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007882 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007883 off += kvm_register_read(vcpu, index_reg)<<scaling;
7884 vmx_get_segment(vcpu, &s, seg_reg);
7885 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007886
7887 if (addr_size == 1) /* 32 bit */
7888 *ret &= 0xffffffff;
7889
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007890 /* Checks for #GP/#SS exceptions. */
7891 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007892 if (is_long_mode(vcpu)) {
7893 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7894 * non-canonical form. This is the only check on the memory
7895 * destination for long mode!
7896 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007897 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007898 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007899 /* Protected mode: apply checks for segment validity in the
7900 * following order:
7901 * - segment type check (#GP(0) may be thrown)
7902 * - usability check (#GP(0)/#SS(0))
7903 * - limit check (#GP(0)/#SS(0))
7904 */
7905 if (wr)
7906 /* #GP(0) if the destination operand is located in a
7907 * read-only data segment or any code segment.
7908 */
7909 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7910 else
7911 /* #GP(0) if the source operand is located in an
7912 * execute-only code segment
7913 */
7914 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007915 if (exn) {
7916 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7917 return 1;
7918 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007919 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7920 */
7921 exn = (s.unusable != 0);
7922 /* Protected mode: #GP(0)/#SS(0) if the memory
7923 * operand is outside the segment limit.
7924 */
7925 exn = exn || (off + sizeof(u64) > s.limit);
7926 }
7927 if (exn) {
7928 kvm_queue_exception_e(vcpu,
7929 seg_reg == VCPU_SREG_SS ?
7930 SS_VECTOR : GP_VECTOR,
7931 0);
7932 return 1;
7933 }
7934
Bandan Das19677e32014-05-06 02:19:15 -04007935 return 0;
7936}
7937
Radim Krčmářcbf71272017-05-19 15:48:51 +02007938static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007939{
7940 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007941 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007942
7943 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007944 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007945 return 1;
7946
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007947 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007948 kvm_inject_page_fault(vcpu, &e);
7949 return 1;
7950 }
7951
Bandan Das3573e222014-05-06 02:19:16 -04007952 return 0;
7953}
7954
Jim Mattsone29acc52016-11-30 12:03:43 -08007955static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7956{
7957 struct vcpu_vmx *vmx = to_vmx(vcpu);
7958 struct vmcs *shadow_vmcs;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007959 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08007960
Paolo Bonzinif21f1652018-01-11 12:16:15 +01007961 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7962 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06007963 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08007964
7965 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7966 if (!vmx->nested.cached_vmcs12)
7967 goto out_cached_vmcs12;
7968
7969 if (enable_shadow_vmcs) {
7970 shadow_vmcs = alloc_vmcs();
7971 if (!shadow_vmcs)
7972 goto out_shadow_vmcs;
7973 /* mark vmcs as shadow */
7974 shadow_vmcs->revision_id |= (1u << 31);
7975 /* init shadow vmcs */
7976 vmcs_clear(shadow_vmcs);
7977 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7978 }
7979
Jim Mattsone29acc52016-11-30 12:03:43 -08007980 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7981 HRTIMER_MODE_REL_PINNED);
7982 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7983
7984 vmx->nested.vmxon = true;
7985 return 0;
7986
7987out_shadow_vmcs:
7988 kfree(vmx->nested.cached_vmcs12);
7989
7990out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06007991 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08007992
Jim Mattsonde3a0022017-11-27 17:22:25 -06007993out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08007994 return -ENOMEM;
7995}
7996
Bandan Das3573e222014-05-06 02:19:16 -04007997/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007998 * Emulate the VMXON instruction.
7999 * Currently, we just remember that VMX is active, and do not save or even
8000 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8001 * do not currently need to store anything in that guest-allocated memory
8002 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8003 * argument is different from the VMXON pointer (which the spec says they do).
8004 */
8005static int handle_vmon(struct kvm_vcpu *vcpu)
8006{
Jim Mattsone29acc52016-11-30 12:03:43 -08008007 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008008 gpa_t vmptr;
8009 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008010 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008011 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8012 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008013
Jim Mattson70f3aac2017-04-26 08:53:46 -07008014 /*
8015 * The Intel VMX Instruction Reference lists a bunch of bits that are
8016 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8017 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8018 * Otherwise, we should fail with #UD. But most faulting conditions
8019 * have already been checked by hardware, prior to the VM-exit for
8020 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8021 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008022 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008023 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008024 kvm_queue_exception(vcpu, UD_VECTOR);
8025 return 1;
8026 }
8027
Felix Wilhelm727ba742018-06-11 09:43:44 +02008028 /* CPL=0 must be checked manually. */
8029 if (vmx_get_cpl(vcpu)) {
8030 kvm_queue_exception(vcpu, UD_VECTOR);
8031 return 1;
8032 }
8033
Abel Gordon145c28d2013-04-18 14:36:55 +03008034 if (vmx->nested.vmxon) {
8035 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008036 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008037 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008038
Haozhong Zhang3b840802016-06-22 14:59:54 +08008039 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008040 != VMXON_NEEDED_FEATURES) {
8041 kvm_inject_gp(vcpu, 0);
8042 return 1;
8043 }
8044
Radim Krčmářcbf71272017-05-19 15:48:51 +02008045 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008046 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008047
8048 /*
8049 * SDM 3: 24.11.5
8050 * The first 4 bytes of VMXON region contain the supported
8051 * VMCS revision identifier
8052 *
8053 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8054 * which replaces physical address width with 32
8055 */
8056 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8057 nested_vmx_failInvalid(vcpu);
8058 return kvm_skip_emulated_instruction(vcpu);
8059 }
8060
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008061 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8062 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008063 nested_vmx_failInvalid(vcpu);
8064 return kvm_skip_emulated_instruction(vcpu);
8065 }
8066 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8067 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008068 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008069 nested_vmx_failInvalid(vcpu);
8070 return kvm_skip_emulated_instruction(vcpu);
8071 }
8072 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008073 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008074
8075 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008076 ret = enter_vmx_operation(vcpu);
8077 if (ret)
8078 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008079
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008080 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008081 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008082}
8083
8084/*
8085 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8086 * for running VMX instructions (except VMXON, whose prerequisites are
8087 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008088 * Note that many of these exceptions have priority over VM exits, so they
8089 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008090 */
8091static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8092{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008093 if (vmx_get_cpl(vcpu)) {
8094 kvm_queue_exception(vcpu, UD_VECTOR);
8095 return 0;
8096 }
8097
Jim Mattson70f3aac2017-04-26 08:53:46 -07008098 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008099 kvm_queue_exception(vcpu, UD_VECTOR);
8100 return 0;
8101 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008102 return 1;
8103}
8104
David Matlack8ca44e82017-08-01 14:00:39 -07008105static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8106{
8107 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8108 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8109}
8110
Abel Gordone7953d72013-04-18 14:37:55 +03008111static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8112{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008113 if (vmx->nested.current_vmptr == -1ull)
8114 return;
8115
Abel Gordon012f83c2013-04-18 14:39:25 +03008116 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008117 /* copy to memory all shadowed fields in case
8118 they were modified */
8119 copy_shadow_to_vmcs12(vmx);
8120 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008121 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008122 }
Wincy Van705699a2015-02-03 23:58:17 +08008123 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008124
8125 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008126 kvm_vcpu_write_guest_page(&vmx->vcpu,
8127 vmx->nested.current_vmptr >> PAGE_SHIFT,
8128 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008129
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008130 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008131}
8132
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008133/*
8134 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8135 * just stops using VMX.
8136 */
8137static void free_nested(struct vcpu_vmx *vmx)
8138{
Wanpeng Lib7455822017-11-22 14:04:00 -08008139 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008140 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008141
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008142 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008143 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008144 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008145 vmx->nested.posted_intr_nv = -1;
8146 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008147 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008148 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008149 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8150 free_vmcs(vmx->vmcs01.shadow_vmcs);
8151 vmx->vmcs01.shadow_vmcs = NULL;
8152 }
David Matlack4f2777b2016-07-13 17:16:37 -07008153 kfree(vmx->nested.cached_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008154 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008155 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008156 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008157 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008158 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008159 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008160 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008161 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008162 }
Wincy Van705699a2015-02-03 23:58:17 +08008163 if (vmx->nested.pi_desc_page) {
8164 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008165 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008166 vmx->nested.pi_desc_page = NULL;
8167 vmx->nested.pi_desc = NULL;
8168 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008169
Jim Mattsonde3a0022017-11-27 17:22:25 -06008170 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008171}
8172
8173/* Emulate the VMXOFF instruction */
8174static int handle_vmoff(struct kvm_vcpu *vcpu)
8175{
8176 if (!nested_vmx_check_permission(vcpu))
8177 return 1;
8178 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008179 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008180 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008181}
8182
Nadav Har'El27d6c862011-05-25 23:06:59 +03008183/* Emulate the VMCLEAR instruction */
8184static int handle_vmclear(struct kvm_vcpu *vcpu)
8185{
8186 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008187 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008188 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008189
8190 if (!nested_vmx_check_permission(vcpu))
8191 return 1;
8192
Radim Krčmářcbf71272017-05-19 15:48:51 +02008193 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008194 return 1;
8195
Radim Krčmářcbf71272017-05-19 15:48:51 +02008196 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8197 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8198 return kvm_skip_emulated_instruction(vcpu);
8199 }
8200
8201 if (vmptr == vmx->nested.vmxon_ptr) {
8202 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8203 return kvm_skip_emulated_instruction(vcpu);
8204 }
8205
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008206 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008207 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008208
Jim Mattson587d7e722017-03-02 12:41:48 -08008209 kvm_vcpu_write_guest(vcpu,
8210 vmptr + offsetof(struct vmcs12, launch_state),
8211 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008212
Nadav Har'El27d6c862011-05-25 23:06:59 +03008213 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008214 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008215}
8216
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008217static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8218
8219/* Emulate the VMLAUNCH instruction */
8220static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8221{
8222 return nested_vmx_run(vcpu, true);
8223}
8224
8225/* Emulate the VMRESUME instruction */
8226static int handle_vmresume(struct kvm_vcpu *vcpu)
8227{
8228
8229 return nested_vmx_run(vcpu, false);
8230}
8231
Nadav Har'El49f705c2011-05-25 23:08:30 +03008232/*
8233 * Read a vmcs12 field. Since these can have varying lengths and we return
8234 * one type, we chose the biggest type (u64) and zero-extend the return value
8235 * to that size. Note that the caller, handle_vmread, might need to use only
8236 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8237 * 64-bit fields are to be returned).
8238 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008239static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
8240 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008241{
8242 short offset = vmcs_field_to_offset(field);
8243 char *p;
8244
8245 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008246 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008247
8248 p = ((char *)(get_vmcs12(vcpu))) + offset;
8249
Jim Mattsond37f4262017-12-22 12:12:16 -08008250 switch (vmcs_field_width(field)) {
8251 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008252 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008253 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008254 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008255 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008256 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008257 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008258 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008259 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008260 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008261 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008262 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008263 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008264 WARN_ON(1);
8265 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008266 }
8267}
8268
Abel Gordon20b97fe2013-04-18 14:36:25 +03008269
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008270static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
8271 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008272 short offset = vmcs_field_to_offset(field);
8273 char *p = ((char *) get_vmcs12(vcpu)) + offset;
8274 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008275 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008276
Jim Mattsond37f4262017-12-22 12:12:16 -08008277 switch (vmcs_field_width(field)) {
8278 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008279 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008280 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008281 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008282 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008283 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008284 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008285 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008286 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008287 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008288 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008289 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008290 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008291 WARN_ON(1);
8292 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008293 }
8294
8295}
8296
Jim Mattsonf4160e42018-05-29 09:11:33 -07008297/*
8298 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8299 * they have been modified by the L1 guest. Note that the "read-only"
8300 * VM-exit information fields are actually writable if the vCPU is
8301 * configured to support "VMWRITE to any supported field in the VMCS."
8302 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008303static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8304{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008305 const u16 *fields[] = {
8306 shadow_read_write_fields,
8307 shadow_read_only_fields
8308 };
8309 const int max_fields[] = {
8310 max_shadow_read_write_fields,
8311 max_shadow_read_only_fields
8312 };
8313 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008314 unsigned long field;
8315 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008316 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008317
Jan Kiszka282da872014-10-08 18:05:39 +02008318 preempt_disable();
8319
Abel Gordon16f5b902013-04-18 14:38:25 +03008320 vmcs_load(shadow_vmcs);
8321
Jim Mattsonf4160e42018-05-29 09:11:33 -07008322 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8323 for (i = 0; i < max_fields[q]; i++) {
8324 field = fields[q][i];
8325 field_value = __vmcs_readl(field);
8326 vmcs12_write_any(&vmx->vcpu, field, field_value);
8327 }
8328 /*
8329 * Skip the VM-exit information fields if they are read-only.
8330 */
8331 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8332 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008333 }
8334
8335 vmcs_clear(shadow_vmcs);
8336 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008337
8338 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008339}
8340
Abel Gordonc3114422013-04-18 14:38:55 +03008341static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8342{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008343 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008344 shadow_read_write_fields,
8345 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008346 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008347 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008348 max_shadow_read_write_fields,
8349 max_shadow_read_only_fields
8350 };
8351 int i, q;
8352 unsigned long field;
8353 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008354 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008355
8356 vmcs_load(shadow_vmcs);
8357
Mathias Krausec2bae892013-06-26 20:36:21 +02008358 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008359 for (i = 0; i < max_fields[q]; i++) {
8360 field = fields[q][i];
8361 vmcs12_read_any(&vmx->vcpu, field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008362 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008363 }
8364 }
8365
8366 vmcs_clear(shadow_vmcs);
8367 vmcs_load(vmx->loaded_vmcs->vmcs);
8368}
8369
Nadav Har'El49f705c2011-05-25 23:08:30 +03008370/*
8371 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8372 * used before) all generate the same failure when it is missing.
8373 */
8374static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8375{
8376 struct vcpu_vmx *vmx = to_vmx(vcpu);
8377 if (vmx->nested.current_vmptr == -1ull) {
8378 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008379 return 0;
8380 }
8381 return 1;
8382}
8383
8384static int handle_vmread(struct kvm_vcpu *vcpu)
8385{
8386 unsigned long field;
8387 u64 field_value;
8388 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8389 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8390 gva_t gva = 0;
8391
Kyle Hueyeb277562016-11-29 12:40:39 -08008392 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008393 return 1;
8394
Kyle Huey6affcbe2016-11-29 12:40:40 -08008395 if (!nested_vmx_check_vmcs12(vcpu))
8396 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008397
Nadav Har'El49f705c2011-05-25 23:08:30 +03008398 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008399 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008400 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008401 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008402 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008403 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008404 }
8405 /*
8406 * Now copy part of this value to register or memory, as requested.
8407 * Note that the number of bits actually copied is 32 or 64 depending
8408 * on the guest's mode (32 or 64 bit), not on the given field's length.
8409 */
8410 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008411 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008412 field_value);
8413 } else {
8414 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008415 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008416 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008417 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008418 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8419 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008420 }
8421
8422 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008423 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008424}
8425
8426
8427static int handle_vmwrite(struct kvm_vcpu *vcpu)
8428{
8429 unsigned long field;
8430 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008431 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008432 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8433 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008434
Nadav Har'El49f705c2011-05-25 23:08:30 +03008435 /* The value to write might be 32 or 64 bits, depending on L1's long
8436 * mode, and eventually we need to write that into a field of several
8437 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008438 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008439 * bits into the vmcs12 field.
8440 */
8441 u64 field_value = 0;
8442 struct x86_exception e;
8443
Kyle Hueyeb277562016-11-29 12:40:39 -08008444 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008445 return 1;
8446
Kyle Huey6affcbe2016-11-29 12:40:40 -08008447 if (!nested_vmx_check_vmcs12(vcpu))
8448 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008449
Nadav Har'El49f705c2011-05-25 23:08:30 +03008450 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008451 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008452 (((vmx_instruction_info) >> 3) & 0xf));
8453 else {
8454 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008455 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008456 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008457 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8458 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008459 kvm_inject_page_fault(vcpu, &e);
8460 return 1;
8461 }
8462 }
8463
8464
Nadav Amit27e6fb52014-06-18 17:19:26 +03008465 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008466 /*
8467 * If the vCPU supports "VMWRITE to any supported field in the
8468 * VMCS," then the "read-only" fields are actually read/write.
8469 */
8470 if (vmcs_field_readonly(field) &&
8471 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008472 nested_vmx_failValid(vcpu,
8473 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008474 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008475 }
8476
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008477 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008478 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008479 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008480 }
8481
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008482 switch (field) {
8483#define SHADOW_FIELD_RW(x) case x:
8484#include "vmx_shadow_fields.h"
8485 /*
8486 * The fields that can be updated by L1 without a vmexit are
8487 * always updated in the vmcs02, the others go down the slow
8488 * path of prepare_vmcs02.
8489 */
8490 break;
8491 default:
8492 vmx->nested.dirty_vmcs12 = true;
8493 break;
8494 }
8495
Nadav Har'El49f705c2011-05-25 23:08:30 +03008496 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008497 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008498}
8499
Jim Mattsona8bc2842016-11-30 12:03:44 -08008500static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8501{
8502 vmx->nested.current_vmptr = vmptr;
8503 if (enable_shadow_vmcs) {
8504 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8505 SECONDARY_EXEC_SHADOW_VMCS);
8506 vmcs_write64(VMCS_LINK_POINTER,
8507 __pa(vmx->vmcs01.shadow_vmcs));
8508 vmx->nested.sync_shadow_vmcs = true;
8509 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008510 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008511}
8512
Nadav Har'El63846662011-05-25 23:07:29 +03008513/* Emulate the VMPTRLD instruction */
8514static int handle_vmptrld(struct kvm_vcpu *vcpu)
8515{
8516 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008517 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008518
8519 if (!nested_vmx_check_permission(vcpu))
8520 return 1;
8521
Radim Krčmářcbf71272017-05-19 15:48:51 +02008522 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008523 return 1;
8524
Radim Krčmářcbf71272017-05-19 15:48:51 +02008525 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8526 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8527 return kvm_skip_emulated_instruction(vcpu);
8528 }
8529
8530 if (vmptr == vmx->nested.vmxon_ptr) {
8531 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8532 return kvm_skip_emulated_instruction(vcpu);
8533 }
8534
Nadav Har'El63846662011-05-25 23:07:29 +03008535 if (vmx->nested.current_vmptr != vmptr) {
8536 struct vmcs12 *new_vmcs12;
8537 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008538 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8539 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008540 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008541 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008542 }
8543 new_vmcs12 = kmap(page);
8544 if (new_vmcs12->revision_id != VMCS12_REVISION) {
8545 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008546 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008547 nested_vmx_failValid(vcpu,
8548 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008549 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008550 }
Nadav Har'El63846662011-05-25 23:07:29 +03008551
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008552 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008553 /*
8554 * Load VMCS12 from guest memory since it is not already
8555 * cached.
8556 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008557 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8558 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008559 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008560
Jim Mattsona8bc2842016-11-30 12:03:44 -08008561 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008562 }
8563
8564 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008565 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008566}
8567
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008568/* Emulate the VMPTRST instruction */
8569static int handle_vmptrst(struct kvm_vcpu *vcpu)
8570{
8571 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8572 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8573 gva_t vmcs_gva;
8574 struct x86_exception e;
8575
8576 if (!nested_vmx_check_permission(vcpu))
8577 return 1;
8578
8579 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008580 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008581 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008582 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008583 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8584 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8585 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008586 kvm_inject_page_fault(vcpu, &e);
8587 return 1;
8588 }
8589 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008590 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008591}
8592
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008593/* Emulate the INVEPT instruction */
8594static int handle_invept(struct kvm_vcpu *vcpu)
8595{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008596 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008597 u32 vmx_instruction_info, types;
8598 unsigned long type;
8599 gva_t gva;
8600 struct x86_exception e;
8601 struct {
8602 u64 eptp, gpa;
8603 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008604
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008605 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008606 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008607 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008608 kvm_queue_exception(vcpu, UD_VECTOR);
8609 return 1;
8610 }
8611
8612 if (!nested_vmx_check_permission(vcpu))
8613 return 1;
8614
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008615 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008616 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008617
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008618 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008619
Jim Mattson85c856b2016-10-26 08:38:38 -07008620 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008621 nested_vmx_failValid(vcpu,
8622 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008623 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008624 }
8625
8626 /* According to the Intel VMX instruction reference, the memory
8627 * operand is read even if it isn't needed (e.g., for type==global)
8628 */
8629 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008630 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008631 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008632 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008633 kvm_inject_page_fault(vcpu, &e);
8634 return 1;
8635 }
8636
8637 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008638 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008639 /*
8640 * TODO: track mappings and invalidate
8641 * single context requests appropriately
8642 */
8643 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008644 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008645 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008646 nested_vmx_succeed(vcpu);
8647 break;
8648 default:
8649 BUG_ON(1);
8650 break;
8651 }
8652
Kyle Huey6affcbe2016-11-29 12:40:40 -08008653 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008654}
8655
Petr Matouseka642fc32014-09-23 20:22:30 +02008656static int handle_invvpid(struct kvm_vcpu *vcpu)
8657{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008658 struct vcpu_vmx *vmx = to_vmx(vcpu);
8659 u32 vmx_instruction_info;
8660 unsigned long type, types;
8661 gva_t gva;
8662 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008663 struct {
8664 u64 vpid;
8665 u64 gla;
8666 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008667
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008668 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008669 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008670 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008671 kvm_queue_exception(vcpu, UD_VECTOR);
8672 return 1;
8673 }
8674
8675 if (!nested_vmx_check_permission(vcpu))
8676 return 1;
8677
8678 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8679 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8680
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008681 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008682 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008683
Jim Mattson85c856b2016-10-26 08:38:38 -07008684 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008685 nested_vmx_failValid(vcpu,
8686 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008687 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008688 }
8689
8690 /* according to the intel vmx instruction reference, the memory
8691 * operand is read even if it isn't needed (e.g., for type==global)
8692 */
8693 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8694 vmx_instruction_info, false, &gva))
8695 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008696 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008697 kvm_inject_page_fault(vcpu, &e);
8698 return 1;
8699 }
Jim Mattson40352602017-06-28 09:37:37 -07008700 if (operand.vpid >> 16) {
8701 nested_vmx_failValid(vcpu,
8702 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8703 return kvm_skip_emulated_instruction(vcpu);
8704 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008705
8706 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008707 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008708 if (!operand.vpid ||
8709 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008710 nested_vmx_failValid(vcpu,
8711 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8712 return kvm_skip_emulated_instruction(vcpu);
8713 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008714 if (cpu_has_vmx_invvpid_individual_addr() &&
8715 vmx->nested.vpid02) {
8716 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8717 vmx->nested.vpid02, operand.gla);
8718 } else
8719 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8720 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008721 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008722 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008723 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008724 nested_vmx_failValid(vcpu,
8725 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008726 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008727 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008728 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008729 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008730 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008731 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008732 break;
8733 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008734 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008735 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008736 }
8737
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008738 nested_vmx_succeed(vcpu);
8739
Kyle Huey6affcbe2016-11-29 12:40:40 -08008740 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008741}
8742
Kai Huang843e4332015-01-28 10:54:28 +08008743static int handle_pml_full(struct kvm_vcpu *vcpu)
8744{
8745 unsigned long exit_qualification;
8746
8747 trace_kvm_pml_full(vcpu->vcpu_id);
8748
8749 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8750
8751 /*
8752 * PML buffer FULL happened while executing iret from NMI,
8753 * "blocked by NMI" bit has to be set before next VM entry.
8754 */
8755 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008756 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008757 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8758 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8759 GUEST_INTR_STATE_NMI);
8760
8761 /*
8762 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8763 * here.., and there's no userspace involvement needed for PML.
8764 */
8765 return 1;
8766}
8767
Yunhong Jiang64672c92016-06-13 14:19:59 -07008768static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8769{
8770 kvm_lapic_expired_hv_timer(vcpu);
8771 return 1;
8772}
8773
Bandan Das41ab9372017-08-03 15:54:43 -04008774static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8775{
8776 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008777 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8778
8779 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008780 switch (address & VMX_EPTP_MT_MASK) {
8781 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008782 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008783 return false;
8784 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008785 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008786 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008787 return false;
8788 break;
8789 default:
8790 return false;
8791 }
8792
David Hildenbrandbb97a012017-08-10 23:15:28 +02008793 /* only 4 levels page-walk length are valid */
8794 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04008795 return false;
8796
8797 /* Reserved bits should not be set */
8798 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
8799 return false;
8800
8801 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008802 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008803 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008804 return false;
8805 }
8806
8807 return true;
8808}
8809
8810static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
8811 struct vmcs12 *vmcs12)
8812{
8813 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
8814 u64 address;
8815 bool accessed_dirty;
8816 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
8817
8818 if (!nested_cpu_has_eptp_switching(vmcs12) ||
8819 !nested_cpu_has_ept(vmcs12))
8820 return 1;
8821
8822 if (index >= VMFUNC_EPTP_ENTRIES)
8823 return 1;
8824
8825
8826 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
8827 &address, index * 8, 8))
8828 return 1;
8829
David Hildenbrandbb97a012017-08-10 23:15:28 +02008830 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04008831
8832 /*
8833 * If the (L2) guest does a vmfunc to the currently
8834 * active ept pointer, we don't have to do anything else
8835 */
8836 if (vmcs12->ept_pointer != address) {
8837 if (!valid_ept_address(vcpu, address))
8838 return 1;
8839
8840 kvm_mmu_unload(vcpu);
8841 mmu->ept_ad = accessed_dirty;
8842 mmu->base_role.ad_disabled = !accessed_dirty;
8843 vmcs12->ept_pointer = address;
8844 /*
8845 * TODO: Check what's the correct approach in case
8846 * mmu reload fails. Currently, we just let the next
8847 * reload potentially fail
8848 */
8849 kvm_mmu_reload(vcpu);
8850 }
8851
8852 return 0;
8853}
8854
Bandan Das2a499e42017-08-03 15:54:41 -04008855static int handle_vmfunc(struct kvm_vcpu *vcpu)
8856{
Bandan Das27c42a12017-08-03 15:54:42 -04008857 struct vcpu_vmx *vmx = to_vmx(vcpu);
8858 struct vmcs12 *vmcs12;
8859 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
8860
8861 /*
8862 * VMFUNC is only supported for nested guests, but we always enable the
8863 * secondary control for simplicity; for non-nested mode, fake that we
8864 * didn't by injecting #UD.
8865 */
8866 if (!is_guest_mode(vcpu)) {
8867 kvm_queue_exception(vcpu, UD_VECTOR);
8868 return 1;
8869 }
8870
8871 vmcs12 = get_vmcs12(vcpu);
8872 if ((vmcs12->vm_function_control & (1 << function)) == 0)
8873 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04008874
8875 switch (function) {
8876 case 0:
8877 if (nested_vmx_eptp_switching(vcpu, vmcs12))
8878 goto fail;
8879 break;
8880 default:
8881 goto fail;
8882 }
8883 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04008884
8885fail:
8886 nested_vmx_vmexit(vcpu, vmx->exit_reason,
8887 vmcs_read32(VM_EXIT_INTR_INFO),
8888 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04008889 return 1;
8890}
8891
Nadav Har'El0140cae2011-05-25 23:06:28 +03008892/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08008893 * The exit handlers return 1 if the exit was handled fully and guest execution
8894 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
8895 * to be done to userspace and return 0.
8896 */
Mathias Krause772e0312012-08-30 01:30:19 +02008897static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008898 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
8899 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08008900 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08008901 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008902 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008903 [EXIT_REASON_CR_ACCESS] = handle_cr,
8904 [EXIT_REASON_DR_ACCESS] = handle_dr,
8905 [EXIT_REASON_CPUID] = handle_cpuid,
8906 [EXIT_REASON_MSR_READ] = handle_rdmsr,
8907 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
8908 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
8909 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02008910 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03008911 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02008912 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02008913 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03008914 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008915 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03008916 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008917 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008918 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008919 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008920 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008921 [EXIT_REASON_VMOFF] = handle_vmoff,
8922 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08008923 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
8924 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08008925 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008926 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02008927 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08008928 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02008929 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08008930 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02008931 [EXIT_REASON_GDTR_IDTR] = handle_desc,
8932 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03008933 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
8934 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008935 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008936 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008937 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008938 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008939 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02008940 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07008941 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07008942 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08008943 [EXIT_REASON_XSAVES] = handle_xsaves,
8944 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08008945 [EXIT_REASON_PML_FULL] = handle_pml_full,
Bandan Das2a499e42017-08-03 15:54:41 -04008946 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07008947 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008948};
8949
8950static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04008951 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008952
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008953static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
8954 struct vmcs12 *vmcs12)
8955{
8956 unsigned long exit_qualification;
8957 gpa_t bitmap, last_bitmap;
8958 unsigned int port;
8959 int size;
8960 u8 b;
8961
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008962 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05008963 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008964
8965 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8966
8967 port = exit_qualification >> 16;
8968 size = (exit_qualification & 7) + 1;
8969
8970 last_bitmap = (gpa_t)-1;
8971 b = -1;
8972
8973 while (size > 0) {
8974 if (port < 0x8000)
8975 bitmap = vmcs12->io_bitmap_a;
8976 else if (port < 0x10000)
8977 bitmap = vmcs12->io_bitmap_b;
8978 else
Joe Perches1d804d02015-03-30 16:46:09 -07008979 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008980 bitmap += (port & 0x7fff) / 8;
8981
8982 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008983 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07008984 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008985 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07008986 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008987
8988 port++;
8989 size--;
8990 last_bitmap = bitmap;
8991 }
8992
Joe Perches1d804d02015-03-30 16:46:09 -07008993 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008994}
8995
Nadav Har'El644d7112011-05-25 23:12:35 +03008996/*
8997 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
8998 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
8999 * disinterest in the current event (read or write a specific MSR) by using an
9000 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9001 */
9002static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9003 struct vmcs12 *vmcs12, u32 exit_reason)
9004{
9005 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9006 gpa_t bitmap;
9007
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009008 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009009 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009010
9011 /*
9012 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9013 * for the four combinations of read/write and low/high MSR numbers.
9014 * First we need to figure out which of the four to use:
9015 */
9016 bitmap = vmcs12->msr_bitmap;
9017 if (exit_reason == EXIT_REASON_MSR_WRITE)
9018 bitmap += 2048;
9019 if (msr_index >= 0xc0000000) {
9020 msr_index -= 0xc0000000;
9021 bitmap += 1024;
9022 }
9023
9024 /* Then read the msr_index'th bit from this bitmap: */
9025 if (msr_index < 1024*8) {
9026 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009027 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009028 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009029 return 1 & (b >> (msr_index & 7));
9030 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009031 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009032}
9033
9034/*
9035 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9036 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9037 * intercept (via guest_host_mask etc.) the current event.
9038 */
9039static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9040 struct vmcs12 *vmcs12)
9041{
9042 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9043 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009044 int reg;
9045 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009046
9047 switch ((exit_qualification >> 4) & 3) {
9048 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009049 reg = (exit_qualification >> 8) & 15;
9050 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009051 switch (cr) {
9052 case 0:
9053 if (vmcs12->cr0_guest_host_mask &
9054 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009055 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009056 break;
9057 case 3:
9058 if ((vmcs12->cr3_target_count >= 1 &&
9059 vmcs12->cr3_target_value0 == val) ||
9060 (vmcs12->cr3_target_count >= 2 &&
9061 vmcs12->cr3_target_value1 == val) ||
9062 (vmcs12->cr3_target_count >= 3 &&
9063 vmcs12->cr3_target_value2 == val) ||
9064 (vmcs12->cr3_target_count >= 4 &&
9065 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009066 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009067 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009068 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009069 break;
9070 case 4:
9071 if (vmcs12->cr4_guest_host_mask &
9072 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009073 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009074 break;
9075 case 8:
9076 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009077 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009078 break;
9079 }
9080 break;
9081 case 2: /* clts */
9082 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9083 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009084 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009085 break;
9086 case 1: /* mov from cr */
9087 switch (cr) {
9088 case 3:
9089 if (vmcs12->cpu_based_vm_exec_control &
9090 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009091 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009092 break;
9093 case 8:
9094 if (vmcs12->cpu_based_vm_exec_control &
9095 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009096 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009097 break;
9098 }
9099 break;
9100 case 3: /* lmsw */
9101 /*
9102 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9103 * cr0. Other attempted changes are ignored, with no exit.
9104 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009105 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009106 if (vmcs12->cr0_guest_host_mask & 0xe &
9107 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009108 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009109 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9110 !(vmcs12->cr0_read_shadow & 0x1) &&
9111 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009112 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009113 break;
9114 }
Joe Perches1d804d02015-03-30 16:46:09 -07009115 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009116}
9117
9118/*
9119 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9120 * should handle it ourselves in L0 (and then continue L2). Only call this
9121 * when in is_guest_mode (L2).
9122 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009123static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009124{
Nadav Har'El644d7112011-05-25 23:12:35 +03009125 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9126 struct vcpu_vmx *vmx = to_vmx(vcpu);
9127 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9128
Jim Mattson4f350c62017-09-14 16:31:44 -07009129 if (vmx->nested.nested_run_pending)
9130 return false;
9131
9132 if (unlikely(vmx->fail)) {
9133 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9134 vmcs_read32(VM_INSTRUCTION_ERROR));
9135 return true;
9136 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009137
David Matlackc9f04402017-08-01 14:00:40 -07009138 /*
9139 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009140 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9141 * Page). The CPU may write to these pages via their host
9142 * physical address while L2 is running, bypassing any
9143 * address-translation-based dirty tracking (e.g. EPT write
9144 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009145 *
9146 * Mark them dirty on every exit from L2 to prevent them from
9147 * getting out of sync with dirty tracking.
9148 */
9149 nested_mark_vmcs12_pages_dirty(vcpu);
9150
Jim Mattson4f350c62017-09-14 16:31:44 -07009151 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9152 vmcs_readl(EXIT_QUALIFICATION),
9153 vmx->idt_vectoring_info,
9154 intr_info,
9155 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9156 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009157
9158 switch (exit_reason) {
9159 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009160 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009161 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009162 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009163 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009164 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009165 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009166 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009167 else if (is_debug(intr_info) &&
9168 vcpu->guest_debug &
9169 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9170 return false;
9171 else if (is_breakpoint(intr_info) &&
9172 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9173 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009174 return vmcs12->exception_bitmap &
9175 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9176 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009177 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009178 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009179 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009180 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009181 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009182 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009183 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009184 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009185 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009186 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009187 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009188 case EXIT_REASON_HLT:
9189 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9190 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009191 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009192 case EXIT_REASON_INVLPG:
9193 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9194 case EXIT_REASON_RDPMC:
9195 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009196 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009197 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009198 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009199 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009200 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009201 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
9202 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9203 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
9204 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
9205 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
9206 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009207 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009208 /*
9209 * VMX instructions trap unconditionally. This allows L1 to
9210 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9211 */
Joe Perches1d804d02015-03-30 16:46:09 -07009212 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009213 case EXIT_REASON_CR_ACCESS:
9214 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9215 case EXIT_REASON_DR_ACCESS:
9216 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9217 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009218 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009219 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9220 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009221 case EXIT_REASON_MSR_READ:
9222 case EXIT_REASON_MSR_WRITE:
9223 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9224 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009225 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009226 case EXIT_REASON_MWAIT_INSTRUCTION:
9227 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009228 case EXIT_REASON_MONITOR_TRAP_FLAG:
9229 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009230 case EXIT_REASON_MONITOR_INSTRUCTION:
9231 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9232 case EXIT_REASON_PAUSE_INSTRUCTION:
9233 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9234 nested_cpu_has2(vmcs12,
9235 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9236 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009237 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009238 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009239 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009240 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009241 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009242 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009243 /*
9244 * The controls for "virtualize APIC accesses," "APIC-
9245 * register virtualization," and "virtual-interrupt
9246 * delivery" only come from vmcs12.
9247 */
Joe Perches1d804d02015-03-30 16:46:09 -07009248 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009249 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009250 /*
9251 * L0 always deals with the EPT violation. If nested EPT is
9252 * used, and the nested mmu code discovers that the address is
9253 * missing in the guest EPT table (EPT12), the EPT violation
9254 * will be injected with nested_ept_inject_page_fault()
9255 */
Joe Perches1d804d02015-03-30 16:46:09 -07009256 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009257 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009258 /*
9259 * L2 never uses directly L1's EPT, but rather L0's own EPT
9260 * table (shadow on EPT) or a merged EPT table that L0 built
9261 * (EPT on EPT). So any problems with the structure of the
9262 * table is L0's fault.
9263 */
Joe Perches1d804d02015-03-30 16:46:09 -07009264 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009265 case EXIT_REASON_INVPCID:
9266 return
9267 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9268 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009269 case EXIT_REASON_WBINVD:
9270 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9271 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009272 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009273 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9274 /*
9275 * This should never happen, since it is not possible to
9276 * set XSS to a non-zero value---neither in L1 nor in L2.
9277 * If if it were, XSS would have to be checked against
9278 * the XSS exit bitmap in vmcs12.
9279 */
9280 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009281 case EXIT_REASON_PREEMPTION_TIMER:
9282 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009283 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009284 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009285 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009286 case EXIT_REASON_VMFUNC:
9287 /* VM functions are emulated through L2->L0 vmexits. */
9288 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009289 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009290 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009291 }
9292}
9293
Paolo Bonzini7313c692017-07-27 10:31:25 +02009294static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9295{
9296 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9297
9298 /*
9299 * At this point, the exit interruption info in exit_intr_info
9300 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9301 * we need to query the in-kernel LAPIC.
9302 */
9303 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9304 if ((exit_intr_info &
9305 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9306 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9307 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9308 vmcs12->vm_exit_intr_error_code =
9309 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9310 }
9311
9312 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9313 vmcs_readl(EXIT_QUALIFICATION));
9314 return 1;
9315}
9316
Avi Kivity586f9602010-11-18 13:09:54 +02009317static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9318{
9319 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9320 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9321}
9322
Kai Huanga3eaa862015-11-04 13:46:05 +08009323static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009324{
Kai Huanga3eaa862015-11-04 13:46:05 +08009325 if (vmx->pml_pg) {
9326 __free_page(vmx->pml_pg);
9327 vmx->pml_pg = NULL;
9328 }
Kai Huang843e4332015-01-28 10:54:28 +08009329}
9330
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009331static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009332{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009333 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009334 u64 *pml_buf;
9335 u16 pml_idx;
9336
9337 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9338
9339 /* Do nothing if PML buffer is empty */
9340 if (pml_idx == (PML_ENTITY_NUM - 1))
9341 return;
9342
9343 /* PML index always points to next available PML buffer entity */
9344 if (pml_idx >= PML_ENTITY_NUM)
9345 pml_idx = 0;
9346 else
9347 pml_idx++;
9348
9349 pml_buf = page_address(vmx->pml_pg);
9350 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9351 u64 gpa;
9352
9353 gpa = pml_buf[pml_idx];
9354 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009355 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009356 }
9357
9358 /* reset PML index */
9359 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9360}
9361
9362/*
9363 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9364 * Called before reporting dirty_bitmap to userspace.
9365 */
9366static void kvm_flush_pml_buffers(struct kvm *kvm)
9367{
9368 int i;
9369 struct kvm_vcpu *vcpu;
9370 /*
9371 * We only need to kick vcpu out of guest mode here, as PML buffer
9372 * is flushed at beginning of all VMEXITs, and it's obvious that only
9373 * vcpus running in guest are possible to have unflushed GPAs in PML
9374 * buffer.
9375 */
9376 kvm_for_each_vcpu(i, vcpu, kvm)
9377 kvm_vcpu_kick(vcpu);
9378}
9379
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009380static void vmx_dump_sel(char *name, uint32_t sel)
9381{
9382 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009383 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009384 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9385 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9386 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9387}
9388
9389static void vmx_dump_dtsel(char *name, uint32_t limit)
9390{
9391 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9392 name, vmcs_read32(limit),
9393 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9394}
9395
9396static void dump_vmcs(void)
9397{
9398 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9399 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9400 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9401 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9402 u32 secondary_exec_control = 0;
9403 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009404 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009405 int i, n;
9406
9407 if (cpu_has_secondary_exec_ctrls())
9408 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9409
9410 pr_err("*** Guest State ***\n");
9411 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9412 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9413 vmcs_readl(CR0_GUEST_HOST_MASK));
9414 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9415 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9416 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9417 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9418 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9419 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009420 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9421 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9422 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9423 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009424 }
9425 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9426 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9427 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9428 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9429 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9430 vmcs_readl(GUEST_SYSENTER_ESP),
9431 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9432 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9433 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9434 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9435 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9436 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9437 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9438 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9439 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9440 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9441 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9442 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9443 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009444 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9445 efer, vmcs_read64(GUEST_IA32_PAT));
9446 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9447 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009448 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009449 if (cpu_has_load_perf_global_ctrl &&
9450 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009451 pr_err("PerfGlobCtl = 0x%016llx\n",
9452 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009453 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009454 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009455 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9456 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9457 vmcs_read32(GUEST_ACTIVITY_STATE));
9458 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9459 pr_err("InterruptStatus = %04x\n",
9460 vmcs_read16(GUEST_INTR_STATUS));
9461
9462 pr_err("*** Host State ***\n");
9463 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9464 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9465 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9466 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9467 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9468 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9469 vmcs_read16(HOST_TR_SELECTOR));
9470 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9471 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9472 vmcs_readl(HOST_TR_BASE));
9473 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9474 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9475 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9476 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9477 vmcs_readl(HOST_CR4));
9478 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9479 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9480 vmcs_read32(HOST_IA32_SYSENTER_CS),
9481 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9482 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009483 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9484 vmcs_read64(HOST_IA32_EFER),
9485 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009486 if (cpu_has_load_perf_global_ctrl &&
9487 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009488 pr_err("PerfGlobCtl = 0x%016llx\n",
9489 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009490
9491 pr_err("*** Control State ***\n");
9492 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9493 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9494 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9495 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9496 vmcs_read32(EXCEPTION_BITMAP),
9497 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9498 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9499 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9500 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9501 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9502 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9503 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9504 vmcs_read32(VM_EXIT_INTR_INFO),
9505 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9506 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9507 pr_err(" reason=%08x qualification=%016lx\n",
9508 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9509 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9510 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9511 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009512 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009513 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009514 pr_err("TSC Multiplier = 0x%016llx\n",
9515 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009516 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9517 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9518 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9519 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9520 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009521 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009522 n = vmcs_read32(CR3_TARGET_COUNT);
9523 for (i = 0; i + 1 < n; i += 4)
9524 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9525 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9526 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9527 if (i < n)
9528 pr_err("CR3 target%u=%016lx\n",
9529 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9530 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9531 pr_err("PLE Gap=%08x Window=%08x\n",
9532 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9533 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9534 pr_err("Virtual processor ID = 0x%04x\n",
9535 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9536}
9537
Avi Kivity6aa8b732006-12-10 02:21:36 -08009538/*
9539 * The guest has exited. See if we can fix it or if we need userspace
9540 * assistance.
9541 */
Avi Kivity851ba692009-08-24 11:10:17 +03009542static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009543{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009544 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009545 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009546 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009547
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009548 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9549
Kai Huang843e4332015-01-28 10:54:28 +08009550 /*
9551 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9552 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9553 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9554 * mode as if vcpus is in root mode, the PML buffer must has been
9555 * flushed already.
9556 */
9557 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009558 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009559
Mohammed Gamal80ced182009-09-01 12:48:18 +02009560 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009561 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009562 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009563
Paolo Bonzini7313c692017-07-27 10:31:25 +02009564 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9565 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009566
Mohammed Gamal51207022010-05-31 22:40:54 +03009567 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009568 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009569 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9570 vcpu->run->fail_entry.hardware_entry_failure_reason
9571 = exit_reason;
9572 return 0;
9573 }
9574
Avi Kivity29bd8a72007-09-10 17:27:03 +03009575 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009576 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9577 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009578 = vmcs_read32(VM_INSTRUCTION_ERROR);
9579 return 0;
9580 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009581
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009582 /*
9583 * Note:
9584 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9585 * delivery event since it indicates guest is accessing MMIO.
9586 * The vm-exit can be triggered again after return to guest that
9587 * will cause infinite loop.
9588 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009589 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009590 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009591 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009592 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009593 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9594 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9595 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009596 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009597 vcpu->run->internal.data[0] = vectoring_info;
9598 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009599 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9600 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9601 vcpu->run->internal.ndata++;
9602 vcpu->run->internal.data[3] =
9603 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9604 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009605 return 0;
9606 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009607
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009608 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009609 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9610 if (vmx_interrupt_allowed(vcpu)) {
9611 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9612 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9613 vcpu->arch.nmi_pending) {
9614 /*
9615 * This CPU don't support us in finding the end of an
9616 * NMI-blocked window if the guest runs with IRQs
9617 * disabled. So we pull the trigger after 1 s of
9618 * futile waiting, but inform the user about this.
9619 */
9620 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9621 "state on VCPU %d after 1 s timeout\n",
9622 __func__, vcpu->vcpu_id);
9623 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9624 }
9625 }
9626
Avi Kivity6aa8b732006-12-10 02:21:36 -08009627 if (exit_reason < kvm_vmx_max_exit_handlers
9628 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009629 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009630 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009631 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9632 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009633 kvm_queue_exception(vcpu, UD_VECTOR);
9634 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009635 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009636}
9637
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009638/*
9639 * Software based L1D cache flush which is used when microcode providing
9640 * the cache control MSR is not loaded.
9641 *
9642 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
9643 * flush it is required to read in 64 KiB because the replacement algorithm
9644 * is not exactly LRU. This could be sized at runtime via topology
9645 * information but as all relevant affected CPUs have 32KiB L1D cache size
9646 * there is no point in doing so.
9647 */
9648#define L1D_CACHE_ORDER 4
9649static void *vmx_l1d_flush_pages;
9650
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009651static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009652{
9653 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009654 bool always;
9655
9656 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +02009657 * This code is only executed when the the flush mode is 'cond' or
9658 * 'always'
Konrad Rzeszutek Wilk390d9752018-06-28 17:10:36 -04009659 *
Thomas Gleixner2f055942018-07-13 16:23:17 +02009660 * If 'flush always', keep the flush bit set, otherwise clear
9661 * it. The flush bit gets set again either from vcpu_run() or from
9662 * one of the unsafe VMEXIT handlers.
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009663 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +02009664 always = l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_ALWAYS;
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009665 vcpu->arch.l1tf_flush_l1d = always;
9666
9667 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009668
Paolo Bonzini3fa045b2018-07-02 13:03:48 +02009669 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
9670 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
9671 return;
9672 }
9673
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +02009674 asm volatile(
9675 /* First ensure the pages are in the TLB */
9676 "xorl %%eax, %%eax\n"
9677 ".Lpopulate_tlb:\n\t"
9678 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9679 "addl $4096, %%eax\n\t"
9680 "cmpl %%eax, %[size]\n\t"
9681 "jne .Lpopulate_tlb\n\t"
9682 "xorl %%eax, %%eax\n\t"
9683 "cpuid\n\t"
9684 /* Now fill the cache */
9685 "xorl %%eax, %%eax\n"
9686 ".Lfill_cache:\n"
9687 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
9688 "addl $64, %%eax\n\t"
9689 "cmpl %%eax, %[size]\n\t"
9690 "jne .Lfill_cache\n\t"
9691 "lfence\n"
9692 :: [empty_zp] "r" (vmx_l1d_flush_pages),
9693 [size] "r" (size)
9694 : "eax", "ebx", "ecx", "edx");
9695}
9696
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009697static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009698{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009699 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9700
9701 if (is_guest_mode(vcpu) &&
9702 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9703 return;
9704
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009705 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009706 vmcs_write32(TPR_THRESHOLD, 0);
9707 return;
9708 }
9709
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009710 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009711}
9712
Jim Mattson8d860bb2018-05-09 16:56:05 -04009713static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009714{
9715 u32 sec_exec_control;
9716
Jim Mattson8d860bb2018-05-09 16:56:05 -04009717 if (!lapic_in_kernel(vcpu))
9718 return;
9719
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009720 /* Postpone execution until vmcs01 is the current VMCS. */
9721 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009722 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009723 return;
9724 }
9725
Paolo Bonzini35754c92015-07-29 12:05:37 +02009726 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009727 return;
9728
9729 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009730 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9731 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009732
Jim Mattson8d860bb2018-05-09 16:56:05 -04009733 switch (kvm_get_apic_mode(vcpu)) {
9734 case LAPIC_MODE_INVALID:
9735 WARN_ONCE(true, "Invalid local APIC state");
9736 case LAPIC_MODE_DISABLED:
9737 break;
9738 case LAPIC_MODE_XAPIC:
9739 if (flexpriority_enabled) {
9740 sec_exec_control |=
9741 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9742 vmx_flush_tlb(vcpu, true);
9743 }
9744 break;
9745 case LAPIC_MODE_X2APIC:
9746 if (cpu_has_vmx_virtualize_x2apic_mode())
9747 sec_exec_control |=
9748 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9749 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009750 }
9751 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9752
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009753 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009754}
9755
Tang Chen38b99172014-09-24 15:57:54 +08009756static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9757{
Jim Mattsonab5df312018-05-09 17:02:03 -04009758 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009759 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009760 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009761 }
Tang Chen38b99172014-09-24 15:57:54 +08009762}
9763
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009764static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009765{
9766 u16 status;
9767 u8 old;
9768
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009769 if (max_isr == -1)
9770 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009771
9772 status = vmcs_read16(GUEST_INTR_STATUS);
9773 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009774 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009775 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009776 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009777 vmcs_write16(GUEST_INTR_STATUS, status);
9778 }
9779}
9780
9781static void vmx_set_rvi(int vector)
9782{
9783 u16 status;
9784 u8 old;
9785
Wei Wang4114c272014-11-05 10:53:43 +08009786 if (vector == -1)
9787 vector = 0;
9788
Yang Zhangc7c9c562013-01-25 10:18:51 +08009789 status = vmcs_read16(GUEST_INTR_STATUS);
9790 old = (u8)status & 0xff;
9791 if ((u8)vector != old) {
9792 status &= ~0xff;
9793 status |= (u8)vector;
9794 vmcs_write16(GUEST_INTR_STATUS, status);
9795 }
9796}
9797
9798static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9799{
Liran Alon851c1a182017-12-24 18:12:56 +02009800 /*
9801 * When running L2, updating RVI is only relevant when
9802 * vmcs12 virtual-interrupt-delivery enabled.
9803 * However, it can be enabled only when L1 also
9804 * intercepts external-interrupts and in that case
9805 * we should not update vmcs02 RVI but instead intercept
9806 * interrupt. Therefore, do nothing when running L2.
9807 */
9808 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009809 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009810}
9811
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009812static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009813{
9814 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009815 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009816 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009817
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009818 WARN_ON(!vcpu->arch.apicv_active);
9819 if (pi_test_on(&vmx->pi_desc)) {
9820 pi_clear_on(&vmx->pi_desc);
9821 /*
9822 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
9823 * But on x86 this is just a compiler barrier anyway.
9824 */
9825 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +02009826 max_irr_updated =
9827 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
9828
9829 /*
9830 * If we are running L2 and L1 has a new pending interrupt
9831 * which can be injected, we should re-evaluate
9832 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +02009833 * If L1 intercepts external-interrupts, we should
9834 * exit from L2 to L1. Otherwise, interrupt should be
9835 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +02009836 */
Liran Alon851c1a182017-12-24 18:12:56 +02009837 if (is_guest_mode(vcpu) && max_irr_updated) {
9838 if (nested_exit_on_intr(vcpu))
9839 kvm_vcpu_exiting_guest_mode(vcpu);
9840 else
9841 kvm_make_request(KVM_REQ_EVENT, vcpu);
9842 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009843 } else {
9844 max_irr = kvm_lapic_find_highest_irr(vcpu);
9845 }
9846 vmx_hwapic_irr_update(vcpu, max_irr);
9847 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009848}
9849
Andrey Smetanin63086302015-11-10 15:36:32 +03009850static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009851{
Andrey Smetanind62caab2015-11-10 15:36:33 +03009852 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08009853 return;
9854
Yang Zhangc7c9c562013-01-25 10:18:51 +08009855 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
9856 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
9857 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
9858 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
9859}
9860
Paolo Bonzini967235d2016-12-19 14:03:45 +01009861static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
9862{
9863 struct vcpu_vmx *vmx = to_vmx(vcpu);
9864
9865 pi_clear_on(&vmx->pi_desc);
9866 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
9867}
9868
Avi Kivity51aa01d2010-07-20 14:31:20 +03009869static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03009870{
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009871 u32 exit_intr_info = 0;
9872 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +02009873
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009874 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
9875 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +02009876 return;
9877
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009878 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
9879 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9880 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08009881
Wanpeng Li1261bfa2017-07-13 18:30:40 -07009882 /* if exit due to PF check for async PF */
9883 if (is_page_fault(exit_intr_info))
9884 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
9885
Andi Kleena0861c02009-06-08 17:37:09 +08009886 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -07009887 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
9888 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08009889 kvm_machine_check();
9890
Gleb Natapov20f65982009-05-11 13:35:55 +03009891 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08009892 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -07009893 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03009894 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -07009895 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08009896 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03009897}
Gleb Natapov20f65982009-05-11 13:35:55 +03009898
Yang Zhanga547c6d2013-04-11 19:25:10 +08009899static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
9900{
9901 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9902
Yang Zhanga547c6d2013-04-11 19:25:10 +08009903 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
9904 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
9905 unsigned int vector;
9906 unsigned long entry;
9907 gate_desc *desc;
9908 struct vcpu_vmx *vmx = to_vmx(vcpu);
9909#ifdef CONFIG_X86_64
9910 unsigned long tmp;
9911#endif
9912
9913 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9914 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +02009915 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009916 asm volatile(
9917#ifdef CONFIG_X86_64
9918 "mov %%" _ASM_SP ", %[sp]\n\t"
9919 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
9920 "push $%c[ss]\n\t"
9921 "push %[sp]\n\t"
9922#endif
9923 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08009924 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009925 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08009926 :
9927#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06009928 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009929#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -05009930 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08009931 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +01009932 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08009933 [ss]"i"(__KERNEL_DS),
9934 [cs]"i"(__KERNEL_CS)
9935 );
Paolo Bonzinic595cee2018-07-02 13:07:14 +02009936 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02009937 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08009938}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05009939STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08009940
Tom Lendackybc226f02018-05-10 22:06:39 +02009941static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009942{
Tom Lendackybc226f02018-05-10 22:06:39 +02009943 switch (index) {
9944 case MSR_IA32_SMBASE:
9945 /*
9946 * We cannot do SMM unless we can run the guest in big
9947 * real mode.
9948 */
9949 return enable_unrestricted_guest || emulate_invalid_guest_state;
9950 case MSR_AMD64_VIRT_SPEC_CTRL:
9951 /* This is AMD only. */
9952 return false;
9953 default:
9954 return true;
9955 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02009956}
9957
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009958static bool vmx_mpx_supported(void)
9959{
9960 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
9961 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
9962}
9963
Wanpeng Li55412b22014-12-02 19:21:30 +08009964static bool vmx_xsaves_supported(void)
9965{
9966 return vmcs_config.cpu_based_2nd_exec_ctrl &
9967 SECONDARY_EXEC_XSAVES;
9968}
9969
Avi Kivity51aa01d2010-07-20 14:31:20 +03009970static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
9971{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02009972 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03009973 bool unblock_nmi;
9974 u8 vector;
9975 bool idtv_info_valid;
9976
9977 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03009978
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009979 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009980 if (vmx->loaded_vmcs->nmi_known_unmasked)
9981 return;
9982 /*
9983 * Can't use vmx->exit_intr_info since we're not sure what
9984 * the exit reason is.
9985 */
9986 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9987 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
9988 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
9989 /*
9990 * SDM 3: 27.7.1.2 (September 2008)
9991 * Re-set bit "block by NMI" before VM entry if vmexit caused by
9992 * a guest IRET fault.
9993 * SDM 3: 23.2.2 (September 2008)
9994 * Bit 12 is undefined in any of the following cases:
9995 * If the VM exit sets the valid bit in the IDT-vectoring
9996 * information field.
9997 * If the VM exit is due to a double fault.
9998 */
9999 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10000 vector != DF_VECTOR && !idtv_info_valid)
10001 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10002 GUEST_INTR_STATE_NMI);
10003 else
10004 vmx->loaded_vmcs->nmi_known_unmasked =
10005 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10006 & GUEST_INTR_STATE_NMI);
10007 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10008 vmx->loaded_vmcs->vnmi_blocked_time +=
10009 ktime_to_ns(ktime_sub(ktime_get(),
10010 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010011}
10012
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010013static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010014 u32 idt_vectoring_info,
10015 int instr_len_field,
10016 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010017{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010018 u8 vector;
10019 int type;
10020 bool idtv_info_valid;
10021
10022 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010023
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010024 vcpu->arch.nmi_injected = false;
10025 kvm_clear_exception_queue(vcpu);
10026 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010027
10028 if (!idtv_info_valid)
10029 return;
10030
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010031 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010032
Avi Kivity668f6122008-07-02 09:28:55 +030010033 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10034 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010035
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010036 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010037 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010038 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010039 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010040 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010041 * Clear bit "block by NMI" before VM entry if a NMI
10042 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010043 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010044 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010045 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010046 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010047 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010048 /* fall through */
10049 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010050 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010051 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010052 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010053 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010054 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010055 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010056 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010057 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010058 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010059 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010060 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010061 break;
10062 default:
10063 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010064 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010065}
10066
Avi Kivity83422e12010-07-20 14:43:23 +030010067static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10068{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010069 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010070 VM_EXIT_INSTRUCTION_LEN,
10071 IDT_VECTORING_ERROR_CODE);
10072}
10073
Avi Kivityb463a6f2010-07-20 15:06:17 +030010074static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10075{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010076 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010077 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10078 VM_ENTRY_INSTRUCTION_LEN,
10079 VM_ENTRY_EXCEPTION_ERROR_CODE);
10080
10081 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10082}
10083
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010084static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10085{
10086 int i, nr_msrs;
10087 struct perf_guest_switch_msr *msrs;
10088
10089 msrs = perf_guest_get_msrs(&nr_msrs);
10090
10091 if (!msrs)
10092 return;
10093
10094 for (i = 0; i < nr_msrs; i++)
10095 if (msrs[i].host == msrs[i].guest)
10096 clear_atomic_switch_msr(vmx, msrs[i].msr);
10097 else
10098 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010099 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010100}
10101
Jiang Biao33365e72016-11-03 15:03:37 +080010102static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010103{
10104 struct vcpu_vmx *vmx = to_vmx(vcpu);
10105 u64 tscl;
10106 u32 delta_tsc;
10107
10108 if (vmx->hv_deadline_tsc == -1)
10109 return;
10110
10111 tscl = rdtsc();
10112 if (vmx->hv_deadline_tsc > tscl)
10113 /* sure to be 32 bit only because checked on set_hv_timer */
10114 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10115 cpu_preemption_timer_multi);
10116 else
10117 delta_tsc = 0;
10118
10119 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10120}
10121
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010122static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010123{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010124 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010125 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010126
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010127 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010128 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010129 vmx->loaded_vmcs->soft_vnmi_blocked))
10130 vmx->loaded_vmcs->entry_time = ktime_get();
10131
Avi Kivity104f2262010-11-18 13:12:52 +020010132 /* Don't enter VMX if guest state is invalid, let the exit handler
10133 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010134 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010135 return;
10136
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010137 if (vmx->ple_window_dirty) {
10138 vmx->ple_window_dirty = false;
10139 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10140 }
10141
Abel Gordon012f83c2013-04-18 14:39:25 +030010142 if (vmx->nested.sync_shadow_vmcs) {
10143 copy_vmcs12_to_shadow(vmx);
10144 vmx->nested.sync_shadow_vmcs = false;
10145 }
10146
Avi Kivity104f2262010-11-18 13:12:52 +020010147 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10148 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10149 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10150 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10151
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010152 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010153 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010154 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010155 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010156 }
10157
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010158 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010159 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010160 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010161 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010162 }
10163
Avi Kivity104f2262010-11-18 13:12:52 +020010164 /* When single-stepping over STI and MOV SS, we must clear the
10165 * corresponding interruptibility bits in the guest state. Otherwise
10166 * vmentry fails as it then expects bit 14 (BS) in pending debug
10167 * exceptions being set, but that's not correct for the guest debugging
10168 * case. */
10169 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10170 vmx_set_interrupt_shadow(vcpu, 0);
10171
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010172 if (static_cpu_has(X86_FEATURE_PKU) &&
10173 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10174 vcpu->arch.pkru != vmx->host_pkru)
10175 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010176
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010177 atomic_switch_perf_msrs(vmx);
10178
Yunhong Jiang64672c92016-06-13 14:19:59 -070010179 vmx_arm_hv_timer(vcpu);
10180
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010181 /*
10182 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10183 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10184 * is no need to worry about the conditional branch over the wrmsr
10185 * being speculatively taken.
10186 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010187 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010188
Nadav Har'Eld462b812011-05-24 15:26:10 +030010189 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010190
10191 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10192 (unsigned long)&current_evmcs->host_rsp : 0;
10193
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010194 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
10195 if (vcpu->arch.l1tf_flush_l1d)
10196 vmx_l1d_flush(vcpu);
10197 }
10198
Avi Kivity104f2262010-11-18 13:12:52 +020010199 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010200 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010201 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10202 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10203 "push %%" _ASM_CX " \n\t"
10204 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010205 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010206 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010207 /* Avoid VMWRITE when Enlightened VMCS is in use */
10208 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10209 "jz 2f \n\t"
10210 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10211 "jmp 1f \n\t"
10212 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010213 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010214 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010215 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010216 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10217 "mov %%cr2, %%" _ASM_DX " \n\t"
10218 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010219 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010220 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010221 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010222 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010223 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010224 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010225 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10226 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10227 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10228 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10229 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10230 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010231#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010232 "mov %c[r8](%0), %%r8 \n\t"
10233 "mov %c[r9](%0), %%r9 \n\t"
10234 "mov %c[r10](%0), %%r10 \n\t"
10235 "mov %c[r11](%0), %%r11 \n\t"
10236 "mov %c[r12](%0), %%r12 \n\t"
10237 "mov %c[r13](%0), %%r13 \n\t"
10238 "mov %c[r14](%0), %%r14 \n\t"
10239 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010240#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010241 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010242
Avi Kivity6aa8b732006-12-10 02:21:36 -080010243 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010244 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010245 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010246 "jmp 2f \n\t"
10247 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10248 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010249 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010250 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010251 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010252 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010253 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10254 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10255 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10256 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10257 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10258 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10259 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010260#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010261 "mov %%r8, %c[r8](%0) \n\t"
10262 "mov %%r9, %c[r9](%0) \n\t"
10263 "mov %%r10, %c[r10](%0) \n\t"
10264 "mov %%r11, %c[r11](%0) \n\t"
10265 "mov %%r12, %c[r12](%0) \n\t"
10266 "mov %%r13, %c[r13](%0) \n\t"
10267 "mov %%r14, %c[r14](%0) \n\t"
10268 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010269 "xor %%r8d, %%r8d \n\t"
10270 "xor %%r9d, %%r9d \n\t"
10271 "xor %%r10d, %%r10d \n\t"
10272 "xor %%r11d, %%r11d \n\t"
10273 "xor %%r12d, %%r12d \n\t"
10274 "xor %%r13d, %%r13d \n\t"
10275 "xor %%r14d, %%r14d \n\t"
10276 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010277#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010278 "mov %%cr2, %%" _ASM_AX " \n\t"
10279 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010280
Jim Mattson0cb5b302018-01-03 14:31:38 -080010281 "xor %%eax, %%eax \n\t"
10282 "xor %%ebx, %%ebx \n\t"
10283 "xor %%esi, %%esi \n\t"
10284 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010285 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010286 ".pushsection .rodata \n\t"
10287 ".global vmx_return \n\t"
10288 "vmx_return: " _ASM_PTR " 2b \n\t"
10289 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010290 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010291 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010292 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010293 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010294 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10295 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10296 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10297 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10298 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10299 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10300 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010301#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010302 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10303 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10304 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10305 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10306 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10307 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10308 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10309 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010310#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010311 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10312 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010313 : "cc", "memory"
10314#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010315 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010316 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010317#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010318 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010319#endif
10320 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010321
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010322 /*
10323 * We do not use IBRS in the kernel. If this vCPU has used the
10324 * SPEC_CTRL MSR it may have left it on; save the value and
10325 * turn it off. This is much more efficient than blindly adding
10326 * it to the atomic save/restore list. Especially as the former
10327 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10328 *
10329 * For non-nested case:
10330 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10331 * save it.
10332 *
10333 * For nested case:
10334 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10335 * save it.
10336 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010337 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010338 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010339
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010340 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010341
David Woodhouse117cc7a2018-01-12 11:11:27 +000010342 /* Eliminate branch target predictions from guest mode */
10343 vmexit_fill_RSB();
10344
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010345 /* All fields are clean at this point */
10346 if (static_branch_unlikely(&enable_evmcs))
10347 current_evmcs->hv_clean_fields |=
10348 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10349
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010350 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010351 if (vmx->host_debugctlmsr)
10352 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010353
Avi Kivityaa67f602012-08-01 16:48:03 +030010354#ifndef CONFIG_X86_64
10355 /*
10356 * The sysexit path does not restore ds/es, so we must set them to
10357 * a reasonable value ourselves.
10358 *
10359 * We can't defer this to vmx_load_host_state() since that function
10360 * may be executed in interrupt context, which saves and restore segments
10361 * around it, nullifying its effect.
10362 */
10363 loadsegment(ds, __USER_DS);
10364 loadsegment(es, __USER_DS);
10365#endif
10366
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010367 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010368 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010369 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010370 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010371 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010372 vcpu->arch.regs_dirty = 0;
10373
Gleb Natapove0b890d2013-09-25 12:51:33 +030010374 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010375 * eager fpu is enabled if PKEY is supported and CR4 is switched
10376 * back on host, so it is safe to read guest PKRU from current
10377 * XSAVE.
10378 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010379 if (static_cpu_has(X86_FEATURE_PKU) &&
10380 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10381 vcpu->arch.pkru = __read_pkru();
10382 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010383 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010384 }
10385
Gleb Natapove0b890d2013-09-25 12:51:33 +030010386 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010387 vmx->idt_vectoring_info = 0;
10388
10389 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10390 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10391 return;
10392
10393 vmx->loaded_vmcs->launched = 1;
10394 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010395
Avi Kivity51aa01d2010-07-20 14:31:20 +030010396 vmx_complete_atomic_exit(vmx);
10397 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010398 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010399}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010400STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010401
Sean Christopherson434a1e92018-03-20 12:17:18 -070010402static struct kvm *vmx_vm_alloc(void)
10403{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010404 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010405 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010406}
10407
10408static void vmx_vm_free(struct kvm *kvm)
10409{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010410 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010411}
10412
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010413static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010414{
10415 struct vcpu_vmx *vmx = to_vmx(vcpu);
10416 int cpu;
10417
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010418 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010419 return;
10420
10421 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010422 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010423 vmx_vcpu_put(vcpu);
10424 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010425 put_cpu();
10426}
10427
Jim Mattson2f1fe812016-07-08 15:36:06 -070010428/*
10429 * Ensure that the current vmcs of the logical processor is the
10430 * vmcs01 of the vcpu before calling free_nested().
10431 */
10432static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10433{
10434 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010435
Christoffer Dallec7660c2017-12-04 21:35:23 +010010436 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010437 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010438 free_nested(vmx);
10439 vcpu_put(vcpu);
10440}
10441
Avi Kivity6aa8b732006-12-10 02:21:36 -080010442static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10443{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010444 struct vcpu_vmx *vmx = to_vmx(vcpu);
10445
Kai Huang843e4332015-01-28 10:54:28 +080010446 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010447 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010448 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010449 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010450 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010451 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010452 kfree(vmx->guest_msrs);
10453 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010454 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010455}
10456
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010457static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010458{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010459 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010460 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010461 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010462 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010463
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010464 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010465 return ERR_PTR(-ENOMEM);
10466
Wanpeng Li991e7a02015-09-16 17:30:05 +080010467 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010468
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010469 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10470 if (err)
10471 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010472
Peter Feiner4e595162016-07-07 14:49:58 -070010473 err = -ENOMEM;
10474
10475 /*
10476 * If PML is turned on, failure on enabling PML just results in failure
10477 * of creating the vcpu, therefore we can simplify PML logic (by
10478 * avoiding dealing with cases, such as enabling PML partially on vcpus
10479 * for the guest, etc.
10480 */
10481 if (enable_pml) {
10482 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10483 if (!vmx->pml_pg)
10484 goto uninit_vcpu;
10485 }
10486
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010487 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010488 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10489 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010490
Peter Feiner4e595162016-07-07 14:49:58 -070010491 if (!vmx->guest_msrs)
10492 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010493
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010494 err = alloc_loaded_vmcs(&vmx->vmcs01);
10495 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010496 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010497
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010498 msr_bitmap = vmx->vmcs01.msr_bitmap;
10499 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10500 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10501 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10502 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10503 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10504 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10505 vmx->msr_bitmap_mode = 0;
10506
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010507 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010508 cpu = get_cpu();
10509 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010510 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010511 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010512 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010513 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010514 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010515 err = alloc_apic_access_page(kvm);
10516 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010517 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010518 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010519
Sean Christophersone90008d2018-03-05 12:04:37 -080010520 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010521 err = init_rmode_identity_map(kvm);
10522 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010523 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010524 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010525
Wanpeng Li5c614b32015-10-13 09:18:36 -070010526 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010527 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10528 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010529 vmx->nested.vpid02 = allocate_vpid();
10530 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010531
Wincy Van705699a2015-02-03 23:58:17 +080010532 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010533 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010534
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010535 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10536
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010537 /*
10538 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10539 * or POSTED_INTR_WAKEUP_VECTOR.
10540 */
10541 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10542 vmx->pi_desc.sn = 1;
10543
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010544 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010545
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010546free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010547 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010548 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010549free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010550 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010551free_pml:
10552 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010553uninit_vcpu:
10554 kvm_vcpu_uninit(&vmx->vcpu);
10555free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010556 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010557 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010558 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010559}
10560
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010561#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
10562
Wanpeng Lib31c1142018-03-12 04:53:04 -070010563static int vmx_vm_init(struct kvm *kvm)
10564{
10565 if (!ple_gap)
10566 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040010567
10568 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
10569 if (nosmt) {
10570 pr_err(L1TF_MSG);
10571 return -EOPNOTSUPP;
10572 }
10573 pr_warn(L1TF_MSG);
10574 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070010575 return 0;
10576}
10577
Yang, Sheng002c7f72007-07-31 14:23:01 +030010578static void __init vmx_check_processor_compat(void *rtn)
10579{
10580 struct vmcs_config vmcs_conf;
10581
10582 *(int *)rtn = 0;
10583 if (setup_vmcs_config(&vmcs_conf) < 0)
10584 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010585 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010586 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10587 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10588 smp_processor_id());
10589 *(int *)rtn = -EIO;
10590 }
10591}
10592
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010593static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010594{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010595 u8 cache;
10596 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010597
Sheng Yang522c68c2009-04-27 20:35:43 +080010598 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010599 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010600 * 2. EPT with VT-d:
10601 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010602 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010603 * b. VT-d with snooping control feature: snooping control feature of
10604 * VT-d engine can guarantee the cache correctness. Just set it
10605 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010606 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010607 * consistent with host MTRR
10608 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010609 if (is_mmio) {
10610 cache = MTRR_TYPE_UNCACHABLE;
10611 goto exit;
10612 }
10613
10614 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010615 ipat = VMX_EPT_IPAT_BIT;
10616 cache = MTRR_TYPE_WRBACK;
10617 goto exit;
10618 }
10619
10620 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10621 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010622 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010623 cache = MTRR_TYPE_WRBACK;
10624 else
10625 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010626 goto exit;
10627 }
10628
Xiao Guangrongff536042015-06-15 16:55:22 +080010629 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010630
10631exit:
10632 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010633}
10634
Sheng Yang17cc3932010-01-05 19:02:27 +080010635static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010636{
Sheng Yang878403b2010-01-05 19:02:29 +080010637 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10638 return PT_DIRECTORY_LEVEL;
10639 else
10640 /* For shadow and EPT supported 1GB page */
10641 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010642}
10643
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010644static void vmcs_set_secondary_exec_control(u32 new_ctl)
10645{
10646 /*
10647 * These bits in the secondary execution controls field
10648 * are dynamic, the others are mostly based on the hypervisor
10649 * architecture and the guest's CPUID. Do not touch the
10650 * dynamic bits.
10651 */
10652 u32 mask =
10653 SECONDARY_EXEC_SHADOW_VMCS |
10654 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010655 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10656 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010657
10658 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10659
10660 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10661 (new_ctl & ~mask) | (cur_ctl & mask));
10662}
10663
David Matlack8322ebb2016-11-29 18:14:09 -080010664/*
10665 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10666 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10667 */
10668static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10669{
10670 struct vcpu_vmx *vmx = to_vmx(vcpu);
10671 struct kvm_cpuid_entry2 *entry;
10672
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010673 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10674 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010675
10676#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10677 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010678 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010679} while (0)
10680
10681 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10682 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10683 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10684 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10685 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10686 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10687 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10688 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10689 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10690 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10691 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10692 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10693 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10694 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10695 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10696
10697 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10698 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10699 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10700 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10701 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010702 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010703
10704#undef cr4_fixed1_update
10705}
10706
Sheng Yang0e851882009-12-18 16:48:46 +080010707static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10708{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010709 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010710
Paolo Bonzini80154d72017-08-24 13:55:35 +020010711 if (cpu_has_secondary_exec_ctrls()) {
10712 vmx_compute_secondary_exec_control(vmx);
10713 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010714 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010715
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010716 if (nested_vmx_allowed(vcpu))
10717 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10718 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10719 else
10720 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10721 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010722
10723 if (nested_vmx_allowed(vcpu))
10724 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010725}
10726
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010727static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10728{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010729 if (func == 1 && nested)
10730 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010731}
10732
Yang Zhang25d92082013-08-06 12:00:32 +030010733static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10734 struct x86_exception *fault)
10735{
Jan Kiszka533558b2014-01-04 18:47:20 +010010736 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010737 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010738 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010739 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010740
Bandan Dasc5f983f2017-05-05 15:25:14 -040010741 if (vmx->nested.pml_full) {
10742 exit_reason = EXIT_REASON_PML_FULL;
10743 vmx->nested.pml_full = false;
10744 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10745 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010746 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010747 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010748 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010749
10750 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010751 vmcs12->guest_physical_address = fault->address;
10752}
10753
Peter Feiner995f00a2017-06-30 17:26:32 -070010754static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10755{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010756 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010757}
10758
Nadav Har'El155a97a2013-08-05 11:07:16 +030010759/* Callbacks for nested_ept_init_mmu_context: */
10760
10761static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10762{
10763 /* return the page table to be shadowed - in our case, EPT12 */
10764 return get_vmcs12(vcpu)->ept_pointer;
10765}
10766
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010767static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010768{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010769 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010770 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010771 return 1;
10772
10773 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +020010774 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010775 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010776 VMX_EPT_EXECUTE_ONLY_BIT,
David Hildenbranda057e0e2017-08-10 23:36:54 +020010777 nested_ept_ad_enabled(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010778 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10779 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10780 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10781
10782 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010783 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010784}
10785
10786static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10787{
10788 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10789}
10790
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010791static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10792 u16 error_code)
10793{
10794 bool inequality, bit;
10795
10796 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10797 inequality =
10798 (error_code & vmcs12->page_fault_error_code_mask) !=
10799 vmcs12->page_fault_error_code_match;
10800 return inequality ^ bit;
10801}
10802
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010803static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10804 struct x86_exception *fault)
10805{
10806 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10807
10808 WARN_ON(!is_guest_mode(vcpu));
10809
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010810 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10811 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010812 vmcs12->vm_exit_intr_error_code = fault->error_code;
10813 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10814 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10815 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10816 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010817 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010818 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010819 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010820}
10821
Paolo Bonzinic9923842017-12-13 14:16:30 +010010822static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10823 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010824
10825static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010826 struct vmcs12 *vmcs12)
10827{
10828 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010829 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010830 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010831
10832 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010833 /*
10834 * Translate L1 physical address to host physical
10835 * address for vmcs02. Keep the page pinned, so this
10836 * physical address remains valid. We keep a reference
10837 * to it so we can release it later.
10838 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010839 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010840 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010841 vmx->nested.apic_access_page = NULL;
10842 }
10843 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010844 /*
10845 * If translation failed, no matter: This feature asks
10846 * to exit when accessing the given address, and if it
10847 * can never be accessed, this feature won't do
10848 * anything anyway.
10849 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010850 if (!is_error_page(page)) {
10851 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010852 hpa = page_to_phys(vmx->nested.apic_access_page);
10853 vmcs_write64(APIC_ACCESS_ADDR, hpa);
10854 } else {
10855 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
10856 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
10857 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010858 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010859
10860 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010861 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020010862 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010863 vmx->nested.virtual_apic_page = NULL;
10864 }
10865 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010866
10867 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010868 * If translation failed, VM entry will fail because
10869 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
10870 * Failing the vm entry is _not_ what the processor
10871 * does but it's basically the only possibility we
10872 * have. We could still enter the guest if CR8 load
10873 * exits are enabled, CR8 store exits are enabled, and
10874 * virtualize APIC access is disabled; in this case
10875 * the processor would never use the TPR shadow and we
10876 * could simply clear the bit from the execution
10877 * control. But such a configuration is useless, so
10878 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010879 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010880 if (!is_error_page(page)) {
10881 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010882 hpa = page_to_phys(vmx->nested.virtual_apic_page);
10883 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
10884 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010885 }
10886
Wincy Van705699a2015-02-03 23:58:17 +080010887 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080010888 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
10889 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020010890 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010891 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080010892 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010893 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
10894 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010895 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010896 vmx->nested.pi_desc_page = page;
10897 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080010898 vmx->nested.pi_desc =
10899 (struct pi_desc *)((void *)vmx->nested.pi_desc +
10900 (unsigned long)(vmcs12->posted_intr_desc_addr &
10901 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010902 vmcs_write64(POSTED_INTR_DESC_ADDR,
10903 page_to_phys(vmx->nested.pi_desc_page) +
10904 (unsigned long)(vmcs12->posted_intr_desc_addr &
10905 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080010906 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080010907 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000010908 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
10909 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010910 else
10911 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
10912 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010913}
10914
Jan Kiszkaf4124502014-03-07 20:03:13 +010010915static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
10916{
10917 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
10918 struct vcpu_vmx *vmx = to_vmx(vcpu);
10919
10920 if (vcpu->arch.virtual_tsc_khz == 0)
10921 return;
10922
10923 /* Make sure short timeouts reliably trigger an immediate vmexit.
10924 * hrtimer_start does not guarantee this. */
10925 if (preemption_timeout <= 1) {
10926 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
10927 return;
10928 }
10929
10930 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10931 preemption_timeout *= 1000000;
10932 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
10933 hrtimer_start(&vmx->nested.preemption_timer,
10934 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
10935}
10936
Jim Mattson56a20512017-07-06 16:33:06 -070010937static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
10938 struct vmcs12 *vmcs12)
10939{
10940 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
10941 return 0;
10942
10943 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
10944 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
10945 return -EINVAL;
10946
10947 return 0;
10948}
10949
Wincy Van3af18d92015-02-03 23:49:31 +080010950static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
10951 struct vmcs12 *vmcs12)
10952{
Wincy Van3af18d92015-02-03 23:49:31 +080010953 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
10954 return 0;
10955
Jim Mattson5fa99cb2017-07-06 16:33:07 -070010956 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080010957 return -EINVAL;
10958
10959 return 0;
10960}
10961
Jim Mattson712b12d2017-08-24 13:24:47 -070010962static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
10963 struct vmcs12 *vmcs12)
10964{
10965 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10966 return 0;
10967
10968 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
10969 return -EINVAL;
10970
10971 return 0;
10972}
10973
Wincy Van3af18d92015-02-03 23:49:31 +080010974/*
10975 * Merge L0's and L1's MSR bitmap, return false to indicate that
10976 * we do not use the hardware.
10977 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010010978static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10979 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080010980{
Wincy Van82f0dd42015-02-03 23:57:18 +080010981 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080010982 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020010983 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010984 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010010985 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010986 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010010987 *
10988 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
10989 * ensures that we do not accidentally generate an L02 MSR bitmap
10990 * from the L12 MSR bitmap that is too permissive.
10991 * 2. That L1 or L2s have actually used the MSR. This avoids
10992 * unnecessarily merging of the bitmap if the MSR is unused. This
10993 * works properly because we only update the L01 MSR bitmap lazily.
10994 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
10995 * updated to reflect this when L1 (or its L2s) actually write to
10996 * the MSR.
10997 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000010998 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
10999 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011000
Paolo Bonzinic9923842017-12-13 14:16:30 +010011001 /* Nothing to do if the MSR bitmap is not in use. */
11002 if (!cpu_has_vmx_msr_bitmap() ||
11003 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11004 return false;
11005
Ashok Raj15d45072018-02-01 22:59:43 +010011006 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011007 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011008 return false;
11009
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011010 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11011 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011012 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011013
Radim Krčmářd048c092016-08-08 20:16:22 +020011014 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011015 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11016 /*
11017 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11018 * just lets the processor take the value from the virtual-APIC page;
11019 * take those 256 bits directly from the L1 bitmap.
11020 */
11021 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11022 unsigned word = msr / BITS_PER_LONG;
11023 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11024 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011025 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011026 } else {
11027 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11028 unsigned word = msr / BITS_PER_LONG;
11029 msr_bitmap_l0[word] = ~0;
11030 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11031 }
11032 }
11033
11034 nested_vmx_disable_intercept_for_msr(
11035 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011036 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011037 MSR_TYPE_W);
11038
11039 if (nested_cpu_has_vid(vmcs12)) {
11040 nested_vmx_disable_intercept_for_msr(
11041 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011042 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011043 MSR_TYPE_W);
11044 nested_vmx_disable_intercept_for_msr(
11045 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011046 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011047 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011048 }
Ashok Raj15d45072018-02-01 22:59:43 +010011049
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011050 if (spec_ctrl)
11051 nested_vmx_disable_intercept_for_msr(
11052 msr_bitmap_l1, msr_bitmap_l0,
11053 MSR_IA32_SPEC_CTRL,
11054 MSR_TYPE_R | MSR_TYPE_W);
11055
Ashok Raj15d45072018-02-01 22:59:43 +010011056 if (pred_cmd)
11057 nested_vmx_disable_intercept_for_msr(
11058 msr_bitmap_l1, msr_bitmap_l0,
11059 MSR_IA32_PRED_CMD,
11060 MSR_TYPE_W);
11061
Wincy Vanf2b93282015-02-03 23:56:03 +080011062 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011063 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011064
11065 return true;
11066}
11067
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011068static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11069 struct vmcs12 *vmcs12)
11070{
11071 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11072 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11073 return -EINVAL;
11074 else
11075 return 0;
11076}
11077
Wincy Vanf2b93282015-02-03 23:56:03 +080011078static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11079 struct vmcs12 *vmcs12)
11080{
Wincy Van82f0dd42015-02-03 23:57:18 +080011081 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011082 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011083 !nested_cpu_has_vid(vmcs12) &&
11084 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011085 return 0;
11086
11087 /*
11088 * If virtualize x2apic mode is enabled,
11089 * virtualize apic access must be disabled.
11090 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011091 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11092 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011093 return -EINVAL;
11094
Wincy Van608406e2015-02-03 23:57:51 +080011095 /*
11096 * If virtual interrupt delivery is enabled,
11097 * we must exit on external interrupts.
11098 */
11099 if (nested_cpu_has_vid(vmcs12) &&
11100 !nested_exit_on_intr(vcpu))
11101 return -EINVAL;
11102
Wincy Van705699a2015-02-03 23:58:17 +080011103 /*
11104 * bits 15:8 should be zero in posted_intr_nv,
11105 * the descriptor address has been already checked
11106 * in nested_get_vmcs12_pages.
11107 */
11108 if (nested_cpu_has_posted_intr(vmcs12) &&
11109 (!nested_cpu_has_vid(vmcs12) ||
11110 !nested_exit_intr_ack_set(vcpu) ||
11111 vmcs12->posted_intr_nv & 0xff00))
11112 return -EINVAL;
11113
Wincy Vanf2b93282015-02-03 23:56:03 +080011114 /* tpr shadow is needed by all apicv features. */
11115 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11116 return -EINVAL;
11117
11118 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011119}
11120
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011121static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11122 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011123 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011124{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011125 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011126 u64 count, addr;
11127
11128 if (vmcs12_read_any(vcpu, count_field, &count) ||
11129 vmcs12_read_any(vcpu, addr_field, &addr)) {
11130 WARN_ON(1);
11131 return -EINVAL;
11132 }
11133 if (count == 0)
11134 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011135 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011136 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11137 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011138 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011139 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11140 addr_field, maxphyaddr, count, addr);
11141 return -EINVAL;
11142 }
11143 return 0;
11144}
11145
11146static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11147 struct vmcs12 *vmcs12)
11148{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011149 if (vmcs12->vm_exit_msr_load_count == 0 &&
11150 vmcs12->vm_exit_msr_store_count == 0 &&
11151 vmcs12->vm_entry_msr_load_count == 0)
11152 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011153 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011154 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011155 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011156 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011157 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011158 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011159 return -EINVAL;
11160 return 0;
11161}
11162
Bandan Dasc5f983f2017-05-05 15:25:14 -040011163static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11164 struct vmcs12 *vmcs12)
11165{
11166 u64 address = vmcs12->pml_address;
11167 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11168
11169 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11170 if (!nested_cpu_has_ept(vmcs12) ||
11171 !IS_ALIGNED(address, 4096) ||
11172 address >> maxphyaddr)
11173 return -EINVAL;
11174 }
11175
11176 return 0;
11177}
11178
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011179static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11180 struct vmx_msr_entry *e)
11181{
11182 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011183 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011184 return -EINVAL;
11185 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11186 e->index == MSR_IA32_UCODE_REV)
11187 return -EINVAL;
11188 if (e->reserved != 0)
11189 return -EINVAL;
11190 return 0;
11191}
11192
11193static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11194 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011195{
11196 if (e->index == MSR_FS_BASE ||
11197 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011198 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11199 nested_vmx_msr_check_common(vcpu, e))
11200 return -EINVAL;
11201 return 0;
11202}
11203
11204static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11205 struct vmx_msr_entry *e)
11206{
11207 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11208 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011209 return -EINVAL;
11210 return 0;
11211}
11212
11213/*
11214 * Load guest's/host's msr at nested entry/exit.
11215 * return 0 for success, entry index for failure.
11216 */
11217static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11218{
11219 u32 i;
11220 struct vmx_msr_entry e;
11221 struct msr_data msr;
11222
11223 msr.host_initiated = false;
11224 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011225 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11226 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011227 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011228 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11229 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011230 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011231 }
11232 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011233 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011234 "%s check failed (%u, 0x%x, 0x%x)\n",
11235 __func__, i, e.index, e.reserved);
11236 goto fail;
11237 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011238 msr.index = e.index;
11239 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011240 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011241 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011242 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11243 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011244 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011245 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011246 }
11247 return 0;
11248fail:
11249 return i + 1;
11250}
11251
11252static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11253{
11254 u32 i;
11255 struct vmx_msr_entry e;
11256
11257 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011258 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011259 if (kvm_vcpu_read_guest(vcpu,
11260 gpa + i * sizeof(e),
11261 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011262 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011263 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11264 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011265 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011266 }
11267 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011268 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011269 "%s check failed (%u, 0x%x, 0x%x)\n",
11270 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011271 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011272 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011273 msr_info.host_initiated = false;
11274 msr_info.index = e.index;
11275 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011276 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011277 "%s cannot read MSR (%u, 0x%x)\n",
11278 __func__, i, e.index);
11279 return -EINVAL;
11280 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011281 if (kvm_vcpu_write_guest(vcpu,
11282 gpa + i * sizeof(e) +
11283 offsetof(struct vmx_msr_entry, value),
11284 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011285 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011286 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011287 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011288 return -EINVAL;
11289 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011290 }
11291 return 0;
11292}
11293
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011294static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11295{
11296 unsigned long invalid_mask;
11297
11298 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11299 return (val & invalid_mask) == 0;
11300}
11301
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011302/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011303 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11304 * emulating VM entry into a guest with EPT enabled.
11305 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11306 * is assigned to entry_failure_code on failure.
11307 */
11308static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011309 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011310{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011311 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011312 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011313 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11314 return 1;
11315 }
11316
11317 /*
11318 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11319 * must not be dereferenced.
11320 */
11321 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11322 !nested_ept) {
11323 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11324 *entry_failure_code = ENTRY_FAIL_PDPTE;
11325 return 1;
11326 }
11327 }
11328
11329 vcpu->arch.cr3 = cr3;
11330 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11331 }
11332
11333 kvm_mmu_reset_context(vcpu);
11334 return 0;
11335}
11336
Jim Mattson6514dc32018-04-26 16:09:12 -070011337static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011338{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011339 struct vcpu_vmx *vmx = to_vmx(vcpu);
11340
11341 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11342 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11343 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11344 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11345 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11346 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11347 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11348 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11349 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11350 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11351 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11352 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11353 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11354 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11355 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11356 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11357 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11358 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11359 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11360 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11361 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11362 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11363 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11364 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11365 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11366 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11367 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11368 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11369 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11370 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11371 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011372
11373 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11374 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11375 vmcs12->guest_pending_dbg_exceptions);
11376 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11377 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11378
11379 if (nested_cpu_has_xsaves(vmcs12))
11380 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11381 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11382
11383 if (cpu_has_vmx_posted_intr())
11384 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11385
11386 /*
11387 * Whether page-faults are trapped is determined by a combination of
11388 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11389 * If enable_ept, L0 doesn't care about page faults and we should
11390 * set all of these to L1's desires. However, if !enable_ept, L0 does
11391 * care about (at least some) page faults, and because it is not easy
11392 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11393 * to exit on each and every L2 page fault. This is done by setting
11394 * MASK=MATCH=0 and (see below) EB.PF=1.
11395 * Note that below we don't need special code to set EB.PF beyond the
11396 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11397 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11398 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11399 */
11400 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11401 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11402 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11403 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11404
11405 /* All VMFUNCs are currently emulated through L0 vmexits. */
11406 if (cpu_has_vmx_vmfunc())
11407 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11408
11409 if (cpu_has_vmx_apicv()) {
11410 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11411 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11412 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11413 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11414 }
11415
11416 /*
11417 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11418 * Some constant fields are set here by vmx_set_constant_host_state().
11419 * Other fields are different per CPU, and will be set later when
11420 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11421 */
11422 vmx_set_constant_host_state(vmx);
11423
11424 /*
11425 * Set the MSR load/store lists to match L0's settings.
11426 */
11427 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011428 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11429 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11430 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11431 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011432
11433 set_cr4_guest_host_mask(vmx);
11434
11435 if (vmx_mpx_supported())
11436 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11437
11438 if (enable_vpid) {
11439 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11440 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11441 else
11442 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11443 }
11444
11445 /*
11446 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11447 */
11448 if (enable_ept) {
11449 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11450 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11451 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11452 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11453 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011454
11455 if (cpu_has_vmx_msr_bitmap())
11456 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011457}
11458
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011459/*
11460 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11461 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011462 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011463 * guest in a way that will both be appropriate to L1's requests, and our
11464 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11465 * function also has additional necessary side-effects, like setting various
11466 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011467 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11468 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011469 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011470static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011471 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011472{
11473 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011474 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011475
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011476 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011477 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011478 vmx->nested.dirty_vmcs12 = false;
11479 }
11480
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011481 /*
11482 * First, the fields that are shadowed. This must be kept in sync
11483 * with vmx_shadow_fields.h.
11484 */
11485
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011486 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011487 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011488 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011489 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11490 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011491
11492 /*
11493 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11494 * HOST_FS_BASE, HOST_GS_BASE.
11495 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011496
Jim Mattson6514dc32018-04-26 16:09:12 -070011497 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011498 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011499 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11500 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11501 } else {
11502 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11503 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11504 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011505 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011506 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11507 vmcs12->vm_entry_intr_info_field);
11508 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11509 vmcs12->vm_entry_exception_error_code);
11510 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11511 vmcs12->vm_entry_instruction_len);
11512 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11513 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011514 vmx->loaded_vmcs->nmi_known_unmasked =
11515 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011516 } else {
11517 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11518 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011519 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011520
Jan Kiszkaf4124502014-03-07 20:03:13 +010011521 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011522
Paolo Bonzini93140062016-07-06 13:23:51 +020011523 /* Preemption timer setting is only taken from vmcs01. */
11524 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11525 exec_control |= vmcs_config.pin_based_exec_ctrl;
11526 if (vmx->hv_deadline_tsc == -1)
11527 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11528
11529 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011530 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011531 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11532 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011533 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011534 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011535 }
Wincy Van705699a2015-02-03 23:58:17 +080011536
Jan Kiszkaf4124502014-03-07 20:03:13 +010011537 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011538
Jan Kiszkaf4124502014-03-07 20:03:13 +010011539 vmx->nested.preemption_timer_expired = false;
11540 if (nested_cpu_has_preemption_timer(vmcs12))
11541 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011542
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011543 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011544 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011545
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011546 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011547 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011548 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011549 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011550 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011551 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011552 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11553 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011554 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011555 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11556 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11557 ~SECONDARY_EXEC_ENABLE_PML;
11558 exec_control |= vmcs12_exec_ctrl;
11559 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011560
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011561 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011562 vmcs_write16(GUEST_INTR_STATUS,
11563 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011564
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011565 /*
11566 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11567 * nested_get_vmcs12_pages will either fix it up or
11568 * remove the VM execution control.
11569 */
11570 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11571 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11572
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011573 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11574 }
11575
Jim Mattson83bafef2016-10-04 10:48:38 -070011576 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011577 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11578 * entry, but only if the current (host) sp changed from the value
11579 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11580 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11581 * here we just force the write to happen on entry.
11582 */
11583 vmx->host_rsp = 0;
11584
11585 exec_control = vmx_exec_control(vmx); /* L0's desires */
11586 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11587 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11588 exec_control &= ~CPU_BASED_TPR_SHADOW;
11589 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011590
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011591 /*
11592 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11593 * nested_get_vmcs12_pages can't fix it up, the illegal value
11594 * will result in a VM entry failure.
11595 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011596 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011597 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011598 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011599 } else {
11600#ifdef CONFIG_X86_64
11601 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11602 CPU_BASED_CR8_STORE_EXITING;
11603#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011604 }
11605
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011606 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080011607 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11608 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011609 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011610 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11611 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11612
11613 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11614
11615 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11616 * bitwise-or of what L1 wants to trap for L2, and what we want to
11617 * trap. Note that CR0.TS also needs updating - we do this later.
11618 */
11619 update_exception_bitmap(vcpu);
11620 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11621 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11622
Nadav Har'El8049d652013-08-05 11:07:06 +030011623 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11624 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11625 * bits are further modified by vmx_set_efer() below.
11626 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010011627 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011628
11629 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11630 * emulated by vmx_set_efer(), below.
11631 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011632 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011633 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11634 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011635 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11636
Jim Mattson6514dc32018-04-26 16:09:12 -070011637 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011638 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011639 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011640 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011641 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011642 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011643 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011644
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011645 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11646
Peter Feinerc95ba922016-08-17 09:36:47 -070011647 if (kvm_has_tsc_control)
11648 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011649
11650 if (enable_vpid) {
11651 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011652 * There is no direct mapping between vpid02 and vpid12, the
11653 * vpid02 is per-vCPU for L0 and reused while the value of
11654 * vpid12 is changed w/ one invvpid during nested vmentry.
11655 * The vpid12 is allocated by L1 for L2, so it will not
11656 * influence global bitmap(for vpid01 and vpid02 allocation)
11657 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011658 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011659 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011660 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11661 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011662 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011663 }
11664 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011665 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011666 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011667 }
11668
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011669 if (enable_pml) {
11670 /*
11671 * Conceptually we want to copy the PML address and index from
11672 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11673 * since we always flush the log on each vmexit, this happens
11674 * to be equivalent to simply resetting the fields in vmcs02.
11675 */
11676 ASSERT(vmx->pml_pg);
11677 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11678 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11679 }
11680
Nadav Har'El155a97a2013-08-05 11:07:16 +030011681 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011682 if (nested_ept_init_mmu_context(vcpu)) {
11683 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11684 return 1;
11685 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011686 } else if (nested_cpu_has2(vmcs12,
11687 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011688 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011689 }
11690
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011691 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011692 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11693 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011694 * The CR0_READ_SHADOW is what L2 should have expected to read given
11695 * the specifications by L1; It's not enough to take
11696 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11697 * have more bits than L1 expected.
11698 */
11699 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11700 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11701
11702 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11703 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11704
Jim Mattson6514dc32018-04-26 16:09:12 -070011705 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011706 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011707 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11708 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11709 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11710 else
11711 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11712 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11713 vmx_set_efer(vcpu, vcpu->arch.efer);
11714
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011715 /*
11716 * Guest state is invalid and unrestricted guest is disabled,
11717 * which means L1 attempted VMEntry to L2 with invalid state.
11718 * Fail the VMEntry.
11719 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011720 if (vmx->emulation_required) {
11721 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011722 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011723 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011724
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011725 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011726 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011727 entry_failure_code))
11728 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011729
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011730 if (!enable_ept)
11731 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11732
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011733 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11734 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011735 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011736}
11737
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011738static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11739{
11740 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11741 nested_cpu_has_virtual_nmis(vmcs12))
11742 return -EINVAL;
11743
11744 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11745 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11746 return -EINVAL;
11747
11748 return 0;
11749}
11750
Jim Mattsonca0bde22016-11-30 12:03:46 -080011751static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11752{
11753 struct vcpu_vmx *vmx = to_vmx(vcpu);
11754
11755 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11756 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11757 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11758
Jim Mattson56a20512017-07-06 16:33:06 -070011759 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11760 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11761
Jim Mattsonca0bde22016-11-30 12:03:46 -080011762 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11763 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11764
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011765 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11766 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11767
Jim Mattson712b12d2017-08-24 13:24:47 -070011768 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11769 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11770
Jim Mattsonca0bde22016-11-30 12:03:46 -080011771 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11772 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11773
11774 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11775 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11776
Bandan Dasc5f983f2017-05-05 15:25:14 -040011777 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11778 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11779
Jim Mattsonca0bde22016-11-30 12:03:46 -080011780 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011781 vmx->nested.msrs.procbased_ctls_low,
11782 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070011783 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
11784 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011785 vmx->nested.msrs.secondary_ctls_low,
11786 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011787 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011788 vmx->nested.msrs.pinbased_ctls_low,
11789 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011790 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011791 vmx->nested.msrs.exit_ctls_low,
11792 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080011793 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011794 vmx->nested.msrs.entry_ctls_low,
11795 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011796 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11797
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011798 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080011799 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11800
Bandan Das41ab9372017-08-03 15:54:43 -040011801 if (nested_cpu_has_vmfunc(vmcs12)) {
11802 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011803 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040011804 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11805
11806 if (nested_cpu_has_eptp_switching(vmcs12)) {
11807 if (!nested_cpu_has_ept(vmcs12) ||
11808 !page_address_valid(vcpu, vmcs12->eptp_list_address))
11809 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11810 }
11811 }
Bandan Das27c42a12017-08-03 15:54:42 -040011812
Jim Mattsonc7c2c702017-05-05 11:28:09 -070011813 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
11814 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11815
Jim Mattsonca0bde22016-11-30 12:03:46 -080011816 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
11817 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
11818 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
11819 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
11820
11821 return 0;
11822}
11823
11824static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
11825 u32 *exit_qual)
11826{
11827 bool ia32e;
11828
11829 *exit_qual = ENTRY_FAIL_DEFAULT;
11830
11831 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
11832 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
11833 return 1;
11834
11835 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
11836 vmcs12->vmcs_link_pointer != -1ull) {
11837 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
11838 return 1;
11839 }
11840
11841 /*
11842 * If the load IA32_EFER VM-entry control is 1, the following checks
11843 * are performed on the field for the IA32_EFER MSR:
11844 * - Bits reserved in the IA32_EFER MSR must be 0.
11845 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
11846 * the IA-32e mode guest VM-exit control. It must also be identical
11847 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
11848 * CR0.PG) is 1.
11849 */
11850 if (to_vmx(vcpu)->nested.nested_run_pending &&
11851 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
11852 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
11853 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
11854 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
11855 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
11856 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
11857 return 1;
11858 }
11859
11860 /*
11861 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
11862 * IA32_EFER MSR must be 0 in the field for that register. In addition,
11863 * the values of the LMA and LME bits in the field must each be that of
11864 * the host address-space size VM-exit control.
11865 */
11866 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
11867 ia32e = (vmcs12->vm_exit_controls &
11868 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
11869 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
11870 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
11871 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
11872 return 1;
11873 }
11874
Wanpeng Lif1b026a2017-11-05 16:54:48 -080011875 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
11876 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
11877 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
11878 return 1;
11879
Jim Mattsonca0bde22016-11-30 12:03:46 -080011880 return 0;
11881}
11882
Jim Mattson6514dc32018-04-26 16:09:12 -070011883static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu)
Jim Mattson858e25c2016-11-30 12:03:47 -080011884{
11885 struct vcpu_vmx *vmx = to_vmx(vcpu);
11886 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080011887 u32 msr_entry_idx;
11888 u32 exit_qual;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011889 int r;
Jim Mattson858e25c2016-11-30 12:03:47 -080011890
Jim Mattson858e25c2016-11-30 12:03:47 -080011891 enter_guest_mode(vcpu);
11892
11893 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
11894 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
11895
Jim Mattsonde3a0022017-11-27 17:22:25 -060011896 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080011897 vmx_segment_cache_clear(vmx);
11898
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011899 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11900 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
11901
11902 r = EXIT_REASON_INVALID_STATE;
Jim Mattson6514dc32018-04-26 16:09:12 -070011903 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011904 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011905
11906 nested_get_vmcs12_pages(vcpu, vmcs12);
11907
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011908 r = EXIT_REASON_MSR_LOAD_FAIL;
Jim Mattson858e25c2016-11-30 12:03:47 -080011909 msr_entry_idx = nested_vmx_load_msr(vcpu,
11910 vmcs12->vm_entry_msr_load_addr,
11911 vmcs12->vm_entry_msr_load_count);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011912 if (msr_entry_idx)
11913 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080011914
Jim Mattson858e25c2016-11-30 12:03:47 -080011915 /*
11916 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
11917 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
11918 * returned as far as L1 is concerned. It will only return (and set
11919 * the success flag) when L2 exits (see nested_vmx_vmexit()).
11920 */
11921 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011922
11923fail:
11924 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
11925 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
11926 leave_guest_mode(vcpu);
11927 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
11928 nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual);
11929 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080011930}
11931
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011932/*
11933 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
11934 * for running an L2 nested guest.
11935 */
11936static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
11937{
11938 struct vmcs12 *vmcs12;
11939 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011940 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080011941 u32 exit_qual;
11942 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011943
Kyle Hueyeb277562016-11-29 12:40:39 -080011944 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011945 return 1;
11946
Kyle Hueyeb277562016-11-29 12:40:39 -080011947 if (!nested_vmx_check_vmcs12(vcpu))
11948 goto out;
11949
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030011950 vmcs12 = get_vmcs12(vcpu);
11951
Abel Gordon012f83c2013-04-18 14:39:25 +030011952 if (enable_shadow_vmcs)
11953 copy_shadow_to_vmcs12(vmx);
11954
Nadav Har'El7c177932011-05-25 23:12:04 +030011955 /*
11956 * The nested entry process starts with enforcing various prerequisites
11957 * on vmcs12 as required by the Intel SDM, and act appropriately when
11958 * they fail: As the SDM explains, some conditions should cause the
11959 * instruction to fail, while others will cause the instruction to seem
11960 * to succeed, but return an EXIT_REASON_INVALID_STATE.
11961 * To speed up the normal (success) code path, we should avoid checking
11962 * for misconfigurations which will anyway be caught by the processor
11963 * when using the merged vmcs02.
11964 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070011965 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
11966 nested_vmx_failValid(vcpu,
11967 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
11968 goto out;
11969 }
11970
Nadav Har'El7c177932011-05-25 23:12:04 +030011971 if (vmcs12->launch_state == launch) {
11972 nested_vmx_failValid(vcpu,
11973 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
11974 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080011975 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030011976 }
11977
Jim Mattsonca0bde22016-11-30 12:03:46 -080011978 ret = check_vmentry_prereqs(vcpu, vmcs12);
11979 if (ret) {
11980 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080011981 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020011982 }
11983
Nadav Har'El7c177932011-05-25 23:12:04 +030011984 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080011985 * After this point, the trap flag no longer triggers a singlestep trap
11986 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
11987 * This is not 100% correct; for performance reasons, we delegate most
11988 * of the checks on host state to the processor. If those fail,
11989 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020011990 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080011991 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020011992
Jim Mattsonca0bde22016-11-30 12:03:46 -080011993 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
11994 if (ret) {
11995 nested_vmx_entry_failure(vcpu, vmcs12,
11996 EXIT_REASON_INVALID_STATE, exit_qual);
11997 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020011998 }
11999
12000 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012001 * We're finally done with prerequisite checking, and can start with
12002 * the nested entry.
12003 */
12004
Jim Mattson6514dc32018-04-26 16:09:12 -070012005 vmx->nested.nested_run_pending = 1;
12006 ret = enter_vmx_non_root_mode(vcpu);
12007 if (ret) {
12008 vmx->nested.nested_run_pending = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012009 return ret;
Jim Mattson6514dc32018-04-26 16:09:12 -070012010 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012011
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012012 /* Hide L1D cache contents from the nested guest. */
12013 vmx->vcpu.arch.l1tf_flush_l1d = true;
12014
Chao Gao135a06c2018-02-11 10:06:30 +080012015 /*
12016 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12017 * by event injection, halt vcpu.
12018 */
12019 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012020 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12021 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012022 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012023 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012024 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012025
12026out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012027 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012028}
12029
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012030/*
12031 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12032 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12033 * This function returns the new value we should put in vmcs12.guest_cr0.
12034 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12035 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12036 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12037 * didn't trap the bit, because if L1 did, so would L0).
12038 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12039 * been modified by L2, and L1 knows it. So just leave the old value of
12040 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12041 * isn't relevant, because if L0 traps this bit it can set it to anything.
12042 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12043 * changed these bits, and therefore they need to be updated, but L0
12044 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12045 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12046 */
12047static inline unsigned long
12048vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12049{
12050 return
12051 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12052 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12053 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12054 vcpu->arch.cr0_guest_owned_bits));
12055}
12056
12057static inline unsigned long
12058vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12059{
12060 return
12061 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12062 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12063 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12064 vcpu->arch.cr4_guest_owned_bits));
12065}
12066
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012067static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12068 struct vmcs12 *vmcs12)
12069{
12070 u32 idt_vectoring;
12071 unsigned int nr;
12072
Wanpeng Li664f8e22017-08-24 03:35:09 -070012073 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012074 nr = vcpu->arch.exception.nr;
12075 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12076
12077 if (kvm_exception_is_soft(nr)) {
12078 vmcs12->vm_exit_instruction_len =
12079 vcpu->arch.event_exit_inst_len;
12080 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12081 } else
12082 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12083
12084 if (vcpu->arch.exception.has_error_code) {
12085 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12086 vmcs12->idt_vectoring_error_code =
12087 vcpu->arch.exception.error_code;
12088 }
12089
12090 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012091 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012092 vmcs12->idt_vectoring_info_field =
12093 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012094 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012095 nr = vcpu->arch.interrupt.nr;
12096 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12097
12098 if (vcpu->arch.interrupt.soft) {
12099 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12100 vmcs12->vm_entry_instruction_len =
12101 vcpu->arch.event_exit_inst_len;
12102 } else
12103 idt_vectoring |= INTR_TYPE_EXT_INTR;
12104
12105 vmcs12->idt_vectoring_info_field = idt_vectoring;
12106 }
12107}
12108
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012109static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12110{
12111 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012112 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012113 bool block_nested_events =
12114 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012115
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012116 if (vcpu->arch.exception.pending &&
12117 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012118 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012119 return -EBUSY;
12120 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012121 return 0;
12122 }
12123
Jan Kiszkaf4124502014-03-07 20:03:13 +010012124 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12125 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012126 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012127 return -EBUSY;
12128 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12129 return 0;
12130 }
12131
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012132 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012133 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012134 return -EBUSY;
12135 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12136 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12137 INTR_INFO_VALID_MASK, 0);
12138 /*
12139 * The NMI-triggered VM exit counts as injection:
12140 * clear this one and block further NMIs.
12141 */
12142 vcpu->arch.nmi_pending = 0;
12143 vmx_set_nmi_mask(vcpu, true);
12144 return 0;
12145 }
12146
12147 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12148 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012149 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012150 return -EBUSY;
12151 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012152 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012153 }
12154
David Hildenbrand6342c502017-01-25 11:58:58 +010012155 vmx_complete_nested_posted_interrupt(vcpu);
12156 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012157}
12158
Jan Kiszkaf4124502014-03-07 20:03:13 +010012159static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12160{
12161 ktime_t remaining =
12162 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12163 u64 value;
12164
12165 if (ktime_to_ns(remaining) <= 0)
12166 return 0;
12167
12168 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12169 do_div(value, 1000000);
12170 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12171}
12172
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012173/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012174 * Update the guest state fields of vmcs12 to reflect changes that
12175 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12176 * VM-entry controls is also updated, since this is really a guest
12177 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012178 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012179static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012180{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012181 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12182 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12183
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012184 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12185 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12186 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12187
12188 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12189 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12190 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12191 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12192 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12193 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12194 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12195 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12196 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12197 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12198 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12199 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12200 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12201 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12202 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12203 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12204 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12205 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12206 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12207 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12208 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12209 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12210 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12211 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12212 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12213 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12214 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12215 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12216 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12217 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12218 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12219 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12220 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12221 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12222 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12223 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12224
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012225 vmcs12->guest_interruptibility_info =
12226 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12227 vmcs12->guest_pending_dbg_exceptions =
12228 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012229 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12230 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12231 else
12232 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012233
Jan Kiszkaf4124502014-03-07 20:03:13 +010012234 if (nested_cpu_has_preemption_timer(vmcs12)) {
12235 if (vmcs12->vm_exit_controls &
12236 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12237 vmcs12->vmx_preemption_timer_value =
12238 vmx_get_preemption_timer_value(vcpu);
12239 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12240 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012241
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012242 /*
12243 * In some cases (usually, nested EPT), L2 is allowed to change its
12244 * own CR3 without exiting. If it has changed it, we must keep it.
12245 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12246 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12247 *
12248 * Additionally, restore L2's PDPTR to vmcs12.
12249 */
12250 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012251 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012252 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12253 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12254 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12255 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12256 }
12257
Jim Mattsond281e132017-06-01 12:44:46 -070012258 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012259
Wincy Van608406e2015-02-03 23:57:51 +080012260 if (nested_cpu_has_vid(vmcs12))
12261 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12262
Jan Kiszkac18911a2013-03-13 16:06:41 +010012263 vmcs12->vm_entry_controls =
12264 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012265 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012266
Jan Kiszka2996fca2014-06-16 13:59:43 +020012267 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12268 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12269 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12270 }
12271
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012272 /* TODO: These cannot have changed unless we have MSR bitmaps and
12273 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012274 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012275 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012276 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12277 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012278 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12279 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12280 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012281 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012282 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012283}
12284
12285/*
12286 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12287 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12288 * and this function updates it to reflect the changes to the guest state while
12289 * L2 was running (and perhaps made some exits which were handled directly by L0
12290 * without going back to L1), and to reflect the exit reason.
12291 * Note that we do not have to copy here all VMCS fields, just those that
12292 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12293 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12294 * which already writes to vmcs12 directly.
12295 */
12296static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12297 u32 exit_reason, u32 exit_intr_info,
12298 unsigned long exit_qualification)
12299{
12300 /* update guest state fields: */
12301 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012302
12303 /* update exit information fields: */
12304
Jan Kiszka533558b2014-01-04 18:47:20 +010012305 vmcs12->vm_exit_reason = exit_reason;
12306 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012307 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012308
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012309 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012310 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12311 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12312
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012313 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012314 vmcs12->launch_state = 1;
12315
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012316 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12317 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012318 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012319
12320 /*
12321 * Transfer the event that L0 or L1 may wanted to inject into
12322 * L2 to IDT_VECTORING_INFO_FIELD.
12323 */
12324 vmcs12_save_pending_event(vcpu, vmcs12);
12325 }
12326
12327 /*
12328 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12329 * preserved above and would only end up incorrectly in L1.
12330 */
12331 vcpu->arch.nmi_injected = false;
12332 kvm_clear_exception_queue(vcpu);
12333 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012334}
12335
Wanpeng Li5af41572017-11-05 16:54:49 -080012336static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12337 struct vmcs12 *vmcs12)
12338{
12339 u32 entry_failure_code;
12340
12341 nested_ept_uninit_mmu_context(vcpu);
12342
12343 /*
12344 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12345 * couldn't have changed.
12346 */
12347 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12348 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12349
12350 if (!enable_ept)
12351 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12352}
12353
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012354/*
12355 * A part of what we need to when the nested L2 guest exits and we want to
12356 * run its L1 parent, is to reset L1's guest state to the host state specified
12357 * in vmcs12.
12358 * This function is to be called not only on normal nested exit, but also on
12359 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12360 * Failures During or After Loading Guest State").
12361 * This function should be called when the active VMCS is L1's (vmcs01).
12362 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012363static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12364 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012365{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012366 struct kvm_segment seg;
12367
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012368 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12369 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012370 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012371 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12372 else
12373 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12374 vmx_set_efer(vcpu, vcpu->arch.efer);
12375
12376 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12377 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012378 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012379 /*
12380 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012381 * actually changed, because vmx_set_cr0 refers to efer set above.
12382 *
12383 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12384 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012385 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012386 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020012387 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012388
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012389 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012390 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012391 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012392
Wanpeng Li5af41572017-11-05 16:54:49 -080012393 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012394
Liran Alon6f1e03b2018-05-22 17:16:14 +030012395 /*
12396 * If vmcs01 don't use VPID, CPU flushes TLB on every
12397 * VMEntry/VMExit. Thus, no need to flush TLB.
12398 *
12399 * If vmcs12 uses VPID, TLB entries populated by L2 are
12400 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12401 * with vmx->vpid. Thus, no need to flush TLB.
12402 *
12403 * Therefore, flush TLB only in case vmcs01 uses VPID and
12404 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12405 * are both tagged with vmx->vpid.
12406 */
12407 if (enable_vpid &&
12408 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012409 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012410 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012411
12412 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12413 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12414 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12415 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12416 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012417 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12418 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012419
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012420 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12421 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12422 vmcs_write64(GUEST_BNDCFGS, 0);
12423
Jan Kiszka44811c02013-08-04 17:17:27 +020012424 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012425 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012426 vcpu->arch.pat = vmcs12->host_ia32_pat;
12427 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012428 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12429 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12430 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012431
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012432 /* Set L1 segment info according to Intel SDM
12433 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12434 seg = (struct kvm_segment) {
12435 .base = 0,
12436 .limit = 0xFFFFFFFF,
12437 .selector = vmcs12->host_cs_selector,
12438 .type = 11,
12439 .present = 1,
12440 .s = 1,
12441 .g = 1
12442 };
12443 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12444 seg.l = 1;
12445 else
12446 seg.db = 1;
12447 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12448 seg = (struct kvm_segment) {
12449 .base = 0,
12450 .limit = 0xFFFFFFFF,
12451 .type = 3,
12452 .present = 1,
12453 .s = 1,
12454 .db = 1,
12455 .g = 1
12456 };
12457 seg.selector = vmcs12->host_ds_selector;
12458 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12459 seg.selector = vmcs12->host_es_selector;
12460 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12461 seg.selector = vmcs12->host_ss_selector;
12462 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12463 seg.selector = vmcs12->host_fs_selector;
12464 seg.base = vmcs12->host_fs_base;
12465 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12466 seg.selector = vmcs12->host_gs_selector;
12467 seg.base = vmcs12->host_gs_base;
12468 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12469 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012470 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012471 .limit = 0x67,
12472 .selector = vmcs12->host_tr_selector,
12473 .type = 11,
12474 .present = 1
12475 };
12476 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12477
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012478 kvm_set_dr(vcpu, 7, 0x400);
12479 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012480
Wincy Van3af18d92015-02-03 23:49:31 +080012481 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012482 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012483
Wincy Vanff651cb2014-12-11 08:52:58 +030012484 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12485 vmcs12->vm_exit_msr_load_count))
12486 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012487}
12488
12489/*
12490 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12491 * and modify vmcs12 to make it see what it would expect to see there if
12492 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12493 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012494static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12495 u32 exit_intr_info,
12496 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012497{
12498 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012499 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12500
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012501 /* trying to cancel vmlaunch/vmresume is a bug */
12502 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12503
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012504 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012505 * The only expected VM-instruction error is "VM entry with
12506 * invalid control field(s)." Anything else indicates a
12507 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012508 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012509 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12510 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12511
12512 leave_guest_mode(vcpu);
12513
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012514 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12515 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12516
Jim Mattson4f350c62017-09-14 16:31:44 -070012517 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012518 if (exit_reason == -1)
12519 sync_vmcs12(vcpu, vmcs12);
12520 else
12521 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12522 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012523
12524 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12525 vmcs12->vm_exit_msr_store_count))
12526 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012527 }
12528
Jim Mattson4f350c62017-09-14 16:31:44 -070012529 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012530 vm_entry_controls_reset_shadow(vmx);
12531 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012532 vmx_segment_cache_clear(vmx);
12533
Paolo Bonzini93140062016-07-06 13:23:51 +020012534 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012535 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12536 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012537 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020012538 if (vmx->hv_deadline_tsc == -1)
12539 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12540 PIN_BASED_VMX_PREEMPTION_TIMER);
12541 else
12542 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12543 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012544 if (kvm_has_tsc_control)
12545 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012546
Jim Mattson8d860bb2018-05-09 16:56:05 -040012547 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12548 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12549 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012550 } else if (!nested_cpu_has_ept(vmcs12) &&
12551 nested_cpu_has2(vmcs12,
12552 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012553 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012554 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012555
12556 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12557 vmx->host_rsp = 0;
12558
12559 /* Unpin physical memory we referred to in vmcs02 */
12560 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012561 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012562 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012563 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012564 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012565 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012566 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012567 }
Wincy Van705699a2015-02-03 23:58:17 +080012568 if (vmx->nested.pi_desc_page) {
12569 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012570 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012571 vmx->nested.pi_desc_page = NULL;
12572 vmx->nested.pi_desc = NULL;
12573 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012574
12575 /*
Tang Chen38b99172014-09-24 15:57:54 +080012576 * We are now running in L2, mmu_notifier will force to reload the
12577 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12578 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012579 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012580
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012581 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012582 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012583
12584 /* in case we halted in L2 */
12585 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012586
12587 if (likely(!vmx->fail)) {
12588 /*
12589 * TODO: SDM says that with acknowledge interrupt on
12590 * exit, bit 31 of the VM-exit interrupt information
12591 * (valid interrupt) is always set to 1 on
12592 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12593 * need kvm_cpu_has_interrupt(). See the commit
12594 * message for details.
12595 */
12596 if (nested_exit_intr_ack_set(vcpu) &&
12597 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12598 kvm_cpu_has_interrupt(vcpu)) {
12599 int irq = kvm_cpu_get_interrupt(vcpu);
12600 WARN_ON(irq < 0);
12601 vmcs12->vm_exit_intr_info = irq |
12602 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12603 }
12604
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012605 if (exit_reason != -1)
12606 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12607 vmcs12->exit_qualification,
12608 vmcs12->idt_vectoring_info_field,
12609 vmcs12->vm_exit_intr_info,
12610 vmcs12->vm_exit_intr_error_code,
12611 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012612
12613 load_vmcs12_host_state(vcpu, vmcs12);
12614
12615 return;
12616 }
12617
12618 /*
12619 * After an early L2 VM-entry failure, we're now back
12620 * in L1 which thinks it just finished a VMLAUNCH or
12621 * VMRESUME instruction, so we need to set the failure
12622 * flag and the VM-instruction error field of the VMCS
12623 * accordingly.
12624 */
12625 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012626
12627 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12628
Jim Mattson4f350c62017-09-14 16:31:44 -070012629 /*
12630 * The emulated instruction was already skipped in
12631 * nested_vmx_run, but the updated RIP was never
12632 * written back to the vmcs01.
12633 */
12634 skip_emulated_instruction(vcpu);
12635 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012636}
12637
Nadav Har'El7c177932011-05-25 23:12:04 +030012638/*
Jan Kiszka42124922014-01-04 18:47:19 +010012639 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12640 */
12641static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12642{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012643 if (is_guest_mode(vcpu)) {
12644 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012645 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012646 }
Jan Kiszka42124922014-01-04 18:47:19 +010012647 free_nested(to_vmx(vcpu));
12648}
12649
12650/*
Nadav Har'El7c177932011-05-25 23:12:04 +030012651 * L1's failure to enter L2 is a subset of a normal exit, as explained in
12652 * 23.7 "VM-entry failures during or after loading guest state" (this also
12653 * lists the acceptable exit-reason and exit-qualification parameters).
12654 * It should only be called before L2 actually succeeded to run, and when
12655 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
12656 */
12657static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
12658 struct vmcs12 *vmcs12,
12659 u32 reason, unsigned long qualification)
12660{
12661 load_vmcs12_host_state(vcpu, vmcs12);
12662 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12663 vmcs12->exit_qualification = qualification;
12664 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030012665 if (enable_shadow_vmcs)
12666 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030012667}
12668
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012669static int vmx_check_intercept(struct kvm_vcpu *vcpu,
12670 struct x86_instruction_info *info,
12671 enum x86_intercept_stage stage)
12672{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020012673 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12674 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
12675
12676 /*
12677 * RDPID causes #UD if disabled through secondary execution controls.
12678 * Because it is marked as EmulateOnUD, we need to intercept it here.
12679 */
12680 if (info->intercept == x86_intercept_rdtscp &&
12681 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
12682 ctxt->exception.vector = UD_VECTOR;
12683 ctxt->exception.error_code_valid = false;
12684 return X86EMUL_PROPAGATE_FAULT;
12685 }
12686
12687 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020012688 return X86EMUL_CONTINUE;
12689}
12690
Yunhong Jiang64672c92016-06-13 14:19:59 -070012691#ifdef CONFIG_X86_64
12692/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
12693static inline int u64_shl_div_u64(u64 a, unsigned int shift,
12694 u64 divisor, u64 *result)
12695{
12696 u64 low = a << shift, high = a >> (64 - shift);
12697
12698 /* To avoid the overflow on divq */
12699 if (high >= divisor)
12700 return 1;
12701
12702 /* Low hold the result, high hold rem which is discarded */
12703 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
12704 "rm" (divisor), "0" (low), "1" (high));
12705 *result = low;
12706
12707 return 0;
12708}
12709
12710static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
12711{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012712 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012713 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020012714
12715 if (kvm_mwait_in_guest(vcpu->kvm))
12716 return -EOPNOTSUPP;
12717
12718 vmx = to_vmx(vcpu);
12719 tscl = rdtsc();
12720 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
12721 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080012722 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
12723
12724 if (delta_tsc > lapic_timer_advance_cycles)
12725 delta_tsc -= lapic_timer_advance_cycles;
12726 else
12727 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012728
12729 /* Convert to host delta tsc if tsc scaling is enabled */
12730 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
12731 u64_shl_div_u64(delta_tsc,
12732 kvm_tsc_scaling_ratio_frac_bits,
12733 vcpu->arch.tsc_scaling_ratio,
12734 &delta_tsc))
12735 return -ERANGE;
12736
12737 /*
12738 * If the delta tsc can't fit in the 32 bit after the multi shift,
12739 * we can't use the preemption timer.
12740 * It's possible that it fits on later vmentries, but checking
12741 * on every vmentry is costly so we just use an hrtimer.
12742 */
12743 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
12744 return -ERANGE;
12745
12746 vmx->hv_deadline_tsc = tscl + delta_tsc;
12747 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12748 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070012749
12750 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070012751}
12752
12753static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
12754{
12755 struct vcpu_vmx *vmx = to_vmx(vcpu);
12756 vmx->hv_deadline_tsc = -1;
12757 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12758 PIN_BASED_VMX_PREEMPTION_TIMER);
12759}
12760#endif
12761
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012762static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012763{
Wanpeng Lib31c1142018-03-12 04:53:04 -070012764 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020012765 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020012766}
12767
Kai Huang843e4332015-01-28 10:54:28 +080012768static void vmx_slot_enable_log_dirty(struct kvm *kvm,
12769 struct kvm_memory_slot *slot)
12770{
12771 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
12772 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
12773}
12774
12775static void vmx_slot_disable_log_dirty(struct kvm *kvm,
12776 struct kvm_memory_slot *slot)
12777{
12778 kvm_mmu_slot_set_dirty(kvm, slot);
12779}
12780
12781static void vmx_flush_log_dirty(struct kvm *kvm)
12782{
12783 kvm_flush_pml_buffers(kvm);
12784}
12785
Bandan Dasc5f983f2017-05-05 15:25:14 -040012786static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
12787{
12788 struct vmcs12 *vmcs12;
12789 struct vcpu_vmx *vmx = to_vmx(vcpu);
12790 gpa_t gpa;
12791 struct page *page = NULL;
12792 u64 *pml_address;
12793
12794 if (is_guest_mode(vcpu)) {
12795 WARN_ON_ONCE(vmx->nested.pml_full);
12796
12797 /*
12798 * Check if PML is enabled for the nested guest.
12799 * Whether eptp bit 6 is set is already checked
12800 * as part of A/D emulation.
12801 */
12802 vmcs12 = get_vmcs12(vcpu);
12803 if (!nested_cpu_has_pml(vmcs12))
12804 return 0;
12805
Dan Carpenter47698862017-05-10 22:43:17 +030012806 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040012807 vmx->nested.pml_full = true;
12808 return 1;
12809 }
12810
12811 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
12812
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020012813 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
12814 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040012815 return 0;
12816
12817 pml_address = kmap(page);
12818 pml_address[vmcs12->guest_pml_index--] = gpa;
12819 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012820 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040012821 }
12822
12823 return 0;
12824}
12825
Kai Huang843e4332015-01-28 10:54:28 +080012826static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
12827 struct kvm_memory_slot *memslot,
12828 gfn_t offset, unsigned long mask)
12829{
12830 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
12831}
12832
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012833static void __pi_post_block(struct kvm_vcpu *vcpu)
12834{
12835 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12836 struct pi_desc old, new;
12837 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012838
12839 do {
12840 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012841 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
12842 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012843
12844 dest = cpu_physical_id(vcpu->cpu);
12845
12846 if (x2apic_enabled())
12847 new.ndst = dest;
12848 else
12849 new.ndst = (dest << 8) & 0xFF00;
12850
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012851 /* set 'NV' to 'notification vector' */
12852 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012853 } while (cmpxchg64(&pi_desc->control, old.control,
12854 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012855
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012856 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
12857 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012858 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012859 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012860 vcpu->pre_pcpu = -1;
12861 }
12862}
12863
Feng Wuefc64402015-09-18 22:29:51 +080012864/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080012865 * This routine does the following things for vCPU which is going
12866 * to be blocked if VT-d PI is enabled.
12867 * - Store the vCPU to the wakeup list, so when interrupts happen
12868 * we can find the right vCPU to wake up.
12869 * - Change the Posted-interrupt descriptor as below:
12870 * 'NDST' <-- vcpu->pre_pcpu
12871 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
12872 * - If 'ON' is set during this process, which means at least one
12873 * interrupt is posted for this vCPU, we cannot block it, in
12874 * this case, return 1, otherwise, return 0.
12875 *
12876 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070012877static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012878{
Feng Wubf9f6ac2015-09-18 22:29:55 +080012879 unsigned int dest;
12880 struct pi_desc old, new;
12881 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
12882
12883 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012884 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12885 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080012886 return 0;
12887
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012888 WARN_ON(irqs_disabled());
12889 local_irq_disable();
12890 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
12891 vcpu->pre_pcpu = vcpu->cpu;
12892 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12893 list_add_tail(&vcpu->blocked_vcpu_list,
12894 &per_cpu(blocked_vcpu_on_cpu,
12895 vcpu->pre_pcpu));
12896 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
12897 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080012898
12899 do {
12900 old.control = new.control = pi_desc->control;
12901
Feng Wubf9f6ac2015-09-18 22:29:55 +080012902 WARN((pi_desc->sn == 1),
12903 "Warning: SN field of posted-interrupts "
12904 "is set before blocking\n");
12905
12906 /*
12907 * Since vCPU can be preempted during this process,
12908 * vcpu->cpu could be different with pre_pcpu, we
12909 * need to set pre_pcpu as the destination of wakeup
12910 * notification event, then we can find the right vCPU
12911 * to wakeup in wakeup handler if interrupts happen
12912 * when the vCPU is in blocked state.
12913 */
12914 dest = cpu_physical_id(vcpu->pre_pcpu);
12915
12916 if (x2apic_enabled())
12917 new.ndst = dest;
12918 else
12919 new.ndst = (dest << 8) & 0xFF00;
12920
12921 /* set 'NV' to 'wakeup vector' */
12922 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020012923 } while (cmpxchg64(&pi_desc->control, old.control,
12924 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012925
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012926 /* We should not block the vCPU if an interrupt is posted for it. */
12927 if (pi_test_on(pi_desc) == 1)
12928 __pi_post_block(vcpu);
12929
12930 local_irq_enable();
12931 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080012932}
12933
Yunhong Jiangbc225122016-06-13 14:19:58 -070012934static int vmx_pre_block(struct kvm_vcpu *vcpu)
12935{
12936 if (pi_pre_block(vcpu))
12937 return 1;
12938
Yunhong Jiang64672c92016-06-13 14:19:59 -070012939 if (kvm_lapic_hv_timer_in_use(vcpu))
12940 kvm_lapic_switch_to_sw_timer(vcpu);
12941
Yunhong Jiangbc225122016-06-13 14:19:58 -070012942 return 0;
12943}
12944
12945static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012946{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012947 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080012948 return;
12949
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012950 WARN_ON(irqs_disabled());
12951 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020012952 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020012953 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080012954}
12955
Yunhong Jiangbc225122016-06-13 14:19:58 -070012956static void vmx_post_block(struct kvm_vcpu *vcpu)
12957{
Yunhong Jiang64672c92016-06-13 14:19:59 -070012958 if (kvm_x86_ops->set_hv_timer)
12959 kvm_lapic_switch_to_hv_timer(vcpu);
12960
Yunhong Jiangbc225122016-06-13 14:19:58 -070012961 pi_post_block(vcpu);
12962}
12963
Feng Wubf9f6ac2015-09-18 22:29:55 +080012964/*
Feng Wuefc64402015-09-18 22:29:51 +080012965 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
12966 *
12967 * @kvm: kvm
12968 * @host_irq: host irq of the interrupt
12969 * @guest_irq: gsi of the interrupt
12970 * @set: set or unset PI
12971 * returns 0 on success, < 0 on failure
12972 */
12973static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
12974 uint32_t guest_irq, bool set)
12975{
12976 struct kvm_kernel_irq_routing_entry *e;
12977 struct kvm_irq_routing_table *irq_rt;
12978 struct kvm_lapic_irq irq;
12979 struct kvm_vcpu *vcpu;
12980 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012981 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080012982
12983 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080012984 !irq_remapping_cap(IRQ_POSTING_CAP) ||
12985 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080012986 return 0;
12987
12988 idx = srcu_read_lock(&kvm->irq_srcu);
12989 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010012990 if (guest_irq >= irq_rt->nr_rt_entries ||
12991 hlist_empty(&irq_rt->map[guest_irq])) {
12992 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
12993 guest_irq, irq_rt->nr_rt_entries);
12994 goto out;
12995 }
Feng Wuefc64402015-09-18 22:29:51 +080012996
12997 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
12998 if (e->type != KVM_IRQ_ROUTING_MSI)
12999 continue;
13000 /*
13001 * VT-d PI cannot support posting multicast/broadcast
13002 * interrupts to a vCPU, we still use interrupt remapping
13003 * for these kind of interrupts.
13004 *
13005 * For lowest-priority interrupts, we only support
13006 * those with single CPU as the destination, e.g. user
13007 * configures the interrupts via /proc/irq or uses
13008 * irqbalance to make the interrupts single-CPU.
13009 *
13010 * We will support full lowest-priority interrupt later.
13011 */
13012
Radim Krčmář371313132016-07-12 22:09:27 +020013013 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013014 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13015 /*
13016 * Make sure the IRTE is in remapped mode if
13017 * we don't handle it in posted mode.
13018 */
13019 ret = irq_set_vcpu_affinity(host_irq, NULL);
13020 if (ret < 0) {
13021 printk(KERN_INFO
13022 "failed to back to remapped mode, irq: %u\n",
13023 host_irq);
13024 goto out;
13025 }
13026
Feng Wuefc64402015-09-18 22:29:51 +080013027 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013028 }
Feng Wuefc64402015-09-18 22:29:51 +080013029
13030 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13031 vcpu_info.vector = irq.vector;
13032
hu huajun2698d822018-04-11 15:16:40 +080013033 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013034 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13035
13036 if (set)
13037 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013038 else
Feng Wuefc64402015-09-18 22:29:51 +080013039 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013040
13041 if (ret < 0) {
13042 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13043 __func__);
13044 goto out;
13045 }
13046 }
13047
13048 ret = 0;
13049out:
13050 srcu_read_unlock(&kvm->irq_srcu, idx);
13051 return ret;
13052}
13053
Ashok Rajc45dcc72016-06-22 14:59:56 +080013054static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13055{
13056 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13057 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13058 FEATURE_CONTROL_LMCE;
13059 else
13060 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13061 ~FEATURE_CONTROL_LMCE;
13062}
13063
Ladi Prosek72d7b372017-10-11 16:54:41 +020013064static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13065{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013066 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13067 if (to_vmx(vcpu)->nested.nested_run_pending)
13068 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013069 return 1;
13070}
13071
Ladi Prosek0234bf82017-10-11 16:54:40 +020013072static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13073{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013074 struct vcpu_vmx *vmx = to_vmx(vcpu);
13075
13076 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13077 if (vmx->nested.smm.guest_mode)
13078 nested_vmx_vmexit(vcpu, -1, 0, 0);
13079
13080 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13081 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013082 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013083 return 0;
13084}
13085
13086static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13087{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013088 struct vcpu_vmx *vmx = to_vmx(vcpu);
13089 int ret;
13090
13091 if (vmx->nested.smm.vmxon) {
13092 vmx->nested.vmxon = true;
13093 vmx->nested.smm.vmxon = false;
13094 }
13095
13096 if (vmx->nested.smm.guest_mode) {
13097 vcpu->arch.hflags &= ~HF_SMM_MASK;
Jim Mattson6514dc32018-04-26 16:09:12 -070013098 ret = enter_vmx_non_root_mode(vcpu);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013099 vcpu->arch.hflags |= HF_SMM_MASK;
13100 if (ret)
13101 return ret;
13102
13103 vmx->nested.smm.guest_mode = false;
13104 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013105 return 0;
13106}
13107
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013108static int enable_smi_window(struct kvm_vcpu *vcpu)
13109{
13110 return 0;
13111}
13112
Kees Cook404f6aa2016-08-08 16:29:06 -070013113static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013114 .cpu_has_kvm_support = cpu_has_kvm_support,
13115 .disabled_by_bios = vmx_disabled_by_bios,
13116 .hardware_setup = hardware_setup,
13117 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013118 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013119 .hardware_enable = hardware_enable,
13120 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013121 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013122 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013123
Wanpeng Lib31c1142018-03-12 04:53:04 -070013124 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013125 .vm_alloc = vmx_vm_alloc,
13126 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013127
Avi Kivity6aa8b732006-12-10 02:21:36 -080013128 .vcpu_create = vmx_create_vcpu,
13129 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013130 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013131
Avi Kivity04d2cc72007-09-10 18:10:54 +030013132 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013133 .vcpu_load = vmx_vcpu_load,
13134 .vcpu_put = vmx_vcpu_put,
13135
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013136 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013137 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013138 .get_msr = vmx_get_msr,
13139 .set_msr = vmx_set_msr,
13140 .get_segment_base = vmx_get_segment_base,
13141 .get_segment = vmx_get_segment,
13142 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013143 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013144 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013145 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013146 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013147 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013148 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013149 .set_cr3 = vmx_set_cr3,
13150 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013151 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013152 .get_idt = vmx_get_idt,
13153 .set_idt = vmx_set_idt,
13154 .get_gdt = vmx_get_gdt,
13155 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013156 .get_dr6 = vmx_get_dr6,
13157 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013158 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013159 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013160 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013161 .get_rflags = vmx_get_rflags,
13162 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013163
Avi Kivity6aa8b732006-12-10 02:21:36 -080013164 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013165
Avi Kivity6aa8b732006-12-10 02:21:36 -080013166 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013167 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013168 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013169 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13170 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013171 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013172 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013173 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013174 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013175 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013176 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013177 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013178 .get_nmi_mask = vmx_get_nmi_mask,
13179 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013180 .enable_nmi_window = enable_nmi_window,
13181 .enable_irq_window = enable_irq_window,
13182 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013183 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013184 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013185 .get_enable_apicv = vmx_get_enable_apicv,
13186 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013187 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013188 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013189 .hwapic_irr_update = vmx_hwapic_irr_update,
13190 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013191 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13192 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013193
Izik Eiduscbc94022007-10-25 00:29:55 +020013194 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013195 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013196 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013197 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013198
Avi Kivity586f9602010-11-18 13:09:54 +020013199 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013200
Sheng Yang17cc3932010-01-05 19:02:27 +080013201 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013202
13203 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013204
13205 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013206 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013207
13208 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013209
13210 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013211
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013212 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013213 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013214
13215 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013216
13217 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013218 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013219 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013220 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013221 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013222
13223 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013224
13225 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013226
13227 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13228 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13229 .flush_log_dirty = vmx_flush_log_dirty,
13230 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013231 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013232
Feng Wubf9f6ac2015-09-18 22:29:55 +080013233 .pre_block = vmx_pre_block,
13234 .post_block = vmx_post_block,
13235
Wei Huang25462f72015-06-19 15:45:05 +020013236 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013237
13238 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013239
13240#ifdef CONFIG_X86_64
13241 .set_hv_timer = vmx_set_hv_timer,
13242 .cancel_hv_timer = vmx_cancel_hv_timer,
13243#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013244
13245 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013246
Ladi Prosek72d7b372017-10-11 16:54:41 +020013247 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013248 .pre_enter_smm = vmx_pre_enter_smm,
13249 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013250 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013251};
13252
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013253static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013254{
13255 if (vmx_l1d_flush_pages) {
13256 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
13257 vmx_l1d_flush_pages = NULL;
13258 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020013259 /* Restore state so sysfs ignores VMX */
13260 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020013261}
13262
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013263static void vmx_exit(void)
13264{
13265#ifdef CONFIG_KEXEC_CORE
13266 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
13267 synchronize_rcu();
13268#endif
13269
13270 kvm_exit();
13271
13272#if IS_ENABLED(CONFIG_HYPERV)
13273 if (static_branch_unlikely(&enable_evmcs)) {
13274 int cpu;
13275 struct hv_vp_assist_page *vp_ap;
13276 /*
13277 * Reset everything to support using non-enlightened VMCS
13278 * access later (e.g. when we reload the module with
13279 * enlightened_vmcs=0)
13280 */
13281 for_each_online_cpu(cpu) {
13282 vp_ap = hv_get_vp_assist_page(cpu);
13283
13284 if (!vp_ap)
13285 continue;
13286
13287 vp_ap->current_nested_vmcs = 0;
13288 vp_ap->enlighten_vmentry = 0;
13289 }
13290
13291 static_branch_disable(&enable_evmcs);
13292 }
13293#endif
13294 vmx_cleanup_l1d_flush();
13295}
13296module_exit(vmx_exit);
13297
Avi Kivity6aa8b732006-12-10 02:21:36 -080013298static int __init vmx_init(void)
13299{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013300 int r;
13301
13302#if IS_ENABLED(CONFIG_HYPERV)
13303 /*
13304 * Enlightened VMCS usage should be recommended and the host needs
13305 * to support eVMCS v1 or above. We can also disable eVMCS support
13306 * with module parameter.
13307 */
13308 if (enlightened_vmcs &&
13309 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13310 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13311 KVM_EVMCS_VERSION) {
13312 int cpu;
13313
13314 /* Check that we have assist pages on all online CPUs */
13315 for_each_online_cpu(cpu) {
13316 if (!hv_get_vp_assist_page(cpu)) {
13317 enlightened_vmcs = false;
13318 break;
13319 }
13320 }
13321
13322 if (enlightened_vmcs) {
13323 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13324 static_branch_enable(&enable_evmcs);
13325 }
13326 } else {
13327 enlightened_vmcs = false;
13328 }
13329#endif
13330
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013331 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
13332 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013333 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013334 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013335
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013336 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013337 * Must be called after kvm_init() so enable_ept is properly set
13338 * up. Hand the parameter mitigation value in which was stored in
13339 * the pre module init parser. If no parameter was given, it will
13340 * contain 'auto' which will be turned into the default 'cond'
13341 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013342 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020013343 if (boot_cpu_has(X86_BUG_L1TF)) {
13344 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
13345 if (r) {
13346 vmx_exit();
13347 return r;
13348 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020013349 }
13350
Dave Young2965faa2015-09-09 15:38:55 -070013351#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013352 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13353 crash_vmclear_local_loaded_vmcss);
13354#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013355 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013356
He, Qingfdef3ad2007-04-30 09:45:24 +030013357 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013358}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020013359module_init(vmx_init);