blob: 6a95e6b1a43d7544e6a8f8ed24c041152bfdd927 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Feng Wu28b835d2015-09-18 22:29:54 +080041#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080043#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020044#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020045#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080046#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020047#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020048#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010049#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080050#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010051#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080052#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070053#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010055#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080056
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020058#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010059#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030060
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040062#define __ex_clear(x, reg) \
63 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030064
Avi Kivity6aa8b732006-12-10 02:21:36 -080065MODULE_AUTHOR("Qumranet");
66MODULE_LICENSE("GPL");
67
Josh Triplette9bda3b2012-03-20 23:33:51 -070068static const struct x86_cpu_id vmx_cpu_id[] = {
69 X86_FEATURE_MATCH(X86_FEATURE_VMX),
70 {}
71};
72MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
73
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080076
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010077static bool __read_mostly enable_vnmi = 1;
78module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
79
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020081module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020084module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070087module_param_named(unrestricted_guest,
88 enable_unrestricted_guest, bool, S_IRUGO);
89
Xudong Hao83c3a332012-05-28 19:33:35 +080090static bool __read_mostly enable_ept_ad_bits = 1;
91module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
92
Avi Kivitya27685c2012-06-12 20:30:18 +030093static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020094module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030095
Rusty Russell476bc002012-01-13 09:32:18 +103096static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030097module_param(fasteoi, bool, S_IRUGO);
98
Yang Zhang5a717852013-04-11 19:25:16 +080099static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800100module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800101
Abel Gordonabc4fc52013-04-18 14:35:25 +0300102static bool __read_mostly enable_shadow_vmcs = 1;
103module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300104/*
105 * If nested=1, nested virtualization is supported, i.e., guests may use
106 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
107 * use VMX instructions.
108 */
Rusty Russell476bc002012-01-13 09:32:18 +1030109static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300110module_param(nested, bool, S_IRUGO);
111
Wanpeng Li20300092014-12-02 19:14:59 +0800112static u64 __read_mostly host_xss;
113
Kai Huang843e4332015-01-28 10:54:28 +0800114static bool __read_mostly enable_pml = 1;
115module_param_named(pml, enable_pml, bool, S_IRUGO);
116
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100117#define MSR_TYPE_R 1
118#define MSR_TYPE_W 2
119#define MSR_TYPE_RW 3
120
121#define MSR_BITMAP_MODE_X2APIC 1
122#define MSR_BITMAP_MODE_X2APIC_APICV 2
123#define MSR_BITMAP_MODE_LM 4
124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf41245002014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Tianyu Lan877ad952018-07-19 08:40:23 +0000191enum ept_pointers_status {
192 EPT_POINTERS_CHECK = 0,
193 EPT_POINTERS_MATCH = 1,
194 EPT_POINTERS_MISMATCH = 2
195};
196
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700197struct kvm_vmx {
198 struct kvm kvm;
199
200 unsigned int tss_addr;
201 bool ept_identity_pagetable_done;
202 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000203
204 enum ept_pointers_status ept_pointers_match;
205 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700206};
207
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200208#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300209
Liran Alon392b2f22018-06-23 02:35:01 +0300210struct vmcs_hdr {
211 u32 revision_id:31;
212 u32 shadow_vmcs:1;
213};
214
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400215struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300216 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400217 u32 abort;
218 char data[0];
219};
220
Nadav Har'Eld462b812011-05-24 15:26:10 +0300221/*
222 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
223 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
224 * loaded on this CPU (so we can clear them if the CPU goes down).
225 */
226struct loaded_vmcs {
227 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700228 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300229 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200230 bool launched;
231 bool nmi_known_unmasked;
Ladi Prosek44889942017-09-22 07:53:15 +0200232 unsigned long vmcs_host_cr3; /* May not match real cr3 */
233 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100234 /* Support for vnmi-less CPUs */
235 int soft_vnmi_blocked;
236 ktime_t entry_time;
237 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100238 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300239 struct list_head loaded_vmcss_on_cpu_link;
240};
241
Avi Kivity26bb0982009-09-07 11:14:12 +0300242struct shared_msr_entry {
243 unsigned index;
244 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200245 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300246};
247
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300248/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300249 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
250 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
251 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
252 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
253 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
254 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600255 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300256 * underlying hardware which will be used to run L2.
257 * This structure is packed to ensure that its layout is identical across
258 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700259 *
260 * IMPORTANT: Changing the layout of existing fields in this structure
261 * will break save/restore compatibility with older kvm releases. When
262 * adding new fields, either use space in the reserved padding* arrays
263 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300264 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300265typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300266struct __packed vmcs12 {
267 /* According to the Intel spec, a VMCS region must start with the
268 * following two fields. Then follow implementation-specific data.
269 */
Liran Alon392b2f22018-06-23 02:35:01 +0300270 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300271 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300272
Nadav Har'El27d6c862011-05-25 23:06:59 +0300273 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
274 u32 padding[7]; /* room for future expansion */
275
Nadav Har'El22bd0352011-05-25 23:05:57 +0300276 u64 io_bitmap_a;
277 u64 io_bitmap_b;
278 u64 msr_bitmap;
279 u64 vm_exit_msr_store_addr;
280 u64 vm_exit_msr_load_addr;
281 u64 vm_entry_msr_load_addr;
282 u64 tsc_offset;
283 u64 virtual_apic_page_addr;
284 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800285 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300286 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800287 u64 eoi_exit_bitmap0;
288 u64 eoi_exit_bitmap1;
289 u64 eoi_exit_bitmap2;
290 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800291 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300292 u64 guest_physical_address;
293 u64 vmcs_link_pointer;
294 u64 guest_ia32_debugctl;
295 u64 guest_ia32_pat;
296 u64 guest_ia32_efer;
297 u64 guest_ia32_perf_global_ctrl;
298 u64 guest_pdptr0;
299 u64 guest_pdptr1;
300 u64 guest_pdptr2;
301 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100302 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300303 u64 host_ia32_pat;
304 u64 host_ia32_efer;
305 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700306 u64 vmread_bitmap;
307 u64 vmwrite_bitmap;
308 u64 vm_function_control;
309 u64 eptp_list_address;
310 u64 pml_address;
311 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300312 /*
313 * To allow migration of L1 (complete with its L2 guests) between
314 * machines of different natural widths (32 or 64 bit), we cannot have
315 * unsigned long fields with no explict size. We use u64 (aliased
316 * natural_width) instead. Luckily, x86 is little-endian.
317 */
318 natural_width cr0_guest_host_mask;
319 natural_width cr4_guest_host_mask;
320 natural_width cr0_read_shadow;
321 natural_width cr4_read_shadow;
322 natural_width cr3_target_value0;
323 natural_width cr3_target_value1;
324 natural_width cr3_target_value2;
325 natural_width cr3_target_value3;
326 natural_width exit_qualification;
327 natural_width guest_linear_address;
328 natural_width guest_cr0;
329 natural_width guest_cr3;
330 natural_width guest_cr4;
331 natural_width guest_es_base;
332 natural_width guest_cs_base;
333 natural_width guest_ss_base;
334 natural_width guest_ds_base;
335 natural_width guest_fs_base;
336 natural_width guest_gs_base;
337 natural_width guest_ldtr_base;
338 natural_width guest_tr_base;
339 natural_width guest_gdtr_base;
340 natural_width guest_idtr_base;
341 natural_width guest_dr7;
342 natural_width guest_rsp;
343 natural_width guest_rip;
344 natural_width guest_rflags;
345 natural_width guest_pending_dbg_exceptions;
346 natural_width guest_sysenter_esp;
347 natural_width guest_sysenter_eip;
348 natural_width host_cr0;
349 natural_width host_cr3;
350 natural_width host_cr4;
351 natural_width host_fs_base;
352 natural_width host_gs_base;
353 natural_width host_tr_base;
354 natural_width host_gdtr_base;
355 natural_width host_idtr_base;
356 natural_width host_ia32_sysenter_esp;
357 natural_width host_ia32_sysenter_eip;
358 natural_width host_rsp;
359 natural_width host_rip;
360 natural_width paddingl[8]; /* room for future expansion */
361 u32 pin_based_vm_exec_control;
362 u32 cpu_based_vm_exec_control;
363 u32 exception_bitmap;
364 u32 page_fault_error_code_mask;
365 u32 page_fault_error_code_match;
366 u32 cr3_target_count;
367 u32 vm_exit_controls;
368 u32 vm_exit_msr_store_count;
369 u32 vm_exit_msr_load_count;
370 u32 vm_entry_controls;
371 u32 vm_entry_msr_load_count;
372 u32 vm_entry_intr_info_field;
373 u32 vm_entry_exception_error_code;
374 u32 vm_entry_instruction_len;
375 u32 tpr_threshold;
376 u32 secondary_vm_exec_control;
377 u32 vm_instruction_error;
378 u32 vm_exit_reason;
379 u32 vm_exit_intr_info;
380 u32 vm_exit_intr_error_code;
381 u32 idt_vectoring_info_field;
382 u32 idt_vectoring_error_code;
383 u32 vm_exit_instruction_len;
384 u32 vmx_instruction_info;
385 u32 guest_es_limit;
386 u32 guest_cs_limit;
387 u32 guest_ss_limit;
388 u32 guest_ds_limit;
389 u32 guest_fs_limit;
390 u32 guest_gs_limit;
391 u32 guest_ldtr_limit;
392 u32 guest_tr_limit;
393 u32 guest_gdtr_limit;
394 u32 guest_idtr_limit;
395 u32 guest_es_ar_bytes;
396 u32 guest_cs_ar_bytes;
397 u32 guest_ss_ar_bytes;
398 u32 guest_ds_ar_bytes;
399 u32 guest_fs_ar_bytes;
400 u32 guest_gs_ar_bytes;
401 u32 guest_ldtr_ar_bytes;
402 u32 guest_tr_ar_bytes;
403 u32 guest_interruptibility_info;
404 u32 guest_activity_state;
405 u32 guest_sysenter_cs;
406 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100407 u32 vmx_preemption_timer_value;
408 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300409 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800410 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300411 u16 guest_es_selector;
412 u16 guest_cs_selector;
413 u16 guest_ss_selector;
414 u16 guest_ds_selector;
415 u16 guest_fs_selector;
416 u16 guest_gs_selector;
417 u16 guest_ldtr_selector;
418 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800419 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300420 u16 host_es_selector;
421 u16 host_cs_selector;
422 u16 host_ss_selector;
423 u16 host_ds_selector;
424 u16 host_fs_selector;
425 u16 host_gs_selector;
426 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700427 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300428};
429
430/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700431 * For save/restore compatibility, the vmcs12 field offsets must not change.
432 */
433#define CHECK_OFFSET(field, loc) \
434 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
435 "Offset of " #field " in struct vmcs12 has changed.")
436
437static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300438 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700439 CHECK_OFFSET(abort, 4);
440 CHECK_OFFSET(launch_state, 8);
441 CHECK_OFFSET(io_bitmap_a, 40);
442 CHECK_OFFSET(io_bitmap_b, 48);
443 CHECK_OFFSET(msr_bitmap, 56);
444 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
445 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
446 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
447 CHECK_OFFSET(tsc_offset, 88);
448 CHECK_OFFSET(virtual_apic_page_addr, 96);
449 CHECK_OFFSET(apic_access_addr, 104);
450 CHECK_OFFSET(posted_intr_desc_addr, 112);
451 CHECK_OFFSET(ept_pointer, 120);
452 CHECK_OFFSET(eoi_exit_bitmap0, 128);
453 CHECK_OFFSET(eoi_exit_bitmap1, 136);
454 CHECK_OFFSET(eoi_exit_bitmap2, 144);
455 CHECK_OFFSET(eoi_exit_bitmap3, 152);
456 CHECK_OFFSET(xss_exit_bitmap, 160);
457 CHECK_OFFSET(guest_physical_address, 168);
458 CHECK_OFFSET(vmcs_link_pointer, 176);
459 CHECK_OFFSET(guest_ia32_debugctl, 184);
460 CHECK_OFFSET(guest_ia32_pat, 192);
461 CHECK_OFFSET(guest_ia32_efer, 200);
462 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
463 CHECK_OFFSET(guest_pdptr0, 216);
464 CHECK_OFFSET(guest_pdptr1, 224);
465 CHECK_OFFSET(guest_pdptr2, 232);
466 CHECK_OFFSET(guest_pdptr3, 240);
467 CHECK_OFFSET(guest_bndcfgs, 248);
468 CHECK_OFFSET(host_ia32_pat, 256);
469 CHECK_OFFSET(host_ia32_efer, 264);
470 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
471 CHECK_OFFSET(vmread_bitmap, 280);
472 CHECK_OFFSET(vmwrite_bitmap, 288);
473 CHECK_OFFSET(vm_function_control, 296);
474 CHECK_OFFSET(eptp_list_address, 304);
475 CHECK_OFFSET(pml_address, 312);
476 CHECK_OFFSET(cr0_guest_host_mask, 344);
477 CHECK_OFFSET(cr4_guest_host_mask, 352);
478 CHECK_OFFSET(cr0_read_shadow, 360);
479 CHECK_OFFSET(cr4_read_shadow, 368);
480 CHECK_OFFSET(cr3_target_value0, 376);
481 CHECK_OFFSET(cr3_target_value1, 384);
482 CHECK_OFFSET(cr3_target_value2, 392);
483 CHECK_OFFSET(cr3_target_value3, 400);
484 CHECK_OFFSET(exit_qualification, 408);
485 CHECK_OFFSET(guest_linear_address, 416);
486 CHECK_OFFSET(guest_cr0, 424);
487 CHECK_OFFSET(guest_cr3, 432);
488 CHECK_OFFSET(guest_cr4, 440);
489 CHECK_OFFSET(guest_es_base, 448);
490 CHECK_OFFSET(guest_cs_base, 456);
491 CHECK_OFFSET(guest_ss_base, 464);
492 CHECK_OFFSET(guest_ds_base, 472);
493 CHECK_OFFSET(guest_fs_base, 480);
494 CHECK_OFFSET(guest_gs_base, 488);
495 CHECK_OFFSET(guest_ldtr_base, 496);
496 CHECK_OFFSET(guest_tr_base, 504);
497 CHECK_OFFSET(guest_gdtr_base, 512);
498 CHECK_OFFSET(guest_idtr_base, 520);
499 CHECK_OFFSET(guest_dr7, 528);
500 CHECK_OFFSET(guest_rsp, 536);
501 CHECK_OFFSET(guest_rip, 544);
502 CHECK_OFFSET(guest_rflags, 552);
503 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
504 CHECK_OFFSET(guest_sysenter_esp, 568);
505 CHECK_OFFSET(guest_sysenter_eip, 576);
506 CHECK_OFFSET(host_cr0, 584);
507 CHECK_OFFSET(host_cr3, 592);
508 CHECK_OFFSET(host_cr4, 600);
509 CHECK_OFFSET(host_fs_base, 608);
510 CHECK_OFFSET(host_gs_base, 616);
511 CHECK_OFFSET(host_tr_base, 624);
512 CHECK_OFFSET(host_gdtr_base, 632);
513 CHECK_OFFSET(host_idtr_base, 640);
514 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
515 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
516 CHECK_OFFSET(host_rsp, 664);
517 CHECK_OFFSET(host_rip, 672);
518 CHECK_OFFSET(pin_based_vm_exec_control, 744);
519 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
520 CHECK_OFFSET(exception_bitmap, 752);
521 CHECK_OFFSET(page_fault_error_code_mask, 756);
522 CHECK_OFFSET(page_fault_error_code_match, 760);
523 CHECK_OFFSET(cr3_target_count, 764);
524 CHECK_OFFSET(vm_exit_controls, 768);
525 CHECK_OFFSET(vm_exit_msr_store_count, 772);
526 CHECK_OFFSET(vm_exit_msr_load_count, 776);
527 CHECK_OFFSET(vm_entry_controls, 780);
528 CHECK_OFFSET(vm_entry_msr_load_count, 784);
529 CHECK_OFFSET(vm_entry_intr_info_field, 788);
530 CHECK_OFFSET(vm_entry_exception_error_code, 792);
531 CHECK_OFFSET(vm_entry_instruction_len, 796);
532 CHECK_OFFSET(tpr_threshold, 800);
533 CHECK_OFFSET(secondary_vm_exec_control, 804);
534 CHECK_OFFSET(vm_instruction_error, 808);
535 CHECK_OFFSET(vm_exit_reason, 812);
536 CHECK_OFFSET(vm_exit_intr_info, 816);
537 CHECK_OFFSET(vm_exit_intr_error_code, 820);
538 CHECK_OFFSET(idt_vectoring_info_field, 824);
539 CHECK_OFFSET(idt_vectoring_error_code, 828);
540 CHECK_OFFSET(vm_exit_instruction_len, 832);
541 CHECK_OFFSET(vmx_instruction_info, 836);
542 CHECK_OFFSET(guest_es_limit, 840);
543 CHECK_OFFSET(guest_cs_limit, 844);
544 CHECK_OFFSET(guest_ss_limit, 848);
545 CHECK_OFFSET(guest_ds_limit, 852);
546 CHECK_OFFSET(guest_fs_limit, 856);
547 CHECK_OFFSET(guest_gs_limit, 860);
548 CHECK_OFFSET(guest_ldtr_limit, 864);
549 CHECK_OFFSET(guest_tr_limit, 868);
550 CHECK_OFFSET(guest_gdtr_limit, 872);
551 CHECK_OFFSET(guest_idtr_limit, 876);
552 CHECK_OFFSET(guest_es_ar_bytes, 880);
553 CHECK_OFFSET(guest_cs_ar_bytes, 884);
554 CHECK_OFFSET(guest_ss_ar_bytes, 888);
555 CHECK_OFFSET(guest_ds_ar_bytes, 892);
556 CHECK_OFFSET(guest_fs_ar_bytes, 896);
557 CHECK_OFFSET(guest_gs_ar_bytes, 900);
558 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
559 CHECK_OFFSET(guest_tr_ar_bytes, 908);
560 CHECK_OFFSET(guest_interruptibility_info, 912);
561 CHECK_OFFSET(guest_activity_state, 916);
562 CHECK_OFFSET(guest_sysenter_cs, 920);
563 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
564 CHECK_OFFSET(vmx_preemption_timer_value, 928);
565 CHECK_OFFSET(virtual_processor_id, 960);
566 CHECK_OFFSET(posted_intr_nv, 962);
567 CHECK_OFFSET(guest_es_selector, 964);
568 CHECK_OFFSET(guest_cs_selector, 966);
569 CHECK_OFFSET(guest_ss_selector, 968);
570 CHECK_OFFSET(guest_ds_selector, 970);
571 CHECK_OFFSET(guest_fs_selector, 972);
572 CHECK_OFFSET(guest_gs_selector, 974);
573 CHECK_OFFSET(guest_ldtr_selector, 976);
574 CHECK_OFFSET(guest_tr_selector, 978);
575 CHECK_OFFSET(guest_intr_status, 980);
576 CHECK_OFFSET(host_es_selector, 982);
577 CHECK_OFFSET(host_cs_selector, 984);
578 CHECK_OFFSET(host_ss_selector, 986);
579 CHECK_OFFSET(host_ds_selector, 988);
580 CHECK_OFFSET(host_fs_selector, 990);
581 CHECK_OFFSET(host_gs_selector, 992);
582 CHECK_OFFSET(host_tr_selector, 994);
583 CHECK_OFFSET(guest_pml_index, 996);
584}
585
586/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300587 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
588 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
589 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700590 *
591 * IMPORTANT: Changing this value will break save/restore compatibility with
592 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300593 */
594#define VMCS12_REVISION 0x11e57ed0
595
596/*
597 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
598 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
599 * current implementation, 4K are reserved to avoid future complications.
600 */
601#define VMCS12_SIZE 0x1000
602
603/*
Jim Mattson5b157062017-12-22 12:11:12 -0800604 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
605 * supported VMCS12 field encoding.
606 */
607#define VMCS12_MAX_FIELD_INDEX 0x17
608
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100609struct nested_vmx_msrs {
610 /*
611 * We only store the "true" versions of the VMX capability MSRs. We
612 * generate the "non-true" versions by setting the must-be-1 bits
613 * according to the SDM.
614 */
615 u32 procbased_ctls_low;
616 u32 procbased_ctls_high;
617 u32 secondary_ctls_low;
618 u32 secondary_ctls_high;
619 u32 pinbased_ctls_low;
620 u32 pinbased_ctls_high;
621 u32 exit_ctls_low;
622 u32 exit_ctls_high;
623 u32 entry_ctls_low;
624 u32 entry_ctls_high;
625 u32 misc_low;
626 u32 misc_high;
627 u32 ept_caps;
628 u32 vpid_caps;
629 u64 basic;
630 u64 cr0_fixed0;
631 u64 cr0_fixed1;
632 u64 cr4_fixed0;
633 u64 cr4_fixed1;
634 u64 vmcs_enum;
635 u64 vmfunc_controls;
636};
637
Jim Mattson5b157062017-12-22 12:11:12 -0800638/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300639 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
640 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
641 */
642struct nested_vmx {
643 /* Has the level1 guest done vmxon? */
644 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400645 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400646 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300647
648 /* The guest-physical address of the current VMCS L1 keeps for L2 */
649 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700650 /*
651 * Cache of the guest's VMCS, existing outside of guest memory.
652 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700653 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700654 */
655 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300656 /*
Liran Alon61ada742018-06-23 02:35:08 +0300657 * Cache of the guest's shadow VMCS, existing outside of guest
658 * memory. Loaded from guest memory during VM entry. Flushed
659 * to guest memory during VM exit.
660 */
661 struct vmcs12 *cached_shadow_vmcs12;
662 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300663 * Indicates if the shadow vmcs must be updated with the
664 * data hold by vmcs12
665 */
666 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100667 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300668
Jim Mattson8d860bb2018-05-09 16:56:05 -0400669 bool change_vmcs01_virtual_apic_mode;
670
Nadav Har'El644d7112011-05-25 23:12:35 +0300671 /* L2 must run next, and mustn't decide to exit to L1. */
672 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600673
674 struct loaded_vmcs vmcs02;
675
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300676 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600677 * Guest pages referred to in the vmcs02 with host-physical
678 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300679 */
680 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800681 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800682 struct page *pi_desc_page;
683 struct pi_desc *pi_desc;
684 bool pi_pending;
685 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100686
687 struct hrtimer preemption_timer;
688 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200689
690 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
691 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800692
Wanpeng Li5c614b32015-10-13 09:18:36 -0700693 u16 vpid02;
694 u16 last_vpid;
695
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100696 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200697
698 /* SMM related state */
699 struct {
700 /* in VMX operation on SMM entry? */
701 bool vmxon;
702 /* in guest mode on SMM entry? */
703 bool guest_mode;
704 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300705};
706
Yang Zhang01e439b2013-04-11 19:25:12 +0800707#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800708#define POSTED_INTR_SN 1
709
Yang Zhang01e439b2013-04-11 19:25:12 +0800710/* Posted-Interrupt Descriptor */
711struct pi_desc {
712 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800713 union {
714 struct {
715 /* bit 256 - Outstanding Notification */
716 u16 on : 1,
717 /* bit 257 - Suppress Notification */
718 sn : 1,
719 /* bit 271:258 - Reserved */
720 rsvd_1 : 14;
721 /* bit 279:272 - Notification Vector */
722 u8 nv;
723 /* bit 287:280 - Reserved */
724 u8 rsvd_2;
725 /* bit 319:288 - Notification Destination */
726 u32 ndst;
727 };
728 u64 control;
729 };
730 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800731} __aligned(64);
732
Yang Zhanga20ed542013-04-11 19:25:15 +0800733static bool pi_test_and_set_on(struct pi_desc *pi_desc)
734{
735 return test_and_set_bit(POSTED_INTR_ON,
736 (unsigned long *)&pi_desc->control);
737}
738
739static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
740{
741 return test_and_clear_bit(POSTED_INTR_ON,
742 (unsigned long *)&pi_desc->control);
743}
744
745static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
746{
747 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
748}
749
Feng Wuebbfc762015-09-18 22:29:46 +0800750static inline void pi_clear_sn(struct pi_desc *pi_desc)
751{
752 return clear_bit(POSTED_INTR_SN,
753 (unsigned long *)&pi_desc->control);
754}
755
756static inline void pi_set_sn(struct pi_desc *pi_desc)
757{
758 return set_bit(POSTED_INTR_SN,
759 (unsigned long *)&pi_desc->control);
760}
761
Paolo Bonziniad361092016-09-20 16:15:05 +0200762static inline void pi_clear_on(struct pi_desc *pi_desc)
763{
764 clear_bit(POSTED_INTR_ON,
765 (unsigned long *)&pi_desc->control);
766}
767
Feng Wuebbfc762015-09-18 22:29:46 +0800768static inline int pi_test_on(struct pi_desc *pi_desc)
769{
770 return test_bit(POSTED_INTR_ON,
771 (unsigned long *)&pi_desc->control);
772}
773
774static inline int pi_test_sn(struct pi_desc *pi_desc)
775{
776 return test_bit(POSTED_INTR_SN,
777 (unsigned long *)&pi_desc->control);
778}
779
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400780struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000781 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300782 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300783 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100784 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300785 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200786 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200787 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300788 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400789 int nmsrs;
790 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800791 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400792#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300793 u64 msr_host_kernel_gs_base;
794 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400795#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100796
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100797 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100798 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100799
Gleb Natapov2961e8762013-11-25 15:37:13 +0200800 u32 vm_entry_controls_shadow;
801 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200802 u32 secondary_exec_control;
803
Nadav Har'Eld462b812011-05-24 15:26:10 +0300804 /*
805 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
806 * non-nested (L1) guest, it always points to vmcs01. For a nested
807 * guest (L2), it points to a different VMCS.
808 */
809 struct loaded_vmcs vmcs01;
810 struct loaded_vmcs *loaded_vmcs;
811 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300812 struct msr_autoload {
813 unsigned nr;
814 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
815 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
816 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400817 struct {
818 int loaded;
819 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300820#ifdef CONFIG_X86_64
821 u16 ds_sel, es_sel;
822#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200823 int gs_ldt_reload_needed;
824 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400825 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200826 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300827 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300828 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300829 struct kvm_segment segs[8];
830 } rmode;
831 struct {
832 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300833 struct kvm_save_segment {
834 u16 selector;
835 unsigned long base;
836 u32 limit;
837 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300838 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300839 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800840 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300841 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200842
Andi Kleena0861c02009-06-08 17:37:09 +0800843 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800844
Yang Zhang01e439b2013-04-11 19:25:12 +0800845 /* Posted interrupt descriptor */
846 struct pi_desc pi_desc;
847
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300848 /* Support for a guest hypervisor (nested VMX) */
849 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200850
851 /* Dynamic PLE window. */
852 int ple_window;
853 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800854
855 /* Support for PML */
856#define PML_ENTITY_NUM 512
857 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800858
Yunhong Jiang64672c92016-06-13 14:19:59 -0700859 /* apic deadline value in host tsc */
860 u64 hv_deadline_tsc;
861
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800862 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800863
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800864 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800865
Wanpeng Li74c55932017-11-29 01:31:20 -0800866 unsigned long host_debugctlmsr;
867
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800868 /*
869 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
870 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
871 * in msr_ia32_feature_control_valid_bits.
872 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800873 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800874 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +0000875 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400876};
877
Avi Kivity2fb92db2011-04-27 19:42:18 +0300878enum segment_cache_field {
879 SEG_FIELD_SEL = 0,
880 SEG_FIELD_BASE = 1,
881 SEG_FIELD_LIMIT = 2,
882 SEG_FIELD_AR = 3,
883
884 SEG_FIELD_NR = 4
885};
886
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700887static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
888{
889 return container_of(kvm, struct kvm_vmx, kvm);
890}
891
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400892static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
893{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000894 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400895}
896
Feng Wuefc64402015-09-18 22:29:51 +0800897static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
898{
899 return &(to_vmx(vcpu)->pi_desc);
900}
901
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800902#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +0300903#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -0800904#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
905#define FIELD64(number, name) \
906 FIELD(number, name), \
907 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +0300908
Abel Gordon4607c2d2013-04-18 14:35:55 +0300909
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100910static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100911#define SHADOW_FIELD_RO(x) x,
912#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300913};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400914static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300915 ARRAY_SIZE(shadow_read_only_fields);
916
Paolo Bonzini44900ba2017-12-13 12:58:02 +0100917static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +0100918#define SHADOW_FIELD_RW(x) x,
919#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +0300920};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400921static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300922 ARRAY_SIZE(shadow_read_write_fields);
923
Mathias Krause772e0312012-08-30 01:30:19 +0200924static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300925 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800926 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300927 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
928 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
929 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
930 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
931 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
932 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
933 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
934 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800935 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -0400936 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300937 FIELD(HOST_ES_SELECTOR, host_es_selector),
938 FIELD(HOST_CS_SELECTOR, host_cs_selector),
939 FIELD(HOST_SS_SELECTOR, host_ss_selector),
940 FIELD(HOST_DS_SELECTOR, host_ds_selector),
941 FIELD(HOST_FS_SELECTOR, host_fs_selector),
942 FIELD(HOST_GS_SELECTOR, host_gs_selector),
943 FIELD(HOST_TR_SELECTOR, host_tr_selector),
944 FIELD64(IO_BITMAP_A, io_bitmap_a),
945 FIELD64(IO_BITMAP_B, io_bitmap_b),
946 FIELD64(MSR_BITMAP, msr_bitmap),
947 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
948 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
949 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -0700950 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300951 FIELD64(TSC_OFFSET, tsc_offset),
952 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
953 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800954 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -0400955 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300956 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800957 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
958 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
959 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
960 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -0400961 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -0700962 FIELD64(VMREAD_BITMAP, vmread_bitmap),
963 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800964 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300965 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
966 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
967 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
968 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
969 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
970 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
971 FIELD64(GUEST_PDPTR0, guest_pdptr0),
972 FIELD64(GUEST_PDPTR1, guest_pdptr1),
973 FIELD64(GUEST_PDPTR2, guest_pdptr2),
974 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100975 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300976 FIELD64(HOST_IA32_PAT, host_ia32_pat),
977 FIELD64(HOST_IA32_EFER, host_ia32_efer),
978 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
979 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
980 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
981 FIELD(EXCEPTION_BITMAP, exception_bitmap),
982 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
983 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
984 FIELD(CR3_TARGET_COUNT, cr3_target_count),
985 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
986 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
987 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
988 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
989 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
990 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
991 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
992 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
993 FIELD(TPR_THRESHOLD, tpr_threshold),
994 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
995 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
996 FIELD(VM_EXIT_REASON, vm_exit_reason),
997 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
998 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
999 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1000 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1001 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1002 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1003 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1004 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1005 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1006 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1007 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1008 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1009 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1010 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1011 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1012 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1013 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1014 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1015 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1016 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1017 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1018 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1019 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1020 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1021 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1022 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1023 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1024 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001025 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001026 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1027 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1028 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1029 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1030 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1031 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1032 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1033 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1034 FIELD(EXIT_QUALIFICATION, exit_qualification),
1035 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1036 FIELD(GUEST_CR0, guest_cr0),
1037 FIELD(GUEST_CR3, guest_cr3),
1038 FIELD(GUEST_CR4, guest_cr4),
1039 FIELD(GUEST_ES_BASE, guest_es_base),
1040 FIELD(GUEST_CS_BASE, guest_cs_base),
1041 FIELD(GUEST_SS_BASE, guest_ss_base),
1042 FIELD(GUEST_DS_BASE, guest_ds_base),
1043 FIELD(GUEST_FS_BASE, guest_fs_base),
1044 FIELD(GUEST_GS_BASE, guest_gs_base),
1045 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1046 FIELD(GUEST_TR_BASE, guest_tr_base),
1047 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1048 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1049 FIELD(GUEST_DR7, guest_dr7),
1050 FIELD(GUEST_RSP, guest_rsp),
1051 FIELD(GUEST_RIP, guest_rip),
1052 FIELD(GUEST_RFLAGS, guest_rflags),
1053 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1054 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1055 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1056 FIELD(HOST_CR0, host_cr0),
1057 FIELD(HOST_CR3, host_cr3),
1058 FIELD(HOST_CR4, host_cr4),
1059 FIELD(HOST_FS_BASE, host_fs_base),
1060 FIELD(HOST_GS_BASE, host_gs_base),
1061 FIELD(HOST_TR_BASE, host_tr_base),
1062 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1063 FIELD(HOST_IDTR_BASE, host_idtr_base),
1064 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1065 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1066 FIELD(HOST_RSP, host_rsp),
1067 FIELD(HOST_RIP, host_rip),
1068};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001069
1070static inline short vmcs_field_to_offset(unsigned long field)
1071{
Dan Williams085331d2018-01-31 17:47:03 -08001072 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1073 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001074 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001075
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001076 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001077 return -ENOENT;
1078
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001079 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001080 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001081 return -ENOENT;
1082
Linus Torvalds15303ba2018-02-10 13:16:35 -08001083 index = array_index_nospec(index, size);
1084 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001085 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001086 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001087 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001088}
1089
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001090static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1091{
David Matlack4f2777b2016-07-13 17:16:37 -07001092 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001093}
1094
Liran Alon61ada742018-06-23 02:35:08 +03001095static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1096{
1097 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1098}
1099
Peter Feiner995f00a2017-06-30 17:26:32 -07001100static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001101static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001102static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001103static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001104static void vmx_set_segment(struct kvm_vcpu *vcpu,
1105 struct kvm_segment *var, int seg);
1106static void vmx_get_segment(struct kvm_vcpu *vcpu,
1107 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001108static bool guest_state_valid(struct kvm_vcpu *vcpu);
1109static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001110static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001111static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1112static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1113static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1114 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001115static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001116static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1117 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001118
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1120static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001121/*
1122 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1123 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1124 */
1125static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126
Feng Wubf9f6ac2015-09-18 22:29:55 +08001127/*
1128 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1129 * can find which vCPU should be waken up.
1130 */
1131static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1132static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1133
Radim Krčmář23611332016-09-29 22:41:33 +02001134enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001135 VMX_VMREAD_BITMAP,
1136 VMX_VMWRITE_BITMAP,
1137 VMX_BITMAP_NR
1138};
1139
1140static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1141
Radim Krčmář23611332016-09-29 22:41:33 +02001142#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1143#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001144
Avi Kivity110312c2010-12-21 12:54:20 +02001145static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001146static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001147
Sheng Yang2384d2b2008-01-17 15:14:33 +08001148static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1149static DEFINE_SPINLOCK(vmx_vpid_lock);
1150
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001151static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152 int size;
1153 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001154 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001156 u32 pin_based_exec_ctrl;
1157 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001158 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001159 u32 vmexit_ctrl;
1160 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001161 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001162} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163
Hannes Ederefff9e52008-11-28 17:02:06 +01001164static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001165 u32 ept;
1166 u32 vpid;
1167} vmx_capability;
1168
Avi Kivity6aa8b732006-12-10 02:21:36 -08001169#define VMX_SEGMENT_FIELD(seg) \
1170 [VCPU_SREG_##seg] = { \
1171 .selector = GUEST_##seg##_SELECTOR, \
1172 .base = GUEST_##seg##_BASE, \
1173 .limit = GUEST_##seg##_LIMIT, \
1174 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1175 }
1176
Mathias Krause772e0312012-08-30 01:30:19 +02001177static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 unsigned selector;
1179 unsigned base;
1180 unsigned limit;
1181 unsigned ar_bytes;
1182} kvm_vmx_segment_fields[] = {
1183 VMX_SEGMENT_FIELD(CS),
1184 VMX_SEGMENT_FIELD(DS),
1185 VMX_SEGMENT_FIELD(ES),
1186 VMX_SEGMENT_FIELD(FS),
1187 VMX_SEGMENT_FIELD(GS),
1188 VMX_SEGMENT_FIELD(SS),
1189 VMX_SEGMENT_FIELD(TR),
1190 VMX_SEGMENT_FIELD(LDTR),
1191};
1192
Avi Kivity26bb0982009-09-07 11:14:12 +03001193static u64 host_efer;
1194
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001195static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1196
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001197/*
Brian Gerst8c065852010-07-17 09:03:26 -04001198 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001199 * away by decrementing the array size.
1200 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001202#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001203 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001205 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001208DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1209
1210#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1211
1212#define KVM_EVMCS_VERSION 1
1213
1214#if IS_ENABLED(CONFIG_HYPERV)
1215static bool __read_mostly enlightened_vmcs = true;
1216module_param(enlightened_vmcs, bool, 0444);
1217
1218static inline void evmcs_write64(unsigned long field, u64 value)
1219{
1220 u16 clean_field;
1221 int offset = get_evmcs_offset(field, &clean_field);
1222
1223 if (offset < 0)
1224 return;
1225
1226 *(u64 *)((char *)current_evmcs + offset) = value;
1227
1228 current_evmcs->hv_clean_fields &= ~clean_field;
1229}
1230
1231static inline void evmcs_write32(unsigned long field, u32 value)
1232{
1233 u16 clean_field;
1234 int offset = get_evmcs_offset(field, &clean_field);
1235
1236 if (offset < 0)
1237 return;
1238
1239 *(u32 *)((char *)current_evmcs + offset) = value;
1240 current_evmcs->hv_clean_fields &= ~clean_field;
1241}
1242
1243static inline void evmcs_write16(unsigned long field, u16 value)
1244{
1245 u16 clean_field;
1246 int offset = get_evmcs_offset(field, &clean_field);
1247
1248 if (offset < 0)
1249 return;
1250
1251 *(u16 *)((char *)current_evmcs + offset) = value;
1252 current_evmcs->hv_clean_fields &= ~clean_field;
1253}
1254
1255static inline u64 evmcs_read64(unsigned long field)
1256{
1257 int offset = get_evmcs_offset(field, NULL);
1258
1259 if (offset < 0)
1260 return 0;
1261
1262 return *(u64 *)((char *)current_evmcs + offset);
1263}
1264
1265static inline u32 evmcs_read32(unsigned long field)
1266{
1267 int offset = get_evmcs_offset(field, NULL);
1268
1269 if (offset < 0)
1270 return 0;
1271
1272 return *(u32 *)((char *)current_evmcs + offset);
1273}
1274
1275static inline u16 evmcs_read16(unsigned long field)
1276{
1277 int offset = get_evmcs_offset(field, NULL);
1278
1279 if (offset < 0)
1280 return 0;
1281
1282 return *(u16 *)((char *)current_evmcs + offset);
1283}
1284
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001285static inline void evmcs_touch_msr_bitmap(void)
1286{
1287 if (unlikely(!current_evmcs))
1288 return;
1289
1290 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1291 current_evmcs->hv_clean_fields &=
1292 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1293}
1294
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001295static void evmcs_load(u64 phys_addr)
1296{
1297 struct hv_vp_assist_page *vp_ap =
1298 hv_get_vp_assist_page(smp_processor_id());
1299
1300 vp_ap->current_nested_vmcs = phys_addr;
1301 vp_ap->enlighten_vmentry = 1;
1302}
1303
1304static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1305{
1306 /*
1307 * Enlightened VMCSv1 doesn't support these:
1308 *
1309 * POSTED_INTR_NV = 0x00000002,
1310 * GUEST_INTR_STATUS = 0x00000810,
1311 * APIC_ACCESS_ADDR = 0x00002014,
1312 * POSTED_INTR_DESC_ADDR = 0x00002016,
1313 * EOI_EXIT_BITMAP0 = 0x0000201c,
1314 * EOI_EXIT_BITMAP1 = 0x0000201e,
1315 * EOI_EXIT_BITMAP2 = 0x00002020,
1316 * EOI_EXIT_BITMAP3 = 0x00002022,
1317 */
1318 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1319 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1320 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1321 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1322 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1323 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1324 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1325
1326 /*
1327 * GUEST_PML_INDEX = 0x00000812,
1328 * PML_ADDRESS = 0x0000200e,
1329 */
1330 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1331
1332 /* VM_FUNCTION_CONTROL = 0x00002018, */
1333 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1334
1335 /*
1336 * EPTP_LIST_ADDRESS = 0x00002024,
1337 * VMREAD_BITMAP = 0x00002026,
1338 * VMWRITE_BITMAP = 0x00002028,
1339 */
1340 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1341
1342 /*
1343 * TSC_MULTIPLIER = 0x00002032,
1344 */
1345 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1346
1347 /*
1348 * PLE_GAP = 0x00004020,
1349 * PLE_WINDOW = 0x00004022,
1350 */
1351 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1352
1353 /*
1354 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1355 */
1356 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1357
1358 /*
1359 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1360 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1361 */
1362 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1363 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1364
1365 /*
1366 * Currently unsupported in KVM:
1367 * GUEST_IA32_RTIT_CTL = 0x00002814,
1368 */
1369}
Tianyu Lan877ad952018-07-19 08:40:23 +00001370
1371/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1372static void check_ept_pointer_match(struct kvm *kvm)
1373{
1374 struct kvm_vcpu *vcpu;
1375 u64 tmp_eptp = INVALID_PAGE;
1376 int i;
1377
1378 kvm_for_each_vcpu(i, vcpu, kvm) {
1379 if (!VALID_PAGE(tmp_eptp)) {
1380 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1381 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1382 to_kvm_vmx(kvm)->ept_pointers_match
1383 = EPT_POINTERS_MISMATCH;
1384 return;
1385 }
1386 }
1387
1388 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1389}
1390
1391static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1392{
1393 int ret;
1394
1395 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1396
1397 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1398 check_ept_pointer_match(kvm);
1399
1400 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1401 ret = -ENOTSUPP;
1402 goto out;
1403 }
1404
1405 ret = hyperv_flush_guest_mapping(
1406 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1407
1408out:
1409 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1410 return ret;
1411}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001412#else /* !IS_ENABLED(CONFIG_HYPERV) */
1413static inline void evmcs_write64(unsigned long field, u64 value) {}
1414static inline void evmcs_write32(unsigned long field, u32 value) {}
1415static inline void evmcs_write16(unsigned long field, u16 value) {}
1416static inline u64 evmcs_read64(unsigned long field) { return 0; }
1417static inline u32 evmcs_read32(unsigned long field) { return 0; }
1418static inline u16 evmcs_read16(unsigned long field) { return 0; }
1419static inline void evmcs_load(u64 phys_addr) {}
1420static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001421static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001422#endif /* IS_ENABLED(CONFIG_HYPERV) */
1423
Jan Kiszka5bb16012016-02-09 20:14:21 +01001424static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425{
1426 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1427 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001428 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1429}
1430
Jan Kiszka6f054852016-02-09 20:15:18 +01001431static inline bool is_debug(u32 intr_info)
1432{
1433 return is_exception_n(intr_info, DB_VECTOR);
1434}
1435
1436static inline bool is_breakpoint(u32 intr_info)
1437{
1438 return is_exception_n(intr_info, BP_VECTOR);
1439}
1440
Jan Kiszka5bb16012016-02-09 20:14:21 +01001441static inline bool is_page_fault(u32 intr_info)
1442{
1443 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444}
1445
Gui Jianfeng31299942010-03-15 17:29:09 +08001446static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001447{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001448 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001449}
1450
Gui Jianfeng31299942010-03-15 17:29:09 +08001451static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001452{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001453 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001454}
1455
Liran Alon9e869482018-03-12 13:12:51 +02001456static inline bool is_gp_fault(u32 intr_info)
1457{
1458 return is_exception_n(intr_info, GP_VECTOR);
1459}
1460
Gui Jianfeng31299942010-03-15 17:29:09 +08001461static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001462{
1463 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1464 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1465}
1466
Gui Jianfeng31299942010-03-15 17:29:09 +08001467static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001468{
1469 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1470 INTR_INFO_VALID_MASK)) ==
1471 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1472}
1473
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001474/* Undocumented: icebp/int1 */
1475static inline bool is_icebp(u32 intr_info)
1476{
1477 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1478 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1479}
1480
Gui Jianfeng31299942010-03-15 17:29:09 +08001481static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001482{
Sheng Yang04547152009-04-01 15:52:31 +08001483 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001484}
1485
Gui Jianfeng31299942010-03-15 17:29:09 +08001486static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001487{
Sheng Yang04547152009-04-01 15:52:31 +08001488 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001489}
1490
Paolo Bonzini35754c92015-07-29 12:05:37 +02001491static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001492{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001493 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001494}
1495
Gui Jianfeng31299942010-03-15 17:29:09 +08001496static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001497{
Sheng Yang04547152009-04-01 15:52:31 +08001498 return vmcs_config.cpu_based_exec_ctrl &
1499 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001500}
1501
Avi Kivity774ead32007-12-26 13:57:04 +02001502static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001503{
Sheng Yang04547152009-04-01 15:52:31 +08001504 return vmcs_config.cpu_based_2nd_exec_ctrl &
1505 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1506}
1507
Yang Zhang8d146952013-01-25 10:18:50 +08001508static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1509{
1510 return vmcs_config.cpu_based_2nd_exec_ctrl &
1511 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1512}
1513
Yang Zhang83d4c282013-01-25 10:18:49 +08001514static inline bool cpu_has_vmx_apic_register_virt(void)
1515{
1516 return vmcs_config.cpu_based_2nd_exec_ctrl &
1517 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1518}
1519
Yang Zhangc7c9c562013-01-25 10:18:51 +08001520static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1521{
1522 return vmcs_config.cpu_based_2nd_exec_ctrl &
1523 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1524}
1525
Yunhong Jiang64672c92016-06-13 14:19:59 -07001526/*
1527 * Comment's format: document - errata name - stepping - processor name.
1528 * Refer from
1529 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1530 */
1531static u32 vmx_preemption_cpu_tfms[] = {
1532/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
15330x000206E6,
1534/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1535/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1536/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
15370x00020652,
1538/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
15390x00020655,
1540/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1541/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1542/*
1543 * 320767.pdf - AAP86 - B1 -
1544 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1545 */
15460x000106E5,
1547/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
15480x000106A0,
1549/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
15500x000106A1,
1551/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
15520x000106A4,
1553 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1554 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1555 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
15560x000106A5,
1557};
1558
1559static inline bool cpu_has_broken_vmx_preemption_timer(void)
1560{
1561 u32 eax = cpuid_eax(0x00000001), i;
1562
1563 /* Clear the reserved bits */
1564 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001565 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001566 if (eax == vmx_preemption_cpu_tfms[i])
1567 return true;
1568
1569 return false;
1570}
1571
1572static inline bool cpu_has_vmx_preemption_timer(void)
1573{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001574 return vmcs_config.pin_based_exec_ctrl &
1575 PIN_BASED_VMX_PREEMPTION_TIMER;
1576}
1577
Yang Zhang01e439b2013-04-11 19:25:12 +08001578static inline bool cpu_has_vmx_posted_intr(void)
1579{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001580 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1581 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001582}
1583
1584static inline bool cpu_has_vmx_apicv(void)
1585{
1586 return cpu_has_vmx_apic_register_virt() &&
1587 cpu_has_vmx_virtual_intr_delivery() &&
1588 cpu_has_vmx_posted_intr();
1589}
1590
Sheng Yang04547152009-04-01 15:52:31 +08001591static inline bool cpu_has_vmx_flexpriority(void)
1592{
1593 return cpu_has_vmx_tpr_shadow() &&
1594 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001595}
1596
Marcelo Tosattie7997942009-06-11 12:07:40 -03001597static inline bool cpu_has_vmx_ept_execute_only(void)
1598{
Gui Jianfeng31299942010-03-15 17:29:09 +08001599 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001600}
1601
Marcelo Tosattie7997942009-06-11 12:07:40 -03001602static inline bool cpu_has_vmx_ept_2m_page(void)
1603{
Gui Jianfeng31299942010-03-15 17:29:09 +08001604 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001605}
1606
Sheng Yang878403b2010-01-05 19:02:29 +08001607static inline bool cpu_has_vmx_ept_1g_page(void)
1608{
Gui Jianfeng31299942010-03-15 17:29:09 +08001609 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001610}
1611
Sheng Yang4bc9b982010-06-02 14:05:24 +08001612static inline bool cpu_has_vmx_ept_4levels(void)
1613{
1614 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1615}
1616
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001617static inline bool cpu_has_vmx_ept_mt_wb(void)
1618{
1619 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1620}
1621
Yu Zhang855feb62017-08-24 20:27:55 +08001622static inline bool cpu_has_vmx_ept_5levels(void)
1623{
1624 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1625}
1626
Xudong Hao83c3a332012-05-28 19:33:35 +08001627static inline bool cpu_has_vmx_ept_ad_bits(void)
1628{
1629 return vmx_capability.ept & VMX_EPT_AD_BIT;
1630}
1631
Gui Jianfeng31299942010-03-15 17:29:09 +08001632static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001633{
Gui Jianfeng31299942010-03-15 17:29:09 +08001634 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001635}
1636
Gui Jianfeng31299942010-03-15 17:29:09 +08001637static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001638{
Gui Jianfeng31299942010-03-15 17:29:09 +08001639 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001640}
1641
Liran Aloncd9a4912018-05-22 17:16:15 +03001642static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1643{
1644 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1645}
1646
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001647static inline bool cpu_has_vmx_invvpid_single(void)
1648{
1649 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1650}
1651
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001652static inline bool cpu_has_vmx_invvpid_global(void)
1653{
1654 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1655}
1656
Wanpeng Li08d839c2017-03-23 05:30:08 -07001657static inline bool cpu_has_vmx_invvpid(void)
1658{
1659 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1660}
1661
Gui Jianfeng31299942010-03-15 17:29:09 +08001662static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001663{
Sheng Yang04547152009-04-01 15:52:31 +08001664 return vmcs_config.cpu_based_2nd_exec_ctrl &
1665 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001666}
1667
Gui Jianfeng31299942010-03-15 17:29:09 +08001668static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001669{
1670 return vmcs_config.cpu_based_2nd_exec_ctrl &
1671 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1672}
1673
Gui Jianfeng31299942010-03-15 17:29:09 +08001674static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001675{
1676 return vmcs_config.cpu_based_2nd_exec_ctrl &
1677 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1678}
1679
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001680static inline bool cpu_has_vmx_basic_inout(void)
1681{
1682 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1683}
1684
Paolo Bonzini35754c92015-07-29 12:05:37 +02001685static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001686{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001687 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001688}
1689
Gui Jianfeng31299942010-03-15 17:29:09 +08001690static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001691{
Sheng Yang04547152009-04-01 15:52:31 +08001692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001694}
1695
Gui Jianfeng31299942010-03-15 17:29:09 +08001696static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_RDTSCP;
1700}
1701
Mao, Junjiead756a12012-07-02 01:18:48 +00001702static inline bool cpu_has_vmx_invpcid(void)
1703{
1704 return vmcs_config.cpu_based_2nd_exec_ctrl &
1705 SECONDARY_EXEC_ENABLE_INVPCID;
1706}
1707
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001708static inline bool cpu_has_virtual_nmis(void)
1709{
1710 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1711}
1712
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001713static inline bool cpu_has_vmx_wbinvd_exit(void)
1714{
1715 return vmcs_config.cpu_based_2nd_exec_ctrl &
1716 SECONDARY_EXEC_WBINVD_EXITING;
1717}
1718
Abel Gordonabc4fc52013-04-18 14:35:25 +03001719static inline bool cpu_has_vmx_shadow_vmcs(void)
1720{
1721 u64 vmx_msr;
1722 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1723 /* check if the cpu supports writing r/o exit information fields */
1724 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1725 return false;
1726
1727 return vmcs_config.cpu_based_2nd_exec_ctrl &
1728 SECONDARY_EXEC_SHADOW_VMCS;
1729}
1730
Kai Huang843e4332015-01-28 10:54:28 +08001731static inline bool cpu_has_vmx_pml(void)
1732{
1733 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1734}
1735
Haozhong Zhang64903d62015-10-20 15:39:09 +08001736static inline bool cpu_has_vmx_tsc_scaling(void)
1737{
1738 return vmcs_config.cpu_based_2nd_exec_ctrl &
1739 SECONDARY_EXEC_TSC_SCALING;
1740}
1741
Bandan Das2a499e42017-08-03 15:54:41 -04001742static inline bool cpu_has_vmx_vmfunc(void)
1743{
1744 return vmcs_config.cpu_based_2nd_exec_ctrl &
1745 SECONDARY_EXEC_ENABLE_VMFUNC;
1746}
1747
Sean Christopherson64f7a112018-04-30 10:01:06 -07001748static bool vmx_umip_emulated(void)
1749{
1750 return vmcs_config.cpu_based_2nd_exec_ctrl &
1751 SECONDARY_EXEC_DESC;
1752}
1753
Sheng Yang04547152009-04-01 15:52:31 +08001754static inline bool report_flexpriority(void)
1755{
1756 return flexpriority_enabled;
1757}
1758
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001759static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1760{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001761 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001762}
1763
Jim Mattsonf4160e42018-05-29 09:11:33 -07001764/*
1765 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1766 * to modify any valid field of the VMCS, or are the VM-exit
1767 * information fields read-only?
1768 */
1769static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1770{
1771 return to_vmx(vcpu)->nested.msrs.misc_low &
1772 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1773}
1774
Marc Orr04473782018-06-20 17:21:29 -07001775static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1776{
1777 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1778}
1779
1780static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1781{
1782 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1783 CPU_BASED_MONITOR_TRAP_FLAG;
1784}
1785
Liran Alonfa97d7d2018-07-18 14:07:59 +02001786static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1787{
1788 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1789 SECONDARY_EXEC_SHADOW_VMCS;
1790}
1791
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001792static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1793{
1794 return vmcs12->cpu_based_vm_exec_control & bit;
1795}
1796
1797static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1798{
1799 return (vmcs12->cpu_based_vm_exec_control &
1800 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1801 (vmcs12->secondary_vm_exec_control & bit);
1802}
1803
Jan Kiszkaf41245002014-03-07 20:03:13 +01001804static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1805{
1806 return vmcs12->pin_based_vm_exec_control &
1807 PIN_BASED_VMX_PREEMPTION_TIMER;
1808}
1809
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001810static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1811{
1812 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1813}
1814
1815static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1816{
1817 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1818}
1819
Nadav Har'El155a97a2013-08-05 11:07:16 +03001820static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1821{
1822 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1823}
1824
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001825static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1826{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001827 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001828}
1829
Bandan Dasc5f983f2017-05-05 15:25:14 -04001830static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1831{
1832 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1833}
1834
Wincy Vanf2b93282015-02-03 23:56:03 +08001835static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1836{
1837 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1838}
1839
Wanpeng Li5c614b32015-10-13 09:18:36 -07001840static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1841{
1842 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1843}
1844
Wincy Van82f0dd42015-02-03 23:57:18 +08001845static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1846{
1847 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1848}
1849
Wincy Van608406e2015-02-03 23:57:51 +08001850static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1851{
1852 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1853}
1854
Wincy Van705699a2015-02-03 23:58:17 +08001855static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1856{
1857 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1858}
1859
Bandan Das27c42a12017-08-03 15:54:42 -04001860static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
1861{
1862 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
1863}
1864
Bandan Das41ab9372017-08-03 15:54:43 -04001865static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
1866{
1867 return nested_cpu_has_vmfunc(vmcs12) &&
1868 (vmcs12->vm_function_control &
1869 VMX_VMFUNC_EPTP_SWITCHING);
1870}
1871
Liran Alonf792d272018-06-23 02:35:05 +03001872static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
1873{
1874 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
1875}
1876
Jim Mattsonef85b672016-12-12 11:01:37 -08001877static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001878{
1879 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001880 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001881}
1882
Jan Kiszka533558b2014-01-04 18:47:20 +01001883static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1884 u32 exit_intr_info,
1885 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001886static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1887 struct vmcs12 *vmcs12,
1888 u32 reason, unsigned long qualification);
1889
Rusty Russell8b9cf982007-07-30 16:31:43 +10001890static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001891{
1892 int i;
1893
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001894 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001895 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001896 return i;
1897 return -1;
1898}
1899
Sheng Yang2384d2b2008-01-17 15:14:33 +08001900static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1901{
1902 struct {
1903 u64 vpid : 16;
1904 u64 rsvd : 48;
1905 u64 gva;
1906 } operand = { vpid, 0, gva };
1907
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001908 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001909 /* CF==1 or ZF==1 --> rc = -1 */
1910 "; ja 1f ; ud2 ; 1:"
1911 : : "a"(&operand), "c"(ext) : "cc", "memory");
1912}
1913
Sheng Yang14394422008-04-28 12:24:45 +08001914static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1915{
1916 struct {
1917 u64 eptp, gpa;
1918 } operand = {eptp, gpa};
1919
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001920 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001921 /* CF==1 or ZF==1 --> rc = -1 */
1922 "; ja 1f ; ud2 ; 1:\n"
1923 : : "a" (&operand), "c" (ext) : "cc", "memory");
1924}
1925
Avi Kivity26bb0982009-09-07 11:14:12 +03001926static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001927{
1928 int i;
1929
Rusty Russell8b9cf982007-07-30 16:31:43 +10001930 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001931 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001932 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001933 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001934}
1935
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936static void vmcs_clear(struct vmcs *vmcs)
1937{
1938 u64 phys_addr = __pa(vmcs);
1939 u8 error;
1940
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001941 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001942 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 : "cc", "memory");
1944 if (error)
1945 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1946 vmcs, phys_addr);
1947}
1948
Nadav Har'Eld462b812011-05-24 15:26:10 +03001949static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1950{
1951 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001952 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1953 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001954 loaded_vmcs->cpu = -1;
1955 loaded_vmcs->launched = 0;
1956}
1957
Dongxiao Xu7725b892010-05-11 18:29:38 +08001958static void vmcs_load(struct vmcs *vmcs)
1959{
1960 u64 phys_addr = __pa(vmcs);
1961 u8 error;
1962
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001963 if (static_branch_unlikely(&enable_evmcs))
1964 return evmcs_load(phys_addr);
1965
Dongxiao Xu7725b892010-05-11 18:29:38 +08001966 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001967 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001968 : "cc", "memory");
1969 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001970 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001971 vmcs, phys_addr);
1972}
1973
Dave Young2965faa2015-09-09 15:38:55 -07001974#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001975/*
1976 * This bitmap is used to indicate whether the vmclear
1977 * operation is enabled on all cpus. All disabled by
1978 * default.
1979 */
1980static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1981
1982static inline void crash_enable_local_vmclear(int cpu)
1983{
1984 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1985}
1986
1987static inline void crash_disable_local_vmclear(int cpu)
1988{
1989 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1990}
1991
1992static inline int crash_local_vmclear_enabled(int cpu)
1993{
1994 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1995}
1996
1997static void crash_vmclear_local_loaded_vmcss(void)
1998{
1999 int cpu = raw_smp_processor_id();
2000 struct loaded_vmcs *v;
2001
2002 if (!crash_local_vmclear_enabled(cpu))
2003 return;
2004
2005 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2006 loaded_vmcss_on_cpu_link)
2007 vmcs_clear(v->vmcs);
2008}
2009#else
2010static inline void crash_enable_local_vmclear(int cpu) { }
2011static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002012#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002013
Nadav Har'Eld462b812011-05-24 15:26:10 +03002014static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002015{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002016 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002017 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002018
Nadav Har'Eld462b812011-05-24 15:26:10 +03002019 if (loaded_vmcs->cpu != cpu)
2020 return; /* vcpu migration can race with cpu offline */
2021 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002022 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002023 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002024 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002025
2026 /*
2027 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2028 * is before setting loaded_vmcs->vcpu to -1 which is done in
2029 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2030 * then adds the vmcs into percpu list before it is deleted.
2031 */
2032 smp_wmb();
2033
Nadav Har'Eld462b812011-05-24 15:26:10 +03002034 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002035 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002036}
2037
Nadav Har'Eld462b812011-05-24 15:26:10 +03002038static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002039{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002040 int cpu = loaded_vmcs->cpu;
2041
2042 if (cpu != -1)
2043 smp_call_function_single(cpu,
2044 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002045}
2046
Junaid Shahidfaff8752018-06-29 13:10:05 -07002047static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2048{
2049 if (vpid == 0)
2050 return true;
2051
2052 if (cpu_has_vmx_invvpid_individual_addr()) {
2053 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2054 return true;
2055 }
2056
2057 return false;
2058}
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
Avi Kivity61d2ef22010-04-28 16:40:38 +03002471static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2472{
2473 unsigned i;
2474 struct msr_autoload *m = &vmx->msr_autoload;
2475
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002476 switch (msr) {
2477 case MSR_EFER:
2478 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002479 clear_atomic_switch_msr_special(vmx,
2480 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002481 VM_EXIT_LOAD_IA32_EFER);
2482 return;
2483 }
2484 break;
2485 case MSR_CORE_PERF_GLOBAL_CTRL:
2486 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002487 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002488 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2489 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2490 return;
2491 }
2492 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002493 }
2494
Avi Kivity61d2ef22010-04-28 16:40:38 +03002495 for (i = 0; i < m->nr; ++i)
2496 if (m->guest[i].index == msr)
2497 break;
2498
2499 if (i == m->nr)
2500 return;
2501 --m->nr;
2502 m->guest[i] = m->guest[m->nr];
2503 m->host[i] = m->host[m->nr];
2504 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2505 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2506}
2507
Gleb Natapov2961e8762013-11-25 15:37:13 +02002508static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2509 unsigned long entry, unsigned long exit,
2510 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2511 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002512{
2513 vmcs_write64(guest_val_vmcs, guest_val);
2514 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002515 vm_entry_controls_setbit(vmx, entry);
2516 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002517}
2518
Avi Kivity61d2ef22010-04-28 16:40:38 +03002519static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2520 u64 guest_val, u64 host_val)
2521{
2522 unsigned i;
2523 struct msr_autoload *m = &vmx->msr_autoload;
2524
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002525 switch (msr) {
2526 case MSR_EFER:
2527 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002528 add_atomic_switch_msr_special(vmx,
2529 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002530 VM_EXIT_LOAD_IA32_EFER,
2531 GUEST_IA32_EFER,
2532 HOST_IA32_EFER,
2533 guest_val, host_val);
2534 return;
2535 }
2536 break;
2537 case MSR_CORE_PERF_GLOBAL_CTRL:
2538 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002539 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002540 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2541 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2542 GUEST_IA32_PERF_GLOBAL_CTRL,
2543 HOST_IA32_PERF_GLOBAL_CTRL,
2544 guest_val, host_val);
2545 return;
2546 }
2547 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002548 case MSR_IA32_PEBS_ENABLE:
2549 /* PEBS needs a quiescent period after being disabled (to write
2550 * a record). Disabling PEBS through VMX MSR swapping doesn't
2551 * provide that period, so a CPU could write host's record into
2552 * guest's memory.
2553 */
2554 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002555 }
2556
Avi Kivity61d2ef22010-04-28 16:40:38 +03002557 for (i = 0; i < m->nr; ++i)
2558 if (m->guest[i].index == msr)
2559 break;
2560
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002561 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002562 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002563 "Can't add msr %x\n", msr);
2564 return;
2565 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002566 ++m->nr;
2567 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2568 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2569 }
2570
2571 m->guest[i].index = msr;
2572 m->guest[i].value = guest_val;
2573 m->host[i].index = msr;
2574 m->host[i].value = host_val;
2575}
2576
Avi Kivity92c0d902009-10-29 11:00:16 +02002577static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002578{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002579 u64 guest_efer = vmx->vcpu.arch.efer;
2580 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002581
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002582 if (!enable_ept) {
2583 /*
2584 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2585 * host CPUID is more efficient than testing guest CPUID
2586 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2587 */
2588 if (boot_cpu_has(X86_FEATURE_SMEP))
2589 guest_efer |= EFER_NX;
2590 else if (!(guest_efer & EFER_NX))
2591 ignore_bits |= EFER_NX;
2592 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002593
Avi Kivity51c6cf62007-08-29 03:48:05 +03002594 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002595 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002596 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002597 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002598#ifdef CONFIG_X86_64
2599 ignore_bits |= EFER_LMA | EFER_LME;
2600 /* SCE is meaningful only in long mode on Intel */
2601 if (guest_efer & EFER_LMA)
2602 ignore_bits &= ~(u64)EFER_SCE;
2603#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002604
2605 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002606
2607 /*
2608 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2609 * On CPUs that support "load IA32_EFER", always switch EFER
2610 * atomically, since it's faster than switching it manually.
2611 */
2612 if (cpu_has_load_ia32_efer ||
2613 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002614 if (!(guest_efer & EFER_LMA))
2615 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002616 if (guest_efer != host_efer)
2617 add_atomic_switch_msr(vmx, MSR_EFER,
2618 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002619 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002620 } else {
2621 guest_efer &= ~ignore_bits;
2622 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002623
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002624 vmx->guest_msrs[efer_offset].data = guest_efer;
2625 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2626
2627 return true;
2628 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002629}
2630
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002631#ifdef CONFIG_X86_32
2632/*
2633 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2634 * VMCS rather than the segment table. KVM uses this helper to figure
2635 * out the current bases to poke them into the VMCS before entry.
2636 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002637static unsigned long segment_base(u16 selector)
2638{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002639 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002640 unsigned long v;
2641
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002642 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002643 return 0;
2644
Thomas Garnier45fc8752017-03-14 10:05:08 -07002645 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002646
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002647 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002648 u16 ldt_selector = kvm_read_ldt();
2649
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002650 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002651 return 0;
2652
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002653 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002654 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002655 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002656 return v;
2657}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002658#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002659
Avi Kivity04d2cc72007-09-10 18:10:54 +03002660static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002661{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002662 struct vcpu_vmx *vmx = to_vmx(vcpu);
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002663#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002664 int cpu = raw_smp_processor_id();
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002665 unsigned long fs_base, kernel_gs_base;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002666#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002667 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002668
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002669 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002670 return;
2671
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002672 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002673 /*
2674 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2675 * allow segment selectors with cpl > 0 or ti == 1.
2676 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002677 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002678 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002679
2680#ifdef CONFIG_X86_64
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002681 if (likely(is_64bit_mm(current->mm))) {
2682 save_fsgs_for_kvm();
2683 vmx->host_state.fs_sel = current->thread.fsindex;
2684 vmx->host_state.gs_sel = current->thread.gsindex;
2685 fs_base = current->thread.fsbase;
2686 kernel_gs_base = current->thread.gsbase;
2687 } else {
2688#endif
2689 savesegment(fs, vmx->host_state.fs_sel);
2690 savesegment(gs, vmx->host_state.gs_sel);
2691#ifdef CONFIG_X86_64
2692 fs_base = read_msr(MSR_FS_BASE);
2693 kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2694 }
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002695#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002696 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002697 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002698 vmx->host_state.fs_reload_needed = 0;
2699 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002700 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002701 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002702 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002703 if (!(vmx->host_state.gs_sel & 7))
2704 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002705 else {
2706 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002707 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002708 }
2709
2710#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002711 savesegment(ds, vmx->host_state.ds_sel);
2712 savesegment(es, vmx->host_state.es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002713
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002714 vmcs_writel(HOST_FS_BASE, fs_base);
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002715 vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu));
Avi Kivity707c0872007-05-02 17:33:43 +03002716
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002717 vmx->msr_host_kernel_gs_base = kernel_gs_base;
Avi Kivityc8770e72010-11-11 12:37:26 +02002718 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002719 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002720#else
2721 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2722 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
2723#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03002724 for (i = 0; i < vmx->save_nmsrs; ++i)
2725 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002726 vmx->guest_msrs[i].data,
2727 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002728}
2729
Avi Kivitya9b21b62008-06-24 11:48:49 +03002730static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002731{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002732 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002733 return;
2734
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002735 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002736 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002737#ifdef CONFIG_X86_64
2738 if (is_long_mode(&vmx->vcpu))
2739 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2740#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002741 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002742 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002743#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002744 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002745#else
2746 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002747#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002748 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002749 if (vmx->host_state.fs_reload_needed)
2750 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002751#ifdef CONFIG_X86_64
2752 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2753 loadsegment(ds, vmx->host_state.ds_sel);
2754 loadsegment(es, vmx->host_state.es_sel);
2755 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002756#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002757 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002758#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002759 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002760#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002761 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002762}
2763
Avi Kivitya9b21b62008-06-24 11:48:49 +03002764static void vmx_load_host_state(struct vcpu_vmx *vmx)
2765{
2766 preempt_disable();
2767 __vmx_load_host_state(vmx);
2768 preempt_enable();
2769}
2770
Feng Wu28b835d2015-09-18 22:29:54 +08002771static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2772{
2773 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2774 struct pi_desc old, new;
2775 unsigned int dest;
2776
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002777 /*
2778 * In case of hot-plug or hot-unplug, we may have to undo
2779 * vmx_vcpu_pi_put even if there is no assigned device. And we
2780 * always keep PI.NDST up to date for simplicity: it makes the
2781 * code easier, and CPU migration is not a fast path.
2782 */
2783 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002784 return;
2785
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002786 /*
2787 * First handle the simple case where no cmpxchg is necessary; just
2788 * allow posting non-urgent interrupts.
2789 *
2790 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2791 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2792 * expects the VCPU to be on the blocked_vcpu_list that matches
2793 * PI.NDST.
2794 */
2795 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2796 vcpu->cpu == cpu) {
2797 pi_clear_sn(pi_desc);
2798 return;
2799 }
2800
2801 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002802 do {
2803 old.control = new.control = pi_desc->control;
2804
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002805 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002806
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002807 if (x2apic_enabled())
2808 new.ndst = dest;
2809 else
2810 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002811
Feng Wu28b835d2015-09-18 22:29:54 +08002812 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02002813 } while (cmpxchg64(&pi_desc->control, old.control,
2814 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002815}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002816
Peter Feinerc95ba922016-08-17 09:36:47 -07002817static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2818{
2819 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2820 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2821}
2822
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823/*
2824 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2825 * vcpu mutex is already taken.
2826 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002827static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002829 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002830 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002832 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002833 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002834 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002835 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002836
2837 /*
2838 * Read loaded_vmcs->cpu should be before fetching
2839 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2840 * See the comments in __loaded_vmcs_clear().
2841 */
2842 smp_rmb();
2843
Nadav Har'Eld462b812011-05-24 15:26:10 +03002844 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2845 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002846 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002847 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002848 }
2849
2850 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2851 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2852 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01002853 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002854 }
2855
2856 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002857 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002858 unsigned long sysenter_esp;
2859
2860 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002861
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 /*
2863 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002864 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002866 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01002867 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07002868 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002870 /*
2871 * VM exits change the host TR limit to 0x67 after a VM
2872 * exit. This is okay, since 0x67 covers everything except
2873 * the IO bitmap and have have code to handle the IO bitmap
2874 * being lost after a VM exit.
2875 */
2876 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2877
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2879 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002880
Nadav Har'Eld462b812011-05-24 15:26:10 +03002881 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 }
Feng Wu28b835d2015-09-18 22:29:54 +08002883
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002884 /* Setup TSC multiplier */
2885 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002886 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2887 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002888
Feng Wu28b835d2015-09-18 22:29:54 +08002889 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002890 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08002891 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08002892}
2893
2894static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2895{
2896 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2897
2898 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002899 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2900 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002901 return;
2902
2903 /* Set SN when the vCPU is preempted */
2904 if (vcpu->preempted)
2905 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002906}
2907
2908static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2909{
Feng Wu28b835d2015-09-18 22:29:54 +08002910 vmx_vcpu_pi_put(vcpu);
2911
Avi Kivitya9b21b62008-06-24 11:48:49 +03002912 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913}
2914
Wanpeng Lif244dee2017-07-20 01:11:54 -07002915static bool emulation_required(struct kvm_vcpu *vcpu)
2916{
2917 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2918}
2919
Avi Kivityedcafe32009-12-30 18:07:40 +02002920static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2921
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002922/*
2923 * Return the cr0 value that a nested guest would read. This is a combination
2924 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2925 * its hypervisor (cr0_read_shadow).
2926 */
2927static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2928{
2929 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2930 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2931}
2932static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2933{
2934 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2935 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2936}
2937
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2939{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002940 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002941
Avi Kivity6de12732011-03-07 12:51:22 +02002942 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2943 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2944 rflags = vmcs_readl(GUEST_RFLAGS);
2945 if (to_vmx(vcpu)->rmode.vm86_active) {
2946 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2947 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2948 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2949 }
2950 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002951 }
Avi Kivity6de12732011-03-07 12:51:22 +02002952 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953}
2954
2955static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2956{
Wanpeng Lif244dee2017-07-20 01:11:54 -07002957 unsigned long old_rflags = vmx_get_rflags(vcpu);
2958
Avi Kivity6de12732011-03-07 12:51:22 +02002959 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2960 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002961 if (to_vmx(vcpu)->rmode.vm86_active) {
2962 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002963 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002964 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07002966
2967 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
2968 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969}
2970
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002971static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002972{
2973 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2974 int ret = 0;
2975
2976 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002977 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002978 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002979 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002980
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002981 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002982}
2983
2984static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2985{
2986 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2987 u32 interruptibility = interruptibility_old;
2988
2989 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2990
Jan Kiszka48005f62010-02-19 19:38:07 +01002991 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002992 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002993 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002994 interruptibility |= GUEST_INTR_STATE_STI;
2995
2996 if ((interruptibility != interruptibility_old))
2997 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2998}
2999
Avi Kivity6aa8b732006-12-10 02:21:36 -08003000static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3001{
3002 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003004 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003006 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007
Glauber Costa2809f5d2009-05-12 16:21:05 -04003008 /* skipping an emulated instruction also counts */
3009 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010}
3011
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003012static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3013 unsigned long exit_qual)
3014{
3015 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3016 unsigned int nr = vcpu->arch.exception.nr;
3017 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3018
3019 if (vcpu->arch.exception.has_error_code) {
3020 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3021 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3022 }
3023
3024 if (kvm_exception_is_soft(nr))
3025 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3026 else
3027 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3028
3029 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3030 vmx_get_nmi_mask(vcpu))
3031 intr_info |= INTR_INFO_UNBLOCK_NMI;
3032
3033 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3034}
3035
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003036/*
3037 * KVM wants to inject page-faults which it got to the guest. This function
3038 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003039 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003040static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003041{
3042 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003043 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003044
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003045 if (nr == PF_VECTOR) {
3046 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003047 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003048 return 1;
3049 }
3050 /*
3051 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3052 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3053 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3054 * can be written only when inject_pending_event runs. This should be
3055 * conditional on a new capability---if the capability is disabled,
3056 * kvm_multiple_exception would write the ancillary information to
3057 * CR2 or DR6, for backwards ABI-compatibility.
3058 */
3059 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3060 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003061 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003062 return 1;
3063 }
3064 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003065 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003066 if (nr == DB_VECTOR)
3067 *exit_qual = vcpu->arch.dr6;
3068 else
3069 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003070 return 1;
3071 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003072 }
3073
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003074 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003075}
3076
Wanpeng Licaa057a2018-03-12 04:53:03 -07003077static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3078{
3079 /*
3080 * Ensure that we clear the HLT state in the VMCS. We don't need to
3081 * explicitly skip the instruction because if the HLT state is set,
3082 * then the instruction is already executing and RIP has already been
3083 * advanced.
3084 */
3085 if (kvm_hlt_in_guest(vcpu->kvm) &&
3086 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3087 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3088}
3089
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003090static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003091{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003092 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003093 unsigned nr = vcpu->arch.exception.nr;
3094 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003095 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003096 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003097
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003098 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003099 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003100 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3101 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003102
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003103 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003104 int inc_eip = 0;
3105 if (kvm_exception_is_soft(nr))
3106 inc_eip = vcpu->arch.event_exit_inst_len;
3107 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003108 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003109 return;
3110 }
3111
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003112 WARN_ON_ONCE(vmx->emulation_required);
3113
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003114 if (kvm_exception_is_soft(nr)) {
3115 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3116 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003117 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3118 } else
3119 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3120
3121 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003122
3123 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003124}
3125
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003126static bool vmx_rdtscp_supported(void)
3127{
3128 return cpu_has_vmx_rdtscp();
3129}
3130
Mao, Junjiead756a12012-07-02 01:18:48 +00003131static bool vmx_invpcid_supported(void)
3132{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003133 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003134}
3135
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136/*
Eddie Donga75beee2007-05-17 18:55:15 +03003137 * Swap MSR entry in host/guest MSR entry array.
3138 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003139static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003140{
Avi Kivity26bb0982009-09-07 11:14:12 +03003141 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003142
3143 tmp = vmx->guest_msrs[to];
3144 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3145 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003146}
3147
3148/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003149 * Set up the vmcs to automatically save and restore system
3150 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3151 * mode, as fiddling with msrs is very expensive.
3152 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003153static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003154{
Avi Kivity26bb0982009-09-07 11:14:12 +03003155 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003156
Eddie Donga75beee2007-05-17 18:55:15 +03003157 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003158#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003159 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003160 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003161 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003162 move_msr_up(vmx, index, save_nmsrs++);
3163 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003164 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003165 move_msr_up(vmx, index, save_nmsrs++);
3166 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003167 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003168 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003169 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003170 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003171 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003172 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003173 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003174 * if efer.sce is enabled.
3175 */
Brian Gerst8c065852010-07-17 09:03:26 -04003176 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003177 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003178 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003179 }
Eddie Donga75beee2007-05-17 18:55:15 +03003180#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003181 index = __find_msr_index(vmx, MSR_EFER);
3182 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003183 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003184
Avi Kivity26bb0982009-09-07 11:14:12 +03003185 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003186
Yang Zhang8d146952013-01-25 10:18:50 +08003187 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003188 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003189}
3190
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003191static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003193 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003195 if (is_guest_mode(vcpu) &&
3196 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3197 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3198
3199 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200}
3201
3202/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003203 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003205static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003207 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003208 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003209 * We're here if L1 chose not to trap WRMSR to TSC. According
3210 * to the spec, this should set L1's TSC; The offset that L1
3211 * set for L2 remains unchanged, and still needs to be added
3212 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003213 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003214 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003215 /* recalculate vmcs02.TSC_OFFSET: */
3216 vmcs12 = get_vmcs12(vcpu);
3217 vmcs_write64(TSC_OFFSET, offset +
3218 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3219 vmcs12->tsc_offset : 0));
3220 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003221 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3222 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003223 vmcs_write64(TSC_OFFSET, offset);
3224 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225}
3226
Nadav Har'El801d3422011-05-25 23:02:23 +03003227/*
3228 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3229 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3230 * all guests if the "nested" module option is off, and can also be disabled
3231 * for a single guest by disabling its VMX cpuid bit.
3232 */
3233static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3234{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003235 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003236}
3237
Avi Kivity6aa8b732006-12-10 02:21:36 -08003238/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003239 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3240 * returned for the various VMX controls MSRs when nested VMX is enabled.
3241 * The same values should also be used to verify that vmcs12 control fields are
3242 * valid during nested entry from L1 to L2.
3243 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3244 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3245 * bit in the high half is on if the corresponding bit in the control field
3246 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003247 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003248static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003249{
Paolo Bonzini13893092018-02-26 13:40:09 +01003250 if (!nested) {
3251 memset(msrs, 0, sizeof(*msrs));
3252 return;
3253 }
3254
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003255 /*
3256 * Note that as a general rule, the high half of the MSRs (bits in
3257 * the control fields which may be 1) should be initialized by the
3258 * intersection of the underlying hardware's MSR (i.e., features which
3259 * can be supported) and the list of features we want to expose -
3260 * because they are known to be properly supported in our code.
3261 * Also, usually, the low half of the MSRs (bits which must be 1) can
3262 * be set to 0, meaning that L1 may turn off any of these bits. The
3263 * reason is that if one of these bits is necessary, it will appear
3264 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3265 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003266 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003267 * These rules have exceptions below.
3268 */
3269
3270 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003271 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003272 msrs->pinbased_ctls_low,
3273 msrs->pinbased_ctls_high);
3274 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003275 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003276 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003277 PIN_BASED_EXT_INTR_MASK |
3278 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003279 PIN_BASED_VIRTUAL_NMIS |
3280 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003281 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003282 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003283 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003284
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003285 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003286 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003287 msrs->exit_ctls_low,
3288 msrs->exit_ctls_high);
3289 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003290 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003291
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003292 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003293#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003294 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003295#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003296 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003297 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003298 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003299 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003300 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3301
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003302 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003303 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003304
Jan Kiszka2996fca2014-06-16 13:59:43 +02003305 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003306 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003307
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003308 /* entry controls */
3309 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003310 msrs->entry_ctls_low,
3311 msrs->entry_ctls_high);
3312 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003313 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003314 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003315#ifdef CONFIG_X86_64
3316 VM_ENTRY_IA32E_MODE |
3317#endif
3318 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003319 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003320 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003321 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003322 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003323
Jan Kiszka2996fca2014-06-16 13:59:43 +02003324 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003325 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003326
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003327 /* cpu-based controls */
3328 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003329 msrs->procbased_ctls_low,
3330 msrs->procbased_ctls_high);
3331 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003332 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003333 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003334 CPU_BASED_VIRTUAL_INTR_PENDING |
3335 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003336 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3337 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3338 CPU_BASED_CR3_STORE_EXITING |
3339#ifdef CONFIG_X86_64
3340 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3341#endif
3342 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003343 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3344 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3345 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3346 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003347 /*
3348 * We can allow some features even when not supported by the
3349 * hardware. For example, L1 can specify an MSR bitmap - and we
3350 * can use it to avoid exits to L1 - even when L0 runs L2
3351 * without MSR bitmaps.
3352 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003353 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003354 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003355 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003356
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003357 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003358 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003359 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3360
Paolo Bonzini80154d72017-08-24 13:55:35 +02003361 /*
3362 * secondary cpu-based controls. Do not include those that
3363 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3364 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003365 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003366 msrs->secondary_ctls_low,
3367 msrs->secondary_ctls_high);
3368 msrs->secondary_ctls_low = 0;
3369 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003370 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003371 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003372 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003373 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003374 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003375 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003376 /*
3377 * We can emulate "VMCS shadowing," even if the hardware
3378 * doesn't support it.
3379 */
3380 msrs->secondary_ctls_high |=
3381 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003382
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003383 if (enable_ept) {
3384 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003385 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003386 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003387 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003388 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003389 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003390 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003391 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003392 msrs->ept_caps &= vmx_capability.ept;
3393 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003394 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3395 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003396 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003397 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003398 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003399 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003400 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003401 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003402
Bandan Das27c42a12017-08-03 15:54:42 -04003403 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003404 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003405 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003406 /*
3407 * Advertise EPTP switching unconditionally
3408 * since we emulate it
3409 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003410 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003411 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003412 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003413 }
3414
Paolo Bonzinief697a72016-03-18 16:58:38 +01003415 /*
3416 * Old versions of KVM use the single-context version without
3417 * checking for support, so declare that it is supported even
3418 * though it is treated as global context. The alternative is
3419 * not failing the single-context invvpid, and it is worse.
3420 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003421 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003422 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003423 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003424 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003425 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003426 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003427
Radim Krčmář0790ec12015-03-17 14:02:32 +01003428 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003429 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003430 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3431
Jan Kiszkac18911a2013-03-13 16:06:41 +01003432 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003433 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003434 msrs->misc_low,
3435 msrs->misc_high);
3436 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3437 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003438 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003439 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003440 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003441 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003442
3443 /*
3444 * This MSR reports some information about VMX support. We
3445 * should return information about the VMX we emulate for the
3446 * guest, and the VMCS structure we give it - not about the
3447 * VMX support of the underlying hardware.
3448 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003449 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003450 VMCS12_REVISION |
3451 VMX_BASIC_TRUE_CTLS |
3452 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3453 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3454
3455 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003456 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003457
3458 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003459 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003460 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3461 * We picked the standard core2 setting.
3462 */
3463#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3464#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003465 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3466 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003467
3468 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003469 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3470 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003471
3472 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003473 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003474}
3475
David Matlack38991522016-11-29 18:14:08 -08003476/*
3477 * if fixed0[i] == 1: val[i] must be 1
3478 * if fixed1[i] == 0: val[i] must be 0
3479 */
3480static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3481{
3482 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003483}
3484
3485static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3486{
David Matlack38991522016-11-29 18:14:08 -08003487 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003488}
3489
3490static inline u64 vmx_control_msr(u32 low, u32 high)
3491{
3492 return low | ((u64)high << 32);
3493}
3494
David Matlack62cc6b9d2016-11-29 18:14:07 -08003495static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3496{
3497 superset &= mask;
3498 subset &= mask;
3499
3500 return (superset | subset) == superset;
3501}
3502
3503static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3504{
3505 const u64 feature_and_reserved =
3506 /* feature (except bit 48; see below) */
3507 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3508 /* reserved */
3509 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003511
3512 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3513 return -EINVAL;
3514
3515 /*
3516 * KVM does not emulate a version of VMX that constrains physical
3517 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3518 */
3519 if (data & BIT_ULL(48))
3520 return -EINVAL;
3521
3522 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3523 vmx_basic_vmcs_revision_id(data))
3524 return -EINVAL;
3525
3526 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3527 return -EINVAL;
3528
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003529 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003530 return 0;
3531}
3532
3533static int
3534vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3535{
3536 u64 supported;
3537 u32 *lowp, *highp;
3538
3539 switch (msr_index) {
3540 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3542 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003543 break;
3544 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 lowp = &vmx->nested.msrs.procbased_ctls_low;
3546 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003547 break;
3548 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003549 lowp = &vmx->nested.msrs.exit_ctls_low;
3550 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003551 break;
3552 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003553 lowp = &vmx->nested.msrs.entry_ctls_low;
3554 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003555 break;
3556 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003557 lowp = &vmx->nested.msrs.secondary_ctls_low;
3558 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003559 break;
3560 default:
3561 BUG();
3562 }
3563
3564 supported = vmx_control_msr(*lowp, *highp);
3565
3566 /* Check must-be-1 bits are still 1. */
3567 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3568 return -EINVAL;
3569
3570 /* Check must-be-0 bits are still 0. */
3571 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3572 return -EINVAL;
3573
3574 *lowp = data;
3575 *highp = data >> 32;
3576 return 0;
3577}
3578
3579static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3580{
3581 const u64 feature_and_reserved_bits =
3582 /* feature */
3583 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3584 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3585 /* reserved */
3586 GENMASK_ULL(13, 9) | BIT_ULL(31);
3587 u64 vmx_misc;
3588
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003589 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3590 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003591
3592 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3593 return -EINVAL;
3594
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003595 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003596 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3597 vmx_misc_preemption_timer_rate(data) !=
3598 vmx_misc_preemption_timer_rate(vmx_misc))
3599 return -EINVAL;
3600
3601 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3602 return -EINVAL;
3603
3604 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3605 return -EINVAL;
3606
3607 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3608 return -EINVAL;
3609
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 vmx->nested.msrs.misc_low = data;
3611 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003612
3613 /*
3614 * If L1 has read-only VM-exit information fields, use the
3615 * less permissive vmx_vmwrite_bitmap to specify write
3616 * permissions for the shadow VMCS.
3617 */
3618 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3619 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3620
David Matlack62cc6b9d2016-11-29 18:14:07 -08003621 return 0;
3622}
3623
3624static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3625{
3626 u64 vmx_ept_vpid_cap;
3627
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003628 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3629 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003630
3631 /* Every bit is either reserved or a feature bit. */
3632 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3633 return -EINVAL;
3634
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003635 vmx->nested.msrs.ept_caps = data;
3636 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003637 return 0;
3638}
3639
3640static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3641{
3642 u64 *msr;
3643
3644 switch (msr_index) {
3645 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003646 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003647 break;
3648 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003649 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003650 break;
3651 default:
3652 BUG();
3653 }
3654
3655 /*
3656 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3657 * must be 1 in the restored value.
3658 */
3659 if (!is_bitwise_subset(data, *msr, -1ULL))
3660 return -EINVAL;
3661
3662 *msr = data;
3663 return 0;
3664}
3665
3666/*
3667 * Called when userspace is restoring VMX MSRs.
3668 *
3669 * Returns 0 on success, non-0 otherwise.
3670 */
3671static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3672{
3673 struct vcpu_vmx *vmx = to_vmx(vcpu);
3674
Jim Mattsona943ac52018-05-29 09:11:32 -07003675 /*
3676 * Don't allow changes to the VMX capability MSRs while the vCPU
3677 * is in VMX operation.
3678 */
3679 if (vmx->nested.vmxon)
3680 return -EBUSY;
3681
David Matlack62cc6b9d2016-11-29 18:14:07 -08003682 switch (msr_index) {
3683 case MSR_IA32_VMX_BASIC:
3684 return vmx_restore_vmx_basic(vmx, data);
3685 case MSR_IA32_VMX_PINBASED_CTLS:
3686 case MSR_IA32_VMX_PROCBASED_CTLS:
3687 case MSR_IA32_VMX_EXIT_CTLS:
3688 case MSR_IA32_VMX_ENTRY_CTLS:
3689 /*
3690 * The "non-true" VMX capability MSRs are generated from the
3691 * "true" MSRs, so we do not support restoring them directly.
3692 *
3693 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3694 * should restore the "true" MSRs with the must-be-1 bits
3695 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3696 * DEFAULT SETTINGS".
3697 */
3698 return -EINVAL;
3699 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3700 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3701 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3702 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3703 case MSR_IA32_VMX_PROCBASED_CTLS2:
3704 return vmx_restore_control_msr(vmx, msr_index, data);
3705 case MSR_IA32_VMX_MISC:
3706 return vmx_restore_vmx_misc(vmx, data);
3707 case MSR_IA32_VMX_CR0_FIXED0:
3708 case MSR_IA32_VMX_CR4_FIXED0:
3709 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3710 case MSR_IA32_VMX_CR0_FIXED1:
3711 case MSR_IA32_VMX_CR4_FIXED1:
3712 /*
3713 * These MSRs are generated based on the vCPU's CPUID, so we
3714 * do not support restoring them directly.
3715 */
3716 return -EINVAL;
3717 case MSR_IA32_VMX_EPT_VPID_CAP:
3718 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3719 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003720 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003721 return 0;
3722 default:
3723 /*
3724 * The rest of the VMX capability MSRs do not support restore.
3725 */
3726 return -EINVAL;
3727 }
3728}
3729
Jan Kiszkacae50132014-01-04 18:47:22 +01003730/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003731static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003732{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003733 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003734 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003735 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003736 break;
3737 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3738 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003739 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003740 msrs->pinbased_ctls_low,
3741 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003742 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3743 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003744 break;
3745 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3746 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003747 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003748 msrs->procbased_ctls_low,
3749 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003750 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3751 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003752 break;
3753 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3754 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003755 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003756 msrs->exit_ctls_low,
3757 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003758 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3759 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003760 break;
3761 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3762 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003763 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003764 msrs->entry_ctls_low,
3765 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003766 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3767 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003768 break;
3769 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003770 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003771 msrs->misc_low,
3772 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003773 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003774 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003776 break;
3777 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003778 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003779 break;
3780 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003781 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003782 break;
3783 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003784 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003785 break;
3786 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003787 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003788 break;
3789 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003790 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003791 msrs->secondary_ctls_low,
3792 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003793 break;
3794 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003795 *pdata = msrs->ept_caps |
3796 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003797 break;
Bandan Das27c42a12017-08-03 15:54:42 -04003798 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003799 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04003800 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003801 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003802 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003803 }
3804
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003805 return 0;
3806}
3807
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003808static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3809 uint64_t val)
3810{
3811 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3812
3813 return !(val & ~valid_bits);
3814}
3815
Tom Lendacky801e4592018-02-21 13:39:51 -06003816static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
3817{
Paolo Bonzini13893092018-02-26 13:40:09 +01003818 switch (msr->index) {
3819 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3820 if (!nested)
3821 return 1;
3822 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
3823 default:
3824 return 1;
3825 }
3826
3827 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06003828}
3829
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003830/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831 * Reads an msr value (of 'msr_index') into 'pdata'.
3832 * Returns 0 on success, non-0 otherwise.
3833 * Assumes vcpu_load() was already called.
3834 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003835static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836{
Borislav Petkova6cb0992017-12-20 12:50:28 +01003837 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003838 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003840 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003841#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003843 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844 break;
3845 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003846 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003848 case MSR_KERNEL_GS_BASE:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003849 vmx_load_host_state(vmx);
3850 msr_info->data = vmx->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003851 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003852#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003853 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003854 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003855 case MSR_IA32_SPEC_CTRL:
3856 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003857 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3858 return 1;
3859
3860 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3861 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01003862 case MSR_IA32_ARCH_CAPABILITIES:
3863 if (!msr_info->host_initiated &&
3864 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
3865 return 1;
3866 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3867 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003869 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003870 break;
3871 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003872 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873 break;
3874 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003875 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003877 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003878 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003879 (!msr_info->host_initiated &&
3880 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003881 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003882 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003883 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003884 case MSR_IA32_MCG_EXT_CTL:
3885 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01003886 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08003887 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003888 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003889 msr_info->data = vcpu->arch.mcg_ext_ctl;
3890 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003891 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003892 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003893 break;
3894 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3895 if (!nested_vmx_allowed(vcpu))
3896 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003897 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
3898 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003899 case MSR_IA32_XSS:
3900 if (!vmx_xsaves_supported())
3901 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003902 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003903 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003904 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02003905 if (!msr_info->host_initiated &&
3906 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003907 return 1;
3908 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01003910 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003911 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003912 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003913 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003915 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916 }
3917
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918 return 0;
3919}
3920
Jan Kiszkacae50132014-01-04 18:47:22 +01003921static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3922
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923/*
3924 * Writes msr value into into the appropriate "register".
3925 * Returns 0 on success, non-0 otherwise.
3926 * Assumes vcpu_load() was already called.
3927 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003928static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003930 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003931 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003932 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003933 u32 msr_index = msr_info->index;
3934 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003935
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003937 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003938 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003939 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003940#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003942 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943 vmcs_writel(GUEST_FS_BASE, data);
3944 break;
3945 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003946 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003947 vmcs_writel(GUEST_GS_BASE, data);
3948 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003949 case MSR_KERNEL_GS_BASE:
3950 vmx_load_host_state(vmx);
3951 vmx->msr_guest_kernel_gs_base = data;
3952 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003953#endif
3954 case MSR_IA32_SYSENTER_CS:
3955 vmcs_write32(GUEST_SYSENTER_CS, data);
3956 break;
3957 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003958 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003959 break;
3960 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003961 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003962 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003963 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08003964 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02003965 (!msr_info->host_initiated &&
3966 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003967 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08003968 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07003969 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003970 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003971 vmcs_write64(GUEST_BNDCFGS, data);
3972 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003973 case MSR_IA32_SPEC_CTRL:
3974 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003975 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
3976 return 1;
3977
3978 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02003979 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01003980 return 1;
3981
3982 vmx->spec_ctrl = data;
3983
3984 if (!data)
3985 break;
3986
3987 /*
3988 * For non-nested:
3989 * When it's written (to non-zero) for the first time, pass
3990 * it through.
3991 *
3992 * For nested:
3993 * The handling of the MSR bitmap for L2 guests is done in
3994 * nested_vmx_merge_msr_bitmap. We should not touch the
3995 * vmcs02.msr_bitmap here since it gets completely overwritten
3996 * in the merging. We update the vmcs01 here for L1 as well
3997 * since it will end up touching the MSR anyway now.
3998 */
3999 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4000 MSR_IA32_SPEC_CTRL,
4001 MSR_TYPE_RW);
4002 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004003 case MSR_IA32_PRED_CMD:
4004 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004005 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4006 return 1;
4007
4008 if (data & ~PRED_CMD_IBPB)
4009 return 1;
4010
4011 if (!data)
4012 break;
4013
4014 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4015
4016 /*
4017 * For non-nested:
4018 * When it's written (to non-zero) for the first time, pass
4019 * it through.
4020 *
4021 * For nested:
4022 * The handling of the MSR bitmap for L2 guests is done in
4023 * nested_vmx_merge_msr_bitmap. We should not touch the
4024 * vmcs02.msr_bitmap here since it gets completely overwritten
4025 * in the merging.
4026 */
4027 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4028 MSR_TYPE_W);
4029 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004030 case MSR_IA32_ARCH_CAPABILITIES:
4031 if (!msr_info->host_initiated)
4032 return 1;
4033 vmx->arch_capabilities = data;
4034 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004035 case MSR_IA32_CR_PAT:
4036 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004037 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4038 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004039 vmcs_write64(GUEST_IA32_PAT, data);
4040 vcpu->arch.pat = data;
4041 break;
4042 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004043 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004044 break;
Will Auldba904632012-11-29 12:42:50 -08004045 case MSR_IA32_TSC_ADJUST:
4046 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004047 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004048 case MSR_IA32_MCG_EXT_CTL:
4049 if ((!msr_info->host_initiated &&
4050 !(to_vmx(vcpu)->msr_ia32_feature_control &
4051 FEATURE_CONTROL_LMCE)) ||
4052 (data & ~MCG_EXT_CTL_LMCE_EN))
4053 return 1;
4054 vcpu->arch.mcg_ext_ctl = data;
4055 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004056 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004057 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004058 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004059 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4060 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004061 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004062 if (msr_info->host_initiated && data == 0)
4063 vmx_leave_nested(vcpu);
4064 break;
4065 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004066 if (!msr_info->host_initiated)
4067 return 1; /* they are read-only */
4068 if (!nested_vmx_allowed(vcpu))
4069 return 1;
4070 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004071 case MSR_IA32_XSS:
4072 if (!vmx_xsaves_supported())
4073 return 1;
4074 /*
4075 * The only supported bit as of Skylake is bit 8, but
4076 * it is not supported on KVM.
4077 */
4078 if (data != 0)
4079 return 1;
4080 vcpu->arch.ia32_xss = data;
4081 if (vcpu->arch.ia32_xss != host_xss)
4082 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
4083 vcpu->arch.ia32_xss, host_xss);
4084 else
4085 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4086 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004087 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004088 if (!msr_info->host_initiated &&
4089 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004090 return 1;
4091 /* Check reserved bit, higher 32 bits should be zero */
4092 if ((data >> 32) != 0)
4093 return 1;
4094 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004096 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004097 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004098 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004099 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004100 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4101 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004102 ret = kvm_set_shared_msr(msr->index, msr->data,
4103 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004104 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004105 if (ret)
4106 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004107 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004108 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004109 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004110 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 }
4112
Eddie Dong2cc51562007-05-21 07:28:09 +03004113 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114}
4115
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004116static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004118 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4119 switch (reg) {
4120 case VCPU_REGS_RSP:
4121 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4122 break;
4123 case VCPU_REGS_RIP:
4124 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4125 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004126 case VCPU_EXREG_PDPTR:
4127 if (enable_ept)
4128 ept_save_pdptrs(vcpu);
4129 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004130 default:
4131 break;
4132 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133}
4134
Avi Kivity6aa8b732006-12-10 02:21:36 -08004135static __init int cpu_has_kvm_support(void)
4136{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004137 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138}
4139
4140static __init int vmx_disabled_by_bios(void)
4141{
4142 u64 msr;
4143
4144 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004145 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004146 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004147 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4148 && tboot_enabled())
4149 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004150 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004151 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004152 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004153 && !tboot_enabled()) {
4154 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004155 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004156 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004157 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004158 /* launched w/o TXT and VMX disabled */
4159 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4160 && !tboot_enabled())
4161 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004162 }
4163
4164 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165}
4166
Dongxiao Xu7725b892010-05-11 18:29:38 +08004167static void kvm_cpu_vmxon(u64 addr)
4168{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004169 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004170 intel_pt_handle_vmx(1);
4171
Dongxiao Xu7725b892010-05-11 18:29:38 +08004172 asm volatile (ASM_VMX_VMXON_RAX
4173 : : "a"(&addr), "m"(addr)
4174 : "memory", "cc");
4175}
4176
Radim Krčmář13a34e02014-08-28 15:13:03 +02004177static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178{
4179 int cpu = raw_smp_processor_id();
4180 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004181 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004183 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004184 return -EBUSY;
4185
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004186 /*
4187 * This can happen if we hot-added a CPU but failed to allocate
4188 * VP assist page for it.
4189 */
4190 if (static_branch_unlikely(&enable_evmcs) &&
4191 !hv_get_vp_assist_page(cpu))
4192 return -EFAULT;
4193
Nadav Har'Eld462b812011-05-24 15:26:10 +03004194 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004195 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4196 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004197
4198 /*
4199 * Now we can enable the vmclear operation in kdump
4200 * since the loaded_vmcss_on_cpu list on this cpu
4201 * has been initialized.
4202 *
4203 * Though the cpu is not in VMX operation now, there
4204 * is no problem to enable the vmclear operation
4205 * for the loaded_vmcss_on_cpu list is empty!
4206 */
4207 crash_enable_local_vmclear(cpu);
4208
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004210
4211 test_bits = FEATURE_CONTROL_LOCKED;
4212 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4213 if (tboot_enabled())
4214 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4215
4216 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004218 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4219 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004220 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004221 if (enable_ept)
4222 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004223
4224 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004225}
4226
Nadav Har'Eld462b812011-05-24 15:26:10 +03004227static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004228{
4229 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004230 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004231
Nadav Har'Eld462b812011-05-24 15:26:10 +03004232 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4233 loaded_vmcss_on_cpu_link)
4234 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004235}
4236
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004237
4238/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4239 * tricks.
4240 */
4241static void kvm_cpu_vmxoff(void)
4242{
4243 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004244
4245 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004246 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004247}
4248
Radim Krčmář13a34e02014-08-28 15:13:03 +02004249static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004250{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004251 vmclear_local_loaded_vmcss();
4252 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004253}
4254
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004255static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004256 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004257{
4258 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004259 u32 ctl = ctl_min | ctl_opt;
4260
4261 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4262
4263 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4264 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4265
4266 /* Ensure minimum (required) set of control bits are supported. */
4267 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004268 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004269
4270 *result = ctl;
4271 return 0;
4272}
4273
Avi Kivity110312c2010-12-21 12:54:20 +02004274static __init bool allow_1_setting(u32 msr, u32 ctl)
4275{
4276 u32 vmx_msr_low, vmx_msr_high;
4277
4278 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4279 return vmx_msr_high & ctl;
4280}
4281
Yang, Sheng002c7f72007-07-31 14:23:01 +03004282static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004283{
4284 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004285 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004286 u32 _pin_based_exec_control = 0;
4287 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004288 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004289 u32 _vmexit_control = 0;
4290 u32 _vmentry_control = 0;
4291
Paolo Bonzini13893092018-02-26 13:40:09 +01004292 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304293 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004294#ifdef CONFIG_X86_64
4295 CPU_BASED_CR8_LOAD_EXITING |
4296 CPU_BASED_CR8_STORE_EXITING |
4297#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004298 CPU_BASED_CR3_LOAD_EXITING |
4299 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004300 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004301 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004302 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004303 CPU_BASED_MWAIT_EXITING |
4304 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004305 CPU_BASED_INVLPG_EXITING |
4306 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004307
Sheng Yangf78e0e22007-10-29 09:40:42 +08004308 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004309 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004310 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004311 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4312 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004313 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004314#ifdef CONFIG_X86_64
4315 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4316 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4317 ~CPU_BASED_CR8_STORE_EXITING;
4318#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004319 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004320 min2 = 0;
4321 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004322 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004323 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004324 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004325 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004326 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004327 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004328 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004329 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004330 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004331 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004332 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004333 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004334 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004335 SECONDARY_EXEC_RDSEED_EXITING |
4336 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004337 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004338 SECONDARY_EXEC_TSC_SCALING |
4339 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004340 if (adjust_vmx_controls(min2, opt2,
4341 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004342 &_cpu_based_2nd_exec_control) < 0)
4343 return -EIO;
4344 }
4345#ifndef CONFIG_X86_64
4346 if (!(_cpu_based_2nd_exec_control &
4347 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4348 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4349#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004350
4351 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4352 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004353 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004354 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4355 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004356
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004357 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4358 &vmx_capability.ept, &vmx_capability.vpid);
4359
Sheng Yangd56f5462008-04-25 10:13:16 +08004360 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004361 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4362 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004363 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4364 CPU_BASED_CR3_STORE_EXITING |
4365 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004366 } else if (vmx_capability.ept) {
4367 vmx_capability.ept = 0;
4368 pr_warn_once("EPT CAP should not exist if not support "
4369 "1-setting enable EPT VM-execution control\n");
4370 }
4371 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4372 vmx_capability.vpid) {
4373 vmx_capability.vpid = 0;
4374 pr_warn_once("VPID CAP should not exist if not support "
4375 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004376 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004377
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004378 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004379#ifdef CONFIG_X86_64
4380 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4381#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004382 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004383 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004384 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4385 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004386 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004387
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004388 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4389 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4390 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004391 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4392 &_pin_based_exec_control) < 0)
4393 return -EIO;
4394
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004395 if (cpu_has_broken_vmx_preemption_timer())
4396 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004397 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004398 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004399 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4400
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004401 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004402 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004403 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4404 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004405 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004407 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004408
4409 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4410 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004411 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004412
4413#ifdef CONFIG_X86_64
4414 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4415 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004416 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004417#endif
4418
4419 /* Require Write-Back (WB) memory type for VMCS accesses. */
4420 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004421 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004422
Yang, Sheng002c7f72007-07-31 14:23:01 +03004423 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004424 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004425 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004426
Liran Alon2307af12018-06-29 22:59:04 +03004427 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004428
Yang, Sheng002c7f72007-07-31 14:23:01 +03004429 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4430 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004431 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004432 vmcs_conf->vmexit_ctrl = _vmexit_control;
4433 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004434
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004435 if (static_branch_unlikely(&enable_evmcs))
4436 evmcs_sanitize_exec_ctrls(vmcs_conf);
4437
Avi Kivity110312c2010-12-21 12:54:20 +02004438 cpu_has_load_ia32_efer =
4439 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4440 VM_ENTRY_LOAD_IA32_EFER)
4441 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4442 VM_EXIT_LOAD_IA32_EFER);
4443
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004444 cpu_has_load_perf_global_ctrl =
4445 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4446 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4447 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4448 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4449
4450 /*
4451 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004452 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004453 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4454 *
4455 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4456 *
4457 * AAK155 (model 26)
4458 * AAP115 (model 30)
4459 * AAT100 (model 37)
4460 * BC86,AAY89,BD102 (model 44)
4461 * BA97 (model 46)
4462 *
4463 */
4464 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4465 switch (boot_cpu_data.x86_model) {
4466 case 26:
4467 case 30:
4468 case 37:
4469 case 44:
4470 case 46:
4471 cpu_has_load_perf_global_ctrl = false;
4472 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4473 "does not work properly. Using workaround\n");
4474 break;
4475 default:
4476 break;
4477 }
4478 }
4479
Borislav Petkov782511b2016-04-04 22:25:03 +02004480 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004481 rdmsrl(MSR_IA32_XSS, host_xss);
4482
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004483 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004484}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004485
Liran Alon491a6032018-06-23 02:35:12 +03004486static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487{
4488 int node = cpu_to_node(cpu);
4489 struct page *pages;
4490 struct vmcs *vmcs;
4491
Vlastimil Babka96db8002015-09-08 15:03:50 -07004492 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493 if (!pages)
4494 return NULL;
4495 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004496 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004497
4498 /* KVM supports Enlightened VMCS v1 only */
4499 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004500 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004501 else
Liran Alon392b2f22018-06-23 02:35:01 +03004502 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004503
Liran Alon491a6032018-06-23 02:35:12 +03004504 if (shadow)
4505 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004506 return vmcs;
4507}
4508
Avi Kivity6aa8b732006-12-10 02:21:36 -08004509static void free_vmcs(struct vmcs *vmcs)
4510{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004511 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512}
4513
Nadav Har'Eld462b812011-05-24 15:26:10 +03004514/*
4515 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4516 */
4517static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4518{
4519 if (!loaded_vmcs->vmcs)
4520 return;
4521 loaded_vmcs_clear(loaded_vmcs);
4522 free_vmcs(loaded_vmcs->vmcs);
4523 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004524 if (loaded_vmcs->msr_bitmap)
4525 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004526 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004527}
4528
Liran Alon491a6032018-06-23 02:35:12 +03004529static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004530{
Liran Alon491a6032018-06-23 02:35:12 +03004531 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004532}
4533
4534static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4535{
Liran Alon491a6032018-06-23 02:35:12 +03004536 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004537 if (!loaded_vmcs->vmcs)
4538 return -ENOMEM;
4539
4540 loaded_vmcs->shadow_vmcs = NULL;
4541 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004542
4543 if (cpu_has_vmx_msr_bitmap()) {
4544 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4545 if (!loaded_vmcs->msr_bitmap)
4546 goto out_vmcs;
4547 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004548
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004549 if (IS_ENABLED(CONFIG_HYPERV) &&
4550 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004551 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4552 struct hv_enlightened_vmcs *evmcs =
4553 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4554
4555 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4556 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004557 }
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004558 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004559
4560out_vmcs:
4561 free_loaded_vmcs(loaded_vmcs);
4562 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004563}
4564
Sam Ravnborg39959582007-06-01 00:47:13 -07004565static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566{
4567 int cpu;
4568
Zachary Amsden3230bb42009-09-29 11:38:37 -10004569 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004570 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004571 per_cpu(vmxarea, cpu) = NULL;
4572 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573}
4574
Jim Mattsond37f4262017-12-22 12:12:16 -08004575enum vmcs_field_width {
4576 VMCS_FIELD_WIDTH_U16 = 0,
4577 VMCS_FIELD_WIDTH_U64 = 1,
4578 VMCS_FIELD_WIDTH_U32 = 2,
4579 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004580};
4581
Jim Mattsond37f4262017-12-22 12:12:16 -08004582static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004583{
4584 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004585 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004586 return (field >> 13) & 0x3 ;
4587}
4588
4589static inline int vmcs_field_readonly(unsigned long field)
4590{
4591 return (((field >> 10) & 0x3) == 1);
4592}
4593
Bandan Dasfe2b2012014-04-21 15:20:14 -04004594static void init_vmcs_shadow_fields(void)
4595{
4596 int i, j;
4597
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004598 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4599 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004600 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004601 (i + 1 == max_shadow_read_only_fields ||
4602 shadow_read_only_fields[i + 1] != field + 1))
4603 pr_err("Missing field from shadow_read_only_field %x\n",
4604 field + 1);
4605
4606 clear_bit(field, vmx_vmread_bitmap);
4607#ifdef CONFIG_X86_64
4608 if (field & 1)
4609 continue;
4610#endif
4611 if (j < i)
4612 shadow_read_only_fields[j] = field;
4613 j++;
4614 }
4615 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004616
4617 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004618 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004619 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004620 (i + 1 == max_shadow_read_write_fields ||
4621 shadow_read_write_fields[i + 1] != field + 1))
4622 pr_err("Missing field from shadow_read_write_field %x\n",
4623 field + 1);
4624
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004625 /*
4626 * PML and the preemption timer can be emulated, but the
4627 * processor cannot vmwrite to fields that don't exist
4628 * on bare metal.
4629 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004630 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004631 case GUEST_PML_INDEX:
4632 if (!cpu_has_vmx_pml())
4633 continue;
4634 break;
4635 case VMX_PREEMPTION_TIMER_VALUE:
4636 if (!cpu_has_vmx_preemption_timer())
4637 continue;
4638 break;
4639 case GUEST_INTR_STATUS:
4640 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004641 continue;
4642 break;
4643 default:
4644 break;
4645 }
4646
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004647 clear_bit(field, vmx_vmwrite_bitmap);
4648 clear_bit(field, vmx_vmread_bitmap);
4649#ifdef CONFIG_X86_64
4650 if (field & 1)
4651 continue;
4652#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004653 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004654 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004655 j++;
4656 }
4657 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004658}
4659
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660static __init int alloc_kvm_area(void)
4661{
4662 int cpu;
4663
Zachary Amsden3230bb42009-09-29 11:38:37 -10004664 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004665 struct vmcs *vmcs;
4666
Liran Alon491a6032018-06-23 02:35:12 +03004667 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004668 if (!vmcs) {
4669 free_kvm_area();
4670 return -ENOMEM;
4671 }
4672
Liran Alon2307af12018-06-29 22:59:04 +03004673 /*
4674 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4675 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4676 * revision_id reported by MSR_IA32_VMX_BASIC.
4677 *
4678 * However, even though not explictly documented by
4679 * TLFS, VMXArea passed as VMXON argument should
4680 * still be marked with revision_id reported by
4681 * physical CPU.
4682 */
4683 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004684 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004685
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686 per_cpu(vmxarea, cpu) = vmcs;
4687 }
4688 return 0;
4689}
4690
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004691static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004692 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004693{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004694 if (!emulate_invalid_guest_state) {
4695 /*
4696 * CS and SS RPL should be equal during guest entry according
4697 * to VMX spec, but in reality it is not always so. Since vcpu
4698 * is in the middle of the transition from real mode to
4699 * protected mode it is safe to assume that RPL 0 is a good
4700 * default value.
4701 */
4702 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004703 save->selector &= ~SEGMENT_RPL_MASK;
4704 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004705 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004707 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708}
4709
4710static void enter_pmode(struct kvm_vcpu *vcpu)
4711{
4712 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004713 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004714
Gleb Natapovd99e4152012-12-20 16:57:45 +02004715 /*
4716 * Update real mode segment cache. It may be not up-to-date if sement
4717 * register was written while vcpu was in a guest mode.
4718 */
4719 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4720 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4722 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4723 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4724 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4725
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004726 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727
Avi Kivity2fb92db2011-04-27 19:42:18 +03004728 vmx_segment_cache_clear(vmx);
4729
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004730 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731
4732 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004733 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4734 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735 vmcs_writel(GUEST_RFLAGS, flags);
4736
Rusty Russell66aee912007-07-17 23:34:16 +10004737 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4738 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739
4740 update_exception_bitmap(vcpu);
4741
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004742 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4743 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4744 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4745 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4746 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4747 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004748}
4749
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004750static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751{
Mathias Krause772e0312012-08-30 01:30:19 +02004752 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004753 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754
Gleb Natapovd99e4152012-12-20 16:57:45 +02004755 var.dpl = 0x3;
4756 if (seg == VCPU_SREG_CS)
4757 var.type = 0x3;
4758
4759 if (!emulate_invalid_guest_state) {
4760 var.selector = var.base >> 4;
4761 var.base = var.base & 0xffff0;
4762 var.limit = 0xffff;
4763 var.g = 0;
4764 var.db = 0;
4765 var.present = 1;
4766 var.s = 1;
4767 var.l = 0;
4768 var.unusable = 0;
4769 var.type = 0x3;
4770 var.avl = 0;
4771 if (save->base & 0xf)
4772 printk_once(KERN_WARNING "kvm: segment base is not "
4773 "paragraph aligned when entering "
4774 "protected mode (seg=%d)", seg);
4775 }
4776
4777 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004778 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004779 vmcs_write32(sf->limit, var.limit);
4780 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781}
4782
4783static void enter_rmode(struct kvm_vcpu *vcpu)
4784{
4785 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004786 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004787 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004789 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
4790 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4791 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4792 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4793 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004794 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4795 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004796
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004797 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798
Gleb Natapov776e58e2011-03-13 12:34:27 +02004799 /*
4800 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004801 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02004802 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004803 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02004804 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
4805 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02004806
Avi Kivity2fb92db2011-04-27 19:42:18 +03004807 vmx_segment_cache_clear(vmx);
4808
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004809 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004810 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004811 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4812
4813 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004814 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01004816 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817
4818 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10004819 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820 update_exception_bitmap(vcpu);
4821
Gleb Natapovd99e4152012-12-20 16:57:45 +02004822 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4823 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4824 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4825 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4826 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
4827 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004828
Eddie Dong8668a3c2007-10-10 14:26:45 +08004829 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004830}
4831
Amit Shah401d10d2009-02-20 22:53:37 +05304832static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
4833{
4834 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004835 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
4836
4837 if (!msr)
4838 return;
Amit Shah401d10d2009-02-20 22:53:37 +05304839
Avi Kivity44ea2b12009-09-06 15:55:37 +03004840 /*
4841 * Force kernel_gs_base reloading before EFER changes, as control
4842 * of this msr depends on is_long_mode().
4843 */
4844 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02004845 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05304846 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004847 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304848 msr->data = efer;
4849 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02004850 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05304851
4852 msr->data = efer & ~EFER_LME;
4853 }
4854 setup_msrs(vmx);
4855}
4856
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004857#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004858
4859static void enter_lmode(struct kvm_vcpu *vcpu)
4860{
4861 u32 guest_tr_ar;
4862
Avi Kivity2fb92db2011-04-27 19:42:18 +03004863 vmx_segment_cache_clear(to_vmx(vcpu));
4864
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004866 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004867 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4868 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004870 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4871 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872 }
Avi Kivityda38f432010-07-06 11:30:49 +03004873 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874}
4875
4876static void exit_lmode(struct kvm_vcpu *vcpu)
4877{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004878 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004879 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880}
4881
4882#endif
4883
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004884static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
4885 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004886{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004887 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004888 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4889 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07004890 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004891 } else {
4892 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004893 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004894}
4895
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004896static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004897{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08004898 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004899}
4900
Junaid Shahidfaff8752018-06-29 13:10:05 -07004901static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
4902{
4903 int vpid = to_vmx(vcpu)->vpid;
4904
4905 if (!vpid_sync_vcpu_addr(vpid, addr))
4906 vpid_sync_context(vpid);
4907
4908 /*
4909 * If VPIDs are not supported or enabled, then the above is a no-op.
4910 * But we don't really need a TLB flush in that case anyway, because
4911 * each VM entry/exit includes an implicit flush when VPID is 0.
4912 */
4913}
4914
Avi Kivitye8467fd2009-12-29 18:43:06 +02004915static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4916{
4917 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4918
4919 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4920 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4921}
4922
Avi Kivityaff48ba2010-12-05 18:56:11 +02004923static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4924{
Sean Christophersonb4d18512018-03-05 12:04:40 -08004925 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02004926 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4927 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4928}
4929
Anthony Liguori25c4c272007-04-27 09:29:21 +03004930static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004931{
Avi Kivityfc78f512009-12-07 12:16:48 +02004932 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4933
4934 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4935 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004936}
4937
Sheng Yang14394422008-04-28 12:24:45 +08004938static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4939{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004940 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4941
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004942 if (!test_bit(VCPU_EXREG_PDPTR,
4943 (unsigned long *)&vcpu->arch.regs_dirty))
4944 return;
4945
Sheng Yang14394422008-04-28 12:24:45 +08004946 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004947 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4948 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4949 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4950 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004951 }
4952}
4953
Avi Kivity8f5d5492009-05-31 18:41:29 +03004954static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4955{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004956 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4957
Avi Kivity8f5d5492009-05-31 18:41:29 +03004958 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004959 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4960 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4961 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4962 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004963 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004964
4965 __set_bit(VCPU_EXREG_PDPTR,
4966 (unsigned long *)&vcpu->arch.regs_avail);
4967 __set_bit(VCPU_EXREG_PDPTR,
4968 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004969}
4970
David Matlack38991522016-11-29 18:14:08 -08004971static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4972{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004973 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4974 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004975 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4976
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004977 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08004978 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4979 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4980 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4981
4982 return fixed_bits_valid(val, fixed0, fixed1);
4983}
4984
4985static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4986{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004987 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
4988 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004989
4990 return fixed_bits_valid(val, fixed0, fixed1);
4991}
4992
4993static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4994{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004995 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
4996 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08004997
4998 return fixed_bits_valid(val, fixed0, fixed1);
4999}
5000
5001/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5002#define nested_guest_cr4_valid nested_cr4_valid
5003#define nested_host_cr4_valid nested_cr4_valid
5004
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005005static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005006
5007static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5008 unsigned long cr0,
5009 struct kvm_vcpu *vcpu)
5010{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005011 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5012 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005013 if (!(cr0 & X86_CR0_PG)) {
5014 /* From paging/starting to nonpaging */
5015 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005016 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005017 (CPU_BASED_CR3_LOAD_EXITING |
5018 CPU_BASED_CR3_STORE_EXITING));
5019 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005020 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005021 } else if (!is_paging(vcpu)) {
5022 /* From nonpaging to paging */
5023 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005024 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005025 ~(CPU_BASED_CR3_LOAD_EXITING |
5026 CPU_BASED_CR3_STORE_EXITING));
5027 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005028 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005029 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005030
5031 if (!(cr0 & X86_CR0_WP))
5032 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005033}
5034
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5036{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005037 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005038 unsigned long hw_cr0;
5039
Gleb Natapov50378782013-02-04 16:00:28 +02005040 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005041 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005042 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005043 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005044 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005045
Gleb Natapov218e7632013-01-21 15:36:45 +02005046 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5047 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048
Gleb Natapov218e7632013-01-21 15:36:45 +02005049 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5050 enter_rmode(vcpu);
5051 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005052
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005053#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005054 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005055 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005056 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005057 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005058 exit_lmode(vcpu);
5059 }
5060#endif
5061
Sean Christophersonb4d18512018-03-05 12:04:40 -08005062 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005063 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5064
Avi Kivity6aa8b732006-12-10 02:21:36 -08005065 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005066 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005067 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005068
5069 /* depends on vcpu->arch.cr0 to be set to a new value */
5070 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005071}
5072
Yu Zhang855feb62017-08-24 20:27:55 +08005073static int get_ept_level(struct kvm_vcpu *vcpu)
5074{
5075 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5076 return 5;
5077 return 4;
5078}
5079
Peter Feiner995f00a2017-06-30 17:26:32 -07005080static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005081{
Yu Zhang855feb62017-08-24 20:27:55 +08005082 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005083
Yu Zhang855feb62017-08-24 20:27:55 +08005084 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005085
Peter Feiner995f00a2017-06-30 17:26:32 -07005086 if (enable_ept_ad_bits &&
5087 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005088 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005089 eptp |= (root_hpa & PAGE_MASK);
5090
5091 return eptp;
5092}
5093
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5095{
Tianyu Lan877ad952018-07-19 08:40:23 +00005096 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005097 unsigned long guest_cr3;
5098 u64 eptp;
5099
5100 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005101 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005102 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005103 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005104
5105 if (kvm_x86_ops->tlb_remote_flush) {
5106 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5107 to_vmx(vcpu)->ept_pointer = eptp;
5108 to_kvm_vmx(kvm)->ept_pointers_match
5109 = EPT_POINTERS_CHECK;
5110 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5111 }
5112
Sean Christophersone90008d2018-03-05 12:04:37 -08005113 if (enable_unrestricted_guest || is_paging(vcpu) ||
5114 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005115 guest_cr3 = kvm_read_cr3(vcpu);
5116 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005117 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005118 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005119 }
5120
Sheng Yang14394422008-04-28 12:24:45 +08005121 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122}
5123
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005124static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005125{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005126 /*
5127 * Pass through host's Machine Check Enable value to hw_cr4, which
5128 * is in force while we are in guest mode. Do not let guests control
5129 * this bit, even if host CR4.MCE == 0.
5130 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005131 unsigned long hw_cr4;
5132
5133 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5134 if (enable_unrestricted_guest)
5135 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5136 else if (to_vmx(vcpu)->rmode.vm86_active)
5137 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5138 else
5139 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005140
Sean Christopherson64f7a112018-04-30 10:01:06 -07005141 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5142 if (cr4 & X86_CR4_UMIP) {
5143 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005144 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005145 hw_cr4 &= ~X86_CR4_UMIP;
5146 } else if (!is_guest_mode(vcpu) ||
5147 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5148 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5149 SECONDARY_EXEC_DESC);
5150 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005151
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005152 if (cr4 & X86_CR4_VMXE) {
5153 /*
5154 * To use VMXON (and later other VMX instructions), a guest
5155 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5156 * So basically the check on whether to allow nested VMX
5157 * is here.
5158 */
5159 if (!nested_vmx_allowed(vcpu))
5160 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005161 }
David Matlack38991522016-11-29 18:14:08 -08005162
5163 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005164 return 1;
5165
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005166 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005167
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005168 if (!enable_unrestricted_guest) {
5169 if (enable_ept) {
5170 if (!is_paging(vcpu)) {
5171 hw_cr4 &= ~X86_CR4_PAE;
5172 hw_cr4 |= X86_CR4_PSE;
5173 } else if (!(cr4 & X86_CR4_PAE)) {
5174 hw_cr4 &= ~X86_CR4_PAE;
5175 }
5176 }
5177
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005178 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005179 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5180 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5181 * to be manually disabled when guest switches to non-paging
5182 * mode.
5183 *
5184 * If !enable_unrestricted_guest, the CPU is always running
5185 * with CR0.PG=1 and CR4 needs to be modified.
5186 * If enable_unrestricted_guest, the CPU automatically
5187 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005188 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005189 if (!is_paging(vcpu))
5190 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5191 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005192
Sheng Yang14394422008-04-28 12:24:45 +08005193 vmcs_writel(CR4_READ_SHADOW, cr4);
5194 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005195 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005196}
5197
Avi Kivity6aa8b732006-12-10 02:21:36 -08005198static void vmx_get_segment(struct kvm_vcpu *vcpu,
5199 struct kvm_segment *var, int seg)
5200{
Avi Kivitya9179492011-01-03 14:28:52 +02005201 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005202 u32 ar;
5203
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005204 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005205 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005206 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005207 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005208 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005209 var->base = vmx_read_guest_seg_base(vmx, seg);
5210 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5211 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005212 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005213 var->base = vmx_read_guest_seg_base(vmx, seg);
5214 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5215 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5216 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005217 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005218 var->type = ar & 15;
5219 var->s = (ar >> 4) & 1;
5220 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005221 /*
5222 * Some userspaces do not preserve unusable property. Since usable
5223 * segment has to be present according to VMX spec we can use present
5224 * property to amend userspace bug by making unusable segment always
5225 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5226 * segment as unusable.
5227 */
5228 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005229 var->avl = (ar >> 12) & 1;
5230 var->l = (ar >> 13) & 1;
5231 var->db = (ar >> 14) & 1;
5232 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005233}
5234
Avi Kivitya9179492011-01-03 14:28:52 +02005235static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5236{
Avi Kivitya9179492011-01-03 14:28:52 +02005237 struct kvm_segment s;
5238
5239 if (to_vmx(vcpu)->rmode.vm86_active) {
5240 vmx_get_segment(vcpu, &s, seg);
5241 return s.base;
5242 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005243 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005244}
5245
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005246static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005247{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005248 struct vcpu_vmx *vmx = to_vmx(vcpu);
5249
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005250 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005251 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005252 else {
5253 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005254 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005255 }
Avi Kivity69c73022011-03-07 15:26:44 +02005256}
5257
Avi Kivity653e3102007-05-07 10:55:37 +03005258static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005259{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260 u32 ar;
5261
Avi Kivityf0495f92012-06-07 17:06:10 +03005262 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263 ar = 1 << 16;
5264 else {
5265 ar = var->type & 15;
5266 ar |= (var->s & 1) << 4;
5267 ar |= (var->dpl & 3) << 5;
5268 ar |= (var->present & 1) << 7;
5269 ar |= (var->avl & 1) << 12;
5270 ar |= (var->l & 1) << 13;
5271 ar |= (var->db & 1) << 14;
5272 ar |= (var->g & 1) << 15;
5273 }
Avi Kivity653e3102007-05-07 10:55:37 +03005274
5275 return ar;
5276}
5277
5278static void vmx_set_segment(struct kvm_vcpu *vcpu,
5279 struct kvm_segment *var, int seg)
5280{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005281 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005282 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005283
Avi Kivity2fb92db2011-04-27 19:42:18 +03005284 vmx_segment_cache_clear(vmx);
5285
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005286 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5287 vmx->rmode.segs[seg] = *var;
5288 if (seg == VCPU_SREG_TR)
5289 vmcs_write16(sf->selector, var->selector);
5290 else if (var->s)
5291 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005292 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005293 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005294
Avi Kivity653e3102007-05-07 10:55:37 +03005295 vmcs_writel(sf->base, var->base);
5296 vmcs_write32(sf->limit, var->limit);
5297 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005298
5299 /*
5300 * Fix the "Accessed" bit in AR field of segment registers for older
5301 * qemu binaries.
5302 * IA32 arch specifies that at the time of processor reset the
5303 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005304 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005305 * state vmexit when "unrestricted guest" mode is turned on.
5306 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5307 * tree. Newer qemu binaries with that qemu fix would not need this
5308 * kvm hack.
5309 */
5310 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005311 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005312
Gleb Natapovf924d662012-12-12 19:10:55 +02005313 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005314
5315out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005316 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005317}
5318
Avi Kivity6aa8b732006-12-10 02:21:36 -08005319static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5320{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005321 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005322
5323 *db = (ar >> 14) & 1;
5324 *l = (ar >> 13) & 1;
5325}
5326
Gleb Natapov89a27f42010-02-16 10:51:48 +02005327static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005328{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005329 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5330 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005331}
5332
Gleb Natapov89a27f42010-02-16 10:51:48 +02005333static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005334{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005335 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5336 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005337}
5338
Gleb Natapov89a27f42010-02-16 10:51:48 +02005339static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005340{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005341 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5342 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005343}
5344
Gleb Natapov89a27f42010-02-16 10:51:48 +02005345static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005346{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005347 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5348 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005349}
5350
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005351static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5352{
5353 struct kvm_segment var;
5354 u32 ar;
5355
5356 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005357 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005358 if (seg == VCPU_SREG_CS)
5359 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005360 ar = vmx_segment_access_rights(&var);
5361
5362 if (var.base != (var.selector << 4))
5363 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005364 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005365 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005366 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005367 return false;
5368
5369 return true;
5370}
5371
5372static bool code_segment_valid(struct kvm_vcpu *vcpu)
5373{
5374 struct kvm_segment cs;
5375 unsigned int cs_rpl;
5376
5377 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005378 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005379
Avi Kivity1872a3f2009-01-04 23:26:52 +02005380 if (cs.unusable)
5381 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005382 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005383 return false;
5384 if (!cs.s)
5385 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005386 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005387 if (cs.dpl > cs_rpl)
5388 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005389 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005390 if (cs.dpl != cs_rpl)
5391 return false;
5392 }
5393 if (!cs.present)
5394 return false;
5395
5396 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5397 return true;
5398}
5399
5400static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5401{
5402 struct kvm_segment ss;
5403 unsigned int ss_rpl;
5404
5405 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005406 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005407
Avi Kivity1872a3f2009-01-04 23:26:52 +02005408 if (ss.unusable)
5409 return true;
5410 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005411 return false;
5412 if (!ss.s)
5413 return false;
5414 if (ss.dpl != ss_rpl) /* DPL != RPL */
5415 return false;
5416 if (!ss.present)
5417 return false;
5418
5419 return true;
5420}
5421
5422static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5423{
5424 struct kvm_segment var;
5425 unsigned int rpl;
5426
5427 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005428 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005429
Avi Kivity1872a3f2009-01-04 23:26:52 +02005430 if (var.unusable)
5431 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005432 if (!var.s)
5433 return false;
5434 if (!var.present)
5435 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005436 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005437 if (var.dpl < rpl) /* DPL < RPL */
5438 return false;
5439 }
5440
5441 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5442 * rights flags
5443 */
5444 return true;
5445}
5446
5447static bool tr_valid(struct kvm_vcpu *vcpu)
5448{
5449 struct kvm_segment tr;
5450
5451 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5452
Avi Kivity1872a3f2009-01-04 23:26:52 +02005453 if (tr.unusable)
5454 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005455 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005456 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005457 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005458 return false;
5459 if (!tr.present)
5460 return false;
5461
5462 return true;
5463}
5464
5465static bool ldtr_valid(struct kvm_vcpu *vcpu)
5466{
5467 struct kvm_segment ldtr;
5468
5469 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5470
Avi Kivity1872a3f2009-01-04 23:26:52 +02005471 if (ldtr.unusable)
5472 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005473 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005474 return false;
5475 if (ldtr.type != 2)
5476 return false;
5477 if (!ldtr.present)
5478 return false;
5479
5480 return true;
5481}
5482
5483static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5484{
5485 struct kvm_segment cs, ss;
5486
5487 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5488 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5489
Nadav Amitb32a9912015-03-29 16:33:04 +03005490 return ((cs.selector & SEGMENT_RPL_MASK) ==
5491 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005492}
5493
5494/*
5495 * Check if guest state is valid. Returns true if valid, false if
5496 * not.
5497 * We assume that registers are always usable
5498 */
5499static bool guest_state_valid(struct kvm_vcpu *vcpu)
5500{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005501 if (enable_unrestricted_guest)
5502 return true;
5503
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005504 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005505 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005506 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5507 return false;
5508 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5509 return false;
5510 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5511 return false;
5512 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5513 return false;
5514 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5515 return false;
5516 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5517 return false;
5518 } else {
5519 /* protected mode guest state checks */
5520 if (!cs_ss_rpl_check(vcpu))
5521 return false;
5522 if (!code_segment_valid(vcpu))
5523 return false;
5524 if (!stack_segment_valid(vcpu))
5525 return false;
5526 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5527 return false;
5528 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5529 return false;
5530 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5531 return false;
5532 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5533 return false;
5534 if (!tr_valid(vcpu))
5535 return false;
5536 if (!ldtr_valid(vcpu))
5537 return false;
5538 }
5539 /* TODO:
5540 * - Add checks on RIP
5541 * - Add checks on RFLAGS
5542 */
5543
5544 return true;
5545}
5546
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005547static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5548{
5549 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5550}
5551
Mike Dayd77c26f2007-10-08 09:02:08 -04005552static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005553{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005554 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005555 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005556 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005557
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005558 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005559 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005560 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5561 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005562 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005563 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005564 r = kvm_write_guest_page(kvm, fn++, &data,
5565 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005566 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005567 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005568 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5569 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005570 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005571 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5572 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005573 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005574 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005575 r = kvm_write_guest_page(kvm, fn, &data,
5576 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5577 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005578out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005579 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005580 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005581}
5582
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005583static int init_rmode_identity_map(struct kvm *kvm)
5584{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005585 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005586 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005587 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005588 u32 tmp;
5589
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005590 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005591 mutex_lock(&kvm->slots_lock);
5592
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005593 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005594 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005595
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005596 if (!kvm_vmx->ept_identity_map_addr)
5597 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5598 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005599
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005600 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005601 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005602 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005603 goto out2;
5604
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005605 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005606 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5607 if (r < 0)
5608 goto out;
5609 /* Set up identity-mapping pagetable for EPT in real mode */
5610 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5611 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5612 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5613 r = kvm_write_guest_page(kvm, identity_map_pfn,
5614 &tmp, i * sizeof(tmp), sizeof(tmp));
5615 if (r < 0)
5616 goto out;
5617 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005618 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005619
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005620out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005621 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005622
5623out2:
5624 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005625 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005626}
5627
Avi Kivity6aa8b732006-12-10 02:21:36 -08005628static void seg_setup(int seg)
5629{
Mathias Krause772e0312012-08-30 01:30:19 +02005630 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005631 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005632
5633 vmcs_write16(sf->selector, 0);
5634 vmcs_writel(sf->base, 0);
5635 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005636 ar = 0x93;
5637 if (seg == VCPU_SREG_CS)
5638 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005639
5640 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005641}
5642
Sheng Yangf78e0e22007-10-29 09:40:42 +08005643static int alloc_apic_access_page(struct kvm *kvm)
5644{
Xiao Guangrong44841412012-09-07 14:14:20 +08005645 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005646 int r = 0;
5647
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005648 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005649 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005650 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005651 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5652 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005653 if (r)
5654 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005655
Tang Chen73a6d942014-09-11 13:38:00 +08005656 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005657 if (is_error_page(page)) {
5658 r = -EFAULT;
5659 goto out;
5660 }
5661
Tang Chenc24ae0d2014-09-24 15:57:58 +08005662 /*
5663 * Do not pin the page in memory, so that memory hot-unplug
5664 * is able to migrate it.
5665 */
5666 put_page(page);
5667 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005668out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005669 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005670 return r;
5671}
5672
Wanpeng Li991e7a02015-09-16 17:30:05 +08005673static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005674{
5675 int vpid;
5676
Avi Kivity919818a2009-03-23 18:01:29 +02005677 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005678 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005679 spin_lock(&vmx_vpid_lock);
5680 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005681 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005682 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005683 else
5684 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005685 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005686 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005687}
5688
Wanpeng Li991e7a02015-09-16 17:30:05 +08005689static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005690{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005691 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005692 return;
5693 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005694 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005695 spin_unlock(&vmx_vpid_lock);
5696}
5697
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005698static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5699 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005700{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005701 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005702
5703 if (!cpu_has_vmx_msr_bitmap())
5704 return;
5705
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005706 if (static_branch_unlikely(&enable_evmcs))
5707 evmcs_touch_msr_bitmap();
5708
Sheng Yang25c5f222008-03-28 13:18:56 +08005709 /*
5710 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5711 * have the write-low and read-high bitmap offsets the wrong way round.
5712 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5713 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005714 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005715 if (type & MSR_TYPE_R)
5716 /* read-low */
5717 __clear_bit(msr, msr_bitmap + 0x000 / f);
5718
5719 if (type & MSR_TYPE_W)
5720 /* write-low */
5721 __clear_bit(msr, msr_bitmap + 0x800 / f);
5722
Sheng Yang25c5f222008-03-28 13:18:56 +08005723 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5724 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005725 if (type & MSR_TYPE_R)
5726 /* read-high */
5727 __clear_bit(msr, msr_bitmap + 0x400 / f);
5728
5729 if (type & MSR_TYPE_W)
5730 /* write-high */
5731 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5732
5733 }
5734}
5735
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005736static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5737 u32 msr, int type)
5738{
5739 int f = sizeof(unsigned long);
5740
5741 if (!cpu_has_vmx_msr_bitmap())
5742 return;
5743
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005744 if (static_branch_unlikely(&enable_evmcs))
5745 evmcs_touch_msr_bitmap();
5746
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005747 /*
5748 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5749 * have the write-low and read-high bitmap offsets the wrong way round.
5750 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5751 */
5752 if (msr <= 0x1fff) {
5753 if (type & MSR_TYPE_R)
5754 /* read-low */
5755 __set_bit(msr, msr_bitmap + 0x000 / f);
5756
5757 if (type & MSR_TYPE_W)
5758 /* write-low */
5759 __set_bit(msr, msr_bitmap + 0x800 / f);
5760
5761 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5762 msr &= 0x1fff;
5763 if (type & MSR_TYPE_R)
5764 /* read-high */
5765 __set_bit(msr, msr_bitmap + 0x400 / f);
5766
5767 if (type & MSR_TYPE_W)
5768 /* write-high */
5769 __set_bit(msr, msr_bitmap + 0xc00 / f);
5770
5771 }
5772}
5773
5774static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5775 u32 msr, int type, bool value)
5776{
5777 if (value)
5778 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5779 else
5780 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5781}
5782
Wincy Vanf2b93282015-02-03 23:56:03 +08005783/*
5784 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5785 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5786 */
5787static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5788 unsigned long *msr_bitmap_nested,
5789 u32 msr, int type)
5790{
5791 int f = sizeof(unsigned long);
5792
Wincy Vanf2b93282015-02-03 23:56:03 +08005793 /*
5794 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5795 * have the write-low and read-high bitmap offsets the wrong way round.
5796 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5797 */
5798 if (msr <= 0x1fff) {
5799 if (type & MSR_TYPE_R &&
5800 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
5801 /* read-low */
5802 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
5803
5804 if (type & MSR_TYPE_W &&
5805 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
5806 /* write-low */
5807 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
5808
5809 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5810 msr &= 0x1fff;
5811 if (type & MSR_TYPE_R &&
5812 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
5813 /* read-high */
5814 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
5815
5816 if (type & MSR_TYPE_W &&
5817 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
5818 /* write-high */
5819 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
5820
5821 }
5822}
5823
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005824static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02005825{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005826 u8 mode = 0;
5827
5828 if (cpu_has_secondary_exec_ctrls() &&
5829 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
5830 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
5831 mode |= MSR_BITMAP_MODE_X2APIC;
5832 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
5833 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
5834 }
5835
5836 if (is_long_mode(vcpu))
5837 mode |= MSR_BITMAP_MODE_LM;
5838
5839 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08005840}
5841
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005842#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
5843
5844static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
5845 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08005846{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005847 int msr;
5848
5849 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
5850 unsigned word = msr / BITS_PER_LONG;
5851 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
5852 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08005853 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005854
5855 if (mode & MSR_BITMAP_MODE_X2APIC) {
5856 /*
5857 * TPR reads and writes can be virtualized even if virtual interrupt
5858 * delivery is not in use.
5859 */
5860 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
5861 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
5862 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
5863 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
5864 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
5865 }
5866 }
5867}
5868
5869static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
5870{
5871 struct vcpu_vmx *vmx = to_vmx(vcpu);
5872 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
5873 u8 mode = vmx_msr_bitmap_mode(vcpu);
5874 u8 changed = mode ^ vmx->msr_bitmap_mode;
5875
5876 if (!changed)
5877 return;
5878
5879 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
5880 !(mode & MSR_BITMAP_MODE_LM));
5881
5882 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
5883 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
5884
5885 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02005886}
5887
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05005888static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005889{
Andrey Smetanind62caab2015-11-10 15:36:33 +03005890 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02005891}
5892
David Matlackc9f04402017-08-01 14:00:40 -07005893static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
5894{
5895 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5896 gfn_t gfn;
5897
5898 /*
5899 * Don't need to mark the APIC access page dirty; it is never
5900 * written to by the CPU during APIC virtualization.
5901 */
5902
5903 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
5904 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
5905 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5906 }
5907
5908 if (nested_cpu_has_posted_intr(vmcs12)) {
5909 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
5910 kvm_vcpu_mark_page_dirty(vcpu, gfn);
5911 }
5912}
5913
5914
David Hildenbrand6342c502017-01-25 11:58:58 +01005915static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08005916{
5917 struct vcpu_vmx *vmx = to_vmx(vcpu);
5918 int max_irr;
5919 void *vapic_page;
5920 u16 status;
5921
David Matlackc9f04402017-08-01 14:00:40 -07005922 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
5923 return;
Wincy Van705699a2015-02-03 23:58:17 +08005924
David Matlackc9f04402017-08-01 14:00:40 -07005925 vmx->nested.pi_pending = false;
5926 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
5927 return;
Wincy Van705699a2015-02-03 23:58:17 +08005928
David Matlackc9f04402017-08-01 14:00:40 -07005929 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5930 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005931 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02005932 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
5933 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08005934 kunmap(vmx->nested.virtual_apic_page);
5935
5936 status = vmcs_read16(GUEST_INTR_STATUS);
5937 if ((u8)max_irr > ((u8)status & 0xff)) {
5938 status &= ~0xff;
5939 status |= (u8)max_irr;
5940 vmcs_write16(GUEST_INTR_STATUS, status);
5941 }
5942 }
David Matlackc9f04402017-08-01 14:00:40 -07005943
5944 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005945}
5946
Wincy Van06a55242017-04-28 13:13:59 +08005947static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
5948 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005949{
5950#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08005951 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
5952
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005953 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005954 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005955 * The vector of interrupt to be delivered to vcpu had
5956 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005957 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08005958 * Following cases will be reached in this block, and
5959 * we always send a notification event in all cases as
5960 * explained below.
5961 *
5962 * Case 1: vcpu keeps in non-root mode. Sending a
5963 * notification event posts the interrupt to vcpu.
5964 *
5965 * Case 2: vcpu exits to root mode and is still
5966 * runnable. PIR will be synced to vIRR before the
5967 * next vcpu entry. Sending a notification event in
5968 * this case has no effect, as vcpu is not in root
5969 * mode.
5970 *
5971 * Case 3: vcpu exits to root mode and is blocked.
5972 * vcpu_block() has already synced PIR to vIRR and
5973 * never blocks vcpu if vIRR is not cleared. Therefore,
5974 * a blocked vcpu here does not wait for any requested
5975 * interrupts in PIR, and sending a notification event
5976 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005977 */
Feng Wu28b835d2015-09-18 22:29:54 +08005978
Wincy Van06a55242017-04-28 13:13:59 +08005979 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005980 return true;
5981 }
5982#endif
5983 return false;
5984}
5985
Wincy Van705699a2015-02-03 23:58:17 +08005986static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5987 int vector)
5988{
5989 struct vcpu_vmx *vmx = to_vmx(vcpu);
5990
5991 if (is_guest_mode(vcpu) &&
5992 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005993 /*
5994 * If a posted intr is not recognized by hardware,
5995 * we will accomplish it in the next vmentry.
5996 */
5997 vmx->nested.pi_pending = true;
5998 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02005999 /* the PIR and ON have been set by L1. */
6000 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6001 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006002 return 0;
6003 }
6004 return -1;
6005}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006006/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006007 * Send interrupt to vcpu via posted interrupt way.
6008 * 1. If target vcpu is running(non-root mode), send posted interrupt
6009 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6010 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6011 * interrupt from PIR in next vmentry.
6012 */
6013static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6014{
6015 struct vcpu_vmx *vmx = to_vmx(vcpu);
6016 int r;
6017
Wincy Van705699a2015-02-03 23:58:17 +08006018 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6019 if (!r)
6020 return;
6021
Yang Zhanga20ed542013-04-11 19:25:15 +08006022 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6023 return;
6024
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006025 /* If a previous notification has sent the IPI, nothing to do. */
6026 if (pi_test_and_set_on(&vmx->pi_desc))
6027 return;
6028
Wincy Van06a55242017-04-28 13:13:59 +08006029 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006030 kvm_vcpu_kick(vcpu);
6031}
6032
Avi Kivity6aa8b732006-12-10 02:21:36 -08006033/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006034 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6035 * will not change in the lifetime of the guest.
6036 * Note that host-state that does change is set elsewhere. E.g., host-state
6037 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6038 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006039static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006040{
6041 u32 low32, high32;
6042 unsigned long tmpl;
6043 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006044 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006045
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006046 cr0 = read_cr0();
6047 WARN_ON(cr0 & X86_CR0_TS);
6048 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006049
6050 /*
6051 * Save the most likely value for this task's CR3 in the VMCS.
6052 * We can't use __get_current_cr3_fast() because we're not atomic.
6053 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006054 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006055 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Ladi Prosek44889942017-09-22 07:53:15 +02006056 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006057
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006058 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006059 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006060 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Ladi Prosek44889942017-09-22 07:53:15 +02006061 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006062
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006063 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006064#ifdef CONFIG_X86_64
6065 /*
6066 * Load null selectors, so we can avoid reloading them in
6067 * __vmx_load_host_state(), in case userspace uses the null selectors
6068 * too (the expected case).
6069 */
6070 vmcs_write16(HOST_DS_SELECTOR, 0);
6071 vmcs_write16(HOST_ES_SELECTOR, 0);
6072#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006073 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6074 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006075#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006076 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6077 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6078
Juergen Gross87930012017-09-04 12:25:27 +02006079 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006080 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006081 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006082
Avi Kivity83287ea422012-09-16 15:10:57 +03006083 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006084
6085 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6086 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6087 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6088 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6089
6090 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6091 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6092 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6093 }
6094}
6095
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006096static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6097{
6098 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6099 if (enable_ept)
6100 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006101 if (is_guest_mode(&vmx->vcpu))
6102 vmx->vcpu.arch.cr4_guest_owned_bits &=
6103 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006104 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6105}
6106
Yang Zhang01e439b2013-04-11 19:25:12 +08006107static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6108{
6109 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6110
Andrey Smetanind62caab2015-11-10 15:36:33 +03006111 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006112 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006113
6114 if (!enable_vnmi)
6115 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6116
Yunhong Jiang64672c92016-06-13 14:19:59 -07006117 /* Enable the preemption timer dynamically */
6118 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006119 return pin_based_exec_ctrl;
6120}
6121
Andrey Smetanind62caab2015-11-10 15:36:33 +03006122static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6123{
6124 struct vcpu_vmx *vmx = to_vmx(vcpu);
6125
6126 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006127 if (cpu_has_secondary_exec_ctrls()) {
6128 if (kvm_vcpu_apicv_active(vcpu))
6129 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6130 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6131 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6132 else
6133 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6134 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6135 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6136 }
6137
6138 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006139 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006140}
6141
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006142static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6143{
6144 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006145
6146 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6147 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6148
Paolo Bonzini35754c92015-07-29 12:05:37 +02006149 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006150 exec_control &= ~CPU_BASED_TPR_SHADOW;
6151#ifdef CONFIG_X86_64
6152 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6153 CPU_BASED_CR8_LOAD_EXITING;
6154#endif
6155 }
6156 if (!enable_ept)
6157 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6158 CPU_BASED_CR3_LOAD_EXITING |
6159 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006160 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6161 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6162 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006163 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6164 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006165 return exec_control;
6166}
6167
Jim Mattson45ec3682017-08-23 16:32:04 -07006168static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006169{
Jim Mattson45ec3682017-08-23 16:32:04 -07006170 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006171 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006172}
6173
Jim Mattson75f4fc82017-08-23 16:32:03 -07006174static bool vmx_rdseed_supported(void)
6175{
6176 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006177 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006178}
6179
Paolo Bonzini80154d72017-08-24 13:55:35 +02006180static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006181{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006182 struct kvm_vcpu *vcpu = &vmx->vcpu;
6183
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006184 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006185
Paolo Bonzini80154d72017-08-24 13:55:35 +02006186 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006187 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6188 if (vmx->vpid == 0)
6189 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6190 if (!enable_ept) {
6191 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6192 enable_unrestricted_guest = 0;
6193 }
6194 if (!enable_unrestricted_guest)
6195 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006196 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006197 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006198 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006199 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6200 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006201 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006202
6203 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6204 * in vmx_set_cr4. */
6205 exec_control &= ~SECONDARY_EXEC_DESC;
6206
Abel Gordonabc4fc52013-04-18 14:35:25 +03006207 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6208 (handle_vmptrld).
6209 We can NOT enable shadow_vmcs here because we don't have yet
6210 a current VMCS12
6211 */
6212 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006213
6214 if (!enable_pml)
6215 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006216
Paolo Bonzini3db13482017-08-24 14:48:03 +02006217 if (vmx_xsaves_supported()) {
6218 /* Exposing XSAVES only when XSAVE is exposed */
6219 bool xsaves_enabled =
6220 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6221 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6222
6223 if (!xsaves_enabled)
6224 exec_control &= ~SECONDARY_EXEC_XSAVES;
6225
6226 if (nested) {
6227 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006228 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006229 SECONDARY_EXEC_XSAVES;
6230 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006231 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006232 ~SECONDARY_EXEC_XSAVES;
6233 }
6234 }
6235
Paolo Bonzini80154d72017-08-24 13:55:35 +02006236 if (vmx_rdtscp_supported()) {
6237 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6238 if (!rdtscp_enabled)
6239 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6240
6241 if (nested) {
6242 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006243 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006244 SECONDARY_EXEC_RDTSCP;
6245 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006246 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006247 ~SECONDARY_EXEC_RDTSCP;
6248 }
6249 }
6250
6251 if (vmx_invpcid_supported()) {
6252 /* Exposing INVPCID only when PCID is exposed */
6253 bool invpcid_enabled =
6254 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6255 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6256
6257 if (!invpcid_enabled) {
6258 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6259 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6260 }
6261
6262 if (nested) {
6263 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006264 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006265 SECONDARY_EXEC_ENABLE_INVPCID;
6266 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006267 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006268 ~SECONDARY_EXEC_ENABLE_INVPCID;
6269 }
6270 }
6271
Jim Mattson45ec3682017-08-23 16:32:04 -07006272 if (vmx_rdrand_supported()) {
6273 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6274 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006275 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006276
6277 if (nested) {
6278 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006279 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006280 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006281 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006282 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006283 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006284 }
6285 }
6286
Jim Mattson75f4fc82017-08-23 16:32:03 -07006287 if (vmx_rdseed_supported()) {
6288 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6289 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006290 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006291
6292 if (nested) {
6293 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006294 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006295 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006296 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006297 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006298 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006299 }
6300 }
6301
Paolo Bonzini80154d72017-08-24 13:55:35 +02006302 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006303}
6304
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006305static void ept_set_mmio_spte_mask(void)
6306{
6307 /*
6308 * EPT Misconfigurations can be generated if the value of bits 2:0
6309 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006310 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006311 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6312 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006313}
6314
Wanpeng Lif53cd632014-12-02 19:14:58 +08006315#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006316/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317 * Sets up the vmcs for emulated real mode.
6318 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006319static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006320{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006321#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006322 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02006323#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08006324 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006325
Abel Gordon4607c2d2013-04-18 14:35:55 +03006326 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006327 /*
6328 * At vCPU creation, "VMWRITE to any supported field
6329 * in the VMCS" is supported, so use the more
6330 * permissive vmx_vmread_bitmap to specify both read
6331 * and write permissions for the shadow VMCS.
6332 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006333 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006334 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006335 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006336 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006337 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006338
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6340
Avi Kivity6aa8b732006-12-10 02:21:36 -08006341 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006342 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006343 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006344
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006345 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006346
Dan Williamsdfa169b2016-06-02 11:17:24 -07006347 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006348 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006349 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006350 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006351 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006352
Andrey Smetanind62caab2015-11-10 15:36:33 +03006353 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006354 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6355 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6356 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6357 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6358
6359 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006360
Li RongQing0bcf2612015-12-03 13:29:34 +08006361 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006362 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006363 }
6364
Wanpeng Lib31c1142018-03-12 04:53:04 -07006365 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006366 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006367 vmx->ple_window = ple_window;
6368 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006369 }
6370
Xiao Guangrongc3707952011-07-12 03:28:04 +08006371 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6372 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006373 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6374
Avi Kivity9581d442010-10-19 16:46:55 +02006375 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6376 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006377 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006378#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08006379 rdmsrl(MSR_FS_BASE, a);
6380 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
6381 rdmsrl(MSR_GS_BASE, a);
6382 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
6383#else
6384 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6385 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
6386#endif
6387
Bandan Das2a499e42017-08-03 15:54:41 -04006388 if (cpu_has_vmx_vmfunc())
6389 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6390
Eddie Dong2cc51562007-05-21 07:28:09 +03006391 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6392 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006393 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03006394 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03006395 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006396
Radim Krčmář74545702015-04-27 15:11:25 +02006397 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6398 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006399
Paolo Bonzini03916db2014-07-24 14:21:57 +02006400 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006401 u32 index = vmx_msr_index[i];
6402 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006403 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006404
6405 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6406 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006407 if (wrmsr_safe(index, data_low, data_high) < 0)
6408 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006409 vmx->guest_msrs[j].index = i;
6410 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006411 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006412 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006413 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006414
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01006415 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
6416 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02006417
6418 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006419
6420 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006421 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006422
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006423 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6424 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6425
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006426 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006427
Wanpeng Lif53cd632014-12-02 19:14:58 +08006428 if (vmx_xsaves_supported())
6429 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6430
Peter Feiner4e595162016-07-07 14:49:58 -07006431 if (enable_pml) {
6432 ASSERT(vmx->pml_pg);
6433 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6434 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6435 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006436}
6437
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006438static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006439{
6440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006441 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006442 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006443
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006444 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006445 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006446
Wanpeng Li518e7b92018-02-28 14:03:31 +08006447 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006448 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006449 kvm_set_cr8(vcpu, 0);
6450
6451 if (!init_event) {
6452 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6453 MSR_IA32_APICBASE_ENABLE;
6454 if (kvm_vcpu_is_reset_bsp(vcpu))
6455 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6456 apic_base_msr.host_initiated = true;
6457 kvm_set_apic_base(vcpu, &apic_base_msr);
6458 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006459
Avi Kivity2fb92db2011-04-27 19:42:18 +03006460 vmx_segment_cache_clear(vmx);
6461
Avi Kivity5706be02008-08-20 15:07:31 +03006462 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006463 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006464 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006465
6466 seg_setup(VCPU_SREG_DS);
6467 seg_setup(VCPU_SREG_ES);
6468 seg_setup(VCPU_SREG_FS);
6469 seg_setup(VCPU_SREG_GS);
6470 seg_setup(VCPU_SREG_SS);
6471
6472 vmcs_write16(GUEST_TR_SELECTOR, 0);
6473 vmcs_writel(GUEST_TR_BASE, 0);
6474 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6475 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6476
6477 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6478 vmcs_writel(GUEST_LDTR_BASE, 0);
6479 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6480 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6481
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006482 if (!init_event) {
6483 vmcs_write32(GUEST_SYSENTER_CS, 0);
6484 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6485 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6486 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6487 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006488
Wanpeng Lic37c2872017-11-20 14:52:21 -08006489 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006490 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006491
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006492 vmcs_writel(GUEST_GDTR_BASE, 0);
6493 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6494
6495 vmcs_writel(GUEST_IDTR_BASE, 0);
6496 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6497
Anthony Liguori443381a2010-12-06 10:53:38 -06006498 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006499 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006500 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006501 if (kvm_mpx_supported())
6502 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006503
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006504 setup_msrs(vmx);
6505
Avi Kivity6aa8b732006-12-10 02:21:36 -08006506 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6507
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006508 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006509 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006510 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006511 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006512 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006513 vmcs_write32(TPR_THRESHOLD, 0);
6514 }
6515
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006516 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006517
Sheng Yang2384d2b2008-01-17 15:14:33 +08006518 if (vmx->vpid != 0)
6519 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6520
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006521 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006522 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006523 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006524 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006525 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006526
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006527 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006528
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006529 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006530 if (init_event)
6531 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006532}
6533
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006534/*
6535 * In nested virtualization, check if L1 asked to exit on external interrupts.
6536 * For most existing hypervisors, this will always return true.
6537 */
6538static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6539{
6540 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6541 PIN_BASED_EXT_INTR_MASK;
6542}
6543
Bandan Das77b0f5d2014-04-19 18:17:45 -04006544/*
6545 * In nested virtualization, check if L1 has set
6546 * VM_EXIT_ACK_INTR_ON_EXIT
6547 */
6548static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6549{
6550 return get_vmcs12(vcpu)->vm_exit_controls &
6551 VM_EXIT_ACK_INTR_ON_EXIT;
6552}
6553
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006554static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6555{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006556 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006557}
6558
Jan Kiszkac9a79532014-03-07 20:03:15 +01006559static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006560{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006561 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6562 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006563}
6564
Jan Kiszkac9a79532014-03-07 20:03:15 +01006565static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006566{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006567 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006568 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006569 enable_irq_window(vcpu);
6570 return;
6571 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006572
Paolo Bonzini47c01522016-12-19 11:44:07 +01006573 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6574 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006575}
6576
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006577static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006578{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006579 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006580 uint32_t intr;
6581 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006582
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006583 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006584
Avi Kivityfa89a812008-09-01 15:57:51 +03006585 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006586 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006587 int inc_eip = 0;
6588 if (vcpu->arch.interrupt.soft)
6589 inc_eip = vcpu->arch.event_exit_inst_len;
6590 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006591 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006592 return;
6593 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006594 intr = irq | INTR_INFO_VALID_MASK;
6595 if (vcpu->arch.interrupt.soft) {
6596 intr |= INTR_TYPE_SOFT_INTR;
6597 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6598 vmx->vcpu.arch.event_exit_inst_len);
6599 } else
6600 intr |= INTR_TYPE_EXT_INTR;
6601 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006602
6603 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006604}
6605
Sheng Yangf08864b2008-05-15 18:23:25 +08006606static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6607{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006608 struct vcpu_vmx *vmx = to_vmx(vcpu);
6609
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006610 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006611 /*
6612 * Tracking the NMI-blocked state in software is built upon
6613 * finding the next open IRQ window. This, in turn, depends on
6614 * well-behaving guests: They have to keep IRQs disabled at
6615 * least as long as the NMI handler runs. Otherwise we may
6616 * cause NMI nesting, maybe breaking the guest. But as this is
6617 * highly unlikely, we can live with the residual risk.
6618 */
6619 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6620 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6621 }
6622
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006623 ++vcpu->stat.nmi_injections;
6624 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006625
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006626 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006627 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006628 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006629 return;
6630 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006631
Sheng Yangf08864b2008-05-15 18:23:25 +08006632 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6633 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006634
6635 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006636}
6637
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006638static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6639{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006640 struct vcpu_vmx *vmx = to_vmx(vcpu);
6641 bool masked;
6642
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006643 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006644 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006645 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006646 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006647 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6648 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6649 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006650}
6651
6652static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6653{
6654 struct vcpu_vmx *vmx = to_vmx(vcpu);
6655
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006656 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006657 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6658 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6659 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6660 }
6661 } else {
6662 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6663 if (masked)
6664 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6665 GUEST_INTR_STATE_NMI);
6666 else
6667 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6668 GUEST_INTR_STATE_NMI);
6669 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006670}
6671
Jan Kiszka2505dc92013-04-14 12:12:47 +02006672static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6673{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006674 if (to_vmx(vcpu)->nested.nested_run_pending)
6675 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006676
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006677 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006678 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6679 return 0;
6680
Jan Kiszka2505dc92013-04-14 12:12:47 +02006681 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6682 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6683 | GUEST_INTR_STATE_NMI));
6684}
6685
Gleb Natapov78646122009-03-23 12:12:11 +02006686static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6687{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006688 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6689 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006690 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6691 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006692}
6693
Izik Eiduscbc94022007-10-25 00:29:55 +02006694static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6695{
6696 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006697
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006698 if (enable_unrestricted_guest)
6699 return 0;
6700
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006701 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6702 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006703 if (ret)
6704 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006705 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006706 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006707}
6708
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006709static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6710{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006711 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006712 return 0;
6713}
6714
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006715static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006716{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006717 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006718 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006719 /*
6720 * Update instruction length as we may reinject the exception
6721 * from user space while in guest debugging mode.
6722 */
6723 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6724 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006725 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006726 return false;
6727 /* fall through */
6728 case DB_VECTOR:
6729 if (vcpu->guest_debug &
6730 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6731 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006732 /* fall through */
6733 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006734 case OF_VECTOR:
6735 case BR_VECTOR:
6736 case UD_VECTOR:
6737 case DF_VECTOR:
6738 case SS_VECTOR:
6739 case GP_VECTOR:
6740 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006741 return true;
6742 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006743 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006744 return false;
6745}
6746
6747static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6748 int vec, u32 err_code)
6749{
6750 /*
6751 * Instruction with address size override prefix opcode 0x67
6752 * Cause the #SS fault with 0 error code in VM86 mode.
6753 */
6754 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6755 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6756 if (vcpu->arch.halt_request) {
6757 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006758 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006759 }
6760 return 1;
6761 }
6762 return 0;
6763 }
6764
6765 /*
6766 * Forward all other exceptions that are valid in real mode.
6767 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6768 * the required debugging infrastructure rework.
6769 */
6770 kvm_queue_exception(vcpu, vec);
6771 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006772}
6773
Andi Kleena0861c02009-06-08 17:37:09 +08006774/*
6775 * Trigger machine check on the host. We assume all the MSRs are already set up
6776 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6777 * We pass a fake environment to the machine check handler because we want
6778 * the guest to be always treated like user space, no matter what context
6779 * it used internally.
6780 */
6781static void kvm_machine_check(void)
6782{
6783#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6784 struct pt_regs regs = {
6785 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
6786 .flags = X86_EFLAGS_IF,
6787 };
6788
6789 do_machine_check(&regs, 0);
6790#endif
6791}
6792
Avi Kivity851ba692009-08-24 11:10:17 +03006793static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08006794{
6795 /* already handled by vcpu_run */
6796 return 1;
6797}
6798
Avi Kivity851ba692009-08-24 11:10:17 +03006799static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006800{
Avi Kivity1155f762007-11-22 11:30:47 +02006801 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006802 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006803 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006804 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006805 u32 vect_info;
6806 enum emulation_result er;
6807
Avi Kivity1155f762007-11-22 11:30:47 +02006808 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02006809 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006810
Andi Kleena0861c02009-06-08 17:37:09 +08006811 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03006812 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006813
Jim Mattsonef85b672016-12-12 11:01:37 -08006814 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02006815 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03006816
Wanpeng Li082d06e2018-04-03 16:28:48 -07006817 if (is_invalid_opcode(intr_info))
6818 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05006819
Avi Kivity6aa8b732006-12-10 02:21:36 -08006820 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06006821 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006822 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006823
Liran Alon9e869482018-03-12 13:12:51 +02006824 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
6825 WARN_ON_ONCE(!enable_vmware_backdoor);
6826 er = emulate_instruction(vcpu,
6827 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
6828 if (er == EMULATE_USER_EXIT)
6829 return 0;
6830 else if (er != EMULATE_DONE)
6831 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
6832 return 1;
6833 }
6834
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006835 /*
6836 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
6837 * MMIO, it is better to report an internal error.
6838 * See the comments in vmx_handle_exit.
6839 */
6840 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
6841 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
6842 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6843 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006844 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006845 vcpu->run->internal.data[0] = vect_info;
6846 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02006847 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08006848 return 0;
6849 }
6850
Avi Kivity6aa8b732006-12-10 02:21:36 -08006851 if (is_page_fault(intr_info)) {
6852 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07006853 /* EPT won't cause page fault directly */
6854 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02006855 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006856 }
6857
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006858 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006859
6860 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
6861 return handle_rmode_exception(vcpu, ex_no, error_code);
6862
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006863 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01006864 case AC_VECTOR:
6865 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
6866 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006867 case DB_VECTOR:
6868 dr6 = vmcs_readl(EXIT_QUALIFICATION);
6869 if (!(vcpu->guest_debug &
6870 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01006871 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006872 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07006873 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01006874 skip_emulated_instruction(vcpu);
6875
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006876 kvm_queue_exception(vcpu, DB_VECTOR);
6877 return 1;
6878 }
6879 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
6880 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
6881 /* fall through */
6882 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006883 /*
6884 * Update instruction length as we may reinject #BP from
6885 * user space while in guest debugging mode. Reading it for
6886 * #DB as well causes no harm, it is not used in that case.
6887 */
6888 vmx->vcpu.arch.event_exit_inst_len =
6889 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006890 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03006891 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006892 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
6893 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006894 break;
6895 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006896 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
6897 kvm_run->ex.exception = ex_no;
6898 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006899 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006900 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006901 return 0;
6902}
6903
Avi Kivity851ba692009-08-24 11:10:17 +03006904static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006905{
Avi Kivity1165f5f2007-04-19 17:27:43 +03006906 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006907 return 1;
6908}
6909
Avi Kivity851ba692009-08-24 11:10:17 +03006910static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08006911{
Avi Kivity851ba692009-08-24 11:10:17 +03006912 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07006913 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08006914 return 0;
6915}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006916
Avi Kivity851ba692009-08-24 11:10:17 +03006917static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006918{
He, Qingbfdaab02007-09-12 14:18:28 +08006919 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08006920 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02006921 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006922
He, Qingbfdaab02007-09-12 14:18:28 +08006923 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02006924 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03006925
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006926 ++vcpu->stat.io_exits;
6927
Sean Christopherson432baf62018-03-08 08:57:26 -08006928 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01006929 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006930
6931 port = exit_qualification >> 16;
6932 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08006933 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02006934
Sean Christophersondca7f122018-03-08 08:57:27 -08006935 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006936}
6937
Ingo Molnar102d8322007-02-19 14:37:47 +02006938static void
6939vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
6940{
6941 /*
6942 * Patch in the VMCALL instruction:
6943 */
6944 hypercall[0] = 0x0f;
6945 hypercall[1] = 0x01;
6946 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02006947}
6948
Guo Chao0fa06072012-06-28 15:16:19 +08006949/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006950static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
6951{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006952 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006953 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6954 unsigned long orig_val = val;
6955
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006956 /*
6957 * We get here when L2 changed cr0 in a way that did not change
6958 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006959 * but did change L0 shadowed bits. So we first calculate the
6960 * effective cr0 value that L1 would like to write into the
6961 * hardware. It consists of the L2-owned bits from the new
6962 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006963 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006964 val = (val & ~vmcs12->cr0_guest_host_mask) |
6965 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
6966
David Matlack38991522016-11-29 18:14:08 -08006967 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006968 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006969
6970 if (kvm_set_cr0(vcpu, val))
6971 return 1;
6972 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006973 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006974 } else {
6975 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08006976 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006977 return 1;
David Matlack38991522016-11-29 18:14:08 -08006978
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006979 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006980 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006981}
6982
6983static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
6984{
6985 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006986 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6987 unsigned long orig_val = val;
6988
6989 /* analogously to handle_set_cr0 */
6990 val = (val & ~vmcs12->cr4_guest_host_mask) |
6991 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
6992 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006993 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01006994 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006995 return 0;
6996 } else
6997 return kvm_set_cr4(vcpu, val);
6998}
6999
Paolo Bonzini0367f202016-07-12 10:44:55 +02007000static int handle_desc(struct kvm_vcpu *vcpu)
7001{
7002 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7003 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7004}
7005
Avi Kivity851ba692009-08-24 11:10:17 +03007006static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007007{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007008 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007009 int cr;
7010 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007011 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007012 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007013
He, Qingbfdaab02007-09-12 14:18:28 +08007014 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007015 cr = exit_qualification & 15;
7016 reg = (exit_qualification >> 8) & 15;
7017 switch ((exit_qualification >> 4) & 3) {
7018 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007019 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007020 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007021 switch (cr) {
7022 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007023 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007024 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007025 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007026 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007027 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007028 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007029 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007030 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007031 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007032 case 8: {
7033 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007034 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007035 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007036 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007037 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007038 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007039 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007040 return ret;
7041 /*
7042 * TODO: we might be squashing a
7043 * KVM_GUESTDBG_SINGLESTEP-triggered
7044 * KVM_EXIT_DEBUG here.
7045 */
Avi Kivity851ba692009-08-24 11:10:17 +03007046 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007047 return 0;
7048 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007049 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007050 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007051 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007052 WARN_ONCE(1, "Guest should always own CR0.TS");
7053 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007054 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007055 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007056 case 1: /*mov from cr*/
7057 switch (cr) {
7058 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007059 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007060 val = kvm_read_cr3(vcpu);
7061 kvm_register_write(vcpu, reg, val);
7062 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007063 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007064 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007065 val = kvm_get_cr8(vcpu);
7066 kvm_register_write(vcpu, reg, val);
7067 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007068 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007069 }
7070 break;
7071 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007072 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007073 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007074 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007075
Kyle Huey6affcbe2016-11-29 12:40:40 -08007076 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007077 default:
7078 break;
7079 }
Avi Kivity851ba692009-08-24 11:10:17 +03007080 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007081 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007082 (int)(exit_qualification >> 4) & 3, cr);
7083 return 0;
7084}
7085
Avi Kivity851ba692009-08-24 11:10:17 +03007086static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007087{
He, Qingbfdaab02007-09-12 14:18:28 +08007088 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007089 int dr, dr7, reg;
7090
7091 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7092 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7093
7094 /* First, if DR does not exist, trigger UD */
7095 if (!kvm_require_dr(vcpu, dr))
7096 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007097
Jan Kiszkaf2483412010-01-20 18:20:20 +01007098 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007099 if (!kvm_require_cpl(vcpu, 0))
7100 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007101 dr7 = vmcs_readl(GUEST_DR7);
7102 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007103 /*
7104 * As the vm-exit takes precedence over the debug trap, we
7105 * need to emulate the latter, either for the host or the
7106 * guest debugging itself.
7107 */
7108 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007109 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007110 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007111 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007112 vcpu->run->debug.arch.exception = DB_VECTOR;
7113 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007114 return 0;
7115 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007116 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007117 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007118 kvm_queue_exception(vcpu, DB_VECTOR);
7119 return 1;
7120 }
7121 }
7122
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007123 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007124 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7125 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007126
7127 /*
7128 * No more DR vmexits; force a reload of the debug registers
7129 * and reenter on this instruction. The next vmexit will
7130 * retrieve the full state of the debug registers.
7131 */
7132 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7133 return 1;
7134 }
7135
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007136 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7137 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007138 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007139
7140 if (kvm_get_dr(vcpu, dr, &val))
7141 return 1;
7142 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007143 } else
Nadav Amit57773922014-06-18 17:19:23 +03007144 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007145 return 1;
7146
Kyle Huey6affcbe2016-11-29 12:40:40 -08007147 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148}
7149
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007150static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7151{
7152 return vcpu->arch.dr6;
7153}
7154
7155static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7156{
7157}
7158
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007159static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7160{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007161 get_debugreg(vcpu->arch.db[0], 0);
7162 get_debugreg(vcpu->arch.db[1], 1);
7163 get_debugreg(vcpu->arch.db[2], 2);
7164 get_debugreg(vcpu->arch.db[3], 3);
7165 get_debugreg(vcpu->arch.dr6, 6);
7166 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7167
7168 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007169 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007170}
7171
Gleb Natapov020df072010-04-13 10:05:23 +03007172static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7173{
7174 vmcs_writel(GUEST_DR7, val);
7175}
7176
Avi Kivity851ba692009-08-24 11:10:17 +03007177static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007178{
Kyle Huey6a908b62016-11-29 12:40:37 -08007179 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007180}
7181
Avi Kivity851ba692009-08-24 11:10:17 +03007182static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007183{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007184 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007185 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007186
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007187 msr_info.index = ecx;
7188 msr_info.host_initiated = false;
7189 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007190 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007191 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007192 return 1;
7193 }
7194
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007195 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007196
Avi Kivity6aa8b732006-12-10 02:21:36 -08007197 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007198 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7199 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007200 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007201}
7202
Avi Kivity851ba692009-08-24 11:10:17 +03007203static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007204{
Will Auld8fe8ab42012-11-29 12:42:12 -08007205 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007206 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7207 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7208 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007209
Will Auld8fe8ab42012-11-29 12:42:12 -08007210 msr.data = data;
7211 msr.index = ecx;
7212 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007213 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007214 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007215 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007216 return 1;
7217 }
7218
Avi Kivity59200272010-01-25 19:47:02 +02007219 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007220 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007221}
7222
Avi Kivity851ba692009-08-24 11:10:17 +03007223static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007224{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007225 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007226 return 1;
7227}
7228
Avi Kivity851ba692009-08-24 11:10:17 +03007229static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007230{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007231 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7232 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007233
Avi Kivity3842d132010-07-27 12:30:24 +03007234 kvm_make_request(KVM_REQ_EVENT, vcpu);
7235
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007236 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007237 return 1;
7238}
7239
Avi Kivity851ba692009-08-24 11:10:17 +03007240static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007241{
Avi Kivityd3bef152007-06-05 15:53:05 +03007242 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007243}
7244
Avi Kivity851ba692009-08-24 11:10:17 +03007245static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007246{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007247 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007248}
7249
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007250static int handle_invd(struct kvm_vcpu *vcpu)
7251{
Andre Przywara51d8b662010-12-21 11:12:02 +01007252 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007253}
7254
Avi Kivity851ba692009-08-24 11:10:17 +03007255static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007256{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007257 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007258
7259 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007260 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007261}
7262
Avi Kivityfee84b02011-11-10 14:57:25 +02007263static int handle_rdpmc(struct kvm_vcpu *vcpu)
7264{
7265 int err;
7266
7267 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007268 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007269}
7270
Avi Kivity851ba692009-08-24 11:10:17 +03007271static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007272{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007273 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007274}
7275
Dexuan Cui2acf9232010-06-10 11:27:12 +08007276static int handle_xsetbv(struct kvm_vcpu *vcpu)
7277{
7278 u64 new_bv = kvm_read_edx_eax(vcpu);
7279 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7280
7281 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007282 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007283 return 1;
7284}
7285
Wanpeng Lif53cd632014-12-02 19:14:58 +08007286static int handle_xsaves(struct kvm_vcpu *vcpu)
7287{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007288 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007289 WARN(1, "this should never happen\n");
7290 return 1;
7291}
7292
7293static int handle_xrstors(struct kvm_vcpu *vcpu)
7294{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007295 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007296 WARN(1, "this should never happen\n");
7297 return 1;
7298}
7299
Avi Kivity851ba692009-08-24 11:10:17 +03007300static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007301{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007302 if (likely(fasteoi)) {
7303 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7304 int access_type, offset;
7305
7306 access_type = exit_qualification & APIC_ACCESS_TYPE;
7307 offset = exit_qualification & APIC_ACCESS_OFFSET;
7308 /*
7309 * Sane guest uses MOV to write EOI, with written value
7310 * not cared. So make a short-circuit here by avoiding
7311 * heavy instruction emulation.
7312 */
7313 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7314 (offset == APIC_EOI)) {
7315 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007316 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007317 }
7318 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007319 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007320}
7321
Yang Zhangc7c9c562013-01-25 10:18:51 +08007322static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7323{
7324 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7325 int vector = exit_qualification & 0xff;
7326
7327 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7328 kvm_apic_set_eoi_accelerated(vcpu, vector);
7329 return 1;
7330}
7331
Yang Zhang83d4c282013-01-25 10:18:49 +08007332static int handle_apic_write(struct kvm_vcpu *vcpu)
7333{
7334 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7335 u32 offset = exit_qualification & 0xfff;
7336
7337 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7338 kvm_apic_write_nodecode(vcpu, offset);
7339 return 1;
7340}
7341
Avi Kivity851ba692009-08-24 11:10:17 +03007342static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007343{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007344 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007345 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007346 bool has_error_code = false;
7347 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007348 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007349 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007350
7351 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007352 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007353 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007354
7355 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7356
7357 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007358 if (reason == TASK_SWITCH_GATE && idt_v) {
7359 switch (type) {
7360 case INTR_TYPE_NMI_INTR:
7361 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007362 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007363 break;
7364 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007365 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007366 kvm_clear_interrupt_queue(vcpu);
7367 break;
7368 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007369 if (vmx->idt_vectoring_info &
7370 VECTORING_INFO_DELIVER_CODE_MASK) {
7371 has_error_code = true;
7372 error_code =
7373 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7374 }
7375 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007376 case INTR_TYPE_SOFT_EXCEPTION:
7377 kvm_clear_exception_queue(vcpu);
7378 break;
7379 default:
7380 break;
7381 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007382 }
Izik Eidus37817f22008-03-24 23:14:53 +02007383 tss_selector = exit_qualification;
7384
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007385 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7386 type != INTR_TYPE_EXT_INTR &&
7387 type != INTR_TYPE_NMI_INTR))
7388 skip_emulated_instruction(vcpu);
7389
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007390 if (kvm_task_switch(vcpu, tss_selector,
7391 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7392 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007393 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7394 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7395 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007396 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007397 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007398
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007399 /*
7400 * TODO: What about debug traps on tss switch?
7401 * Are we supposed to inject them and update dr6?
7402 */
7403
7404 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007405}
7406
Avi Kivity851ba692009-08-24 11:10:17 +03007407static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007408{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007409 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007410 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007411 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007412
Sheng Yangf9c617f2009-03-25 10:08:52 +08007413 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007414
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007415 /*
7416 * EPT violation happened while executing iret from NMI,
7417 * "blocked by NMI" bit has to be set before next VM entry.
7418 * There are errata that may cause this bit to not be set:
7419 * AAK134, BY25.
7420 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007421 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007422 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007423 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007424 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7425
Sheng Yang14394422008-04-28 12:24:45 +08007426 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007427 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007428
Junaid Shahid27959a42016-12-06 16:46:10 -08007429 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007430 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007431 ? PFERR_USER_MASK : 0;
7432 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007433 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007434 ? PFERR_WRITE_MASK : 0;
7435 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007436 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007437 ? PFERR_FETCH_MASK : 0;
7438 /* ept page table entry is present? */
7439 error_code |= (exit_qualification &
7440 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7441 EPT_VIOLATION_EXECUTABLE))
7442 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007443
Paolo Bonzinieebed242016-11-28 14:39:58 +01007444 error_code |= (exit_qualification & 0x100) != 0 ?
7445 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007446
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007447 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007448 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007449}
7450
Avi Kivity851ba692009-08-24 11:10:17 +03007451static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007452{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007453 gpa_t gpa;
7454
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007455 /*
7456 * A nested guest cannot optimize MMIO vmexits, because we have an
7457 * nGPA here instead of the required GPA.
7458 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007459 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007460 if (!is_guest_mode(vcpu) &&
7461 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007462 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007463 /*
7464 * Doing kvm_skip_emulated_instruction() depends on undefined
7465 * behavior: Intel's manual doesn't mandate
7466 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7467 * occurs and while on real hardware it was observed to be set,
7468 * other hypervisors (namely Hyper-V) don't set it, we end up
7469 * advancing IP with some random value. Disable fast mmio when
7470 * running nested and keep it for real hardware in hope that
7471 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7472 */
7473 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7474 return kvm_skip_emulated_instruction(vcpu);
7475 else
7476 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7477 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007478 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007479
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007480 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007481}
7482
Avi Kivity851ba692009-08-24 11:10:17 +03007483static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007484{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007485 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007486 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7487 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007488 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007489 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007490
7491 return 1;
7492}
7493
Mohammed Gamal80ced182009-09-01 12:48:18 +02007494static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007495{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007496 struct vcpu_vmx *vmx = to_vmx(vcpu);
7497 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007498 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007499 u32 cpu_exec_ctrl;
7500 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007501 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007502
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007503 /*
7504 * We should never reach the point where we are emulating L2
7505 * due to invalid guest state as that means we incorrectly
7506 * allowed a nested VMEntry with an invalid vmcs12.
7507 */
7508 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7509
Avi Kivity49e9d552010-09-19 14:34:08 +02007510 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7511 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007512
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007513 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007514 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007515 return handle_interrupt_window(&vmx->vcpu);
7516
Radim Krčmář72875d82017-04-26 22:32:19 +02007517 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007518 return 1;
7519
Liran Alon9b8ae632017-11-05 16:56:34 +02007520 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007521
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007522 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007523 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007524 ret = 0;
7525 goto out;
7526 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007527
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007528 if (err != EMULATE_DONE)
7529 goto emulation_error;
7530
7531 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7532 vcpu->arch.exception.pending)
7533 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007534
Gleb Natapov8d76c492013-05-08 18:38:44 +03007535 if (vcpu->arch.halt_request) {
7536 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007537 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007538 goto out;
7539 }
7540
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007541 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007542 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007543 if (need_resched())
7544 schedule();
7545 }
7546
Mohammed Gamal80ced182009-09-01 12:48:18 +02007547out:
7548 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007549
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007550emulation_error:
7551 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7552 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7553 vcpu->run->internal.ndata = 0;
7554 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007555}
7556
7557static void grow_ple_window(struct kvm_vcpu *vcpu)
7558{
7559 struct vcpu_vmx *vmx = to_vmx(vcpu);
7560 int old = vmx->ple_window;
7561
Babu Mogerc8e88712018-03-16 16:37:24 -04007562 vmx->ple_window = __grow_ple_window(old, ple_window,
7563 ple_window_grow,
7564 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007565
7566 if (vmx->ple_window != old)
7567 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007568
7569 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007570}
7571
7572static void shrink_ple_window(struct kvm_vcpu *vcpu)
7573{
7574 struct vcpu_vmx *vmx = to_vmx(vcpu);
7575 int old = vmx->ple_window;
7576
Babu Mogerc8e88712018-03-16 16:37:24 -04007577 vmx->ple_window = __shrink_ple_window(old, ple_window,
7578 ple_window_shrink,
7579 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007580
7581 if (vmx->ple_window != old)
7582 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007583
7584 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007585}
7586
7587/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007588 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7589 */
7590static void wakeup_handler(void)
7591{
7592 struct kvm_vcpu *vcpu;
7593 int cpu = smp_processor_id();
7594
7595 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7596 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7597 blocked_vcpu_list) {
7598 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7599
7600 if (pi_test_on(pi_desc) == 1)
7601 kvm_vcpu_kick(vcpu);
7602 }
7603 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7604}
7605
Peng Haoe01bca22018-04-07 05:47:32 +08007606static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007607{
7608 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7609 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7610 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7611 0ull, VMX_EPT_EXECUTABLE_MASK,
7612 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007613 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007614
7615 ept_set_mmio_spte_mask();
7616 kvm_enable_tdp();
7617}
7618
Tiejun Chenf2c76482014-10-28 10:14:47 +08007619static __init int hardware_setup(void)
7620{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007621 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007622 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007623
7624 rdmsrl_safe(MSR_EFER, &host_efer);
7625
7626 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7627 kvm_define_shared_msr(i, vmx_msr_index[i]);
7628
Radim Krčmář23611332016-09-29 22:41:33 +02007629 for (i = 0; i < VMX_BITMAP_NR; i++) {
7630 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7631 if (!vmx_bitmap[i])
7632 goto out;
7633 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007634
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007635 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7636 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7637
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007638 if (setup_vmcs_config(&vmcs_config) < 0) {
7639 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007640 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007641 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007642
7643 if (boot_cpu_has(X86_FEATURE_NX))
7644 kvm_enable_efer_bits(EFER_NX);
7645
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007646 if (boot_cpu_has(X86_FEATURE_MPX)) {
7647 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7648 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7649 }
7650
Wanpeng Li08d839c2017-03-23 05:30:08 -07007651 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7652 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007653 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007654
Tiejun Chenf2c76482014-10-28 10:14:47 +08007655 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007656 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007657 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007658 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007659 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007660
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007661 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007662 enable_ept_ad_bits = 0;
7663
Wanpeng Li8ad81822017-10-09 15:51:53 -07007664 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007665 enable_unrestricted_guest = 0;
7666
Paolo Bonziniad15a292015-01-30 16:18:49 +01007667 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007668 flexpriority_enabled = 0;
7669
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007670 if (!cpu_has_virtual_nmis())
7671 enable_vnmi = 0;
7672
Paolo Bonziniad15a292015-01-30 16:18:49 +01007673 /*
7674 * set_apic_access_page_addr() is used to reload apic access
7675 * page upon invalidation. No need to do anything if not
7676 * using the APIC_ACCESS_ADDR VMCS field.
7677 */
7678 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007679 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007680
7681 if (!cpu_has_vmx_tpr_shadow())
7682 kvm_x86_ops->update_cr8_intercept = NULL;
7683
7684 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7685 kvm_disable_largepages();
7686
Tianyu Lan877ad952018-07-19 08:40:23 +00007687#if IS_ENABLED(CONFIG_HYPERV)
7688 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7689 && enable_ept)
7690 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7691#endif
7692
Wanpeng Li0f107682017-09-28 18:06:24 -07007693 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007694 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007695 ple_window = 0;
7696 ple_window_grow = 0;
7697 ple_window_max = 0;
7698 ple_window_shrink = 0;
7699 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007700
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007701 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007702 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007703 kvm_x86_ops->sync_pir_to_irr = NULL;
7704 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007705
Haozhong Zhang64903d62015-10-20 15:39:09 +08007706 if (cpu_has_vmx_tsc_scaling()) {
7707 kvm_has_tsc_control = true;
7708 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7709 kvm_tsc_scaling_ratio_frac_bits = 48;
7710 }
7711
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007712 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7713
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007714 if (enable_ept)
7715 vmx_enable_tdp();
7716 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007717 kvm_disable_tdp();
7718
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007719 if (!nested) {
7720 kvm_x86_ops->get_nested_state = NULL;
7721 kvm_x86_ops->set_nested_state = NULL;
7722 }
7723
Kai Huang843e4332015-01-28 10:54:28 +08007724 /*
7725 * Only enable PML when hardware supports PML feature, and both EPT
7726 * and EPT A/D bit features are enabled -- PML depends on them to work.
7727 */
7728 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7729 enable_pml = 0;
7730
7731 if (!enable_pml) {
7732 kvm_x86_ops->slot_enable_log_dirty = NULL;
7733 kvm_x86_ops->slot_disable_log_dirty = NULL;
7734 kvm_x86_ops->flush_log_dirty = NULL;
7735 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7736 }
7737
Yunhong Jiang64672c92016-06-13 14:19:59 -07007738 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7739 u64 vmx_msr;
7740
7741 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7742 cpu_preemption_timer_multi =
7743 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7744 } else {
7745 kvm_x86_ops->set_hv_timer = NULL;
7746 kvm_x86_ops->cancel_hv_timer = NULL;
7747 }
7748
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007749 if (!cpu_has_vmx_shadow_vmcs())
7750 enable_shadow_vmcs = 0;
7751 if (enable_shadow_vmcs)
7752 init_vmcs_shadow_fields();
7753
Feng Wubf9f6ac2015-09-18 22:29:55 +08007754 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007755 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007756
Ashok Rajc45dcc72016-06-22 14:59:56 +08007757 kvm_mce_cap_supported |= MCG_LMCE_P;
7758
Tiejun Chenf2c76482014-10-28 10:14:47 +08007759 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007760
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007761out:
Radim Krčmář23611332016-09-29 22:41:33 +02007762 for (i = 0; i < VMX_BITMAP_NR; i++)
7763 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007764
7765 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007766}
7767
7768static __exit void hardware_unsetup(void)
7769{
Radim Krčmář23611332016-09-29 22:41:33 +02007770 int i;
7771
7772 for (i = 0; i < VMX_BITMAP_NR; i++)
7773 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007774
Tiejun Chenf2c76482014-10-28 10:14:47 +08007775 free_kvm_area();
7776}
7777
Avi Kivity6aa8b732006-12-10 02:21:36 -08007778/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007779 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7780 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7781 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007782static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007783{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007784 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007785 grow_ple_window(vcpu);
7786
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08007787 /*
7788 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
7789 * VM-execution control is ignored if CPL > 0. OTOH, KVM
7790 * never set PAUSE_EXITING and just set PLE if supported,
7791 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
7792 */
7793 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007794 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007795}
7796
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007797static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08007798{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007799 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08007800}
7801
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007802static int handle_mwait(struct kvm_vcpu *vcpu)
7803{
7804 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
7805 return handle_nop(vcpu);
7806}
7807
Jim Mattson45ec3682017-08-23 16:32:04 -07007808static int handle_invalid_op(struct kvm_vcpu *vcpu)
7809{
7810 kvm_queue_exception(vcpu, UD_VECTOR);
7811 return 1;
7812}
7813
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007814static int handle_monitor_trap(struct kvm_vcpu *vcpu)
7815{
7816 return 1;
7817}
7818
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007819static int handle_monitor(struct kvm_vcpu *vcpu)
7820{
7821 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
7822 return handle_nop(vcpu);
7823}
7824
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007825/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007826 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
7827 * set the success or error code of an emulated VMX instruction, as specified
7828 * by Vol 2B, VMX Instruction Reference, "Conventions".
7829 */
7830static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
7831{
7832 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
7833 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7834 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
7835}
7836
7837static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
7838{
7839 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7840 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
7841 X86_EFLAGS_SF | X86_EFLAGS_OF))
7842 | X86_EFLAGS_CF);
7843}
7844
Abel Gordon145c28d2013-04-18 14:36:55 +03007845static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08007846 u32 vm_instruction_error)
7847{
7848 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
7849 /*
7850 * failValid writes the error number to the current VMCS, which
7851 * can't be done there isn't a current VMCS.
7852 */
7853 nested_vmx_failInvalid(vcpu);
7854 return;
7855 }
7856 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
7857 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
7858 X86_EFLAGS_SF | X86_EFLAGS_OF))
7859 | X86_EFLAGS_ZF);
7860 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
7861 /*
7862 * We don't need to force a shadow sync because
7863 * VM_INSTRUCTION_ERROR is not shadowed
7864 */
7865}
Abel Gordon145c28d2013-04-18 14:36:55 +03007866
Wincy Vanff651cb2014-12-11 08:52:58 +03007867static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
7868{
7869 /* TODO: not to reset guest simply here. */
7870 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02007871 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03007872}
7873
Jan Kiszkaf41245002014-03-07 20:03:13 +01007874static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
7875{
7876 struct vcpu_vmx *vmx =
7877 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
7878
7879 vmx->nested.preemption_timer_expired = true;
7880 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
7881 kvm_vcpu_kick(&vmx->vcpu);
7882
7883 return HRTIMER_NORESTART;
7884}
7885
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007886/*
Bandan Das19677e32014-05-06 02:19:15 -04007887 * Decode the memory-address operand of a vmx instruction, as recorded on an
7888 * exit caused by such an instruction (run by a guest hypervisor).
7889 * On success, returns 0. When the operand is invalid, returns 1 and throws
7890 * #UD or #GP.
7891 */
7892static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
7893 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007894 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04007895{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007896 gva_t off;
7897 bool exn;
7898 struct kvm_segment s;
7899
Bandan Das19677e32014-05-06 02:19:15 -04007900 /*
7901 * According to Vol. 3B, "Information for VM Exits Due to Instruction
7902 * Execution", on an exit, vmx_instruction_info holds most of the
7903 * addressing components of the operand. Only the displacement part
7904 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
7905 * For how an actual address is calculated from all these components,
7906 * refer to Vol. 1, "Operand Addressing".
7907 */
7908 int scaling = vmx_instruction_info & 3;
7909 int addr_size = (vmx_instruction_info >> 7) & 7;
7910 bool is_reg = vmx_instruction_info & (1u << 10);
7911 int seg_reg = (vmx_instruction_info >> 15) & 7;
7912 int index_reg = (vmx_instruction_info >> 18) & 0xf;
7913 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
7914 int base_reg = (vmx_instruction_info >> 23) & 0xf;
7915 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
7916
7917 if (is_reg) {
7918 kvm_queue_exception(vcpu, UD_VECTOR);
7919 return 1;
7920 }
7921
7922 /* Addr = segment_base + offset */
7923 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007924 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04007925 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007926 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04007927 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007928 off += kvm_register_read(vcpu, index_reg)<<scaling;
7929 vmx_get_segment(vcpu, &s, seg_reg);
7930 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04007931
7932 if (addr_size == 1) /* 32 bit */
7933 *ret &= 0xffffffff;
7934
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007935 /* Checks for #GP/#SS exceptions. */
7936 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007937 if (is_long_mode(vcpu)) {
7938 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
7939 * non-canonical form. This is the only check on the memory
7940 * destination for long mode!
7941 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08007942 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007943 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007944 /* Protected mode: apply checks for segment validity in the
7945 * following order:
7946 * - segment type check (#GP(0) may be thrown)
7947 * - usability check (#GP(0)/#SS(0))
7948 * - limit check (#GP(0)/#SS(0))
7949 */
7950 if (wr)
7951 /* #GP(0) if the destination operand is located in a
7952 * read-only data segment or any code segment.
7953 */
7954 exn = ((s.type & 0xa) == 0 || (s.type & 8));
7955 else
7956 /* #GP(0) if the source operand is located in an
7957 * execute-only code segment
7958 */
7959 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02007960 if (exn) {
7961 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
7962 return 1;
7963 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007964 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
7965 */
7966 exn = (s.unusable != 0);
7967 /* Protected mode: #GP(0)/#SS(0) if the memory
7968 * operand is outside the segment limit.
7969 */
7970 exn = exn || (off + sizeof(u64) > s.limit);
7971 }
7972 if (exn) {
7973 kvm_queue_exception_e(vcpu,
7974 seg_reg == VCPU_SREG_SS ?
7975 SS_VECTOR : GP_VECTOR,
7976 0);
7977 return 1;
7978 }
7979
Bandan Das19677e32014-05-06 02:19:15 -04007980 return 0;
7981}
7982
Radim Krčmářcbf71272017-05-19 15:48:51 +02007983static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04007984{
7985 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04007986 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04007987
7988 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007989 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007990 return 1;
7991
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02007992 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007993 kvm_inject_page_fault(vcpu, &e);
7994 return 1;
7995 }
7996
Bandan Das3573e222014-05-06 02:19:16 -04007997 return 0;
7998}
7999
Liran Alonabfc52c2018-06-23 02:35:13 +03008000/*
8001 * Allocate a shadow VMCS and associate it with the currently loaded
8002 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8003 * VMCS is also VMCLEARed, so that it is ready for use.
8004 */
8005static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8006{
8007 struct vcpu_vmx *vmx = to_vmx(vcpu);
8008 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8009
8010 /*
8011 * We should allocate a shadow vmcs for vmcs01 only when L1
8012 * executes VMXON and free it when L1 executes VMXOFF.
8013 * As it is invalid to execute VMXON twice, we shouldn't reach
8014 * here when vmcs01 already have an allocated shadow vmcs.
8015 */
8016 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8017
8018 if (!loaded_vmcs->shadow_vmcs) {
8019 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8020 if (loaded_vmcs->shadow_vmcs)
8021 vmcs_clear(loaded_vmcs->shadow_vmcs);
8022 }
8023 return loaded_vmcs->shadow_vmcs;
8024}
8025
Jim Mattsone29acc52016-11-30 12:03:43 -08008026static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8027{
8028 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008029 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008030
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008031 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8032 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008033 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008034
8035 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8036 if (!vmx->nested.cached_vmcs12)
8037 goto out_cached_vmcs12;
8038
Liran Alon61ada742018-06-23 02:35:08 +03008039 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8040 if (!vmx->nested.cached_shadow_vmcs12)
8041 goto out_cached_shadow_vmcs12;
8042
Liran Alonabfc52c2018-06-23 02:35:13 +03008043 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8044 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008045
Jim Mattsone29acc52016-11-30 12:03:43 -08008046 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8047 HRTIMER_MODE_REL_PINNED);
8048 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8049
8050 vmx->nested.vmxon = true;
8051 return 0;
8052
8053out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008054 kfree(vmx->nested.cached_shadow_vmcs12);
8055
8056out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008057 kfree(vmx->nested.cached_vmcs12);
8058
8059out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008060 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008061
Jim Mattsonde3a0022017-11-27 17:22:25 -06008062out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008063 return -ENOMEM;
8064}
8065
Bandan Das3573e222014-05-06 02:19:16 -04008066/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008067 * Emulate the VMXON instruction.
8068 * Currently, we just remember that VMX is active, and do not save or even
8069 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8070 * do not currently need to store anything in that guest-allocated memory
8071 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8072 * argument is different from the VMXON pointer (which the spec says they do).
8073 */
8074static int handle_vmon(struct kvm_vcpu *vcpu)
8075{
Jim Mattsone29acc52016-11-30 12:03:43 -08008076 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008077 gpa_t vmptr;
8078 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008079 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008080 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8081 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008082
Jim Mattson70f3aac2017-04-26 08:53:46 -07008083 /*
8084 * The Intel VMX Instruction Reference lists a bunch of bits that are
8085 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8086 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8087 * Otherwise, we should fail with #UD. But most faulting conditions
8088 * have already been checked by hardware, prior to the VM-exit for
8089 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8090 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008091 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008092 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008093 kvm_queue_exception(vcpu, UD_VECTOR);
8094 return 1;
8095 }
8096
Felix Wilhelm727ba742018-06-11 09:43:44 +02008097 /* CPL=0 must be checked manually. */
8098 if (vmx_get_cpl(vcpu)) {
8099 kvm_queue_exception(vcpu, UD_VECTOR);
8100 return 1;
8101 }
8102
Abel Gordon145c28d2013-04-18 14:36:55 +03008103 if (vmx->nested.vmxon) {
8104 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008105 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008106 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008107
Haozhong Zhang3b840802016-06-22 14:59:54 +08008108 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008109 != VMXON_NEEDED_FEATURES) {
8110 kvm_inject_gp(vcpu, 0);
8111 return 1;
8112 }
8113
Radim Krčmářcbf71272017-05-19 15:48:51 +02008114 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008115 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008116
8117 /*
8118 * SDM 3: 24.11.5
8119 * The first 4 bytes of VMXON region contain the supported
8120 * VMCS revision identifier
8121 *
8122 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8123 * which replaces physical address width with 32
8124 */
8125 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8126 nested_vmx_failInvalid(vcpu);
8127 return kvm_skip_emulated_instruction(vcpu);
8128 }
8129
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008130 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8131 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008132 nested_vmx_failInvalid(vcpu);
8133 return kvm_skip_emulated_instruction(vcpu);
8134 }
8135 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8136 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008137 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008138 nested_vmx_failInvalid(vcpu);
8139 return kvm_skip_emulated_instruction(vcpu);
8140 }
8141 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008142 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008143
8144 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008145 ret = enter_vmx_operation(vcpu);
8146 if (ret)
8147 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008148
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008149 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008150 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008151}
8152
8153/*
8154 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8155 * for running VMX instructions (except VMXON, whose prerequisites are
8156 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008157 * Note that many of these exceptions have priority over VM exits, so they
8158 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008159 */
8160static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8161{
Felix Wilhelm727ba742018-06-11 09:43:44 +02008162 if (vmx_get_cpl(vcpu)) {
8163 kvm_queue_exception(vcpu, UD_VECTOR);
8164 return 0;
8165 }
8166
Jim Mattson70f3aac2017-04-26 08:53:46 -07008167 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008168 kvm_queue_exception(vcpu, UD_VECTOR);
8169 return 0;
8170 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008171 return 1;
8172}
8173
David Matlack8ca44e82017-08-01 14:00:39 -07008174static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8175{
8176 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8177 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8178}
8179
Abel Gordone7953d72013-04-18 14:37:55 +03008180static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8181{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008182 if (vmx->nested.current_vmptr == -1ull)
8183 return;
8184
Abel Gordon012f83c2013-04-18 14:39:25 +03008185 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008186 /* copy to memory all shadowed fields in case
8187 they were modified */
8188 copy_shadow_to_vmcs12(vmx);
8189 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008190 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008191 }
Wincy Van705699a2015-02-03 23:58:17 +08008192 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008193
8194 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008195 kvm_vcpu_write_guest_page(&vmx->vcpu,
8196 vmx->nested.current_vmptr >> PAGE_SHIFT,
8197 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008198
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008199 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008200}
8201
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008202/*
8203 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8204 * just stops using VMX.
8205 */
8206static void free_nested(struct vcpu_vmx *vmx)
8207{
Wanpeng Lib7455822017-11-22 14:04:00 -08008208 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008209 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008210
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008211 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008212 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008213 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008214 vmx->nested.posted_intr_nv = -1;
8215 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008216 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008217 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008218 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8219 free_vmcs(vmx->vmcs01.shadow_vmcs);
8220 vmx->vmcs01.shadow_vmcs = NULL;
8221 }
David Matlack4f2777b2016-07-13 17:16:37 -07008222 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008223 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008224 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008225 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008226 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008227 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008228 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008229 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008230 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008231 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008232 }
Wincy Van705699a2015-02-03 23:58:17 +08008233 if (vmx->nested.pi_desc_page) {
8234 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008235 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008236 vmx->nested.pi_desc_page = NULL;
8237 vmx->nested.pi_desc = NULL;
8238 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008239
Jim Mattsonde3a0022017-11-27 17:22:25 -06008240 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008241}
8242
8243/* Emulate the VMXOFF instruction */
8244static int handle_vmoff(struct kvm_vcpu *vcpu)
8245{
8246 if (!nested_vmx_check_permission(vcpu))
8247 return 1;
8248 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008249 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008250 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008251}
8252
Nadav Har'El27d6c862011-05-25 23:06:59 +03008253/* Emulate the VMCLEAR instruction */
8254static int handle_vmclear(struct kvm_vcpu *vcpu)
8255{
8256 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008257 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008258 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008259
8260 if (!nested_vmx_check_permission(vcpu))
8261 return 1;
8262
Radim Krčmářcbf71272017-05-19 15:48:51 +02008263 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008264 return 1;
8265
Radim Krčmářcbf71272017-05-19 15:48:51 +02008266 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8267 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8268 return kvm_skip_emulated_instruction(vcpu);
8269 }
8270
8271 if (vmptr == vmx->nested.vmxon_ptr) {
8272 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8273 return kvm_skip_emulated_instruction(vcpu);
8274 }
8275
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008276 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008277 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008278
Jim Mattson587d7e722017-03-02 12:41:48 -08008279 kvm_vcpu_write_guest(vcpu,
8280 vmptr + offsetof(struct vmcs12, launch_state),
8281 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008282
Nadav Har'El27d6c862011-05-25 23:06:59 +03008283 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008284 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008285}
8286
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008287static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8288
8289/* Emulate the VMLAUNCH instruction */
8290static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8291{
8292 return nested_vmx_run(vcpu, true);
8293}
8294
8295/* Emulate the VMRESUME instruction */
8296static int handle_vmresume(struct kvm_vcpu *vcpu)
8297{
8298
8299 return nested_vmx_run(vcpu, false);
8300}
8301
Nadav Har'El49f705c2011-05-25 23:08:30 +03008302/*
8303 * Read a vmcs12 field. Since these can have varying lengths and we return
8304 * one type, we chose the biggest type (u64) and zero-extend the return value
8305 * to that size. Note that the caller, handle_vmread, might need to use only
8306 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8307 * 64-bit fields are to be returned).
8308 */
Liran Alone2536742018-06-23 02:35:02 +03008309static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008310 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008311{
8312 short offset = vmcs_field_to_offset(field);
8313 char *p;
8314
8315 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008316 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008317
Liran Alone2536742018-06-23 02:35:02 +03008318 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008319
Jim Mattsond37f4262017-12-22 12:12:16 -08008320 switch (vmcs_field_width(field)) {
8321 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008322 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008323 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008324 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008325 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008326 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008327 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008328 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008329 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008330 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008331 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008332 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008333 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008334 WARN_ON(1);
8335 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008336 }
8337}
8338
Abel Gordon20b97fe2013-04-18 14:36:25 +03008339
Liran Alone2536742018-06-23 02:35:02 +03008340static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008341 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008342 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008343 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008344 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008345 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008346
Jim Mattsond37f4262017-12-22 12:12:16 -08008347 switch (vmcs_field_width(field)) {
8348 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008349 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008350 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008351 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008352 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008353 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008354 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008355 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008356 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008357 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008358 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008359 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008360 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008361 WARN_ON(1);
8362 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008363 }
8364
8365}
8366
Jim Mattsonf4160e42018-05-29 09:11:33 -07008367/*
8368 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8369 * they have been modified by the L1 guest. Note that the "read-only"
8370 * VM-exit information fields are actually writable if the vCPU is
8371 * configured to support "VMWRITE to any supported field in the VMCS."
8372 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008373static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8374{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008375 const u16 *fields[] = {
8376 shadow_read_write_fields,
8377 shadow_read_only_fields
8378 };
8379 const int max_fields[] = {
8380 max_shadow_read_write_fields,
8381 max_shadow_read_only_fields
8382 };
8383 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008384 unsigned long field;
8385 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008386 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008387
Jan Kiszka282da872014-10-08 18:05:39 +02008388 preempt_disable();
8389
Abel Gordon16f5b902013-04-18 14:38:25 +03008390 vmcs_load(shadow_vmcs);
8391
Jim Mattsonf4160e42018-05-29 09:11:33 -07008392 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8393 for (i = 0; i < max_fields[q]; i++) {
8394 field = fields[q][i];
8395 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008396 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008397 }
8398 /*
8399 * Skip the VM-exit information fields if they are read-only.
8400 */
8401 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8402 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008403 }
8404
8405 vmcs_clear(shadow_vmcs);
8406 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008407
8408 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008409}
8410
Abel Gordonc3114422013-04-18 14:38:55 +03008411static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8412{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008413 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008414 shadow_read_write_fields,
8415 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008416 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008417 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008418 max_shadow_read_write_fields,
8419 max_shadow_read_only_fields
8420 };
8421 int i, q;
8422 unsigned long field;
8423 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008424 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008425
8426 vmcs_load(shadow_vmcs);
8427
Mathias Krausec2bae892013-06-26 20:36:21 +02008428 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008429 for (i = 0; i < max_fields[q]; i++) {
8430 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008431 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008432 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008433 }
8434 }
8435
8436 vmcs_clear(shadow_vmcs);
8437 vmcs_load(vmx->loaded_vmcs->vmcs);
8438}
8439
Nadav Har'El49f705c2011-05-25 23:08:30 +03008440/*
8441 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8442 * used before) all generate the same failure when it is missing.
8443 */
8444static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8445{
8446 struct vcpu_vmx *vmx = to_vmx(vcpu);
8447 if (vmx->nested.current_vmptr == -1ull) {
8448 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008449 return 0;
8450 }
8451 return 1;
8452}
8453
8454static int handle_vmread(struct kvm_vcpu *vcpu)
8455{
8456 unsigned long field;
8457 u64 field_value;
8458 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8459 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8460 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008461 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008462
Kyle Hueyeb277562016-11-29 12:40:39 -08008463 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008464 return 1;
8465
Kyle Huey6affcbe2016-11-29 12:40:40 -08008466 if (!nested_vmx_check_vmcs12(vcpu))
8467 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008468
Liran Alon6d894f42018-06-23 02:35:09 +03008469 if (!is_guest_mode(vcpu))
8470 vmcs12 = get_vmcs12(vcpu);
8471 else {
8472 /*
8473 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8474 * to shadowed-field sets the ALU flags for VMfailInvalid.
8475 */
8476 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8477 nested_vmx_failInvalid(vcpu);
8478 return kvm_skip_emulated_instruction(vcpu);
8479 }
8480 vmcs12 = get_shadow_vmcs12(vcpu);
8481 }
8482
Nadav Har'El49f705c2011-05-25 23:08:30 +03008483 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008484 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008485 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008486 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008487 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008488 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008489 }
8490 /*
8491 * Now copy part of this value to register or memory, as requested.
8492 * Note that the number of bits actually copied is 32 or 64 depending
8493 * on the guest's mode (32 or 64 bit), not on the given field's length.
8494 */
8495 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008496 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008497 field_value);
8498 } else {
8499 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008500 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008501 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008502 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008503 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8504 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008505 }
8506
8507 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008508 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008509}
8510
8511
8512static int handle_vmwrite(struct kvm_vcpu *vcpu)
8513{
8514 unsigned long field;
8515 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008516 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008517 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8518 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008519
Nadav Har'El49f705c2011-05-25 23:08:30 +03008520 /* The value to write might be 32 or 64 bits, depending on L1's long
8521 * mode, and eventually we need to write that into a field of several
8522 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008523 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008524 * bits into the vmcs12 field.
8525 */
8526 u64 field_value = 0;
8527 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008528 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008529
Kyle Hueyeb277562016-11-29 12:40:39 -08008530 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008531 return 1;
8532
Kyle Huey6affcbe2016-11-29 12:40:40 -08008533 if (!nested_vmx_check_vmcs12(vcpu))
8534 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008535
Nadav Har'El49f705c2011-05-25 23:08:30 +03008536 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008537 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008538 (((vmx_instruction_info) >> 3) & 0xf));
8539 else {
8540 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008541 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008542 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008543 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8544 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008545 kvm_inject_page_fault(vcpu, &e);
8546 return 1;
8547 }
8548 }
8549
8550
Nadav Amit27e6fb52014-06-18 17:19:26 +03008551 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008552 /*
8553 * If the vCPU supports "VMWRITE to any supported field in the
8554 * VMCS," then the "read-only" fields are actually read/write.
8555 */
8556 if (vmcs_field_readonly(field) &&
8557 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008558 nested_vmx_failValid(vcpu,
8559 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008560 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008561 }
8562
Liran Alon6d894f42018-06-23 02:35:09 +03008563 if (!is_guest_mode(vcpu))
8564 vmcs12 = get_vmcs12(vcpu);
8565 else {
8566 /*
8567 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8568 * to shadowed-field sets the ALU flags for VMfailInvalid.
8569 */
8570 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8571 nested_vmx_failInvalid(vcpu);
8572 return kvm_skip_emulated_instruction(vcpu);
8573 }
8574 vmcs12 = get_shadow_vmcs12(vcpu);
8575
8576 }
8577
8578 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008579 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008580 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008581 }
8582
Liran Alon6d894f42018-06-23 02:35:09 +03008583 /*
8584 * Do not track vmcs12 dirty-state if in guest-mode
8585 * as we actually dirty shadow vmcs12 instead of vmcs12.
8586 */
8587 if (!is_guest_mode(vcpu)) {
8588 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008589#define SHADOW_FIELD_RW(x) case x:
8590#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008591 /*
8592 * The fields that can be updated by L1 without a vmexit are
8593 * always updated in the vmcs02, the others go down the slow
8594 * path of prepare_vmcs02.
8595 */
8596 break;
8597 default:
8598 vmx->nested.dirty_vmcs12 = true;
8599 break;
8600 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008601 }
8602
Nadav Har'El49f705c2011-05-25 23:08:30 +03008603 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008604 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008605}
8606
Jim Mattsona8bc2842016-11-30 12:03:44 -08008607static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8608{
8609 vmx->nested.current_vmptr = vmptr;
8610 if (enable_shadow_vmcs) {
8611 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8612 SECONDARY_EXEC_SHADOW_VMCS);
8613 vmcs_write64(VMCS_LINK_POINTER,
8614 __pa(vmx->vmcs01.shadow_vmcs));
8615 vmx->nested.sync_shadow_vmcs = true;
8616 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008617 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008618}
8619
Nadav Har'El63846662011-05-25 23:07:29 +03008620/* Emulate the VMPTRLD instruction */
8621static int handle_vmptrld(struct kvm_vcpu *vcpu)
8622{
8623 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008624 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008625
8626 if (!nested_vmx_check_permission(vcpu))
8627 return 1;
8628
Radim Krčmářcbf71272017-05-19 15:48:51 +02008629 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008630 return 1;
8631
Radim Krčmářcbf71272017-05-19 15:48:51 +02008632 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8633 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8634 return kvm_skip_emulated_instruction(vcpu);
8635 }
8636
8637 if (vmptr == vmx->nested.vmxon_ptr) {
8638 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8639 return kvm_skip_emulated_instruction(vcpu);
8640 }
8641
Nadav Har'El63846662011-05-25 23:07:29 +03008642 if (vmx->nested.current_vmptr != vmptr) {
8643 struct vmcs12 *new_vmcs12;
8644 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008645 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8646 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008647 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008648 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008649 }
8650 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008651 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008652 (new_vmcs12->hdr.shadow_vmcs &&
8653 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008654 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008655 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008656 nested_vmx_failValid(vcpu,
8657 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008658 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008659 }
Nadav Har'El63846662011-05-25 23:07:29 +03008660
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008661 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008662 /*
8663 * Load VMCS12 from guest memory since it is not already
8664 * cached.
8665 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008666 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8667 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008668 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008669
Jim Mattsona8bc2842016-11-30 12:03:44 -08008670 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008671 }
8672
8673 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008674 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008675}
8676
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008677/* Emulate the VMPTRST instruction */
8678static int handle_vmptrst(struct kvm_vcpu *vcpu)
8679{
8680 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8681 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8682 gva_t vmcs_gva;
8683 struct x86_exception e;
8684
8685 if (!nested_vmx_check_permission(vcpu))
8686 return 1;
8687
8688 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008689 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008690 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008691 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008692 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
8693 (void *)&to_vmx(vcpu)->nested.current_vmptr,
8694 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008695 kvm_inject_page_fault(vcpu, &e);
8696 return 1;
8697 }
8698 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008699 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008700}
8701
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008702/* Emulate the INVEPT instruction */
8703static int handle_invept(struct kvm_vcpu *vcpu)
8704{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008705 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008706 u32 vmx_instruction_info, types;
8707 unsigned long type;
8708 gva_t gva;
8709 struct x86_exception e;
8710 struct {
8711 u64 eptp, gpa;
8712 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008713
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008714 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008715 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008716 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008717 kvm_queue_exception(vcpu, UD_VECTOR);
8718 return 1;
8719 }
8720
8721 if (!nested_vmx_check_permission(vcpu))
8722 return 1;
8723
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008724 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008725 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008726
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008727 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008728
Jim Mattson85c856b2016-10-26 08:38:38 -07008729 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008730 nested_vmx_failValid(vcpu,
8731 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008732 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008733 }
8734
8735 /* According to the Intel VMX instruction reference, the memory
8736 * operand is read even if it isn't needed (e.g., for type==global)
8737 */
8738 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008739 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008740 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008741 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008742 kvm_inject_page_fault(vcpu, &e);
8743 return 1;
8744 }
8745
8746 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008747 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008748 /*
8749 * TODO: track mappings and invalidate
8750 * single context requests appropriately
8751 */
8752 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008753 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008754 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008755 nested_vmx_succeed(vcpu);
8756 break;
8757 default:
8758 BUG_ON(1);
8759 break;
8760 }
8761
Kyle Huey6affcbe2016-11-29 12:40:40 -08008762 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008763}
8764
Petr Matouseka642fc32014-09-23 20:22:30 +02008765static int handle_invvpid(struct kvm_vcpu *vcpu)
8766{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008767 struct vcpu_vmx *vmx = to_vmx(vcpu);
8768 u32 vmx_instruction_info;
8769 unsigned long type, types;
8770 gva_t gva;
8771 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008772 struct {
8773 u64 vpid;
8774 u64 gla;
8775 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008776
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008777 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008778 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008779 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008780 kvm_queue_exception(vcpu, UD_VECTOR);
8781 return 1;
8782 }
8783
8784 if (!nested_vmx_check_permission(vcpu))
8785 return 1;
8786
8787 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8788 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8789
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008790 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008791 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008792
Jim Mattson85c856b2016-10-26 08:38:38 -07008793 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008794 nested_vmx_failValid(vcpu,
8795 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008796 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008797 }
8798
8799 /* according to the intel vmx instruction reference, the memory
8800 * operand is read even if it isn't needed (e.g., for type==global)
8801 */
8802 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8803 vmx_instruction_info, false, &gva))
8804 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008805 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008806 kvm_inject_page_fault(vcpu, &e);
8807 return 1;
8808 }
Jim Mattson40352602017-06-28 09:37:37 -07008809 if (operand.vpid >> 16) {
8810 nested_vmx_failValid(vcpu,
8811 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8812 return kvm_skip_emulated_instruction(vcpu);
8813 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008814
8815 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008816 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03008817 if (!operand.vpid ||
8818 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07008819 nested_vmx_failValid(vcpu,
8820 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
8821 return kvm_skip_emulated_instruction(vcpu);
8822 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008823 if (cpu_has_vmx_invvpid_individual_addr() &&
8824 vmx->nested.vpid02) {
8825 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
8826 vmx->nested.vpid02, operand.gla);
8827 } else
8828 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
8829 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01008830 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008831 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07008832 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008833 nested_vmx_failValid(vcpu,
8834 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008835 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008836 }
Liran Aloncd9a4912018-05-22 17:16:15 +03008837 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008838 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008839 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03008840 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008841 break;
8842 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008843 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008844 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008845 }
8846
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008847 nested_vmx_succeed(vcpu);
8848
Kyle Huey6affcbe2016-11-29 12:40:40 -08008849 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02008850}
8851
Junaid Shahideb4b2482018-06-27 14:59:14 -07008852static int handle_invpcid(struct kvm_vcpu *vcpu)
8853{
8854 u32 vmx_instruction_info;
8855 unsigned long type;
8856 bool pcid_enabled;
8857 gva_t gva;
8858 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07008859 unsigned i;
8860 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07008861 struct {
8862 u64 pcid;
8863 u64 gla;
8864 } operand;
8865
8866 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
8867 kvm_queue_exception(vcpu, UD_VECTOR);
8868 return 1;
8869 }
8870
8871 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8872 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8873
8874 if (type > 3) {
8875 kvm_inject_gp(vcpu, 0);
8876 return 1;
8877 }
8878
8879 /* According to the Intel instruction reference, the memory operand
8880 * is read even if it isn't needed (e.g., for type==all)
8881 */
8882 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
8883 vmx_instruction_info, false, &gva))
8884 return 1;
8885
8886 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
8887 kvm_inject_page_fault(vcpu, &e);
8888 return 1;
8889 }
8890
8891 if (operand.pcid >> 12 != 0) {
8892 kvm_inject_gp(vcpu, 0);
8893 return 1;
8894 }
8895
8896 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
8897
8898 switch (type) {
8899 case INVPCID_TYPE_INDIV_ADDR:
8900 if ((!pcid_enabled && (operand.pcid != 0)) ||
8901 is_noncanonical_address(operand.gla, vcpu)) {
8902 kvm_inject_gp(vcpu, 0);
8903 return 1;
8904 }
8905 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
8906 return kvm_skip_emulated_instruction(vcpu);
8907
8908 case INVPCID_TYPE_SINGLE_CTXT:
8909 if (!pcid_enabled && (operand.pcid != 0)) {
8910 kvm_inject_gp(vcpu, 0);
8911 return 1;
8912 }
8913
8914 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
8915 kvm_mmu_sync_roots(vcpu);
8916 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
8917 }
8918
Junaid Shahidb94742c2018-06-27 14:59:20 -07008919 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
8920 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
8921 == operand.pcid)
8922 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07008923
Junaid Shahidb94742c2018-06-27 14:59:20 -07008924 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07008925 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07008926 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07008927 * given PCID, then nothing needs to be done here because a
8928 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07008929 */
8930
8931 return kvm_skip_emulated_instruction(vcpu);
8932
8933 case INVPCID_TYPE_ALL_NON_GLOBAL:
8934 /*
8935 * Currently, KVM doesn't mark global entries in the shadow
8936 * page tables, so a non-global flush just degenerates to a
8937 * global flush. If needed, we could optimize this later by
8938 * keeping track of global entries in shadow page tables.
8939 */
8940
8941 /* fall-through */
8942 case INVPCID_TYPE_ALL_INCL_GLOBAL:
8943 kvm_mmu_unload(vcpu);
8944 return kvm_skip_emulated_instruction(vcpu);
8945
8946 default:
8947 BUG(); /* We have already checked above that type <= 3 */
8948 }
8949}
8950
Kai Huang843e4332015-01-28 10:54:28 +08008951static int handle_pml_full(struct kvm_vcpu *vcpu)
8952{
8953 unsigned long exit_qualification;
8954
8955 trace_kvm_pml_full(vcpu->vcpu_id);
8956
8957 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8958
8959 /*
8960 * PML buffer FULL happened while executing iret from NMI,
8961 * "blocked by NMI" bit has to be set before next VM entry.
8962 */
8963 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01008964 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08008965 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
8966 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8967 GUEST_INTR_STATE_NMI);
8968
8969 /*
8970 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
8971 * here.., and there's no userspace involvement needed for PML.
8972 */
8973 return 1;
8974}
8975
Yunhong Jiang64672c92016-06-13 14:19:59 -07008976static int handle_preemption_timer(struct kvm_vcpu *vcpu)
8977{
8978 kvm_lapic_expired_hv_timer(vcpu);
8979 return 1;
8980}
8981
Bandan Das41ab9372017-08-03 15:54:43 -04008982static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
8983{
8984 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04008985 int maxphyaddr = cpuid_maxphyaddr(vcpu);
8986
8987 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02008988 switch (address & VMX_EPTP_MT_MASK) {
8989 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008990 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008991 return false;
8992 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02008993 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008994 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04008995 return false;
8996 break;
8997 default:
8998 return false;
8999 }
9000
David Hildenbrandbb97a012017-08-10 23:15:28 +02009001 /* only 4 levels page-walk length are valid */
9002 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009003 return false;
9004
9005 /* Reserved bits should not be set */
9006 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9007 return false;
9008
9009 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009010 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009011 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009012 return false;
9013 }
9014
9015 return true;
9016}
9017
9018static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9019 struct vmcs12 *vmcs12)
9020{
9021 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9022 u64 address;
9023 bool accessed_dirty;
9024 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9025
9026 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9027 !nested_cpu_has_ept(vmcs12))
9028 return 1;
9029
9030 if (index >= VMFUNC_EPTP_ENTRIES)
9031 return 1;
9032
9033
9034 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9035 &address, index * 8, 8))
9036 return 1;
9037
David Hildenbrandbb97a012017-08-10 23:15:28 +02009038 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009039
9040 /*
9041 * If the (L2) guest does a vmfunc to the currently
9042 * active ept pointer, we don't have to do anything else
9043 */
9044 if (vmcs12->ept_pointer != address) {
9045 if (!valid_ept_address(vcpu, address))
9046 return 1;
9047
9048 kvm_mmu_unload(vcpu);
9049 mmu->ept_ad = accessed_dirty;
9050 mmu->base_role.ad_disabled = !accessed_dirty;
9051 vmcs12->ept_pointer = address;
9052 /*
9053 * TODO: Check what's the correct approach in case
9054 * mmu reload fails. Currently, we just let the next
9055 * reload potentially fail
9056 */
9057 kvm_mmu_reload(vcpu);
9058 }
9059
9060 return 0;
9061}
9062
Bandan Das2a499e42017-08-03 15:54:41 -04009063static int handle_vmfunc(struct kvm_vcpu *vcpu)
9064{
Bandan Das27c42a12017-08-03 15:54:42 -04009065 struct vcpu_vmx *vmx = to_vmx(vcpu);
9066 struct vmcs12 *vmcs12;
9067 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9068
9069 /*
9070 * VMFUNC is only supported for nested guests, but we always enable the
9071 * secondary control for simplicity; for non-nested mode, fake that we
9072 * didn't by injecting #UD.
9073 */
9074 if (!is_guest_mode(vcpu)) {
9075 kvm_queue_exception(vcpu, UD_VECTOR);
9076 return 1;
9077 }
9078
9079 vmcs12 = get_vmcs12(vcpu);
9080 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9081 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009082
9083 switch (function) {
9084 case 0:
9085 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9086 goto fail;
9087 break;
9088 default:
9089 goto fail;
9090 }
9091 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009092
9093fail:
9094 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9095 vmcs_read32(VM_EXIT_INTR_INFO),
9096 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009097 return 1;
9098}
9099
Nadav Har'El0140cae2011-05-25 23:06:28 +03009100/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009101 * The exit handlers return 1 if the exit was handled fully and guest execution
9102 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9103 * to be done to userspace and return 0.
9104 */
Mathias Krause772e0312012-08-30 01:30:19 +02009105static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009106 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9107 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009108 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009109 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009110 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009111 [EXIT_REASON_CR_ACCESS] = handle_cr,
9112 [EXIT_REASON_DR_ACCESS] = handle_dr,
9113 [EXIT_REASON_CPUID] = handle_cpuid,
9114 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9115 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9116 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9117 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009118 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009119 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009120 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009121 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009122 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009123 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009124 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009125 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009126 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009127 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009128 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009129 [EXIT_REASON_VMOFF] = handle_vmoff,
9130 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009131 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9132 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009133 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009134 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009135 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009136 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009137 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009138 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009139 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9140 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009141 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9142 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009143 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009144 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009145 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009146 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009147 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009148 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009149 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009150 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009151 [EXIT_REASON_XSAVES] = handle_xsaves,
9152 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009153 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009154 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009155 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009156 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009157};
9158
9159static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009160 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009161
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009162static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9163 struct vmcs12 *vmcs12)
9164{
9165 unsigned long exit_qualification;
9166 gpa_t bitmap, last_bitmap;
9167 unsigned int port;
9168 int size;
9169 u8 b;
9170
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009171 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009172 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009173
9174 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9175
9176 port = exit_qualification >> 16;
9177 size = (exit_qualification & 7) + 1;
9178
9179 last_bitmap = (gpa_t)-1;
9180 b = -1;
9181
9182 while (size > 0) {
9183 if (port < 0x8000)
9184 bitmap = vmcs12->io_bitmap_a;
9185 else if (port < 0x10000)
9186 bitmap = vmcs12->io_bitmap_b;
9187 else
Joe Perches1d804d02015-03-30 16:46:09 -07009188 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009189 bitmap += (port & 0x7fff) / 8;
9190
9191 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009192 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009193 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009194 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009195 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009196
9197 port++;
9198 size--;
9199 last_bitmap = bitmap;
9200 }
9201
Joe Perches1d804d02015-03-30 16:46:09 -07009202 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009203}
9204
Nadav Har'El644d7112011-05-25 23:12:35 +03009205/*
9206 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9207 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9208 * disinterest in the current event (read or write a specific MSR) by using an
9209 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9210 */
9211static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9212 struct vmcs12 *vmcs12, u32 exit_reason)
9213{
9214 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9215 gpa_t bitmap;
9216
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009217 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009218 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009219
9220 /*
9221 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9222 * for the four combinations of read/write and low/high MSR numbers.
9223 * First we need to figure out which of the four to use:
9224 */
9225 bitmap = vmcs12->msr_bitmap;
9226 if (exit_reason == EXIT_REASON_MSR_WRITE)
9227 bitmap += 2048;
9228 if (msr_index >= 0xc0000000) {
9229 msr_index -= 0xc0000000;
9230 bitmap += 1024;
9231 }
9232
9233 /* Then read the msr_index'th bit from this bitmap: */
9234 if (msr_index < 1024*8) {
9235 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009236 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009237 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009238 return 1 & (b >> (msr_index & 7));
9239 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009240 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009241}
9242
9243/*
9244 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9245 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9246 * intercept (via guest_host_mask etc.) the current event.
9247 */
9248static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9249 struct vmcs12 *vmcs12)
9250{
9251 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9252 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009253 int reg;
9254 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009255
9256 switch ((exit_qualification >> 4) & 3) {
9257 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009258 reg = (exit_qualification >> 8) & 15;
9259 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009260 switch (cr) {
9261 case 0:
9262 if (vmcs12->cr0_guest_host_mask &
9263 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009264 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009265 break;
9266 case 3:
9267 if ((vmcs12->cr3_target_count >= 1 &&
9268 vmcs12->cr3_target_value0 == val) ||
9269 (vmcs12->cr3_target_count >= 2 &&
9270 vmcs12->cr3_target_value1 == val) ||
9271 (vmcs12->cr3_target_count >= 3 &&
9272 vmcs12->cr3_target_value2 == val) ||
9273 (vmcs12->cr3_target_count >= 4 &&
9274 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009275 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009276 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009277 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009278 break;
9279 case 4:
9280 if (vmcs12->cr4_guest_host_mask &
9281 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009282 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009283 break;
9284 case 8:
9285 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009286 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009287 break;
9288 }
9289 break;
9290 case 2: /* clts */
9291 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9292 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009293 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009294 break;
9295 case 1: /* mov from cr */
9296 switch (cr) {
9297 case 3:
9298 if (vmcs12->cpu_based_vm_exec_control &
9299 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009300 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009301 break;
9302 case 8:
9303 if (vmcs12->cpu_based_vm_exec_control &
9304 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009305 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009306 break;
9307 }
9308 break;
9309 case 3: /* lmsw */
9310 /*
9311 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9312 * cr0. Other attempted changes are ignored, with no exit.
9313 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009314 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009315 if (vmcs12->cr0_guest_host_mask & 0xe &
9316 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009317 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009318 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9319 !(vmcs12->cr0_read_shadow & 0x1) &&
9320 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009321 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009322 break;
9323 }
Joe Perches1d804d02015-03-30 16:46:09 -07009324 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009325}
9326
Liran Alona7cde482018-06-23 02:35:10 +03009327static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9328 struct vmcs12 *vmcs12, gpa_t bitmap)
9329{
9330 u32 vmx_instruction_info;
9331 unsigned long field;
9332 u8 b;
9333
9334 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9335 return true;
9336
9337 /* Decode instruction info and find the field to access */
9338 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9339 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9340
9341 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9342 if (field >> 15)
9343 return true;
9344
9345 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9346 return true;
9347
9348 return 1 & (b >> (field & 7));
9349}
9350
Nadav Har'El644d7112011-05-25 23:12:35 +03009351/*
9352 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9353 * should handle it ourselves in L0 (and then continue L2). Only call this
9354 * when in is_guest_mode (L2).
9355 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009356static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009357{
Nadav Har'El644d7112011-05-25 23:12:35 +03009358 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9359 struct vcpu_vmx *vmx = to_vmx(vcpu);
9360 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9361
Jim Mattson4f350c62017-09-14 16:31:44 -07009362 if (vmx->nested.nested_run_pending)
9363 return false;
9364
9365 if (unlikely(vmx->fail)) {
9366 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9367 vmcs_read32(VM_INSTRUCTION_ERROR));
9368 return true;
9369 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009370
David Matlackc9f04402017-08-01 14:00:40 -07009371 /*
9372 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009373 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9374 * Page). The CPU may write to these pages via their host
9375 * physical address while L2 is running, bypassing any
9376 * address-translation-based dirty tracking (e.g. EPT write
9377 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009378 *
9379 * Mark them dirty on every exit from L2 to prevent them from
9380 * getting out of sync with dirty tracking.
9381 */
9382 nested_mark_vmcs12_pages_dirty(vcpu);
9383
Jim Mattson4f350c62017-09-14 16:31:44 -07009384 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9385 vmcs_readl(EXIT_QUALIFICATION),
9386 vmx->idt_vectoring_info,
9387 intr_info,
9388 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9389 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009390
9391 switch (exit_reason) {
9392 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009393 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009394 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009395 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009396 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009397 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009398 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009399 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009400 else if (is_debug(intr_info) &&
9401 vcpu->guest_debug &
9402 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9403 return false;
9404 else if (is_breakpoint(intr_info) &&
9405 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9406 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009407 return vmcs12->exception_bitmap &
9408 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9409 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009410 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009411 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009412 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009413 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009414 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009415 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009416 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009417 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009418 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009419 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009420 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009421 case EXIT_REASON_HLT:
9422 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9423 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009424 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009425 case EXIT_REASON_INVLPG:
9426 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9427 case EXIT_REASON_RDPMC:
9428 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009429 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009430 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009431 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009432 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009433 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009434 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009435 case EXIT_REASON_VMREAD:
9436 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9437 vmcs12->vmread_bitmap);
9438 case EXIT_REASON_VMWRITE:
9439 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9440 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009441 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9442 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009443 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009444 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009445 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009446 /*
9447 * VMX instructions trap unconditionally. This allows L1 to
9448 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9449 */
Joe Perches1d804d02015-03-30 16:46:09 -07009450 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009451 case EXIT_REASON_CR_ACCESS:
9452 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9453 case EXIT_REASON_DR_ACCESS:
9454 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9455 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009456 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009457 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9458 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009459 case EXIT_REASON_MSR_READ:
9460 case EXIT_REASON_MSR_WRITE:
9461 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9462 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009463 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009464 case EXIT_REASON_MWAIT_INSTRUCTION:
9465 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009466 case EXIT_REASON_MONITOR_TRAP_FLAG:
9467 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009468 case EXIT_REASON_MONITOR_INSTRUCTION:
9469 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9470 case EXIT_REASON_PAUSE_INSTRUCTION:
9471 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9472 nested_cpu_has2(vmcs12,
9473 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9474 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009475 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009476 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009477 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009478 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009479 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009480 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009481 /*
9482 * The controls for "virtualize APIC accesses," "APIC-
9483 * register virtualization," and "virtual-interrupt
9484 * delivery" only come from vmcs12.
9485 */
Joe Perches1d804d02015-03-30 16:46:09 -07009486 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009487 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009488 /*
9489 * L0 always deals with the EPT violation. If nested EPT is
9490 * used, and the nested mmu code discovers that the address is
9491 * missing in the guest EPT table (EPT12), the EPT violation
9492 * will be injected with nested_ept_inject_page_fault()
9493 */
Joe Perches1d804d02015-03-30 16:46:09 -07009494 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009495 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009496 /*
9497 * L2 never uses directly L1's EPT, but rather L0's own EPT
9498 * table (shadow on EPT) or a merged EPT table that L0 built
9499 * (EPT on EPT). So any problems with the structure of the
9500 * table is L0's fault.
9501 */
Joe Perches1d804d02015-03-30 16:46:09 -07009502 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009503 case EXIT_REASON_INVPCID:
9504 return
9505 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9506 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009507 case EXIT_REASON_WBINVD:
9508 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9509 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009510 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009511 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9512 /*
9513 * This should never happen, since it is not possible to
9514 * set XSS to a non-zero value---neither in L1 nor in L2.
9515 * If if it were, XSS would have to be checked against
9516 * the XSS exit bitmap in vmcs12.
9517 */
9518 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009519 case EXIT_REASON_PREEMPTION_TIMER:
9520 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009521 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009522 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009523 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009524 case EXIT_REASON_VMFUNC:
9525 /* VM functions are emulated through L2->L0 vmexits. */
9526 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009527 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009528 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009529 }
9530}
9531
Paolo Bonzini7313c692017-07-27 10:31:25 +02009532static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9533{
9534 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9535
9536 /*
9537 * At this point, the exit interruption info in exit_intr_info
9538 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9539 * we need to query the in-kernel LAPIC.
9540 */
9541 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9542 if ((exit_intr_info &
9543 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9544 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9545 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9546 vmcs12->vm_exit_intr_error_code =
9547 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9548 }
9549
9550 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9551 vmcs_readl(EXIT_QUALIFICATION));
9552 return 1;
9553}
9554
Avi Kivity586f9602010-11-18 13:09:54 +02009555static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9556{
9557 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9558 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9559}
9560
Kai Huanga3eaa862015-11-04 13:46:05 +08009561static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009562{
Kai Huanga3eaa862015-11-04 13:46:05 +08009563 if (vmx->pml_pg) {
9564 __free_page(vmx->pml_pg);
9565 vmx->pml_pg = NULL;
9566 }
Kai Huang843e4332015-01-28 10:54:28 +08009567}
9568
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009569static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009570{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009571 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009572 u64 *pml_buf;
9573 u16 pml_idx;
9574
9575 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9576
9577 /* Do nothing if PML buffer is empty */
9578 if (pml_idx == (PML_ENTITY_NUM - 1))
9579 return;
9580
9581 /* PML index always points to next available PML buffer entity */
9582 if (pml_idx >= PML_ENTITY_NUM)
9583 pml_idx = 0;
9584 else
9585 pml_idx++;
9586
9587 pml_buf = page_address(vmx->pml_pg);
9588 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9589 u64 gpa;
9590
9591 gpa = pml_buf[pml_idx];
9592 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009593 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009594 }
9595
9596 /* reset PML index */
9597 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9598}
9599
9600/*
9601 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9602 * Called before reporting dirty_bitmap to userspace.
9603 */
9604static void kvm_flush_pml_buffers(struct kvm *kvm)
9605{
9606 int i;
9607 struct kvm_vcpu *vcpu;
9608 /*
9609 * We only need to kick vcpu out of guest mode here, as PML buffer
9610 * is flushed at beginning of all VMEXITs, and it's obvious that only
9611 * vcpus running in guest are possible to have unflushed GPAs in PML
9612 * buffer.
9613 */
9614 kvm_for_each_vcpu(i, vcpu, kvm)
9615 kvm_vcpu_kick(vcpu);
9616}
9617
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009618static void vmx_dump_sel(char *name, uint32_t sel)
9619{
9620 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009621 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009622 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9623 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9624 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9625}
9626
9627static void vmx_dump_dtsel(char *name, uint32_t limit)
9628{
9629 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9630 name, vmcs_read32(limit),
9631 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9632}
9633
9634static void dump_vmcs(void)
9635{
9636 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9637 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9638 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9639 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9640 u32 secondary_exec_control = 0;
9641 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009642 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009643 int i, n;
9644
9645 if (cpu_has_secondary_exec_ctrls())
9646 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9647
9648 pr_err("*** Guest State ***\n");
9649 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9650 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9651 vmcs_readl(CR0_GUEST_HOST_MASK));
9652 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9653 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9654 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9655 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9656 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9657 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009658 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9659 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9660 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9661 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009662 }
9663 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9664 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9665 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9666 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9667 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9668 vmcs_readl(GUEST_SYSENTER_ESP),
9669 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9670 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9671 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9672 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9673 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9674 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9675 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9676 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9677 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9678 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9679 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9680 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9681 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009682 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9683 efer, vmcs_read64(GUEST_IA32_PAT));
9684 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9685 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009686 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009687 if (cpu_has_load_perf_global_ctrl &&
9688 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009689 pr_err("PerfGlobCtl = 0x%016llx\n",
9690 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009691 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009692 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009693 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9694 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9695 vmcs_read32(GUEST_ACTIVITY_STATE));
9696 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9697 pr_err("InterruptStatus = %04x\n",
9698 vmcs_read16(GUEST_INTR_STATUS));
9699
9700 pr_err("*** Host State ***\n");
9701 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9702 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9703 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9704 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9705 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9706 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9707 vmcs_read16(HOST_TR_SELECTOR));
9708 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9709 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9710 vmcs_readl(HOST_TR_BASE));
9711 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9712 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9713 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9714 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9715 vmcs_readl(HOST_CR4));
9716 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9717 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9718 vmcs_read32(HOST_IA32_SYSENTER_CS),
9719 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9720 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009721 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9722 vmcs_read64(HOST_IA32_EFER),
9723 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009724 if (cpu_has_load_perf_global_ctrl &&
9725 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009726 pr_err("PerfGlobCtl = 0x%016llx\n",
9727 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009728
9729 pr_err("*** Control State ***\n");
9730 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9731 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9732 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9733 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9734 vmcs_read32(EXCEPTION_BITMAP),
9735 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9736 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9737 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9738 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9739 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9740 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9741 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9742 vmcs_read32(VM_EXIT_INTR_INFO),
9743 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9744 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9745 pr_err(" reason=%08x qualification=%016lx\n",
9746 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9747 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9748 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9749 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009750 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009751 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009752 pr_err("TSC Multiplier = 0x%016llx\n",
9753 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009754 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9755 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9756 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9757 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9758 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009759 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009760 n = vmcs_read32(CR3_TARGET_COUNT);
9761 for (i = 0; i + 1 < n; i += 4)
9762 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9763 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9764 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9765 if (i < n)
9766 pr_err("CR3 target%u=%016lx\n",
9767 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9768 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9769 pr_err("PLE Gap=%08x Window=%08x\n",
9770 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9771 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9772 pr_err("Virtual processor ID = 0x%04x\n",
9773 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9774}
9775
Avi Kivity6aa8b732006-12-10 02:21:36 -08009776/*
9777 * The guest has exited. See if we can fix it or if we need userspace
9778 * assistance.
9779 */
Avi Kivity851ba692009-08-24 11:10:17 +03009780static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009781{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009782 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009783 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009784 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009785
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009786 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9787
Kai Huang843e4332015-01-28 10:54:28 +08009788 /*
9789 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9790 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
9791 * querying dirty_bitmap, we only need to kick all vcpus out of guest
9792 * mode as if vcpus is in root mode, the PML buffer must has been
9793 * flushed already.
9794 */
9795 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009796 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009797
Mohammed Gamal80ced182009-09-01 12:48:18 +02009798 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02009799 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02009800 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01009801
Paolo Bonzini7313c692017-07-27 10:31:25 +02009802 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
9803 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +03009804
Mohammed Gamal51207022010-05-31 22:40:54 +03009805 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009806 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03009807 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9808 vcpu->run->fail_entry.hardware_entry_failure_reason
9809 = exit_reason;
9810 return 0;
9811 }
9812
Avi Kivity29bd8a72007-09-10 17:27:03 +03009813 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03009814 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
9815 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03009816 = vmcs_read32(VM_INSTRUCTION_ERROR);
9817 return 0;
9818 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009819
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009820 /*
9821 * Note:
9822 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
9823 * delivery event since it indicates guest is accessing MMIO.
9824 * The vm-exit can be triggered again after return to guest that
9825 * will cause infinite loop.
9826 */
Mike Dayd77c26f2007-10-08 09:02:08 -04009827 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08009828 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02009829 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00009830 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009831 exit_reason != EXIT_REASON_TASK_SWITCH)) {
9832 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
9833 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009834 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009835 vcpu->run->internal.data[0] = vectoring_info;
9836 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +02009837 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
9838 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
9839 vcpu->run->internal.ndata++;
9840 vcpu->run->internal.data[3] =
9841 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
9842 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08009843 return 0;
9844 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02009845
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009846 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01009847 vmx->loaded_vmcs->soft_vnmi_blocked)) {
9848 if (vmx_interrupt_allowed(vcpu)) {
9849 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9850 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
9851 vcpu->arch.nmi_pending) {
9852 /*
9853 * This CPU don't support us in finding the end of an
9854 * NMI-blocked window if the guest runs with IRQs
9855 * disabled. So we pull the trigger after 1 s of
9856 * futile waiting, but inform the user about this.
9857 */
9858 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
9859 "state on VCPU %d after 1 s timeout\n",
9860 __func__, vcpu->vcpu_id);
9861 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
9862 }
9863 }
9864
Avi Kivity6aa8b732006-12-10 02:21:36 -08009865 if (exit_reason < kvm_vmx_max_exit_handlers
9866 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03009867 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009868 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01009869 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
9870 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03009871 kvm_queue_exception(vcpu, UD_VECTOR);
9872 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009873 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08009874}
9875
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009876static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009877{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009878 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9879
9880 if (is_guest_mode(vcpu) &&
9881 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9882 return;
9883
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009884 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009885 vmcs_write32(TPR_THRESHOLD, 0);
9886 return;
9887 }
9888
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009889 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08009890}
9891
Jim Mattson8d860bb2018-05-09 16:56:05 -04009892static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08009893{
9894 u32 sec_exec_control;
9895
Jim Mattson8d860bb2018-05-09 16:56:05 -04009896 if (!lapic_in_kernel(vcpu))
9897 return;
9898
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009899 /* Postpone execution until vmcs01 is the current VMCS. */
9900 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -04009901 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02009902 return;
9903 }
9904
Paolo Bonzini35754c92015-07-29 12:05:37 +02009905 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08009906 return;
9907
9908 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -04009909 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
9910 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +08009911
Jim Mattson8d860bb2018-05-09 16:56:05 -04009912 switch (kvm_get_apic_mode(vcpu)) {
9913 case LAPIC_MODE_INVALID:
9914 WARN_ONCE(true, "Invalid local APIC state");
9915 case LAPIC_MODE_DISABLED:
9916 break;
9917 case LAPIC_MODE_XAPIC:
9918 if (flexpriority_enabled) {
9919 sec_exec_control |=
9920 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9921 vmx_flush_tlb(vcpu, true);
9922 }
9923 break;
9924 case LAPIC_MODE_X2APIC:
9925 if (cpu_has_vmx_virtualize_x2apic_mode())
9926 sec_exec_control |=
9927 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
9928 break;
Yang Zhang8d146952013-01-25 10:18:50 +08009929 }
9930 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
9931
Paolo Bonzini904e14f2018-01-16 16:51:18 +01009932 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08009933}
9934
Tang Chen38b99172014-09-24 15:57:54 +08009935static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
9936{
Jim Mattsonab5df312018-05-09 17:02:03 -04009937 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +08009938 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -07009939 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07009940 }
Tang Chen38b99172014-09-24 15:57:54 +08009941}
9942
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009943static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08009944{
9945 u16 status;
9946 u8 old;
9947
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009948 if (max_isr == -1)
9949 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009950
9951 status = vmcs_read16(GUEST_INTR_STATUS);
9952 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009953 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08009954 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02009955 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08009956 vmcs_write16(GUEST_INTR_STATUS, status);
9957 }
9958}
9959
9960static void vmx_set_rvi(int vector)
9961{
9962 u16 status;
9963 u8 old;
9964
Wei Wang4114c272014-11-05 10:53:43 +08009965 if (vector == -1)
9966 vector = 0;
9967
Yang Zhangc7c9c562013-01-25 10:18:51 +08009968 status = vmcs_read16(GUEST_INTR_STATUS);
9969 old = (u8)status & 0xff;
9970 if ((u8)vector != old) {
9971 status &= ~0xff;
9972 status |= (u8)vector;
9973 vmcs_write16(GUEST_INTR_STATUS, status);
9974 }
9975}
9976
9977static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
9978{
Liran Alon851c1a182017-12-24 18:12:56 +02009979 /*
9980 * When running L2, updating RVI is only relevant when
9981 * vmcs12 virtual-interrupt-delivery enabled.
9982 * However, it can be enabled only when L1 also
9983 * intercepts external-interrupts and in that case
9984 * we should not update vmcs02 RVI but instead intercept
9985 * interrupt. Therefore, do nothing when running L2.
9986 */
9987 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +08009988 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009989}
9990
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009991static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009992{
9993 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009994 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +02009995 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01009996
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01009997 WARN_ON(!vcpu->arch.apicv_active);
9998 if (pi_test_on(&vmx->pi_desc)) {
9999 pi_clear_on(&vmx->pi_desc);
10000 /*
10001 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10002 * But on x86 this is just a compiler barrier anyway.
10003 */
10004 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010005 max_irr_updated =
10006 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10007
10008 /*
10009 * If we are running L2 and L1 has a new pending interrupt
10010 * which can be injected, we should re-evaluate
10011 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010012 * If L1 intercepts external-interrupts, we should
10013 * exit from L2 to L1. Otherwise, interrupt should be
10014 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010015 */
Liran Alon851c1a182017-12-24 18:12:56 +020010016 if (is_guest_mode(vcpu) && max_irr_updated) {
10017 if (nested_exit_on_intr(vcpu))
10018 kvm_vcpu_exiting_guest_mode(vcpu);
10019 else
10020 kvm_make_request(KVM_REQ_EVENT, vcpu);
10021 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010022 } else {
10023 max_irr = kvm_lapic_find_highest_irr(vcpu);
10024 }
10025 vmx_hwapic_irr_update(vcpu, max_irr);
10026 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010027}
10028
Andrey Smetanin63086302015-11-10 15:36:32 +030010029static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010030{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010031 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010032 return;
10033
Yang Zhangc7c9c562013-01-25 10:18:51 +080010034 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10035 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10036 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10037 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10038}
10039
Paolo Bonzini967235d2016-12-19 14:03:45 +010010040static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10041{
10042 struct vcpu_vmx *vmx = to_vmx(vcpu);
10043
10044 pi_clear_on(&vmx->pi_desc);
10045 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10046}
10047
Avi Kivity51aa01d2010-07-20 14:31:20 +030010048static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010049{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010050 u32 exit_intr_info = 0;
10051 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010052
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010053 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10054 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010055 return;
10056
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010057 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10058 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10059 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010060
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010061 /* if exit due to PF check for async PF */
10062 if (is_page_fault(exit_intr_info))
10063 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10064
Andi Kleena0861c02009-06-08 17:37:09 +080010065 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010066 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10067 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010068 kvm_machine_check();
10069
Gleb Natapov20f65982009-05-11 13:35:55 +030010070 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010071 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010072 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010073 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010074 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010075 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010076}
Gleb Natapov20f65982009-05-11 13:35:55 +030010077
Yang Zhanga547c6d2013-04-11 19:25:10 +080010078static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10079{
10080 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10081
Yang Zhanga547c6d2013-04-11 19:25:10 +080010082 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10083 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10084 unsigned int vector;
10085 unsigned long entry;
10086 gate_desc *desc;
10087 struct vcpu_vmx *vmx = to_vmx(vcpu);
10088#ifdef CONFIG_X86_64
10089 unsigned long tmp;
10090#endif
10091
10092 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10093 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010094 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010095 asm volatile(
10096#ifdef CONFIG_X86_64
10097 "mov %%" _ASM_SP ", %[sp]\n\t"
10098 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10099 "push $%c[ss]\n\t"
10100 "push %[sp]\n\t"
10101#endif
10102 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010103 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010104 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010105 :
10106#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010107 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010108#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010109 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010110 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010111 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010112 [ss]"i"(__KERNEL_DS),
10113 [cs]"i"(__KERNEL_CS)
10114 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010115 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010116}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010117STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010118
Tom Lendackybc226f02018-05-10 22:06:39 +020010119static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010120{
Tom Lendackybc226f02018-05-10 22:06:39 +020010121 switch (index) {
10122 case MSR_IA32_SMBASE:
10123 /*
10124 * We cannot do SMM unless we can run the guest in big
10125 * real mode.
10126 */
10127 return enable_unrestricted_guest || emulate_invalid_guest_state;
10128 case MSR_AMD64_VIRT_SPEC_CTRL:
10129 /* This is AMD only. */
10130 return false;
10131 default:
10132 return true;
10133 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010134}
10135
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010136static bool vmx_mpx_supported(void)
10137{
10138 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10139 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10140}
10141
Wanpeng Li55412b22014-12-02 19:21:30 +080010142static bool vmx_xsaves_supported(void)
10143{
10144 return vmcs_config.cpu_based_2nd_exec_ctrl &
10145 SECONDARY_EXEC_XSAVES;
10146}
10147
Avi Kivity51aa01d2010-07-20 14:31:20 +030010148static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10149{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010150 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010151 bool unblock_nmi;
10152 u8 vector;
10153 bool idtv_info_valid;
10154
10155 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010156
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010157 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010158 if (vmx->loaded_vmcs->nmi_known_unmasked)
10159 return;
10160 /*
10161 * Can't use vmx->exit_intr_info since we're not sure what
10162 * the exit reason is.
10163 */
10164 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10165 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10166 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10167 /*
10168 * SDM 3: 27.7.1.2 (September 2008)
10169 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10170 * a guest IRET fault.
10171 * SDM 3: 23.2.2 (September 2008)
10172 * Bit 12 is undefined in any of the following cases:
10173 * If the VM exit sets the valid bit in the IDT-vectoring
10174 * information field.
10175 * If the VM exit is due to a double fault.
10176 */
10177 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10178 vector != DF_VECTOR && !idtv_info_valid)
10179 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10180 GUEST_INTR_STATE_NMI);
10181 else
10182 vmx->loaded_vmcs->nmi_known_unmasked =
10183 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10184 & GUEST_INTR_STATE_NMI);
10185 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10186 vmx->loaded_vmcs->vnmi_blocked_time +=
10187 ktime_to_ns(ktime_sub(ktime_get(),
10188 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010189}
10190
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010191static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010192 u32 idt_vectoring_info,
10193 int instr_len_field,
10194 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010195{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010196 u8 vector;
10197 int type;
10198 bool idtv_info_valid;
10199
10200 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010201
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010202 vcpu->arch.nmi_injected = false;
10203 kvm_clear_exception_queue(vcpu);
10204 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010205
10206 if (!idtv_info_valid)
10207 return;
10208
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010209 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010210
Avi Kivity668f6122008-07-02 09:28:55 +030010211 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10212 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010213
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010214 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010215 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010216 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010217 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010218 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010219 * Clear bit "block by NMI" before VM entry if a NMI
10220 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010221 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010222 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010223 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010224 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010225 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010226 /* fall through */
10227 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010228 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010229 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010230 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010231 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010232 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010233 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010234 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010235 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010236 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010237 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010238 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010239 break;
10240 default:
10241 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010242 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010243}
10244
Avi Kivity83422e12010-07-20 14:43:23 +030010245static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10246{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010247 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010248 VM_EXIT_INSTRUCTION_LEN,
10249 IDT_VECTORING_ERROR_CODE);
10250}
10251
Avi Kivityb463a6f2010-07-20 15:06:17 +030010252static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10253{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010254 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010255 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10256 VM_ENTRY_INSTRUCTION_LEN,
10257 VM_ENTRY_EXCEPTION_ERROR_CODE);
10258
10259 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10260}
10261
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010262static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10263{
10264 int i, nr_msrs;
10265 struct perf_guest_switch_msr *msrs;
10266
10267 msrs = perf_guest_get_msrs(&nr_msrs);
10268
10269 if (!msrs)
10270 return;
10271
10272 for (i = 0; i < nr_msrs; i++)
10273 if (msrs[i].host == msrs[i].guest)
10274 clear_atomic_switch_msr(vmx, msrs[i].msr);
10275 else
10276 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
10277 msrs[i].host);
10278}
10279
Jiang Biao33365e72016-11-03 15:03:37 +080010280static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010281{
10282 struct vcpu_vmx *vmx = to_vmx(vcpu);
10283 u64 tscl;
10284 u32 delta_tsc;
10285
10286 if (vmx->hv_deadline_tsc == -1)
10287 return;
10288
10289 tscl = rdtsc();
10290 if (vmx->hv_deadline_tsc > tscl)
10291 /* sure to be 32 bit only because checked on set_hv_timer */
10292 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10293 cpu_preemption_timer_multi);
10294 else
10295 delta_tsc = 0;
10296
10297 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10298}
10299
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010300static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010301{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010302 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010303 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010304
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010305 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010306 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010307 vmx->loaded_vmcs->soft_vnmi_blocked))
10308 vmx->loaded_vmcs->entry_time = ktime_get();
10309
Avi Kivity104f2262010-11-18 13:12:52 +020010310 /* Don't enter VMX if guest state is invalid, let the exit handler
10311 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010312 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010313 return;
10314
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010315 if (vmx->ple_window_dirty) {
10316 vmx->ple_window_dirty = false;
10317 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10318 }
10319
Abel Gordon012f83c2013-04-18 14:39:25 +030010320 if (vmx->nested.sync_shadow_vmcs) {
10321 copy_vmcs12_to_shadow(vmx);
10322 vmx->nested.sync_shadow_vmcs = false;
10323 }
10324
Avi Kivity104f2262010-11-18 13:12:52 +020010325 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10326 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10327 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10328 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10329
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010330 cr3 = __get_current_cr3_fast();
Ladi Prosek44889942017-09-22 07:53:15 +020010331 if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010332 vmcs_writel(HOST_CR3, cr3);
Ladi Prosek44889942017-09-22 07:53:15 +020010333 vmx->loaded_vmcs->vmcs_host_cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010334 }
10335
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010336 cr4 = cr4_read_shadow();
Ladi Prosek44889942017-09-22 07:53:15 +020010337 if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010338 vmcs_writel(HOST_CR4, cr4);
Ladi Prosek44889942017-09-22 07:53:15 +020010339 vmx->loaded_vmcs->vmcs_host_cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010340 }
10341
Avi Kivity104f2262010-11-18 13:12:52 +020010342 /* When single-stepping over STI and MOV SS, we must clear the
10343 * corresponding interruptibility bits in the guest state. Otherwise
10344 * vmentry fails as it then expects bit 14 (BS) in pending debug
10345 * exceptions being set, but that's not correct for the guest debugging
10346 * case. */
10347 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10348 vmx_set_interrupt_shadow(vcpu, 0);
10349
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010350 if (static_cpu_has(X86_FEATURE_PKU) &&
10351 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10352 vcpu->arch.pkru != vmx->host_pkru)
10353 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010354
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010355 atomic_switch_perf_msrs(vmx);
10356
Yunhong Jiang64672c92016-06-13 14:19:59 -070010357 vmx_arm_hv_timer(vcpu);
10358
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010359 /*
10360 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10361 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10362 * is no need to worry about the conditional branch over the wrmsr
10363 * being speculatively taken.
10364 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010365 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010366
Nadav Har'Eld462b812011-05-24 15:26:10 +030010367 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010368
10369 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10370 (unsigned long)&current_evmcs->host_rsp : 0;
10371
Avi Kivity104f2262010-11-18 13:12:52 +020010372 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010373 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010374 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10375 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10376 "push %%" _ASM_CX " \n\t"
10377 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010378 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010379 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010380 /* Avoid VMWRITE when Enlightened VMCS is in use */
10381 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10382 "jz 2f \n\t"
10383 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10384 "jmp 1f \n\t"
10385 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010386 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010387 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010388 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010389 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10390 "mov %%cr2, %%" _ASM_DX " \n\t"
10391 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010392 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010393 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010394 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010395 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010396 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010397 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010398 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10399 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10400 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10401 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10402 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10403 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010404#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010405 "mov %c[r8](%0), %%r8 \n\t"
10406 "mov %c[r9](%0), %%r9 \n\t"
10407 "mov %c[r10](%0), %%r10 \n\t"
10408 "mov %c[r11](%0), %%r11 \n\t"
10409 "mov %c[r12](%0), %%r12 \n\t"
10410 "mov %c[r13](%0), %%r13 \n\t"
10411 "mov %c[r14](%0), %%r14 \n\t"
10412 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010413#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010414 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010415
Avi Kivity6aa8b732006-12-10 02:21:36 -080010416 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010417 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010418 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010419 "jmp 2f \n\t"
10420 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10421 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010422 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010423 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010424 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010425 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010426 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10427 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10428 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10429 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10430 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10431 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10432 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010433#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010434 "mov %%r8, %c[r8](%0) \n\t"
10435 "mov %%r9, %c[r9](%0) \n\t"
10436 "mov %%r10, %c[r10](%0) \n\t"
10437 "mov %%r11, %c[r11](%0) \n\t"
10438 "mov %%r12, %c[r12](%0) \n\t"
10439 "mov %%r13, %c[r13](%0) \n\t"
10440 "mov %%r14, %c[r14](%0) \n\t"
10441 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010442 "xor %%r8d, %%r8d \n\t"
10443 "xor %%r9d, %%r9d \n\t"
10444 "xor %%r10d, %%r10d \n\t"
10445 "xor %%r11d, %%r11d \n\t"
10446 "xor %%r12d, %%r12d \n\t"
10447 "xor %%r13d, %%r13d \n\t"
10448 "xor %%r14d, %%r14d \n\t"
10449 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010450#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010451 "mov %%cr2, %%" _ASM_AX " \n\t"
10452 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010453
Jim Mattson0cb5b302018-01-03 14:31:38 -080010454 "xor %%eax, %%eax \n\t"
10455 "xor %%ebx, %%ebx \n\t"
10456 "xor %%esi, %%esi \n\t"
10457 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010458 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010459 ".pushsection .rodata \n\t"
10460 ".global vmx_return \n\t"
10461 "vmx_return: " _ASM_PTR " 2b \n\t"
10462 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010463 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010464 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010465 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010466 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010467 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10468 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10469 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10470 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10471 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10472 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10473 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010474#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010475 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10476 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10477 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10478 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10479 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10480 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10481 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10482 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010483#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010484 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10485 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010486 : "cc", "memory"
10487#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010488 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010489 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010490#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010491 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010492#endif
10493 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010494
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010495 /*
10496 * We do not use IBRS in the kernel. If this vCPU has used the
10497 * SPEC_CTRL MSR it may have left it on; save the value and
10498 * turn it off. This is much more efficient than blindly adding
10499 * it to the atomic save/restore list. Especially as the former
10500 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10501 *
10502 * For non-nested case:
10503 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10504 * save it.
10505 *
10506 * For nested case:
10507 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10508 * save it.
10509 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010510 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010511 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010512
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010513 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010514
David Woodhouse117cc7a2018-01-12 11:11:27 +000010515 /* Eliminate branch target predictions from guest mode */
10516 vmexit_fill_RSB();
10517
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010518 /* All fields are clean at this point */
10519 if (static_branch_unlikely(&enable_evmcs))
10520 current_evmcs->hv_clean_fields |=
10521 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10522
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010523 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010524 if (vmx->host_debugctlmsr)
10525 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010526
Avi Kivityaa67f602012-08-01 16:48:03 +030010527#ifndef CONFIG_X86_64
10528 /*
10529 * The sysexit path does not restore ds/es, so we must set them to
10530 * a reasonable value ourselves.
10531 *
10532 * We can't defer this to vmx_load_host_state() since that function
10533 * may be executed in interrupt context, which saves and restore segments
10534 * around it, nullifying its effect.
10535 */
10536 loadsegment(ds, __USER_DS);
10537 loadsegment(es, __USER_DS);
10538#endif
10539
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010540 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010541 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010542 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010543 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010544 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010545 vcpu->arch.regs_dirty = 0;
10546
Gleb Natapove0b890d2013-09-25 12:51:33 +030010547 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010548 * eager fpu is enabled if PKEY is supported and CR4 is switched
10549 * back on host, so it is safe to read guest PKRU from current
10550 * XSAVE.
10551 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010552 if (static_cpu_has(X86_FEATURE_PKU) &&
10553 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10554 vcpu->arch.pkru = __read_pkru();
10555 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010556 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010557 }
10558
Gleb Natapove0b890d2013-09-25 12:51:33 +030010559 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010560 vmx->idt_vectoring_info = 0;
10561
10562 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10563 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10564 return;
10565
10566 vmx->loaded_vmcs->launched = 1;
10567 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010568
Avi Kivity51aa01d2010-07-20 14:31:20 +030010569 vmx_complete_atomic_exit(vmx);
10570 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010571 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010572}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010573STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010574
Sean Christopherson434a1e92018-03-20 12:17:18 -070010575static struct kvm *vmx_vm_alloc(void)
10576{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010577 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010578 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010579}
10580
10581static void vmx_vm_free(struct kvm *kvm)
10582{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010583 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010584}
10585
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010586static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010587{
10588 struct vcpu_vmx *vmx = to_vmx(vcpu);
10589 int cpu;
10590
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010591 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010592 return;
10593
10594 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010595 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010596 vmx_vcpu_put(vcpu);
10597 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010598 put_cpu();
10599}
10600
Jim Mattson2f1fe812016-07-08 15:36:06 -070010601/*
10602 * Ensure that the current vmcs of the logical processor is the
10603 * vmcs01 of the vcpu before calling free_nested().
10604 */
10605static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10606{
10607 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010608
Christoffer Dallec7660c2017-12-04 21:35:23 +010010609 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010610 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010611 free_nested(vmx);
10612 vcpu_put(vcpu);
10613}
10614
Avi Kivity6aa8b732006-12-10 02:21:36 -080010615static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10616{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010617 struct vcpu_vmx *vmx = to_vmx(vcpu);
10618
Kai Huang843e4332015-01-28 10:54:28 +080010619 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010620 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010621 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010622 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010623 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010624 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010625 kfree(vmx->guest_msrs);
10626 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010627 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010628}
10629
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010630static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010631{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010632 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010633 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010634 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010635 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010636
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010637 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010638 return ERR_PTR(-ENOMEM);
10639
Wanpeng Li991e7a02015-09-16 17:30:05 +080010640 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010641
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010642 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10643 if (err)
10644 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010645
Peter Feiner4e595162016-07-07 14:49:58 -070010646 err = -ENOMEM;
10647
10648 /*
10649 * If PML is turned on, failure on enabling PML just results in failure
10650 * of creating the vcpu, therefore we can simplify PML logic (by
10651 * avoiding dealing with cases, such as enabling PML partially on vcpus
10652 * for the guest, etc.
10653 */
10654 if (enable_pml) {
10655 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10656 if (!vmx->pml_pg)
10657 goto uninit_vcpu;
10658 }
10659
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010660 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010661 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10662 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010663
Peter Feiner4e595162016-07-07 14:49:58 -070010664 if (!vmx->guest_msrs)
10665 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010666
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010667 err = alloc_loaded_vmcs(&vmx->vmcs01);
10668 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010669 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010670
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010671 msr_bitmap = vmx->vmcs01.msr_bitmap;
10672 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10673 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10674 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10675 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10676 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10677 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10678 vmx->msr_bitmap_mode = 0;
10679
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010680 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010681 cpu = get_cpu();
10682 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010683 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010684 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010685 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010686 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010687 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010688 err = alloc_apic_access_page(kvm);
10689 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010690 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010691 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010692
Sean Christophersone90008d2018-03-05 12:04:37 -080010693 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010694 err = init_rmode_identity_map(kvm);
10695 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010696 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010697 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010698
Wanpeng Li5c614b32015-10-13 09:18:36 -070010699 if (nested) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010700 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10701 kvm_vcpu_apicv_active(&vmx->vcpu));
Wanpeng Li5c614b32015-10-13 09:18:36 -070010702 vmx->nested.vpid02 = allocate_vpid();
10703 }
Wincy Vanb9c237b2015-02-03 23:56:30 +080010704
Wincy Van705699a2015-02-03 23:58:17 +080010705 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010706 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010707
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010708 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
10709
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020010710 /*
10711 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
10712 * or POSTED_INTR_WAKEUP_VECTOR.
10713 */
10714 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
10715 vmx->pi_desc.sn = 1;
10716
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010717 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010718
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010719free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -070010720 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080010721 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010722free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010723 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070010724free_pml:
10725 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010726uninit_vcpu:
10727 kvm_vcpu_uninit(&vmx->vcpu);
10728free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080010729 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100010730 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010731 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010732}
10733
Wanpeng Lib31c1142018-03-12 04:53:04 -070010734static int vmx_vm_init(struct kvm *kvm)
10735{
Tianyu Lan877ad952018-07-19 08:40:23 +000010736 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
10737
Wanpeng Lib31c1142018-03-12 04:53:04 -070010738 if (!ple_gap)
10739 kvm->arch.pause_in_guest = true;
10740 return 0;
10741}
10742
Yang, Sheng002c7f72007-07-31 14:23:01 +030010743static void __init vmx_check_processor_compat(void *rtn)
10744{
10745 struct vmcs_config vmcs_conf;
10746
10747 *(int *)rtn = 0;
10748 if (setup_vmcs_config(&vmcs_conf) < 0)
10749 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010010750 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030010751 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
10752 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
10753 smp_processor_id());
10754 *(int *)rtn = -EIO;
10755 }
10756}
10757
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010758static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080010759{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010760 u8 cache;
10761 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010762
Sheng Yang522c68c2009-04-27 20:35:43 +080010763 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020010764 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080010765 * 2. EPT with VT-d:
10766 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020010767 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080010768 * b. VT-d with snooping control feature: snooping control feature of
10769 * VT-d engine can guarantee the cache correctness. Just set it
10770 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080010771 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080010772 * consistent with host MTRR
10773 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020010774 if (is_mmio) {
10775 cache = MTRR_TYPE_UNCACHABLE;
10776 goto exit;
10777 }
10778
10779 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010780 ipat = VMX_EPT_IPAT_BIT;
10781 cache = MTRR_TYPE_WRBACK;
10782 goto exit;
10783 }
10784
10785 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
10786 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020010787 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080010788 cache = MTRR_TYPE_WRBACK;
10789 else
10790 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010791 goto exit;
10792 }
10793
Xiao Guangrongff536042015-06-15 16:55:22 +080010794 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080010795
10796exit:
10797 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080010798}
10799
Sheng Yang17cc3932010-01-05 19:02:27 +080010800static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020010801{
Sheng Yang878403b2010-01-05 19:02:29 +080010802 if (enable_ept && !cpu_has_vmx_ept_1g_page())
10803 return PT_DIRECTORY_LEVEL;
10804 else
10805 /* For shadow and EPT supported 1GB page */
10806 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020010807}
10808
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010809static void vmcs_set_secondary_exec_control(u32 new_ctl)
10810{
10811 /*
10812 * These bits in the secondary execution controls field
10813 * are dynamic, the others are mostly based on the hypervisor
10814 * architecture and the guest's CPUID. Do not touch the
10815 * dynamic bits.
10816 */
10817 u32 mask =
10818 SECONDARY_EXEC_SHADOW_VMCS |
10819 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020010820 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10821 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080010822
10823 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
10824
10825 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
10826 (new_ctl & ~mask) | (cur_ctl & mask));
10827}
10828
David Matlack8322ebb2016-11-29 18:14:09 -080010829/*
10830 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
10831 * (indicating "allowed-1") if they are supported in the guest's CPUID.
10832 */
10833static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
10834{
10835 struct vcpu_vmx *vmx = to_vmx(vcpu);
10836 struct kvm_cpuid_entry2 *entry;
10837
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010838 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
10839 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080010840
10841#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
10842 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010843 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080010844} while (0)
10845
10846 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
10847 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
10848 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
10849 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
10850 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
10851 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
10852 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
10853 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
10854 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
10855 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
10856 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
10857 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
10858 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
10859 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
10860 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
10861
10862 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
10863 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
10864 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
10865 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
10866 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010010867 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080010868
10869#undef cr4_fixed1_update
10870}
10871
Sheng Yang0e851882009-12-18 16:48:46 +080010872static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
10873{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010874 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010875
Paolo Bonzini80154d72017-08-24 13:55:35 +020010876 if (cpu_has_secondary_exec_ctrls()) {
10877 vmx_compute_secondary_exec_control(vmx);
10878 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010879 }
Mao, Junjiead756a12012-07-02 01:18:48 +000010880
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010881 if (nested_vmx_allowed(vcpu))
10882 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
10883 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
10884 else
10885 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
10886 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080010887
10888 if (nested_vmx_allowed(vcpu))
10889 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080010890}
10891
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010892static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
10893{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030010894 if (func == 1 && nested)
10895 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010896}
10897
Yang Zhang25d92082013-08-06 12:00:32 +030010898static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
10899 struct x86_exception *fault)
10900{
Jan Kiszka533558b2014-01-04 18:47:20 +010010901 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040010902 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010903 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010904 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030010905
Bandan Dasc5f983f2017-05-05 15:25:14 -040010906 if (vmx->nested.pml_full) {
10907 exit_reason = EXIT_REASON_PML_FULL;
10908 vmx->nested.pml_full = false;
10909 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
10910 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010010911 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030010912 else
Jan Kiszka533558b2014-01-04 18:47:20 +010010913 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040010914
10915 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030010916 vmcs12->guest_physical_address = fault->address;
10917}
10918
Peter Feiner995f00a2017-06-30 17:26:32 -070010919static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
10920{
David Hildenbrandbb97a012017-08-10 23:15:28 +020010921 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070010922}
10923
Nadav Har'El155a97a2013-08-05 11:07:16 +030010924/* Callbacks for nested_ept_init_mmu_context: */
10925
10926static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
10927{
10928 /* return the page table to be shadowed - in our case, EPT12 */
10929 return get_vmcs12(vcpu)->ept_pointer;
10930}
10931
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010932static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030010933{
Paolo Bonziniad896af2013-10-02 16:56:14 +020010934 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020010935 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010936 return 1;
10937
Paolo Bonziniad896af2013-10-02 16:56:14 +020010938 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010939 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010940 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070010941 nested_ept_ad_enabled(vcpu),
10942 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030010943 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
10944 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
10945 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
10946
10947 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010948 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030010949}
10950
10951static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
10952{
10953 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
10954}
10955
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030010956static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
10957 u16 error_code)
10958{
10959 bool inequality, bit;
10960
10961 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
10962 inequality =
10963 (error_code & vmcs12->page_fault_error_code_mask) !=
10964 vmcs12->page_fault_error_code_match;
10965 return inequality ^ bit;
10966}
10967
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010968static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
10969 struct x86_exception *fault)
10970{
10971 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10972
10973 WARN_ON(!is_guest_mode(vcpu));
10974
Wanpeng Li305d0ab2017-09-28 18:16:44 -070010975 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
10976 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020010977 vmcs12->vm_exit_intr_error_code = fault->error_code;
10978 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10979 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
10980 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
10981 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010982 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010983 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020010984 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010985}
10986
Paolo Bonzinic9923842017-12-13 14:16:30 +010010987static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
10988 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010989
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010990static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010991{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020010992 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010993 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020010994 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010995 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010996
10997 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080010998 /*
10999 * Translate L1 physical address to host physical
11000 * address for vmcs02. Keep the page pinned, so this
11001 * physical address remains valid. We keep a reference
11002 * to it so we can release it later.
11003 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011004 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011005 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011006 vmx->nested.apic_access_page = NULL;
11007 }
11008 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011009 /*
11010 * If translation failed, no matter: This feature asks
11011 * to exit when accessing the given address, and if it
11012 * can never be accessed, this feature won't do
11013 * anything anyway.
11014 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011015 if (!is_error_page(page)) {
11016 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011017 hpa = page_to_phys(vmx->nested.apic_access_page);
11018 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11019 } else {
11020 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11021 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11022 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011023 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011024
11025 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011026 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011027 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011028 vmx->nested.virtual_apic_page = NULL;
11029 }
11030 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011031
11032 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011033 * If translation failed, VM entry will fail because
11034 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11035 * Failing the vm entry is _not_ what the processor
11036 * does but it's basically the only possibility we
11037 * have. We could still enter the guest if CR8 load
11038 * exits are enabled, CR8 store exits are enabled, and
11039 * virtualize APIC access is disabled; in this case
11040 * the processor would never use the TPR shadow and we
11041 * could simply clear the bit from the execution
11042 * control. But such a configuration is useless, so
11043 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011044 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011045 if (!is_error_page(page)) {
11046 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011047 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11048 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11049 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011050 }
11051
Wincy Van705699a2015-02-03 23:58:17 +080011052 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011053 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11054 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011055 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011056 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011057 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011058 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11059 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011060 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011061 vmx->nested.pi_desc_page = page;
11062 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011063 vmx->nested.pi_desc =
11064 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11065 (unsigned long)(vmcs12->posted_intr_desc_addr &
11066 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011067 vmcs_write64(POSTED_INTR_DESC_ADDR,
11068 page_to_phys(vmx->nested.pi_desc_page) +
11069 (unsigned long)(vmcs12->posted_intr_desc_addr &
11070 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011071 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011072 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011073 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11074 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011075 else
11076 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11077 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011078}
11079
Jan Kiszkaf41245002014-03-07 20:03:13 +010011080static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11081{
11082 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11083 struct vcpu_vmx *vmx = to_vmx(vcpu);
11084
11085 if (vcpu->arch.virtual_tsc_khz == 0)
11086 return;
11087
11088 /* Make sure short timeouts reliably trigger an immediate vmexit.
11089 * hrtimer_start does not guarantee this. */
11090 if (preemption_timeout <= 1) {
11091 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11092 return;
11093 }
11094
11095 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11096 preemption_timeout *= 1000000;
11097 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11098 hrtimer_start(&vmx->nested.preemption_timer,
11099 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11100}
11101
Jim Mattson56a20512017-07-06 16:33:06 -070011102static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11103 struct vmcs12 *vmcs12)
11104{
11105 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11106 return 0;
11107
11108 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11109 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11110 return -EINVAL;
11111
11112 return 0;
11113}
11114
Wincy Van3af18d92015-02-03 23:49:31 +080011115static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11116 struct vmcs12 *vmcs12)
11117{
Wincy Van3af18d92015-02-03 23:49:31 +080011118 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11119 return 0;
11120
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011121 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011122 return -EINVAL;
11123
11124 return 0;
11125}
11126
Jim Mattson712b12d2017-08-24 13:24:47 -070011127static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11128 struct vmcs12 *vmcs12)
11129{
11130 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11131 return 0;
11132
11133 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11134 return -EINVAL;
11135
11136 return 0;
11137}
11138
Wincy Van3af18d92015-02-03 23:49:31 +080011139/*
11140 * Merge L0's and L1's MSR bitmap, return false to indicate that
11141 * we do not use the hardware.
11142 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011143static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11144 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011145{
Wincy Van82f0dd42015-02-03 23:57:18 +080011146 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011147 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011148 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011149 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011150 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011151 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011152 *
11153 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11154 * ensures that we do not accidentally generate an L02 MSR bitmap
11155 * from the L12 MSR bitmap that is too permissive.
11156 * 2. That L1 or L2s have actually used the MSR. This avoids
11157 * unnecessarily merging of the bitmap if the MSR is unused. This
11158 * works properly because we only update the L01 MSR bitmap lazily.
11159 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11160 * updated to reflect this when L1 (or its L2s) actually write to
11161 * the MSR.
11162 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011163 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11164 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011165
Paolo Bonzinic9923842017-12-13 14:16:30 +010011166 /* Nothing to do if the MSR bitmap is not in use. */
11167 if (!cpu_has_vmx_msr_bitmap() ||
11168 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11169 return false;
11170
Ashok Raj15d45072018-02-01 22:59:43 +010011171 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011172 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011173 return false;
11174
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011175 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11176 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011177 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011178
Radim Krčmářd048c092016-08-08 20:16:22 +020011179 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011180 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11181 /*
11182 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11183 * just lets the processor take the value from the virtual-APIC page;
11184 * take those 256 bits directly from the L1 bitmap.
11185 */
11186 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11187 unsigned word = msr / BITS_PER_LONG;
11188 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11189 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011190 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011191 } else {
11192 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11193 unsigned word = msr / BITS_PER_LONG;
11194 msr_bitmap_l0[word] = ~0;
11195 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11196 }
11197 }
11198
11199 nested_vmx_disable_intercept_for_msr(
11200 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011201 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011202 MSR_TYPE_W);
11203
11204 if (nested_cpu_has_vid(vmcs12)) {
11205 nested_vmx_disable_intercept_for_msr(
11206 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011207 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011208 MSR_TYPE_W);
11209 nested_vmx_disable_intercept_for_msr(
11210 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011211 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011212 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011213 }
Ashok Raj15d45072018-02-01 22:59:43 +010011214
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011215 if (spec_ctrl)
11216 nested_vmx_disable_intercept_for_msr(
11217 msr_bitmap_l1, msr_bitmap_l0,
11218 MSR_IA32_SPEC_CTRL,
11219 MSR_TYPE_R | MSR_TYPE_W);
11220
Ashok Raj15d45072018-02-01 22:59:43 +010011221 if (pred_cmd)
11222 nested_vmx_disable_intercept_for_msr(
11223 msr_bitmap_l1, msr_bitmap_l0,
11224 MSR_IA32_PRED_CMD,
11225 MSR_TYPE_W);
11226
Wincy Vanf2b93282015-02-03 23:56:03 +080011227 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011228 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011229
11230 return true;
11231}
11232
Liran Alon61ada742018-06-23 02:35:08 +030011233static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11234 struct vmcs12 *vmcs12)
11235{
11236 struct vmcs12 *shadow;
11237 struct page *page;
11238
11239 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11240 vmcs12->vmcs_link_pointer == -1ull)
11241 return;
11242
11243 shadow = get_shadow_vmcs12(vcpu);
11244 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11245
11246 memcpy(shadow, kmap(page), VMCS12_SIZE);
11247
11248 kunmap(page);
11249 kvm_release_page_clean(page);
11250}
11251
11252static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11253 struct vmcs12 *vmcs12)
11254{
11255 struct vcpu_vmx *vmx = to_vmx(vcpu);
11256
11257 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11258 vmcs12->vmcs_link_pointer == -1ull)
11259 return;
11260
11261 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11262 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11263}
11264
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011265static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11266 struct vmcs12 *vmcs12)
11267{
11268 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11269 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11270 return -EINVAL;
11271 else
11272 return 0;
11273}
11274
Wincy Vanf2b93282015-02-03 23:56:03 +080011275static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11276 struct vmcs12 *vmcs12)
11277{
Wincy Van82f0dd42015-02-03 23:57:18 +080011278 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011279 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011280 !nested_cpu_has_vid(vmcs12) &&
11281 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011282 return 0;
11283
11284 /*
11285 * If virtualize x2apic mode is enabled,
11286 * virtualize apic access must be disabled.
11287 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011288 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11289 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011290 return -EINVAL;
11291
Wincy Van608406e2015-02-03 23:57:51 +080011292 /*
11293 * If virtual interrupt delivery is enabled,
11294 * we must exit on external interrupts.
11295 */
11296 if (nested_cpu_has_vid(vmcs12) &&
11297 !nested_exit_on_intr(vcpu))
11298 return -EINVAL;
11299
Wincy Van705699a2015-02-03 23:58:17 +080011300 /*
11301 * bits 15:8 should be zero in posted_intr_nv,
11302 * the descriptor address has been already checked
11303 * in nested_get_vmcs12_pages.
11304 */
11305 if (nested_cpu_has_posted_intr(vmcs12) &&
11306 (!nested_cpu_has_vid(vmcs12) ||
11307 !nested_exit_intr_ack_set(vcpu) ||
11308 vmcs12->posted_intr_nv & 0xff00))
11309 return -EINVAL;
11310
Wincy Vanf2b93282015-02-03 23:56:03 +080011311 /* tpr shadow is needed by all apicv features. */
11312 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11313 return -EINVAL;
11314
11315 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011316}
11317
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011318static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11319 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011320 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011321{
Liran Alone2536742018-06-23 02:35:02 +030011322 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011323 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011324 u64 count, addr;
11325
Liran Alone2536742018-06-23 02:35:02 +030011326 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11327 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011328 WARN_ON(1);
11329 return -EINVAL;
11330 }
11331 if (count == 0)
11332 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011333 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011334 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11335 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011336 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011337 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11338 addr_field, maxphyaddr, count, addr);
11339 return -EINVAL;
11340 }
11341 return 0;
11342}
11343
11344static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11345 struct vmcs12 *vmcs12)
11346{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011347 if (vmcs12->vm_exit_msr_load_count == 0 &&
11348 vmcs12->vm_exit_msr_store_count == 0 &&
11349 vmcs12->vm_entry_msr_load_count == 0)
11350 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011351 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011352 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011353 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011354 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011355 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011356 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011357 return -EINVAL;
11358 return 0;
11359}
11360
Bandan Dasc5f983f2017-05-05 15:25:14 -040011361static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11362 struct vmcs12 *vmcs12)
11363{
11364 u64 address = vmcs12->pml_address;
11365 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11366
11367 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11368 if (!nested_cpu_has_ept(vmcs12) ||
11369 !IS_ALIGNED(address, 4096) ||
11370 address >> maxphyaddr)
11371 return -EINVAL;
11372 }
11373
11374 return 0;
11375}
11376
Liran Alona8a7c022018-06-23 02:35:06 +030011377static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11378 struct vmcs12 *vmcs12)
11379{
11380 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11381 return 0;
11382
11383 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11384 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11385 return -EINVAL;
11386
11387 return 0;
11388}
11389
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011390static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11391 struct vmx_msr_entry *e)
11392{
11393 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011394 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011395 return -EINVAL;
11396 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11397 e->index == MSR_IA32_UCODE_REV)
11398 return -EINVAL;
11399 if (e->reserved != 0)
11400 return -EINVAL;
11401 return 0;
11402}
11403
11404static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11405 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011406{
11407 if (e->index == MSR_FS_BASE ||
11408 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011409 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11410 nested_vmx_msr_check_common(vcpu, e))
11411 return -EINVAL;
11412 return 0;
11413}
11414
11415static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11416 struct vmx_msr_entry *e)
11417{
11418 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11419 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011420 return -EINVAL;
11421 return 0;
11422}
11423
11424/*
11425 * Load guest's/host's msr at nested entry/exit.
11426 * return 0 for success, entry index for failure.
11427 */
11428static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11429{
11430 u32 i;
11431 struct vmx_msr_entry e;
11432 struct msr_data msr;
11433
11434 msr.host_initiated = false;
11435 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011436 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11437 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011438 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011439 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11440 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011441 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011442 }
11443 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011444 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011445 "%s check failed (%u, 0x%x, 0x%x)\n",
11446 __func__, i, e.index, e.reserved);
11447 goto fail;
11448 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011449 msr.index = e.index;
11450 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011451 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011452 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011453 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11454 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011455 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011456 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011457 }
11458 return 0;
11459fail:
11460 return i + 1;
11461}
11462
11463static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11464{
11465 u32 i;
11466 struct vmx_msr_entry e;
11467
11468 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011469 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011470 if (kvm_vcpu_read_guest(vcpu,
11471 gpa + i * sizeof(e),
11472 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011473 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011474 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11475 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011476 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011477 }
11478 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011479 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011480 "%s check failed (%u, 0x%x, 0x%x)\n",
11481 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011482 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011483 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011484 msr_info.host_initiated = false;
11485 msr_info.index = e.index;
11486 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011487 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011488 "%s cannot read MSR (%u, 0x%x)\n",
11489 __func__, i, e.index);
11490 return -EINVAL;
11491 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011492 if (kvm_vcpu_write_guest(vcpu,
11493 gpa + i * sizeof(e) +
11494 offsetof(struct vmx_msr_entry, value),
11495 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011496 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011497 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011498 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011499 return -EINVAL;
11500 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011501 }
11502 return 0;
11503}
11504
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011505static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11506{
11507 unsigned long invalid_mask;
11508
11509 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11510 return (val & invalid_mask) == 0;
11511}
11512
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011513/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011514 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11515 * emulating VM entry into a guest with EPT enabled.
11516 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11517 * is assigned to entry_failure_code on failure.
11518 */
11519static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011520 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011521{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011522 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011523 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011524 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11525 return 1;
11526 }
11527
11528 /*
11529 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11530 * must not be dereferenced.
11531 */
11532 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11533 !nested_ept) {
11534 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11535 *entry_failure_code = ENTRY_FAIL_PDPTE;
11536 return 1;
11537 }
11538 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011539 }
11540
Junaid Shahid50c28f22018-06-27 14:59:11 -070011541 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011542 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011543
11544 vcpu->arch.cr3 = cr3;
11545 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11546
11547 kvm_init_mmu(vcpu, false);
11548
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011549 return 0;
11550}
11551
Jim Mattson6514dc32018-04-26 16:09:12 -070011552static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011553{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011554 struct vcpu_vmx *vmx = to_vmx(vcpu);
11555
11556 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11557 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11558 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11559 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11560 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11561 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11562 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11563 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11564 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11565 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11566 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11567 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11568 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11569 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11570 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11571 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11572 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11573 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11574 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11575 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11576 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11577 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11578 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11579 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11580 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11581 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11582 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11583 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11584 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11585 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11586 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011587
11588 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11589 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11590 vmcs12->guest_pending_dbg_exceptions);
11591 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11592 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11593
11594 if (nested_cpu_has_xsaves(vmcs12))
11595 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11596 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11597
11598 if (cpu_has_vmx_posted_intr())
11599 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11600
11601 /*
11602 * Whether page-faults are trapped is determined by a combination of
11603 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11604 * If enable_ept, L0 doesn't care about page faults and we should
11605 * set all of these to L1's desires. However, if !enable_ept, L0 does
11606 * care about (at least some) page faults, and because it is not easy
11607 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11608 * to exit on each and every L2 page fault. This is done by setting
11609 * MASK=MATCH=0 and (see below) EB.PF=1.
11610 * Note that below we don't need special code to set EB.PF beyond the
11611 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11612 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11613 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11614 */
11615 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11616 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11617 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11618 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11619
11620 /* All VMFUNCs are currently emulated through L0 vmexits. */
11621 if (cpu_has_vmx_vmfunc())
11622 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11623
11624 if (cpu_has_vmx_apicv()) {
11625 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11626 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11627 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11628 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11629 }
11630
11631 /*
11632 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11633 * Some constant fields are set here by vmx_set_constant_host_state().
11634 * Other fields are different per CPU, and will be set later when
11635 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
11636 */
11637 vmx_set_constant_host_state(vmx);
11638
11639 /*
11640 * Set the MSR load/store lists to match L0's settings.
11641 */
11642 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
11643 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11644 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
11645 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
11646 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
11647
11648 set_cr4_guest_host_mask(vmx);
11649
11650 if (vmx_mpx_supported())
11651 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11652
11653 if (enable_vpid) {
11654 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11655 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11656 else
11657 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11658 }
11659
11660 /*
11661 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11662 */
11663 if (enable_ept) {
11664 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11665 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11666 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11667 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11668 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011669
11670 if (cpu_has_vmx_msr_bitmap())
11671 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011672}
11673
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011674/*
11675 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11676 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011677 * 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 +030011678 * guest in a way that will both be appropriate to L1's requests, and our
11679 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11680 * function also has additional necessary side-effects, like setting various
11681 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010011682 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11683 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011684 */
Ladi Prosekee146c12016-11-30 16:03:09 +010011685static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070011686 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011687{
11688 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040011689 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011690
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011691 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070011692 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080011693 vmx->nested.dirty_vmcs12 = false;
11694 }
11695
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011696 /*
11697 * First, the fields that are shadowed. This must be kept in sync
11698 * with vmx_shadow_fields.h.
11699 */
11700
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011701 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011702 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011703 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011704 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
11705 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011706
11707 /*
11708 * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR,
11709 * HOST_FS_BASE, HOST_GS_BASE.
11710 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011711
Jim Mattson6514dc32018-04-26 16:09:12 -070011712 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011713 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020011714 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
11715 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
11716 } else {
11717 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
11718 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
11719 }
Jim Mattson6514dc32018-04-26 16:09:12 -070011720 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011721 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
11722 vmcs12->vm_entry_intr_info_field);
11723 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
11724 vmcs12->vm_entry_exception_error_code);
11725 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
11726 vmcs12->vm_entry_instruction_len);
11727 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
11728 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070011729 vmx->loaded_vmcs->nmi_known_unmasked =
11730 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011731 } else {
11732 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
11733 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030011734 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011735
Jan Kiszkaf41245002014-03-07 20:03:13 +010011736 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080011737
Paolo Bonzini9314006db2016-07-06 13:23:51 +020011738 /* Preemption timer setting is only taken from vmcs01. */
11739 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11740 exec_control |= vmcs_config.pin_based_exec_ctrl;
11741 if (vmx->hv_deadline_tsc == -1)
11742 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
11743
11744 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080011745 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011746 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
11747 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011748 } else {
Wincy Van705699a2015-02-03 23:58:17 +080011749 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011750 }
Wincy Van705699a2015-02-03 23:58:17 +080011751
Jan Kiszkaf41245002014-03-07 20:03:13 +010011752 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011753
Jan Kiszkaf41245002014-03-07 20:03:13 +010011754 vmx->nested.preemption_timer_expired = false;
11755 if (nested_cpu_has_preemption_timer(vmcs12))
11756 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010011757
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011758 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020011759 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080011760
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011761 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011762 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020011763 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010011764 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020011765 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020011766 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040011767 SECONDARY_EXEC_APIC_REGISTER_VIRT |
11768 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011769 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040011770 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
11771 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
11772 ~SECONDARY_EXEC_ENABLE_PML;
11773 exec_control |= vmcs12_exec_ctrl;
11774 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011775
Liran Alon32c7acf2018-06-23 02:35:11 +030011776 /* VMCS shadowing for L2 is emulated for now */
11777 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
11778
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011779 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080011780 vmcs_write16(GUEST_INTR_STATUS,
11781 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080011782
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011783 /*
11784 * Write an illegal value to APIC_ACCESS_ADDR. Later,
11785 * nested_get_vmcs12_pages will either fix it up or
11786 * remove the VM execution control.
11787 */
11788 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
11789 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
11790
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011791 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
11792 }
11793
Jim Mattson83bafef2016-10-04 10:48:38 -070011794 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011795 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
11796 * entry, but only if the current (host) sp changed from the value
11797 * we wrote last (vmx->host_rsp). This cache is no longer relevant
11798 * if we switch vmcs, and rather than hold a separate cache per vmcs,
11799 * here we just force the write to happen on entry.
11800 */
11801 vmx->host_rsp = 0;
11802
11803 exec_control = vmx_exec_control(vmx); /* L0's desires */
11804 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
11805 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
11806 exec_control &= ~CPU_BASED_TPR_SHADOW;
11807 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011808
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011809 /*
11810 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
11811 * nested_get_vmcs12_pages can't fix it up, the illegal value
11812 * will result in a VM entry failure.
11813 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011814 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011815 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011816 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070011817 } else {
11818#ifdef CONFIG_X86_64
11819 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
11820 CPU_BASED_CR8_STORE_EXITING;
11821#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011822 }
11823
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011824 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080011825 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
11826 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011827 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011828 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
11829 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
11830
11831 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
11832
11833 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
11834 * bitwise-or of what L1 wants to trap for L2, and what we want to
11835 * trap. Note that CR0.TS also needs updating - we do this later.
11836 */
11837 update_exception_bitmap(vcpu);
11838 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
11839 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
11840
Nadav Har'El8049d652013-08-05 11:07:06 +030011841 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
11842 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
11843 * bits are further modified by vmx_set_efer() below.
11844 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010011845 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030011846
11847 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
11848 * emulated by vmx_set_efer(), below.
11849 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020011850 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030011851 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
11852 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011853 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
11854
Jim Mattson6514dc32018-04-26 16:09:12 -070011855 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011856 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011857 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020011858 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011859 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011860 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011861 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011862
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020011863 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
11864
Peter Feinerc95ba922016-08-17 09:36:47 -070011865 if (kvm_has_tsc_control)
11866 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011867
11868 if (enable_vpid) {
11869 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070011870 * There is no direct mapping between vpid02 and vpid12, the
11871 * vpid02 is per-vCPU for L0 and reused while the value of
11872 * vpid12 is changed w/ one invvpid during nested vmentry.
11873 * The vpid12 is allocated by L1 for L2, so it will not
11874 * influence global bitmap(for vpid01 and vpid02 allocation)
11875 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011876 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070011877 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070011878 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
11879 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030011880 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011881 }
11882 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080011883 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070011884 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011885 }
11886
Ladi Prosek1fb883b2017-04-04 14:18:53 +020011887 if (enable_pml) {
11888 /*
11889 * Conceptually we want to copy the PML address and index from
11890 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
11891 * since we always flush the log on each vmexit, this happens
11892 * to be equivalent to simply resetting the fields in vmcs02.
11893 */
11894 ASSERT(vmx->pml_pg);
11895 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
11896 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
11897 }
11898
Nadav Har'El155a97a2013-08-05 11:07:16 +030011899 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011900 if (nested_ept_init_mmu_context(vcpu)) {
11901 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11902 return 1;
11903 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011904 } else if (nested_cpu_has2(vmcs12,
11905 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070011906 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030011907 }
11908
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011909 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080011910 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
11911 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011912 * The CR0_READ_SHADOW is what L2 should have expected to read given
11913 * the specifications by L1; It's not enough to take
11914 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
11915 * have more bits than L1 expected.
11916 */
11917 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
11918 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
11919
11920 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
11921 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
11922
Jim Mattson6514dc32018-04-26 16:09:12 -070011923 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080011924 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080011925 vcpu->arch.efer = vmcs12->guest_ia32_efer;
11926 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11927 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
11928 else
11929 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
11930 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
11931 vmx_set_efer(vcpu, vcpu->arch.efer);
11932
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011933 /*
11934 * Guest state is invalid and unrestricted guest is disabled,
11935 * which means L1 attempted VMEntry to L2 with invalid state.
11936 * Fail the VMEntry.
11937 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010011938 if (vmx->emulation_required) {
11939 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011940 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010011941 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070011942
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011943 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010011944 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011945 entry_failure_code))
11946 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010011947
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011948 if (!enable_ept)
11949 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
11950
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011951 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
11952 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010011953 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011954}
11955
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050011956static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
11957{
11958 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
11959 nested_cpu_has_virtual_nmis(vmcs12))
11960 return -EINVAL;
11961
11962 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
11963 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
11964 return -EINVAL;
11965
11966 return 0;
11967}
11968
Jim Mattsonca0bde22016-11-30 12:03:46 -080011969static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
11970{
11971 struct vcpu_vmx *vmx = to_vmx(vcpu);
11972
11973 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
11974 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
11975 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11976
Jim Mattson56a20512017-07-06 16:33:06 -070011977 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
11978 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11979
Jim Mattsonca0bde22016-11-30 12:03:46 -080011980 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
11981 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11982
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011983 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
11984 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11985
Jim Mattson712b12d2017-08-24 13:24:47 -070011986 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
11987 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11988
Jim Mattsonca0bde22016-11-30 12:03:46 -080011989 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
11990 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11991
11992 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
11993 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11994
Bandan Dasc5f983f2017-05-05 15:25:14 -040011995 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
11996 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
11997
Liran Alona8a7c022018-06-23 02:35:06 +030011998 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
11999 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12000
Jim Mattsonca0bde22016-11-30 12:03:46 -080012001 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012002 vmx->nested.msrs.procbased_ctls_low,
12003 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012004 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12005 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012006 vmx->nested.msrs.secondary_ctls_low,
12007 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012008 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012009 vmx->nested.msrs.pinbased_ctls_low,
12010 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012011 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012012 vmx->nested.msrs.exit_ctls_low,
12013 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012014 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012015 vmx->nested.msrs.entry_ctls_low,
12016 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012017 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12018
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012019 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012020 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12021
Bandan Das41ab9372017-08-03 15:54:43 -040012022 if (nested_cpu_has_vmfunc(vmcs12)) {
12023 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012024 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012025 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12026
12027 if (nested_cpu_has_eptp_switching(vmcs12)) {
12028 if (!nested_cpu_has_ept(vmcs12) ||
12029 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12030 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12031 }
12032 }
Bandan Das27c42a12017-08-03 15:54:42 -040012033
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070012034 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12035 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12036
Jim Mattsonca0bde22016-11-30 12:03:46 -080012037 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12038 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12039 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12040 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12041
Marc Orr04473782018-06-20 17:21:29 -070012042 /*
12043 * From the Intel SDM, volume 3:
12044 * Fields relevant to VM-entry event injection must be set properly.
12045 * These fields are the VM-entry interruption-information field, the
12046 * VM-entry exception error code, and the VM-entry instruction length.
12047 */
12048 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12049 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12050 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12051 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12052 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12053 bool should_have_error_code;
12054 bool urg = nested_cpu_has2(vmcs12,
12055 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12056 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12057
12058 /* VM-entry interruption-info field: interruption type */
12059 if (intr_type == INTR_TYPE_RESERVED ||
12060 (intr_type == INTR_TYPE_OTHER_EVENT &&
12061 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12062 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12063
12064 /* VM-entry interruption-info field: vector */
12065 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12066 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12067 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12068 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12069
12070 /* VM-entry interruption-info field: deliver error code */
12071 should_have_error_code =
12072 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12073 x86_exception_has_error_code(vector);
12074 if (has_error_code != should_have_error_code)
12075 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12076
12077 /* VM-entry exception error code */
12078 if (has_error_code &&
12079 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12080 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12081
12082 /* VM-entry interruption-info field: reserved bits */
12083 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12084 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12085
12086 /* VM-entry instruction length */
12087 switch (intr_type) {
12088 case INTR_TYPE_SOFT_EXCEPTION:
12089 case INTR_TYPE_SOFT_INTR:
12090 case INTR_TYPE_PRIV_SW_EXCEPTION:
12091 if ((vmcs12->vm_entry_instruction_len > 15) ||
12092 (vmcs12->vm_entry_instruction_len == 0 &&
12093 !nested_cpu_has_zero_length_injection(vcpu)))
12094 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12095 }
12096 }
12097
Jim Mattsonca0bde22016-11-30 12:03:46 -080012098 return 0;
12099}
12100
Liran Alonf145d902018-06-23 02:35:07 +030012101static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12102 struct vmcs12 *vmcs12)
12103{
12104 int r;
12105 struct page *page;
12106 struct vmcs12 *shadow;
12107
12108 if (vmcs12->vmcs_link_pointer == -1ull)
12109 return 0;
12110
12111 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12112 return -EINVAL;
12113
12114 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12115 if (is_error_page(page))
12116 return -EINVAL;
12117
12118 r = 0;
12119 shadow = kmap(page);
12120 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12121 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12122 r = -EINVAL;
12123 kunmap(page);
12124 kvm_release_page_clean(page);
12125 return r;
12126}
12127
Jim Mattsonca0bde22016-11-30 12:03:46 -080012128static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12129 u32 *exit_qual)
12130{
12131 bool ia32e;
12132
12133 *exit_qual = ENTRY_FAIL_DEFAULT;
12134
12135 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12136 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12137 return 1;
12138
Liran Alonf145d902018-06-23 02:35:07 +030012139 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012140 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12141 return 1;
12142 }
12143
12144 /*
12145 * If the load IA32_EFER VM-entry control is 1, the following checks
12146 * are performed on the field for the IA32_EFER MSR:
12147 * - Bits reserved in the IA32_EFER MSR must be 0.
12148 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12149 * the IA-32e mode guest VM-exit control. It must also be identical
12150 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12151 * CR0.PG) is 1.
12152 */
12153 if (to_vmx(vcpu)->nested.nested_run_pending &&
12154 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12155 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12156 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12157 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12158 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12159 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12160 return 1;
12161 }
12162
12163 /*
12164 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12165 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12166 * the values of the LMA and LME bits in the field must each be that of
12167 * the host address-space size VM-exit control.
12168 */
12169 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12170 ia32e = (vmcs12->vm_exit_controls &
12171 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12172 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12173 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12174 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12175 return 1;
12176 }
12177
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012178 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12179 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12180 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12181 return 1;
12182
Jim Mattsonca0bde22016-11-30 12:03:46 -080012183 return 0;
12184}
12185
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012186/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012187 * If exit_qual is NULL, this is being called from state restore (either RSM
12188 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012189 */
12190static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012191{
12192 struct vcpu_vmx *vmx = to_vmx(vcpu);
12193 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012194 bool from_vmentry = !!exit_qual;
12195 u32 dummy_exit_qual;
12196 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012197
Jim Mattson858e25c2016-11-30 12:03:47 -080012198 enter_guest_mode(vcpu);
12199
12200 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12201 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12202
Jim Mattsonde3a0022017-11-27 17:22:25 -060012203 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012204 vmx_segment_cache_clear(vmx);
12205
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012206 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12207 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12208
12209 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012210 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012211 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012212
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012213 if (from_vmentry) {
12214 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012215
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012216 r = EXIT_REASON_MSR_LOAD_FAIL;
12217 *exit_qual = nested_vmx_load_msr(vcpu,
12218 vmcs12->vm_entry_msr_load_addr,
12219 vmcs12->vm_entry_msr_load_count);
12220 if (*exit_qual)
12221 goto fail;
12222 } else {
12223 /*
12224 * The MMU is not initialized to point at the right entities yet and
12225 * "get pages" would need to read data from the guest (i.e. we will
12226 * need to perform gpa to hpa translation). Request a call
12227 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12228 * have already been set at vmentry time and should not be reset.
12229 */
12230 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12231 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012232
Jim Mattson858e25c2016-11-30 12:03:47 -080012233 /*
12234 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12235 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12236 * returned as far as L1 is concerned. It will only return (and set
12237 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12238 */
12239 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012240
12241fail:
12242 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12243 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12244 leave_guest_mode(vcpu);
12245 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012246 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012247}
12248
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012249/*
12250 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12251 * for running an L2 nested guest.
12252 */
12253static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12254{
12255 struct vmcs12 *vmcs12;
12256 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012257 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012258 u32 exit_qual;
12259 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012260
Kyle Hueyeb277562016-11-29 12:40:39 -080012261 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012262 return 1;
12263
Kyle Hueyeb277562016-11-29 12:40:39 -080012264 if (!nested_vmx_check_vmcs12(vcpu))
12265 goto out;
12266
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012267 vmcs12 = get_vmcs12(vcpu);
12268
Liran Alona6192d42018-06-23 02:35:04 +030012269 /*
12270 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12271 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12272 * rather than RFLAGS.ZF, and no error number is stored to the
12273 * VM-instruction error field.
12274 */
12275 if (vmcs12->hdr.shadow_vmcs) {
12276 nested_vmx_failInvalid(vcpu);
12277 goto out;
12278 }
12279
Abel Gordon012f83c2013-04-18 14:39:25 +030012280 if (enable_shadow_vmcs)
12281 copy_shadow_to_vmcs12(vmx);
12282
Nadav Har'El7c177932011-05-25 23:12:04 +030012283 /*
12284 * The nested entry process starts with enforcing various prerequisites
12285 * on vmcs12 as required by the Intel SDM, and act appropriately when
12286 * they fail: As the SDM explains, some conditions should cause the
12287 * instruction to fail, while others will cause the instruction to seem
12288 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12289 * To speed up the normal (success) code path, we should avoid checking
12290 * for misconfigurations which will anyway be caught by the processor
12291 * when using the merged vmcs02.
12292 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012293 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12294 nested_vmx_failValid(vcpu,
12295 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12296 goto out;
12297 }
12298
Nadav Har'El7c177932011-05-25 23:12:04 +030012299 if (vmcs12->launch_state == launch) {
12300 nested_vmx_failValid(vcpu,
12301 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12302 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012303 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012304 }
12305
Jim Mattsonca0bde22016-11-30 12:03:46 -080012306 ret = check_vmentry_prereqs(vcpu, vmcs12);
12307 if (ret) {
12308 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012309 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012310 }
12311
Nadav Har'El7c177932011-05-25 23:12:04 +030012312 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012313 * After this point, the trap flag no longer triggers a singlestep trap
12314 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12315 * This is not 100% correct; for performance reasons, we delegate most
12316 * of the checks on host state to the processor. If those fail,
12317 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012318 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012319 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012320
Jim Mattsonca0bde22016-11-30 12:03:46 -080012321 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12322 if (ret) {
12323 nested_vmx_entry_failure(vcpu, vmcs12,
12324 EXIT_REASON_INVALID_STATE, exit_qual);
12325 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012326 }
12327
12328 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012329 * We're finally done with prerequisite checking, and can start with
12330 * the nested entry.
12331 */
12332
Jim Mattson6514dc32018-04-26 16:09:12 -070012333 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012334 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012335 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012336 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012337 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012338 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012339 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012340
Chao Gao135a06c2018-02-11 10:06:30 +080012341 /*
Liran Alon61ada742018-06-23 02:35:08 +030012342 * Must happen outside of enter_vmx_non_root_mode() as it will
12343 * also be used as part of restoring nVMX state for
12344 * snapshot restore (migration).
12345 *
12346 * In this flow, it is assumed that vmcs12 cache was
12347 * trasferred as part of captured nVMX state and should
12348 * therefore not be read from guest memory (which may not
12349 * exist on destination host yet).
12350 */
12351 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12352
12353 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012354 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12355 * by event injection, halt vcpu.
12356 */
12357 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012358 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12359 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012360 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012361 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012362 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012363
12364out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012365 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012366}
12367
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012368/*
12369 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12370 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12371 * This function returns the new value we should put in vmcs12.guest_cr0.
12372 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12373 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12374 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12375 * didn't trap the bit, because if L1 did, so would L0).
12376 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12377 * been modified by L2, and L1 knows it. So just leave the old value of
12378 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12379 * isn't relevant, because if L0 traps this bit it can set it to anything.
12380 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12381 * changed these bits, and therefore they need to be updated, but L0
12382 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12383 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12384 */
12385static inline unsigned long
12386vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12387{
12388 return
12389 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12390 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12391 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12392 vcpu->arch.cr0_guest_owned_bits));
12393}
12394
12395static inline unsigned long
12396vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12397{
12398 return
12399 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12400 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12401 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12402 vcpu->arch.cr4_guest_owned_bits));
12403}
12404
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012405static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12406 struct vmcs12 *vmcs12)
12407{
12408 u32 idt_vectoring;
12409 unsigned int nr;
12410
Wanpeng Li664f8e22017-08-24 03:35:09 -070012411 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012412 nr = vcpu->arch.exception.nr;
12413 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12414
12415 if (kvm_exception_is_soft(nr)) {
12416 vmcs12->vm_exit_instruction_len =
12417 vcpu->arch.event_exit_inst_len;
12418 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12419 } else
12420 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12421
12422 if (vcpu->arch.exception.has_error_code) {
12423 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12424 vmcs12->idt_vectoring_error_code =
12425 vcpu->arch.exception.error_code;
12426 }
12427
12428 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012429 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012430 vmcs12->idt_vectoring_info_field =
12431 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012432 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012433 nr = vcpu->arch.interrupt.nr;
12434 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12435
12436 if (vcpu->arch.interrupt.soft) {
12437 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12438 vmcs12->vm_entry_instruction_len =
12439 vcpu->arch.event_exit_inst_len;
12440 } else
12441 idt_vectoring |= INTR_TYPE_EXT_INTR;
12442
12443 vmcs12->idt_vectoring_info_field = idt_vectoring;
12444 }
12445}
12446
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012447static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12448{
12449 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012450 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012451 bool block_nested_events =
12452 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012453
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012454 if (vcpu->arch.exception.pending &&
12455 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012456 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012457 return -EBUSY;
12458 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012459 return 0;
12460 }
12461
Jan Kiszkaf41245002014-03-07 20:03:13 +010012462 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12463 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012464 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012465 return -EBUSY;
12466 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12467 return 0;
12468 }
12469
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012470 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012471 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012472 return -EBUSY;
12473 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12474 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12475 INTR_INFO_VALID_MASK, 0);
12476 /*
12477 * The NMI-triggered VM exit counts as injection:
12478 * clear this one and block further NMIs.
12479 */
12480 vcpu->arch.nmi_pending = 0;
12481 vmx_set_nmi_mask(vcpu, true);
12482 return 0;
12483 }
12484
12485 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12486 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012487 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012488 return -EBUSY;
12489 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012490 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012491 }
12492
David Hildenbrand6342c502017-01-25 11:58:58 +010012493 vmx_complete_nested_posted_interrupt(vcpu);
12494 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012495}
12496
Jan Kiszkaf41245002014-03-07 20:03:13 +010012497static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12498{
12499 ktime_t remaining =
12500 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12501 u64 value;
12502
12503 if (ktime_to_ns(remaining) <= 0)
12504 return 0;
12505
12506 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12507 do_div(value, 1000000);
12508 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12509}
12510
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012511/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012512 * Update the guest state fields of vmcs12 to reflect changes that
12513 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12514 * VM-entry controls is also updated, since this is really a guest
12515 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012516 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012517static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012518{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012519 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12520 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12521
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012522 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12523 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12524 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12525
12526 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12527 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12528 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12529 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12530 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12531 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12532 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12533 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12534 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12535 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12536 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12537 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12538 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12539 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12540 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12541 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12542 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12543 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12544 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12545 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12546 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12547 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12548 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12549 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12550 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12551 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12552 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12553 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12554 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12555 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12556 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12557 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12558 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12559 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12560 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12561 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12562
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012563 vmcs12->guest_interruptibility_info =
12564 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12565 vmcs12->guest_pending_dbg_exceptions =
12566 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012567 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12568 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12569 else
12570 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012571
Jan Kiszkaf41245002014-03-07 20:03:13 +010012572 if (nested_cpu_has_preemption_timer(vmcs12)) {
12573 if (vmcs12->vm_exit_controls &
12574 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12575 vmcs12->vmx_preemption_timer_value =
12576 vmx_get_preemption_timer_value(vcpu);
12577 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12578 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012579
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012580 /*
12581 * In some cases (usually, nested EPT), L2 is allowed to change its
12582 * own CR3 without exiting. If it has changed it, we must keep it.
12583 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12584 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12585 *
12586 * Additionally, restore L2's PDPTR to vmcs12.
12587 */
12588 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012589 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012590 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12591 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12592 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12593 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12594 }
12595
Jim Mattsond281e132017-06-01 12:44:46 -070012596 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012597
Wincy Van608406e2015-02-03 23:57:51 +080012598 if (nested_cpu_has_vid(vmcs12))
12599 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12600
Jan Kiszkac18911a2013-03-13 16:06:41 +010012601 vmcs12->vm_entry_controls =
12602 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012603 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012604
Jan Kiszka2996fca2014-06-16 13:59:43 +020012605 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12606 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12607 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12608 }
12609
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012610 /* TODO: These cannot have changed unless we have MSR bitmaps and
12611 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012612 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012613 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012614 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12615 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012616 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12617 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12618 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012619 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012620 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012621}
12622
12623/*
12624 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12625 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12626 * and this function updates it to reflect the changes to the guest state while
12627 * L2 was running (and perhaps made some exits which were handled directly by L0
12628 * without going back to L1), and to reflect the exit reason.
12629 * Note that we do not have to copy here all VMCS fields, just those that
12630 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12631 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12632 * which already writes to vmcs12 directly.
12633 */
12634static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12635 u32 exit_reason, u32 exit_intr_info,
12636 unsigned long exit_qualification)
12637{
12638 /* update guest state fields: */
12639 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012640
12641 /* update exit information fields: */
12642
Jan Kiszka533558b2014-01-04 18:47:20 +010012643 vmcs12->vm_exit_reason = exit_reason;
12644 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012645 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012646
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012647 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012648 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12649 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12650
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012651 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012652 vmcs12->launch_state = 1;
12653
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012654 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12655 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012656 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012657
12658 /*
12659 * Transfer the event that L0 or L1 may wanted to inject into
12660 * L2 to IDT_VECTORING_INFO_FIELD.
12661 */
12662 vmcs12_save_pending_event(vcpu, vmcs12);
12663 }
12664
12665 /*
12666 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12667 * preserved above and would only end up incorrectly in L1.
12668 */
12669 vcpu->arch.nmi_injected = false;
12670 kvm_clear_exception_queue(vcpu);
12671 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012672}
12673
Wanpeng Li5af41572017-11-05 16:54:49 -080012674static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12675 struct vmcs12 *vmcs12)
12676{
12677 u32 entry_failure_code;
12678
12679 nested_ept_uninit_mmu_context(vcpu);
12680
12681 /*
12682 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12683 * couldn't have changed.
12684 */
12685 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
12686 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
12687
12688 if (!enable_ept)
12689 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
12690}
12691
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012692/*
12693 * A part of what we need to when the nested L2 guest exits and we want to
12694 * run its L1 parent, is to reset L1's guest state to the host state specified
12695 * in vmcs12.
12696 * This function is to be called not only on normal nested exit, but also on
12697 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
12698 * Failures During or After Loading Guest State").
12699 * This function should be called when the active VMCS is L1's (vmcs01).
12700 */
Jan Kiszka733568f2013-02-23 15:07:47 +010012701static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12702 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012703{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012704 struct kvm_segment seg;
12705
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012706 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
12707 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020012708 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012709 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12710 else
12711 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12712 vmx_set_efer(vcpu, vcpu->arch.efer);
12713
12714 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
12715 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070012716 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012717 /*
12718 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012719 * actually changed, because vmx_set_cr0 refers to efer set above.
12720 *
12721 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
12722 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012723 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012724 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020012725 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012726
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012727 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012728 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080012729 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012730
Wanpeng Li5af41572017-11-05 16:54:49 -080012731 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012732
Liran Alon6f1e03b2018-05-22 17:16:14 +030012733 /*
12734 * If vmcs01 don't use VPID, CPU flushes TLB on every
12735 * VMEntry/VMExit. Thus, no need to flush TLB.
12736 *
12737 * If vmcs12 uses VPID, TLB entries populated by L2 are
12738 * tagged with vmx->nested.vpid02 while L1 entries are tagged
12739 * with vmx->vpid. Thus, no need to flush TLB.
12740 *
12741 * Therefore, flush TLB only in case vmcs01 uses VPID and
12742 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
12743 * are both tagged with vmx->vpid.
12744 */
12745 if (enable_vpid &&
12746 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012747 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012748 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012749
12750 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
12751 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
12752 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
12753 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
12754 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020012755 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
12756 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012757
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012758 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
12759 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
12760 vmcs_write64(GUEST_BNDCFGS, 0);
12761
Jan Kiszka44811c02013-08-04 17:17:27 +020012762 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012763 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012764 vcpu->arch.pat = vmcs12->host_ia32_pat;
12765 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012766 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
12767 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
12768 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012769
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012770 /* Set L1 segment info according to Intel SDM
12771 27.5.2 Loading Host Segment and Descriptor-Table Registers */
12772 seg = (struct kvm_segment) {
12773 .base = 0,
12774 .limit = 0xFFFFFFFF,
12775 .selector = vmcs12->host_cs_selector,
12776 .type = 11,
12777 .present = 1,
12778 .s = 1,
12779 .g = 1
12780 };
12781 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
12782 seg.l = 1;
12783 else
12784 seg.db = 1;
12785 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
12786 seg = (struct kvm_segment) {
12787 .base = 0,
12788 .limit = 0xFFFFFFFF,
12789 .type = 3,
12790 .present = 1,
12791 .s = 1,
12792 .db = 1,
12793 .g = 1
12794 };
12795 seg.selector = vmcs12->host_ds_selector;
12796 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
12797 seg.selector = vmcs12->host_es_selector;
12798 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
12799 seg.selector = vmcs12->host_ss_selector;
12800 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
12801 seg.selector = vmcs12->host_fs_selector;
12802 seg.base = vmcs12->host_fs_base;
12803 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
12804 seg.selector = vmcs12->host_gs_selector;
12805 seg.base = vmcs12->host_gs_base;
12806 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
12807 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030012808 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080012809 .limit = 0x67,
12810 .selector = vmcs12->host_tr_selector,
12811 .type = 11,
12812 .present = 1
12813 };
12814 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
12815
Jan Kiszka503cd0c2013-03-03 13:05:44 +010012816 kvm_set_dr(vcpu, 7, 0x400);
12817 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030012818
Wincy Van3af18d92015-02-03 23:49:31 +080012819 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010012820 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080012821
Wincy Vanff651cb2014-12-11 08:52:58 +030012822 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
12823 vmcs12->vm_exit_msr_load_count))
12824 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012825}
12826
12827/*
12828 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
12829 * and modify vmcs12 to make it see what it would expect to see there if
12830 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
12831 */
Jan Kiszka533558b2014-01-04 18:47:20 +010012832static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
12833 u32 exit_intr_info,
12834 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012835{
12836 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012837 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
12838
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012839 /* trying to cancel vmlaunch/vmresume is a bug */
12840 WARN_ON_ONCE(vmx->nested.nested_run_pending);
12841
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012842 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070012843 * The only expected VM-instruction error is "VM entry with
12844 * invalid control field(s)." Anything else indicates a
12845 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070012846 */
Jim Mattson4f350c62017-09-14 16:31:44 -070012847 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
12848 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
12849
12850 leave_guest_mode(vcpu);
12851
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012852 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12853 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12854
Jim Mattson4f350c62017-09-14 16:31:44 -070012855 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012856 if (exit_reason == -1)
12857 sync_vmcs12(vcpu, vmcs12);
12858 else
12859 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
12860 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070012861
Liran Alon61ada742018-06-23 02:35:08 +030012862 /*
12863 * Must happen outside of sync_vmcs12() as it will
12864 * also be used to capture vmcs12 cache as part of
12865 * capturing nVMX state for snapshot (migration).
12866 *
12867 * Otherwise, this flush will dirty guest memory at a
12868 * point it is already assumed by user-space to be
12869 * immutable.
12870 */
12871 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
12872
Jim Mattson4f350c62017-09-14 16:31:44 -070012873 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
12874 vmcs12->vm_exit_msr_store_count))
12875 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040012876 }
12877
Jim Mattson4f350c62017-09-14 16:31:44 -070012878 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020012879 vm_entry_controls_reset_shadow(vmx);
12880 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010012881 vmx_segment_cache_clear(vmx);
12882
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012883 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070012884 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
12885 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010012886 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012887 if (vmx->hv_deadline_tsc == -1)
12888 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
12889 PIN_BASED_VMX_PREEMPTION_TIMER);
12890 else
12891 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
12892 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070012893 if (kvm_has_tsc_control)
12894 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012895
Jim Mattson8d860bb2018-05-09 16:56:05 -040012896 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
12897 vmx->nested.change_vmcs01_virtual_apic_mode = false;
12898 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012899 } else if (!nested_cpu_has_ept(vmcs12) &&
12900 nested_cpu_has2(vmcs12,
12901 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012902 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020012903 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012904
12905 /* This is needed for same reason as it was needed in prepare_vmcs02 */
12906 vmx->host_rsp = 0;
12907
12908 /* Unpin physical memory we referred to in vmcs02 */
12909 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012910 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012911 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012912 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012913 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020012914 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020012915 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012916 }
Wincy Van705699a2015-02-03 23:58:17 +080012917 if (vmx->nested.pi_desc_page) {
12918 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020012919 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080012920 vmx->nested.pi_desc_page = NULL;
12921 vmx->nested.pi_desc = NULL;
12922 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012923
12924 /*
Tang Chen38b99172014-09-24 15:57:54 +080012925 * We are now running in L2, mmu_notifier will force to reload the
12926 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
12927 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080012928 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080012929
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012930 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030012931 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012932
12933 /* in case we halted in L2 */
12934 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070012935
12936 if (likely(!vmx->fail)) {
12937 /*
12938 * TODO: SDM says that with acknowledge interrupt on
12939 * exit, bit 31 of the VM-exit interrupt information
12940 * (valid interrupt) is always set to 1 on
12941 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
12942 * need kvm_cpu_has_interrupt(). See the commit
12943 * message for details.
12944 */
12945 if (nested_exit_intr_ack_set(vcpu) &&
12946 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
12947 kvm_cpu_has_interrupt(vcpu)) {
12948 int irq = kvm_cpu_get_interrupt(vcpu);
12949 WARN_ON(irq < 0);
12950 vmcs12->vm_exit_intr_info = irq |
12951 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
12952 }
12953
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020012954 if (exit_reason != -1)
12955 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
12956 vmcs12->exit_qualification,
12957 vmcs12->idt_vectoring_info_field,
12958 vmcs12->vm_exit_intr_info,
12959 vmcs12->vm_exit_intr_error_code,
12960 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070012961
12962 load_vmcs12_host_state(vcpu, vmcs12);
12963
12964 return;
12965 }
12966
12967 /*
12968 * After an early L2 VM-entry failure, we're now back
12969 * in L1 which thinks it just finished a VMLAUNCH or
12970 * VMRESUME instruction, so we need to set the failure
12971 * flag and the VM-instruction error field of the VMCS
12972 * accordingly.
12973 */
12974 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080012975
12976 load_vmcs12_mmu_host_state(vcpu, vmcs12);
12977
Jim Mattson4f350c62017-09-14 16:31:44 -070012978 /*
12979 * The emulated instruction was already skipped in
12980 * nested_vmx_run, but the updated RIP was never
12981 * written back to the vmcs01.
12982 */
12983 skip_emulated_instruction(vcpu);
12984 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012985}
12986
Nadav Har'El7c177932011-05-25 23:12:04 +030012987/*
Jan Kiszka42124922014-01-04 18:47:19 +010012988 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
12989 */
12990static void vmx_leave_nested(struct kvm_vcpu *vcpu)
12991{
Wanpeng Li2f707d92017-03-06 04:03:28 -080012992 if (is_guest_mode(vcpu)) {
12993 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010012994 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080012995 }
Jan Kiszka42124922014-01-04 18:47:19 +010012996 free_nested(to_vmx(vcpu));
12997}
12998
12999/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013000 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13001 * 23.7 "VM-entry failures during or after loading guest state" (this also
13002 * lists the acceptable exit-reason and exit-qualification parameters).
13003 * It should only be called before L2 actually succeeded to run, and when
13004 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13005 */
13006static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13007 struct vmcs12 *vmcs12,
13008 u32 reason, unsigned long qualification)
13009{
13010 load_vmcs12_host_state(vcpu, vmcs12);
13011 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13012 vmcs12->exit_qualification = qualification;
13013 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013014 if (enable_shadow_vmcs)
13015 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013016}
13017
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013018static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13019 struct x86_instruction_info *info,
13020 enum x86_intercept_stage stage)
13021{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013022 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13023 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13024
13025 /*
13026 * RDPID causes #UD if disabled through secondary execution controls.
13027 * Because it is marked as EmulateOnUD, we need to intercept it here.
13028 */
13029 if (info->intercept == x86_intercept_rdtscp &&
13030 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13031 ctxt->exception.vector = UD_VECTOR;
13032 ctxt->exception.error_code_valid = false;
13033 return X86EMUL_PROPAGATE_FAULT;
13034 }
13035
13036 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013037 return X86EMUL_CONTINUE;
13038}
13039
Yunhong Jiang64672c92016-06-13 14:19:59 -070013040#ifdef CONFIG_X86_64
13041/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13042static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13043 u64 divisor, u64 *result)
13044{
13045 u64 low = a << shift, high = a >> (64 - shift);
13046
13047 /* To avoid the overflow on divq */
13048 if (high >= divisor)
13049 return 1;
13050
13051 /* Low hold the result, high hold rem which is discarded */
13052 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13053 "rm" (divisor), "0" (low), "1" (high));
13054 *result = low;
13055
13056 return 0;
13057}
13058
13059static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13060{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013061 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013062 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013063
13064 if (kvm_mwait_in_guest(vcpu->kvm))
13065 return -EOPNOTSUPP;
13066
13067 vmx = to_vmx(vcpu);
13068 tscl = rdtsc();
13069 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13070 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013071 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13072
13073 if (delta_tsc > lapic_timer_advance_cycles)
13074 delta_tsc -= lapic_timer_advance_cycles;
13075 else
13076 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013077
13078 /* Convert to host delta tsc if tsc scaling is enabled */
13079 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13080 u64_shl_div_u64(delta_tsc,
13081 kvm_tsc_scaling_ratio_frac_bits,
13082 vcpu->arch.tsc_scaling_ratio,
13083 &delta_tsc))
13084 return -ERANGE;
13085
13086 /*
13087 * If the delta tsc can't fit in the 32 bit after the multi shift,
13088 * we can't use the preemption timer.
13089 * It's possible that it fits on later vmentries, but checking
13090 * on every vmentry is costly so we just use an hrtimer.
13091 */
13092 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13093 return -ERANGE;
13094
13095 vmx->hv_deadline_tsc = tscl + delta_tsc;
13096 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13097 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013098
13099 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013100}
13101
13102static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13103{
13104 struct vcpu_vmx *vmx = to_vmx(vcpu);
13105 vmx->hv_deadline_tsc = -1;
13106 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13107 PIN_BASED_VMX_PREEMPTION_TIMER);
13108}
13109#endif
13110
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013111static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013112{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013113 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013114 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013115}
13116
Kai Huang843e4332015-01-28 10:54:28 +080013117static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13118 struct kvm_memory_slot *slot)
13119{
13120 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13121 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13122}
13123
13124static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13125 struct kvm_memory_slot *slot)
13126{
13127 kvm_mmu_slot_set_dirty(kvm, slot);
13128}
13129
13130static void vmx_flush_log_dirty(struct kvm *kvm)
13131{
13132 kvm_flush_pml_buffers(kvm);
13133}
13134
Bandan Dasc5f983f2017-05-05 15:25:14 -040013135static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13136{
13137 struct vmcs12 *vmcs12;
13138 struct vcpu_vmx *vmx = to_vmx(vcpu);
13139 gpa_t gpa;
13140 struct page *page = NULL;
13141 u64 *pml_address;
13142
13143 if (is_guest_mode(vcpu)) {
13144 WARN_ON_ONCE(vmx->nested.pml_full);
13145
13146 /*
13147 * Check if PML is enabled for the nested guest.
13148 * Whether eptp bit 6 is set is already checked
13149 * as part of A/D emulation.
13150 */
13151 vmcs12 = get_vmcs12(vcpu);
13152 if (!nested_cpu_has_pml(vmcs12))
13153 return 0;
13154
Dan Carpenter47698862017-05-10 22:43:17 +030013155 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013156 vmx->nested.pml_full = true;
13157 return 1;
13158 }
13159
13160 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13161
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013162 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13163 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013164 return 0;
13165
13166 pml_address = kmap(page);
13167 pml_address[vmcs12->guest_pml_index--] = gpa;
13168 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013169 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013170 }
13171
13172 return 0;
13173}
13174
Kai Huang843e4332015-01-28 10:54:28 +080013175static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13176 struct kvm_memory_slot *memslot,
13177 gfn_t offset, unsigned long mask)
13178{
13179 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13180}
13181
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013182static void __pi_post_block(struct kvm_vcpu *vcpu)
13183{
13184 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13185 struct pi_desc old, new;
13186 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013187
13188 do {
13189 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013190 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13191 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013192
13193 dest = cpu_physical_id(vcpu->cpu);
13194
13195 if (x2apic_enabled())
13196 new.ndst = dest;
13197 else
13198 new.ndst = (dest << 8) & 0xFF00;
13199
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013200 /* set 'NV' to 'notification vector' */
13201 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013202 } while (cmpxchg64(&pi_desc->control, old.control,
13203 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013204
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013205 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13206 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013207 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013208 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013209 vcpu->pre_pcpu = -1;
13210 }
13211}
13212
Feng Wuefc64402015-09-18 22:29:51 +080013213/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013214 * This routine does the following things for vCPU which is going
13215 * to be blocked if VT-d PI is enabled.
13216 * - Store the vCPU to the wakeup list, so when interrupts happen
13217 * we can find the right vCPU to wake up.
13218 * - Change the Posted-interrupt descriptor as below:
13219 * 'NDST' <-- vcpu->pre_pcpu
13220 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13221 * - If 'ON' is set during this process, which means at least one
13222 * interrupt is posted for this vCPU, we cannot block it, in
13223 * this case, return 1, otherwise, return 0.
13224 *
13225 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013226static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013227{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013228 unsigned int dest;
13229 struct pi_desc old, new;
13230 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13231
13232 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013233 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13234 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013235 return 0;
13236
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013237 WARN_ON(irqs_disabled());
13238 local_irq_disable();
13239 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13240 vcpu->pre_pcpu = vcpu->cpu;
13241 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13242 list_add_tail(&vcpu->blocked_vcpu_list,
13243 &per_cpu(blocked_vcpu_on_cpu,
13244 vcpu->pre_pcpu));
13245 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13246 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013247
13248 do {
13249 old.control = new.control = pi_desc->control;
13250
Feng Wubf9f6ac2015-09-18 22:29:55 +080013251 WARN((pi_desc->sn == 1),
13252 "Warning: SN field of posted-interrupts "
13253 "is set before blocking\n");
13254
13255 /*
13256 * Since vCPU can be preempted during this process,
13257 * vcpu->cpu could be different with pre_pcpu, we
13258 * need to set pre_pcpu as the destination of wakeup
13259 * notification event, then we can find the right vCPU
13260 * to wakeup in wakeup handler if interrupts happen
13261 * when the vCPU is in blocked state.
13262 */
13263 dest = cpu_physical_id(vcpu->pre_pcpu);
13264
13265 if (x2apic_enabled())
13266 new.ndst = dest;
13267 else
13268 new.ndst = (dest << 8) & 0xFF00;
13269
13270 /* set 'NV' to 'wakeup vector' */
13271 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013272 } while (cmpxchg64(&pi_desc->control, old.control,
13273 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013274
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013275 /* We should not block the vCPU if an interrupt is posted for it. */
13276 if (pi_test_on(pi_desc) == 1)
13277 __pi_post_block(vcpu);
13278
13279 local_irq_enable();
13280 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013281}
13282
Yunhong Jiangbc225122016-06-13 14:19:58 -070013283static int vmx_pre_block(struct kvm_vcpu *vcpu)
13284{
13285 if (pi_pre_block(vcpu))
13286 return 1;
13287
Yunhong Jiang64672c92016-06-13 14:19:59 -070013288 if (kvm_lapic_hv_timer_in_use(vcpu))
13289 kvm_lapic_switch_to_sw_timer(vcpu);
13290
Yunhong Jiangbc225122016-06-13 14:19:58 -070013291 return 0;
13292}
13293
13294static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013295{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013296 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013297 return;
13298
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013299 WARN_ON(irqs_disabled());
13300 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013301 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013302 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013303}
13304
Yunhong Jiangbc225122016-06-13 14:19:58 -070013305static void vmx_post_block(struct kvm_vcpu *vcpu)
13306{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013307 if (kvm_x86_ops->set_hv_timer)
13308 kvm_lapic_switch_to_hv_timer(vcpu);
13309
Yunhong Jiangbc225122016-06-13 14:19:58 -070013310 pi_post_block(vcpu);
13311}
13312
Feng Wubf9f6ac2015-09-18 22:29:55 +080013313/*
Feng Wuefc64402015-09-18 22:29:51 +080013314 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13315 *
13316 * @kvm: kvm
13317 * @host_irq: host irq of the interrupt
13318 * @guest_irq: gsi of the interrupt
13319 * @set: set or unset PI
13320 * returns 0 on success, < 0 on failure
13321 */
13322static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13323 uint32_t guest_irq, bool set)
13324{
13325 struct kvm_kernel_irq_routing_entry *e;
13326 struct kvm_irq_routing_table *irq_rt;
13327 struct kvm_lapic_irq irq;
13328 struct kvm_vcpu *vcpu;
13329 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013330 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013331
13332 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013333 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13334 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013335 return 0;
13336
13337 idx = srcu_read_lock(&kvm->irq_srcu);
13338 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013339 if (guest_irq >= irq_rt->nr_rt_entries ||
13340 hlist_empty(&irq_rt->map[guest_irq])) {
13341 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13342 guest_irq, irq_rt->nr_rt_entries);
13343 goto out;
13344 }
Feng Wuefc64402015-09-18 22:29:51 +080013345
13346 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13347 if (e->type != KVM_IRQ_ROUTING_MSI)
13348 continue;
13349 /*
13350 * VT-d PI cannot support posting multicast/broadcast
13351 * interrupts to a vCPU, we still use interrupt remapping
13352 * for these kind of interrupts.
13353 *
13354 * For lowest-priority interrupts, we only support
13355 * those with single CPU as the destination, e.g. user
13356 * configures the interrupts via /proc/irq or uses
13357 * irqbalance to make the interrupts single-CPU.
13358 *
13359 * We will support full lowest-priority interrupt later.
13360 */
13361
Radim Krčmář371313132016-07-12 22:09:27 +020013362 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013363 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13364 /*
13365 * Make sure the IRTE is in remapped mode if
13366 * we don't handle it in posted mode.
13367 */
13368 ret = irq_set_vcpu_affinity(host_irq, NULL);
13369 if (ret < 0) {
13370 printk(KERN_INFO
13371 "failed to back to remapped mode, irq: %u\n",
13372 host_irq);
13373 goto out;
13374 }
13375
Feng Wuefc64402015-09-18 22:29:51 +080013376 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013377 }
Feng Wuefc64402015-09-18 22:29:51 +080013378
13379 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13380 vcpu_info.vector = irq.vector;
13381
hu huajun2698d822018-04-11 15:16:40 +080013382 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013383 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13384
13385 if (set)
13386 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013387 else
Feng Wuefc64402015-09-18 22:29:51 +080013388 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013389
13390 if (ret < 0) {
13391 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13392 __func__);
13393 goto out;
13394 }
13395 }
13396
13397 ret = 0;
13398out:
13399 srcu_read_unlock(&kvm->irq_srcu, idx);
13400 return ret;
13401}
13402
Ashok Rajc45dcc72016-06-22 14:59:56 +080013403static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13404{
13405 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13406 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13407 FEATURE_CONTROL_LMCE;
13408 else
13409 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13410 ~FEATURE_CONTROL_LMCE;
13411}
13412
Ladi Prosek72d7b372017-10-11 16:54:41 +020013413static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13414{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013415 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13416 if (to_vmx(vcpu)->nested.nested_run_pending)
13417 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013418 return 1;
13419}
13420
Ladi Prosek0234bf82017-10-11 16:54:40 +020013421static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13422{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013423 struct vcpu_vmx *vmx = to_vmx(vcpu);
13424
13425 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13426 if (vmx->nested.smm.guest_mode)
13427 nested_vmx_vmexit(vcpu, -1, 0, 0);
13428
13429 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13430 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013431 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013432 return 0;
13433}
13434
13435static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13436{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013437 struct vcpu_vmx *vmx = to_vmx(vcpu);
13438 int ret;
13439
13440 if (vmx->nested.smm.vmxon) {
13441 vmx->nested.vmxon = true;
13442 vmx->nested.smm.vmxon = false;
13443 }
13444
13445 if (vmx->nested.smm.guest_mode) {
13446 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013447 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013448 vcpu->arch.hflags |= HF_SMM_MASK;
13449 if (ret)
13450 return ret;
13451
13452 vmx->nested.smm.guest_mode = false;
13453 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013454 return 0;
13455}
13456
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013457static int enable_smi_window(struct kvm_vcpu *vcpu)
13458{
13459 return 0;
13460}
13461
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013462static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13463 struct kvm_nested_state __user *user_kvm_nested_state,
13464 u32 user_data_size)
13465{
13466 struct vcpu_vmx *vmx;
13467 struct vmcs12 *vmcs12;
13468 struct kvm_nested_state kvm_state = {
13469 .flags = 0,
13470 .format = 0,
13471 .size = sizeof(kvm_state),
13472 .vmx.vmxon_pa = -1ull,
13473 .vmx.vmcs_pa = -1ull,
13474 };
13475
13476 if (!vcpu)
13477 return kvm_state.size + 2 * VMCS12_SIZE;
13478
13479 vmx = to_vmx(vcpu);
13480 vmcs12 = get_vmcs12(vcpu);
13481 if (nested_vmx_allowed(vcpu) &&
13482 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13483 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13484 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13485
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013486 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013487 kvm_state.size += VMCS12_SIZE;
13488
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013489 if (is_guest_mode(vcpu) &&
13490 nested_cpu_has_shadow_vmcs(vmcs12) &&
13491 vmcs12->vmcs_link_pointer != -1ull)
13492 kvm_state.size += VMCS12_SIZE;
13493 }
13494
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013495 if (vmx->nested.smm.vmxon)
13496 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13497
13498 if (vmx->nested.smm.guest_mode)
13499 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13500
13501 if (is_guest_mode(vcpu)) {
13502 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13503
13504 if (vmx->nested.nested_run_pending)
13505 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13506 }
13507 }
13508
13509 if (user_data_size < kvm_state.size)
13510 goto out;
13511
13512 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13513 return -EFAULT;
13514
13515 if (vmx->nested.current_vmptr == -1ull)
13516 goto out;
13517
13518 /*
13519 * When running L2, the authoritative vmcs12 state is in the
13520 * vmcs02. When running L1, the authoritative vmcs12 state is
13521 * in the shadow vmcs linked to vmcs01, unless
13522 * sync_shadow_vmcs is set, in which case, the authoritative
13523 * vmcs12 state is in the vmcs12 already.
13524 */
13525 if (is_guest_mode(vcpu))
13526 sync_vmcs12(vcpu, vmcs12);
13527 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13528 copy_shadow_to_vmcs12(vmx);
13529
13530 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13531 return -EFAULT;
13532
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013533 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13534 vmcs12->vmcs_link_pointer != -1ull) {
13535 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13536 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13537 return -EFAULT;
13538 }
13539
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013540out:
13541 return kvm_state.size;
13542}
13543
13544static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13545 struct kvm_nested_state __user *user_kvm_nested_state,
13546 struct kvm_nested_state *kvm_state)
13547{
13548 struct vcpu_vmx *vmx = to_vmx(vcpu);
13549 struct vmcs12 *vmcs12;
13550 u32 exit_qual;
13551 int ret;
13552
13553 if (kvm_state->format != 0)
13554 return -EINVAL;
13555
13556 if (!nested_vmx_allowed(vcpu))
13557 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13558
13559 if (kvm_state->vmx.vmxon_pa == -1ull) {
13560 if (kvm_state->vmx.smm.flags)
13561 return -EINVAL;
13562
13563 if (kvm_state->vmx.vmcs_pa != -1ull)
13564 return -EINVAL;
13565
13566 vmx_leave_nested(vcpu);
13567 return 0;
13568 }
13569
13570 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13571 return -EINVAL;
13572
13573 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13574 return -EINVAL;
13575
13576 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13577 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13578 return -EINVAL;
13579
13580 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13581 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13582 return -EINVAL;
13583
13584 if (kvm_state->vmx.smm.flags &
13585 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13586 return -EINVAL;
13587
13588 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13589 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13590 return -EINVAL;
13591
13592 vmx_leave_nested(vcpu);
13593 if (kvm_state->vmx.vmxon_pa == -1ull)
13594 return 0;
13595
13596 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13597 ret = enter_vmx_operation(vcpu);
13598 if (ret)
13599 return ret;
13600
13601 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13602
13603 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13604 vmx->nested.smm.vmxon = true;
13605 vmx->nested.vmxon = false;
13606
13607 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13608 vmx->nested.smm.guest_mode = true;
13609 }
13610
13611 vmcs12 = get_vmcs12(vcpu);
13612 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13613 return -EFAULT;
13614
Liran Alon392b2f22018-06-23 02:35:01 +030013615 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013616 return -EINVAL;
13617
13618 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13619 return 0;
13620
13621 vmx->nested.nested_run_pending =
13622 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13623
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013624 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13625 vmcs12->vmcs_link_pointer != -1ull) {
13626 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13627 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13628 return -EINVAL;
13629
13630 if (copy_from_user(shadow_vmcs12,
13631 user_kvm_nested_state->data + VMCS12_SIZE,
13632 sizeof(*vmcs12)))
13633 return -EFAULT;
13634
13635 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13636 !shadow_vmcs12->hdr.shadow_vmcs)
13637 return -EINVAL;
13638 }
13639
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013640 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13641 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13642 return -EINVAL;
13643
13644 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13645 vmx->nested.nested_run_pending = 1;
13646
13647 vmx->nested.dirty_vmcs12 = true;
13648 ret = enter_vmx_non_root_mode(vcpu, NULL);
13649 if (ret)
13650 return -EINVAL;
13651
13652 return 0;
13653}
13654
Kees Cook404f6aa2016-08-08 16:29:06 -070013655static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013656 .cpu_has_kvm_support = cpu_has_kvm_support,
13657 .disabled_by_bios = vmx_disabled_by_bios,
13658 .hardware_setup = hardware_setup,
13659 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013660 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013661 .hardware_enable = hardware_enable,
13662 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013663 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013664 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013665
Wanpeng Lib31c1142018-03-12 04:53:04 -070013666 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013667 .vm_alloc = vmx_vm_alloc,
13668 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013669
Avi Kivity6aa8b732006-12-10 02:21:36 -080013670 .vcpu_create = vmx_create_vcpu,
13671 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013672 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013673
Avi Kivity04d2cc72007-09-10 18:10:54 +030013674 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013675 .vcpu_load = vmx_vcpu_load,
13676 .vcpu_put = vmx_vcpu_put,
13677
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013678 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013679 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013680 .get_msr = vmx_get_msr,
13681 .set_msr = vmx_set_msr,
13682 .get_segment_base = vmx_get_segment_base,
13683 .get_segment = vmx_get_segment,
13684 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020013685 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013686 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020013687 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020013688 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030013689 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013690 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013691 .set_cr3 = vmx_set_cr3,
13692 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013693 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013694 .get_idt = vmx_get_idt,
13695 .set_idt = vmx_set_idt,
13696 .get_gdt = vmx_get_gdt,
13697 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010013698 .get_dr6 = vmx_get_dr6,
13699 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030013700 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010013701 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030013702 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013703 .get_rflags = vmx_get_rflags,
13704 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080013705
Avi Kivity6aa8b732006-12-10 02:21:36 -080013706 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070013707 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013708
Avi Kivity6aa8b732006-12-10 02:21:36 -080013709 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020013710 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013711 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040013712 .set_interrupt_shadow = vmx_set_interrupt_shadow,
13713 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020013714 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030013715 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013716 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020013717 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030013718 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020013719 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013720 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010013721 .get_nmi_mask = vmx_get_nmi_mask,
13722 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013723 .enable_nmi_window = enable_nmi_window,
13724 .enable_irq_window = enable_irq_window,
13725 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040013726 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080013727 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030013728 .get_enable_apicv = vmx_get_enable_apicv,
13729 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013730 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010013731 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080013732 .hwapic_irr_update = vmx_hwapic_irr_update,
13733 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080013734 .sync_pir_to_irr = vmx_sync_pir_to_irr,
13735 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030013736
Izik Eiduscbc94022007-10-25 00:29:55 +020013737 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070013738 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080013739 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080013740 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030013741
Avi Kivity586f9602010-11-18 13:09:54 +020013742 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020013743
Sheng Yang17cc3932010-01-05 19:02:27 +080013744 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080013745
13746 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080013747
13748 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000013749 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020013750
13751 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080013752
13753 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013754
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013755 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100013756 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020013757
13758 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013759
13760 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080013761 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000013762 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080013763 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020013764 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013765
13766 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013767
13768 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080013769
13770 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
13771 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
13772 .flush_log_dirty = vmx_flush_log_dirty,
13773 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040013774 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020013775
Feng Wubf9f6ac2015-09-18 22:29:55 +080013776 .pre_block = vmx_pre_block,
13777 .post_block = vmx_post_block,
13778
Wei Huang25462f72015-06-19 15:45:05 +020013779 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080013780
13781 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070013782
13783#ifdef CONFIG_X86_64
13784 .set_hv_timer = vmx_set_hv_timer,
13785 .cancel_hv_timer = vmx_cancel_hv_timer,
13786#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080013787
13788 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013789
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013790 .get_nested_state = vmx_get_nested_state,
13791 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013792 .get_vmcs12_pages = nested_get_vmcs12_pages,
13793
Ladi Prosek72d7b372017-10-11 16:54:41 +020013794 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020013795 .pre_enter_smm = vmx_pre_enter_smm,
13796 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013797 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013798};
13799
13800static int __init vmx_init(void)
13801{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013802 int r;
13803
13804#if IS_ENABLED(CONFIG_HYPERV)
13805 /*
13806 * Enlightened VMCS usage should be recommended and the host needs
13807 * to support eVMCS v1 or above. We can also disable eVMCS support
13808 * with module parameter.
13809 */
13810 if (enlightened_vmcs &&
13811 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
13812 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
13813 KVM_EVMCS_VERSION) {
13814 int cpu;
13815
13816 /* Check that we have assist pages on all online CPUs */
13817 for_each_online_cpu(cpu) {
13818 if (!hv_get_vp_assist_page(cpu)) {
13819 enlightened_vmcs = false;
13820 break;
13821 }
13822 }
13823
13824 if (enlightened_vmcs) {
13825 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
13826 static_branch_enable(&enable_evmcs);
13827 }
13828 } else {
13829 enlightened_vmcs = false;
13830 }
13831#endif
13832
13833 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013834 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030013835 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080013836 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080013837
Dave Young2965faa2015-09-09 15:38:55 -070013838#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013839 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
13840 crash_vmclear_local_loaded_vmcss);
13841#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070013842 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013843
He, Qingfdef3ad2007-04-30 09:45:24 +030013844 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080013845}
13846
13847static void __exit vmx_exit(void)
13848{
Dave Young2965faa2015-09-09 15:38:55 -070013849#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053013850 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080013851 synchronize_rcu();
13852#endif
13853
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080013854 kvm_exit();
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010013855
13856#if IS_ENABLED(CONFIG_HYPERV)
13857 if (static_branch_unlikely(&enable_evmcs)) {
13858 int cpu;
13859 struct hv_vp_assist_page *vp_ap;
13860 /*
13861 * Reset everything to support using non-enlightened VMCS
13862 * access later (e.g. when we reload the module with
13863 * enlightened_vmcs=0)
13864 */
13865 for_each_online_cpu(cpu) {
13866 vp_ap = hv_get_vp_assist_page(cpu);
13867
13868 if (!vp_ap)
13869 continue;
13870
13871 vp_ap->current_nested_vmcs = 0;
13872 vp_ap->enlighten_vmentry = 0;
13873 }
13874
13875 static_branch_disable(&enable_evmcs);
13876 }
13877#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -080013878}
13879
13880module_init(vmx_init)
13881module_exit(vmx_exit)