blob: e868c06939cd9fc4face71b7aa71bdd3f1727d69 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeuf935893a2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williamseb99bd62018-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>
Paolo Bonzinia175d512018-02-22 16:43:17 +010053#include <asm/microcode.h>
Thomas Gleixner7a2d2352018-04-29 15:01:37 +020054#include <asm/spec-ctrl.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080055
Marcelo Tosatti229456f2009-06-17 09:22:14 -030056#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020057#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058
Avi Kivity4ecac3f2008-05-13 13:23:38 +030059#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040060#define __ex_clear(x, reg) \
61 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062
Avi Kivity6aa8b732006-12-10 02:21:36 -080063MODULE_AUTHOR("Qumranet");
64MODULE_LICENSE("GPL");
65
Josh Triplette9bda3b2012-03-20 23:33:51 -070066static const struct x86_cpu_id vmx_cpu_id[] = {
67 X86_FEATURE_MATCH(X86_FEATURE_VMX),
68 {}
69};
70MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
71
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040072static bool __read_mostly nosmt;
73module_param(nosmt, bool, S_IRUGO);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Rusty Russell476bc002012-01-13 09:32:18 +103078static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020079module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020080
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070085module_param_named(unrestricted_guest,
86 enable_unrestricted_guest, bool, S_IRUGO);
87
Xudong Hao83c3a332012-05-28 19:33:35 +080088static bool __read_mostly enable_ept_ad_bits = 1;
89module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
90
Avi Kivitya27685c2012-06-12 20:30:18 +030091static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020092module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030093
Rusty Russell476bc002012-01-13 09:32:18 +103094static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080095module_param(vmm_exclusive, bool, S_IRUGO);
96
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini6236b782018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
124#define MSR_BITMAP_MODE_LM 4
125
Haozhong Zhang64903d62015-10-20 15:39:09 +0800126#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127
Yunhong Jiang64672c92016-06-13 14:19:59 -0700128/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
129static int __read_mostly cpu_preemption_timer_multi;
130static bool __read_mostly enable_preemption_timer = 1;
131#ifdef CONFIG_X86_64
132module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133#endif
134
Gleb Natapov50378782013-02-04 16:00:28 +0200135#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
136#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200137#define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | 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 \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700141 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Avi Kivitycdc0e242009-12-06 17:21:14 +0200143#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
144#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
145
Avi Kivity78ac8b42010-04-08 18:19:35 +0300146#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
147
Jan Kiszkaf4124502014-03-07 20:03:13 +0100148#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
149
Jan Dakinevich04c776e2018-02-23 11:42:18 +0100150#define VMX_VPID_EXTENT_SUPPORTED_MASK \
151 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
152 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
153 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
154 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
155
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800156/*
157 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
158 * ple_gap: upper bound on the amount of time between two successive
159 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500160 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161 * ple_window: upper bound on the amount of time a guest is allowed to execute
162 * in a PAUSE loop. Tests indicate that most spinlocks are held for
163 * less than 2^12 cycles
164 * Time is measured based on a counter that runs at the same rate as the TSC,
165 * refer SDM volume 3b section 21.6.13 & 22.1.3.
166 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200167#define KVM_VMX_DEFAULT_PLE_GAP 128
168#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
169#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
170#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
171#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
172 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
173
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800174static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
175module_param(ple_gap, int, S_IRUGO);
176
177static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
178module_param(ple_window, int, S_IRUGO);
179
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180/* Default doubles per-vcpu window every exit. */
181static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
182module_param(ple_window_grow, int, S_IRUGO);
183
184/* Default resets per-vcpu window every exit to ple_window. */
185static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
186module_param(ple_window_shrink, int, S_IRUGO);
187
188/* Default is to compute the maximum so we can never overflow. */
189static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
190static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
191module_param(ple_window_max, int, S_IRUGO);
192
Avi Kivity83287ea422012-09-16 15:10:57 +0300193extern const ulong vmx_return;
194
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +0200195static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
196
197/* These MUST be in sync with vmentry_l1d_param order. */
198enum vmx_l1d_flush_state {
199 VMENTER_L1D_FLUSH_NEVER,
200 VMENTER_L1D_FLUSH_COND,
201 VMENTER_L1D_FLUSH_ALWAYS,
202};
203
204static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush = VMENTER_L1D_FLUSH_COND;
205
206static const struct {
207 const char *option;
208 enum vmx_l1d_flush_state cmd;
209} vmentry_l1d_param[] = {
210 {"never", VMENTER_L1D_FLUSH_NEVER},
211 {"cond", VMENTER_L1D_FLUSH_COND},
212 {"always", VMENTER_L1D_FLUSH_ALWAYS},
213};
214
215static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
216{
217 unsigned int i;
218
219 if (!s)
220 return -EINVAL;
221
222 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
223 if (!strcmp(s, vmentry_l1d_param[i].option)) {
224 vmentry_l1d_flush = vmentry_l1d_param[i].cmd;
225 return 0;
226 }
227 }
228
229 return -EINVAL;
230}
231
232static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
233{
234 return sprintf(s, "%s\n", vmentry_l1d_param[vmentry_l1d_flush].option);
235}
236
237static const struct kernel_param_ops vmentry_l1d_flush_ops = {
238 .set = vmentry_l1d_flush_set,
239 .get = vmentry_l1d_flush_get,
240};
241module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, &vmentry_l1d_flush, S_IRUGO);
242
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200243#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300244
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400245struct vmcs {
246 u32 revision_id;
247 u32 abort;
248 char data[0];
249};
250
Nadav Har'Eld462b812011-05-24 15:26:10 +0300251/*
252 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
253 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
254 * loaded on this CPU (so we can clear them if the CPU goes down).
255 */
256struct loaded_vmcs {
257 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700258 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300259 int cpu;
260 int launched;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100261 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300262 struct list_head loaded_vmcss_on_cpu_link;
263};
264
Avi Kivity26bb0982009-09-07 11:14:12 +0300265struct shared_msr_entry {
266 unsigned index;
267 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200268 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300269};
270
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300271/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300272 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
273 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
274 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
275 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
276 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
277 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattson46e24df2017-11-27 17:22:25 -0600278 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300279 * underlying hardware which will be used to run L2.
280 * This structure is packed to ensure that its layout is identical across
281 * machines (necessary for live migration).
282 * If there are changes in this struct, VMCS12_REVISION must be changed.
283 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300284typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300285struct __packed vmcs12 {
286 /* According to the Intel spec, a VMCS region must start with the
287 * following two fields. Then follow implementation-specific data.
288 */
289 u32 revision_id;
290 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300291
Nadav Har'El27d6c862011-05-25 23:06:59 +0300292 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
293 u32 padding[7]; /* room for future expansion */
294
Nadav Har'El22bd0352011-05-25 23:05:57 +0300295 u64 io_bitmap_a;
296 u64 io_bitmap_b;
297 u64 msr_bitmap;
298 u64 vm_exit_msr_store_addr;
299 u64 vm_exit_msr_load_addr;
300 u64 vm_entry_msr_load_addr;
301 u64 tsc_offset;
302 u64 virtual_apic_page_addr;
303 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800304 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300305 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800306 u64 eoi_exit_bitmap0;
307 u64 eoi_exit_bitmap1;
308 u64 eoi_exit_bitmap2;
309 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800310 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300311 u64 guest_physical_address;
312 u64 vmcs_link_pointer;
313 u64 guest_ia32_debugctl;
314 u64 guest_ia32_pat;
315 u64 guest_ia32_efer;
316 u64 guest_ia32_perf_global_ctrl;
317 u64 guest_pdptr0;
318 u64 guest_pdptr1;
319 u64 guest_pdptr2;
320 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100321 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300322 u64 host_ia32_pat;
323 u64 host_ia32_efer;
324 u64 host_ia32_perf_global_ctrl;
325 u64 padding64[8]; /* room for future expansion */
326 /*
327 * To allow migration of L1 (complete with its L2 guests) between
328 * machines of different natural widths (32 or 64 bit), we cannot have
329 * unsigned long fields with no explict size. We use u64 (aliased
330 * natural_width) instead. Luckily, x86 is little-endian.
331 */
332 natural_width cr0_guest_host_mask;
333 natural_width cr4_guest_host_mask;
334 natural_width cr0_read_shadow;
335 natural_width cr4_read_shadow;
336 natural_width cr3_target_value0;
337 natural_width cr3_target_value1;
338 natural_width cr3_target_value2;
339 natural_width cr3_target_value3;
340 natural_width exit_qualification;
341 natural_width guest_linear_address;
342 natural_width guest_cr0;
343 natural_width guest_cr3;
344 natural_width guest_cr4;
345 natural_width guest_es_base;
346 natural_width guest_cs_base;
347 natural_width guest_ss_base;
348 natural_width guest_ds_base;
349 natural_width guest_fs_base;
350 natural_width guest_gs_base;
351 natural_width guest_ldtr_base;
352 natural_width guest_tr_base;
353 natural_width guest_gdtr_base;
354 natural_width guest_idtr_base;
355 natural_width guest_dr7;
356 natural_width guest_rsp;
357 natural_width guest_rip;
358 natural_width guest_rflags;
359 natural_width guest_pending_dbg_exceptions;
360 natural_width guest_sysenter_esp;
361 natural_width guest_sysenter_eip;
362 natural_width host_cr0;
363 natural_width host_cr3;
364 natural_width host_cr4;
365 natural_width host_fs_base;
366 natural_width host_gs_base;
367 natural_width host_tr_base;
368 natural_width host_gdtr_base;
369 natural_width host_idtr_base;
370 natural_width host_ia32_sysenter_esp;
371 natural_width host_ia32_sysenter_eip;
372 natural_width host_rsp;
373 natural_width host_rip;
374 natural_width paddingl[8]; /* room for future expansion */
375 u32 pin_based_vm_exec_control;
376 u32 cpu_based_vm_exec_control;
377 u32 exception_bitmap;
378 u32 page_fault_error_code_mask;
379 u32 page_fault_error_code_match;
380 u32 cr3_target_count;
381 u32 vm_exit_controls;
382 u32 vm_exit_msr_store_count;
383 u32 vm_exit_msr_load_count;
384 u32 vm_entry_controls;
385 u32 vm_entry_msr_load_count;
386 u32 vm_entry_intr_info_field;
387 u32 vm_entry_exception_error_code;
388 u32 vm_entry_instruction_len;
389 u32 tpr_threshold;
390 u32 secondary_vm_exec_control;
391 u32 vm_instruction_error;
392 u32 vm_exit_reason;
393 u32 vm_exit_intr_info;
394 u32 vm_exit_intr_error_code;
395 u32 idt_vectoring_info_field;
396 u32 idt_vectoring_error_code;
397 u32 vm_exit_instruction_len;
398 u32 vmx_instruction_info;
399 u32 guest_es_limit;
400 u32 guest_cs_limit;
401 u32 guest_ss_limit;
402 u32 guest_ds_limit;
403 u32 guest_fs_limit;
404 u32 guest_gs_limit;
405 u32 guest_ldtr_limit;
406 u32 guest_tr_limit;
407 u32 guest_gdtr_limit;
408 u32 guest_idtr_limit;
409 u32 guest_es_ar_bytes;
410 u32 guest_cs_ar_bytes;
411 u32 guest_ss_ar_bytes;
412 u32 guest_ds_ar_bytes;
413 u32 guest_fs_ar_bytes;
414 u32 guest_gs_ar_bytes;
415 u32 guest_ldtr_ar_bytes;
416 u32 guest_tr_ar_bytes;
417 u32 guest_interruptibility_info;
418 u32 guest_activity_state;
419 u32 guest_sysenter_cs;
420 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100421 u32 vmx_preemption_timer_value;
422 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300423 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800424 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300425 u16 guest_es_selector;
426 u16 guest_cs_selector;
427 u16 guest_ss_selector;
428 u16 guest_ds_selector;
429 u16 guest_fs_selector;
430 u16 guest_gs_selector;
431 u16 guest_ldtr_selector;
432 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800433 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300434 u16 host_es_selector;
435 u16 host_cs_selector;
436 u16 host_ss_selector;
437 u16 host_ds_selector;
438 u16 host_fs_selector;
439 u16 host_gs_selector;
440 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300441};
442
443/*
444 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
445 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
446 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
447 */
448#define VMCS12_REVISION 0x11e57ed0
449
450/*
451 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
452 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
453 * current implementation, 4K are reserved to avoid future complications.
454 */
455#define VMCS12_SIZE 0x1000
456
457/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300458 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
459 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
460 */
461struct nested_vmx {
462 /* Has the level1 guest done vmxon? */
463 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400464 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300465
466 /* The guest-physical address of the current VMCS L1 keeps for L2 */
467 gpa_t current_vmptr;
468 /* The host-usable pointer to the above */
469 struct page *current_vmcs12_page;
470 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700471 /*
472 * Cache of the guest's VMCS, existing outside of guest memory.
473 * Loaded from guest memory during VMPTRLD. Flushed to guest
474 * memory during VMXOFF, VMCLEAR, VMPTRLD.
475 */
476 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300477 /*
478 * Indicates if the shadow vmcs must be updated with the
479 * data hold by vmcs12
480 */
481 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300482
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200483 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300484 /* L2 must run next, and mustn't decide to exit to L1. */
485 bool nested_run_pending;
Jim Mattson46e24df2017-11-27 17:22:25 -0600486
487 struct loaded_vmcs vmcs02;
488
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300489 /*
Jim Mattson46e24df2017-11-27 17:22:25 -0600490 * Guest pages referred to in the vmcs02 with host-physical
491 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300492 */
493 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800494 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800495 struct page *pi_desc_page;
496 struct pi_desc *pi_desc;
497 bool pi_pending;
498 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100499
500 struct hrtimer preemption_timer;
501 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200502
503 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
504 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800505
Wanpeng Li5c614b32015-10-13 09:18:36 -0700506 u16 vpid02;
507 u16 last_vpid;
508
Wincy Vanb9c237b2015-02-03 23:56:30 +0800509 u32 nested_vmx_procbased_ctls_low;
510 u32 nested_vmx_procbased_ctls_high;
511 u32 nested_vmx_true_procbased_ctls_low;
512 u32 nested_vmx_secondary_ctls_low;
513 u32 nested_vmx_secondary_ctls_high;
514 u32 nested_vmx_pinbased_ctls_low;
515 u32 nested_vmx_pinbased_ctls_high;
516 u32 nested_vmx_exit_ctls_low;
517 u32 nested_vmx_exit_ctls_high;
518 u32 nested_vmx_true_exit_ctls_low;
519 u32 nested_vmx_entry_ctls_low;
520 u32 nested_vmx_entry_ctls_high;
521 u32 nested_vmx_true_entry_ctls_low;
522 u32 nested_vmx_misc_low;
523 u32 nested_vmx_misc_high;
524 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700525 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300526};
527
Yang Zhang01e439b2013-04-11 19:25:12 +0800528#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800529#define POSTED_INTR_SN 1
530
Yang Zhang01e439b2013-04-11 19:25:12 +0800531/* Posted-Interrupt Descriptor */
532struct pi_desc {
533 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800534 union {
535 struct {
536 /* bit 256 - Outstanding Notification */
537 u16 on : 1,
538 /* bit 257 - Suppress Notification */
539 sn : 1,
540 /* bit 271:258 - Reserved */
541 rsvd_1 : 14;
542 /* bit 279:272 - Notification Vector */
543 u8 nv;
544 /* bit 287:280 - Reserved */
545 u8 rsvd_2;
546 /* bit 319:288 - Notification Destination */
547 u32 ndst;
548 };
549 u64 control;
550 };
551 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800552} __aligned(64);
553
Yang Zhanga20ed542013-04-11 19:25:15 +0800554static bool pi_test_and_set_on(struct pi_desc *pi_desc)
555{
556 return test_and_set_bit(POSTED_INTR_ON,
557 (unsigned long *)&pi_desc->control);
558}
559
560static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
561{
562 return test_and_clear_bit(POSTED_INTR_ON,
563 (unsigned long *)&pi_desc->control);
564}
565
566static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
567{
568 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
569}
570
Feng Wuebbfc762015-09-18 22:29:46 +0800571static inline void pi_clear_sn(struct pi_desc *pi_desc)
572{
573 return clear_bit(POSTED_INTR_SN,
574 (unsigned long *)&pi_desc->control);
575}
576
577static inline void pi_set_sn(struct pi_desc *pi_desc)
578{
579 return set_bit(POSTED_INTR_SN,
580 (unsigned long *)&pi_desc->control);
581}
582
583static inline int pi_test_on(struct pi_desc *pi_desc)
584{
585 return test_bit(POSTED_INTR_ON,
586 (unsigned long *)&pi_desc->control);
587}
588
589static inline int pi_test_sn(struct pi_desc *pi_desc)
590{
591 return test_bit(POSTED_INTR_SN,
592 (unsigned long *)&pi_desc->control);
593}
594
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400595struct vmx_msrs {
596 unsigned int nr;
597 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
598};
599
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400600struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000601 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300602 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300603 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200604 bool nmi_known_unmasked;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100605 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300606 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200607 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200608 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300609 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400610 int nmsrs;
611 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800612 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400613#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300614 u64 msr_host_kernel_gs_base;
615 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400616#endif
Ashok Raj70131292018-02-01 22:59:43 +0100617
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100618 u64 arch_capabilities;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +0100619 u64 spec_ctrl;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100620
Gleb Natapov2961e8762013-11-25 15:37:13 +0200621 u32 vm_entry_controls_shadow;
622 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300623 /*
624 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
625 * non-nested (L1) guest, it always points to vmcs01. For a nested
626 * guest (L2), it points to a different VMCS.
627 */
628 struct loaded_vmcs vmcs01;
629 struct loaded_vmcs *loaded_vmcs;
630 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300631 struct msr_autoload {
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -0400632 struct vmx_msrs guest;
633 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300634 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400635 struct {
636 int loaded;
637 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300638#ifdef CONFIG_X86_64
639 u16 ds_sel, es_sel;
640#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200641 int gs_ldt_reload_needed;
642 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000643 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700644 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400645 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200646 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300647 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300648 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300649 struct kvm_segment segs[8];
650 } rmode;
651 struct {
652 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300653 struct kvm_save_segment {
654 u16 selector;
655 unsigned long base;
656 u32 limit;
657 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300658 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300659 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800660 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300661 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200662
663 /* Support for vnmi-less CPUs */
664 int soft_vnmi_blocked;
665 ktime_t entry_time;
666 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800667 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800668
Yang Zhang01e439b2013-04-11 19:25:12 +0800669 /* Posted interrupt descriptor */
670 struct pi_desc pi_desc;
671
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300672 /* Support for a guest hypervisor (nested VMX) */
673 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200674
675 /* Dynamic PLE window. */
676 int ple_window;
677 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800678
679 /* Support for PML */
680#define PML_ENTITY_NUM 512
681 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800682
Yunhong Jiang64672c92016-06-13 14:19:59 -0700683 /* apic deadline value in host tsc */
684 u64 hv_deadline_tsc;
685
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800686 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800687
688 bool guest_pkru_valid;
689 u32 guest_pkru;
690 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800691
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800692 /*
693 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
694 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
695 * in msr_ia32_feature_control_valid_bits.
696 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800697 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800698 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400699};
700
Avi Kivity2fb92db2011-04-27 19:42:18 +0300701enum segment_cache_field {
702 SEG_FIELD_SEL = 0,
703 SEG_FIELD_BASE = 1,
704 SEG_FIELD_LIMIT = 2,
705 SEG_FIELD_AR = 3,
706
707 SEG_FIELD_NR = 4
708};
709
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400710static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
711{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000712 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400713}
714
Feng Wuefc64402015-09-18 22:29:51 +0800715static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
716{
717 return &(to_vmx(vcpu)->pi_desc);
718}
719
Nadav Har'El22bd0352011-05-25 23:05:57 +0300720#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
721#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
722#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
723 [number##_HIGH] = VMCS12_OFFSET(name)+4
724
Abel Gordon4607c2d2013-04-18 14:35:55 +0300725
Bandan Dasfe2b2012014-04-21 15:20:14 -0400726static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300727 /*
728 * We do NOT shadow fields that are modified when L0
729 * traps and emulates any vmx instruction (e.g. VMPTRLD,
730 * VMXON...) executed by L1.
731 * For example, VM_INSTRUCTION_ERROR is read
732 * by L1 if a vmx instruction fails (part of the error path).
733 * Note the code assumes this logic. If for some reason
734 * we start shadowing these fields then we need to
735 * force a shadow sync when L0 emulates vmx instructions
736 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
737 * by nested_vmx_failValid)
738 */
739 VM_EXIT_REASON,
740 VM_EXIT_INTR_INFO,
741 VM_EXIT_INSTRUCTION_LEN,
742 IDT_VECTORING_INFO_FIELD,
743 IDT_VECTORING_ERROR_CODE,
744 VM_EXIT_INTR_ERROR_CODE,
745 EXIT_QUALIFICATION,
746 GUEST_LINEAR_ADDRESS,
747 GUEST_PHYSICAL_ADDRESS
748};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400749static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300750 ARRAY_SIZE(shadow_read_only_fields);
751
Bandan Dasfe2b2012014-04-21 15:20:14 -0400752static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800753 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300754 GUEST_RIP,
755 GUEST_RSP,
756 GUEST_CR0,
757 GUEST_CR3,
758 GUEST_CR4,
759 GUEST_INTERRUPTIBILITY_INFO,
760 GUEST_RFLAGS,
761 GUEST_CS_SELECTOR,
762 GUEST_CS_AR_BYTES,
763 GUEST_CS_LIMIT,
764 GUEST_CS_BASE,
765 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100766 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300767 CR0_GUEST_HOST_MASK,
768 CR0_READ_SHADOW,
769 CR4_READ_SHADOW,
770 TSC_OFFSET,
771 EXCEPTION_BITMAP,
772 CPU_BASED_VM_EXEC_CONTROL,
773 VM_ENTRY_EXCEPTION_ERROR_CODE,
774 VM_ENTRY_INTR_INFO_FIELD,
775 VM_ENTRY_INSTRUCTION_LEN,
776 VM_ENTRY_EXCEPTION_ERROR_CODE,
777 HOST_FS_BASE,
778 HOST_GS_BASE,
779 HOST_FS_SELECTOR,
780 HOST_GS_SELECTOR
781};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400782static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300783 ARRAY_SIZE(shadow_read_write_fields);
784
Mathias Krause772e0312012-08-30 01:30:19 +0200785static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300786 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800787 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300788 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
789 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
790 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
791 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
792 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
793 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
794 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
795 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800796 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300797 FIELD(HOST_ES_SELECTOR, host_es_selector),
798 FIELD(HOST_CS_SELECTOR, host_cs_selector),
799 FIELD(HOST_SS_SELECTOR, host_ss_selector),
800 FIELD(HOST_DS_SELECTOR, host_ds_selector),
801 FIELD(HOST_FS_SELECTOR, host_fs_selector),
802 FIELD(HOST_GS_SELECTOR, host_gs_selector),
803 FIELD(HOST_TR_SELECTOR, host_tr_selector),
804 FIELD64(IO_BITMAP_A, io_bitmap_a),
805 FIELD64(IO_BITMAP_B, io_bitmap_b),
806 FIELD64(MSR_BITMAP, msr_bitmap),
807 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
808 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
809 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
810 FIELD64(TSC_OFFSET, tsc_offset),
811 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
812 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800813 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300814 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800815 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
816 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
817 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
818 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800819 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300820 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
821 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
822 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
823 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
824 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
825 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
826 FIELD64(GUEST_PDPTR0, guest_pdptr0),
827 FIELD64(GUEST_PDPTR1, guest_pdptr1),
828 FIELD64(GUEST_PDPTR2, guest_pdptr2),
829 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100830 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300831 FIELD64(HOST_IA32_PAT, host_ia32_pat),
832 FIELD64(HOST_IA32_EFER, host_ia32_efer),
833 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
834 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
835 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
836 FIELD(EXCEPTION_BITMAP, exception_bitmap),
837 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
838 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
839 FIELD(CR3_TARGET_COUNT, cr3_target_count),
840 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
841 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
842 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
843 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
844 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
845 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
846 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
847 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
848 FIELD(TPR_THRESHOLD, tpr_threshold),
849 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
850 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
851 FIELD(VM_EXIT_REASON, vm_exit_reason),
852 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
853 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
854 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
855 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
856 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
857 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
858 FIELD(GUEST_ES_LIMIT, guest_es_limit),
859 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
860 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
861 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
862 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
863 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
864 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
865 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
866 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
867 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
868 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
869 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
870 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
871 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
872 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
873 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
874 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
875 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
876 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
877 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
878 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
879 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100880 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300881 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
882 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
883 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
884 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
885 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
886 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
887 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
888 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
889 FIELD(EXIT_QUALIFICATION, exit_qualification),
890 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
891 FIELD(GUEST_CR0, guest_cr0),
892 FIELD(GUEST_CR3, guest_cr3),
893 FIELD(GUEST_CR4, guest_cr4),
894 FIELD(GUEST_ES_BASE, guest_es_base),
895 FIELD(GUEST_CS_BASE, guest_cs_base),
896 FIELD(GUEST_SS_BASE, guest_ss_base),
897 FIELD(GUEST_DS_BASE, guest_ds_base),
898 FIELD(GUEST_FS_BASE, guest_fs_base),
899 FIELD(GUEST_GS_BASE, guest_gs_base),
900 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
901 FIELD(GUEST_TR_BASE, guest_tr_base),
902 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
903 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
904 FIELD(GUEST_DR7, guest_dr7),
905 FIELD(GUEST_RSP, guest_rsp),
906 FIELD(GUEST_RIP, guest_rip),
907 FIELD(GUEST_RFLAGS, guest_rflags),
908 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
909 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
910 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
911 FIELD(HOST_CR0, host_cr0),
912 FIELD(HOST_CR3, host_cr3),
913 FIELD(HOST_CR4, host_cr4),
914 FIELD(HOST_FS_BASE, host_fs_base),
915 FIELD(HOST_GS_BASE, host_gs_base),
916 FIELD(HOST_TR_BASE, host_tr_base),
917 FIELD(HOST_GDTR_BASE, host_gdtr_base),
918 FIELD(HOST_IDTR_BASE, host_idtr_base),
919 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
920 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
921 FIELD(HOST_RSP, host_rsp),
922 FIELD(HOST_RIP, host_rip),
923};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300924
925static inline short vmcs_field_to_offset(unsigned long field)
926{
Dan Williamseb99bd62018-01-31 17:47:03 -0800927 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
928 unsigned short offset;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100929
Dan Williamseb99bd62018-01-31 17:47:03 -0800930 BUILD_BUG_ON(size > SHRT_MAX);
931 if (field >= size)
Andrew Honig012df712018-01-10 10:12:03 -0800932 return -ENOENT;
933
Dan Williamseb99bd62018-01-31 17:47:03 -0800934 field = array_index_nospec(field, size);
935 offset = vmcs_field_to_offset_table[field];
936 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100937 return -ENOENT;
Dan Williamseb99bd62018-01-31 17:47:03 -0800938 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300939}
940
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300941static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
942{
David Matlack4f2777b2016-07-13 17:16:37 -0700943 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300944}
945
946static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
947{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200948 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800949 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300950 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800951
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300952 return page;
953}
954
955static void nested_release_page(struct page *page)
956{
957 kvm_release_page_dirty(page);
958}
959
960static void nested_release_page_clean(struct page *page)
961{
962 kvm_release_page_clean(page);
963}
964
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300965static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800966static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800967static void kvm_cpu_vmxon(u64 addr);
968static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800969static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200970static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300971static void vmx_set_segment(struct kvm_vcpu *vcpu,
972 struct kvm_segment *var, int seg);
973static void vmx_get_segment(struct kvm_vcpu *vcpu,
974 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200975static bool guest_state_valid(struct kvm_vcpu *vcpu);
976static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300977static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300978static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800979static int alloc_identity_pagetable(struct kvm *kvm);
Paolo Bonzini6236b782018-01-16 16:51:18 +0100980static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj70131292018-02-01 22:59:43 +0100981static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
982 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300983
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984static DEFINE_PER_CPU(struct vmcs *, vmxarea);
985static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300986/*
987 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
988 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
989 */
990static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300991static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992
Feng Wubf9f6ac2015-09-18 22:29:55 +0800993/*
994 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
995 * can find which vCPU should be waken up.
996 */
997static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
998static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
999
Avi Kivity3e7c73e2009-02-24 21:46:19 +02001000static unsigned long *vmx_io_bitmap_a;
1001static unsigned long *vmx_io_bitmap_b;
Abel Gordon4607c2d2013-04-18 14:35:55 +03001002static unsigned long *vmx_vmread_bitmap;
1003static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +03001004
Avi Kivity110312c2010-12-21 12:54:20 +02001005static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001006static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001007
Sheng Yang2384d2b2008-01-17 15:14:33 +08001008static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1009static DEFINE_SPINLOCK(vmx_vpid_lock);
1010
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001011static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 int size;
1013 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001014 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001015 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001016 u32 pin_based_exec_ctrl;
1017 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001018 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001019 u32 vmexit_ctrl;
1020 u32 vmentry_ctrl;
1021} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022
Hannes Ederefff9e52008-11-28 17:02:06 +01001023static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001024 u32 ept;
1025 u32 vpid;
1026} vmx_capability;
1027
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028#define VMX_SEGMENT_FIELD(seg) \
1029 [VCPU_SREG_##seg] = { \
1030 .selector = GUEST_##seg##_SELECTOR, \
1031 .base = GUEST_##seg##_BASE, \
1032 .limit = GUEST_##seg##_LIMIT, \
1033 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1034 }
1035
Mathias Krause772e0312012-08-30 01:30:19 +02001036static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001037 unsigned selector;
1038 unsigned base;
1039 unsigned limit;
1040 unsigned ar_bytes;
1041} kvm_vmx_segment_fields[] = {
1042 VMX_SEGMENT_FIELD(CS),
1043 VMX_SEGMENT_FIELD(DS),
1044 VMX_SEGMENT_FIELD(ES),
1045 VMX_SEGMENT_FIELD(FS),
1046 VMX_SEGMENT_FIELD(GS),
1047 VMX_SEGMENT_FIELD(SS),
1048 VMX_SEGMENT_FIELD(TR),
1049 VMX_SEGMENT_FIELD(LDTR),
1050};
1051
Avi Kivity26bb0982009-09-07 11:14:12 +03001052static u64 host_efer;
1053
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001054static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1055
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001056/*
Brian Gerst8c065852010-07-17 09:03:26 -04001057 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001058 * away by decrementing the array size.
1059 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001061#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001062 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001064 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066
Jan Kiszka5bb16012016-02-09 20:14:21 +01001067static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068{
1069 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1070 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001071 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1072}
1073
Jan Kiszka6f054852016-02-09 20:15:18 +01001074static inline bool is_debug(u32 intr_info)
1075{
1076 return is_exception_n(intr_info, DB_VECTOR);
1077}
1078
1079static inline bool is_breakpoint(u32 intr_info)
1080{
1081 return is_exception_n(intr_info, BP_VECTOR);
1082}
1083
Jan Kiszka5bb16012016-02-09 20:14:21 +01001084static inline bool is_page_fault(u32 intr_info)
1085{
1086 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087}
1088
Gui Jianfeng31299942010-03-15 17:29:09 +08001089static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001090{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001091 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001092}
1093
Gui Jianfeng31299942010-03-15 17:29:09 +08001094static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001095{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001096 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001097}
1098
Gui Jianfeng31299942010-03-15 17:29:09 +08001099static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100{
1101 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1102 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1103}
1104
Gui Jianfeng31299942010-03-15 17:29:09 +08001105static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001106{
1107 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1108 INTR_INFO_VALID_MASK)) ==
1109 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1110}
1111
Linus Torvalds587da2b2018-03-20 12:16:59 -07001112/* Undocumented: icebp/int1 */
1113static inline bool is_icebp(u32 intr_info)
1114{
1115 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1116 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1117}
1118
Gui Jianfeng31299942010-03-15 17:29:09 +08001119static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001120{
Sheng Yang04547152009-04-01 15:52:31 +08001121 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001122}
1123
Gui Jianfeng31299942010-03-15 17:29:09 +08001124static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001125{
Sheng Yang04547152009-04-01 15:52:31 +08001126 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001127}
1128
Paolo Bonzini35754c92015-07-29 12:05:37 +02001129static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001130{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001131 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001132}
1133
Gui Jianfeng31299942010-03-15 17:29:09 +08001134static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001135{
Sheng Yang04547152009-04-01 15:52:31 +08001136 return vmcs_config.cpu_based_exec_ctrl &
1137 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001138}
1139
Avi Kivity774ead32007-12-26 13:57:04 +02001140static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001141{
Sheng Yang04547152009-04-01 15:52:31 +08001142 return vmcs_config.cpu_based_2nd_exec_ctrl &
1143 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1144}
1145
Yang Zhang8d146952013-01-25 10:18:50 +08001146static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1147{
1148 return vmcs_config.cpu_based_2nd_exec_ctrl &
1149 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1150}
1151
Yang Zhang83d4c282013-01-25 10:18:49 +08001152static inline bool cpu_has_vmx_apic_register_virt(void)
1153{
1154 return vmcs_config.cpu_based_2nd_exec_ctrl &
1155 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1156}
1157
Yang Zhangc7c9c562013-01-25 10:18:51 +08001158static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1159{
1160 return vmcs_config.cpu_based_2nd_exec_ctrl &
1161 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1162}
1163
Yunhong Jiang64672c92016-06-13 14:19:59 -07001164/*
1165 * Comment's format: document - errata name - stepping - processor name.
1166 * Refer from
1167 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1168 */
1169static u32 vmx_preemption_cpu_tfms[] = {
1170/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11710x000206E6,
1172/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1173/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1174/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11750x00020652,
1176/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11770x00020655,
1178/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1179/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1180/*
1181 * 320767.pdf - AAP86 - B1 -
1182 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1183 */
11840x000106E5,
1185/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11860x000106A0,
1187/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11880x000106A1,
1189/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11900x000106A4,
1191 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1192 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1193 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11940x000106A5,
1195};
1196
1197static inline bool cpu_has_broken_vmx_preemption_timer(void)
1198{
1199 u32 eax = cpuid_eax(0x00000001), i;
1200
1201 /* Clear the reserved bits */
1202 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001203 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001204 if (eax == vmx_preemption_cpu_tfms[i])
1205 return true;
1206
1207 return false;
1208}
1209
1210static inline bool cpu_has_vmx_preemption_timer(void)
1211{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001212 return vmcs_config.pin_based_exec_ctrl &
1213 PIN_BASED_VMX_PREEMPTION_TIMER;
1214}
1215
Yang Zhang01e439b2013-04-11 19:25:12 +08001216static inline bool cpu_has_vmx_posted_intr(void)
1217{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001218 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1219 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001220}
1221
1222static inline bool cpu_has_vmx_apicv(void)
1223{
1224 return cpu_has_vmx_apic_register_virt() &&
1225 cpu_has_vmx_virtual_intr_delivery() &&
1226 cpu_has_vmx_posted_intr();
1227}
1228
Sheng Yang04547152009-04-01 15:52:31 +08001229static inline bool cpu_has_vmx_flexpriority(void)
1230{
1231 return cpu_has_vmx_tpr_shadow() &&
1232 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001233}
1234
Marcelo Tosattie7997942009-06-11 12:07:40 -03001235static inline bool cpu_has_vmx_ept_execute_only(void)
1236{
Gui Jianfeng31299942010-03-15 17:29:09 +08001237 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001238}
1239
Marcelo Tosattie7997942009-06-11 12:07:40 -03001240static inline bool cpu_has_vmx_ept_2m_page(void)
1241{
Gui Jianfeng31299942010-03-15 17:29:09 +08001242 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001243}
1244
Sheng Yang878403b2010-01-05 19:02:29 +08001245static inline bool cpu_has_vmx_ept_1g_page(void)
1246{
Gui Jianfeng31299942010-03-15 17:29:09 +08001247 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001248}
1249
Sheng Yang4bc9b982010-06-02 14:05:24 +08001250static inline bool cpu_has_vmx_ept_4levels(void)
1251{
1252 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1253}
1254
Xudong Hao83c3a332012-05-28 19:33:35 +08001255static inline bool cpu_has_vmx_ept_ad_bits(void)
1256{
1257 return vmx_capability.ept & VMX_EPT_AD_BIT;
1258}
1259
Gui Jianfeng31299942010-03-15 17:29:09 +08001260static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001261{
Gui Jianfeng31299942010-03-15 17:29:09 +08001262 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001263}
1264
Gui Jianfeng31299942010-03-15 17:29:09 +08001265static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001266{
Gui Jianfeng31299942010-03-15 17:29:09 +08001267 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001268}
1269
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001270static inline bool cpu_has_vmx_invvpid_single(void)
1271{
1272 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1273}
1274
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001275static inline bool cpu_has_vmx_invvpid_global(void)
1276{
1277 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1278}
1279
Wanpeng Li2df19692017-03-23 05:30:08 -07001280static inline bool cpu_has_vmx_invvpid(void)
1281{
1282 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1283}
1284
Gui Jianfeng31299942010-03-15 17:29:09 +08001285static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001286{
Sheng Yang04547152009-04-01 15:52:31 +08001287 return vmcs_config.cpu_based_2nd_exec_ctrl &
1288 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001289}
1290
Gui Jianfeng31299942010-03-15 17:29:09 +08001291static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001292{
1293 return vmcs_config.cpu_based_2nd_exec_ctrl &
1294 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1295}
1296
Gui Jianfeng31299942010-03-15 17:29:09 +08001297static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001298{
1299 return vmcs_config.cpu_based_2nd_exec_ctrl &
1300 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1301}
1302
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001303static inline bool cpu_has_vmx_basic_inout(void)
1304{
1305 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1306}
1307
Paolo Bonzini35754c92015-07-29 12:05:37 +02001308static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001309{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001310 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001311}
1312
Gui Jianfeng31299942010-03-15 17:29:09 +08001313static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001314{
Sheng Yang04547152009-04-01 15:52:31 +08001315 return vmcs_config.cpu_based_2nd_exec_ctrl &
1316 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001317}
1318
Gui Jianfeng31299942010-03-15 17:29:09 +08001319static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001320{
1321 return vmcs_config.cpu_based_2nd_exec_ctrl &
1322 SECONDARY_EXEC_RDTSCP;
1323}
1324
Mao, Junjiead756a12012-07-02 01:18:48 +00001325static inline bool cpu_has_vmx_invpcid(void)
1326{
1327 return vmcs_config.cpu_based_2nd_exec_ctrl &
1328 SECONDARY_EXEC_ENABLE_INVPCID;
1329}
1330
Gui Jianfeng31299942010-03-15 17:29:09 +08001331static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001332{
1333 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1334}
1335
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001336static inline bool cpu_has_vmx_wbinvd_exit(void)
1337{
1338 return vmcs_config.cpu_based_2nd_exec_ctrl &
1339 SECONDARY_EXEC_WBINVD_EXITING;
1340}
1341
Abel Gordonabc4fc52013-04-18 14:35:25 +03001342static inline bool cpu_has_vmx_shadow_vmcs(void)
1343{
1344 u64 vmx_msr;
1345 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1346 /* check if the cpu supports writing r/o exit information fields */
1347 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1348 return false;
1349
1350 return vmcs_config.cpu_based_2nd_exec_ctrl &
1351 SECONDARY_EXEC_SHADOW_VMCS;
1352}
1353
Kai Huang843e4332015-01-28 10:54:28 +08001354static inline bool cpu_has_vmx_pml(void)
1355{
1356 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1357}
1358
Haozhong Zhang64903d62015-10-20 15:39:09 +08001359static inline bool cpu_has_vmx_tsc_scaling(void)
1360{
1361 return vmcs_config.cpu_based_2nd_exec_ctrl &
1362 SECONDARY_EXEC_TSC_SCALING;
1363}
1364
Sheng Yang04547152009-04-01 15:52:31 +08001365static inline bool report_flexpriority(void)
1366{
1367 return flexpriority_enabled;
1368}
1369
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001370static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1371{
1372 return vmcs12->cpu_based_vm_exec_control & bit;
1373}
1374
1375static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1376{
1377 return (vmcs12->cpu_based_vm_exec_control &
1378 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1379 (vmcs12->secondary_vm_exec_control & bit);
1380}
1381
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001382static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001383{
1384 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1385}
1386
Jan Kiszkaf4124502014-03-07 20:03:13 +01001387static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1388{
1389 return vmcs12->pin_based_vm_exec_control &
1390 PIN_BASED_VMX_PREEMPTION_TIMER;
1391}
1392
Nadav Har'El155a97a2013-08-05 11:07:16 +03001393static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1394{
1395 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1396}
1397
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001398static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1399{
1400 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1401 vmx_xsaves_supported();
1402}
1403
Wincy Vanf2b93282015-02-03 23:56:03 +08001404static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1405{
1406 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1407}
1408
Wanpeng Li5c614b32015-10-13 09:18:36 -07001409static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1410{
1411 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1412}
1413
Wincy Van82f0dd42015-02-03 23:57:18 +08001414static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1415{
1416 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1417}
1418
Wincy Van608406e2015-02-03 23:57:51 +08001419static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1420{
1421 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1422}
1423
Wincy Van705699a2015-02-03 23:58:17 +08001424static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1425{
1426 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1427}
1428
Jim Mattson3f618a02016-12-12 11:01:37 -08001429static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001430{
1431 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattson3f618a02016-12-12 11:01:37 -08001432 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001433}
1434
Jan Kiszka533558b2014-01-04 18:47:20 +01001435static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1436 u32 exit_intr_info,
1437 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001438static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1439 struct vmcs12 *vmcs12,
1440 u32 reason, unsigned long qualification);
1441
Rusty Russell8b9cf982007-07-30 16:31:43 +10001442static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001443{
1444 int i;
1445
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001446 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001447 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001448 return i;
1449 return -1;
1450}
1451
Sheng Yang2384d2b2008-01-17 15:14:33 +08001452static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1453{
1454 struct {
1455 u64 vpid : 16;
1456 u64 rsvd : 48;
1457 u64 gva;
1458 } operand = { vpid, 0, gva };
1459
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001460 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001461 /* CF==1 or ZF==1 --> rc = -1 */
1462 "; ja 1f ; ud2 ; 1:"
1463 : : "a"(&operand), "c"(ext) : "cc", "memory");
1464}
1465
Sheng Yang14394422008-04-28 12:24:45 +08001466static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1467{
1468 struct {
1469 u64 eptp, gpa;
1470 } operand = {eptp, gpa};
1471
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001472 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001473 /* CF==1 or ZF==1 --> rc = -1 */
1474 "; ja 1f ; ud2 ; 1:\n"
1475 : : "a" (&operand), "c" (ext) : "cc", "memory");
1476}
1477
Avi Kivity26bb0982009-09-07 11:14:12 +03001478static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001479{
1480 int i;
1481
Rusty Russell8b9cf982007-07-30 16:31:43 +10001482 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001483 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001484 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001485 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001486}
1487
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488static void vmcs_clear(struct vmcs *vmcs)
1489{
1490 u64 phys_addr = __pa(vmcs);
1491 u8 error;
1492
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001493 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001494 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 : "cc", "memory");
1496 if (error)
1497 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1498 vmcs, phys_addr);
1499}
1500
Nadav Har'Eld462b812011-05-24 15:26:10 +03001501static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1502{
1503 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001504 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1505 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001506 loaded_vmcs->cpu = -1;
1507 loaded_vmcs->launched = 0;
1508}
1509
Dongxiao Xu7725b892010-05-11 18:29:38 +08001510static void vmcs_load(struct vmcs *vmcs)
1511{
1512 u64 phys_addr = __pa(vmcs);
1513 u8 error;
1514
1515 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001516 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001517 : "cc", "memory");
1518 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001519 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001520 vmcs, phys_addr);
1521}
1522
Dave Young2965faa2015-09-09 15:38:55 -07001523#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001524/*
1525 * This bitmap is used to indicate whether the vmclear
1526 * operation is enabled on all cpus. All disabled by
1527 * default.
1528 */
1529static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1530
1531static inline void crash_enable_local_vmclear(int cpu)
1532{
1533 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1534}
1535
1536static inline void crash_disable_local_vmclear(int cpu)
1537{
1538 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1539}
1540
1541static inline int crash_local_vmclear_enabled(int cpu)
1542{
1543 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1544}
1545
1546static void crash_vmclear_local_loaded_vmcss(void)
1547{
1548 int cpu = raw_smp_processor_id();
1549 struct loaded_vmcs *v;
1550
1551 if (!crash_local_vmclear_enabled(cpu))
1552 return;
1553
1554 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1555 loaded_vmcss_on_cpu_link)
1556 vmcs_clear(v->vmcs);
1557}
1558#else
1559static inline void crash_enable_local_vmclear(int cpu) { }
1560static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001561#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001562
Nadav Har'Eld462b812011-05-24 15:26:10 +03001563static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001564{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001565 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001566 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567
Nadav Har'Eld462b812011-05-24 15:26:10 +03001568 if (loaded_vmcs->cpu != cpu)
1569 return; /* vcpu migration can race with cpu offline */
1570 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001572 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001573 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001574
1575 /*
1576 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1577 * is before setting loaded_vmcs->vcpu to -1 which is done in
1578 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1579 * then adds the vmcs into percpu list before it is deleted.
1580 */
1581 smp_wmb();
1582
Nadav Har'Eld462b812011-05-24 15:26:10 +03001583 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001584 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585}
1586
Nadav Har'Eld462b812011-05-24 15:26:10 +03001587static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001588{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001589 int cpu = loaded_vmcs->cpu;
1590
1591 if (cpu != -1)
1592 smp_call_function_single(cpu,
1593 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001594}
1595
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001596static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001597{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001598 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001599 return;
1600
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001601 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001602 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001603}
1604
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001605static inline void vpid_sync_vcpu_global(void)
1606{
1607 if (cpu_has_vmx_invvpid_global())
1608 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1609}
1610
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001611static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001612{
1613 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001614 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001615 else
1616 vpid_sync_vcpu_global();
1617}
1618
Sheng Yang14394422008-04-28 12:24:45 +08001619static inline void ept_sync_global(void)
1620{
1621 if (cpu_has_vmx_invept_global())
1622 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1623}
1624
1625static inline void ept_sync_context(u64 eptp)
1626{
Avi Kivity089d0342009-03-23 18:26:32 +02001627 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001628 if (cpu_has_vmx_invept_context())
1629 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1630 else
1631 ept_sync_global();
1632 }
1633}
1634
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001635static __always_inline void vmcs_check16(unsigned long field)
1636{
1637 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1638 "16-bit accessor invalid for 64-bit field");
1639 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1640 "16-bit accessor invalid for 64-bit high field");
1641 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1642 "16-bit accessor invalid for 32-bit high field");
1643 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1644 "16-bit accessor invalid for natural width field");
1645}
1646
1647static __always_inline void vmcs_check32(unsigned long field)
1648{
1649 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1650 "32-bit accessor invalid for 16-bit field");
1651 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1652 "32-bit accessor invalid for natural width field");
1653}
1654
1655static __always_inline void vmcs_check64(unsigned long field)
1656{
1657 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1658 "64-bit accessor invalid for 16-bit field");
1659 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1660 "64-bit accessor invalid for 64-bit high field");
1661 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1662 "64-bit accessor invalid for 32-bit field");
1663 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1664 "64-bit accessor invalid for natural width field");
1665}
1666
1667static __always_inline void vmcs_checkl(unsigned long field)
1668{
1669 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1670 "Natural width accessor invalid for 16-bit field");
1671 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1672 "Natural width accessor invalid for 64-bit field");
1673 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1674 "Natural width accessor invalid for 64-bit high field");
1675 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1676 "Natural width accessor invalid for 32-bit field");
1677}
1678
1679static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001680{
Avi Kivity5e520e62011-05-15 10:13:12 -04001681 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682
Avi Kivity5e520e62011-05-15 10:13:12 -04001683 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1684 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685 return value;
1686}
1687
Avi Kivity96304212011-05-15 10:13:13 -04001688static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001690 vmcs_check16(field);
1691 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692}
1693
Avi Kivity96304212011-05-15 10:13:13 -04001694static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001696 vmcs_check32(field);
1697 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698}
1699
Avi Kivity96304212011-05-15 10:13:13 -04001700static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001702 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001703#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001704 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001706 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707#endif
1708}
1709
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001710static __always_inline unsigned long vmcs_readl(unsigned long field)
1711{
1712 vmcs_checkl(field);
1713 return __vmcs_readl(field);
1714}
1715
Avi Kivitye52de1b2007-01-05 16:36:56 -08001716static noinline void vmwrite_error(unsigned long field, unsigned long value)
1717{
1718 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1719 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1720 dump_stack();
1721}
1722
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001723static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724{
1725 u8 error;
1726
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001727 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001728 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001729 if (unlikely(error))
1730 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001731}
1732
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001733static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001735 vmcs_check16(field);
1736 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737}
1738
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001739static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001741 vmcs_check32(field);
1742 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743}
1744
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001745static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001746{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001747 vmcs_check64(field);
1748 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001749#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001751 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752#endif
1753}
1754
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001755static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001756{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001757 vmcs_checkl(field);
1758 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001759}
1760
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001761static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001762{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001763 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1764 "vmcs_clear_bits does not support 64-bit fields");
1765 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1766}
1767
1768static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1769{
1770 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1771 "vmcs_set_bits does not support 64-bit fields");
1772 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001773}
1774
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001775static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1776{
1777 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1778}
1779
Gleb Natapov2961e8762013-11-25 15:37:13 +02001780static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1781{
1782 vmcs_write32(VM_ENTRY_CONTROLS, val);
1783 vmx->vm_entry_controls_shadow = val;
1784}
1785
1786static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1787{
1788 if (vmx->vm_entry_controls_shadow != val)
1789 vm_entry_controls_init(vmx, val);
1790}
1791
1792static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1793{
1794 return vmx->vm_entry_controls_shadow;
1795}
1796
1797
1798static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1799{
1800 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1801}
1802
1803static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1804{
1805 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1806}
1807
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001808static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1809{
1810 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1811}
1812
Gleb Natapov2961e8762013-11-25 15:37:13 +02001813static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1814{
1815 vmcs_write32(VM_EXIT_CONTROLS, val);
1816 vmx->vm_exit_controls_shadow = val;
1817}
1818
1819static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1820{
1821 if (vmx->vm_exit_controls_shadow != val)
1822 vm_exit_controls_init(vmx, val);
1823}
1824
1825static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1826{
1827 return vmx->vm_exit_controls_shadow;
1828}
1829
1830
1831static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1832{
1833 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1834}
1835
1836static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1837{
1838 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1839}
1840
Avi Kivity2fb92db2011-04-27 19:42:18 +03001841static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1842{
1843 vmx->segment_cache.bitmask = 0;
1844}
1845
1846static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1847 unsigned field)
1848{
1849 bool ret;
1850 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1851
1852 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1853 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1854 vmx->segment_cache.bitmask = 0;
1855 }
1856 ret = vmx->segment_cache.bitmask & mask;
1857 vmx->segment_cache.bitmask |= mask;
1858 return ret;
1859}
1860
1861static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1862{
1863 u16 *p = &vmx->segment_cache.seg[seg].selector;
1864
1865 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1866 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1867 return *p;
1868}
1869
1870static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1871{
1872 ulong *p = &vmx->segment_cache.seg[seg].base;
1873
1874 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1875 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1876 return *p;
1877}
1878
1879static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1880{
1881 u32 *p = &vmx->segment_cache.seg[seg].limit;
1882
1883 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1884 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1885 return *p;
1886}
1887
1888static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1889{
1890 u32 *p = &vmx->segment_cache.seg[seg].ar;
1891
1892 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1893 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1894 return *p;
1895}
1896
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001897static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1898{
1899 u32 eb;
1900
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001901 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01001902 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001903 if ((vcpu->guest_debug &
1904 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1905 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1906 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001907 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001908 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001909 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001910 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001911 if (vcpu->fpu_active)
1912 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001913
1914 /* When we are running a nested L2 guest and L1 specified for it a
1915 * certain exception bitmap, we must trap the same exceptions and pass
1916 * them to L1. When running L2, we will only handle the exceptions
1917 * specified above if L1 did not want them.
1918 */
1919 if (is_guest_mode(vcpu))
1920 eb |= get_vmcs12(vcpu)->exception_bitmap;
1921
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001922 vmcs_write32(EXCEPTION_BITMAP, eb);
1923}
1924
Ashok Raj70131292018-02-01 22:59:43 +01001925/*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01001926 * Check if MSR is intercepted for currently loaded MSR bitmap.
1927 */
1928static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
1929{
1930 unsigned long *msr_bitmap;
1931 int f = sizeof(unsigned long);
1932
1933 if (!cpu_has_vmx_msr_bitmap())
1934 return true;
1935
1936 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
1937
1938 if (msr <= 0x1fff) {
1939 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1940 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1941 msr &= 0x1fff;
1942 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1943 }
1944
1945 return true;
1946}
1947
1948/*
Ashok Raj70131292018-02-01 22:59:43 +01001949 * Check if MSR is intercepted for L01 MSR bitmap.
1950 */
1951static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
1952{
1953 unsigned long *msr_bitmap;
1954 int f = sizeof(unsigned long);
1955
1956 if (!cpu_has_vmx_msr_bitmap())
1957 return true;
1958
1959 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
1960
1961 if (msr <= 0x1fff) {
1962 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1963 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1964 msr &= 0x1fff;
1965 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1966 }
1967
1968 return true;
1969}
1970
Gleb Natapov2961e8762013-11-25 15:37:13 +02001971static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1972 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001973{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001974 vm_entry_controls_clearbit(vmx, entry);
1975 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001976}
1977
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04001978static int find_msr(struct vmx_msrs *m, unsigned int msr)
1979{
1980 unsigned int i;
1981
1982 for (i = 0; i < m->nr; ++i) {
1983 if (m->val[i].index == msr)
1984 return i;
1985 }
1986 return -ENOENT;
1987}
1988
Avi Kivity61d2ef22010-04-28 16:40:38 +03001989static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1990{
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04001991 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001992 struct msr_autoload *m = &vmx->msr_autoload;
1993
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001994 switch (msr) {
1995 case MSR_EFER:
1996 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001997 clear_atomic_switch_msr_special(vmx,
1998 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001999 VM_EXIT_LOAD_IA32_EFER);
2000 return;
2001 }
2002 break;
2003 case MSR_CORE_PERF_GLOBAL_CTRL:
2004 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002005 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002006 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2007 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2008 return;
2009 }
2010 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002011 }
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002012 i = find_msr(&m->guest, msr);
2013 if (i < 0)
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002014 goto skip_guest;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002015 --m->guest.nr;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002016 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002017 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002018
2019skip_guest:
2020 i = find_msr(&m->host, msr);
2021 if (i < 0)
2022 return;
2023
2024 --m->host.nr;
2025 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002026 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002027}
2028
Gleb Natapov2961e8762013-11-25 15:37:13 +02002029static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2030 unsigned long entry, unsigned long exit,
2031 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2032 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002033{
2034 vmcs_write64(guest_val_vmcs, guest_val);
2035 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002036 vm_entry_controls_setbit(vmx, entry);
2037 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002038}
2039
Avi Kivity61d2ef22010-04-28 16:40:38 +03002040static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002041 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002042{
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002043 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002044 struct msr_autoload *m = &vmx->msr_autoload;
2045
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002046 switch (msr) {
2047 case MSR_EFER:
2048 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002049 add_atomic_switch_msr_special(vmx,
2050 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002051 VM_EXIT_LOAD_IA32_EFER,
2052 GUEST_IA32_EFER,
2053 HOST_IA32_EFER,
2054 guest_val, host_val);
2055 return;
2056 }
2057 break;
2058 case MSR_CORE_PERF_GLOBAL_CTRL:
2059 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002060 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002061 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2062 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2063 GUEST_IA32_PERF_GLOBAL_CTRL,
2064 HOST_IA32_PERF_GLOBAL_CTRL,
2065 guest_val, host_val);
2066 return;
2067 }
2068 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002069 case MSR_IA32_PEBS_ENABLE:
2070 /* PEBS needs a quiescent period after being disabled (to write
2071 * a record). Disabling PEBS through VMX MSR swapping doesn't
2072 * provide that period, so a CPU could write host's record into
2073 * guest's memory.
2074 */
2075 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002076 }
2077
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002078 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002079 if (!entry_only)
2080 j = find_msr(&m->host, msr);
2081
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002082 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002083 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002084 "Can't add msr %x\n", msr);
2085 return;
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002086 }
2087 if (i < 0) {
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002088 i = m->guest.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002089 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002090 }
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002091 m->guest.val[i].index = msr;
2092 m->guest.val[i].value = guest_val;
2093
2094 if (entry_only)
2095 return;
2096
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002097 if (j < 0) {
2098 j = m->host.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002099 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002100 }
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002101 m->host.val[j].index = msr;
2102 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002103}
2104
Avi Kivity33ed6322007-05-02 16:54:03 +03002105static void reload_tss(void)
2106{
Avi Kivity33ed6322007-05-02 16:54:03 +03002107 /*
2108 * VT restores TR but not its size. Useless.
2109 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002110 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002111 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002112
Avi Kivityd3591922010-07-26 18:32:39 +03002113 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002114 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2115 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002116}
2117
Avi Kivity92c0d902009-10-29 11:00:16 +02002118static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002119{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002120 u64 guest_efer = vmx->vcpu.arch.efer;
2121 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002122
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002123 if (!enable_ept) {
2124 /*
2125 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2126 * host CPUID is more efficient than testing guest CPUID
2127 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2128 */
2129 if (boot_cpu_has(X86_FEATURE_SMEP))
2130 guest_efer |= EFER_NX;
2131 else if (!(guest_efer & EFER_NX))
2132 ignore_bits |= EFER_NX;
2133 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002134
Avi Kivity51c6cf62007-08-29 03:48:05 +03002135 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002136 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002137 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002138 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002139#ifdef CONFIG_X86_64
2140 ignore_bits |= EFER_LMA | EFER_LME;
2141 /* SCE is meaningful only in long mode on Intel */
2142 if (guest_efer & EFER_LMA)
2143 ignore_bits &= ~(u64)EFER_SCE;
2144#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002145
2146 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002147
2148 /*
2149 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2150 * On CPUs that support "load IA32_EFER", always switch EFER
2151 * atomically, since it's faster than switching it manually.
2152 */
2153 if (cpu_has_load_ia32_efer ||
2154 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002155 if (!(guest_efer & EFER_LMA))
2156 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002157 if (guest_efer != host_efer)
2158 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04002159 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002160 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002161 } else {
2162 guest_efer &= ~ignore_bits;
2163 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002164
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002165 vmx->guest_msrs[efer_offset].data = guest_efer;
2166 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2167
2168 return true;
2169 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002170}
2171
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002172static unsigned long segment_base(u16 selector)
2173{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002174 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002175 struct desc_struct *d;
2176 unsigned long table_base;
2177 unsigned long v;
2178
2179 if (!(selector & ~3))
2180 return 0;
2181
Avi Kivityd3591922010-07-26 18:32:39 +03002182 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002183
2184 if (selector & 4) { /* from ldt */
2185 u16 ldt_selector = kvm_read_ldt();
2186
2187 if (!(ldt_selector & ~3))
2188 return 0;
2189
2190 table_base = segment_base(ldt_selector);
2191 }
2192 d = (struct desc_struct *)(table_base + (selector & ~7));
2193 v = get_desc_base(d);
2194#ifdef CONFIG_X86_64
2195 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2196 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2197#endif
2198 return v;
2199}
2200
2201static inline unsigned long kvm_read_tr_base(void)
2202{
2203 u16 tr;
2204 asm("str %0" : "=g"(tr));
2205 return segment_base(tr);
2206}
2207
Avi Kivity04d2cc72007-09-10 18:10:54 +03002208static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002209{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002210 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002211 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002212
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002213 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002214 return;
2215
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002216 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002217 /*
2218 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2219 * allow segment selectors with cpl > 0 or ti == 1.
2220 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002221 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002222 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002223 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002224 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002225 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002226 vmx->host_state.fs_reload_needed = 0;
2227 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002228 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002229 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002230 }
Avi Kivity9581d442010-10-19 16:46:55 +02002231 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002232 if (!(vmx->host_state.gs_sel & 7))
2233 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002234 else {
2235 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002236 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002237 }
2238
2239#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002240 savesegment(ds, vmx->host_state.ds_sel);
2241 savesegment(es, vmx->host_state.es_sel);
2242#endif
2243
2244#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002245 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2246 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2247#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002248 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2249 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002250#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002251
2252#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002253 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2254 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002255 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002256#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002257 if (boot_cpu_has(X86_FEATURE_MPX))
2258 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002259 for (i = 0; i < vmx->save_nmsrs; ++i)
2260 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002261 vmx->guest_msrs[i].data,
2262 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002263}
2264
Avi Kivitya9b21b62008-06-24 11:48:49 +03002265static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002266{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002267 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002268 return;
2269
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002270 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002271 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002272#ifdef CONFIG_X86_64
2273 if (is_long_mode(&vmx->vcpu))
2274 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2275#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002276 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002277 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002278#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002279 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002280#else
2281 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002282#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002283 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002284 if (vmx->host_state.fs_reload_needed)
2285 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002286#ifdef CONFIG_X86_64
2287 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2288 loadsegment(ds, vmx->host_state.ds_sel);
2289 loadsegment(es, vmx->host_state.es_sel);
2290 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002291#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002292 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002293#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002294 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002295#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002296 if (vmx->host_state.msr_host_bndcfgs)
2297 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002298 /*
2299 * If the FPU is not active (through the host task or
2300 * the guest vcpu), then restore the cr0.TS bit.
2301 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002302 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002303 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002304 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002305}
2306
Avi Kivitya9b21b62008-06-24 11:48:49 +03002307static void vmx_load_host_state(struct vcpu_vmx *vmx)
2308{
2309 preempt_disable();
2310 __vmx_load_host_state(vmx);
2311 preempt_enable();
2312}
2313
Feng Wu28b835d2015-09-18 22:29:54 +08002314static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2315{
2316 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2317 struct pi_desc old, new;
2318 unsigned int dest;
2319
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002320 /*
2321 * In case of hot-plug or hot-unplug, we may have to undo
2322 * vmx_vcpu_pi_put even if there is no assigned device. And we
2323 * always keep PI.NDST up to date for simplicity: it makes the
2324 * code easier, and CPU migration is not a fast path.
2325 */
2326 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002327 return;
2328
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002329 /*
2330 * First handle the simple case where no cmpxchg is necessary; just
2331 * allow posting non-urgent interrupts.
2332 *
2333 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2334 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2335 * expects the VCPU to be on the blocked_vcpu_list that matches
2336 * PI.NDST.
2337 */
2338 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2339 vcpu->cpu == cpu) {
2340 pi_clear_sn(pi_desc);
2341 return;
2342 }
2343
2344 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002345 do {
2346 old.control = new.control = pi_desc->control;
2347
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002348 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002349
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002350 if (x2apic_enabled())
2351 new.ndst = dest;
2352 else
2353 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002354
Feng Wu28b835d2015-09-18 22:29:54 +08002355 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002356 } while (cmpxchg64(&pi_desc->control, old.control,
2357 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002358}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002359
Peter Feinerc95ba922016-08-17 09:36:47 -07002360static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2361{
2362 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2363 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2364}
2365
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366/*
2367 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2368 * vcpu mutex is already taken.
2369 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002370static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002372 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002373 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002374 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002376 if (!vmm_exclusive)
2377 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002378 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002379 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002381 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002382 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002383 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002384
2385 /*
2386 * Read loaded_vmcs->cpu should be before fetching
2387 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2388 * See the comments in __loaded_vmcs_clear().
2389 */
2390 smp_rmb();
2391
Nadav Har'Eld462b812011-05-24 15:26:10 +03002392 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2393 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002394 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002395 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002396 }
2397
2398 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2399 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2400 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002401 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002402 }
2403
2404 if (!already_loaded) {
2405 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2406 unsigned long sysenter_esp;
2407
2408 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002409
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410 /*
2411 * Linux uses per-cpu TSS and GDT, so set these when switching
2412 * processors.
2413 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002414 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002415 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416
2417 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2418 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002419
Nadav Har'Eld462b812011-05-24 15:26:10 +03002420 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 }
Feng Wu28b835d2015-09-18 22:29:54 +08002422
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002423 /* Setup TSC multiplier */
2424 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002425 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2426 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002427
Feng Wu28b835d2015-09-18 22:29:54 +08002428 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002429 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002430}
2431
2432static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2433{
2434 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2435
2436 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002437 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2438 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002439 return;
2440
2441 /* Set SN when the vCPU is preempted */
2442 if (vcpu->preempted)
2443 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444}
2445
2446static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2447{
Feng Wu28b835d2015-09-18 22:29:54 +08002448 vmx_vcpu_pi_put(vcpu);
2449
Avi Kivitya9b21b62008-06-24 11:48:49 +03002450 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002451 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002452 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2453 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002454 kvm_cpu_vmxoff();
2455 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002456}
2457
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002458static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2459{
Avi Kivity81231c62010-01-24 16:26:40 +02002460 ulong cr0;
2461
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002462 if (vcpu->fpu_active)
2463 return;
2464 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002465 cr0 = vmcs_readl(GUEST_CR0);
2466 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2467 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2468 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002469 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002470 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002471 if (is_guest_mode(vcpu))
2472 vcpu->arch.cr0_guest_owned_bits &=
2473 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002474 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002475}
2476
Avi Kivityedcafe32009-12-30 18:07:40 +02002477static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2478
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002479/*
2480 * Return the cr0 value that a nested guest would read. This is a combination
2481 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2482 * its hypervisor (cr0_read_shadow).
2483 */
2484static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2485{
2486 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2487 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2488}
2489static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2490{
2491 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2492 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2493}
2494
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002495static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2496{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002497 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2498 * set this *before* calling this function.
2499 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002500 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002501 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002502 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002503 vcpu->arch.cr0_guest_owned_bits = 0;
2504 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002505 if (is_guest_mode(vcpu)) {
2506 /*
2507 * L1's specified read shadow might not contain the TS bit,
2508 * so now that we turned on shadowing of this bit, we need to
2509 * set this bit of the shadow. Like in nested_vmx_run we need
2510 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2511 * up-to-date here because we just decached cr0.TS (and we'll
2512 * only update vmcs12->guest_cr0 on nested exit).
2513 */
2514 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2515 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2516 (vcpu->arch.cr0 & X86_CR0_TS);
2517 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2518 } else
2519 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002520}
2521
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2523{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002524 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002525
Avi Kivity6de12732011-03-07 12:51:22 +02002526 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2527 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2528 rflags = vmcs_readl(GUEST_RFLAGS);
2529 if (to_vmx(vcpu)->rmode.vm86_active) {
2530 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2531 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2532 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2533 }
2534 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002535 }
Avi Kivity6de12732011-03-07 12:51:22 +02002536 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537}
2538
2539static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2540{
Avi Kivity6de12732011-03-07 12:51:22 +02002541 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2542 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002543 if (to_vmx(vcpu)->rmode.vm86_active) {
2544 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002545 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002546 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547 vmcs_writel(GUEST_RFLAGS, rflags);
2548}
2549
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002550static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2551{
2552 return to_vmx(vcpu)->guest_pkru;
2553}
2554
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002555static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002556{
2557 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2558 int ret = 0;
2559
2560 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002561 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002562 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002563 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002564
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002565 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002566}
2567
2568static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2569{
2570 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2571 u32 interruptibility = interruptibility_old;
2572
2573 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2574
Jan Kiszka48005f62010-02-19 19:38:07 +01002575 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002576 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002577 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002578 interruptibility |= GUEST_INTR_STATE_STI;
2579
2580 if ((interruptibility != interruptibility_old))
2581 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2582}
2583
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2585{
2586 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002588 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002590 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591
Glauber Costa2809f5d2009-05-12 16:21:05 -04002592 /* skipping an emulated instruction also counts */
2593 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594}
2595
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002596/*
2597 * KVM wants to inject page-faults which it got to the guest. This function
2598 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002599 */
Gleb Natapove011c662013-09-25 12:51:35 +03002600static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002601{
2602 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2603
Gleb Natapove011c662013-09-25 12:51:35 +03002604 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002605 return 0;
2606
Wanpeng Lia29fd272017-06-05 05:19:09 -07002607 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002608 vmcs_read32(VM_EXIT_INTR_INFO),
2609 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002610 return 1;
2611}
2612
Avi Kivity298101d2007-11-25 13:41:11 +02002613static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002614 bool has_error_code, u32 error_code,
2615 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002616{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002617 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002618 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002619
Gleb Natapove011c662013-09-25 12:51:35 +03002620 if (!reinject && is_guest_mode(vcpu) &&
2621 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002622 return;
2623
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002624 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002625 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002626 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2627 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002628
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002629 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002630 int inc_eip = 0;
2631 if (kvm_exception_is_soft(nr))
2632 inc_eip = vcpu->arch.event_exit_inst_len;
2633 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002634 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002635 return;
2636 }
2637
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002638 WARN_ON_ONCE(vmx->emulation_required);
2639
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002640 if (kvm_exception_is_soft(nr)) {
2641 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2642 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002643 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2644 } else
2645 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2646
2647 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002648}
2649
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002650static bool vmx_rdtscp_supported(void)
2651{
2652 return cpu_has_vmx_rdtscp();
2653}
2654
Mao, Junjiead756a12012-07-02 01:18:48 +00002655static bool vmx_invpcid_supported(void)
2656{
2657 return cpu_has_vmx_invpcid() && enable_ept;
2658}
2659
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660/*
Eddie Donga75beee2007-05-17 18:55:15 +03002661 * Swap MSR entry in host/guest MSR entry array.
2662 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002663static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002664{
Avi Kivity26bb0982009-09-07 11:14:12 +03002665 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002666
2667 tmp = vmx->guest_msrs[to];
2668 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2669 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002670}
2671
2672/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002673 * Set up the vmcs to automatically save and restore system
2674 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2675 * mode, as fiddling with msrs is very expensive.
2676 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002677static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002678{
Avi Kivity26bb0982009-09-07 11:14:12 +03002679 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002680
Eddie Donga75beee2007-05-17 18:55:15 +03002681 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002682#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002683 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002684 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002685 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002686 move_msr_up(vmx, index, save_nmsrs++);
2687 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002688 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002689 move_msr_up(vmx, index, save_nmsrs++);
2690 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002691 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002692 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002693 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002694 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002695 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002696 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002697 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002698 * if efer.sce is enabled.
2699 */
Brian Gerst8c065852010-07-17 09:03:26 -04002700 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002701 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002702 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002703 }
Eddie Donga75beee2007-05-17 18:55:15 +03002704#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002705 index = __find_msr_index(vmx, MSR_EFER);
2706 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002707 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002708
Avi Kivity26bb0982009-09-07 11:14:12 +03002709 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002710
Yang Zhang8d146952013-01-25 10:18:50 +08002711 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002712 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002713}
2714
2715/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002717 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2718 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002720static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721{
2722 u64 host_tsc, tsc_offset;
2723
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002724 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002726 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727}
2728
2729/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002730 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002732static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002734 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002735 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002736 * We're here if L1 chose not to trap WRMSR to TSC. According
2737 * to the spec, this should set L1's TSC; The offset that L1
2738 * set for L2 remains unchanged, and still needs to be added
2739 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002740 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002741 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002742 /* recalculate vmcs02.TSC_OFFSET: */
2743 vmcs12 = get_vmcs12(vcpu);
2744 vmcs_write64(TSC_OFFSET, offset +
2745 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2746 vmcs12->tsc_offset : 0));
2747 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002748 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2749 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002750 vmcs_write64(TSC_OFFSET, offset);
2751 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752}
2753
Nadav Har'El801d3422011-05-25 23:02:23 +03002754static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2755{
2756 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2757 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2758}
2759
2760/*
2761 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2762 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2763 * all guests if the "nested" module option is off, and can also be disabled
2764 * for a single guest by disabling its VMX cpuid bit.
2765 */
2766static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2767{
2768 return nested && guest_cpuid_has_vmx(vcpu);
2769}
2770
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002772 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2773 * returned for the various VMX controls MSRs when nested VMX is enabled.
2774 * The same values should also be used to verify that vmcs12 control fields are
2775 * valid during nested entry from L1 to L2.
2776 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2777 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2778 * bit in the high half is on if the corresponding bit in the control field
2779 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002780 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002781static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002782{
2783 /*
2784 * Note that as a general rule, the high half of the MSRs (bits in
2785 * the control fields which may be 1) should be initialized by the
2786 * intersection of the underlying hardware's MSR (i.e., features which
2787 * can be supported) and the list of features we want to expose -
2788 * because they are known to be properly supported in our code.
2789 * Also, usually, the low half of the MSRs (bits which must be 1) can
2790 * be set to 0, meaning that L1 may turn off any of these bits. The
2791 * reason is that if one of these bits is necessary, it will appear
2792 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2793 * fields of vmcs01 and vmcs02, will turn these bits off - and
2794 * nested_vmx_exit_handled() will not pass related exits to L1.
2795 * These rules have exceptions below.
2796 */
2797
2798 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002799 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002800 vmx->nested.nested_vmx_pinbased_ctls_low,
2801 vmx->nested.nested_vmx_pinbased_ctls_high);
2802 vmx->nested.nested_vmx_pinbased_ctls_low |=
2803 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2804 vmx->nested.nested_vmx_pinbased_ctls_high &=
2805 PIN_BASED_EXT_INTR_MASK |
2806 PIN_BASED_NMI_EXITING |
2807 PIN_BASED_VIRTUAL_NMIS;
2808 vmx->nested.nested_vmx_pinbased_ctls_high |=
2809 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002810 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002811 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002812 vmx->nested.nested_vmx_pinbased_ctls_high |=
2813 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002814
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002815 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002816 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002817 vmx->nested.nested_vmx_exit_ctls_low,
2818 vmx->nested.nested_vmx_exit_ctls_high);
2819 vmx->nested.nested_vmx_exit_ctls_low =
2820 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002821
Wincy Vanb9c237b2015-02-03 23:56:30 +08002822 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002823#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002824 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002825#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002826 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002827 vmx->nested.nested_vmx_exit_ctls_high |=
2828 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002829 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002830 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2831
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002832 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002833 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002834
Jan Kiszka2996fca2014-06-16 13:59:43 +02002835 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002836 vmx->nested.nested_vmx_true_exit_ctls_low =
2837 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002838 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2839
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002840 /* entry controls */
2841 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002842 vmx->nested.nested_vmx_entry_ctls_low,
2843 vmx->nested.nested_vmx_entry_ctls_high);
2844 vmx->nested.nested_vmx_entry_ctls_low =
2845 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2846 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002847#ifdef CONFIG_X86_64
2848 VM_ENTRY_IA32E_MODE |
2849#endif
2850 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002851 vmx->nested.nested_vmx_entry_ctls_high |=
2852 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002853 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002854 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002855
Jan Kiszka2996fca2014-06-16 13:59:43 +02002856 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002857 vmx->nested.nested_vmx_true_entry_ctls_low =
2858 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002859 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2860
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002861 /* cpu-based controls */
2862 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002863 vmx->nested.nested_vmx_procbased_ctls_low,
2864 vmx->nested.nested_vmx_procbased_ctls_high);
2865 vmx->nested.nested_vmx_procbased_ctls_low =
2866 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2867 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002868 CPU_BASED_VIRTUAL_INTR_PENDING |
2869 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002870 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2871 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2872 CPU_BASED_CR3_STORE_EXITING |
2873#ifdef CONFIG_X86_64
2874 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2875#endif
2876 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002877 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2878 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2879 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2880 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002881 /*
2882 * We can allow some features even when not supported by the
2883 * hardware. For example, L1 can specify an MSR bitmap - and we
2884 * can use it to avoid exits to L1 - even when L0 runs L2
2885 * without MSR bitmaps.
2886 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002887 vmx->nested.nested_vmx_procbased_ctls_high |=
2888 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002889 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002890
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002891 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002892 vmx->nested.nested_vmx_true_procbased_ctls_low =
2893 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002894 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2895
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002896 /* secondary cpu-based controls */
2897 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002898 vmx->nested.nested_vmx_secondary_ctls_low,
2899 vmx->nested.nested_vmx_secondary_ctls_high);
2900 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2901 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002902 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002903 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002904 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002905 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002906 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002907 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002908 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002909 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002910
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002911 if (enable_ept) {
2912 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002913 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002914 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002915 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002916 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2917 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002918 if (cpu_has_vmx_ept_execute_only())
2919 vmx->nested.nested_vmx_ept_caps |=
2920 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002921 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002922 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2923 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002924 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002925 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002926
Paolo Bonzinief697a72016-03-18 16:58:38 +01002927 /*
2928 * Old versions of KVM use the single-context version without
2929 * checking for support, so declare that it is supported even
2930 * though it is treated as global context. The alternative is
2931 * not failing the single-context invvpid, and it is worse.
2932 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002933 if (enable_vpid)
2934 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01002935 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002936 else
2937 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002938
Radim Krčmář0790ec12015-03-17 14:02:32 +01002939 if (enable_unrestricted_guest)
2940 vmx->nested.nested_vmx_secondary_ctls_high |=
2941 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2942
Jan Kiszkac18911a2013-03-13 16:06:41 +01002943 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002944 rdmsr(MSR_IA32_VMX_MISC,
2945 vmx->nested.nested_vmx_misc_low,
2946 vmx->nested.nested_vmx_misc_high);
2947 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2948 vmx->nested.nested_vmx_misc_low |=
2949 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002950 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002951 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002952}
2953
2954static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2955{
2956 /*
2957 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2958 */
2959 return ((control & high) | low) == control;
2960}
2961
2962static inline u64 vmx_control_msr(u32 low, u32 high)
2963{
2964 return low | ((u64)high << 32);
2965}
2966
Jan Kiszkacae50132014-01-04 18:47:22 +01002967/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002968static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2969{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002970 struct vcpu_vmx *vmx = to_vmx(vcpu);
2971
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002972 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002973 case MSR_IA32_VMX_BASIC:
2974 /*
2975 * This MSR reports some information about VMX support. We
2976 * should return information about the VMX we emulate for the
2977 * guest, and the VMCS structure we give it - not about the
2978 * VMX support of the underlying hardware.
2979 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002980 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002981 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2982 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002983 if (cpu_has_vmx_basic_inout())
2984 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002985 break;
2986 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2987 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002988 *pdata = vmx_control_msr(
2989 vmx->nested.nested_vmx_pinbased_ctls_low,
2990 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002991 break;
2992 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002993 *pdata = vmx_control_msr(
2994 vmx->nested.nested_vmx_true_procbased_ctls_low,
2995 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002996 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002997 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002998 *pdata = vmx_control_msr(
2999 vmx->nested.nested_vmx_procbased_ctls_low,
3000 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003001 break;
3002 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003003 *pdata = vmx_control_msr(
3004 vmx->nested.nested_vmx_true_exit_ctls_low,
3005 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003006 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003007 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003008 *pdata = vmx_control_msr(
3009 vmx->nested.nested_vmx_exit_ctls_low,
3010 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003011 break;
3012 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003013 *pdata = vmx_control_msr(
3014 vmx->nested.nested_vmx_true_entry_ctls_low,
3015 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003016 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003017 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003018 *pdata = vmx_control_msr(
3019 vmx->nested.nested_vmx_entry_ctls_low,
3020 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003021 break;
3022 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003023 *pdata = vmx_control_msr(
3024 vmx->nested.nested_vmx_misc_low,
3025 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003026 break;
3027 /*
3028 * These MSRs specify bits which the guest must keep fixed (on or off)
3029 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3030 * We picked the standard core2 setting.
3031 */
3032#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3033#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3034 case MSR_IA32_VMX_CR0_FIXED0:
3035 *pdata = VMXON_CR0_ALWAYSON;
3036 break;
3037 case MSR_IA32_VMX_CR0_FIXED1:
3038 *pdata = -1ULL;
3039 break;
3040 case MSR_IA32_VMX_CR4_FIXED0:
3041 *pdata = VMXON_CR4_ALWAYSON;
3042 break;
3043 case MSR_IA32_VMX_CR4_FIXED1:
3044 *pdata = -1ULL;
3045 break;
3046 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02003047 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003048 break;
3049 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003050 *pdata = vmx_control_msr(
3051 vmx->nested.nested_vmx_secondary_ctls_low,
3052 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003053 break;
3054 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003055 *pdata = vmx->nested.nested_vmx_ept_caps |
3056 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003057 break;
3058 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003059 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003060 }
3061
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003062 return 0;
3063}
3064
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003065static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3066 uint64_t val)
3067{
3068 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3069
3070 return !(val & ~valid_bits);
3071}
3072
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003073/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074 * Reads an msr value (of 'msr_index') into 'pdata'.
3075 * Returns 0 on success, non-0 otherwise.
3076 * Assumes vcpu_load() was already called.
3077 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003078static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079{
Avi Kivity26bb0982009-09-07 11:14:12 +03003080 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003082 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003083#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003085 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 break;
3087 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003088 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003090 case MSR_KERNEL_GS_BASE:
3091 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003092 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003093 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003094#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003096 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303097 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003098 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003100 case MSR_IA32_SPEC_CTRL:
3101 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003102 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003103 return 1;
3104
3105 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3106 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003107 case MSR_IA32_ARCH_CAPABILITIES:
3108 if (!msr_info->host_initiated &&
3109 !guest_cpuid_has_arch_capabilities(vcpu))
3110 return 1;
3111 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3112 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003114 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 break;
3116 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003117 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118 break;
3119 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003120 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003122 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003123 if (!kvm_mpx_supported() ||
3124 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003125 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003126 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003127 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003128 case MSR_IA32_MCG_EXT_CTL:
3129 if (!msr_info->host_initiated &&
3130 !(to_vmx(vcpu)->msr_ia32_feature_control &
3131 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003132 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003133 msr_info->data = vcpu->arch.mcg_ext_ctl;
3134 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003135 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003136 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003137 break;
3138 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3139 if (!nested_vmx_allowed(vcpu))
3140 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003141 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003142 case MSR_IA32_XSS:
3143 if (!vmx_xsaves_supported())
3144 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003145 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003146 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003147 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003148 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003149 return 1;
3150 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003152 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003153 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003154 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003155 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003157 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 }
3159
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 return 0;
3161}
3162
Jan Kiszkacae50132014-01-04 18:47:22 +01003163static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3164
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165/*
3166 * Writes msr value into into the appropriate "register".
3167 * Returns 0 on success, non-0 otherwise.
3168 * Assumes vcpu_load() was already called.
3169 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003170static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003172 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003173 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003174 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003175 u32 msr_index = msr_info->index;
3176 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003177
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003179 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003180 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003181 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003182#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003184 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185 vmcs_writel(GUEST_FS_BASE, data);
3186 break;
3187 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003188 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 vmcs_writel(GUEST_GS_BASE, data);
3190 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003191 case MSR_KERNEL_GS_BASE:
3192 vmx_load_host_state(vmx);
3193 vmx->msr_guest_kernel_gs_base = data;
3194 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195#endif
3196 case MSR_IA32_SYSENTER_CS:
3197 vmcs_write32(GUEST_SYSENTER_CS, data);
3198 break;
3199 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003200 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 break;
3202 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003203 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003205 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003206 if (!kvm_mpx_supported() ||
3207 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003208 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003209 if (is_noncanonical_address(data & PAGE_MASK) ||
3210 (data & MSR_IA32_BNDCFGS_RSVD))
3211 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003212 vmcs_write64(GUEST_BNDCFGS, data);
3213 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303214 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003215 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003217 case MSR_IA32_SPEC_CTRL:
3218 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003219 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003220 return 1;
3221
3222 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003223 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003224 return 1;
3225
3226 vmx->spec_ctrl = data;
3227
3228 if (!data)
3229 break;
3230
3231 /*
3232 * For non-nested:
3233 * When it's written (to non-zero) for the first time, pass
3234 * it through.
3235 *
3236 * For nested:
3237 * The handling of the MSR bitmap for L2 guests is done in
3238 * nested_vmx_merge_msr_bitmap. We should not touch the
3239 * vmcs02.msr_bitmap here since it gets completely overwritten
3240 * in the merging. We update the vmcs01 here for L1 as well
3241 * since it will end up touching the MSR anyway now.
3242 */
3243 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3244 MSR_IA32_SPEC_CTRL,
3245 MSR_TYPE_RW);
3246 break;
Ashok Raj70131292018-02-01 22:59:43 +01003247 case MSR_IA32_PRED_CMD:
3248 if (!msr_info->host_initiated &&
3249 !guest_cpuid_has_ibpb(vcpu))
3250 return 1;
3251
3252 if (data & ~PRED_CMD_IBPB)
3253 return 1;
3254
3255 if (!data)
3256 break;
3257
3258 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3259
3260 /*
3261 * For non-nested:
3262 * When it's written (to non-zero) for the first time, pass
3263 * it through.
3264 *
3265 * For nested:
3266 * The handling of the MSR bitmap for L2 guests is done in
3267 * nested_vmx_merge_msr_bitmap. We should not touch the
3268 * vmcs02.msr_bitmap here since it gets completely overwritten
3269 * in the merging.
3270 */
3271 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3272 MSR_TYPE_W);
3273 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003274 case MSR_IA32_ARCH_CAPABILITIES:
3275 if (!msr_info->host_initiated)
3276 return 1;
3277 vmx->arch_capabilities = data;
3278 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003279 case MSR_IA32_CR_PAT:
3280 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003281 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3282 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003283 vmcs_write64(GUEST_IA32_PAT, data);
3284 vcpu->arch.pat = data;
3285 break;
3286 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003287 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003288 break;
Will Auldba904632012-11-29 12:42:50 -08003289 case MSR_IA32_TSC_ADJUST:
3290 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003291 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003292 case MSR_IA32_MCG_EXT_CTL:
3293 if ((!msr_info->host_initiated &&
3294 !(to_vmx(vcpu)->msr_ia32_feature_control &
3295 FEATURE_CONTROL_LMCE)) ||
3296 (data & ~MCG_EXT_CTL_LMCE_EN))
3297 return 1;
3298 vcpu->arch.mcg_ext_ctl = data;
3299 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003300 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003301 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003302 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003303 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3304 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003305 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003306 if (msr_info->host_initiated && data == 0)
3307 vmx_leave_nested(vcpu);
3308 break;
3309 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3310 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003311 case MSR_IA32_XSS:
3312 if (!vmx_xsaves_supported())
3313 return 1;
3314 /*
3315 * The only supported bit as of Skylake is bit 8, but
3316 * it is not supported on KVM.
3317 */
3318 if (data != 0)
3319 return 1;
3320 vcpu->arch.ia32_xss = data;
3321 if (vcpu->arch.ia32_xss != host_xss)
3322 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04003323 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08003324 else
3325 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3326 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003327 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003328 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003329 return 1;
3330 /* Check reserved bit, higher 32 bits should be zero */
3331 if ((data >> 32) != 0)
3332 return 1;
3333 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003335 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003336 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003337 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003338 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003339 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3340 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003341 ret = kvm_set_shared_msr(msr->index, msr->data,
3342 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003343 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003344 if (ret)
3345 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003346 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003347 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003349 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350 }
3351
Eddie Dong2cc51562007-05-21 07:28:09 +03003352 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353}
3354
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003355static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003357 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3358 switch (reg) {
3359 case VCPU_REGS_RSP:
3360 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3361 break;
3362 case VCPU_REGS_RIP:
3363 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3364 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003365 case VCPU_EXREG_PDPTR:
3366 if (enable_ept)
3367 ept_save_pdptrs(vcpu);
3368 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003369 default:
3370 break;
3371 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372}
3373
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374static __init int cpu_has_kvm_support(void)
3375{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003376 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377}
3378
3379static __init int vmx_disabled_by_bios(void)
3380{
3381 u64 msr;
3382
3383 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003384 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003385 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003386 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3387 && tboot_enabled())
3388 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003389 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003390 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003391 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003392 && !tboot_enabled()) {
3393 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003394 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003395 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003396 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003397 /* launched w/o TXT and VMX disabled */
3398 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3399 && !tboot_enabled())
3400 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003401 }
3402
3403 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003404}
3405
Dongxiao Xu7725b892010-05-11 18:29:38 +08003406static void kvm_cpu_vmxon(u64 addr)
3407{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003408 intel_pt_handle_vmx(1);
3409
Dongxiao Xu7725b892010-05-11 18:29:38 +08003410 asm volatile (ASM_VMX_VMXON_RAX
3411 : : "a"(&addr), "m"(addr)
3412 : "memory", "cc");
3413}
3414
Radim Krčmář13a34e02014-08-28 15:13:03 +02003415static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416{
3417 int cpu = raw_smp_processor_id();
3418 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003419 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003421 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003422 return -EBUSY;
3423
Nadav Har'Eld462b812011-05-24 15:26:10 +03003424 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003425 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3426 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003427
3428 /*
3429 * Now we can enable the vmclear operation in kdump
3430 * since the loaded_vmcss_on_cpu list on this cpu
3431 * has been initialized.
3432 *
3433 * Though the cpu is not in VMX operation now, there
3434 * is no problem to enable the vmclear operation
3435 * for the loaded_vmcss_on_cpu list is empty!
3436 */
3437 crash_enable_local_vmclear(cpu);
3438
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003440
3441 test_bits = FEATURE_CONTROL_LOCKED;
3442 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3443 if (tboot_enabled())
3444 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3445
3446 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003448 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3449 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003450 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003451
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003452 if (vmm_exclusive) {
3453 kvm_cpu_vmxon(phys_addr);
3454 ept_sync_global();
3455 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003456
Christoph Lameter89cbc762014-08-17 12:30:40 -05003457 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003458
Alexander Graf10474ae2009-09-15 11:37:46 +02003459 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460}
3461
Nadav Har'Eld462b812011-05-24 15:26:10 +03003462static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003463{
3464 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003465 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003466
Nadav Har'Eld462b812011-05-24 15:26:10 +03003467 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3468 loaded_vmcss_on_cpu_link)
3469 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003470}
3471
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003472
3473/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3474 * tricks.
3475 */
3476static void kvm_cpu_vmxoff(void)
3477{
3478 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003479
3480 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003481}
3482
Radim Krčmář13a34e02014-08-28 15:13:03 +02003483static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003485 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003486 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003487 kvm_cpu_vmxoff();
3488 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003489 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490}
3491
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003492static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003493 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494{
3495 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003496 u32 ctl = ctl_min | ctl_opt;
3497
3498 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3499
3500 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3501 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3502
3503 /* Ensure minimum (required) set of control bits are supported. */
3504 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003505 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003506
3507 *result = ctl;
3508 return 0;
3509}
3510
Avi Kivity110312c2010-12-21 12:54:20 +02003511static __init bool allow_1_setting(u32 msr, u32 ctl)
3512{
3513 u32 vmx_msr_low, vmx_msr_high;
3514
3515 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3516 return vmx_msr_high & ctl;
3517}
3518
Yang, Sheng002c7f72007-07-31 14:23:01 +03003519static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003520{
3521 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003522 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003523 u32 _pin_based_exec_control = 0;
3524 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003525 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003526 u32 _vmexit_control = 0;
3527 u32 _vmentry_control = 0;
3528
Raghavendra K T10166742012-02-07 23:19:20 +05303529 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003530#ifdef CONFIG_X86_64
3531 CPU_BASED_CR8_LOAD_EXITING |
3532 CPU_BASED_CR8_STORE_EXITING |
3533#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003534 CPU_BASED_CR3_LOAD_EXITING |
3535 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003536 CPU_BASED_USE_IO_BITMAPS |
3537 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003538 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003539 CPU_BASED_MWAIT_EXITING |
3540 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003541 CPU_BASED_INVLPG_EXITING |
3542 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003543
Sheng Yangf78e0e22007-10-29 09:40:42 +08003544 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003545 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003546 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003547 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3548 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003549 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003550#ifdef CONFIG_X86_64
3551 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3552 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3553 ~CPU_BASED_CR8_STORE_EXITING;
3554#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003555 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003556 min2 = 0;
3557 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003558 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003559 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003560 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003561 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003562 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003563 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003564 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003565 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003566 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003567 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003568 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003569 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003570 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003571 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003572 if (adjust_vmx_controls(min2, opt2,
3573 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003574 &_cpu_based_2nd_exec_control) < 0)
3575 return -EIO;
3576 }
3577#ifndef CONFIG_X86_64
3578 if (!(_cpu_based_2nd_exec_control &
3579 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3580 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3581#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003582
3583 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3584 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003585 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003586 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3587 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003588
Sheng Yangd56f5462008-04-25 10:13:16 +08003589 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003590 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3591 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003592 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3593 CPU_BASED_CR3_STORE_EXITING |
3594 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003595 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3596 vmx_capability.ept, vmx_capability.vpid);
3597 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003598
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003599 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003600#ifdef CONFIG_X86_64
3601 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3602#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003603 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003604 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003605 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3606 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003607 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003608
Yang Zhang01e439b2013-04-11 19:25:12 +08003609 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003610 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3611 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003612 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3613 &_pin_based_exec_control) < 0)
3614 return -EIO;
3615
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003616 if (cpu_has_broken_vmx_preemption_timer())
3617 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003618 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003619 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003620 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3621
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003622 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003623 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003624 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3625 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003626 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003627
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003628 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003629
3630 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3631 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003632 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003633
3634#ifdef CONFIG_X86_64
3635 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3636 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003637 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003638#endif
3639
3640 /* Require Write-Back (WB) memory type for VMCS accesses. */
3641 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003642 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003643
Yang, Sheng002c7f72007-07-31 14:23:01 +03003644 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003645 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003646 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003647 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003648
Yang, Sheng002c7f72007-07-31 14:23:01 +03003649 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3650 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003651 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003652 vmcs_conf->vmexit_ctrl = _vmexit_control;
3653 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003654
Avi Kivity110312c2010-12-21 12:54:20 +02003655 cpu_has_load_ia32_efer =
3656 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3657 VM_ENTRY_LOAD_IA32_EFER)
3658 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3659 VM_EXIT_LOAD_IA32_EFER);
3660
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003661 cpu_has_load_perf_global_ctrl =
3662 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3663 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3664 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3665 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3666
3667 /*
3668 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003669 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003670 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3671 *
3672 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3673 *
3674 * AAK155 (model 26)
3675 * AAP115 (model 30)
3676 * AAT100 (model 37)
3677 * BC86,AAY89,BD102 (model 44)
3678 * BA97 (model 46)
3679 *
3680 */
3681 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3682 switch (boot_cpu_data.x86_model) {
3683 case 26:
3684 case 30:
3685 case 37:
3686 case 44:
3687 case 46:
3688 cpu_has_load_perf_global_ctrl = false;
3689 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3690 "does not work properly. Using workaround\n");
3691 break;
3692 default:
3693 break;
3694 }
3695 }
3696
Borislav Petkov782511b2016-04-04 22:25:03 +02003697 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003698 rdmsrl(MSR_IA32_XSS, host_xss);
3699
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003700 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003701}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003702
3703static struct vmcs *alloc_vmcs_cpu(int cpu)
3704{
3705 int node = cpu_to_node(cpu);
3706 struct page *pages;
3707 struct vmcs *vmcs;
3708
Vlastimil Babka96db8002015-09-08 15:03:50 -07003709 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710 if (!pages)
3711 return NULL;
3712 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003713 memset(vmcs, 0, vmcs_config.size);
3714 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003715 return vmcs;
3716}
3717
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718static void free_vmcs(struct vmcs *vmcs)
3719{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003720 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003721}
3722
Nadav Har'Eld462b812011-05-24 15:26:10 +03003723/*
3724 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3725 */
3726static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3727{
3728 if (!loaded_vmcs->vmcs)
3729 return;
3730 loaded_vmcs_clear(loaded_vmcs);
3731 free_vmcs(loaded_vmcs->vmcs);
3732 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003733 if (loaded_vmcs->msr_bitmap)
3734 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003735 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003736}
3737
Paolo Bonziniff546f92018-01-11 12:16:15 +01003738static struct vmcs *alloc_vmcs(void)
3739{
3740 return alloc_vmcs_cpu(raw_smp_processor_id());
3741}
3742
3743static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3744{
3745 loaded_vmcs->vmcs = alloc_vmcs();
3746 if (!loaded_vmcs->vmcs)
3747 return -ENOMEM;
3748
3749 loaded_vmcs->shadow_vmcs = NULL;
3750 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003751
3752 if (cpu_has_vmx_msr_bitmap()) {
3753 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3754 if (!loaded_vmcs->msr_bitmap)
3755 goto out_vmcs;
3756 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3757 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003758 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003759
3760out_vmcs:
3761 free_loaded_vmcs(loaded_vmcs);
3762 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003763}
3764
Sam Ravnborg39959582007-06-01 00:47:13 -07003765static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003766{
3767 int cpu;
3768
Zachary Amsden3230bb42009-09-29 11:38:37 -10003769 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003770 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003771 per_cpu(vmxarea, cpu) = NULL;
3772 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773}
3774
Bandan Dasfe2b2012014-04-21 15:20:14 -04003775static void init_vmcs_shadow_fields(void)
3776{
3777 int i, j;
3778
3779 /* No checks for read only fields yet */
3780
3781 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3782 switch (shadow_read_write_fields[i]) {
3783 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003784 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003785 continue;
3786 break;
3787 default:
3788 break;
3789 }
3790
3791 if (j < i)
3792 shadow_read_write_fields[j] =
3793 shadow_read_write_fields[i];
3794 j++;
3795 }
3796 max_shadow_read_write_fields = j;
3797
3798 /* shadowed fields guest access without vmexit */
3799 for (i = 0; i < max_shadow_read_write_fields; i++) {
3800 clear_bit(shadow_read_write_fields[i],
3801 vmx_vmwrite_bitmap);
3802 clear_bit(shadow_read_write_fields[i],
3803 vmx_vmread_bitmap);
3804 }
3805 for (i = 0; i < max_shadow_read_only_fields; i++)
3806 clear_bit(shadow_read_only_fields[i],
3807 vmx_vmread_bitmap);
3808}
3809
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810static __init int alloc_kvm_area(void)
3811{
3812 int cpu;
3813
Zachary Amsden3230bb42009-09-29 11:38:37 -10003814 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815 struct vmcs *vmcs;
3816
3817 vmcs = alloc_vmcs_cpu(cpu);
3818 if (!vmcs) {
3819 free_kvm_area();
3820 return -ENOMEM;
3821 }
3822
3823 per_cpu(vmxarea, cpu) = vmcs;
3824 }
3825 return 0;
3826}
3827
Gleb Natapov14168782013-01-21 15:36:49 +02003828static bool emulation_required(struct kvm_vcpu *vcpu)
3829{
3830 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3831}
3832
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003833static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003834 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003836 if (!emulate_invalid_guest_state) {
3837 /*
3838 * CS and SS RPL should be equal during guest entry according
3839 * to VMX spec, but in reality it is not always so. Since vcpu
3840 * is in the middle of the transition from real mode to
3841 * protected mode it is safe to assume that RPL 0 is a good
3842 * default value.
3843 */
3844 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003845 save->selector &= ~SEGMENT_RPL_MASK;
3846 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003847 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003848 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003849 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850}
3851
3852static void enter_pmode(struct kvm_vcpu *vcpu)
3853{
3854 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003855 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003856
Gleb Natapovd99e4152012-12-20 16:57:45 +02003857 /*
3858 * Update real mode segment cache. It may be not up-to-date if sement
3859 * register was written while vcpu was in a guest mode.
3860 */
3861 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3862 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3863 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3864 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3865 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3866 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3867
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003868 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869
Avi Kivity2fb92db2011-04-27 19:42:18 +03003870 vmx_segment_cache_clear(vmx);
3871
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003872 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873
3874 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003875 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3876 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 vmcs_writel(GUEST_RFLAGS, flags);
3878
Rusty Russell66aee912007-07-17 23:34:16 +10003879 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3880 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881
3882 update_exception_bitmap(vcpu);
3883
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003884 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3885 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3886 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3887 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3888 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3889 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003890}
3891
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003892static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893{
Mathias Krause772e0312012-08-30 01:30:19 +02003894 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003895 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003896
Gleb Natapovd99e4152012-12-20 16:57:45 +02003897 var.dpl = 0x3;
3898 if (seg == VCPU_SREG_CS)
3899 var.type = 0x3;
3900
3901 if (!emulate_invalid_guest_state) {
3902 var.selector = var.base >> 4;
3903 var.base = var.base & 0xffff0;
3904 var.limit = 0xffff;
3905 var.g = 0;
3906 var.db = 0;
3907 var.present = 1;
3908 var.s = 1;
3909 var.l = 0;
3910 var.unusable = 0;
3911 var.type = 0x3;
3912 var.avl = 0;
3913 if (save->base & 0xf)
3914 printk_once(KERN_WARNING "kvm: segment base is not "
3915 "paragraph aligned when entering "
3916 "protected mode (seg=%d)", seg);
3917 }
3918
3919 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05003920 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003921 vmcs_write32(sf->limit, var.limit);
3922 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923}
3924
3925static void enter_rmode(struct kvm_vcpu *vcpu)
3926{
3927 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003928 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003930 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3931 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3932 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3933 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3934 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003935 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3936 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003937
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003938 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939
Gleb Natapov776e58e2011-03-13 12:34:27 +02003940 /*
3941 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003942 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003943 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003944 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003945 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3946 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003947
Avi Kivity2fb92db2011-04-27 19:42:18 +03003948 vmx_segment_cache_clear(vmx);
3949
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003950 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3953
3954 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003955 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003956
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003957 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958
3959 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003960 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003961 update_exception_bitmap(vcpu);
3962
Gleb Natapovd99e4152012-12-20 16:57:45 +02003963 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3964 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3965 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3966 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3967 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3968 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003969
Eddie Dong8668a3c2007-10-10 14:26:45 +08003970 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003971}
3972
Amit Shah401d10d2009-02-20 22:53:37 +05303973static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3974{
3975 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003976 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3977
3978 if (!msr)
3979 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303980
Avi Kivity44ea2b12009-09-06 15:55:37 +03003981 /*
3982 * Force kernel_gs_base reloading before EFER changes, as control
3983 * of this msr depends on is_long_mode().
3984 */
3985 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003986 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303987 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003988 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303989 msr->data = efer;
3990 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003991 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303992
3993 msr->data = efer & ~EFER_LME;
3994 }
3995 setup_msrs(vmx);
3996}
3997
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003998#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999
4000static void enter_lmode(struct kvm_vcpu *vcpu)
4001{
4002 u32 guest_tr_ar;
4003
Avi Kivity2fb92db2011-04-27 19:42:18 +03004004 vmx_segment_cache_clear(to_vmx(vcpu));
4005
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004007 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004008 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4009 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004010 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004011 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4012 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 }
Avi Kivityda38f432010-07-06 11:30:49 +03004014 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004015}
4016
4017static void exit_lmode(struct kvm_vcpu *vcpu)
4018{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004019 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004020 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004021}
4022
4023#endif
4024
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004025static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004026{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004027 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004028 if (enable_ept) {
4029 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4030 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004031 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004032 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004033}
4034
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004035static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4036{
4037 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4038}
4039
Jim Mattson8386ff52017-03-16 13:53:59 -07004040static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4041{
4042 if (enable_ept)
4043 vmx_flush_tlb(vcpu);
4044}
4045
Avi Kivitye8467fd2009-12-29 18:43:06 +02004046static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4047{
4048 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4049
4050 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4051 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4052}
4053
Avi Kivityaff48ba2010-12-05 18:56:11 +02004054static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4055{
4056 if (enable_ept && is_paging(vcpu))
4057 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4058 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4059}
4060
Anthony Liguori25c4c272007-04-27 09:29:21 +03004061static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004062{
Avi Kivityfc78f512009-12-07 12:16:48 +02004063 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4064
4065 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4066 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004067}
4068
Sheng Yang14394422008-04-28 12:24:45 +08004069static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4070{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004071 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4072
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004073 if (!test_bit(VCPU_EXREG_PDPTR,
4074 (unsigned long *)&vcpu->arch.regs_dirty))
4075 return;
4076
Sheng Yang14394422008-04-28 12:24:45 +08004077 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004078 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4079 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4080 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4081 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004082 }
4083}
4084
Avi Kivity8f5d5492009-05-31 18:41:29 +03004085static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4086{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004087 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4088
Avi Kivity8f5d5492009-05-31 18:41:29 +03004089 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004090 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4091 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4092 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4093 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004094 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004095
4096 __set_bit(VCPU_EXREG_PDPTR,
4097 (unsigned long *)&vcpu->arch.regs_avail);
4098 __set_bit(VCPU_EXREG_PDPTR,
4099 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004100}
4101
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004102static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004103
4104static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4105 unsigned long cr0,
4106 struct kvm_vcpu *vcpu)
4107{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004108 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4109 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004110 if (!(cr0 & X86_CR0_PG)) {
4111 /* From paging/starting to nonpaging */
4112 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004113 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004114 (CPU_BASED_CR3_LOAD_EXITING |
4115 CPU_BASED_CR3_STORE_EXITING));
4116 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004117 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004118 } else if (!is_paging(vcpu)) {
4119 /* From nonpaging to paging */
4120 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004121 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004122 ~(CPU_BASED_CR3_LOAD_EXITING |
4123 CPU_BASED_CR3_STORE_EXITING));
4124 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004125 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004126 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004127
4128 if (!(cr0 & X86_CR0_WP))
4129 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004130}
4131
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4133{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004134 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004135 unsigned long hw_cr0;
4136
Gleb Natapov50378782013-02-04 16:00:28 +02004137 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004138 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004139 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004140 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004141 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004142
Gleb Natapov218e7632013-01-21 15:36:45 +02004143 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4144 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145
Gleb Natapov218e7632013-01-21 15:36:45 +02004146 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4147 enter_rmode(vcpu);
4148 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004150#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004151 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10004152 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10004154 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 exit_lmode(vcpu);
4156 }
4157#endif
4158
Avi Kivity089d0342009-03-23 18:26:32 +02004159 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004160 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4161
Avi Kivity02daab22009-12-30 12:40:26 +02004162 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004163 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004164
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004166 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004167 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004168
4169 /* depends on vcpu->arch.cr0 to be set to a new value */
4170 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171}
4172
Sheng Yang14394422008-04-28 12:24:45 +08004173static u64 construct_eptp(unsigned long root_hpa)
4174{
4175 u64 eptp;
4176
4177 /* TODO write the value reading from MSR */
4178 eptp = VMX_EPT_DEFAULT_MT |
4179 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004180 if (enable_ept_ad_bits)
4181 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004182 eptp |= (root_hpa & PAGE_MASK);
4183
4184 return eptp;
4185}
4186
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4188{
Sheng Yang14394422008-04-28 12:24:45 +08004189 unsigned long guest_cr3;
4190 u64 eptp;
4191
4192 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004193 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004194 eptp = construct_eptp(cr3);
4195 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004196 if (is_paging(vcpu) || is_guest_mode(vcpu))
4197 guest_cr3 = kvm_read_cr3(vcpu);
4198 else
4199 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004200 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004201 }
4202
Sheng Yang2384d2b2008-01-17 15:14:33 +08004203 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004204 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004205}
4206
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004207static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004208{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004209 /*
4210 * Pass through host's Machine Check Enable value to hw_cr4, which
4211 * is in force while we are in guest mode. Do not let guests control
4212 * this bit, even if host CR4.MCE == 0.
4213 */
4214 unsigned long hw_cr4 =
4215 (cr4_read_shadow() & X86_CR4_MCE) |
4216 (cr4 & ~X86_CR4_MCE) |
4217 (to_vmx(vcpu)->rmode.vm86_active ?
4218 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004219
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004220 if (cr4 & X86_CR4_VMXE) {
4221 /*
4222 * To use VMXON (and later other VMX instructions), a guest
4223 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4224 * So basically the check on whether to allow nested VMX
4225 * is here.
4226 */
4227 if (!nested_vmx_allowed(vcpu))
4228 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004229 }
4230 if (to_vmx(vcpu)->nested.vmxon &&
4231 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004232 return 1;
4233
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004234 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004235 if (enable_ept) {
4236 if (!is_paging(vcpu)) {
4237 hw_cr4 &= ~X86_CR4_PAE;
4238 hw_cr4 |= X86_CR4_PSE;
4239 } else if (!(cr4 & X86_CR4_PAE)) {
4240 hw_cr4 &= ~X86_CR4_PAE;
4241 }
4242 }
Sheng Yang14394422008-04-28 12:24:45 +08004243
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004244 if (!enable_unrestricted_guest && !is_paging(vcpu))
4245 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004246 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4247 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4248 * to be manually disabled when guest switches to non-paging
4249 * mode.
4250 *
4251 * If !enable_unrestricted_guest, the CPU is always running
4252 * with CR0.PG=1 and CR4 needs to be modified.
4253 * If enable_unrestricted_guest, the CPU automatically
4254 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004255 */
Huaitong Handdba2622016-03-22 16:51:15 +08004256 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004257
Sheng Yang14394422008-04-28 12:24:45 +08004258 vmcs_writel(CR4_READ_SHADOW, cr4);
4259 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004260 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261}
4262
Avi Kivity6aa8b732006-12-10 02:21:36 -08004263static void vmx_get_segment(struct kvm_vcpu *vcpu,
4264 struct kvm_segment *var, int seg)
4265{
Avi Kivitya9179492011-01-03 14:28:52 +02004266 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004267 u32 ar;
4268
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004269 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004270 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004271 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004272 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004273 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004274 var->base = vmx_read_guest_seg_base(vmx, seg);
4275 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4276 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004277 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004278 var->base = vmx_read_guest_seg_base(vmx, seg);
4279 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4280 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4281 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004282 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004283 var->type = ar & 15;
4284 var->s = (ar >> 4) & 1;
4285 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004286 /*
4287 * Some userspaces do not preserve unusable property. Since usable
4288 * segment has to be present according to VMX spec we can use present
4289 * property to amend userspace bug by making unusable segment always
4290 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4291 * segment as unusable.
4292 */
4293 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004294 var->avl = (ar >> 12) & 1;
4295 var->l = (ar >> 13) & 1;
4296 var->db = (ar >> 14) & 1;
4297 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004298}
4299
Avi Kivitya9179492011-01-03 14:28:52 +02004300static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4301{
Avi Kivitya9179492011-01-03 14:28:52 +02004302 struct kvm_segment s;
4303
4304 if (to_vmx(vcpu)->rmode.vm86_active) {
4305 vmx_get_segment(vcpu, &s, seg);
4306 return s.base;
4307 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004308 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004309}
4310
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004311static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004312{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004313 struct vcpu_vmx *vmx = to_vmx(vcpu);
4314
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004315 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004316 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004317 else {
4318 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004319 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004320 }
Avi Kivity69c73022011-03-07 15:26:44 +02004321}
4322
Avi Kivity653e3102007-05-07 10:55:37 +03004323static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325 u32 ar;
4326
Avi Kivityf0495f92012-06-07 17:06:10 +03004327 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004328 ar = 1 << 16;
4329 else {
4330 ar = var->type & 15;
4331 ar |= (var->s & 1) << 4;
4332 ar |= (var->dpl & 3) << 5;
4333 ar |= (var->present & 1) << 7;
4334 ar |= (var->avl & 1) << 12;
4335 ar |= (var->l & 1) << 13;
4336 ar |= (var->db & 1) << 14;
4337 ar |= (var->g & 1) << 15;
4338 }
Avi Kivity653e3102007-05-07 10:55:37 +03004339
4340 return ar;
4341}
4342
4343static void vmx_set_segment(struct kvm_vcpu *vcpu,
4344 struct kvm_segment *var, int seg)
4345{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004346 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004347 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004348
Avi Kivity2fb92db2011-04-27 19:42:18 +03004349 vmx_segment_cache_clear(vmx);
4350
Gleb Natapov1ecd50a92012-12-12 19:10:54 +02004351 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4352 vmx->rmode.segs[seg] = *var;
4353 if (seg == VCPU_SREG_TR)
4354 vmcs_write16(sf->selector, var->selector);
4355 else if (var->s)
4356 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004357 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004358 }
Gleb Natapov1ecd50a92012-12-12 19:10:54 +02004359
Avi Kivity653e3102007-05-07 10:55:37 +03004360 vmcs_writel(sf->base, var->base);
4361 vmcs_write32(sf->limit, var->limit);
4362 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004363
4364 /*
4365 * Fix the "Accessed" bit in AR field of segment registers for older
4366 * qemu binaries.
4367 * IA32 arch specifies that at the time of processor reset the
4368 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004369 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004370 * state vmexit when "unrestricted guest" mode is turned on.
4371 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4372 * tree. Newer qemu binaries with that qemu fix would not need this
4373 * kvm hack.
4374 */
4375 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004376 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004377
Gleb Natapovf924d662012-12-12 19:10:55 +02004378 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004379
4380out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004381 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004382}
4383
Avi Kivity6aa8b732006-12-10 02:21:36 -08004384static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4385{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004386 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387
4388 *db = (ar >> 14) & 1;
4389 *l = (ar >> 13) & 1;
4390}
4391
Gleb Natapov89a27f42010-02-16 10:51:48 +02004392static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004393{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004394 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4395 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396}
4397
Gleb Natapov89a27f42010-02-16 10:51:48 +02004398static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004400 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4401 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004402}
4403
Gleb Natapov89a27f42010-02-16 10:51:48 +02004404static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004406 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4407 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004408}
4409
Gleb Natapov89a27f42010-02-16 10:51:48 +02004410static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004411{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004412 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4413 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414}
4415
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004416static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4417{
4418 struct kvm_segment var;
4419 u32 ar;
4420
4421 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004422 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004423 if (seg == VCPU_SREG_CS)
4424 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004425 ar = vmx_segment_access_rights(&var);
4426
4427 if (var.base != (var.selector << 4))
4428 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004429 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004430 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004431 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004432 return false;
4433
4434 return true;
4435}
4436
4437static bool code_segment_valid(struct kvm_vcpu *vcpu)
4438{
4439 struct kvm_segment cs;
4440 unsigned int cs_rpl;
4441
4442 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004443 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004444
Avi Kivity1872a3f2009-01-04 23:26:52 +02004445 if (cs.unusable)
4446 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004447 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004448 return false;
4449 if (!cs.s)
4450 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004451 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004452 if (cs.dpl > cs_rpl)
4453 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004454 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004455 if (cs.dpl != cs_rpl)
4456 return false;
4457 }
4458 if (!cs.present)
4459 return false;
4460
4461 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4462 return true;
4463}
4464
4465static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4466{
4467 struct kvm_segment ss;
4468 unsigned int ss_rpl;
4469
4470 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004471 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004472
Avi Kivity1872a3f2009-01-04 23:26:52 +02004473 if (ss.unusable)
4474 return true;
4475 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004476 return false;
4477 if (!ss.s)
4478 return false;
4479 if (ss.dpl != ss_rpl) /* DPL != RPL */
4480 return false;
4481 if (!ss.present)
4482 return false;
4483
4484 return true;
4485}
4486
4487static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4488{
4489 struct kvm_segment var;
4490 unsigned int rpl;
4491
4492 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004493 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004494
Avi Kivity1872a3f2009-01-04 23:26:52 +02004495 if (var.unusable)
4496 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004497 if (!var.s)
4498 return false;
4499 if (!var.present)
4500 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004501 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004502 if (var.dpl < rpl) /* DPL < RPL */
4503 return false;
4504 }
4505
4506 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4507 * rights flags
4508 */
4509 return true;
4510}
4511
4512static bool tr_valid(struct kvm_vcpu *vcpu)
4513{
4514 struct kvm_segment tr;
4515
4516 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4517
Avi Kivity1872a3f2009-01-04 23:26:52 +02004518 if (tr.unusable)
4519 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004520 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004521 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004522 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004523 return false;
4524 if (!tr.present)
4525 return false;
4526
4527 return true;
4528}
4529
4530static bool ldtr_valid(struct kvm_vcpu *vcpu)
4531{
4532 struct kvm_segment ldtr;
4533
4534 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4535
Avi Kivity1872a3f2009-01-04 23:26:52 +02004536 if (ldtr.unusable)
4537 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004538 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004539 return false;
4540 if (ldtr.type != 2)
4541 return false;
4542 if (!ldtr.present)
4543 return false;
4544
4545 return true;
4546}
4547
4548static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4549{
4550 struct kvm_segment cs, ss;
4551
4552 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4553 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4554
Nadav Amitb32a9912015-03-29 16:33:04 +03004555 return ((cs.selector & SEGMENT_RPL_MASK) ==
4556 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004557}
4558
4559/*
4560 * Check if guest state is valid. Returns true if valid, false if
4561 * not.
4562 * We assume that registers are always usable
4563 */
4564static bool guest_state_valid(struct kvm_vcpu *vcpu)
4565{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004566 if (enable_unrestricted_guest)
4567 return true;
4568
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004569 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004570 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004571 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4572 return false;
4573 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4574 return false;
4575 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4576 return false;
4577 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4578 return false;
4579 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4580 return false;
4581 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4582 return false;
4583 } else {
4584 /* protected mode guest state checks */
4585 if (!cs_ss_rpl_check(vcpu))
4586 return false;
4587 if (!code_segment_valid(vcpu))
4588 return false;
4589 if (!stack_segment_valid(vcpu))
4590 return false;
4591 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4592 return false;
4593 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4594 return false;
4595 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4596 return false;
4597 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4598 return false;
4599 if (!tr_valid(vcpu))
4600 return false;
4601 if (!ldtr_valid(vcpu))
4602 return false;
4603 }
4604 /* TODO:
4605 * - Add checks on RIP
4606 * - Add checks on RFLAGS
4607 */
4608
4609 return true;
4610}
4611
Mike Dayd77c26f2007-10-08 09:02:08 -04004612static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004613{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004614 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004615 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004616 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004617
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004618 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004619 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004620 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4621 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004622 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004623 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004624 r = kvm_write_guest_page(kvm, fn++, &data,
4625 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004626 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004627 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004628 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4629 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004630 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004631 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4632 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004633 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004634 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004635 r = kvm_write_guest_page(kvm, fn, &data,
4636 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4637 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004638out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004639 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004640 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004641}
4642
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004643static int init_rmode_identity_map(struct kvm *kvm)
4644{
Tang Chenf51770e2014-09-16 18:41:59 +08004645 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004646 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004647 u32 tmp;
4648
Avi Kivity089d0342009-03-23 18:26:32 +02004649 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004650 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004651
4652 /* Protect kvm->arch.ept_identity_pagetable_done. */
4653 mutex_lock(&kvm->slots_lock);
4654
Tang Chenf51770e2014-09-16 18:41:59 +08004655 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004656 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004657
Sheng Yangb927a3c2009-07-21 10:42:48 +08004658 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004659
4660 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004661 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004662 goto out2;
4663
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004664 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004665 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4666 if (r < 0)
4667 goto out;
4668 /* Set up identity-mapping pagetable for EPT in real mode */
4669 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4670 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4671 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4672 r = kvm_write_guest_page(kvm, identity_map_pfn,
4673 &tmp, i * sizeof(tmp), sizeof(tmp));
4674 if (r < 0)
4675 goto out;
4676 }
4677 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004678
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004679out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004680 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004681
4682out2:
4683 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004684 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004685}
4686
Avi Kivity6aa8b732006-12-10 02:21:36 -08004687static void seg_setup(int seg)
4688{
Mathias Krause772e0312012-08-30 01:30:19 +02004689 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004690 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004691
4692 vmcs_write16(sf->selector, 0);
4693 vmcs_writel(sf->base, 0);
4694 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004695 ar = 0x93;
4696 if (seg == VCPU_SREG_CS)
4697 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004698
4699 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700}
4701
Sheng Yangf78e0e22007-10-29 09:40:42 +08004702static int alloc_apic_access_page(struct kvm *kvm)
4703{
Xiao Guangrong44841412012-09-07 14:14:20 +08004704 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004705 int r = 0;
4706
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004707 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004708 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004709 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004710 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4711 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004712 if (r)
4713 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004714
Tang Chen73a6d942014-09-11 13:38:00 +08004715 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004716 if (is_error_page(page)) {
4717 r = -EFAULT;
4718 goto out;
4719 }
4720
Tang Chenc24ae0d2014-09-24 15:57:58 +08004721 /*
4722 * Do not pin the page in memory, so that memory hot-unplug
4723 * is able to migrate it.
4724 */
4725 put_page(page);
4726 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004727out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004728 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004729 return r;
4730}
4731
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004732static int alloc_identity_pagetable(struct kvm *kvm)
4733{
Tang Chena255d472014-09-16 18:41:58 +08004734 /* Called with kvm->slots_lock held. */
4735
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004736 int r = 0;
4737
Tang Chena255d472014-09-16 18:41:58 +08004738 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4739
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004740 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4741 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004742
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004743 return r;
4744}
4745
Wanpeng Li991e7a02015-09-16 17:30:05 +08004746static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004747{
4748 int vpid;
4749
Avi Kivity919818a2009-03-23 18:01:29 +02004750 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004751 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004752 spin_lock(&vmx_vpid_lock);
4753 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004754 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004755 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004756 else
4757 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004758 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004759 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004760}
4761
Wanpeng Li991e7a02015-09-16 17:30:05 +08004762static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004763{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004764 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004765 return;
4766 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004767 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004768 spin_unlock(&vmx_vpid_lock);
4769}
4770
Paolo Bonzini6236b782018-01-16 16:51:18 +01004771static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4772 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004773{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004774 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004775
4776 if (!cpu_has_vmx_msr_bitmap())
4777 return;
4778
4779 /*
4780 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4781 * have the write-low and read-high bitmap offsets the wrong way round.
4782 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4783 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004784 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004785 if (type & MSR_TYPE_R)
4786 /* read-low */
4787 __clear_bit(msr, msr_bitmap + 0x000 / f);
4788
4789 if (type & MSR_TYPE_W)
4790 /* write-low */
4791 __clear_bit(msr, msr_bitmap + 0x800 / f);
4792
Sheng Yang25c5f222008-03-28 13:18:56 +08004793 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4794 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004795 if (type & MSR_TYPE_R)
4796 /* read-high */
4797 __clear_bit(msr, msr_bitmap + 0x400 / f);
4798
4799 if (type & MSR_TYPE_W)
4800 /* write-high */
4801 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4802
4803 }
4804}
4805
Paolo Bonzini6236b782018-01-16 16:51:18 +01004806static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4807 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004808{
4809 int f = sizeof(unsigned long);
4810
4811 if (!cpu_has_vmx_msr_bitmap())
4812 return;
4813
4814 /*
4815 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4816 * have the write-low and read-high bitmap offsets the wrong way round.
4817 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4818 */
4819 if (msr <= 0x1fff) {
4820 if (type & MSR_TYPE_R)
4821 /* read-low */
4822 __set_bit(msr, msr_bitmap + 0x000 / f);
4823
4824 if (type & MSR_TYPE_W)
4825 /* write-low */
4826 __set_bit(msr, msr_bitmap + 0x800 / f);
4827
4828 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4829 msr &= 0x1fff;
4830 if (type & MSR_TYPE_R)
4831 /* read-high */
4832 __set_bit(msr, msr_bitmap + 0x400 / f);
4833
4834 if (type & MSR_TYPE_W)
4835 /* write-high */
4836 __set_bit(msr, msr_bitmap + 0xc00 / f);
4837
Sheng Yang25c5f222008-03-28 13:18:56 +08004838 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004839}
4840
Paolo Bonzini6236b782018-01-16 16:51:18 +01004841static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4842 u32 msr, int type, bool value)
4843{
4844 if (value)
4845 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4846 else
4847 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4848}
4849
Wincy Vanf2b93282015-02-03 23:56:03 +08004850/*
4851 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4852 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4853 */
4854static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4855 unsigned long *msr_bitmap_nested,
4856 u32 msr, int type)
4857{
4858 int f = sizeof(unsigned long);
4859
4860 if (!cpu_has_vmx_msr_bitmap()) {
4861 WARN_ON(1);
4862 return;
4863 }
4864
4865 /*
4866 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4867 * have the write-low and read-high bitmap offsets the wrong way round.
4868 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4869 */
4870 if (msr <= 0x1fff) {
4871 if (type & MSR_TYPE_R &&
4872 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4873 /* read-low */
4874 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4875
4876 if (type & MSR_TYPE_W &&
4877 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4878 /* write-low */
4879 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4880
4881 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4882 msr &= 0x1fff;
4883 if (type & MSR_TYPE_R &&
4884 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4885 /* read-high */
4886 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4887
4888 if (type & MSR_TYPE_W &&
4889 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4890 /* write-high */
4891 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4892
4893 }
4894}
4895
Paolo Bonzini6236b782018-01-16 16:51:18 +01004896static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004897{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004898 u8 mode = 0;
4899
4900 if (cpu_has_secondary_exec_ctrls() &&
4901 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4902 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4903 mode |= MSR_BITMAP_MODE_X2APIC;
4904 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4905 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4906 }
4907
4908 if (is_long_mode(vcpu))
4909 mode |= MSR_BITMAP_MODE_LM;
4910
4911 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004912}
4913
Paolo Bonzini6236b782018-01-16 16:51:18 +01004914#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
4915
4916static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4917 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08004918{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004919 int msr;
4920
4921 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4922 unsigned word = msr / BITS_PER_LONG;
4923 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4924 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4925 }
4926
4927 if (mode & MSR_BITMAP_MODE_X2APIC) {
4928 /*
4929 * TPR reads and writes can be virtualized even if virtual interrupt
4930 * delivery is not in use.
4931 */
4932 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4933 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4934 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4935 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4936 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4937 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004938 }
Yang Zhang8d146952013-01-25 10:18:50 +08004939}
4940
Paolo Bonzini6236b782018-01-16 16:51:18 +01004941static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08004942{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004943 struct vcpu_vmx *vmx = to_vmx(vcpu);
4944 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4945 u8 mode = vmx_msr_bitmap_mode(vcpu);
4946 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004947
Paolo Bonzini6236b782018-01-16 16:51:18 +01004948 if (!changed)
4949 return;
4950
4951 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
4952 !(mode & MSR_BITMAP_MODE_LM));
4953
4954 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4955 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4956
4957 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02004958}
4959
Andrey Smetanind62caab2015-11-10 15:36:33 +03004960static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004961{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004962 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004963}
4964
David Matlackb7649e12017-08-01 14:00:40 -07004965static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
4966{
4967 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4968 gfn_t gfn;
4969
4970 /*
4971 * Don't need to mark the APIC access page dirty; it is never
4972 * written to by the CPU during APIC virtualization.
4973 */
4974
4975 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
4976 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
4977 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4978 }
4979
4980 if (nested_cpu_has_posted_intr(vmcs12)) {
4981 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
4982 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4983 }
4984}
4985
4986
David Hildenbrand1edccf22017-01-25 11:58:58 +01004987static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08004988{
4989 struct vcpu_vmx *vmx = to_vmx(vcpu);
4990 int max_irr;
4991 void *vapic_page;
4992 u16 status;
4993
David Matlackb7649e12017-08-01 14:00:40 -07004994 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
4995 return;
Wincy Van705699a2015-02-03 23:58:17 +08004996
David Matlackb7649e12017-08-01 14:00:40 -07004997 vmx->nested.pi_pending = false;
4998 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4999 return;
Wincy Van705699a2015-02-03 23:58:17 +08005000
David Matlackb7649e12017-08-01 14:00:40 -07005001 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
5002 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08005003 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08005004 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
5005 kunmap(vmx->nested.virtual_apic_page);
5006
5007 status = vmcs_read16(GUEST_INTR_STATUS);
5008 if ((u8)max_irr > ((u8)status & 0xff)) {
5009 status &= ~0xff;
5010 status |= (u8)max_irr;
5011 vmcs_write16(GUEST_INTR_STATUS, status);
5012 }
5013 }
David Matlackb7649e12017-08-01 14:00:40 -07005014
5015 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005016}
5017
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005018static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
5019{
5020#ifdef CONFIG_SMP
5021 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005022 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005023 * The vector of interrupt to be delivered to vcpu had
5024 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005025 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005026 * Following cases will be reached in this block, and
5027 * we always send a notification event in all cases as
5028 * explained below.
5029 *
5030 * Case 1: vcpu keeps in non-root mode. Sending a
5031 * notification event posts the interrupt to vcpu.
5032 *
5033 * Case 2: vcpu exits to root mode and is still
5034 * runnable. PIR will be synced to vIRR before the
5035 * next vcpu entry. Sending a notification event in
5036 * this case has no effect, as vcpu is not in root
5037 * mode.
5038 *
5039 * Case 3: vcpu exits to root mode and is blocked.
5040 * vcpu_block() has already synced PIR to vIRR and
5041 * never blocks vcpu if vIRR is not cleared. Therefore,
5042 * a blocked vcpu here does not wait for any requested
5043 * interrupts in PIR, and sending a notification event
5044 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005045 */
Feng Wu28b835d2015-09-18 22:29:54 +08005046
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005047 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
5048 POSTED_INTR_VECTOR);
5049 return true;
5050 }
5051#endif
5052 return false;
5053}
5054
Wincy Van705699a2015-02-03 23:58:17 +08005055static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5056 int vector)
5057{
5058 struct vcpu_vmx *vmx = to_vmx(vcpu);
5059
5060 if (is_guest_mode(vcpu) &&
5061 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005062 /*
5063 * If a posted intr is not recognized by hardware,
5064 * we will accomplish it in the next vmentry.
5065 */
5066 vmx->nested.pi_pending = true;
5067 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02005068 /* the PIR and ON have been set by L1. */
5069 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
5070 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005071 return 0;
5072 }
5073 return -1;
5074}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005076 * Send interrupt to vcpu via posted interrupt way.
5077 * 1. If target vcpu is running(non-root mode), send posted interrupt
5078 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5079 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5080 * interrupt from PIR in next vmentry.
5081 */
5082static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5083{
5084 struct vcpu_vmx *vmx = to_vmx(vcpu);
5085 int r;
5086
Wincy Van705699a2015-02-03 23:58:17 +08005087 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5088 if (!r)
5089 return;
5090
Yang Zhanga20ed542013-04-11 19:25:15 +08005091 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5092 return;
5093
5094 r = pi_test_and_set_on(&vmx->pi_desc);
5095 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005096 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005097 kvm_vcpu_kick(vcpu);
5098}
5099
5100static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5101{
5102 struct vcpu_vmx *vmx = to_vmx(vcpu);
5103
5104 if (!pi_test_and_clear_on(&vmx->pi_desc))
5105 return;
5106
5107 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5108}
5109
Avi Kivity6aa8b732006-12-10 02:21:36 -08005110/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005111 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5112 * will not change in the lifetime of the guest.
5113 * Note that host-state that does change is set elsewhere. E.g., host-state
5114 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5115 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005116static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005117{
5118 u32 low32, high32;
5119 unsigned long tmpl;
5120 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005121 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005122
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005123 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005124 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5125
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005126 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005127 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005128 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5129 vmx->host_state.vmcs_host_cr4 = cr4;
5130
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005131 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005132#ifdef CONFIG_X86_64
5133 /*
5134 * Load null selectors, so we can avoid reloading them in
5135 * __vmx_load_host_state(), in case userspace uses the null selectors
5136 * too (the expected case).
5137 */
5138 vmcs_write16(HOST_DS_SELECTOR, 0);
5139 vmcs_write16(HOST_ES_SELECTOR, 0);
5140#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005141 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5142 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005143#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005144 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5145 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5146
5147 native_store_idt(&dt);
5148 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005149 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005150
Avi Kivity83287ea422012-09-16 15:10:57 +03005151 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005152
5153 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5154 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5155 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5156 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5157
5158 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5159 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5160 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5161 }
5162}
5163
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005164static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5165{
5166 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5167 if (enable_ept)
5168 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005169 if (is_guest_mode(&vmx->vcpu))
5170 vmx->vcpu.arch.cr4_guest_owned_bits &=
5171 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005172 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5173}
5174
Yang Zhang01e439b2013-04-11 19:25:12 +08005175static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5176{
5177 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5178
Andrey Smetanind62caab2015-11-10 15:36:33 +03005179 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005180 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005181 /* Enable the preemption timer dynamically */
5182 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005183 return pin_based_exec_ctrl;
5184}
5185
Andrey Smetanind62caab2015-11-10 15:36:33 +03005186static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5187{
5188 struct vcpu_vmx *vmx = to_vmx(vcpu);
5189
5190 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005191 if (cpu_has_secondary_exec_ctrls()) {
5192 if (kvm_vcpu_apicv_active(vcpu))
5193 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5194 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5195 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5196 else
5197 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5198 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5199 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5200 }
5201
5202 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005203 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005204}
5205
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005206static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5207{
5208 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005209
5210 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5211 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5212
Paolo Bonzini35754c92015-07-29 12:05:37 +02005213 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005214 exec_control &= ~CPU_BASED_TPR_SHADOW;
5215#ifdef CONFIG_X86_64
5216 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5217 CPU_BASED_CR8_LOAD_EXITING;
5218#endif
5219 }
5220 if (!enable_ept)
5221 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5222 CPU_BASED_CR3_LOAD_EXITING |
5223 CPU_BASED_INVLPG_EXITING;
5224 return exec_control;
5225}
5226
5227static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5228{
5229 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005230 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005231 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5232 if (vmx->vpid == 0)
5233 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5234 if (!enable_ept) {
5235 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5236 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005237 /* Enable INVPCID for non-ept guests may cause performance regression. */
5238 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005239 }
5240 if (!enable_unrestricted_guest)
5241 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5242 if (!ple_gap)
5243 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005244 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005245 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5246 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005247 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005248 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5249 (handle_vmptrld).
5250 We can NOT enable shadow_vmcs here because we don't have yet
5251 a current VMCS12
5252 */
5253 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005254
5255 if (!enable_pml)
5256 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005257
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005258 return exec_control;
5259}
5260
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005261static void ept_set_mmio_spte_mask(void)
5262{
5263 /*
5264 * EPT Misconfigurations can be generated if the value of bits 2:0
5265 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005266 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005267 * spte.
5268 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005269 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005270}
5271
Wanpeng Lif53cd632014-12-02 19:14:58 +08005272#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005273/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005274 * Sets up the vmcs for emulated real mode.
5275 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005276static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005277{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005278#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005280#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005281 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005282
Avi Kivity6aa8b732006-12-10 02:21:36 -08005283 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005284 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5285 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005286
Abel Gordon4607c2d2013-04-18 14:35:55 +03005287 if (enable_shadow_vmcs) {
5288 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5289 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5290 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005291 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005292 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005293
Avi Kivity6aa8b732006-12-10 02:21:36 -08005294 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5295
Avi Kivity6aa8b732006-12-10 02:21:36 -08005296 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005297 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005298 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005299
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005300 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005301
Dan Williamsdfa169b2016-06-02 11:17:24 -07005302 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005303 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5304 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005305 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005306
Andrey Smetanind62caab2015-11-10 15:36:33 +03005307 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005308 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5309 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5310 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5311 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5312
5313 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005314
Li RongQing0bcf2612015-12-03 13:29:34 +08005315 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005316 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005317 }
5318
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005319 if (ple_gap) {
5320 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005321 vmx->ple_window = ple_window;
5322 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005323 }
5324
Xiao Guangrongc3707952011-07-12 03:28:04 +08005325 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5326 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005327 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5328
Avi Kivity9581d442010-10-19 16:46:55 +02005329 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5330 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005331 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005332#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005333 rdmsrl(MSR_FS_BASE, a);
5334 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5335 rdmsrl(MSR_GS_BASE, a);
5336 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5337#else
5338 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5339 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5340#endif
5341
Eddie Dong2cc51562007-05-21 07:28:09 +03005342 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5343 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005344 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03005345 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005346 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005347
Radim Krčmář74545702015-04-27 15:11:25 +02005348 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5349 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005350
Paolo Bonzini03916db2014-07-24 14:21:57 +02005351 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005352 u32 index = vmx_msr_index[i];
5353 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005354 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005355
5356 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5357 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005358 if (wrmsr_safe(index, data_low, data_high) < 0)
5359 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005360 vmx->guest_msrs[j].index = i;
5361 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005362 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005363 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005364 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005365
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01005366 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5367 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005368
5369 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005370
5371 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005372 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005373
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005374 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005375 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005376
Wanpeng Lif53cd632014-12-02 19:14:58 +08005377 if (vmx_xsaves_supported())
5378 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5379
Peter Feiner4e595162016-07-07 14:49:58 -07005380 if (enable_pml) {
5381 ASSERT(vmx->pml_pg);
5382 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5383 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5384 }
5385
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005386 return 0;
5387}
5388
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005389static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005390{
5391 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005392 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005393 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005394
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005395 vmx->rmode.vm86_active = 0;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005396 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005397
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005398 vmx->soft_vnmi_blocked = 0;
5399
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005400 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005401 kvm_set_cr8(vcpu, 0);
5402
5403 if (!init_event) {
5404 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5405 MSR_IA32_APICBASE_ENABLE;
5406 if (kvm_vcpu_is_reset_bsp(vcpu))
5407 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5408 apic_base_msr.host_initiated = true;
5409 kvm_set_apic_base(vcpu, &apic_base_msr);
5410 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005411
Avi Kivity2fb92db2011-04-27 19:42:18 +03005412 vmx_segment_cache_clear(vmx);
5413
Avi Kivity5706be02008-08-20 15:07:31 +03005414 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005415 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005416 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005417
5418 seg_setup(VCPU_SREG_DS);
5419 seg_setup(VCPU_SREG_ES);
5420 seg_setup(VCPU_SREG_FS);
5421 seg_setup(VCPU_SREG_GS);
5422 seg_setup(VCPU_SREG_SS);
5423
5424 vmcs_write16(GUEST_TR_SELECTOR, 0);
5425 vmcs_writel(GUEST_TR_BASE, 0);
5426 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5427 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5428
5429 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5430 vmcs_writel(GUEST_LDTR_BASE, 0);
5431 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5432 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5433
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005434 if (!init_event) {
5435 vmcs_write32(GUEST_SYSENTER_CS, 0);
5436 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5437 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5438 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5439 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005440
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005441 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005442 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005443
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005444 vmcs_writel(GUEST_GDTR_BASE, 0);
5445 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5446
5447 vmcs_writel(GUEST_IDTR_BASE, 0);
5448 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5449
Anthony Liguori443381a2010-12-06 10:53:38 -06005450 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005451 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005452 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005453
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005454 setup_msrs(vmx);
5455
Avi Kivity6aa8b732006-12-10 02:21:36 -08005456 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5457
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005458 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005459 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005460 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005461 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005462 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005463 vmcs_write32(TPR_THRESHOLD, 0);
5464 }
5465
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005466 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005467
Andrey Smetanind62caab2015-11-10 15:36:33 +03005468 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005469 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5470
Sheng Yang2384d2b2008-01-17 15:14:33 +08005471 if (vmx->vpid != 0)
5472 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5473
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005474 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005475 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005476 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005477 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005478 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005479 vmx_fpu_activate(vcpu);
5480 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005481
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005482 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005483}
5484
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005485/*
5486 * In nested virtualization, check if L1 asked to exit on external interrupts.
5487 * For most existing hypervisors, this will always return true.
5488 */
5489static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5490{
5491 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5492 PIN_BASED_EXT_INTR_MASK;
5493}
5494
Bandan Das77b0f5d2014-04-19 18:17:45 -04005495/*
5496 * In nested virtualization, check if L1 has set
5497 * VM_EXIT_ACK_INTR_ON_EXIT
5498 */
5499static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5500{
5501 return get_vmcs12(vcpu)->vm_exit_controls &
5502 VM_EXIT_ACK_INTR_ON_EXIT;
5503}
5504
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005505static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5506{
5507 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5508 PIN_BASED_NMI_EXITING;
5509}
5510
Jan Kiszkac9a79532014-03-07 20:03:15 +01005511static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005512{
5513 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005514
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005515 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5516 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5517 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5518}
5519
Jan Kiszkac9a79532014-03-07 20:03:15 +01005520static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005521{
5522 u32 cpu_based_vm_exec_control;
5523
Jan Kiszkac9a79532014-03-07 20:03:15 +01005524 if (!cpu_has_virtual_nmis() ||
5525 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5526 enable_irq_window(vcpu);
5527 return;
5528 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005529
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005530 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5531 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5532 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5533}
5534
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005535static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005536{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005537 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005538 uint32_t intr;
5539 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005540
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005541 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005542
Avi Kivityfa89a812008-09-01 15:57:51 +03005543 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005544 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005545 int inc_eip = 0;
5546 if (vcpu->arch.interrupt.soft)
5547 inc_eip = vcpu->arch.event_exit_inst_len;
5548 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005549 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005550 return;
5551 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005552 intr = irq | INTR_INFO_VALID_MASK;
5553 if (vcpu->arch.interrupt.soft) {
5554 intr |= INTR_TYPE_SOFT_INTR;
5555 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5556 vmx->vcpu.arch.event_exit_inst_len);
5557 } else
5558 intr |= INTR_TYPE_EXT_INTR;
5559 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005560}
5561
Sheng Yangf08864b2008-05-15 18:23:25 +08005562static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5563{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005564 struct vcpu_vmx *vmx = to_vmx(vcpu);
5565
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005566 if (!is_guest_mode(vcpu)) {
5567 if (!cpu_has_virtual_nmis()) {
5568 /*
5569 * Tracking the NMI-blocked state in software is built upon
5570 * finding the next open IRQ window. This, in turn, depends on
5571 * well-behaving guests: They have to keep IRQs disabled at
5572 * least as long as the NMI handler runs. Otherwise we may
5573 * cause NMI nesting, maybe breaking the guest. But as this is
5574 * highly unlikely, we can live with the residual risk.
5575 */
5576 vmx->soft_vnmi_blocked = 1;
5577 vmx->vnmi_blocked_time = 0;
5578 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005579
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005580 ++vcpu->stat.nmi_injections;
5581 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005582 }
5583
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005584 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005585 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005586 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005587 return;
5588 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005589
Sheng Yangf08864b2008-05-15 18:23:25 +08005590 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5591 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005592}
5593
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005594static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5595{
5596 if (!cpu_has_virtual_nmis())
5597 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005598 if (to_vmx(vcpu)->nmi_known_unmasked)
5599 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005600 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005601}
5602
5603static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5604{
5605 struct vcpu_vmx *vmx = to_vmx(vcpu);
5606
5607 if (!cpu_has_virtual_nmis()) {
5608 if (vmx->soft_vnmi_blocked != masked) {
5609 vmx->soft_vnmi_blocked = masked;
5610 vmx->vnmi_blocked_time = 0;
5611 }
5612 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005613 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005614 if (masked)
5615 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5616 GUEST_INTR_STATE_NMI);
5617 else
5618 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5619 GUEST_INTR_STATE_NMI);
5620 }
5621}
5622
Jan Kiszka2505dc92013-04-14 12:12:47 +02005623static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5624{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005625 if (to_vmx(vcpu)->nested.nested_run_pending)
5626 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005627
Jan Kiszka2505dc92013-04-14 12:12:47 +02005628 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5629 return 0;
5630
5631 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5632 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5633 | GUEST_INTR_STATE_NMI));
5634}
5635
Gleb Natapov78646122009-03-23 12:12:11 +02005636static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5637{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005638 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5639 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005640 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5641 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005642}
5643
Izik Eiduscbc94022007-10-25 00:29:55 +02005644static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5645{
5646 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005647
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005648 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5649 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005650 if (ret)
5651 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005652 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005653 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005654}
5655
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005656static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005657{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005658 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005659 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005660 /*
5661 * Update instruction length as we may reinject the exception
5662 * from user space while in guest debugging mode.
5663 */
5664 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5665 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005666 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005667 return false;
5668 /* fall through */
5669 case DB_VECTOR:
5670 if (vcpu->guest_debug &
5671 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5672 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005673 /* fall through */
5674 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005675 case OF_VECTOR:
5676 case BR_VECTOR:
5677 case UD_VECTOR:
5678 case DF_VECTOR:
5679 case SS_VECTOR:
5680 case GP_VECTOR:
5681 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005682 return true;
5683 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005684 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005685 return false;
5686}
5687
5688static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5689 int vec, u32 err_code)
5690{
5691 /*
5692 * Instruction with address size override prefix opcode 0x67
5693 * Cause the #SS fault with 0 error code in VM86 mode.
5694 */
5695 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5696 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5697 if (vcpu->arch.halt_request) {
5698 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005699 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005700 }
5701 return 1;
5702 }
5703 return 0;
5704 }
5705
5706 /*
5707 * Forward all other exceptions that are valid in real mode.
5708 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5709 * the required debugging infrastructure rework.
5710 */
5711 kvm_queue_exception(vcpu, vec);
5712 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005713}
5714
Andi Kleena0861c02009-06-08 17:37:09 +08005715/*
5716 * Trigger machine check on the host. We assume all the MSRs are already set up
5717 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5718 * We pass a fake environment to the machine check handler because we want
5719 * the guest to be always treated like user space, no matter what context
5720 * it used internally.
5721 */
5722static void kvm_machine_check(void)
5723{
5724#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5725 struct pt_regs regs = {
5726 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5727 .flags = X86_EFLAGS_IF,
5728 };
5729
5730 do_machine_check(&regs, 0);
5731#endif
5732}
5733
Avi Kivity851ba692009-08-24 11:10:17 +03005734static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005735{
5736 /* already handled by vcpu_run */
5737 return 1;
5738}
5739
Avi Kivity851ba692009-08-24 11:10:17 +03005740static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005741{
Avi Kivity1155f762007-11-22 11:30:47 +02005742 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005743 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005744 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005745 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005746 u32 vect_info;
5747 enum emulation_result er;
5748
Avi Kivity1155f762007-11-22 11:30:47 +02005749 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005750 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005751
Andi Kleena0861c02009-06-08 17:37:09 +08005752 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005753 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005754
Jim Mattson3f618a02016-12-12 11:01:37 -08005755 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005756 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005757
5758 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005759 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005760 return 1;
5761 }
5762
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005763 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005764 if (is_guest_mode(vcpu)) {
5765 kvm_queue_exception(vcpu, UD_VECTOR);
5766 return 1;
5767 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005768 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005769 if (er == EMULATE_USER_EXIT)
5770 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005771 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005772 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005773 return 1;
5774 }
5775
Avi Kivity6aa8b732006-12-10 02:21:36 -08005776 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005777 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005778 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005779
5780 /*
5781 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5782 * MMIO, it is better to report an internal error.
5783 * See the comments in vmx_handle_exit.
5784 */
5785 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5786 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5787 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5788 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005789 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005790 vcpu->run->internal.data[0] = vect_info;
5791 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005792 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005793 return 0;
5794 }
5795
Avi Kivity6aa8b732006-12-10 02:21:36 -08005796 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005797 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005798 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005799 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005800 trace_kvm_page_fault(cr2, error_code);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02005801 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005802
Gleb Natapov3298b752009-05-11 13:35:46 +03005803 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005804 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005805 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005806 }
5807
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005808 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005809
5810 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5811 return handle_rmode_exception(vcpu, ex_no, error_code);
5812
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005813 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005814 case AC_VECTOR:
5815 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5816 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005817 case DB_VECTOR:
5818 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5819 if (!(vcpu->guest_debug &
5820 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005821 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005822 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005823 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005824 skip_emulated_instruction(vcpu);
5825
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005826 kvm_queue_exception(vcpu, DB_VECTOR);
5827 return 1;
5828 }
5829 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5830 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5831 /* fall through */
5832 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005833 /*
5834 * Update instruction length as we may reinject #BP from
5835 * user space while in guest debugging mode. Reading it for
5836 * #DB as well causes no harm, it is not used in that case.
5837 */
5838 vmx->vcpu.arch.event_exit_inst_len =
5839 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005840 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005841 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005842 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5843 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005844 break;
5845 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005846 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5847 kvm_run->ex.exception = ex_no;
5848 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005849 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005850 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005851 return 0;
5852}
5853
Avi Kivity851ba692009-08-24 11:10:17 +03005854static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005855{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005856 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005857 return 1;
5858}
5859
Avi Kivity851ba692009-08-24 11:10:17 +03005860static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005861{
Avi Kivity851ba692009-08-24 11:10:17 +03005862 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005863 return 0;
5864}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005865
Avi Kivity851ba692009-08-24 11:10:17 +03005866static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005867{
He, Qingbfdaab02007-09-12 14:18:28 +08005868 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005869 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005870 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005871
He, Qingbfdaab02007-09-12 14:18:28 +08005872 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005873 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005874 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005875
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005876 ++vcpu->stat.io_exits;
5877
5878 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005879 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005880
5881 port = exit_qualification >> 16;
5882 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005883 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005884
5885 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005886}
5887
Ingo Molnar102d8322007-02-19 14:37:47 +02005888static void
5889vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5890{
5891 /*
5892 * Patch in the VMCALL instruction:
5893 */
5894 hypercall[0] = 0x0f;
5895 hypercall[1] = 0x01;
5896 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005897}
5898
Wincy Vanb9c237b2015-02-03 23:56:30 +08005899static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005900{
5901 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005902 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005903
Wincy Vanb9c237b2015-02-03 23:56:30 +08005904 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005905 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5906 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5907 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5908 return (val & always_on) == always_on;
5909}
5910
Guo Chao0fa06072012-06-28 15:16:19 +08005911/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005912static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5913{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005914 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005915 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5916 unsigned long orig_val = val;
5917
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005918 /*
5919 * We get here when L2 changed cr0 in a way that did not change
5920 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005921 * but did change L0 shadowed bits. So we first calculate the
5922 * effective cr0 value that L1 would like to write into the
5923 * hardware. It consists of the L2-owned bits from the new
5924 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005925 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005926 val = (val & ~vmcs12->cr0_guest_host_mask) |
5927 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5928
Wincy Vanb9c237b2015-02-03 23:56:30 +08005929 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005930 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005931
5932 if (kvm_set_cr0(vcpu, val))
5933 return 1;
5934 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005935 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005936 } else {
5937 if (to_vmx(vcpu)->nested.vmxon &&
5938 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5939 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005940 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005941 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005942}
5943
5944static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5945{
5946 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005947 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5948 unsigned long orig_val = val;
5949
5950 /* analogously to handle_set_cr0 */
5951 val = (val & ~vmcs12->cr4_guest_host_mask) |
5952 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5953 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005954 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005955 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005956 return 0;
5957 } else
5958 return kvm_set_cr4(vcpu, val);
5959}
5960
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005961/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005962static void handle_clts(struct kvm_vcpu *vcpu)
5963{
5964 if (is_guest_mode(vcpu)) {
5965 /*
5966 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5967 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5968 * just pretend it's off (also in arch.cr0 for fpu_activate).
5969 */
5970 vmcs_writel(CR0_READ_SHADOW,
5971 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5972 vcpu->arch.cr0 &= ~X86_CR0_TS;
5973 } else
5974 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5975}
5976
Avi Kivity851ba692009-08-24 11:10:17 +03005977static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005978{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005979 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005980 int cr;
5981 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005982 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005983
He, Qingbfdaab02007-09-12 14:18:28 +08005984 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005985 cr = exit_qualification & 15;
5986 reg = (exit_qualification >> 8) & 15;
5987 switch ((exit_qualification >> 4) & 3) {
5988 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005989 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005990 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005991 switch (cr) {
5992 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005993 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005994 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005995 return 1;
5996 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005997 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005998 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005999 return 1;
6000 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006001 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006002 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006003 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006004 case 8: {
6005 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006006 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006007 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006008 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006009 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006010 return 1;
6011 if (cr8_prev <= cr8)
6012 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03006013 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006014 return 0;
6015 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006016 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006017 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006018 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006019 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02006020 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03006021 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02006022 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03006023 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006024 case 1: /*mov from cr*/
6025 switch (cr) {
6026 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006027 val = kvm_read_cr3(vcpu);
6028 kvm_register_write(vcpu, reg, val);
6029 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006030 skip_emulated_instruction(vcpu);
6031 return 1;
6032 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006033 val = kvm_get_cr8(vcpu);
6034 kvm_register_write(vcpu, reg, val);
6035 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006036 skip_emulated_instruction(vcpu);
6037 return 1;
6038 }
6039 break;
6040 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006041 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006042 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006043 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006044
6045 skip_emulated_instruction(vcpu);
6046 return 1;
6047 default:
6048 break;
6049 }
Avi Kivity851ba692009-08-24 11:10:17 +03006050 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006051 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006052 (int)(exit_qualification >> 4) & 3, cr);
6053 return 0;
6054}
6055
Avi Kivity851ba692009-08-24 11:10:17 +03006056static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006057{
He, Qingbfdaab02007-09-12 14:18:28 +08006058 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006059 int dr, dr7, reg;
6060
6061 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6062 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6063
6064 /* First, if DR does not exist, trigger UD */
6065 if (!kvm_require_dr(vcpu, dr))
6066 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006067
Jan Kiszkaf2483412010-01-20 18:20:20 +01006068 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006069 if (!kvm_require_cpl(vcpu, 0))
6070 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006071 dr7 = vmcs_readl(GUEST_DR7);
6072 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006073 /*
6074 * As the vm-exit takes precedence over the debug trap, we
6075 * need to emulate the latter, either for the host or the
6076 * guest debugging itself.
6077 */
6078 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006079 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006080 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006081 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006082 vcpu->run->debug.arch.exception = DB_VECTOR;
6083 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006084 return 0;
6085 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006086 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006087 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006088 kvm_queue_exception(vcpu, DB_VECTOR);
6089 return 1;
6090 }
6091 }
6092
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006093 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006094 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6095 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006096
6097 /*
6098 * No more DR vmexits; force a reload of the debug registers
6099 * and reenter on this instruction. The next vmexit will
6100 * retrieve the full state of the debug registers.
6101 */
6102 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6103 return 1;
6104 }
6105
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006106 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6107 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006108 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006109
6110 if (kvm_get_dr(vcpu, dr, &val))
6111 return 1;
6112 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006113 } else
Nadav Amit57773922014-06-18 17:19:23 +03006114 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006115 return 1;
6116
Avi Kivity6aa8b732006-12-10 02:21:36 -08006117 skip_emulated_instruction(vcpu);
6118 return 1;
6119}
6120
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006121static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6122{
6123 return vcpu->arch.dr6;
6124}
6125
6126static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6127{
6128}
6129
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006130static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6131{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006132 get_debugreg(vcpu->arch.db[0], 0);
6133 get_debugreg(vcpu->arch.db[1], 1);
6134 get_debugreg(vcpu->arch.db[2], 2);
6135 get_debugreg(vcpu->arch.db[3], 3);
6136 get_debugreg(vcpu->arch.dr6, 6);
6137 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6138
6139 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006140 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006141}
6142
Gleb Natapov020df072010-04-13 10:05:23 +03006143static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6144{
6145 vmcs_writel(GUEST_DR7, val);
6146}
6147
Avi Kivity851ba692009-08-24 11:10:17 +03006148static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006149{
Avi Kivity06465c52007-02-28 20:46:53 +02006150 kvm_emulate_cpuid(vcpu);
6151 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006152}
6153
Avi Kivity851ba692009-08-24 11:10:17 +03006154static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006155{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006156 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006157 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006158
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006159 msr_info.index = ecx;
6160 msr_info.host_initiated = false;
6161 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006162 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006163 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006164 return 1;
6165 }
6166
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006167 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006168
Avi Kivity6aa8b732006-12-10 02:21:36 -08006169 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006170 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6171 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006172 skip_emulated_instruction(vcpu);
6173 return 1;
6174}
6175
Avi Kivity851ba692009-08-24 11:10:17 +03006176static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006177{
Will Auld8fe8ab42012-11-29 12:42:12 -08006178 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006179 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6180 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6181 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006182
Will Auld8fe8ab42012-11-29 12:42:12 -08006183 msr.data = data;
6184 msr.index = ecx;
6185 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006186 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006187 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006188 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006189 return 1;
6190 }
6191
Avi Kivity59200272010-01-25 19:47:02 +02006192 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006193 skip_emulated_instruction(vcpu);
6194 return 1;
6195}
6196
Avi Kivity851ba692009-08-24 11:10:17 +03006197static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006198{
Avi Kivity3842d132010-07-27 12:30:24 +03006199 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006200 return 1;
6201}
6202
Avi Kivity851ba692009-08-24 11:10:17 +03006203static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006204{
Eddie Dong85f455f2007-07-06 12:20:49 +03006205 u32 cpu_based_vm_exec_control;
6206
6207 /* clear pending irq */
6208 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6209 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6210 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006211
Avi Kivity3842d132010-07-27 12:30:24 +03006212 kvm_make_request(KVM_REQ_EVENT, vcpu);
6213
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006214 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006215 return 1;
6216}
6217
Avi Kivity851ba692009-08-24 11:10:17 +03006218static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006219{
Avi Kivityd3bef152007-06-05 15:53:05 +03006220 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006221}
6222
Avi Kivity851ba692009-08-24 11:10:17 +03006223static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006224{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006225 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006226}
6227
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006228static int handle_invd(struct kvm_vcpu *vcpu)
6229{
Andre Przywara51d8b662010-12-21 11:12:02 +01006230 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006231}
6232
Avi Kivity851ba692009-08-24 11:10:17 +03006233static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006234{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006235 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006236
6237 kvm_mmu_invlpg(vcpu, exit_qualification);
6238 skip_emulated_instruction(vcpu);
6239 return 1;
6240}
6241
Avi Kivityfee84b02011-11-10 14:57:25 +02006242static int handle_rdpmc(struct kvm_vcpu *vcpu)
6243{
6244 int err;
6245
6246 err = kvm_rdpmc(vcpu);
6247 kvm_complete_insn_gp(vcpu, err);
6248
6249 return 1;
6250}
6251
Avi Kivity851ba692009-08-24 11:10:17 +03006252static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006253{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006254 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006255 return 1;
6256}
6257
Dexuan Cui2acf9232010-06-10 11:27:12 +08006258static int handle_xsetbv(struct kvm_vcpu *vcpu)
6259{
6260 u64 new_bv = kvm_read_edx_eax(vcpu);
6261 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6262
6263 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6264 skip_emulated_instruction(vcpu);
6265 return 1;
6266}
6267
Wanpeng Lif53cd632014-12-02 19:14:58 +08006268static int handle_xsaves(struct kvm_vcpu *vcpu)
6269{
6270 skip_emulated_instruction(vcpu);
6271 WARN(1, "this should never happen\n");
6272 return 1;
6273}
6274
6275static int handle_xrstors(struct kvm_vcpu *vcpu)
6276{
6277 skip_emulated_instruction(vcpu);
6278 WARN(1, "this should never happen\n");
6279 return 1;
6280}
6281
Avi Kivity851ba692009-08-24 11:10:17 +03006282static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006283{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006284 if (likely(fasteoi)) {
6285 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6286 int access_type, offset;
6287
6288 access_type = exit_qualification & APIC_ACCESS_TYPE;
6289 offset = exit_qualification & APIC_ACCESS_OFFSET;
6290 /*
6291 * Sane guest uses MOV to write EOI, with written value
6292 * not cared. So make a short-circuit here by avoiding
6293 * heavy instruction emulation.
6294 */
6295 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6296 (offset == APIC_EOI)) {
6297 kvm_lapic_set_eoi(vcpu);
6298 skip_emulated_instruction(vcpu);
6299 return 1;
6300 }
6301 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006302 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006303}
6304
Yang Zhangc7c9c562013-01-25 10:18:51 +08006305static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6306{
6307 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6308 int vector = exit_qualification & 0xff;
6309
6310 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6311 kvm_apic_set_eoi_accelerated(vcpu, vector);
6312 return 1;
6313}
6314
Yang Zhang83d4c282013-01-25 10:18:49 +08006315static int handle_apic_write(struct kvm_vcpu *vcpu)
6316{
6317 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6318 u32 offset = exit_qualification & 0xfff;
6319
6320 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6321 kvm_apic_write_nodecode(vcpu, offset);
6322 return 1;
6323}
6324
Avi Kivity851ba692009-08-24 11:10:17 +03006325static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006326{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006327 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006328 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006329 bool has_error_code = false;
6330 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006331 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006332 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006333
6334 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006335 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006336 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006337
6338 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6339
6340 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006341 if (reason == TASK_SWITCH_GATE && idt_v) {
6342 switch (type) {
6343 case INTR_TYPE_NMI_INTR:
6344 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006345 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006346 break;
6347 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006348 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006349 kvm_clear_interrupt_queue(vcpu);
6350 break;
6351 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006352 if (vmx->idt_vectoring_info &
6353 VECTORING_INFO_DELIVER_CODE_MASK) {
6354 has_error_code = true;
6355 error_code =
6356 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6357 }
6358 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006359 case INTR_TYPE_SOFT_EXCEPTION:
6360 kvm_clear_exception_queue(vcpu);
6361 break;
6362 default:
6363 break;
6364 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006365 }
Izik Eidus37817f22008-03-24 23:14:53 +02006366 tss_selector = exit_qualification;
6367
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006368 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6369 type != INTR_TYPE_EXT_INTR &&
6370 type != INTR_TYPE_NMI_INTR))
6371 skip_emulated_instruction(vcpu);
6372
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006373 if (kvm_task_switch(vcpu, tss_selector,
6374 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6375 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006376 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6377 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6378 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006379 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006380 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006381
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006382 /*
6383 * TODO: What about debug traps on tss switch?
6384 * Are we supposed to inject them and update dr6?
6385 */
6386
6387 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006388}
6389
Avi Kivity851ba692009-08-24 11:10:17 +03006390static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006391{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006392 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006393 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006394 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006395 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006396
Sheng Yangf9c617f2009-03-25 10:08:52 +08006397 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006398
Sheng Yang14394422008-04-28 12:24:45 +08006399 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006400 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006401 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6402 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6403 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006404 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006405 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6406 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006407 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6408 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006409 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006410 }
6411
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006412 /*
6413 * EPT violation happened while executing iret from NMI,
6414 * "blocked by NMI" bit has to be set before next VM entry.
6415 * There are errata that may cause this bit to not be set:
6416 * AAK134, BY25.
6417 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006418 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6419 cpu_has_virtual_nmis() &&
6420 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006421 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6422
Sheng Yang14394422008-04-28 12:24:45 +08006423 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006424 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006425
Bandan Dasd95c5562016-07-12 18:18:51 -04006426 /* it is a read fault? */
6427 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6428 /* it is a write fault? */
6429 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006430 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006431 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006432 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006433 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006434
Yang Zhang25d92082013-08-06 12:00:32 +03006435 vcpu->arch.exit_qualification = exit_qualification;
6436
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006437 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006438}
6439
Avi Kivity851ba692009-08-24 11:10:17 +03006440static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006441{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006442 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006443 gpa_t gpa;
6444
6445 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006446 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006447 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006448 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006449 return 1;
6450 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006451
Paolo Bonzini450869d2015-11-04 13:41:21 +01006452 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006453 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006454 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6455 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006456
6457 if (unlikely(ret == RET_MMIO_PF_INVALID))
6458 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6459
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006460 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006461 return 1;
6462
6463 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006464 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006465
Avi Kivity851ba692009-08-24 11:10:17 +03006466 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6467 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006468
6469 return 0;
6470}
6471
Avi Kivity851ba692009-08-24 11:10:17 +03006472static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006473{
6474 u32 cpu_based_vm_exec_control;
6475
6476 /* clear pending NMI */
6477 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6478 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6479 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6480 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006481 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006482
6483 return 1;
6484}
6485
Mohammed Gamal80ced182009-09-01 12:48:18 +02006486static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006487{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006488 struct vcpu_vmx *vmx = to_vmx(vcpu);
6489 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006490 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006491 u32 cpu_exec_ctrl;
6492 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006493 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006494
6495 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6496 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006497
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006498 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006499 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006500 return handle_interrupt_window(&vmx->vcpu);
6501
Avi Kivityde87dcd2012-06-12 20:21:38 +03006502 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6503 return 1;
6504
Liran Alon114de9b2017-11-05 16:56:34 +02006505 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006506
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006507 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006508 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006509 ret = 0;
6510 goto out;
6511 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006512
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006513 if (err != EMULATE_DONE)
6514 goto emulation_error;
6515
6516 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6517 vcpu->arch.exception.pending)
6518 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006519
Gleb Natapov8d76c492013-05-08 18:38:44 +03006520 if (vcpu->arch.halt_request) {
6521 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006522 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006523 goto out;
6524 }
6525
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006526 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006527 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006528 if (need_resched())
6529 schedule();
6530 }
6531
Mohammed Gamal80ced182009-09-01 12:48:18 +02006532out:
6533 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006534
6535emulation_error:
6536 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6537 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6538 vcpu->run->internal.ndata = 0;
6539 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006540}
6541
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006542static int __grow_ple_window(int val)
6543{
6544 if (ple_window_grow < 1)
6545 return ple_window;
6546
6547 val = min(val, ple_window_actual_max);
6548
6549 if (ple_window_grow < ple_window)
6550 val *= ple_window_grow;
6551 else
6552 val += ple_window_grow;
6553
6554 return val;
6555}
6556
6557static int __shrink_ple_window(int val, int modifier, int minimum)
6558{
6559 if (modifier < 1)
6560 return ple_window;
6561
6562 if (modifier < ple_window)
6563 val /= modifier;
6564 else
6565 val -= modifier;
6566
6567 return max(val, minimum);
6568}
6569
6570static void grow_ple_window(struct kvm_vcpu *vcpu)
6571{
6572 struct vcpu_vmx *vmx = to_vmx(vcpu);
6573 int old = vmx->ple_window;
6574
6575 vmx->ple_window = __grow_ple_window(old);
6576
6577 if (vmx->ple_window != old)
6578 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006579
6580 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006581}
6582
6583static void shrink_ple_window(struct kvm_vcpu *vcpu)
6584{
6585 struct vcpu_vmx *vmx = to_vmx(vcpu);
6586 int old = vmx->ple_window;
6587
6588 vmx->ple_window = __shrink_ple_window(old,
6589 ple_window_shrink, ple_window);
6590
6591 if (vmx->ple_window != old)
6592 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006593
6594 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006595}
6596
6597/*
6598 * ple_window_actual_max is computed to be one grow_ple_window() below
6599 * ple_window_max. (See __grow_ple_window for the reason.)
6600 * This prevents overflows, because ple_window_max is int.
6601 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6602 * this process.
6603 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6604 */
6605static void update_ple_window_actual_max(void)
6606{
6607 ple_window_actual_max =
6608 __shrink_ple_window(max(ple_window_max, ple_window),
6609 ple_window_grow, INT_MIN);
6610}
6611
Feng Wubf9f6ac2015-09-18 22:29:55 +08006612/*
6613 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6614 */
6615static void wakeup_handler(void)
6616{
6617 struct kvm_vcpu *vcpu;
6618 int cpu = smp_processor_id();
6619
6620 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6621 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6622 blocked_vcpu_list) {
6623 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6624
6625 if (pi_test_on(pi_desc) == 1)
6626 kvm_vcpu_kick(vcpu);
6627 }
6628 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6629}
6630
Tiejun Chenf2c76482014-10-28 10:14:47 +08006631static __init int hardware_setup(void)
6632{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006633 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006634
6635 rdmsrl_safe(MSR_EFER, &host_efer);
6636
6637 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6638 kvm_define_shared_msr(i, vmx_msr_index[i]);
6639
6640 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6641 if (!vmx_io_bitmap_a)
6642 return r;
6643
6644 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6645 if (!vmx_io_bitmap_b)
6646 goto out;
6647
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006648 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6649 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006650 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006651
6652 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6653 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006654 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006655
6656 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6657 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6658
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006659 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006660
6661 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6662
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006663 if (setup_vmcs_config(&vmcs_config) < 0) {
6664 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006665 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006666 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006667
6668 if (boot_cpu_has(X86_FEATURE_NX))
6669 kvm_enable_efer_bits(EFER_NX);
6670
Wanpeng Li2df19692017-03-23 05:30:08 -07006671 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6672 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006673 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006674
Tiejun Chenf2c76482014-10-28 10:14:47 +08006675 if (!cpu_has_vmx_shadow_vmcs())
6676 enable_shadow_vmcs = 0;
6677 if (enable_shadow_vmcs)
6678 init_vmcs_shadow_fields();
6679
6680 if (!cpu_has_vmx_ept() ||
6681 !cpu_has_vmx_ept_4levels()) {
6682 enable_ept = 0;
6683 enable_unrestricted_guest = 0;
6684 enable_ept_ad_bits = 0;
6685 }
6686
6687 if (!cpu_has_vmx_ept_ad_bits())
6688 enable_ept_ad_bits = 0;
6689
6690 if (!cpu_has_vmx_unrestricted_guest())
6691 enable_unrestricted_guest = 0;
6692
Paolo Bonziniad15a292015-01-30 16:18:49 +01006693 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006694 flexpriority_enabled = 0;
6695
Paolo Bonziniad15a292015-01-30 16:18:49 +01006696 /*
6697 * set_apic_access_page_addr() is used to reload apic access
6698 * page upon invalidation. No need to do anything if not
6699 * using the APIC_ACCESS_ADDR VMCS field.
6700 */
6701 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006702 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006703
6704 if (!cpu_has_vmx_tpr_shadow())
6705 kvm_x86_ops->update_cr8_intercept = NULL;
6706
6707 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6708 kvm_disable_largepages();
6709
6710 if (!cpu_has_vmx_ple())
6711 ple_gap = 0;
6712
6713 if (!cpu_has_vmx_apicv())
6714 enable_apicv = 0;
6715
Haozhong Zhang64903d62015-10-20 15:39:09 +08006716 if (cpu_has_vmx_tsc_scaling()) {
6717 kvm_has_tsc_control = true;
6718 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6719 kvm_tsc_scaling_ratio_frac_bits = 48;
6720 }
6721
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006722 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6723
Tiejun Chenbaa03522014-12-23 16:21:11 +08006724 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006725 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006726 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6727 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006728 0ull, VMX_EPT_EXECUTABLE_MASK,
6729 cpu_has_vmx_ept_execute_only() ?
6730 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006731 ept_set_mmio_spte_mask();
6732 kvm_enable_tdp();
6733 } else
6734 kvm_disable_tdp();
6735
6736 update_ple_window_actual_max();
6737
Kai Huang843e4332015-01-28 10:54:28 +08006738 /*
6739 * Only enable PML when hardware supports PML feature, and both EPT
6740 * and EPT A/D bit features are enabled -- PML depends on them to work.
6741 */
6742 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6743 enable_pml = 0;
6744
6745 if (!enable_pml) {
6746 kvm_x86_ops->slot_enable_log_dirty = NULL;
6747 kvm_x86_ops->slot_disable_log_dirty = NULL;
6748 kvm_x86_ops->flush_log_dirty = NULL;
6749 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6750 }
6751
Yunhong Jiang64672c92016-06-13 14:19:59 -07006752 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6753 u64 vmx_msr;
6754
6755 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6756 cpu_preemption_timer_multi =
6757 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6758 } else {
6759 kvm_x86_ops->set_hv_timer = NULL;
6760 kvm_x86_ops->cancel_hv_timer = NULL;
6761 }
6762
Feng Wubf9f6ac2015-09-18 22:29:55 +08006763 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6764
Ashok Rajc45dcc72016-06-22 14:59:56 +08006765 kvm_mce_cap_supported |= MCG_LMCE_P;
6766
Tiejun Chenf2c76482014-10-28 10:14:47 +08006767 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006768
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006769out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006770 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006771out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006772 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006773out1:
6774 free_page((unsigned long)vmx_io_bitmap_b);
6775out:
6776 free_page((unsigned long)vmx_io_bitmap_a);
6777
6778 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006779}
6780
6781static __exit void hardware_unsetup(void)
6782{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006783 free_page((unsigned long)vmx_io_bitmap_b);
6784 free_page((unsigned long)vmx_io_bitmap_a);
6785 free_page((unsigned long)vmx_vmwrite_bitmap);
6786 free_page((unsigned long)vmx_vmread_bitmap);
6787
Tiejun Chenf2c76482014-10-28 10:14:47 +08006788 free_kvm_area();
6789}
6790
Avi Kivity6aa8b732006-12-10 02:21:36 -08006791/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006792 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6793 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6794 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006795static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006796{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006797 if (ple_gap)
6798 grow_ple_window(vcpu);
6799
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006800 skip_emulated_instruction(vcpu);
6801 kvm_vcpu_on_spin(vcpu);
6802
6803 return 1;
6804}
6805
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006806static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006807{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006808 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006809 return 1;
6810}
6811
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006812static int handle_mwait(struct kvm_vcpu *vcpu)
6813{
6814 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6815 return handle_nop(vcpu);
6816}
6817
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006818static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6819{
6820 return 1;
6821}
6822
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006823static int handle_monitor(struct kvm_vcpu *vcpu)
6824{
6825 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6826 return handle_nop(vcpu);
6827}
6828
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006829/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006830 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6831 * set the success or error code of an emulated VMX instruction, as specified
6832 * by Vol 2B, VMX Instruction Reference, "Conventions".
6833 */
6834static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6835{
6836 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6837 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6838 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6839}
6840
6841static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6842{
6843 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6844 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6845 X86_EFLAGS_SF | X86_EFLAGS_OF))
6846 | X86_EFLAGS_CF);
6847}
6848
Abel Gordon145c28d2013-04-18 14:36:55 +03006849static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006850 u32 vm_instruction_error)
6851{
6852 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6853 /*
6854 * failValid writes the error number to the current VMCS, which
6855 * can't be done there isn't a current VMCS.
6856 */
6857 nested_vmx_failInvalid(vcpu);
6858 return;
6859 }
6860 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6861 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6862 X86_EFLAGS_SF | X86_EFLAGS_OF))
6863 | X86_EFLAGS_ZF);
6864 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6865 /*
6866 * We don't need to force a shadow sync because
6867 * VM_INSTRUCTION_ERROR is not shadowed
6868 */
6869}
Abel Gordon145c28d2013-04-18 14:36:55 +03006870
Wincy Vanff651cb2014-12-11 08:52:58 +03006871static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6872{
6873 /* TODO: not to reset guest simply here. */
6874 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006875 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006876}
6877
Jan Kiszkaf4124502014-03-07 20:03:13 +01006878static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6879{
6880 struct vcpu_vmx *vmx =
6881 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6882
6883 vmx->nested.preemption_timer_expired = true;
6884 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6885 kvm_vcpu_kick(&vmx->vcpu);
6886
6887 return HRTIMER_NORESTART;
6888}
6889
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006890/*
Bandan Das19677e32014-05-06 02:19:15 -04006891 * Decode the memory-address operand of a vmx instruction, as recorded on an
6892 * exit caused by such an instruction (run by a guest hypervisor).
6893 * On success, returns 0. When the operand is invalid, returns 1 and throws
6894 * #UD or #GP.
6895 */
6896static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6897 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006898 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006899{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006900 gva_t off;
6901 bool exn;
6902 struct kvm_segment s;
6903
Bandan Das19677e32014-05-06 02:19:15 -04006904 /*
6905 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6906 * Execution", on an exit, vmx_instruction_info holds most of the
6907 * addressing components of the operand. Only the displacement part
6908 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6909 * For how an actual address is calculated from all these components,
6910 * refer to Vol. 1, "Operand Addressing".
6911 */
6912 int scaling = vmx_instruction_info & 3;
6913 int addr_size = (vmx_instruction_info >> 7) & 7;
6914 bool is_reg = vmx_instruction_info & (1u << 10);
6915 int seg_reg = (vmx_instruction_info >> 15) & 7;
6916 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6917 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6918 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6919 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6920
6921 if (is_reg) {
6922 kvm_queue_exception(vcpu, UD_VECTOR);
6923 return 1;
6924 }
6925
6926 /* Addr = segment_base + offset */
6927 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006928 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006929 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006930 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006931 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006932 off += kvm_register_read(vcpu, index_reg)<<scaling;
6933 vmx_get_segment(vcpu, &s, seg_reg);
6934 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006935
6936 if (addr_size == 1) /* 32 bit */
6937 *ret &= 0xffffffff;
6938
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006939 /* Checks for #GP/#SS exceptions. */
6940 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006941 if (is_long_mode(vcpu)) {
6942 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6943 * non-canonical form. This is the only check on the memory
6944 * destination for long mode!
6945 */
6946 exn = is_noncanonical_address(*ret);
6947 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006948 /* Protected mode: apply checks for segment validity in the
6949 * following order:
6950 * - segment type check (#GP(0) may be thrown)
6951 * - usability check (#GP(0)/#SS(0))
6952 * - limit check (#GP(0)/#SS(0))
6953 */
6954 if (wr)
6955 /* #GP(0) if the destination operand is located in a
6956 * read-only data segment or any code segment.
6957 */
6958 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6959 else
6960 /* #GP(0) if the source operand is located in an
6961 * execute-only code segment
6962 */
6963 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006964 if (exn) {
6965 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6966 return 1;
6967 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006968 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6969 */
6970 exn = (s.unusable != 0);
6971 /* Protected mode: #GP(0)/#SS(0) if the memory
6972 * operand is outside the segment limit.
6973 */
6974 exn = exn || (off + sizeof(u64) > s.limit);
6975 }
6976 if (exn) {
6977 kvm_queue_exception_e(vcpu,
6978 seg_reg == VCPU_SREG_SS ?
6979 SS_VECTOR : GP_VECTOR,
6980 0);
6981 return 1;
6982 }
6983
Bandan Das19677e32014-05-06 02:19:15 -04006984 return 0;
6985}
6986
6987/*
Bandan Das3573e222014-05-06 02:19:16 -04006988 * This function performs the various checks including
6989 * - if it's 4KB aligned
6990 * - No bits beyond the physical address width are set
6991 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006992 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006993 */
Bandan Das4291b582014-05-06 02:19:18 -04006994static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6995 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006996{
6997 gva_t gva;
6998 gpa_t vmptr;
6999 struct x86_exception e;
7000 struct page *page;
7001 struct vcpu_vmx *vmx = to_vmx(vcpu);
7002 int maxphyaddr = cpuid_maxphyaddr(vcpu);
7003
7004 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007005 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007006 return 1;
7007
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007008 if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007009 kvm_inject_page_fault(vcpu, &e);
7010 return 1;
7011 }
7012
7013 switch (exit_reason) {
7014 case EXIT_REASON_VMON:
7015 /*
7016 * SDM 3: 24.11.5
7017 * The first 4 bytes of VMXON region contain the supported
7018 * VMCS revision identifier
7019 *
7020 * Note - IA32_VMX_BASIC[48] will never be 1
7021 * for the nested case;
7022 * which replaces physical address width with 32
7023 *
7024 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007025 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04007026 nested_vmx_failInvalid(vcpu);
7027 skip_emulated_instruction(vcpu);
7028 return 1;
7029 }
7030
7031 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007032 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04007033 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007034 skip_emulated_instruction(vcpu);
7035 return 1;
7036 }
7037 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04007038 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007039 nested_release_page_clean(page);
7040 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04007041 skip_emulated_instruction(vcpu);
7042 return 1;
7043 }
7044 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007045 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04007046 vmx->nested.vmxon_ptr = vmptr;
7047 break;
Bandan Das4291b582014-05-06 02:19:18 -04007048 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007049 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007050 nested_vmx_failValid(vcpu,
7051 VMXERR_VMCLEAR_INVALID_ADDRESS);
7052 skip_emulated_instruction(vcpu);
7053 return 1;
7054 }
Bandan Das3573e222014-05-06 02:19:16 -04007055
Bandan Das4291b582014-05-06 02:19:18 -04007056 if (vmptr == vmx->nested.vmxon_ptr) {
7057 nested_vmx_failValid(vcpu,
7058 VMXERR_VMCLEAR_VMXON_POINTER);
7059 skip_emulated_instruction(vcpu);
7060 return 1;
7061 }
7062 break;
7063 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007064 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007065 nested_vmx_failValid(vcpu,
7066 VMXERR_VMPTRLD_INVALID_ADDRESS);
7067 skip_emulated_instruction(vcpu);
7068 return 1;
7069 }
7070
7071 if (vmptr == vmx->nested.vmxon_ptr) {
7072 nested_vmx_failValid(vcpu,
7073 VMXERR_VMCLEAR_VMXON_POINTER);
7074 skip_emulated_instruction(vcpu);
7075 return 1;
7076 }
7077 break;
Bandan Das3573e222014-05-06 02:19:16 -04007078 default:
7079 return 1; /* shouldn't happen */
7080 }
7081
Bandan Das4291b582014-05-06 02:19:18 -04007082 if (vmpointer)
7083 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007084 return 0;
7085}
7086
7087/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007088 * Emulate the VMXON instruction.
7089 * Currently, we just remember that VMX is active, and do not save or even
7090 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7091 * do not currently need to store anything in that guest-allocated memory
7092 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7093 * argument is different from the VMXON pointer (which the spec says they do).
7094 */
7095static int handle_vmon(struct kvm_vcpu *vcpu)
7096{
7097 struct kvm_segment cs;
7098 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007099 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007100 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7101 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007102 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007103
7104 /* The Intel VMX Instruction Reference lists a bunch of bits that
7105 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7106 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7107 * Otherwise, we should fail with #UD. We test these now:
7108 */
7109 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7110 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7111 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7112 kvm_queue_exception(vcpu, UD_VECTOR);
7113 return 1;
7114 }
7115
7116 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7117 if (is_long_mode(vcpu) && !cs.l) {
7118 kvm_queue_exception(vcpu, UD_VECTOR);
7119 return 1;
7120 }
7121
7122 if (vmx_get_cpl(vcpu)) {
7123 kvm_inject_gp(vcpu, 0);
7124 return 1;
7125 }
Bandan Das3573e222014-05-06 02:19:16 -04007126
Bandan Das4291b582014-05-06 02:19:18 -04007127 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007128 return 1;
7129
Abel Gordon145c28d2013-04-18 14:36:55 +03007130 if (vmx->nested.vmxon) {
7131 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7132 skip_emulated_instruction(vcpu);
7133 return 1;
7134 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007135
Haozhong Zhang3b840802016-06-22 14:59:54 +08007136 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007137 != VMXON_NEEDED_FEATURES) {
7138 kvm_inject_gp(vcpu, 0);
7139 return 1;
7140 }
7141
Paolo Bonziniff546f92018-01-11 12:16:15 +01007142 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7143 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007144 goto out_vmcs02;
Jim Mattson46e24df2017-11-27 17:22:25 -06007145
David Matlack4f2777b2016-07-13 17:16:37 -07007146 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7147 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007148 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007149
Abel Gordon8de48832013-04-18 14:37:25 +03007150 if (enable_shadow_vmcs) {
7151 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007152 if (!shadow_vmcs)
7153 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007154 /* mark vmcs as shadow */
7155 shadow_vmcs->revision_id |= (1u << 31);
7156 /* init shadow vmcs */
7157 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007158 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007159 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007160
Jan Kiszkaf4124502014-03-07 20:03:13 +01007161 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007162 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007163 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7164
Roman Kagan020a90f2018-07-19 21:59:07 +03007165 vmx->nested.vpid02 = allocate_vpid();
7166
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007167 vmx->nested.vmxon = true;
7168
7169 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007170 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007171 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007172
7173out_shadow_vmcs:
7174 kfree(vmx->nested.cached_vmcs12);
7175
7176out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007177 free_loaded_vmcs(&vmx->nested.vmcs02);
7178
7179out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007180 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007181}
7182
7183/*
7184 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7185 * for running VMX instructions (except VMXON, whose prerequisites are
7186 * slightly different). It also specifies what exception to inject otherwise.
7187 */
7188static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7189{
7190 struct kvm_segment cs;
7191 struct vcpu_vmx *vmx = to_vmx(vcpu);
7192
7193 if (!vmx->nested.vmxon) {
7194 kvm_queue_exception(vcpu, UD_VECTOR);
7195 return 0;
7196 }
7197
7198 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7199 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7200 (is_long_mode(vcpu) && !cs.l)) {
7201 kvm_queue_exception(vcpu, UD_VECTOR);
7202 return 0;
7203 }
7204
7205 if (vmx_get_cpl(vcpu)) {
7206 kvm_inject_gp(vcpu, 0);
7207 return 0;
7208 }
7209
7210 return 1;
7211}
7212
Abel Gordone7953d72013-04-18 14:37:55 +03007213static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7214{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007215 if (vmx->nested.current_vmptr == -1ull)
7216 return;
7217
7218 /* current_vmptr and current_vmcs12 are always set/reset together */
7219 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7220 return;
7221
Abel Gordon012f83c2013-04-18 14:39:25 +03007222 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007223 /* copy to memory all shadowed fields in case
7224 they were modified */
7225 copy_shadow_to_vmcs12(vmx);
7226 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007227 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7228 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007229 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007230 }
Wincy Van705699a2015-02-03 23:58:17 +08007231 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007232
7233 /* Flush VMCS12 to guest memory */
7234 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7235 VMCS12_SIZE);
7236
Abel Gordone7953d72013-04-18 14:37:55 +03007237 kunmap(vmx->nested.current_vmcs12_page);
7238 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007239 vmx->nested.current_vmptr = -1ull;
7240 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007241}
7242
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007243/*
7244 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7245 * just stops using VMX.
7246 */
7247static void free_nested(struct vcpu_vmx *vmx)
7248{
7249 if (!vmx->nested.vmxon)
7250 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007251
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007252 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007253 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007254 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007255 if (enable_shadow_vmcs) {
7256 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7257 free_vmcs(vmx->vmcs01.shadow_vmcs);
7258 vmx->vmcs01.shadow_vmcs = NULL;
7259 }
David Matlack4f2777b2016-07-13 17:16:37 -07007260 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007261 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007262 if (vmx->nested.apic_access_page) {
7263 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007264 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007265 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007266 if (vmx->nested.virtual_apic_page) {
7267 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007268 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007269 }
Wincy Van705699a2015-02-03 23:58:17 +08007270 if (vmx->nested.pi_desc_page) {
7271 kunmap(vmx->nested.pi_desc_page);
7272 nested_release_page(vmx->nested.pi_desc_page);
7273 vmx->nested.pi_desc_page = NULL;
7274 vmx->nested.pi_desc = NULL;
7275 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007276
Jim Mattson46e24df2017-11-27 17:22:25 -06007277 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007278}
7279
7280/* Emulate the VMXOFF instruction */
7281static int handle_vmoff(struct kvm_vcpu *vcpu)
7282{
7283 if (!nested_vmx_check_permission(vcpu))
7284 return 1;
7285 free_nested(to_vmx(vcpu));
7286 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007287 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007288 return 1;
7289}
7290
Nadav Har'El27d6c862011-05-25 23:06:59 +03007291/* Emulate the VMCLEAR instruction */
7292static int handle_vmclear(struct kvm_vcpu *vcpu)
7293{
7294 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007295 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007296 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007297
7298 if (!nested_vmx_check_permission(vcpu))
7299 return 1;
7300
Bandan Das4291b582014-05-06 02:19:18 -04007301 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007302 return 1;
7303
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007304 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007305 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007306
Jim Mattson29deec42017-03-02 12:41:48 -08007307 kvm_vcpu_write_guest(vcpu,
7308 vmptr + offsetof(struct vmcs12, launch_state),
7309 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007310
Nadav Har'El27d6c862011-05-25 23:06:59 +03007311 skip_emulated_instruction(vcpu);
7312 nested_vmx_succeed(vcpu);
7313 return 1;
7314}
7315
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007316static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7317
7318/* Emulate the VMLAUNCH instruction */
7319static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7320{
7321 return nested_vmx_run(vcpu, true);
7322}
7323
7324/* Emulate the VMRESUME instruction */
7325static int handle_vmresume(struct kvm_vcpu *vcpu)
7326{
7327
7328 return nested_vmx_run(vcpu, false);
7329}
7330
Nadav Har'El49f705c2011-05-25 23:08:30 +03007331enum vmcs_field_type {
7332 VMCS_FIELD_TYPE_U16 = 0,
7333 VMCS_FIELD_TYPE_U64 = 1,
7334 VMCS_FIELD_TYPE_U32 = 2,
7335 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7336};
7337
7338static inline int vmcs_field_type(unsigned long field)
7339{
7340 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7341 return VMCS_FIELD_TYPE_U32;
7342 return (field >> 13) & 0x3 ;
7343}
7344
7345static inline int vmcs_field_readonly(unsigned long field)
7346{
7347 return (((field >> 10) & 0x3) == 1);
7348}
7349
7350/*
7351 * Read a vmcs12 field. Since these can have varying lengths and we return
7352 * one type, we chose the biggest type (u64) and zero-extend the return value
7353 * to that size. Note that the caller, handle_vmread, might need to use only
7354 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7355 * 64-bit fields are to be returned).
7356 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007357static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7358 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007359{
7360 short offset = vmcs_field_to_offset(field);
7361 char *p;
7362
7363 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007364 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007365
7366 p = ((char *)(get_vmcs12(vcpu))) + offset;
7367
7368 switch (vmcs_field_type(field)) {
7369 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7370 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007371 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007372 case VMCS_FIELD_TYPE_U16:
7373 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007374 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007375 case VMCS_FIELD_TYPE_U32:
7376 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007377 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007378 case VMCS_FIELD_TYPE_U64:
7379 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007380 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007381 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007382 WARN_ON(1);
7383 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007384 }
7385}
7386
Abel Gordon20b97fe2013-04-18 14:36:25 +03007387
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007388static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7389 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007390 short offset = vmcs_field_to_offset(field);
7391 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7392 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007393 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007394
7395 switch (vmcs_field_type(field)) {
7396 case VMCS_FIELD_TYPE_U16:
7397 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007398 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007399 case VMCS_FIELD_TYPE_U32:
7400 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007401 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007402 case VMCS_FIELD_TYPE_U64:
7403 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007404 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007405 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7406 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007407 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007408 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007409 WARN_ON(1);
7410 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007411 }
7412
7413}
7414
Abel Gordon16f5b902013-04-18 14:38:25 +03007415static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7416{
7417 int i;
7418 unsigned long field;
7419 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007420 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007421 const unsigned long *fields = shadow_read_write_fields;
7422 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007423
Jan Kiszka282da872014-10-08 18:05:39 +02007424 preempt_disable();
7425
Abel Gordon16f5b902013-04-18 14:38:25 +03007426 vmcs_load(shadow_vmcs);
7427
7428 for (i = 0; i < num_fields; i++) {
7429 field = fields[i];
7430 switch (vmcs_field_type(field)) {
7431 case VMCS_FIELD_TYPE_U16:
7432 field_value = vmcs_read16(field);
7433 break;
7434 case VMCS_FIELD_TYPE_U32:
7435 field_value = vmcs_read32(field);
7436 break;
7437 case VMCS_FIELD_TYPE_U64:
7438 field_value = vmcs_read64(field);
7439 break;
7440 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7441 field_value = vmcs_readl(field);
7442 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007443 default:
7444 WARN_ON(1);
7445 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007446 }
7447 vmcs12_write_any(&vmx->vcpu, field, field_value);
7448 }
7449
7450 vmcs_clear(shadow_vmcs);
7451 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007452
7453 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007454}
7455
Abel Gordonc3114422013-04-18 14:38:55 +03007456static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7457{
Mathias Krausec2bae892013-06-26 20:36:21 +02007458 const unsigned long *fields[] = {
7459 shadow_read_write_fields,
7460 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007461 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007462 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007463 max_shadow_read_write_fields,
7464 max_shadow_read_only_fields
7465 };
7466 int i, q;
7467 unsigned long field;
7468 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007469 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007470
7471 vmcs_load(shadow_vmcs);
7472
Mathias Krausec2bae892013-06-26 20:36:21 +02007473 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007474 for (i = 0; i < max_fields[q]; i++) {
7475 field = fields[q][i];
7476 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7477
7478 switch (vmcs_field_type(field)) {
7479 case VMCS_FIELD_TYPE_U16:
7480 vmcs_write16(field, (u16)field_value);
7481 break;
7482 case VMCS_FIELD_TYPE_U32:
7483 vmcs_write32(field, (u32)field_value);
7484 break;
7485 case VMCS_FIELD_TYPE_U64:
7486 vmcs_write64(field, (u64)field_value);
7487 break;
7488 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7489 vmcs_writel(field, (long)field_value);
7490 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007491 default:
7492 WARN_ON(1);
7493 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007494 }
7495 }
7496 }
7497
7498 vmcs_clear(shadow_vmcs);
7499 vmcs_load(vmx->loaded_vmcs->vmcs);
7500}
7501
Nadav Har'El49f705c2011-05-25 23:08:30 +03007502/*
7503 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7504 * used before) all generate the same failure when it is missing.
7505 */
7506static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7507{
7508 struct vcpu_vmx *vmx = to_vmx(vcpu);
7509 if (vmx->nested.current_vmptr == -1ull) {
7510 nested_vmx_failInvalid(vcpu);
7511 skip_emulated_instruction(vcpu);
7512 return 0;
7513 }
7514 return 1;
7515}
7516
7517static int handle_vmread(struct kvm_vcpu *vcpu)
7518{
7519 unsigned long field;
7520 u64 field_value;
7521 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7522 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7523 gva_t gva = 0;
7524
7525 if (!nested_vmx_check_permission(vcpu) ||
7526 !nested_vmx_check_vmcs12(vcpu))
7527 return 1;
7528
7529 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007530 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007531 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007532 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007533 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7534 skip_emulated_instruction(vcpu);
7535 return 1;
7536 }
7537 /*
7538 * Now copy part of this value to register or memory, as requested.
7539 * Note that the number of bits actually copied is 32 or 64 depending
7540 * on the guest's mode (32 or 64 bit), not on the given field's length.
7541 */
7542 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007543 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007544 field_value);
7545 } else {
7546 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007547 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007548 return 1;
7549 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007550 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7551 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007552 }
7553
7554 nested_vmx_succeed(vcpu);
7555 skip_emulated_instruction(vcpu);
7556 return 1;
7557}
7558
7559
7560static int handle_vmwrite(struct kvm_vcpu *vcpu)
7561{
7562 unsigned long field;
7563 gva_t gva;
7564 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7565 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007566 /* The value to write might be 32 or 64 bits, depending on L1's long
7567 * mode, and eventually we need to write that into a field of several
7568 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007569 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007570 * bits into the vmcs12 field.
7571 */
7572 u64 field_value = 0;
7573 struct x86_exception e;
7574
7575 if (!nested_vmx_check_permission(vcpu) ||
7576 !nested_vmx_check_vmcs12(vcpu))
7577 return 1;
7578
7579 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007580 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007581 (((vmx_instruction_info) >> 3) & 0xf));
7582 else {
7583 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007584 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007585 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007586 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7587 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007588 kvm_inject_page_fault(vcpu, &e);
7589 return 1;
7590 }
7591 }
7592
7593
Nadav Amit27e6fb52014-06-18 17:19:26 +03007594 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007595 if (vmcs_field_readonly(field)) {
7596 nested_vmx_failValid(vcpu,
7597 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7598 skip_emulated_instruction(vcpu);
7599 return 1;
7600 }
7601
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007602 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007603 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7604 skip_emulated_instruction(vcpu);
7605 return 1;
7606 }
7607
7608 nested_vmx_succeed(vcpu);
7609 skip_emulated_instruction(vcpu);
7610 return 1;
7611}
7612
Nadav Har'El63846662011-05-25 23:07:29 +03007613/* Emulate the VMPTRLD instruction */
7614static int handle_vmptrld(struct kvm_vcpu *vcpu)
7615{
7616 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007617 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007618
7619 if (!nested_vmx_check_permission(vcpu))
7620 return 1;
7621
Bandan Das4291b582014-05-06 02:19:18 -04007622 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007623 return 1;
7624
Nadav Har'El63846662011-05-25 23:07:29 +03007625 if (vmx->nested.current_vmptr != vmptr) {
7626 struct vmcs12 *new_vmcs12;
7627 struct page *page;
7628 page = nested_get_page(vcpu, vmptr);
7629 if (page == NULL) {
7630 nested_vmx_failInvalid(vcpu);
7631 skip_emulated_instruction(vcpu);
7632 return 1;
7633 }
7634 new_vmcs12 = kmap(page);
7635 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7636 kunmap(page);
7637 nested_release_page_clean(page);
7638 nested_vmx_failValid(vcpu,
7639 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7640 skip_emulated_instruction(vcpu);
7641 return 1;
7642 }
Nadav Har'El63846662011-05-25 23:07:29 +03007643
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007644 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007645 vmx->nested.current_vmptr = vmptr;
7646 vmx->nested.current_vmcs12 = new_vmcs12;
7647 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007648 /*
7649 * Load VMCS12 from guest memory since it is not already
7650 * cached.
7651 */
7652 memcpy(vmx->nested.cached_vmcs12,
7653 vmx->nested.current_vmcs12, VMCS12_SIZE);
7654
Abel Gordon012f83c2013-04-18 14:39:25 +03007655 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007656 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7657 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007658 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007659 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007660 vmx->nested.sync_shadow_vmcs = true;
7661 }
Nadav Har'El63846662011-05-25 23:07:29 +03007662 }
7663
7664 nested_vmx_succeed(vcpu);
7665 skip_emulated_instruction(vcpu);
7666 return 1;
7667}
7668
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007669/* Emulate the VMPTRST instruction */
7670static int handle_vmptrst(struct kvm_vcpu *vcpu)
7671{
7672 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7673 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7674 gva_t vmcs_gva;
7675 struct x86_exception e;
7676
7677 if (!nested_vmx_check_permission(vcpu))
7678 return 1;
7679
7680 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007681 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007682 return 1;
7683 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007684 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
7685 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7686 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007687 kvm_inject_page_fault(vcpu, &e);
7688 return 1;
7689 }
7690 nested_vmx_succeed(vcpu);
7691 skip_emulated_instruction(vcpu);
7692 return 1;
7693}
7694
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007695/* Emulate the INVEPT instruction */
7696static int handle_invept(struct kvm_vcpu *vcpu)
7697{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007698 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007699 u32 vmx_instruction_info, types;
7700 unsigned long type;
7701 gva_t gva;
7702 struct x86_exception e;
7703 struct {
7704 u64 eptp, gpa;
7705 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007706
Wincy Vanb9c237b2015-02-03 23:56:30 +08007707 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7708 SECONDARY_EXEC_ENABLE_EPT) ||
7709 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007710 kvm_queue_exception(vcpu, UD_VECTOR);
7711 return 1;
7712 }
7713
7714 if (!nested_vmx_check_permission(vcpu))
7715 return 1;
7716
7717 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7718 kvm_queue_exception(vcpu, UD_VECTOR);
7719 return 1;
7720 }
7721
7722 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007723 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007724
Wincy Vanb9c237b2015-02-03 23:56:30 +08007725 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007726
Jim Mattson85c856b2016-10-26 08:38:38 -07007727 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007728 nested_vmx_failValid(vcpu,
7729 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007730 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007731 return 1;
7732 }
7733
7734 /* According to the Intel VMX instruction reference, the memory
7735 * operand is read even if it isn't needed (e.g., for type==global)
7736 */
7737 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007738 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007739 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007740 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007741 kvm_inject_page_fault(vcpu, &e);
7742 return 1;
7743 }
7744
7745 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007746 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007747 /*
7748 * TODO: track mappings and invalidate
7749 * single context requests appropriately
7750 */
7751 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007752 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007753 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007754 nested_vmx_succeed(vcpu);
7755 break;
7756 default:
7757 BUG_ON(1);
7758 break;
7759 }
7760
7761 skip_emulated_instruction(vcpu);
7762 return 1;
7763}
7764
Petr Matouseka642fc32014-09-23 20:22:30 +02007765static int handle_invvpid(struct kvm_vcpu *vcpu)
7766{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007767 struct vcpu_vmx *vmx = to_vmx(vcpu);
7768 u32 vmx_instruction_info;
7769 unsigned long type, types;
7770 gva_t gva;
7771 struct x86_exception e;
7772 int vpid;
7773
7774 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7775 SECONDARY_EXEC_ENABLE_VPID) ||
7776 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7777 kvm_queue_exception(vcpu, UD_VECTOR);
7778 return 1;
7779 }
7780
7781 if (!nested_vmx_check_permission(vcpu))
7782 return 1;
7783
7784 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7785 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7786
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007787 types = (vmx->nested.nested_vmx_vpid_caps &
7788 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007789
Jim Mattson85c856b2016-10-26 08:38:38 -07007790 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007791 nested_vmx_failValid(vcpu,
7792 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007793 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007794 return 1;
7795 }
7796
7797 /* according to the intel vmx instruction reference, the memory
7798 * operand is read even if it isn't needed (e.g., for type==global)
7799 */
7800 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7801 vmx_instruction_info, false, &gva))
7802 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007803 if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007804 kvm_inject_page_fault(vcpu, &e);
7805 return 1;
7806 }
7807
7808 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007809 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007810 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007811 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7812 if (!vpid) {
7813 nested_vmx_failValid(vcpu,
7814 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7815 skip_emulated_instruction(vcpu);
7816 return 1;
7817 }
7818 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007819 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007820 break;
7821 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007822 WARN_ON_ONCE(1);
7823 skip_emulated_instruction(vcpu);
7824 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007825 }
7826
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007827 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7828 nested_vmx_succeed(vcpu);
7829
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007830 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007831 return 1;
7832}
7833
Kai Huang843e4332015-01-28 10:54:28 +08007834static int handle_pml_full(struct kvm_vcpu *vcpu)
7835{
7836 unsigned long exit_qualification;
7837
7838 trace_kvm_pml_full(vcpu->vcpu_id);
7839
7840 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7841
7842 /*
7843 * PML buffer FULL happened while executing iret from NMI,
7844 * "blocked by NMI" bit has to be set before next VM entry.
7845 */
7846 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7847 cpu_has_virtual_nmis() &&
7848 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7849 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7850 GUEST_INTR_STATE_NMI);
7851
7852 /*
7853 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7854 * here.., and there's no userspace involvement needed for PML.
7855 */
7856 return 1;
7857}
7858
Yunhong Jiang64672c92016-06-13 14:19:59 -07007859static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7860{
7861 kvm_lapic_expired_hv_timer(vcpu);
7862 return 1;
7863}
7864
Nadav Har'El0140cae2011-05-25 23:06:28 +03007865/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007866 * The exit handlers return 1 if the exit was handled fully and guest execution
7867 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7868 * to be done to userspace and return 0.
7869 */
Mathias Krause772e0312012-08-30 01:30:19 +02007870static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007871 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7872 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007873 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007874 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007875 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007876 [EXIT_REASON_CR_ACCESS] = handle_cr,
7877 [EXIT_REASON_DR_ACCESS] = handle_dr,
7878 [EXIT_REASON_CPUID] = handle_cpuid,
7879 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7880 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7881 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7882 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007883 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007884 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007885 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007886 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007887 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007888 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007889 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007890 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007891 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007892 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007893 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007894 [EXIT_REASON_VMOFF] = handle_vmoff,
7895 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007896 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7897 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007898 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007899 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007900 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007901 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007902 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007903 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007904 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7905 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007906 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007907 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007908 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007909 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007910 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007911 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007912 [EXIT_REASON_XSAVES] = handle_xsaves,
7913 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007914 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007915 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007916};
7917
7918static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007919 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007920
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007921static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7922 struct vmcs12 *vmcs12)
7923{
7924 unsigned long exit_qualification;
7925 gpa_t bitmap, last_bitmap;
7926 unsigned int port;
7927 int size;
7928 u8 b;
7929
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007930 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007931 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007932
7933 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7934
7935 port = exit_qualification >> 16;
7936 size = (exit_qualification & 7) + 1;
7937
7938 last_bitmap = (gpa_t)-1;
7939 b = -1;
7940
7941 while (size > 0) {
7942 if (port < 0x8000)
7943 bitmap = vmcs12->io_bitmap_a;
7944 else if (port < 0x10000)
7945 bitmap = vmcs12->io_bitmap_b;
7946 else
Joe Perches1d804d02015-03-30 16:46:09 -07007947 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007948 bitmap += (port & 0x7fff) / 8;
7949
7950 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007951 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007952 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007953 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007954 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007955
7956 port++;
7957 size--;
7958 last_bitmap = bitmap;
7959 }
7960
Joe Perches1d804d02015-03-30 16:46:09 -07007961 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007962}
7963
Nadav Har'El644d7112011-05-25 23:12:35 +03007964/*
7965 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7966 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7967 * disinterest in the current event (read or write a specific MSR) by using an
7968 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7969 */
7970static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7971 struct vmcs12 *vmcs12, u32 exit_reason)
7972{
7973 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7974 gpa_t bitmap;
7975
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007976 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007977 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007978
7979 /*
7980 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7981 * for the four combinations of read/write and low/high MSR numbers.
7982 * First we need to figure out which of the four to use:
7983 */
7984 bitmap = vmcs12->msr_bitmap;
7985 if (exit_reason == EXIT_REASON_MSR_WRITE)
7986 bitmap += 2048;
7987 if (msr_index >= 0xc0000000) {
7988 msr_index -= 0xc0000000;
7989 bitmap += 1024;
7990 }
7991
7992 /* Then read the msr_index'th bit from this bitmap: */
7993 if (msr_index < 1024*8) {
7994 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007995 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007996 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007997 return 1 & (b >> (msr_index & 7));
7998 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007999 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03008000}
8001
8002/*
8003 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
8004 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
8005 * intercept (via guest_host_mask etc.) the current event.
8006 */
8007static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8008 struct vmcs12 *vmcs12)
8009{
8010 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8011 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008012 int reg;
8013 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008014
8015 switch ((exit_qualification >> 4) & 3) {
8016 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008017 reg = (exit_qualification >> 8) & 15;
8018 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008019 switch (cr) {
8020 case 0:
8021 if (vmcs12->cr0_guest_host_mask &
8022 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008023 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008024 break;
8025 case 3:
8026 if ((vmcs12->cr3_target_count >= 1 &&
8027 vmcs12->cr3_target_value0 == val) ||
8028 (vmcs12->cr3_target_count >= 2 &&
8029 vmcs12->cr3_target_value1 == val) ||
8030 (vmcs12->cr3_target_count >= 3 &&
8031 vmcs12->cr3_target_value2 == val) ||
8032 (vmcs12->cr3_target_count >= 4 &&
8033 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008034 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008035 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008036 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008037 break;
8038 case 4:
8039 if (vmcs12->cr4_guest_host_mask &
8040 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008041 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008042 break;
8043 case 8:
8044 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008045 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008046 break;
8047 }
8048 break;
8049 case 2: /* clts */
8050 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8051 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008052 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008053 break;
8054 case 1: /* mov from cr */
8055 switch (cr) {
8056 case 3:
8057 if (vmcs12->cpu_based_vm_exec_control &
8058 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008059 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008060 break;
8061 case 8:
8062 if (vmcs12->cpu_based_vm_exec_control &
8063 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008064 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008065 break;
8066 }
8067 break;
8068 case 3: /* lmsw */
8069 /*
8070 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8071 * cr0. Other attempted changes are ignored, with no exit.
8072 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008073 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008074 if (vmcs12->cr0_guest_host_mask & 0xe &
8075 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008076 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008077 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8078 !(vmcs12->cr0_read_shadow & 0x1) &&
8079 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008080 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008081 break;
8082 }
Joe Perches1d804d02015-03-30 16:46:09 -07008083 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008084}
8085
8086/*
8087 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8088 * should handle it ourselves in L0 (and then continue L2). Only call this
8089 * when in is_guest_mode (L2).
8090 */
8091static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8092{
Nadav Har'El644d7112011-05-25 23:12:35 +03008093 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8094 struct vcpu_vmx *vmx = to_vmx(vcpu);
8095 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008096 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008097
Jan Kiszka542060e2014-01-04 18:47:21 +01008098 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8099 vmcs_readl(EXIT_QUALIFICATION),
8100 vmx->idt_vectoring_info,
8101 intr_info,
8102 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8103 KVM_ISA_VMX);
8104
David Matlackb7649e12017-08-01 14:00:40 -07008105 /*
8106 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008107 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8108 * Page). The CPU may write to these pages via their host
8109 * physical address while L2 is running, bypassing any
8110 * address-translation-based dirty tracking (e.g. EPT write
8111 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008112 *
8113 * Mark them dirty on every exit from L2 to prevent them from
8114 * getting out of sync with dirty tracking.
8115 */
8116 nested_mark_vmcs12_pages_dirty(vcpu);
8117
Nadav Har'El644d7112011-05-25 23:12:35 +03008118 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008119 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008120
8121 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008122 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8123 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008124 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008125 }
8126
8127 switch (exit_reason) {
8128 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008129 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008130 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008131 else if (is_page_fault(intr_info))
8132 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008133 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008134 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008135 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008136 else if (is_debug(intr_info) &&
8137 vcpu->guest_debug &
8138 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8139 return false;
8140 else if (is_breakpoint(intr_info) &&
8141 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8142 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008143 return vmcs12->exception_bitmap &
8144 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8145 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008146 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008147 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008148 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008149 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008150 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008151 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008152 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008153 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008154 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008155 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008156 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008157 case EXIT_REASON_HLT:
8158 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8159 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008160 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008161 case EXIT_REASON_INVLPG:
8162 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8163 case EXIT_REASON_RDPMC:
8164 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008165 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008166 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8167 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8168 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8169 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8170 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8171 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008172 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008173 /*
8174 * VMX instructions trap unconditionally. This allows L1 to
8175 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8176 */
Joe Perches1d804d02015-03-30 16:46:09 -07008177 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008178 case EXIT_REASON_CR_ACCESS:
8179 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8180 case EXIT_REASON_DR_ACCESS:
8181 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8182 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008183 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008184 case EXIT_REASON_MSR_READ:
8185 case EXIT_REASON_MSR_WRITE:
8186 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8187 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008188 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008189 case EXIT_REASON_MWAIT_INSTRUCTION:
8190 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008191 case EXIT_REASON_MONITOR_TRAP_FLAG:
8192 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008193 case EXIT_REASON_MONITOR_INSTRUCTION:
8194 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8195 case EXIT_REASON_PAUSE_INSTRUCTION:
8196 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8197 nested_cpu_has2(vmcs12,
8198 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8199 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008200 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008201 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008202 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008203 case EXIT_REASON_APIC_ACCESS:
8204 return nested_cpu_has2(vmcs12,
8205 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008206 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008207 case EXIT_REASON_EOI_INDUCED:
8208 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008209 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008210 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008211 /*
8212 * L0 always deals with the EPT violation. If nested EPT is
8213 * used, and the nested mmu code discovers that the address is
8214 * missing in the guest EPT table (EPT12), the EPT violation
8215 * will be injected with nested_ept_inject_page_fault()
8216 */
Joe Perches1d804d02015-03-30 16:46:09 -07008217 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008218 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008219 /*
8220 * L2 never uses directly L1's EPT, but rather L0's own EPT
8221 * table (shadow on EPT) or a merged EPT table that L0 built
8222 * (EPT on EPT). So any problems with the structure of the
8223 * table is L0's fault.
8224 */
Joe Perches1d804d02015-03-30 16:46:09 -07008225 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008226 case EXIT_REASON_WBINVD:
8227 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8228 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008229 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008230 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8231 /*
8232 * This should never happen, since it is not possible to
8233 * set XSS to a non-zero value---neither in L1 nor in L2.
8234 * If if it were, XSS would have to be checked against
8235 * the XSS exit bitmap in vmcs12.
8236 */
8237 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008238 case EXIT_REASON_PREEMPTION_TIMER:
8239 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008240 case EXIT_REASON_PML_FULL:
8241 /* We don't expose PML support to L1. */
8242 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008243 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008244 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008245 }
8246}
8247
Avi Kivity586f9602010-11-18 13:09:54 +02008248static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8249{
8250 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8251 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8252}
8253
Kai Huanga3eaa862015-11-04 13:46:05 +08008254static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008255{
Kai Huanga3eaa862015-11-04 13:46:05 +08008256 if (vmx->pml_pg) {
8257 __free_page(vmx->pml_pg);
8258 vmx->pml_pg = NULL;
8259 }
Kai Huang843e4332015-01-28 10:54:28 +08008260}
8261
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008262static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008263{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008264 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008265 u64 *pml_buf;
8266 u16 pml_idx;
8267
8268 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8269
8270 /* Do nothing if PML buffer is empty */
8271 if (pml_idx == (PML_ENTITY_NUM - 1))
8272 return;
8273
8274 /* PML index always points to next available PML buffer entity */
8275 if (pml_idx >= PML_ENTITY_NUM)
8276 pml_idx = 0;
8277 else
8278 pml_idx++;
8279
8280 pml_buf = page_address(vmx->pml_pg);
8281 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8282 u64 gpa;
8283
8284 gpa = pml_buf[pml_idx];
8285 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008286 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008287 }
8288
8289 /* reset PML index */
8290 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8291}
8292
8293/*
8294 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8295 * Called before reporting dirty_bitmap to userspace.
8296 */
8297static void kvm_flush_pml_buffers(struct kvm *kvm)
8298{
8299 int i;
8300 struct kvm_vcpu *vcpu;
8301 /*
8302 * We only need to kick vcpu out of guest mode here, as PML buffer
8303 * is flushed at beginning of all VMEXITs, and it's obvious that only
8304 * vcpus running in guest are possible to have unflushed GPAs in PML
8305 * buffer.
8306 */
8307 kvm_for_each_vcpu(i, vcpu, kvm)
8308 kvm_vcpu_kick(vcpu);
8309}
8310
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008311static void vmx_dump_sel(char *name, uint32_t sel)
8312{
8313 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008314 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008315 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8316 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8317 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8318}
8319
8320static void vmx_dump_dtsel(char *name, uint32_t limit)
8321{
8322 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8323 name, vmcs_read32(limit),
8324 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8325}
8326
8327static void dump_vmcs(void)
8328{
8329 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8330 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8331 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8332 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8333 u32 secondary_exec_control = 0;
8334 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008335 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008336 int i, n;
8337
8338 if (cpu_has_secondary_exec_ctrls())
8339 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8340
8341 pr_err("*** Guest State ***\n");
8342 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8343 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8344 vmcs_readl(CR0_GUEST_HOST_MASK));
8345 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8346 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8347 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8348 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8349 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8350 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008351 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8352 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8353 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8354 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008355 }
8356 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8357 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8358 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8359 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8360 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8361 vmcs_readl(GUEST_SYSENTER_ESP),
8362 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8363 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8364 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8365 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8366 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8367 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8368 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8369 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8370 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8371 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8372 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8373 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8374 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008375 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8376 efer, vmcs_read64(GUEST_IA32_PAT));
8377 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8378 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008379 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8380 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008381 pr_err("PerfGlobCtl = 0x%016llx\n",
8382 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008383 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008384 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008385 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8386 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8387 vmcs_read32(GUEST_ACTIVITY_STATE));
8388 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8389 pr_err("InterruptStatus = %04x\n",
8390 vmcs_read16(GUEST_INTR_STATUS));
8391
8392 pr_err("*** Host State ***\n");
8393 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8394 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8395 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8396 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8397 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8398 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8399 vmcs_read16(HOST_TR_SELECTOR));
8400 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8401 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8402 vmcs_readl(HOST_TR_BASE));
8403 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8404 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8405 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8406 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8407 vmcs_readl(HOST_CR4));
8408 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8409 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8410 vmcs_read32(HOST_IA32_SYSENTER_CS),
8411 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8412 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008413 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8414 vmcs_read64(HOST_IA32_EFER),
8415 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008416 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008417 pr_err("PerfGlobCtl = 0x%016llx\n",
8418 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008419
8420 pr_err("*** Control State ***\n");
8421 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8422 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8423 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8424 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8425 vmcs_read32(EXCEPTION_BITMAP),
8426 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8427 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8428 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8429 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8430 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8431 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8432 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8433 vmcs_read32(VM_EXIT_INTR_INFO),
8434 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8435 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8436 pr_err(" reason=%08x qualification=%016lx\n",
8437 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8438 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8439 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8440 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008441 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008442 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008443 pr_err("TSC Multiplier = 0x%016llx\n",
8444 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008445 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8446 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8447 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8448 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8449 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008450 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008451 n = vmcs_read32(CR3_TARGET_COUNT);
8452 for (i = 0; i + 1 < n; i += 4)
8453 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8454 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8455 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8456 if (i < n)
8457 pr_err("CR3 target%u=%016lx\n",
8458 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8459 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8460 pr_err("PLE Gap=%08x Window=%08x\n",
8461 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8462 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8463 pr_err("Virtual processor ID = 0x%04x\n",
8464 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8465}
8466
Avi Kivity6aa8b732006-12-10 02:21:36 -08008467/*
8468 * The guest has exited. See if we can fix it or if we need userspace
8469 * assistance.
8470 */
Avi Kivity851ba692009-08-24 11:10:17 +03008471static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008472{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008473 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008474 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008475 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008476
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008477 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8478
Kai Huang843e4332015-01-28 10:54:28 +08008479 /*
8480 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8481 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8482 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8483 * mode as if vcpus is in root mode, the PML buffer must has been
8484 * flushed already.
8485 */
8486 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008487 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008488
Mohammed Gamal80ced182009-09-01 12:48:18 +02008489 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008490 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008491 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008492
Nadav Har'El644d7112011-05-25 23:12:35 +03008493 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008494 nested_vmx_vmexit(vcpu, exit_reason,
8495 vmcs_read32(VM_EXIT_INTR_INFO),
8496 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008497 return 1;
8498 }
8499
Mohammed Gamal51207022010-05-31 22:40:54 +03008500 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008501 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008502 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8503 vcpu->run->fail_entry.hardware_entry_failure_reason
8504 = exit_reason;
8505 return 0;
8506 }
8507
Avi Kivity29bd8a72007-09-10 17:27:03 +03008508 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008509 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8510 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008511 = vmcs_read32(VM_INSTRUCTION_ERROR);
8512 return 0;
8513 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008514
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008515 /*
8516 * Note:
8517 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8518 * delivery event since it indicates guest is accessing MMIO.
8519 * The vm-exit can be triggered again after return to guest that
8520 * will cause infinite loop.
8521 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008522 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008523 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008524 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008525 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008526 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8527 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8528 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8529 vcpu->run->internal.ndata = 2;
8530 vcpu->run->internal.data[0] = vectoring_info;
8531 vcpu->run->internal.data[1] = exit_reason;
8532 return 0;
8533 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008534
Nadav Har'El644d7112011-05-25 23:12:35 +03008535 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8536 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008537 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008538 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008539 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008540 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008541 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008542 /*
8543 * This CPU don't support us in finding the end of an
8544 * NMI-blocked window if the guest runs with IRQs
8545 * disabled. So we pull the trigger after 1 s of
8546 * futile waiting, but inform the user about this.
8547 */
8548 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8549 "state on VCPU %d after 1 s timeout\n",
8550 __func__, vcpu->vcpu_id);
8551 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008552 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008553 }
8554
Avi Kivity6aa8b732006-12-10 02:21:36 -08008555 if (exit_reason < kvm_vmx_max_exit_handlers
8556 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008557 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008558 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008559 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8560 kvm_queue_exception(vcpu, UD_VECTOR);
8561 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008562 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008563}
8564
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008565/*
8566 * Software based L1D cache flush which is used when microcode providing
8567 * the cache control MSR is not loaded.
8568 *
8569 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8570 * flush it is required to read in 64 KiB because the replacement algorithm
8571 * is not exactly LRU. This could be sized at runtime via topology
8572 * information but as all relevant affected CPUs have 32KiB L1D cache size
8573 * there is no point in doing so.
8574 */
8575#define L1D_CACHE_ORDER 4
8576static void *vmx_l1d_flush_pages;
8577
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008578static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008579{
8580 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008581 bool always;
8582
8583 /*
8584 * If the mitigation mode is 'flush always', keep the flush bit
8585 * set, otherwise clear it. It gets set again either from
8586 * vcpu_run() or from one of the unsafe VMEXIT handlers.
8587 */
8588 always = vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS;
8589 vcpu->arch.l1tf_flush_l1d = always;
8590
8591 vcpu->stat.l1d_flush++;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008592
Paolo Bonziniacca8a72018-07-02 13:03:48 +02008593 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8594 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8595 return;
8596 }
8597
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008598 asm volatile(
8599 /* First ensure the pages are in the TLB */
8600 "xorl %%eax, %%eax\n"
8601 ".Lpopulate_tlb:\n\t"
8602 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
8603 "addl $4096, %%eax\n\t"
8604 "cmpl %%eax, %[size]\n\t"
8605 "jne .Lpopulate_tlb\n\t"
8606 "xorl %%eax, %%eax\n\t"
8607 "cpuid\n\t"
8608 /* Now fill the cache */
8609 "xorl %%eax, %%eax\n"
8610 ".Lfill_cache:\n"
8611 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
8612 "addl $64, %%eax\n\t"
8613 "cmpl %%eax, %[size]\n\t"
8614 "jne .Lfill_cache\n\t"
8615 "lfence\n"
8616 :: [empty_zp] "r" (vmx_l1d_flush_pages),
8617 [size] "r" (size)
8618 : "eax", "ebx", "ecx", "edx");
8619}
8620
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008621static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008622{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008623 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8624
8625 if (is_guest_mode(vcpu) &&
8626 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8627 return;
8628
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008629 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008630 vmcs_write32(TPR_THRESHOLD, 0);
8631 return;
8632 }
8633
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008634 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008635}
8636
Yang Zhang8d146952013-01-25 10:18:50 +08008637static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8638{
8639 u32 sec_exec_control;
8640
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008641 /* Postpone execution until vmcs01 is the current VMCS. */
8642 if (is_guest_mode(vcpu)) {
8643 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8644 return;
8645 }
8646
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008647 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008648 return;
8649
Paolo Bonzini35754c92015-07-29 12:05:37 +02008650 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008651 return;
8652
8653 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8654
8655 if (set) {
8656 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8657 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8658 } else {
8659 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8660 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008661 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008662 }
8663 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8664
Paolo Bonzini6236b782018-01-16 16:51:18 +01008665 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008666}
8667
Tang Chen38b99172014-09-24 15:57:54 +08008668static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8669{
8670 struct vcpu_vmx *vmx = to_vmx(vcpu);
8671
8672 /*
8673 * Currently we do not handle the nested case where L2 has an
8674 * APIC access page of its own; that page is still pinned.
8675 * Hence, we skip the case where the VCPU is in guest mode _and_
8676 * L1 prepared an APIC access page for L2.
8677 *
8678 * For the case where L1 and L2 share the same APIC access page
8679 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8680 * in the vmcs12), this function will only update either the vmcs01
8681 * or the vmcs02. If the former, the vmcs02 will be updated by
8682 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8683 * the next L2->L1 exit.
8684 */
8685 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008686 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008687 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008688 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008689 vmx_flush_tlb_ept_only(vcpu);
8690 }
Tang Chen38b99172014-09-24 15:57:54 +08008691}
8692
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008693static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008694{
8695 u16 status;
8696 u8 old;
8697
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008698 if (max_isr == -1)
8699 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008700
8701 status = vmcs_read16(GUEST_INTR_STATUS);
8702 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008703 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008704 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008705 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008706 vmcs_write16(GUEST_INTR_STATUS, status);
8707 }
8708}
8709
8710static void vmx_set_rvi(int vector)
8711{
8712 u16 status;
8713 u8 old;
8714
Wei Wang4114c272014-11-05 10:53:43 +08008715 if (vector == -1)
8716 vector = 0;
8717
Yang Zhangc7c9c562013-01-25 10:18:51 +08008718 status = vmcs_read16(GUEST_INTR_STATUS);
8719 old = (u8)status & 0xff;
8720 if ((u8)vector != old) {
8721 status &= ~0xff;
8722 status |= (u8)vector;
8723 vmcs_write16(GUEST_INTR_STATUS, status);
8724 }
8725}
8726
8727static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8728{
Wanpeng Li963fee12014-07-17 19:03:00 +08008729 if (!is_guest_mode(vcpu)) {
8730 vmx_set_rvi(max_irr);
8731 return;
8732 }
8733
Wei Wang4114c272014-11-05 10:53:43 +08008734 if (max_irr == -1)
8735 return;
8736
Wanpeng Li963fee12014-07-17 19:03:00 +08008737 /*
Wei Wang4114c272014-11-05 10:53:43 +08008738 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8739 * handles it.
8740 */
8741 if (nested_exit_on_intr(vcpu))
8742 return;
8743
8744 /*
8745 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008746 * is run without virtual interrupt delivery.
8747 */
8748 if (!kvm_event_needs_reinjection(vcpu) &&
8749 vmx_interrupt_allowed(vcpu)) {
8750 kvm_queue_interrupt(vcpu, max_irr, false);
8751 vmx_inject_irq(vcpu);
8752 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008753}
8754
Andrey Smetanin63086302015-11-10 15:36:32 +03008755static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008756{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008757 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008758 return;
8759
Yang Zhangc7c9c562013-01-25 10:18:51 +08008760 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8761 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8762 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8763 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8764}
8765
Avi Kivity51aa01d2010-07-20 14:31:20 +03008766static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008767{
Avi Kivity00eba012011-03-07 17:24:54 +02008768 u32 exit_intr_info;
8769
8770 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8771 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8772 return;
8773
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008774 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008775 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008776
8777 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008778 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008779 kvm_machine_check();
8780
Gleb Natapov20f65982009-05-11 13:35:55 +03008781 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008782 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008783 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008784 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008785 kvm_after_handle_nmi(&vmx->vcpu);
8786 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008787}
Gleb Natapov20f65982009-05-11 13:35:55 +03008788
Yang Zhanga547c6d2013-04-11 19:25:10 +08008789static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8790{
8791 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008792 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008793
8794 /*
8795 * If external interrupt exists, IF bit is set in rflags/eflags on the
8796 * interrupt stack frame, and interrupt will be enabled on a return
8797 * from interrupt handler.
8798 */
8799 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8800 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8801 unsigned int vector;
8802 unsigned long entry;
8803 gate_desc *desc;
8804 struct vcpu_vmx *vmx = to_vmx(vcpu);
8805#ifdef CONFIG_X86_64
8806 unsigned long tmp;
8807#endif
8808
8809 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8810 desc = (gate_desc *)vmx->host_idt_base + vector;
8811 entry = gate_offset(*desc);
8812 asm volatile(
8813#ifdef CONFIG_X86_64
8814 "mov %%" _ASM_SP ", %[sp]\n\t"
8815 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8816 "push $%c[ss]\n\t"
8817 "push %[sp]\n\t"
8818#endif
8819 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008820 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008821 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008822 :
8823#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008824 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008825#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008826 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008827 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008828 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008829 [ss]"i"(__KERNEL_DS),
8830 [cs]"i"(__KERNEL_CS)
8831 );
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008832 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008833 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008834}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05008835STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008836
Tom Lendackyb9655922018-05-10 22:06:39 +02008837static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008838{
Tom Lendackyb9655922018-05-10 22:06:39 +02008839 switch (index) {
8840 case MSR_IA32_SMBASE:
8841 /*
8842 * We cannot do SMM unless we can run the guest in big
8843 * real mode.
8844 */
8845 return enable_unrestricted_guest || emulate_invalid_guest_state;
8846 case MSR_AMD64_VIRT_SPEC_CTRL:
8847 /* This is AMD only. */
8848 return false;
8849 default:
8850 return true;
8851 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008852}
8853
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008854static bool vmx_mpx_supported(void)
8855{
8856 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8857 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8858}
8859
Wanpeng Li55412b22014-12-02 19:21:30 +08008860static bool vmx_xsaves_supported(void)
8861{
8862 return vmcs_config.cpu_based_2nd_exec_ctrl &
8863 SECONDARY_EXEC_XSAVES;
8864}
8865
Avi Kivity51aa01d2010-07-20 14:31:20 +03008866static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8867{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008868 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008869 bool unblock_nmi;
8870 u8 vector;
8871 bool idtv_info_valid;
8872
8873 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008874
Avi Kivitycf393f72008-07-01 16:20:21 +03008875 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008876 if (vmx->nmi_known_unmasked)
8877 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008878 /*
8879 * Can't use vmx->exit_intr_info since we're not sure what
8880 * the exit reason is.
8881 */
8882 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008883 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8884 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8885 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008886 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008887 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8888 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008889 * SDM 3: 23.2.2 (September 2008)
8890 * Bit 12 is undefined in any of the following cases:
8891 * If the VM exit sets the valid bit in the IDT-vectoring
8892 * information field.
8893 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008894 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008895 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8896 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008897 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8898 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008899 else
8900 vmx->nmi_known_unmasked =
8901 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8902 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008903 } else if (unlikely(vmx->soft_vnmi_blocked))
8904 vmx->vnmi_blocked_time +=
8905 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008906}
8907
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008908static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008909 u32 idt_vectoring_info,
8910 int instr_len_field,
8911 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008912{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008913 u8 vector;
8914 int type;
8915 bool idtv_info_valid;
8916
8917 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008918
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008919 vcpu->arch.nmi_injected = false;
8920 kvm_clear_exception_queue(vcpu);
8921 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008922
8923 if (!idtv_info_valid)
8924 return;
8925
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008926 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008927
Avi Kivity668f6122008-07-02 09:28:55 +03008928 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8929 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008930
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008931 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008932 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008933 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008934 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008935 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008936 * Clear bit "block by NMI" before VM entry if a NMI
8937 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008938 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008939 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008940 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008941 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008942 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008943 /* fall through */
8944 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008945 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008946 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008947 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008948 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008949 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008950 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008951 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008952 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008953 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008954 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008955 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008956 break;
8957 default:
8958 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008959 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008960}
8961
Avi Kivity83422e12010-07-20 14:43:23 +03008962static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8963{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008964 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008965 VM_EXIT_INSTRUCTION_LEN,
8966 IDT_VECTORING_ERROR_CODE);
8967}
8968
Avi Kivityb463a6f2010-07-20 15:06:17 +03008969static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8970{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008971 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008972 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8973 VM_ENTRY_INSTRUCTION_LEN,
8974 VM_ENTRY_EXCEPTION_ERROR_CODE);
8975
8976 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8977}
8978
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008979static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8980{
8981 int i, nr_msrs;
8982 struct perf_guest_switch_msr *msrs;
8983
8984 msrs = perf_guest_get_msrs(&nr_msrs);
8985
8986 if (!msrs)
8987 return;
8988
8989 for (i = 0; i < nr_msrs; i++)
8990 if (msrs[i].host == msrs[i].guest)
8991 clear_atomic_switch_msr(vmx, msrs[i].msr);
8992 else
8993 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilkc45ff812018-06-20 22:01:22 -04008994 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008995}
8996
Yunhong Jiang64672c92016-06-13 14:19:59 -07008997void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8998{
8999 struct vcpu_vmx *vmx = to_vmx(vcpu);
9000 u64 tscl;
9001 u32 delta_tsc;
9002
9003 if (vmx->hv_deadline_tsc == -1)
9004 return;
9005
9006 tscl = rdtsc();
9007 if (vmx->hv_deadline_tsc > tscl)
9008 /* sure to be 32 bit only because checked on set_hv_timer */
9009 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9010 cpu_preemption_timer_multi);
9011 else
9012 delta_tsc = 0;
9013
9014 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9015}
9016
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009017static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009018{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009019 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009020 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009021
9022 /* Record the guest's net vcpu time for enforced NMI injections. */
9023 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
9024 vmx->entry_time = ktime_get();
9025
9026 /* Don't enter VMX if guest state is invalid, let the exit handler
9027 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009028 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009029 return;
9030
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009031 if (vmx->ple_window_dirty) {
9032 vmx->ple_window_dirty = false;
9033 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9034 }
9035
Abel Gordon012f83c2013-04-18 14:39:25 +03009036 if (vmx->nested.sync_shadow_vmcs) {
9037 copy_vmcs12_to_shadow(vmx);
9038 vmx->nested.sync_shadow_vmcs = false;
9039 }
9040
Avi Kivity104f2262010-11-18 13:12:52 +02009041 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9042 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9043 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9044 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9045
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009046 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009047 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9048 vmcs_writel(HOST_CR4, cr4);
9049 vmx->host_state.vmcs_host_cr4 = cr4;
9050 }
9051
Avi Kivity104f2262010-11-18 13:12:52 +02009052 /* When single-stepping over STI and MOV SS, we must clear the
9053 * corresponding interruptibility bits in the guest state. Otherwise
9054 * vmentry fails as it then expects bit 14 (BS) in pending debug
9055 * exceptions being set, but that's not correct for the guest debugging
9056 * case. */
9057 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9058 vmx_set_interrupt_shadow(vcpu, 0);
9059
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009060 if (vmx->guest_pkru_valid)
9061 __write_pkru(vmx->guest_pkru);
9062
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009063 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009064 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009065
Yunhong Jiang64672c92016-06-13 14:19:59 -07009066 vmx_arm_hv_timer(vcpu);
9067
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009068 /*
9069 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9070 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9071 * is no need to worry about the conditional branch over the wrmsr
9072 * being speculatively taken.
9073 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009074 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009075
Nadav Har'Eld462b812011-05-24 15:26:10 +03009076 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009077
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02009078 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
9079 if (vcpu->arch.l1tf_flush_l1d)
9080 vmx_l1d_flush(vcpu);
9081 }
9082
Avi Kivity104f2262010-11-18 13:12:52 +02009083 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009084 /* Store host registers */
Avi Kivityb188c812012-09-16 15:10:58 +03009085 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9086 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9087 "push %%" _ASM_CX " \n\t"
9088 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009089 "je 1f \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009090 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009091 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009092 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009093 /* Reload cr2 if changed */
Avi Kivityb188c812012-09-16 15:10:58 +03009094 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9095 "mov %%cr2, %%" _ASM_DX " \n\t"
9096 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009097 "je 2f \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009098 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009099 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009100 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009101 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009102 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c812012-09-16 15:10:58 +03009103 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9104 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9105 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9106 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9107 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9108 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009109#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009110 "mov %c[r8](%0), %%r8 \n\t"
9111 "mov %c[r9](%0), %%r9 \n\t"
9112 "mov %c[r10](%0), %%r10 \n\t"
9113 "mov %c[r11](%0), %%r11 \n\t"
9114 "mov %c[r12](%0), %%r12 \n\t"
9115 "mov %c[r13](%0), %%r13 \n\t"
9116 "mov %c[r14](%0), %%r14 \n\t"
9117 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009118#endif
Avi Kivityb188c812012-09-16 15:10:58 +03009119 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009120
Avi Kivity6aa8b732006-12-10 02:21:36 -08009121 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009122 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009123 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009124 "jmp 2f \n\t"
9125 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9126 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009127 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c812012-09-16 15:10:58 +03009128 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009129 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009130 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009131 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9132 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9133 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9134 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9135 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9136 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9137 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009138#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009139 "mov %%r8, %c[r8](%0) \n\t"
9140 "mov %%r9, %c[r9](%0) \n\t"
9141 "mov %%r10, %c[r10](%0) \n\t"
9142 "mov %%r11, %c[r11](%0) \n\t"
9143 "mov %%r12, %c[r12](%0) \n\t"
9144 "mov %%r13, %c[r13](%0) \n\t"
9145 "mov %%r14, %c[r14](%0) \n\t"
9146 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009147 "xor %%r8d, %%r8d \n\t"
9148 "xor %%r9d, %%r9d \n\t"
9149 "xor %%r10d, %%r10d \n\t"
9150 "xor %%r11d, %%r11d \n\t"
9151 "xor %%r12d, %%r12d \n\t"
9152 "xor %%r13d, %%r13d \n\t"
9153 "xor %%r14d, %%r14d \n\t"
9154 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009155#endif
Avi Kivityb188c812012-09-16 15:10:58 +03009156 "mov %%cr2, %%" _ASM_AX " \n\t"
9157 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009158
Jim Mattson491c0ca2018-01-03 14:31:38 -08009159 "xor %%eax, %%eax \n\t"
9160 "xor %%ebx, %%ebx \n\t"
9161 "xor %%esi, %%esi \n\t"
9162 "xor %%edi, %%edi \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009163 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009164 ".pushsection .rodata \n\t"
9165 ".global vmx_return \n\t"
9166 "vmx_return: " _ASM_PTR " 2b \n\t"
9167 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009168 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009169 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009170 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009171 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009172 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9173 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9174 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9175 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9176 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9177 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9178 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009179#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009180 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9181 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9182 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9183 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9184 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9185 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9186 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9187 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009188#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009189 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9190 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009191 : "cc", "memory"
9192#ifdef CONFIG_X86_64
Avi Kivityb188c812012-09-16 15:10:58 +03009193 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009194 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c812012-09-16 15:10:58 +03009195#else
9196 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009197#endif
9198 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009199
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009200 /*
9201 * We do not use IBRS in the kernel. If this vCPU has used the
9202 * SPEC_CTRL MSR it may have left it on; save the value and
9203 * turn it off. This is much more efficient than blindly adding
9204 * it to the atomic save/restore list. Especially as the former
9205 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9206 *
9207 * For non-nested case:
9208 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9209 * save it.
9210 *
9211 * For nested case:
9212 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9213 * save it.
9214 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009215 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009216 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009217
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009218 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009219
David Woodhousec1ddd992018-01-12 11:11:27 +00009220 /* Eliminate branch target predictions from guest mode */
9221 vmexit_fill_RSB();
9222
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009223 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9224 if (debugctlmsr)
9225 update_debugctlmsr(debugctlmsr);
9226
Avi Kivityaa67f602012-08-01 16:48:03 +03009227#ifndef CONFIG_X86_64
9228 /*
9229 * The sysexit path does not restore ds/es, so we must set them to
9230 * a reasonable value ourselves.
9231 *
9232 * We can't defer this to vmx_load_host_state() since that function
9233 * may be executed in interrupt context, which saves and restore segments
9234 * around it, nullifying its effect.
9235 */
9236 loadsegment(ds, __USER_DS);
9237 loadsegment(es, __USER_DS);
9238#endif
9239
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009240 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009241 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009242 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009243 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009244 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009245 vcpu->arch.regs_dirty = 0;
9246
Avi Kivity1155f762007-11-22 11:30:47 +02009247 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9248
Nadav Har'Eld462b812011-05-24 15:26:10 +03009249 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009250
Avi Kivity51aa01d2010-07-20 14:31:20 +03009251 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009252
Gleb Natapove0b890d2013-09-25 12:51:33 +03009253 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009254 * eager fpu is enabled if PKEY is supported and CR4 is switched
9255 * back on host, so it is safe to read guest PKRU from current
9256 * XSAVE.
9257 */
9258 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9259 vmx->guest_pkru = __read_pkru();
9260 if (vmx->guest_pkru != vmx->host_pkru) {
9261 vmx->guest_pkru_valid = true;
9262 __write_pkru(vmx->host_pkru);
9263 } else
9264 vmx->guest_pkru_valid = false;
9265 }
9266
9267 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009268 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9269 * we did not inject a still-pending event to L1 now because of
9270 * nested_run_pending, we need to re-enable this bit.
9271 */
9272 if (vmx->nested.nested_run_pending)
9273 kvm_make_request(KVM_REQ_EVENT, vcpu);
9274
9275 vmx->nested.nested_run_pending = 0;
9276
Avi Kivity51aa01d2010-07-20 14:31:20 +03009277 vmx_complete_atomic_exit(vmx);
9278 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009279 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009280}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05009281STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009282
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009283static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9284{
9285 struct vcpu_vmx *vmx = to_vmx(vcpu);
9286 int cpu;
9287
9288 if (vmx->loaded_vmcs == &vmx->vmcs01)
9289 return;
9290
9291 cpu = get_cpu();
9292 vmx->loaded_vmcs = &vmx->vmcs01;
9293 vmx_vcpu_put(vcpu);
9294 vmx_vcpu_load(vcpu, cpu);
9295 vcpu->cpu = cpu;
9296 put_cpu();
9297}
9298
Jim Mattson2f1fe812016-07-08 15:36:06 -07009299/*
9300 * Ensure that the current vmcs of the logical processor is the
9301 * vmcs01 of the vcpu before calling free_nested().
9302 */
9303static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9304{
9305 struct vcpu_vmx *vmx = to_vmx(vcpu);
9306 int r;
9307
9308 r = vcpu_load(vcpu);
9309 BUG_ON(r);
9310 vmx_load_vmcs01(vcpu);
9311 free_nested(vmx);
9312 vcpu_put(vcpu);
9313}
9314
Avi Kivity6aa8b732006-12-10 02:21:36 -08009315static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9316{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009317 struct vcpu_vmx *vmx = to_vmx(vcpu);
9318
Kai Huang843e4332015-01-28 10:54:28 +08009319 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009320 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009321 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009322 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009323 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009324 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009325 kfree(vmx->guest_msrs);
9326 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009327 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009328}
9329
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009330static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009331{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009332 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009333 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009334 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009335 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009336
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009337 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009338 return ERR_PTR(-ENOMEM);
9339
Wanpeng Li991e7a02015-09-16 17:30:05 +08009340 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009341
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009342 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9343 if (err)
9344 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009345
Peter Feiner4e595162016-07-07 14:49:58 -07009346 err = -ENOMEM;
9347
9348 /*
9349 * If PML is turned on, failure on enabling PML just results in failure
9350 * of creating the vcpu, therefore we can simplify PML logic (by
9351 * avoiding dealing with cases, such as enabling PML partially on vcpus
9352 * for the guest, etc.
9353 */
9354 if (enable_pml) {
9355 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9356 if (!vmx->pml_pg)
9357 goto uninit_vcpu;
9358 }
9359
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009360 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009361 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9362 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009363
Peter Feiner4e595162016-07-07 14:49:58 -07009364 if (!vmx->guest_msrs)
9365 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009366
Nadav Har'Eld462b812011-05-24 15:26:10 +03009367 if (!vmm_exclusive)
9368 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009369 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009370 if (!vmm_exclusive)
9371 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009372 if (err < 0)
9373 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009374
Paolo Bonzini6236b782018-01-16 16:51:18 +01009375 msr_bitmap = vmx->vmcs01.msr_bitmap;
9376 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9377 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9378 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9379 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9380 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9381 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9382 vmx->msr_bitmap_mode = 0;
9383
Paolo Bonziniff546f92018-01-11 12:16:15 +01009384 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009385 cpu = get_cpu();
9386 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009387 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009388 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009389 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009390 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009391 if (err)
9392 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009393 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009394 err = alloc_apic_access_page(kvm);
9395 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009396 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009397 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009398
Sheng Yangb927a3c2009-07-21 10:42:48 +08009399 if (enable_ept) {
9400 if (!kvm->arch.ept_identity_map_addr)
9401 kvm->arch.ept_identity_map_addr =
9402 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009403 err = init_rmode_identity_map(kvm);
9404 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009405 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009406 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009407
Roman Kagan020a90f2018-07-19 21:59:07 +03009408 if (nested)
Wincy Vanb9c237b2015-02-03 23:56:30 +08009409 nested_vmx_setup_ctls_msrs(vmx);
9410
Wincy Van705699a2015-02-03 23:58:17 +08009411 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009412 vmx->nested.current_vmptr = -1ull;
9413 vmx->nested.current_vmcs12 = NULL;
9414
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009415 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9416
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009417 /*
9418 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9419 * or POSTED_INTR_WAKEUP_VECTOR.
9420 */
9421 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9422 vmx->pi_desc.sn = 1;
9423
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009424 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009425
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009426free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009427 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009428free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009429 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009430free_pml:
9431 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009432uninit_vcpu:
9433 kvm_vcpu_uninit(&vmx->vcpu);
9434free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009435 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009436 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009437 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009438}
9439
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009440#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
9441
9442static int vmx_vm_init(struct kvm *kvm)
9443{
9444 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
9445 if (nosmt) {
9446 pr_err(L1TF_MSG);
9447 return -EOPNOTSUPP;
9448 }
9449 pr_warn(L1TF_MSG);
9450 }
9451 return 0;
9452}
9453
Yang, Sheng002c7f72007-07-31 14:23:01 +03009454static void __init vmx_check_processor_compat(void *rtn)
9455{
9456 struct vmcs_config vmcs_conf;
9457
9458 *(int *)rtn = 0;
9459 if (setup_vmcs_config(&vmcs_conf) < 0)
9460 *(int *)rtn = -EIO;
9461 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9462 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9463 smp_processor_id());
9464 *(int *)rtn = -EIO;
9465 }
9466}
9467
Sheng Yang67253af2008-04-25 10:20:22 +08009468static int get_ept_level(void)
9469{
9470 return VMX_EPT_DEFAULT_GAW + 1;
9471}
9472
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009473static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009474{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009475 u8 cache;
9476 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009477
Sheng Yang522c68c2009-04-27 20:35:43 +08009478 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009479 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009480 * 2. EPT with VT-d:
9481 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009482 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009483 * b. VT-d with snooping control feature: snooping control feature of
9484 * VT-d engine can guarantee the cache correctness. Just set it
9485 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009486 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009487 * consistent with host MTRR
9488 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009489 if (is_mmio) {
9490 cache = MTRR_TYPE_UNCACHABLE;
9491 goto exit;
9492 }
9493
9494 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009495 ipat = VMX_EPT_IPAT_BIT;
9496 cache = MTRR_TYPE_WRBACK;
9497 goto exit;
9498 }
9499
9500 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9501 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009502 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009503 cache = MTRR_TYPE_WRBACK;
9504 else
9505 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009506 goto exit;
9507 }
9508
Xiao Guangrongff536042015-06-15 16:55:22 +08009509 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009510
9511exit:
9512 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009513}
9514
Sheng Yang17cc3932010-01-05 19:02:27 +08009515static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009516{
Sheng Yang878403b2010-01-05 19:02:29 +08009517 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9518 return PT_DIRECTORY_LEVEL;
9519 else
9520 /* For shadow and EPT supported 1GB page */
9521 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009522}
9523
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009524static void vmcs_set_secondary_exec_control(u32 new_ctl)
9525{
9526 /*
9527 * These bits in the secondary execution controls field
9528 * are dynamic, the others are mostly based on the hypervisor
9529 * architecture and the guest's CPUID. Do not touch the
9530 * dynamic bits.
9531 */
9532 u32 mask =
9533 SECONDARY_EXEC_SHADOW_VMCS |
9534 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9535 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9536
9537 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9538
9539 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9540 (new_ctl & ~mask) | (cur_ctl & mask));
9541}
9542
Sheng Yang0e851882009-12-18 16:48:46 +08009543static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9544{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009545 struct kvm_cpuid_entry2 *best;
9546 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009547 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009548
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009549 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009550 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9551 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009552 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009553
Paolo Bonzini8b972652015-09-15 17:34:42 +02009554 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009555 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009556 vmx->nested.nested_vmx_secondary_ctls_high |=
9557 SECONDARY_EXEC_RDTSCP;
9558 else
9559 vmx->nested.nested_vmx_secondary_ctls_high &=
9560 ~SECONDARY_EXEC_RDTSCP;
9561 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009562 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009563
Mao, Junjiead756a12012-07-02 01:18:48 +00009564 /* Exposing INVPCID only when PCID is exposed */
9565 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9566 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009567 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9568 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009569 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009570
Mao, Junjiead756a12012-07-02 01:18:48 +00009571 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009572 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009573 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009574
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009575 if (cpu_has_secondary_exec_ctrls())
9576 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009577
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009578 if (nested_vmx_allowed(vcpu))
9579 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9580 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9581 else
9582 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9583 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009584}
9585
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009586static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9587{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009588 if (func == 1 && nested)
9589 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009590}
9591
Yang Zhang25d92082013-08-06 12:00:32 +03009592static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9593 struct x86_exception *fault)
9594{
Jan Kiszka533558b2014-01-04 18:47:20 +01009595 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9596 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009597
9598 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009599 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009600 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009601 exit_reason = EXIT_REASON_EPT_VIOLATION;
9602 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009603 vmcs12->guest_physical_address = fault->address;
9604}
9605
Nadav Har'El155a97a2013-08-05 11:07:16 +03009606/* Callbacks for nested_ept_init_mmu_context: */
9607
9608static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9609{
9610 /* return the page table to be shadowed - in our case, EPT12 */
9611 return get_vmcs12(vcpu)->ept_pointer;
9612}
9613
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009614static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009615{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009616 WARN_ON(mmu_is_nested(vcpu));
9617 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009618 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9619 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009620 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9621 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9622 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9623
9624 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009625}
9626
9627static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9628{
9629 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9630}
9631
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009632static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9633 u16 error_code)
9634{
9635 bool inequality, bit;
9636
9637 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9638 inequality =
9639 (error_code & vmcs12->page_fault_error_code_mask) !=
9640 vmcs12->page_fault_error_code_match;
9641 return inequality ^ bit;
9642}
9643
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009644static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9645 struct x86_exception *fault)
9646{
9647 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9648
9649 WARN_ON(!is_guest_mode(vcpu));
9650
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009651 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009652 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9653 vmcs_read32(VM_EXIT_INTR_INFO),
9654 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009655 else
9656 kvm_inject_page_fault(vcpu, fault);
9657}
9658
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009659static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9660 struct vmcs12 *vmcs12)
9661{
9662 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009663 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009664
9665 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009666 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9667 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009668 return false;
9669
9670 /*
9671 * Translate L1 physical address to host physical
9672 * address for vmcs02. Keep the page pinned, so this
9673 * physical address remains valid. We keep a reference
9674 * to it so we can release it later.
9675 */
9676 if (vmx->nested.apic_access_page) /* shouldn't happen */
9677 nested_release_page(vmx->nested.apic_access_page);
9678 vmx->nested.apic_access_page =
9679 nested_get_page(vcpu, vmcs12->apic_access_addr);
9680 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009681
9682 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009683 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9684 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009685 return false;
9686
9687 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9688 nested_release_page(vmx->nested.virtual_apic_page);
9689 vmx->nested.virtual_apic_page =
9690 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9691
9692 /*
9693 * Failing the vm entry is _not_ what the processor does
9694 * but it's basically the only possibility we have.
9695 * We could still enter the guest if CR8 load exits are
9696 * enabled, CR8 store exits are enabled, and virtualize APIC
9697 * access is disabled; in this case the processor would never
9698 * use the TPR shadow and we could simply clear the bit from
9699 * the execution control. But such a configuration is useless,
9700 * so let's keep the code simple.
9701 */
9702 if (!vmx->nested.virtual_apic_page)
9703 return false;
9704 }
9705
Wincy Van705699a2015-02-03 23:58:17 +08009706 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009707 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9708 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009709 return false;
9710
9711 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9712 kunmap(vmx->nested.pi_desc_page);
9713 nested_release_page(vmx->nested.pi_desc_page);
9714 }
9715 vmx->nested.pi_desc_page =
9716 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9717 if (!vmx->nested.pi_desc_page)
9718 return false;
9719
9720 vmx->nested.pi_desc =
9721 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9722 if (!vmx->nested.pi_desc) {
9723 nested_release_page_clean(vmx->nested.pi_desc_page);
9724 return false;
9725 }
9726 vmx->nested.pi_desc =
9727 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9728 (unsigned long)(vmcs12->posted_intr_desc_addr &
9729 (PAGE_SIZE - 1)));
9730 }
9731
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009732 return true;
9733}
9734
Jan Kiszkaf4124502014-03-07 20:03:13 +01009735static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9736{
9737 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9738 struct vcpu_vmx *vmx = to_vmx(vcpu);
9739
9740 if (vcpu->arch.virtual_tsc_khz == 0)
9741 return;
9742
9743 /* Make sure short timeouts reliably trigger an immediate vmexit.
9744 * hrtimer_start does not guarantee this. */
9745 if (preemption_timeout <= 1) {
9746 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9747 return;
9748 }
9749
9750 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9751 preemption_timeout *= 1000000;
9752 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9753 hrtimer_start(&vmx->nested.preemption_timer,
9754 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9755}
9756
Wincy Van3af18d92015-02-03 23:49:31 +08009757static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9758 struct vmcs12 *vmcs12)
9759{
9760 int maxphyaddr;
9761 u64 addr;
9762
9763 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9764 return 0;
9765
9766 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9767 WARN_ON(1);
9768 return -EINVAL;
9769 }
9770 maxphyaddr = cpuid_maxphyaddr(vcpu);
9771
9772 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9773 ((addr + PAGE_SIZE) >> maxphyaddr))
9774 return -EINVAL;
9775
9776 return 0;
9777}
9778
9779/*
9780 * Merge L0's and L1's MSR bitmap, return false to indicate that
9781 * we do not use the hardware.
9782 */
9783static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9784 struct vmcs12 *vmcs12)
9785{
Wincy Van82f0dd42015-02-03 23:57:18 +08009786 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009787 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009788 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009789 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009790 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009791 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009792 *
9793 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9794 * ensures that we do not accidentally generate an L02 MSR bitmap
9795 * from the L12 MSR bitmap that is too permissive.
9796 * 2. That L1 or L2s have actually used the MSR. This avoids
9797 * unnecessarily merging of the bitmap if the MSR is unused. This
9798 * works properly because we only update the L01 MSR bitmap lazily.
9799 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9800 * updated to reflect this when L1 (or its L2s) actually write to
9801 * the MSR.
9802 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009803 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9804 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009805
Ashok Raj70131292018-02-01 22:59:43 +01009806 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009807 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009808 return false;
9809
9810 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009811 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009812 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009813 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009814
Radim Krčmářd048c092016-08-08 20:16:22 +02009815 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9816
Wincy Vanf2b93282015-02-03 23:56:03 +08009817 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009818 if (nested_cpu_has_apic_reg_virt(vmcs12))
9819 for (msr = 0x800; msr <= 0x8ff; msr++)
9820 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009821 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009822 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009823
9824 nested_vmx_disable_intercept_for_msr(
9825 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009826 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9827 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009828
Wincy Van608406e2015-02-03 23:57:51 +08009829 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009830 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009831 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009832 APIC_BASE_MSR + (APIC_EOI >> 4),
9833 MSR_TYPE_W);
9834 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009835 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009836 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9837 MSR_TYPE_W);
9838 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009839 }
Ashok Raj70131292018-02-01 22:59:43 +01009840
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009841 if (spec_ctrl)
9842 nested_vmx_disable_intercept_for_msr(
9843 msr_bitmap_l1, msr_bitmap_l0,
9844 MSR_IA32_SPEC_CTRL,
9845 MSR_TYPE_R | MSR_TYPE_W);
9846
Ashok Raj70131292018-02-01 22:59:43 +01009847 if (pred_cmd)
9848 nested_vmx_disable_intercept_for_msr(
9849 msr_bitmap_l1, msr_bitmap_l0,
9850 MSR_IA32_PRED_CMD,
9851 MSR_TYPE_W);
9852
Wincy Vanf2b93282015-02-03 23:56:03 +08009853 kunmap(page);
9854 nested_release_page_clean(page);
9855
9856 return true;
9857}
9858
9859static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9860 struct vmcs12 *vmcs12)
9861{
Wincy Van82f0dd42015-02-03 23:57:18 +08009862 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009863 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009864 !nested_cpu_has_vid(vmcs12) &&
9865 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009866 return 0;
9867
9868 /*
9869 * If virtualize x2apic mode is enabled,
9870 * virtualize apic access must be disabled.
9871 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009872 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9873 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009874 return -EINVAL;
9875
Wincy Van608406e2015-02-03 23:57:51 +08009876 /*
9877 * If virtual interrupt delivery is enabled,
9878 * we must exit on external interrupts.
9879 */
9880 if (nested_cpu_has_vid(vmcs12) &&
9881 !nested_exit_on_intr(vcpu))
9882 return -EINVAL;
9883
Wincy Van705699a2015-02-03 23:58:17 +08009884 /*
9885 * bits 15:8 should be zero in posted_intr_nv,
9886 * the descriptor address has been already checked
9887 * in nested_get_vmcs12_pages.
9888 */
9889 if (nested_cpu_has_posted_intr(vmcs12) &&
9890 (!nested_cpu_has_vid(vmcs12) ||
9891 !nested_exit_intr_ack_set(vcpu) ||
9892 vmcs12->posted_intr_nv & 0xff00))
9893 return -EINVAL;
9894
Wincy Vanf2b93282015-02-03 23:56:03 +08009895 /* tpr shadow is needed by all apicv features. */
9896 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9897 return -EINVAL;
9898
9899 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009900}
9901
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009902static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9903 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009904 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009905{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009906 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009907 u64 count, addr;
9908
9909 if (vmcs12_read_any(vcpu, count_field, &count) ||
9910 vmcs12_read_any(vcpu, addr_field, &addr)) {
9911 WARN_ON(1);
9912 return -EINVAL;
9913 }
9914 if (count == 0)
9915 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009916 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009917 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9918 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009919 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009920 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9921 addr_field, maxphyaddr, count, addr);
9922 return -EINVAL;
9923 }
9924 return 0;
9925}
9926
9927static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9928 struct vmcs12 *vmcs12)
9929{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009930 if (vmcs12->vm_exit_msr_load_count == 0 &&
9931 vmcs12->vm_exit_msr_store_count == 0 &&
9932 vmcs12->vm_entry_msr_load_count == 0)
9933 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009934 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009935 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009936 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009937 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009938 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009939 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009940 return -EINVAL;
9941 return 0;
9942}
9943
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009944static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9945 struct vmx_msr_entry *e)
9946{
9947 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009948 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009949 return -EINVAL;
9950 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9951 e->index == MSR_IA32_UCODE_REV)
9952 return -EINVAL;
9953 if (e->reserved != 0)
9954 return -EINVAL;
9955 return 0;
9956}
9957
9958static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9959 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009960{
9961 if (e->index == MSR_FS_BASE ||
9962 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009963 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9964 nested_vmx_msr_check_common(vcpu, e))
9965 return -EINVAL;
9966 return 0;
9967}
9968
9969static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9970 struct vmx_msr_entry *e)
9971{
9972 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9973 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009974 return -EINVAL;
9975 return 0;
9976}
9977
9978/*
9979 * Load guest's/host's msr at nested entry/exit.
9980 * return 0 for success, entry index for failure.
9981 */
9982static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9983{
9984 u32 i;
9985 struct vmx_msr_entry e;
9986 struct msr_data msr;
9987
9988 msr.host_initiated = false;
9989 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009990 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9991 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009992 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009993 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9994 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009995 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009996 }
9997 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009998 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009999 "%s check failed (%u, 0x%x, 0x%x)\n",
10000 __func__, i, e.index, e.reserved);
10001 goto fail;
10002 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010003 msr.index = e.index;
10004 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010005 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010006 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010007 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10008 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010009 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010010 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010011 }
10012 return 0;
10013fail:
10014 return i + 1;
10015}
10016
10017static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10018{
10019 u32 i;
10020 struct vmx_msr_entry e;
10021
10022 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010023 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010024 if (kvm_vcpu_read_guest(vcpu,
10025 gpa + i * sizeof(e),
10026 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010027 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010028 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10029 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010030 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010031 }
10032 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010033 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010034 "%s check failed (%u, 0x%x, 0x%x)\n",
10035 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010036 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010037 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010038 msr_info.host_initiated = false;
10039 msr_info.index = e.index;
10040 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010041 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010042 "%s cannot read MSR (%u, 0x%x)\n",
10043 __func__, i, e.index);
10044 return -EINVAL;
10045 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010046 if (kvm_vcpu_write_guest(vcpu,
10047 gpa + i * sizeof(e) +
10048 offsetof(struct vmx_msr_entry, value),
10049 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010050 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010051 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010052 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010053 return -EINVAL;
10054 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010055 }
10056 return 0;
10057}
10058
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010059/*
10060 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10061 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010062 * 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 +030010063 * guest in a way that will both be appropriate to L1's requests, and our
10064 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10065 * function also has additional necessary side-effects, like setting various
10066 * vcpu->arch fields.
10067 */
10068static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10069{
10070 struct vcpu_vmx *vmx = to_vmx(vcpu);
10071 u32 exec_control;
10072
10073 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10074 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10075 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10076 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10077 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10078 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10079 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10080 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10081 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10082 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10083 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10084 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10085 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10086 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10087 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10088 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10089 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10090 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10091 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10092 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10093 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10094 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10095 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10096 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10097 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10098 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10099 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10100 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10101 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10102 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10103 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10104 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10105 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10106 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10107 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10108 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10109
Jan Kiszka2996fca2014-06-16 13:59:43 +020010110 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
10111 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10112 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10113 } else {
10114 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10115 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10116 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010117 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10118 vmcs12->vm_entry_intr_info_field);
10119 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10120 vmcs12->vm_entry_exception_error_code);
10121 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10122 vmcs12->vm_entry_instruction_len);
10123 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10124 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010125 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010126 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010127 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10128 vmcs12->guest_pending_dbg_exceptions);
10129 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10130 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10131
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010132 if (nested_cpu_has_xsaves(vmcs12))
10133 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010134 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10135
Jan Kiszkaf4124502014-03-07 20:03:13 +010010136 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010137
Paolo Bonzini93140062016-07-06 13:23:51 +020010138 /* Preemption timer setting is only taken from vmcs01. */
10139 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10140 exec_control |= vmcs_config.pin_based_exec_ctrl;
10141 if (vmx->hv_deadline_tsc == -1)
10142 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10143
10144 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010145 if (nested_cpu_has_posted_intr(vmcs12)) {
10146 /*
10147 * Note that we use L0's vector here and in
10148 * vmx_deliver_nested_posted_interrupt.
10149 */
10150 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10151 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010152 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010153 vmcs_write64(POSTED_INTR_DESC_ADDR,
10154 page_to_phys(vmx->nested.pi_desc_page) +
10155 (unsigned long)(vmcs12->posted_intr_desc_addr &
10156 (PAGE_SIZE - 1)));
10157 } else
10158 exec_control &= ~PIN_BASED_POSTED_INTR;
10159
Jan Kiszkaf4124502014-03-07 20:03:13 +010010160 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010161
Jan Kiszkaf4124502014-03-07 20:03:13 +010010162 vmx->nested.preemption_timer_expired = false;
10163 if (nested_cpu_has_preemption_timer(vmcs12))
10164 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010165
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010166 /*
10167 * Whether page-faults are trapped is determined by a combination of
10168 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10169 * If enable_ept, L0 doesn't care about page faults and we should
10170 * set all of these to L1's desires. However, if !enable_ept, L0 does
10171 * care about (at least some) page faults, and because it is not easy
10172 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10173 * to exit on each and every L2 page fault. This is done by setting
10174 * MASK=MATCH=0 and (see below) EB.PF=1.
10175 * Note that below we don't need special code to set EB.PF beyond the
10176 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10177 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10178 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10179 *
10180 * A problem with this approach (when !enable_ept) is that L1 may be
10181 * injected with more page faults than it asked for. This could have
10182 * caused problems, but in practice existing hypervisors don't care.
10183 * To fix this, we will need to emulate the PFEC checking (on the L1
10184 * page tables), using walk_addr(), when injecting PFs to L1.
10185 */
10186 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10187 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10188 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10189 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10190
10191 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010192 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010193
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010194 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010195 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010196 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010197 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010198 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010199 if (nested_cpu_has(vmcs12,
10200 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10201 exec_control |= vmcs12->secondary_vm_exec_control;
10202
10203 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10204 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010205 * If translation failed, no matter: This feature asks
10206 * to exit when accessing the given address, and if it
10207 * can never be accessed, this feature won't do
10208 * anything anyway.
10209 */
10210 if (!vmx->nested.apic_access_page)
10211 exec_control &=
10212 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10213 else
10214 vmcs_write64(APIC_ACCESS_ADDR,
10215 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010216 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010217 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010218 exec_control |=
10219 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010220 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010221 }
10222
Wincy Van608406e2015-02-03 23:57:51 +080010223 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10224 vmcs_write64(EOI_EXIT_BITMAP0,
10225 vmcs12->eoi_exit_bitmap0);
10226 vmcs_write64(EOI_EXIT_BITMAP1,
10227 vmcs12->eoi_exit_bitmap1);
10228 vmcs_write64(EOI_EXIT_BITMAP2,
10229 vmcs12->eoi_exit_bitmap2);
10230 vmcs_write64(EOI_EXIT_BITMAP3,
10231 vmcs12->eoi_exit_bitmap3);
10232 vmcs_write16(GUEST_INTR_STATUS,
10233 vmcs12->guest_intr_status);
10234 }
10235
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010236 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10237 }
10238
10239
10240 /*
10241 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10242 * Some constant fields are set here by vmx_set_constant_host_state().
10243 * Other fields are different per CPU, and will be set later when
10244 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10245 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010246 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010247
10248 /*
Jim Mattson69c25252016-10-04 10:48:38 -070010249 * Set the MSR load/store lists to match L0's settings.
10250 */
10251 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040010252 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10253 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10254 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10255 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Jim Mattson69c25252016-10-04 10:48:38 -070010256
10257 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010258 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10259 * entry, but only if the current (host) sp changed from the value
10260 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10261 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10262 * here we just force the write to happen on entry.
10263 */
10264 vmx->host_rsp = 0;
10265
10266 exec_control = vmx_exec_control(vmx); /* L0's desires */
10267 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10268 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10269 exec_control &= ~CPU_BASED_TPR_SHADOW;
10270 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010271
10272 if (exec_control & CPU_BASED_TPR_SHADOW) {
10273 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10274 page_to_phys(vmx->nested.virtual_apic_page));
10275 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010276 } else {
10277#ifdef CONFIG_X86_64
10278 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10279 CPU_BASED_CR8_STORE_EXITING;
10280#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010281 }
10282
Wincy Van3af18d92015-02-03 23:49:31 +080010283 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010284 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10285 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10286 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10287 else
Wincy Van3af18d92015-02-03 23:49:31 +080010288 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10289
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010290 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010291 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010292 * Rather, exit every time.
10293 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010294 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10295 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10296
10297 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10298
10299 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10300 * bitwise-or of what L1 wants to trap for L2, and what we want to
10301 * trap. Note that CR0.TS also needs updating - we do this later.
10302 */
10303 update_exception_bitmap(vcpu);
10304 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10305 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10306
Nadav Har'El8049d652013-08-05 11:07:06 +030010307 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10308 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10309 * bits are further modified by vmx_set_efer() below.
10310 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010311 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010312
10313 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10314 * emulated by vmx_set_efer(), below.
10315 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010316 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010317 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10318 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010319 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10320
Jan Kiszka44811c02013-08-04 17:17:27 +020010321 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010322 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010323 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10324 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010325 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10326
10327
10328 set_cr4_guest_host_mask(vmx);
10329
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010330 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10331 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10332
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010333 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10334 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010335 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010336 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010337 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010338 if (kvm_has_tsc_control)
10339 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010340
Paolo Bonzini6236b782018-01-16 16:51:18 +010010341 if (cpu_has_vmx_msr_bitmap())
10342 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10343
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010344 if (enable_vpid) {
10345 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010346 * There is no direct mapping between vpid02 and vpid12, the
10347 * vpid02 is per-vCPU for L0 and reused while the value of
10348 * vpid12 is changed w/ one invvpid during nested vmentry.
10349 * The vpid12 is allocated by L1 for L2, so it will not
10350 * influence global bitmap(for vpid01 and vpid02 allocation)
10351 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010352 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010353 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10354 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10355 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10356 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10357 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10358 }
10359 } else {
10360 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10361 vmx_flush_tlb(vcpu);
10362 }
10363
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010364 }
10365
Ladi Prosek560a9792017-04-04 14:18:53 +020010366 if (enable_pml) {
10367 /*
10368 * Conceptually we want to copy the PML address and index from
10369 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10370 * since we always flush the log on each vmexit, this happens
10371 * to be equivalent to simply resetting the fields in vmcs02.
10372 */
10373 ASSERT(vmx->pml_pg);
10374 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10375 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10376 }
10377
Nadav Har'El155a97a2013-08-05 11:07:16 +030010378 if (nested_cpu_has_ept(vmcs12)) {
10379 kvm_mmu_unload(vcpu);
10380 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010381 } else if (nested_cpu_has2(vmcs12,
10382 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10383 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010384 }
10385
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010386 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10387 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010388 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010389 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10390 else
10391 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10392 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10393 vmx_set_efer(vcpu, vcpu->arch.efer);
10394
10395 /*
10396 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10397 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10398 * The CR0_READ_SHADOW is what L2 should have expected to read given
10399 * the specifications by L1; It's not enough to take
10400 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10401 * have more bits than L1 expected.
10402 */
10403 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10404 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10405
10406 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10407 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10408
10409 /* shadow page tables on either EPT or shadow page tables */
10410 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10411 kvm_mmu_reset_context(vcpu);
10412
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010413 if (!enable_ept)
10414 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10415
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010416 /*
10417 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10418 */
10419 if (enable_ept) {
10420 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10421 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10422 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10423 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10424 }
10425
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010426 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10427 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10428}
10429
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010430/*
10431 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10432 * for running an L2 nested guest.
10433 */
10434static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10435{
10436 struct vmcs12 *vmcs12;
10437 struct vcpu_vmx *vmx = to_vmx(vcpu);
10438 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010439 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010440 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010441
10442 if (!nested_vmx_check_permission(vcpu) ||
10443 !nested_vmx_check_vmcs12(vcpu))
10444 return 1;
10445
10446 skip_emulated_instruction(vcpu);
10447 vmcs12 = get_vmcs12(vcpu);
10448
Abel Gordon012f83c2013-04-18 14:39:25 +030010449 if (enable_shadow_vmcs)
10450 copy_shadow_to_vmcs12(vmx);
10451
Nadav Har'El7c177932011-05-25 23:12:04 +030010452 /*
10453 * The nested entry process starts with enforcing various prerequisites
10454 * on vmcs12 as required by the Intel SDM, and act appropriately when
10455 * they fail: As the SDM explains, some conditions should cause the
10456 * instruction to fail, while others will cause the instruction to seem
10457 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10458 * To speed up the normal (success) code path, we should avoid checking
10459 * for misconfigurations which will anyway be caught by the processor
10460 * when using the merged vmcs02.
10461 */
10462 if (vmcs12->launch_state == launch) {
10463 nested_vmx_failValid(vcpu,
10464 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10465 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10466 return 1;
10467 }
10468
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010469 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10470 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010471 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10472 return 1;
10473 }
10474
Wincy Van3af18d92015-02-03 23:49:31 +080010475 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010476 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10477 return 1;
10478 }
10479
Wincy Van3af18d92015-02-03 23:49:31 +080010480 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010481 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10482 return 1;
10483 }
10484
Wincy Vanf2b93282015-02-03 23:56:03 +080010485 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10486 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10487 return 1;
10488 }
10489
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010490 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10491 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10492 return 1;
10493 }
10494
Nadav Har'El7c177932011-05-25 23:12:04 +030010495 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010496 vmx->nested.nested_vmx_true_procbased_ctls_low,
10497 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010498 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010499 vmx->nested.nested_vmx_secondary_ctls_low,
10500 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010501 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010502 vmx->nested.nested_vmx_pinbased_ctls_low,
10503 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010504 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010505 vmx->nested.nested_vmx_true_exit_ctls_low,
10506 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010507 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010508 vmx->nested.nested_vmx_true_entry_ctls_low,
10509 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010510 {
10511 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10512 return 1;
10513 }
10514
10515 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10516 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10517 nested_vmx_failValid(vcpu,
10518 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10519 return 1;
10520 }
10521
Wincy Vanb9c237b2015-02-03 23:56:30 +080010522 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010523 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10524 nested_vmx_entry_failure(vcpu, vmcs12,
10525 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10526 return 1;
10527 }
10528 if (vmcs12->vmcs_link_pointer != -1ull) {
10529 nested_vmx_entry_failure(vcpu, vmcs12,
10530 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10531 return 1;
10532 }
10533
10534 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010535 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010536 * are performed on the field for the IA32_EFER MSR:
10537 * - Bits reserved in the IA32_EFER MSR must be 0.
10538 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10539 * the IA-32e mode guest VM-exit control. It must also be identical
10540 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10541 * CR0.PG) is 1.
10542 */
10543 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10544 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10545 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10546 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10547 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10548 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10549 nested_vmx_entry_failure(vcpu, vmcs12,
10550 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10551 return 1;
10552 }
10553 }
10554
10555 /*
10556 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10557 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10558 * the values of the LMA and LME bits in the field must each be that of
10559 * the host address-space size VM-exit control.
10560 */
10561 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10562 ia32e = (vmcs12->vm_exit_controls &
10563 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10564 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10565 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10566 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10567 nested_vmx_entry_failure(vcpu, vmcs12,
10568 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10569 return 1;
10570 }
10571 }
10572
10573 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010574 * We're finally done with prerequisite checking, and can start with
10575 * the nested entry.
10576 */
10577
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010578 enter_guest_mode(vcpu);
10579
Jan Kiszka2996fca2014-06-16 13:59:43 +020010580 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10581 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10582
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010583 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010584 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010585 vmx_vcpu_put(vcpu);
10586 vmx_vcpu_load(vcpu, cpu);
10587 vcpu->cpu = cpu;
10588 put_cpu();
10589
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010590 vmx_segment_cache_clear(vmx);
10591
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010592 prepare_vmcs02(vcpu, vmcs12);
10593
Wincy Vanff651cb2014-12-11 08:52:58 +030010594 msr_entry_idx = nested_vmx_load_msr(vcpu,
10595 vmcs12->vm_entry_msr_load_addr,
10596 vmcs12->vm_entry_msr_load_count);
10597 if (msr_entry_idx) {
10598 leave_guest_mode(vcpu);
10599 vmx_load_vmcs01(vcpu);
10600 nested_vmx_entry_failure(vcpu, vmcs12,
10601 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10602 return 1;
10603 }
10604
10605 vmcs12->launch_state = 1;
10606
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +020010607 /* Hide L1D cache contents from the nested guest. */
10608 vmx->vcpu.arch.l1tf_flush_l1d = true;
10609
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010610 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010611 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010612
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010613 vmx->nested.nested_run_pending = 1;
10614
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010615 /*
10616 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10617 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10618 * returned as far as L1 is concerned. It will only return (and set
10619 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10620 */
10621 return 1;
10622}
10623
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010624/*
10625 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10626 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10627 * This function returns the new value we should put in vmcs12.guest_cr0.
10628 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10629 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10630 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10631 * didn't trap the bit, because if L1 did, so would L0).
10632 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10633 * been modified by L2, and L1 knows it. So just leave the old value of
10634 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10635 * isn't relevant, because if L0 traps this bit it can set it to anything.
10636 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10637 * changed these bits, and therefore they need to be updated, but L0
10638 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10639 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10640 */
10641static inline unsigned long
10642vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10643{
10644 return
10645 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10646 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10647 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10648 vcpu->arch.cr0_guest_owned_bits));
10649}
10650
10651static inline unsigned long
10652vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10653{
10654 return
10655 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10656 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10657 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10658 vcpu->arch.cr4_guest_owned_bits));
10659}
10660
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010661static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10662 struct vmcs12 *vmcs12)
10663{
10664 u32 idt_vectoring;
10665 unsigned int nr;
10666
Gleb Natapov851eb6672013-09-25 12:51:34 +030010667 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010668 nr = vcpu->arch.exception.nr;
10669 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10670
10671 if (kvm_exception_is_soft(nr)) {
10672 vmcs12->vm_exit_instruction_len =
10673 vcpu->arch.event_exit_inst_len;
10674 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10675 } else
10676 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10677
10678 if (vcpu->arch.exception.has_error_code) {
10679 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10680 vmcs12->idt_vectoring_error_code =
10681 vcpu->arch.exception.error_code;
10682 }
10683
10684 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010685 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010686 vmcs12->idt_vectoring_info_field =
10687 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10688 } else if (vcpu->arch.interrupt.pending) {
10689 nr = vcpu->arch.interrupt.nr;
10690 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10691
10692 if (vcpu->arch.interrupt.soft) {
10693 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10694 vmcs12->vm_entry_instruction_len =
10695 vcpu->arch.event_exit_inst_len;
10696 } else
10697 idt_vectoring |= INTR_TYPE_EXT_INTR;
10698
10699 vmcs12->idt_vectoring_info_field = idt_vectoring;
10700 }
10701}
10702
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010703static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10704{
10705 struct vcpu_vmx *vmx = to_vmx(vcpu);
10706
Jan Kiszkaf4124502014-03-07 20:03:13 +010010707 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10708 vmx->nested.preemption_timer_expired) {
10709 if (vmx->nested.nested_run_pending)
10710 return -EBUSY;
10711 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10712 return 0;
10713 }
10714
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010715 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010716 if (vmx->nested.nested_run_pending ||
10717 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010718 return -EBUSY;
10719 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10720 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10721 INTR_INFO_VALID_MASK, 0);
10722 /*
10723 * The NMI-triggered VM exit counts as injection:
10724 * clear this one and block further NMIs.
10725 */
10726 vcpu->arch.nmi_pending = 0;
10727 vmx_set_nmi_mask(vcpu, true);
10728 return 0;
10729 }
10730
10731 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10732 nested_exit_on_intr(vcpu)) {
10733 if (vmx->nested.nested_run_pending)
10734 return -EBUSY;
10735 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010736 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010737 }
10738
David Hildenbrand1edccf22017-01-25 11:58:58 +010010739 vmx_complete_nested_posted_interrupt(vcpu);
10740 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010741}
10742
Jan Kiszkaf4124502014-03-07 20:03:13 +010010743static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10744{
10745 ktime_t remaining =
10746 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10747 u64 value;
10748
10749 if (ktime_to_ns(remaining) <= 0)
10750 return 0;
10751
10752 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10753 do_div(value, 1000000);
10754 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10755}
10756
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010757/*
10758 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10759 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10760 * and this function updates it to reflect the changes to the guest state while
10761 * L2 was running (and perhaps made some exits which were handled directly by L0
10762 * without going back to L1), and to reflect the exit reason.
10763 * Note that we do not have to copy here all VMCS fields, just those that
10764 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10765 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10766 * which already writes to vmcs12 directly.
10767 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010768static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10769 u32 exit_reason, u32 exit_intr_info,
10770 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010771{
10772 /* update guest state fields: */
10773 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10774 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10775
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010776 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10777 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10778 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10779
10780 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10781 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10782 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10783 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10784 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10785 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10786 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10787 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10788 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10789 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10790 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10791 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10792 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10793 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10794 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10795 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10796 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10797 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10798 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10799 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10800 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10801 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10802 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10803 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10804 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10805 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10806 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10807 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10808 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10809 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10810 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10811 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10812 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10813 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10814 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10815 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10816
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010817 vmcs12->guest_interruptibility_info =
10818 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10819 vmcs12->guest_pending_dbg_exceptions =
10820 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010821 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10822 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10823 else
10824 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010825
Jan Kiszkaf4124502014-03-07 20:03:13 +010010826 if (nested_cpu_has_preemption_timer(vmcs12)) {
10827 if (vmcs12->vm_exit_controls &
10828 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10829 vmcs12->vmx_preemption_timer_value =
10830 vmx_get_preemption_timer_value(vcpu);
10831 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10832 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010833
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010834 /*
10835 * In some cases (usually, nested EPT), L2 is allowed to change its
10836 * own CR3 without exiting. If it has changed it, we must keep it.
10837 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10838 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10839 *
10840 * Additionally, restore L2's PDPTR to vmcs12.
10841 */
10842 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010843 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010844 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10845 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10846 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10847 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10848 }
10849
Jim Mattson4933e9f2017-06-01 12:44:46 -070010850 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010851
Wincy Van608406e2015-02-03 23:57:51 +080010852 if (nested_cpu_has_vid(vmcs12))
10853 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10854
Jan Kiszkac18911a2013-03-13 16:06:41 +010010855 vmcs12->vm_entry_controls =
10856 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010857 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010858
Jan Kiszka2996fca2014-06-16 13:59:43 +020010859 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10860 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10861 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10862 }
10863
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010864 /* TODO: These cannot have changed unless we have MSR bitmaps and
10865 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010866 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010867 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010868 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10869 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010870 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10871 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10872 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010873 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010874 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010875 if (nested_cpu_has_xsaves(vmcs12))
10876 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010877
10878 /* update exit information fields: */
10879
Jan Kiszka533558b2014-01-04 18:47:20 +010010880 vmcs12->vm_exit_reason = exit_reason;
10881 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010882
Jan Kiszka533558b2014-01-04 18:47:20 +010010883 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010884 if ((vmcs12->vm_exit_intr_info &
10885 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10886 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10887 vmcs12->vm_exit_intr_error_code =
10888 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010889 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010890 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10891 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10892
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010893 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10894 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10895 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010896 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010897
10898 /*
10899 * Transfer the event that L0 or L1 may wanted to inject into
10900 * L2 to IDT_VECTORING_INFO_FIELD.
10901 */
10902 vmcs12_save_pending_event(vcpu, vmcs12);
10903 }
10904
10905 /*
10906 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10907 * preserved above and would only end up incorrectly in L1.
10908 */
10909 vcpu->arch.nmi_injected = false;
10910 kvm_clear_exception_queue(vcpu);
10911 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010912}
10913
10914/*
10915 * A part of what we need to when the nested L2 guest exits and we want to
10916 * run its L1 parent, is to reset L1's guest state to the host state specified
10917 * in vmcs12.
10918 * This function is to be called not only on normal nested exit, but also on
10919 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10920 * Failures During or After Loading Guest State").
10921 * This function should be called when the active VMCS is L1's (vmcs01).
10922 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010923static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10924 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010925{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010926 struct kvm_segment seg;
10927
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010928 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10929 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010930 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010931 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10932 else
10933 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10934 vmx_set_efer(vcpu, vcpu->arch.efer);
10935
10936 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10937 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010938 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010939 /*
10940 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10941 * actually changed, because it depends on the current state of
10942 * fpu_active (which may have changed).
10943 * Note that vmx_set_cr0 refers to efer set above.
10944 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010945 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010946 /*
10947 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10948 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10949 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10950 */
10951 update_exception_bitmap(vcpu);
10952 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10953 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10954
10955 /*
10956 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10957 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10958 */
10959 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080010960 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010961
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010962 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010963
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010964 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10965 kvm_mmu_reset_context(vcpu);
10966
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010967 if (!enable_ept)
10968 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10969
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010970 if (enable_vpid) {
10971 /*
10972 * Trivially support vpid by letting L2s share their parent
10973 * L1's vpid. TODO: move to a more elaborate solution, giving
10974 * each L2 its own vpid and exposing the vpid feature to L1.
10975 */
10976 vmx_flush_tlb(vcpu);
10977 }
10978
10979
10980 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10981 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10982 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10983 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10984 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020010985 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
10986 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010987
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010988 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10989 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10990 vmcs_write64(GUEST_BNDCFGS, 0);
10991
Jan Kiszka44811c02013-08-04 17:17:27 +020010992 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010993 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010994 vcpu->arch.pat = vmcs12->host_ia32_pat;
10995 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010996 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10997 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10998 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010999
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011000 /* Set L1 segment info according to Intel SDM
11001 27.5.2 Loading Host Segment and Descriptor-Table Registers */
11002 seg = (struct kvm_segment) {
11003 .base = 0,
11004 .limit = 0xFFFFFFFF,
11005 .selector = vmcs12->host_cs_selector,
11006 .type = 11,
11007 .present = 1,
11008 .s = 1,
11009 .g = 1
11010 };
11011 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11012 seg.l = 1;
11013 else
11014 seg.db = 1;
11015 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11016 seg = (struct kvm_segment) {
11017 .base = 0,
11018 .limit = 0xFFFFFFFF,
11019 .type = 3,
11020 .present = 1,
11021 .s = 1,
11022 .db = 1,
11023 .g = 1
11024 };
11025 seg.selector = vmcs12->host_ds_selector;
11026 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11027 seg.selector = vmcs12->host_es_selector;
11028 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11029 seg.selector = vmcs12->host_ss_selector;
11030 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11031 seg.selector = vmcs12->host_fs_selector;
11032 seg.base = vmcs12->host_fs_base;
11033 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11034 seg.selector = vmcs12->host_gs_selector;
11035 seg.base = vmcs12->host_gs_base;
11036 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11037 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011038 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011039 .limit = 0x67,
11040 .selector = vmcs12->host_tr_selector,
11041 .type = 11,
11042 .present = 1
11043 };
11044 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11045
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011046 kvm_set_dr(vcpu, 7, 0x400);
11047 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011048
Wincy Van3af18d92015-02-03 23:49:31 +080011049 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010011050 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011051
Wincy Vanff651cb2014-12-11 08:52:58 +030011052 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11053 vmcs12->vm_exit_msr_load_count))
11054 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011055}
11056
11057/*
11058 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11059 * and modify vmcs12 to make it see what it would expect to see there if
11060 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11061 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011062static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11063 u32 exit_intr_info,
11064 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011065{
11066 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011067 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11068
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011069 /* trying to cancel vmlaunch/vmresume is a bug */
11070 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11071
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011072 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011073 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11074 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011075
Wincy Vanff651cb2014-12-11 08:52:58 +030011076 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11077 vmcs12->vm_exit_msr_store_count))
11078 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11079
Wanpeng Lif3380ca2014-08-05 12:42:23 +080011080 vmx_load_vmcs01(vcpu);
11081
Bandan Das77b0f5d2014-04-19 18:17:45 -040011082 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
11083 && nested_exit_intr_ack_set(vcpu)) {
11084 int irq = kvm_cpu_get_interrupt(vcpu);
11085 WARN_ON(irq < 0);
11086 vmcs12->vm_exit_intr_info = irq |
11087 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11088 }
11089
Jan Kiszka542060e2014-01-04 18:47:21 +010011090 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11091 vmcs12->exit_qualification,
11092 vmcs12->idt_vectoring_info_field,
11093 vmcs12->vm_exit_intr_info,
11094 vmcs12->vm_exit_intr_error_code,
11095 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011096
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011097 vm_entry_controls_reset_shadow(vmx);
11098 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011099 vmx_segment_cache_clear(vmx);
11100
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011101 load_vmcs12_host_state(vcpu, vmcs12);
11102
Paolo Bonzini93140062016-07-06 13:23:51 +020011103 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040011104 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11105 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011106 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011107 if (vmx->hv_deadline_tsc == -1)
11108 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11109 PIN_BASED_VMX_PREEMPTION_TIMER);
11110 else
11111 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11112 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011113 if (kvm_has_tsc_control)
11114 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011115
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011116 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11117 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11118 vmx_set_virtual_x2apic_mode(vcpu,
11119 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070011120 } else if (!nested_cpu_has_ept(vmcs12) &&
11121 nested_cpu_has2(vmcs12,
11122 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11123 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011124 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011125
11126 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11127 vmx->host_rsp = 0;
11128
11129 /* Unpin physical memory we referred to in vmcs02 */
11130 if (vmx->nested.apic_access_page) {
11131 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011132 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011133 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011134 if (vmx->nested.virtual_apic_page) {
11135 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011136 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011137 }
Wincy Van705699a2015-02-03 23:58:17 +080011138 if (vmx->nested.pi_desc_page) {
11139 kunmap(vmx->nested.pi_desc_page);
11140 nested_release_page(vmx->nested.pi_desc_page);
11141 vmx->nested.pi_desc_page = NULL;
11142 vmx->nested.pi_desc = NULL;
11143 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011144
11145 /*
Tang Chen38b99172014-09-24 15:57:54 +080011146 * We are now running in L2, mmu_notifier will force to reload the
11147 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11148 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011149 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011150
11151 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011152 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11153 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11154 * success or failure flag accordingly.
11155 */
11156 if (unlikely(vmx->fail)) {
11157 vmx->fail = 0;
11158 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
11159 } else
11160 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011161 if (enable_shadow_vmcs)
11162 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011163
11164 /* in case we halted in L2 */
11165 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011166}
11167
Nadav Har'El7c177932011-05-25 23:12:04 +030011168/*
Jan Kiszka42124922014-01-04 18:47:19 +010011169 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11170 */
11171static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11172{
Wanpeng Lic886f282017-03-06 04:03:28 -080011173 if (is_guest_mode(vcpu)) {
11174 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011175 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011176 }
Jan Kiszka42124922014-01-04 18:47:19 +010011177 free_nested(to_vmx(vcpu));
11178}
11179
11180/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011181 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11182 * 23.7 "VM-entry failures during or after loading guest state" (this also
11183 * lists the acceptable exit-reason and exit-qualification parameters).
11184 * It should only be called before L2 actually succeeded to run, and when
11185 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11186 */
11187static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11188 struct vmcs12 *vmcs12,
11189 u32 reason, unsigned long qualification)
11190{
11191 load_vmcs12_host_state(vcpu, vmcs12);
11192 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11193 vmcs12->exit_qualification = qualification;
11194 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011195 if (enable_shadow_vmcs)
11196 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011197}
11198
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011199static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11200 struct x86_instruction_info *info,
11201 enum x86_intercept_stage stage)
11202{
11203 return X86EMUL_CONTINUE;
11204}
11205
Yunhong Jiang64672c92016-06-13 14:19:59 -070011206#ifdef CONFIG_X86_64
11207/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11208static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11209 u64 divisor, u64 *result)
11210{
11211 u64 low = a << shift, high = a >> (64 - shift);
11212
11213 /* To avoid the overflow on divq */
11214 if (high >= divisor)
11215 return 1;
11216
11217 /* Low hold the result, high hold rem which is discarded */
11218 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11219 "rm" (divisor), "0" (low), "1" (high));
11220 *result = low;
11221
11222 return 0;
11223}
11224
11225static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11226{
11227 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011228 u64 tscl = rdtsc();
11229 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11230 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011231
11232 /* Convert to host delta tsc if tsc scaling is enabled */
11233 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11234 u64_shl_div_u64(delta_tsc,
11235 kvm_tsc_scaling_ratio_frac_bits,
11236 vcpu->arch.tsc_scaling_ratio,
11237 &delta_tsc))
11238 return -ERANGE;
11239
11240 /*
11241 * If the delta tsc can't fit in the 32 bit after the multi shift,
11242 * we can't use the preemption timer.
11243 * It's possible that it fits on later vmentries, but checking
11244 * on every vmentry is costly so we just use an hrtimer.
11245 */
11246 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11247 return -ERANGE;
11248
11249 vmx->hv_deadline_tsc = tscl + delta_tsc;
11250 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11251 PIN_BASED_VMX_PREEMPTION_TIMER);
11252 return 0;
11253}
11254
11255static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11256{
11257 struct vcpu_vmx *vmx = to_vmx(vcpu);
11258 vmx->hv_deadline_tsc = -1;
11259 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11260 PIN_BASED_VMX_PREEMPTION_TIMER);
11261}
11262#endif
11263
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011264static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011265{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011266 if (ple_gap)
11267 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011268}
11269
Kai Huang843e4332015-01-28 10:54:28 +080011270static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11271 struct kvm_memory_slot *slot)
11272{
11273 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11274 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11275}
11276
11277static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11278 struct kvm_memory_slot *slot)
11279{
11280 kvm_mmu_slot_set_dirty(kvm, slot);
11281}
11282
11283static void vmx_flush_log_dirty(struct kvm *kvm)
11284{
11285 kvm_flush_pml_buffers(kvm);
11286}
11287
11288static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11289 struct kvm_memory_slot *memslot,
11290 gfn_t offset, unsigned long mask)
11291{
11292 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11293}
11294
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011295static void __pi_post_block(struct kvm_vcpu *vcpu)
11296{
11297 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11298 struct pi_desc old, new;
11299 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011300
11301 do {
11302 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011303 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11304 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011305
11306 dest = cpu_physical_id(vcpu->cpu);
11307
11308 if (x2apic_enabled())
11309 new.ndst = dest;
11310 else
11311 new.ndst = (dest << 8) & 0xFF00;
11312
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011313 /* set 'NV' to 'notification vector' */
11314 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011315 } while (cmpxchg64(&pi_desc->control, old.control,
11316 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011317
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011318 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11319 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011320 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011321 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011322 vcpu->pre_pcpu = -1;
11323 }
11324}
11325
Feng Wuefc64402015-09-18 22:29:51 +080011326/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011327 * This routine does the following things for vCPU which is going
11328 * to be blocked if VT-d PI is enabled.
11329 * - Store the vCPU to the wakeup list, so when interrupts happen
11330 * we can find the right vCPU to wake up.
11331 * - Change the Posted-interrupt descriptor as below:
11332 * 'NDST' <-- vcpu->pre_pcpu
11333 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11334 * - If 'ON' is set during this process, which means at least one
11335 * interrupt is posted for this vCPU, we cannot block it, in
11336 * this case, return 1, otherwise, return 0.
11337 *
11338 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011339static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011340{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011341 unsigned int dest;
11342 struct pi_desc old, new;
11343 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11344
11345 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011346 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11347 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011348 return 0;
11349
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011350 WARN_ON(irqs_disabled());
11351 local_irq_disable();
11352 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11353 vcpu->pre_pcpu = vcpu->cpu;
11354 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11355 list_add_tail(&vcpu->blocked_vcpu_list,
11356 &per_cpu(blocked_vcpu_on_cpu,
11357 vcpu->pre_pcpu));
11358 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11359 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011360
11361 do {
11362 old.control = new.control = pi_desc->control;
11363
Feng Wubf9f6ac2015-09-18 22:29:55 +080011364 WARN((pi_desc->sn == 1),
11365 "Warning: SN field of posted-interrupts "
11366 "is set before blocking\n");
11367
11368 /*
11369 * Since vCPU can be preempted during this process,
11370 * vcpu->cpu could be different with pre_pcpu, we
11371 * need to set pre_pcpu as the destination of wakeup
11372 * notification event, then we can find the right vCPU
11373 * to wakeup in wakeup handler if interrupts happen
11374 * when the vCPU is in blocked state.
11375 */
11376 dest = cpu_physical_id(vcpu->pre_pcpu);
11377
11378 if (x2apic_enabled())
11379 new.ndst = dest;
11380 else
11381 new.ndst = (dest << 8) & 0xFF00;
11382
11383 /* set 'NV' to 'wakeup vector' */
11384 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011385 } while (cmpxchg64(&pi_desc->control, old.control,
11386 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011387
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011388 /* We should not block the vCPU if an interrupt is posted for it. */
11389 if (pi_test_on(pi_desc) == 1)
11390 __pi_post_block(vcpu);
11391
11392 local_irq_enable();
11393 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011394}
11395
Yunhong Jiangbc225122016-06-13 14:19:58 -070011396static int vmx_pre_block(struct kvm_vcpu *vcpu)
11397{
11398 if (pi_pre_block(vcpu))
11399 return 1;
11400
Yunhong Jiang64672c92016-06-13 14:19:59 -070011401 if (kvm_lapic_hv_timer_in_use(vcpu))
11402 kvm_lapic_switch_to_sw_timer(vcpu);
11403
Yunhong Jiangbc225122016-06-13 14:19:58 -070011404 return 0;
11405}
11406
11407static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011408{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011409 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011410 return;
11411
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011412 WARN_ON(irqs_disabled());
11413 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011414 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011415 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011416}
11417
Yunhong Jiangbc225122016-06-13 14:19:58 -070011418static void vmx_post_block(struct kvm_vcpu *vcpu)
11419{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011420 if (kvm_x86_ops->set_hv_timer)
11421 kvm_lapic_switch_to_hv_timer(vcpu);
11422
Yunhong Jiangbc225122016-06-13 14:19:58 -070011423 pi_post_block(vcpu);
11424}
11425
Feng Wubf9f6ac2015-09-18 22:29:55 +080011426/*
Feng Wuefc64402015-09-18 22:29:51 +080011427 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11428 *
11429 * @kvm: kvm
11430 * @host_irq: host irq of the interrupt
11431 * @guest_irq: gsi of the interrupt
11432 * @set: set or unset PI
11433 * returns 0 on success, < 0 on failure
11434 */
11435static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11436 uint32_t guest_irq, bool set)
11437{
11438 struct kvm_kernel_irq_routing_entry *e;
11439 struct kvm_irq_routing_table *irq_rt;
11440 struct kvm_lapic_irq irq;
11441 struct kvm_vcpu *vcpu;
11442 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011443 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011444
11445 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011446 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11447 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011448 return 0;
11449
11450 idx = srcu_read_lock(&kvm->irq_srcu);
11451 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011452 if (guest_irq >= irq_rt->nr_rt_entries ||
11453 hlist_empty(&irq_rt->map[guest_irq])) {
11454 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11455 guest_irq, irq_rt->nr_rt_entries);
11456 goto out;
11457 }
Feng Wuefc64402015-09-18 22:29:51 +080011458
11459 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11460 if (e->type != KVM_IRQ_ROUTING_MSI)
11461 continue;
11462 /*
11463 * VT-d PI cannot support posting multicast/broadcast
11464 * interrupts to a vCPU, we still use interrupt remapping
11465 * for these kind of interrupts.
11466 *
11467 * For lowest-priority interrupts, we only support
11468 * those with single CPU as the destination, e.g. user
11469 * configures the interrupts via /proc/irq or uses
11470 * irqbalance to make the interrupts single-CPU.
11471 *
11472 * We will support full lowest-priority interrupt later.
11473 */
11474
Radim Krčmář371313132016-07-12 22:09:27 +020011475 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011476 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11477 /*
11478 * Make sure the IRTE is in remapped mode if
11479 * we don't handle it in posted mode.
11480 */
11481 ret = irq_set_vcpu_affinity(host_irq, NULL);
11482 if (ret < 0) {
11483 printk(KERN_INFO
11484 "failed to back to remapped mode, irq: %u\n",
11485 host_irq);
11486 goto out;
11487 }
11488
Feng Wuefc64402015-09-18 22:29:51 +080011489 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011490 }
Feng Wuefc64402015-09-18 22:29:51 +080011491
11492 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11493 vcpu_info.vector = irq.vector;
11494
Feng Wub6ce9782016-01-25 16:53:35 +080011495 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011496 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11497
11498 if (set)
11499 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011500 else
Feng Wuefc64402015-09-18 22:29:51 +080011501 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011502
11503 if (ret < 0) {
11504 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11505 __func__);
11506 goto out;
11507 }
11508 }
11509
11510 ret = 0;
11511out:
11512 srcu_read_unlock(&kvm->irq_srcu, idx);
11513 return ret;
11514}
11515
Ashok Rajc45dcc72016-06-22 14:59:56 +080011516static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11517{
11518 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11519 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11520 FEATURE_CONTROL_LMCE;
11521 else
11522 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11523 ~FEATURE_CONTROL_LMCE;
11524}
11525
Kees Cook404f6aa2016-08-08 16:29:06 -070011526static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011527 .cpu_has_kvm_support = cpu_has_kvm_support,
11528 .disabled_by_bios = vmx_disabled_by_bios,
11529 .hardware_setup = hardware_setup,
11530 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011531 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011532 .hardware_enable = hardware_enable,
11533 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011534 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011535 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011536
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040011537 .vm_init = vmx_vm_init,
11538
Avi Kivity6aa8b732006-12-10 02:21:36 -080011539 .vcpu_create = vmx_create_vcpu,
11540 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011541 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011542
Avi Kivity04d2cc72007-09-10 18:10:54 +030011543 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011544 .vcpu_load = vmx_vcpu_load,
11545 .vcpu_put = vmx_vcpu_put,
11546
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011547 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011548 .get_msr = vmx_get_msr,
11549 .set_msr = vmx_set_msr,
11550 .get_segment_base = vmx_get_segment_base,
11551 .get_segment = vmx_get_segment,
11552 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011553 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011554 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011555 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011556 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011557 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011558 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011559 .set_cr3 = vmx_set_cr3,
11560 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011561 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011562 .get_idt = vmx_get_idt,
11563 .set_idt = vmx_set_idt,
11564 .get_gdt = vmx_get_gdt,
11565 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011566 .get_dr6 = vmx_get_dr6,
11567 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011568 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011569 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011570 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011571 .get_rflags = vmx_get_rflags,
11572 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011573
11574 .get_pkru = vmx_get_pkru,
11575
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011576 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011577 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011578
11579 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011580
Avi Kivity6aa8b732006-12-10 02:21:36 -080011581 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011582 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011583 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011584 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11585 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011586 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011587 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011588 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011589 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011590 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011591 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011592 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011593 .get_nmi_mask = vmx_get_nmi_mask,
11594 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011595 .enable_nmi_window = enable_nmi_window,
11596 .enable_irq_window = enable_irq_window,
11597 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011598 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011599 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011600 .get_enable_apicv = vmx_get_enable_apicv,
11601 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011602 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11603 .hwapic_irr_update = vmx_hwapic_irr_update,
11604 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011605 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11606 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011607
Izik Eiduscbc94022007-10-25 00:29:55 +020011608 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011609 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011610 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011611
Avi Kivity586f9602010-11-18 13:09:54 +020011612 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011613
Sheng Yang17cc3932010-01-05 19:02:27 +080011614 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011615
11616 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011617
11618 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011619 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011620
11621 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011622
11623 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011624
11625 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011626
11627 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011628
11629 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011630 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011631 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011632 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011633
11634 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011635
11636 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011637
11638 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11639 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11640 .flush_log_dirty = vmx_flush_log_dirty,
11641 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011642
Feng Wubf9f6ac2015-09-18 22:29:55 +080011643 .pre_block = vmx_pre_block,
11644 .post_block = vmx_post_block,
11645
Wei Huang25462f72015-06-19 15:45:05 +020011646 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011647
11648 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011649
11650#ifdef CONFIG_X86_64
11651 .set_hv_timer = vmx_set_hv_timer,
11652 .cancel_hv_timer = vmx_cancel_hv_timer,
11653#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011654
11655 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011656};
11657
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011658static int __init vmx_setup_l1d_flush(void)
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011659{
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011660 struct page *page;
11661
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011662 if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER ||
11663 !boot_cpu_has_bug(X86_BUG_L1TF))
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011664 return 0;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011665
Paolo Bonziniacca8a72018-07-02 13:03:48 +020011666 if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
11667 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
11668 if (!page)
11669 return -ENOMEM;
11670 vmx_l1d_flush_pages = page_address(page);
11671 }
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011672
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011673 static_branch_enable(&vmx_l1d_should_flush);
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011674 return 0;
11675}
11676
11677static void vmx_free_l1d_flush_pages(void)
11678{
11679 if (vmx_l1d_flush_pages) {
11680 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
11681 vmx_l1d_flush_pages = NULL;
11682 }
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011683}
11684
Avi Kivity6aa8b732006-12-10 02:21:36 -080011685static int __init vmx_init(void)
11686{
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011687 int r;
11688
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011689 r = vmx_setup_l1d_flush();
11690 if (r)
11691 return r;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011692
11693 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11694 __alignof__(struct vcpu_vmx), THIS_MODULE);
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011695 if (r) {
11696 vmx_free_l1d_flush_pages();
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011697 return r;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011698 }
Sheng Yang25c5f222008-03-28 13:18:56 +080011699
Dave Young2965faa2015-09-09 15:38:55 -070011700#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011701 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11702 crash_vmclear_local_loaded_vmcss);
11703#endif
11704
He, Qingfdef3ad2007-04-30 09:45:24 +030011705 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011706}
11707
11708static void __exit vmx_exit(void)
11709{
Dave Young2965faa2015-09-09 15:38:55 -070011710#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011711 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011712 synchronize_rcu();
11713#endif
11714
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011715 kvm_exit();
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011716
11717 vmx_free_l1d_flush_pages();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011718}
11719
11720module_init(vmx_init)
11721module_exit(vmx_exit)