blob: e4ccb931ab403e21288270c063245bd7aaaa8585 [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,
2041 u64 guest_val, u64 host_val)
2042{
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002043 int i, j;
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 Wilk5d3eaa22018-06-20 22:00:47 -04002079 j = find_msr(&m->host, msr);
2080 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002081 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002082 "Can't add msr %x\n", msr);
2083 return;
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002084 }
2085 if (i < 0) {
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002086 i = m->guest.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002087 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002088 }
2089 if (j < 0) {
2090 j = m->host.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002091 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002092 }
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002093 m->guest.val[i].index = msr;
2094 m->guest.val[i].value = guest_val;
Konrad Rzeszutek Wilk5d3eaa22018-06-20 22:00:47 -04002095 m->host.val[j].index = msr;
2096 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002097}
2098
Avi Kivity33ed6322007-05-02 16:54:03 +03002099static void reload_tss(void)
2100{
Avi Kivity33ed6322007-05-02 16:54:03 +03002101 /*
2102 * VT restores TR but not its size. Useless.
2103 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002104 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002105 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002106
Avi Kivityd3591922010-07-26 18:32:39 +03002107 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002108 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2109 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002110}
2111
Avi Kivity92c0d902009-10-29 11:00:16 +02002112static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002113{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002114 u64 guest_efer = vmx->vcpu.arch.efer;
2115 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002116
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002117 if (!enable_ept) {
2118 /*
2119 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2120 * host CPUID is more efficient than testing guest CPUID
2121 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2122 */
2123 if (boot_cpu_has(X86_FEATURE_SMEP))
2124 guest_efer |= EFER_NX;
2125 else if (!(guest_efer & EFER_NX))
2126 ignore_bits |= EFER_NX;
2127 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002128
Avi Kivity51c6cf62007-08-29 03:48:05 +03002129 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002130 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002131 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002132 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002133#ifdef CONFIG_X86_64
2134 ignore_bits |= EFER_LMA | EFER_LME;
2135 /* SCE is meaningful only in long mode on Intel */
2136 if (guest_efer & EFER_LMA)
2137 ignore_bits &= ~(u64)EFER_SCE;
2138#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002139
2140 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002141
2142 /*
2143 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2144 * On CPUs that support "load IA32_EFER", always switch EFER
2145 * atomically, since it's faster than switching it manually.
2146 */
2147 if (cpu_has_load_ia32_efer ||
2148 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002149 if (!(guest_efer & EFER_LMA))
2150 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002151 if (guest_efer != host_efer)
2152 add_atomic_switch_msr(vmx, MSR_EFER,
2153 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002154 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002155 } else {
2156 guest_efer &= ~ignore_bits;
2157 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002158
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002159 vmx->guest_msrs[efer_offset].data = guest_efer;
2160 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2161
2162 return true;
2163 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002164}
2165
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002166static unsigned long segment_base(u16 selector)
2167{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002168 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002169 struct desc_struct *d;
2170 unsigned long table_base;
2171 unsigned long v;
2172
2173 if (!(selector & ~3))
2174 return 0;
2175
Avi Kivityd3591922010-07-26 18:32:39 +03002176 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002177
2178 if (selector & 4) { /* from ldt */
2179 u16 ldt_selector = kvm_read_ldt();
2180
2181 if (!(ldt_selector & ~3))
2182 return 0;
2183
2184 table_base = segment_base(ldt_selector);
2185 }
2186 d = (struct desc_struct *)(table_base + (selector & ~7));
2187 v = get_desc_base(d);
2188#ifdef CONFIG_X86_64
2189 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2190 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2191#endif
2192 return v;
2193}
2194
2195static inline unsigned long kvm_read_tr_base(void)
2196{
2197 u16 tr;
2198 asm("str %0" : "=g"(tr));
2199 return segment_base(tr);
2200}
2201
Avi Kivity04d2cc72007-09-10 18:10:54 +03002202static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002203{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002204 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002205 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002206
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002207 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002208 return;
2209
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002210 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002211 /*
2212 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2213 * allow segment selectors with cpl > 0 or ti == 1.
2214 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002215 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002216 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002217 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002218 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002219 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002220 vmx->host_state.fs_reload_needed = 0;
2221 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002222 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002223 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002224 }
Avi Kivity9581d442010-10-19 16:46:55 +02002225 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002226 if (!(vmx->host_state.gs_sel & 7))
2227 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002228 else {
2229 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002230 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002231 }
2232
2233#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002234 savesegment(ds, vmx->host_state.ds_sel);
2235 savesegment(es, vmx->host_state.es_sel);
2236#endif
2237
2238#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002239 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2240 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2241#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002242 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2243 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002244#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002245
2246#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002247 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2248 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002249 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002250#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002251 if (boot_cpu_has(X86_FEATURE_MPX))
2252 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002253 for (i = 0; i < vmx->save_nmsrs; ++i)
2254 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002255 vmx->guest_msrs[i].data,
2256 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002257}
2258
Avi Kivitya9b21b62008-06-24 11:48:49 +03002259static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002260{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002261 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002262 return;
2263
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002264 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002265 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002266#ifdef CONFIG_X86_64
2267 if (is_long_mode(&vmx->vcpu))
2268 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2269#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002270 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002271 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002272#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002273 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002274#else
2275 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002276#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002277 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002278 if (vmx->host_state.fs_reload_needed)
2279 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002280#ifdef CONFIG_X86_64
2281 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2282 loadsegment(ds, vmx->host_state.ds_sel);
2283 loadsegment(es, vmx->host_state.es_sel);
2284 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002285#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002286 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002287#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002288 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002289#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002290 if (vmx->host_state.msr_host_bndcfgs)
2291 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002292 /*
2293 * If the FPU is not active (through the host task or
2294 * the guest vcpu), then restore the cr0.TS bit.
2295 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002296 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002297 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002298 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002299}
2300
Avi Kivitya9b21b62008-06-24 11:48:49 +03002301static void vmx_load_host_state(struct vcpu_vmx *vmx)
2302{
2303 preempt_disable();
2304 __vmx_load_host_state(vmx);
2305 preempt_enable();
2306}
2307
Feng Wu28b835d2015-09-18 22:29:54 +08002308static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2309{
2310 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2311 struct pi_desc old, new;
2312 unsigned int dest;
2313
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002314 /*
2315 * In case of hot-plug or hot-unplug, we may have to undo
2316 * vmx_vcpu_pi_put even if there is no assigned device. And we
2317 * always keep PI.NDST up to date for simplicity: it makes the
2318 * code easier, and CPU migration is not a fast path.
2319 */
2320 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002321 return;
2322
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002323 /*
2324 * First handle the simple case where no cmpxchg is necessary; just
2325 * allow posting non-urgent interrupts.
2326 *
2327 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2328 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2329 * expects the VCPU to be on the blocked_vcpu_list that matches
2330 * PI.NDST.
2331 */
2332 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2333 vcpu->cpu == cpu) {
2334 pi_clear_sn(pi_desc);
2335 return;
2336 }
2337
2338 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002339 do {
2340 old.control = new.control = pi_desc->control;
2341
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002342 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002343
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002344 if (x2apic_enabled())
2345 new.ndst = dest;
2346 else
2347 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002348
Feng Wu28b835d2015-09-18 22:29:54 +08002349 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002350 } while (cmpxchg64(&pi_desc->control, old.control,
2351 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002352}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002353
Peter Feinerc95ba922016-08-17 09:36:47 -07002354static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2355{
2356 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2357 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2358}
2359
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360/*
2361 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2362 * vcpu mutex is already taken.
2363 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002364static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002366 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002367 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002368 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002370 if (!vmm_exclusive)
2371 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002372 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002373 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002375 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002376 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002377 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002378
2379 /*
2380 * Read loaded_vmcs->cpu should be before fetching
2381 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2382 * See the comments in __loaded_vmcs_clear().
2383 */
2384 smp_rmb();
2385
Nadav Har'Eld462b812011-05-24 15:26:10 +03002386 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2387 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002388 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002389 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002390 }
2391
2392 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2393 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2394 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002395 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002396 }
2397
2398 if (!already_loaded) {
2399 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2400 unsigned long sysenter_esp;
2401
2402 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002403
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404 /*
2405 * Linux uses per-cpu TSS and GDT, so set these when switching
2406 * processors.
2407 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002408 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002409 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410
2411 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2412 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002413
Nadav Har'Eld462b812011-05-24 15:26:10 +03002414 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002415 }
Feng Wu28b835d2015-09-18 22:29:54 +08002416
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002417 /* Setup TSC multiplier */
2418 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002419 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2420 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002421
Feng Wu28b835d2015-09-18 22:29:54 +08002422 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002423 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002424}
2425
2426static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2427{
2428 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2429
2430 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002431 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2432 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002433 return;
2434
2435 /* Set SN when the vCPU is preempted */
2436 if (vcpu->preempted)
2437 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438}
2439
2440static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2441{
Feng Wu28b835d2015-09-18 22:29:54 +08002442 vmx_vcpu_pi_put(vcpu);
2443
Avi Kivitya9b21b62008-06-24 11:48:49 +03002444 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002445 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002446 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2447 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002448 kvm_cpu_vmxoff();
2449 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002450}
2451
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002452static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2453{
Avi Kivity81231c62010-01-24 16:26:40 +02002454 ulong cr0;
2455
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002456 if (vcpu->fpu_active)
2457 return;
2458 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002459 cr0 = vmcs_readl(GUEST_CR0);
2460 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2461 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2462 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002463 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002464 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002465 if (is_guest_mode(vcpu))
2466 vcpu->arch.cr0_guest_owned_bits &=
2467 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002468 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002469}
2470
Avi Kivityedcafe32009-12-30 18:07:40 +02002471static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2472
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002473/*
2474 * Return the cr0 value that a nested guest would read. This is a combination
2475 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2476 * its hypervisor (cr0_read_shadow).
2477 */
2478static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2479{
2480 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2481 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2482}
2483static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2484{
2485 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2486 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2487}
2488
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002489static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2490{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002491 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2492 * set this *before* calling this function.
2493 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002494 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002495 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002496 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002497 vcpu->arch.cr0_guest_owned_bits = 0;
2498 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002499 if (is_guest_mode(vcpu)) {
2500 /*
2501 * L1's specified read shadow might not contain the TS bit,
2502 * so now that we turned on shadowing of this bit, we need to
2503 * set this bit of the shadow. Like in nested_vmx_run we need
2504 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2505 * up-to-date here because we just decached cr0.TS (and we'll
2506 * only update vmcs12->guest_cr0 on nested exit).
2507 */
2508 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2509 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2510 (vcpu->arch.cr0 & X86_CR0_TS);
2511 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2512 } else
2513 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002514}
2515
Avi Kivity6aa8b732006-12-10 02:21:36 -08002516static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2517{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002518 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002519
Avi Kivity6de12732011-03-07 12:51:22 +02002520 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2521 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2522 rflags = vmcs_readl(GUEST_RFLAGS);
2523 if (to_vmx(vcpu)->rmode.vm86_active) {
2524 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2525 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2526 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2527 }
2528 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002529 }
Avi Kivity6de12732011-03-07 12:51:22 +02002530 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531}
2532
2533static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2534{
Avi Kivity6de12732011-03-07 12:51:22 +02002535 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2536 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002537 if (to_vmx(vcpu)->rmode.vm86_active) {
2538 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002539 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002540 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 vmcs_writel(GUEST_RFLAGS, rflags);
2542}
2543
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002544static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2545{
2546 return to_vmx(vcpu)->guest_pkru;
2547}
2548
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002549static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002550{
2551 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2552 int ret = 0;
2553
2554 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002555 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002556 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002557 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002558
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002559 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002560}
2561
2562static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2563{
2564 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2565 u32 interruptibility = interruptibility_old;
2566
2567 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2568
Jan Kiszka48005f62010-02-19 19:38:07 +01002569 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002570 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002571 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002572 interruptibility |= GUEST_INTR_STATE_STI;
2573
2574 if ((interruptibility != interruptibility_old))
2575 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2576}
2577
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2579{
2580 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002582 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002584 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585
Glauber Costa2809f5d2009-05-12 16:21:05 -04002586 /* skipping an emulated instruction also counts */
2587 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588}
2589
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002590/*
2591 * KVM wants to inject page-faults which it got to the guest. This function
2592 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002593 */
Gleb Natapove011c662013-09-25 12:51:35 +03002594static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002595{
2596 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2597
Gleb Natapove011c662013-09-25 12:51:35 +03002598 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002599 return 0;
2600
Wanpeng Lia29fd272017-06-05 05:19:09 -07002601 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002602 vmcs_read32(VM_EXIT_INTR_INFO),
2603 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002604 return 1;
2605}
2606
Avi Kivity298101d2007-11-25 13:41:11 +02002607static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002608 bool has_error_code, u32 error_code,
2609 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002610{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002611 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002612 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002613
Gleb Natapove011c662013-09-25 12:51:35 +03002614 if (!reinject && is_guest_mode(vcpu) &&
2615 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002616 return;
2617
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002618 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002619 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002620 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2621 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002622
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002623 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002624 int inc_eip = 0;
2625 if (kvm_exception_is_soft(nr))
2626 inc_eip = vcpu->arch.event_exit_inst_len;
2627 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002628 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002629 return;
2630 }
2631
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002632 WARN_ON_ONCE(vmx->emulation_required);
2633
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002634 if (kvm_exception_is_soft(nr)) {
2635 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2636 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002637 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2638 } else
2639 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2640
2641 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002642}
2643
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002644static bool vmx_rdtscp_supported(void)
2645{
2646 return cpu_has_vmx_rdtscp();
2647}
2648
Mao, Junjiead756a12012-07-02 01:18:48 +00002649static bool vmx_invpcid_supported(void)
2650{
2651 return cpu_has_vmx_invpcid() && enable_ept;
2652}
2653
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654/*
Eddie Donga75beee2007-05-17 18:55:15 +03002655 * Swap MSR entry in host/guest MSR entry array.
2656 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002657static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002658{
Avi Kivity26bb0982009-09-07 11:14:12 +03002659 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002660
2661 tmp = vmx->guest_msrs[to];
2662 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2663 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002664}
2665
2666/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002667 * Set up the vmcs to automatically save and restore system
2668 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2669 * mode, as fiddling with msrs is very expensive.
2670 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002671static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002672{
Avi Kivity26bb0982009-09-07 11:14:12 +03002673 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002674
Eddie Donga75beee2007-05-17 18:55:15 +03002675 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002676#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002677 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002678 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002679 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002680 move_msr_up(vmx, index, save_nmsrs++);
2681 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002682 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002683 move_msr_up(vmx, index, save_nmsrs++);
2684 index = __find_msr_index(vmx, MSR_CSTAR);
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++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002687 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002688 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002689 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002690 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002691 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002692 * if efer.sce is enabled.
2693 */
Brian Gerst8c065852010-07-17 09:03:26 -04002694 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002695 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002696 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002697 }
Eddie Donga75beee2007-05-17 18:55:15 +03002698#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002699 index = __find_msr_index(vmx, MSR_EFER);
2700 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002701 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002702
Avi Kivity26bb0982009-09-07 11:14:12 +03002703 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002704
Yang Zhang8d146952013-01-25 10:18:50 +08002705 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002706 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002707}
2708
2709/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002711 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2712 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002714static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715{
2716 u64 host_tsc, tsc_offset;
2717
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002718 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002720 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721}
2722
2723/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002724 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002726static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002728 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002729 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002730 * We're here if L1 chose not to trap WRMSR to TSC. According
2731 * to the spec, this should set L1's TSC; The offset that L1
2732 * set for L2 remains unchanged, and still needs to be added
2733 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002734 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002735 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002736 /* recalculate vmcs02.TSC_OFFSET: */
2737 vmcs12 = get_vmcs12(vcpu);
2738 vmcs_write64(TSC_OFFSET, offset +
2739 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2740 vmcs12->tsc_offset : 0));
2741 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002742 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2743 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002744 vmcs_write64(TSC_OFFSET, offset);
2745 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746}
2747
Nadav Har'El801d3422011-05-25 23:02:23 +03002748static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2749{
2750 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2751 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2752}
2753
2754/*
2755 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2756 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2757 * all guests if the "nested" module option is off, and can also be disabled
2758 * for a single guest by disabling its VMX cpuid bit.
2759 */
2760static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2761{
2762 return nested && guest_cpuid_has_vmx(vcpu);
2763}
2764
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002766 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2767 * returned for the various VMX controls MSRs when nested VMX is enabled.
2768 * The same values should also be used to verify that vmcs12 control fields are
2769 * valid during nested entry from L1 to L2.
2770 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2771 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2772 * bit in the high half is on if the corresponding bit in the control field
2773 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002774 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002775static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002776{
2777 /*
2778 * Note that as a general rule, the high half of the MSRs (bits in
2779 * the control fields which may be 1) should be initialized by the
2780 * intersection of the underlying hardware's MSR (i.e., features which
2781 * can be supported) and the list of features we want to expose -
2782 * because they are known to be properly supported in our code.
2783 * Also, usually, the low half of the MSRs (bits which must be 1) can
2784 * be set to 0, meaning that L1 may turn off any of these bits. The
2785 * reason is that if one of these bits is necessary, it will appear
2786 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2787 * fields of vmcs01 and vmcs02, will turn these bits off - and
2788 * nested_vmx_exit_handled() will not pass related exits to L1.
2789 * These rules have exceptions below.
2790 */
2791
2792 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002793 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002794 vmx->nested.nested_vmx_pinbased_ctls_low,
2795 vmx->nested.nested_vmx_pinbased_ctls_high);
2796 vmx->nested.nested_vmx_pinbased_ctls_low |=
2797 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2798 vmx->nested.nested_vmx_pinbased_ctls_high &=
2799 PIN_BASED_EXT_INTR_MASK |
2800 PIN_BASED_NMI_EXITING |
2801 PIN_BASED_VIRTUAL_NMIS;
2802 vmx->nested.nested_vmx_pinbased_ctls_high |=
2803 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002804 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002805 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002806 vmx->nested.nested_vmx_pinbased_ctls_high |=
2807 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002808
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002809 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002810 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002811 vmx->nested.nested_vmx_exit_ctls_low,
2812 vmx->nested.nested_vmx_exit_ctls_high);
2813 vmx->nested.nested_vmx_exit_ctls_low =
2814 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002815
Wincy Vanb9c237b2015-02-03 23:56:30 +08002816 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002817#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002818 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002819#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002820 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002821 vmx->nested.nested_vmx_exit_ctls_high |=
2822 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002823 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002824 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2825
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002826 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002827 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002828
Jan Kiszka2996fca2014-06-16 13:59:43 +02002829 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002830 vmx->nested.nested_vmx_true_exit_ctls_low =
2831 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002832 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2833
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002834 /* entry controls */
2835 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002836 vmx->nested.nested_vmx_entry_ctls_low,
2837 vmx->nested.nested_vmx_entry_ctls_high);
2838 vmx->nested.nested_vmx_entry_ctls_low =
2839 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2840 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002841#ifdef CONFIG_X86_64
2842 VM_ENTRY_IA32E_MODE |
2843#endif
2844 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002845 vmx->nested.nested_vmx_entry_ctls_high |=
2846 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002847 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002848 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002849
Jan Kiszka2996fca2014-06-16 13:59:43 +02002850 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002851 vmx->nested.nested_vmx_true_entry_ctls_low =
2852 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002853 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2854
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002855 /* cpu-based controls */
2856 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002857 vmx->nested.nested_vmx_procbased_ctls_low,
2858 vmx->nested.nested_vmx_procbased_ctls_high);
2859 vmx->nested.nested_vmx_procbased_ctls_low =
2860 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2861 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002862 CPU_BASED_VIRTUAL_INTR_PENDING |
2863 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002864 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2865 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2866 CPU_BASED_CR3_STORE_EXITING |
2867#ifdef CONFIG_X86_64
2868 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2869#endif
2870 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002871 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2872 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2873 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2874 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002875 /*
2876 * We can allow some features even when not supported by the
2877 * hardware. For example, L1 can specify an MSR bitmap - and we
2878 * can use it to avoid exits to L1 - even when L0 runs L2
2879 * without MSR bitmaps.
2880 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002881 vmx->nested.nested_vmx_procbased_ctls_high |=
2882 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002883 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002884
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002885 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002886 vmx->nested.nested_vmx_true_procbased_ctls_low =
2887 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002888 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2889
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002890 /* secondary cpu-based controls */
2891 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002892 vmx->nested.nested_vmx_secondary_ctls_low,
2893 vmx->nested.nested_vmx_secondary_ctls_high);
2894 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2895 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002896 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002897 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002898 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002899 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002900 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002901 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002902 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002903 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002904
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002905 if (enable_ept) {
2906 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002907 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002908 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002909 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002910 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2911 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002912 if (cpu_has_vmx_ept_execute_only())
2913 vmx->nested.nested_vmx_ept_caps |=
2914 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002915 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002916 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2917 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002918 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002919 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002920
Paolo Bonzinief697a72016-03-18 16:58:38 +01002921 /*
2922 * Old versions of KVM use the single-context version without
2923 * checking for support, so declare that it is supported even
2924 * though it is treated as global context. The alternative is
2925 * not failing the single-context invvpid, and it is worse.
2926 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002927 if (enable_vpid)
2928 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01002929 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002930 else
2931 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002932
Radim Krčmář0790ec12015-03-17 14:02:32 +01002933 if (enable_unrestricted_guest)
2934 vmx->nested.nested_vmx_secondary_ctls_high |=
2935 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2936
Jan Kiszkac18911a2013-03-13 16:06:41 +01002937 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002938 rdmsr(MSR_IA32_VMX_MISC,
2939 vmx->nested.nested_vmx_misc_low,
2940 vmx->nested.nested_vmx_misc_high);
2941 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2942 vmx->nested.nested_vmx_misc_low |=
2943 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002944 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002945 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002946}
2947
2948static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2949{
2950 /*
2951 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2952 */
2953 return ((control & high) | low) == control;
2954}
2955
2956static inline u64 vmx_control_msr(u32 low, u32 high)
2957{
2958 return low | ((u64)high << 32);
2959}
2960
Jan Kiszkacae50132014-01-04 18:47:22 +01002961/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002962static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2963{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002964 struct vcpu_vmx *vmx = to_vmx(vcpu);
2965
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002966 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002967 case MSR_IA32_VMX_BASIC:
2968 /*
2969 * This MSR reports some information about VMX support. We
2970 * should return information about the VMX we emulate for the
2971 * guest, and the VMCS structure we give it - not about the
2972 * VMX support of the underlying hardware.
2973 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002974 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002975 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2976 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002977 if (cpu_has_vmx_basic_inout())
2978 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002979 break;
2980 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2981 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002982 *pdata = vmx_control_msr(
2983 vmx->nested.nested_vmx_pinbased_ctls_low,
2984 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002985 break;
2986 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002987 *pdata = vmx_control_msr(
2988 vmx->nested.nested_vmx_true_procbased_ctls_low,
2989 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002990 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002991 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002992 *pdata = vmx_control_msr(
2993 vmx->nested.nested_vmx_procbased_ctls_low,
2994 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002995 break;
2996 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002997 *pdata = vmx_control_msr(
2998 vmx->nested.nested_vmx_true_exit_ctls_low,
2999 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003000 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003001 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003002 *pdata = vmx_control_msr(
3003 vmx->nested.nested_vmx_exit_ctls_low,
3004 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003005 break;
3006 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003007 *pdata = vmx_control_msr(
3008 vmx->nested.nested_vmx_true_entry_ctls_low,
3009 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003010 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003011 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003012 *pdata = vmx_control_msr(
3013 vmx->nested.nested_vmx_entry_ctls_low,
3014 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003015 break;
3016 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003017 *pdata = vmx_control_msr(
3018 vmx->nested.nested_vmx_misc_low,
3019 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003020 break;
3021 /*
3022 * These MSRs specify bits which the guest must keep fixed (on or off)
3023 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3024 * We picked the standard core2 setting.
3025 */
3026#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3027#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3028 case MSR_IA32_VMX_CR0_FIXED0:
3029 *pdata = VMXON_CR0_ALWAYSON;
3030 break;
3031 case MSR_IA32_VMX_CR0_FIXED1:
3032 *pdata = -1ULL;
3033 break;
3034 case MSR_IA32_VMX_CR4_FIXED0:
3035 *pdata = VMXON_CR4_ALWAYSON;
3036 break;
3037 case MSR_IA32_VMX_CR4_FIXED1:
3038 *pdata = -1ULL;
3039 break;
3040 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02003041 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003042 break;
3043 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003044 *pdata = vmx_control_msr(
3045 vmx->nested.nested_vmx_secondary_ctls_low,
3046 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003047 break;
3048 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003049 *pdata = vmx->nested.nested_vmx_ept_caps |
3050 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003051 break;
3052 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003053 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003054 }
3055
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003056 return 0;
3057}
3058
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003059static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3060 uint64_t val)
3061{
3062 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3063
3064 return !(val & ~valid_bits);
3065}
3066
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003067/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 * Reads an msr value (of 'msr_index') into 'pdata'.
3069 * Returns 0 on success, non-0 otherwise.
3070 * Assumes vcpu_load() was already called.
3071 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003072static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003073{
Avi Kivity26bb0982009-09-07 11:14:12 +03003074 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003076 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003077#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003078 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003079 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 break;
3081 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003082 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003084 case MSR_KERNEL_GS_BASE:
3085 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003086 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003087 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003088#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003090 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303091 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003092 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003094 case MSR_IA32_SPEC_CTRL:
3095 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003096 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003097 return 1;
3098
3099 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3100 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003101 case MSR_IA32_ARCH_CAPABILITIES:
3102 if (!msr_info->host_initiated &&
3103 !guest_cpuid_has_arch_capabilities(vcpu))
3104 return 1;
3105 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3106 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003108 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 break;
3110 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003111 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112 break;
3113 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003114 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003116 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003117 if (!kvm_mpx_supported() ||
3118 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003119 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003120 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003121 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003122 case MSR_IA32_MCG_EXT_CTL:
3123 if (!msr_info->host_initiated &&
3124 !(to_vmx(vcpu)->msr_ia32_feature_control &
3125 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003126 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003127 msr_info->data = vcpu->arch.mcg_ext_ctl;
3128 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003129 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003130 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003131 break;
3132 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3133 if (!nested_vmx_allowed(vcpu))
3134 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003135 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003136 case MSR_IA32_XSS:
3137 if (!vmx_xsaves_supported())
3138 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003139 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003140 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003141 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003142 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003143 return 1;
3144 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003146 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003147 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003148 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003149 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003151 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 }
3153
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 return 0;
3155}
3156
Jan Kiszkacae50132014-01-04 18:47:22 +01003157static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3158
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159/*
3160 * Writes msr value into into the appropriate "register".
3161 * Returns 0 on success, non-0 otherwise.
3162 * Assumes vcpu_load() was already called.
3163 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003164static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003166 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003167 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003168 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003169 u32 msr_index = msr_info->index;
3170 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003171
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003173 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003174 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003175 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003176#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003178 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 vmcs_writel(GUEST_FS_BASE, data);
3180 break;
3181 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003182 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183 vmcs_writel(GUEST_GS_BASE, data);
3184 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003185 case MSR_KERNEL_GS_BASE:
3186 vmx_load_host_state(vmx);
3187 vmx->msr_guest_kernel_gs_base = data;
3188 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189#endif
3190 case MSR_IA32_SYSENTER_CS:
3191 vmcs_write32(GUEST_SYSENTER_CS, data);
3192 break;
3193 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003194 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 break;
3196 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003197 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003198 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003199 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003200 if (!kvm_mpx_supported() ||
3201 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003202 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003203 if (is_noncanonical_address(data & PAGE_MASK) ||
3204 (data & MSR_IA32_BNDCFGS_RSVD))
3205 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003206 vmcs_write64(GUEST_BNDCFGS, data);
3207 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303208 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003209 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003211 case MSR_IA32_SPEC_CTRL:
3212 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003213 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003214 return 1;
3215
3216 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003217 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003218 return 1;
3219
3220 vmx->spec_ctrl = data;
3221
3222 if (!data)
3223 break;
3224
3225 /*
3226 * For non-nested:
3227 * When it's written (to non-zero) for the first time, pass
3228 * it through.
3229 *
3230 * For nested:
3231 * The handling of the MSR bitmap for L2 guests is done in
3232 * nested_vmx_merge_msr_bitmap. We should not touch the
3233 * vmcs02.msr_bitmap here since it gets completely overwritten
3234 * in the merging. We update the vmcs01 here for L1 as well
3235 * since it will end up touching the MSR anyway now.
3236 */
3237 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3238 MSR_IA32_SPEC_CTRL,
3239 MSR_TYPE_RW);
3240 break;
Ashok Raj70131292018-02-01 22:59:43 +01003241 case MSR_IA32_PRED_CMD:
3242 if (!msr_info->host_initiated &&
3243 !guest_cpuid_has_ibpb(vcpu))
3244 return 1;
3245
3246 if (data & ~PRED_CMD_IBPB)
3247 return 1;
3248
3249 if (!data)
3250 break;
3251
3252 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3253
3254 /*
3255 * For non-nested:
3256 * When it's written (to non-zero) for the first time, pass
3257 * it through.
3258 *
3259 * For nested:
3260 * The handling of the MSR bitmap for L2 guests is done in
3261 * nested_vmx_merge_msr_bitmap. We should not touch the
3262 * vmcs02.msr_bitmap here since it gets completely overwritten
3263 * in the merging.
3264 */
3265 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3266 MSR_TYPE_W);
3267 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003268 case MSR_IA32_ARCH_CAPABILITIES:
3269 if (!msr_info->host_initiated)
3270 return 1;
3271 vmx->arch_capabilities = data;
3272 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003273 case MSR_IA32_CR_PAT:
3274 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003275 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3276 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003277 vmcs_write64(GUEST_IA32_PAT, data);
3278 vcpu->arch.pat = data;
3279 break;
3280 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003281 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003282 break;
Will Auldba904632012-11-29 12:42:50 -08003283 case MSR_IA32_TSC_ADJUST:
3284 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003285 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003286 case MSR_IA32_MCG_EXT_CTL:
3287 if ((!msr_info->host_initiated &&
3288 !(to_vmx(vcpu)->msr_ia32_feature_control &
3289 FEATURE_CONTROL_LMCE)) ||
3290 (data & ~MCG_EXT_CTL_LMCE_EN))
3291 return 1;
3292 vcpu->arch.mcg_ext_ctl = data;
3293 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003294 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003295 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003296 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003297 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3298 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003299 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003300 if (msr_info->host_initiated && data == 0)
3301 vmx_leave_nested(vcpu);
3302 break;
3303 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3304 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003305 case MSR_IA32_XSS:
3306 if (!vmx_xsaves_supported())
3307 return 1;
3308 /*
3309 * The only supported bit as of Skylake is bit 8, but
3310 * it is not supported on KVM.
3311 */
3312 if (data != 0)
3313 return 1;
3314 vcpu->arch.ia32_xss = data;
3315 if (vcpu->arch.ia32_xss != host_xss)
3316 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3317 vcpu->arch.ia32_xss, host_xss);
3318 else
3319 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3320 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003321 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003322 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003323 return 1;
3324 /* Check reserved bit, higher 32 bits should be zero */
3325 if ((data >> 32) != 0)
3326 return 1;
3327 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003328 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003329 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003330 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003331 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003332 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003333 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3334 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003335 ret = kvm_set_shared_msr(msr->index, msr->data,
3336 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003337 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003338 if (ret)
3339 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003340 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003341 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003343 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344 }
3345
Eddie Dong2cc51562007-05-21 07:28:09 +03003346 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347}
3348
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003349static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003350{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003351 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3352 switch (reg) {
3353 case VCPU_REGS_RSP:
3354 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3355 break;
3356 case VCPU_REGS_RIP:
3357 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3358 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003359 case VCPU_EXREG_PDPTR:
3360 if (enable_ept)
3361 ept_save_pdptrs(vcpu);
3362 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003363 default:
3364 break;
3365 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366}
3367
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368static __init int cpu_has_kvm_support(void)
3369{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003370 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371}
3372
3373static __init int vmx_disabled_by_bios(void)
3374{
3375 u64 msr;
3376
3377 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003378 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003379 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003380 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3381 && tboot_enabled())
3382 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003383 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003384 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003385 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003386 && !tboot_enabled()) {
3387 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003388 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003389 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003390 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003391 /* launched w/o TXT and VMX disabled */
3392 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3393 && !tboot_enabled())
3394 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003395 }
3396
3397 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003398}
3399
Dongxiao Xu7725b892010-05-11 18:29:38 +08003400static void kvm_cpu_vmxon(u64 addr)
3401{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003402 intel_pt_handle_vmx(1);
3403
Dongxiao Xu7725b892010-05-11 18:29:38 +08003404 asm volatile (ASM_VMX_VMXON_RAX
3405 : : "a"(&addr), "m"(addr)
3406 : "memory", "cc");
3407}
3408
Radim Krčmář13a34e02014-08-28 15:13:03 +02003409static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410{
3411 int cpu = raw_smp_processor_id();
3412 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003413 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003414
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003415 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003416 return -EBUSY;
3417
Nadav Har'Eld462b812011-05-24 15:26:10 +03003418 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003419 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3420 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003421
3422 /*
3423 * Now we can enable the vmclear operation in kdump
3424 * since the loaded_vmcss_on_cpu list on this cpu
3425 * has been initialized.
3426 *
3427 * Though the cpu is not in VMX operation now, there
3428 * is no problem to enable the vmclear operation
3429 * for the loaded_vmcss_on_cpu list is empty!
3430 */
3431 crash_enable_local_vmclear(cpu);
3432
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003434
3435 test_bits = FEATURE_CONTROL_LOCKED;
3436 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3437 if (tboot_enabled())
3438 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3439
3440 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003442 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3443 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003444 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003445
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003446 if (vmm_exclusive) {
3447 kvm_cpu_vmxon(phys_addr);
3448 ept_sync_global();
3449 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003450
Christoph Lameter89cbc762014-08-17 12:30:40 -05003451 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003452
Alexander Graf10474ae2009-09-15 11:37:46 +02003453 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454}
3455
Nadav Har'Eld462b812011-05-24 15:26:10 +03003456static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003457{
3458 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003459 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003460
Nadav Har'Eld462b812011-05-24 15:26:10 +03003461 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3462 loaded_vmcss_on_cpu_link)
3463 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003464}
3465
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003466
3467/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3468 * tricks.
3469 */
3470static void kvm_cpu_vmxoff(void)
3471{
3472 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003473
3474 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003475}
3476
Radim Krčmář13a34e02014-08-28 15:13:03 +02003477static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003479 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003480 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003481 kvm_cpu_vmxoff();
3482 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003483 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484}
3485
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003486static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003487 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488{
3489 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003490 u32 ctl = ctl_min | ctl_opt;
3491
3492 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3493
3494 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3495 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3496
3497 /* Ensure minimum (required) set of control bits are supported. */
3498 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003499 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003500
3501 *result = ctl;
3502 return 0;
3503}
3504
Avi Kivity110312c2010-12-21 12:54:20 +02003505static __init bool allow_1_setting(u32 msr, u32 ctl)
3506{
3507 u32 vmx_msr_low, vmx_msr_high;
3508
3509 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3510 return vmx_msr_high & ctl;
3511}
3512
Yang, Sheng002c7f72007-07-31 14:23:01 +03003513static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003514{
3515 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003516 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003517 u32 _pin_based_exec_control = 0;
3518 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003519 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003520 u32 _vmexit_control = 0;
3521 u32 _vmentry_control = 0;
3522
Raghavendra K T10166742012-02-07 23:19:20 +05303523 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003524#ifdef CONFIG_X86_64
3525 CPU_BASED_CR8_LOAD_EXITING |
3526 CPU_BASED_CR8_STORE_EXITING |
3527#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003528 CPU_BASED_CR3_LOAD_EXITING |
3529 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003530 CPU_BASED_USE_IO_BITMAPS |
3531 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003532 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003533 CPU_BASED_MWAIT_EXITING |
3534 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003535 CPU_BASED_INVLPG_EXITING |
3536 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003537
Sheng Yangf78e0e22007-10-29 09:40:42 +08003538 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003539 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003540 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003541 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3542 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003543 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003544#ifdef CONFIG_X86_64
3545 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3546 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3547 ~CPU_BASED_CR8_STORE_EXITING;
3548#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003549 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003550 min2 = 0;
3551 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003552 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003553 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003554 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003555 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003556 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003557 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003558 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003559 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003560 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003561 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003562 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003563 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003564 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003565 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003566 if (adjust_vmx_controls(min2, opt2,
3567 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003568 &_cpu_based_2nd_exec_control) < 0)
3569 return -EIO;
3570 }
3571#ifndef CONFIG_X86_64
3572 if (!(_cpu_based_2nd_exec_control &
3573 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3574 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3575#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003576
3577 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3578 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003579 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003580 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3581 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003582
Sheng Yangd56f5462008-04-25 10:13:16 +08003583 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003584 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3585 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003586 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3587 CPU_BASED_CR3_STORE_EXITING |
3588 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003589 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3590 vmx_capability.ept, vmx_capability.vpid);
3591 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003592
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003593 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003594#ifdef CONFIG_X86_64
3595 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3596#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003597 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003598 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003599 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3600 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003601 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003602
Yang Zhang01e439b2013-04-11 19:25:12 +08003603 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003604 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3605 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003606 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3607 &_pin_based_exec_control) < 0)
3608 return -EIO;
3609
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003610 if (cpu_has_broken_vmx_preemption_timer())
3611 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003612 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003613 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003614 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3615
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003616 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003617 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003618 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3619 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003620 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003621
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003622 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003623
3624 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3625 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003626 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003627
3628#ifdef CONFIG_X86_64
3629 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3630 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003631 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003632#endif
3633
3634 /* Require Write-Back (WB) memory type for VMCS accesses. */
3635 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003636 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003637
Yang, Sheng002c7f72007-07-31 14:23:01 +03003638 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003639 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003640 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003641 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003642
Yang, Sheng002c7f72007-07-31 14:23:01 +03003643 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3644 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003645 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003646 vmcs_conf->vmexit_ctrl = _vmexit_control;
3647 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003648
Avi Kivity110312c2010-12-21 12:54:20 +02003649 cpu_has_load_ia32_efer =
3650 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3651 VM_ENTRY_LOAD_IA32_EFER)
3652 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3653 VM_EXIT_LOAD_IA32_EFER);
3654
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003655 cpu_has_load_perf_global_ctrl =
3656 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3657 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3658 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3659 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3660
3661 /*
3662 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003663 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003664 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3665 *
3666 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3667 *
3668 * AAK155 (model 26)
3669 * AAP115 (model 30)
3670 * AAT100 (model 37)
3671 * BC86,AAY89,BD102 (model 44)
3672 * BA97 (model 46)
3673 *
3674 */
3675 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3676 switch (boot_cpu_data.x86_model) {
3677 case 26:
3678 case 30:
3679 case 37:
3680 case 44:
3681 case 46:
3682 cpu_has_load_perf_global_ctrl = false;
3683 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3684 "does not work properly. Using workaround\n");
3685 break;
3686 default:
3687 break;
3688 }
3689 }
3690
Borislav Petkov782511b2016-04-04 22:25:03 +02003691 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003692 rdmsrl(MSR_IA32_XSS, host_xss);
3693
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003694 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003695}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696
3697static struct vmcs *alloc_vmcs_cpu(int cpu)
3698{
3699 int node = cpu_to_node(cpu);
3700 struct page *pages;
3701 struct vmcs *vmcs;
3702
Vlastimil Babka96db8002015-09-08 15:03:50 -07003703 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003704 if (!pages)
3705 return NULL;
3706 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003707 memset(vmcs, 0, vmcs_config.size);
3708 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003709 return vmcs;
3710}
3711
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712static void free_vmcs(struct vmcs *vmcs)
3713{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003714 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003715}
3716
Nadav Har'Eld462b812011-05-24 15:26:10 +03003717/*
3718 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3719 */
3720static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3721{
3722 if (!loaded_vmcs->vmcs)
3723 return;
3724 loaded_vmcs_clear(loaded_vmcs);
3725 free_vmcs(loaded_vmcs->vmcs);
3726 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003727 if (loaded_vmcs->msr_bitmap)
3728 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003729 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003730}
3731
Paolo Bonziniff546f92018-01-11 12:16:15 +01003732static struct vmcs *alloc_vmcs(void)
3733{
3734 return alloc_vmcs_cpu(raw_smp_processor_id());
3735}
3736
3737static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3738{
3739 loaded_vmcs->vmcs = alloc_vmcs();
3740 if (!loaded_vmcs->vmcs)
3741 return -ENOMEM;
3742
3743 loaded_vmcs->shadow_vmcs = NULL;
3744 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003745
3746 if (cpu_has_vmx_msr_bitmap()) {
3747 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3748 if (!loaded_vmcs->msr_bitmap)
3749 goto out_vmcs;
3750 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3751 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003752 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003753
3754out_vmcs:
3755 free_loaded_vmcs(loaded_vmcs);
3756 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003757}
3758
Sam Ravnborg39959582007-06-01 00:47:13 -07003759static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760{
3761 int cpu;
3762
Zachary Amsden3230bb42009-09-29 11:38:37 -10003763 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003764 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003765 per_cpu(vmxarea, cpu) = NULL;
3766 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003767}
3768
Bandan Dasfe2b2012014-04-21 15:20:14 -04003769static void init_vmcs_shadow_fields(void)
3770{
3771 int i, j;
3772
3773 /* No checks for read only fields yet */
3774
3775 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3776 switch (shadow_read_write_fields[i]) {
3777 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003778 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003779 continue;
3780 break;
3781 default:
3782 break;
3783 }
3784
3785 if (j < i)
3786 shadow_read_write_fields[j] =
3787 shadow_read_write_fields[i];
3788 j++;
3789 }
3790 max_shadow_read_write_fields = j;
3791
3792 /* shadowed fields guest access without vmexit */
3793 for (i = 0; i < max_shadow_read_write_fields; i++) {
3794 clear_bit(shadow_read_write_fields[i],
3795 vmx_vmwrite_bitmap);
3796 clear_bit(shadow_read_write_fields[i],
3797 vmx_vmread_bitmap);
3798 }
3799 for (i = 0; i < max_shadow_read_only_fields; i++)
3800 clear_bit(shadow_read_only_fields[i],
3801 vmx_vmread_bitmap);
3802}
3803
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804static __init int alloc_kvm_area(void)
3805{
3806 int cpu;
3807
Zachary Amsden3230bb42009-09-29 11:38:37 -10003808 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003809 struct vmcs *vmcs;
3810
3811 vmcs = alloc_vmcs_cpu(cpu);
3812 if (!vmcs) {
3813 free_kvm_area();
3814 return -ENOMEM;
3815 }
3816
3817 per_cpu(vmxarea, cpu) = vmcs;
3818 }
3819 return 0;
3820}
3821
Gleb Natapov14168782013-01-21 15:36:49 +02003822static bool emulation_required(struct kvm_vcpu *vcpu)
3823{
3824 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3825}
3826
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003827static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003828 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003829{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003830 if (!emulate_invalid_guest_state) {
3831 /*
3832 * CS and SS RPL should be equal during guest entry according
3833 * to VMX spec, but in reality it is not always so. Since vcpu
3834 * is in the middle of the transition from real mode to
3835 * protected mode it is safe to assume that RPL 0 is a good
3836 * default value.
3837 */
3838 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003839 save->selector &= ~SEGMENT_RPL_MASK;
3840 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003841 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003843 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844}
3845
3846static void enter_pmode(struct kvm_vcpu *vcpu)
3847{
3848 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003849 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850
Gleb Natapovd99e4152012-12-20 16:57:45 +02003851 /*
3852 * Update real mode segment cache. It may be not up-to-date if sement
3853 * register was written while vcpu was in a guest mode.
3854 */
3855 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3856 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3857 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3858 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3859 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3860 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3861
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003862 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003863
Avi Kivity2fb92db2011-04-27 19:42:18 +03003864 vmx_segment_cache_clear(vmx);
3865
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003866 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003867
3868 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003869 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3870 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871 vmcs_writel(GUEST_RFLAGS, flags);
3872
Rusty Russell66aee912007-07-17 23:34:16 +10003873 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3874 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875
3876 update_exception_bitmap(vcpu);
3877
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003878 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3879 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3880 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3881 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3882 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3883 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003884}
3885
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003886static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003887{
Mathias Krause772e0312012-08-30 01:30:19 +02003888 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003889 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003890
Gleb Natapovd99e4152012-12-20 16:57:45 +02003891 var.dpl = 0x3;
3892 if (seg == VCPU_SREG_CS)
3893 var.type = 0x3;
3894
3895 if (!emulate_invalid_guest_state) {
3896 var.selector = var.base >> 4;
3897 var.base = var.base & 0xffff0;
3898 var.limit = 0xffff;
3899 var.g = 0;
3900 var.db = 0;
3901 var.present = 1;
3902 var.s = 1;
3903 var.l = 0;
3904 var.unusable = 0;
3905 var.type = 0x3;
3906 var.avl = 0;
3907 if (save->base & 0xf)
3908 printk_once(KERN_WARNING "kvm: segment base is not "
3909 "paragraph aligned when entering "
3910 "protected mode (seg=%d)", seg);
3911 }
3912
3913 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05003914 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003915 vmcs_write32(sf->limit, var.limit);
3916 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917}
3918
3919static void enter_rmode(struct kvm_vcpu *vcpu)
3920{
3921 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003922 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003924 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3925 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3926 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3927 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3928 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003929 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3930 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003931
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003932 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933
Gleb Natapov776e58e2011-03-13 12:34:27 +02003934 /*
3935 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003936 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003937 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003938 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003939 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3940 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003941
Avi Kivity2fb92db2011-04-27 19:42:18 +03003942 vmx_segment_cache_clear(vmx);
3943
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003944 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003945 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3947
3948 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003949 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003951 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952
3953 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003954 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955 update_exception_bitmap(vcpu);
3956
Gleb Natapovd99e4152012-12-20 16:57:45 +02003957 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3958 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3959 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3960 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3961 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3962 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003963
Eddie Dong8668a3c2007-10-10 14:26:45 +08003964 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965}
3966
Amit Shah401d10d2009-02-20 22:53:37 +05303967static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3968{
3969 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003970 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3971
3972 if (!msr)
3973 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303974
Avi Kivity44ea2b12009-09-06 15:55:37 +03003975 /*
3976 * Force kernel_gs_base reloading before EFER changes, as control
3977 * of this msr depends on is_long_mode().
3978 */
3979 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003980 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303981 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003982 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303983 msr->data = efer;
3984 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003985 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303986
3987 msr->data = efer & ~EFER_LME;
3988 }
3989 setup_msrs(vmx);
3990}
3991
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003992#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003993
3994static void enter_lmode(struct kvm_vcpu *vcpu)
3995{
3996 u32 guest_tr_ar;
3997
Avi Kivity2fb92db2011-04-27 19:42:18 +03003998 vmx_segment_cache_clear(to_vmx(vcpu));
3999
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004001 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02004002 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
4003 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004004 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004005 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
4006 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004007 }
Avi Kivityda38f432010-07-06 11:30:49 +03004008 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004009}
4010
4011static void exit_lmode(struct kvm_vcpu *vcpu)
4012{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004013 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
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
4017#endif
4018
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004019static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004020{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004021 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004022 if (enable_ept) {
4023 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4024 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004025 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004026 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004027}
4028
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004029static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4030{
4031 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4032}
4033
Jim Mattson8386ff52017-03-16 13:53:59 -07004034static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4035{
4036 if (enable_ept)
4037 vmx_flush_tlb(vcpu);
4038}
4039
Avi Kivitye8467fd2009-12-29 18:43:06 +02004040static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4041{
4042 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4043
4044 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4045 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4046}
4047
Avi Kivityaff48ba2010-12-05 18:56:11 +02004048static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4049{
4050 if (enable_ept && is_paging(vcpu))
4051 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4052 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4053}
4054
Anthony Liguori25c4c272007-04-27 09:29:21 +03004055static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004056{
Avi Kivityfc78f512009-12-07 12:16:48 +02004057 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4058
4059 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4060 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004061}
4062
Sheng Yang14394422008-04-28 12:24:45 +08004063static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4064{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004065 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4066
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004067 if (!test_bit(VCPU_EXREG_PDPTR,
4068 (unsigned long *)&vcpu->arch.regs_dirty))
4069 return;
4070
Sheng Yang14394422008-04-28 12:24:45 +08004071 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004072 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4073 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4074 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4075 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004076 }
4077}
4078
Avi Kivity8f5d5492009-05-31 18:41:29 +03004079static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4080{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004081 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4082
Avi Kivity8f5d5492009-05-31 18:41:29 +03004083 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004084 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4085 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4086 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4087 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004088 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004089
4090 __set_bit(VCPU_EXREG_PDPTR,
4091 (unsigned long *)&vcpu->arch.regs_avail);
4092 __set_bit(VCPU_EXREG_PDPTR,
4093 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004094}
4095
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004096static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004097
4098static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4099 unsigned long cr0,
4100 struct kvm_vcpu *vcpu)
4101{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004102 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4103 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004104 if (!(cr0 & X86_CR0_PG)) {
4105 /* From paging/starting to nonpaging */
4106 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004107 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004108 (CPU_BASED_CR3_LOAD_EXITING |
4109 CPU_BASED_CR3_STORE_EXITING));
4110 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004111 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004112 } else if (!is_paging(vcpu)) {
4113 /* From nonpaging to paging */
4114 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004115 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004116 ~(CPU_BASED_CR3_LOAD_EXITING |
4117 CPU_BASED_CR3_STORE_EXITING));
4118 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004119 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004120 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004121
4122 if (!(cr0 & X86_CR0_WP))
4123 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004124}
4125
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4127{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004128 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004129 unsigned long hw_cr0;
4130
Gleb Natapov50378782013-02-04 16:00:28 +02004131 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004132 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004133 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004134 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004135 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004136
Gleb Natapov218e7632013-01-21 15:36:45 +02004137 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4138 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139
Gleb Natapov218e7632013-01-21 15:36:45 +02004140 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4141 enter_rmode(vcpu);
4142 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004144#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004145 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004146 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004148 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149 exit_lmode(vcpu);
4150 }
4151#endif
4152
Avi Kivity089d0342009-03-23 18:26:32 +02004153 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004154 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4155
Avi Kivity02daab22009-12-30 12:40:26 +02004156 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004157 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004158
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004160 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004161 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004162
4163 /* depends on vcpu->arch.cr0 to be set to a new value */
4164 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165}
4166
Sheng Yang14394422008-04-28 12:24:45 +08004167static u64 construct_eptp(unsigned long root_hpa)
4168{
4169 u64 eptp;
4170
4171 /* TODO write the value reading from MSR */
4172 eptp = VMX_EPT_DEFAULT_MT |
4173 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004174 if (enable_ept_ad_bits)
4175 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004176 eptp |= (root_hpa & PAGE_MASK);
4177
4178 return eptp;
4179}
4180
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4182{
Sheng Yang14394422008-04-28 12:24:45 +08004183 unsigned long guest_cr3;
4184 u64 eptp;
4185
4186 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004187 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004188 eptp = construct_eptp(cr3);
4189 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004190 if (is_paging(vcpu) || is_guest_mode(vcpu))
4191 guest_cr3 = kvm_read_cr3(vcpu);
4192 else
4193 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004194 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004195 }
4196
Sheng Yang2384d2b2008-01-17 15:14:33 +08004197 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004198 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004199}
4200
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004201static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004202{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004203 /*
4204 * Pass through host's Machine Check Enable value to hw_cr4, which
4205 * is in force while we are in guest mode. Do not let guests control
4206 * this bit, even if host CR4.MCE == 0.
4207 */
4208 unsigned long hw_cr4 =
4209 (cr4_read_shadow() & X86_CR4_MCE) |
4210 (cr4 & ~X86_CR4_MCE) |
4211 (to_vmx(vcpu)->rmode.vm86_active ?
4212 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004213
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004214 if (cr4 & X86_CR4_VMXE) {
4215 /*
4216 * To use VMXON (and later other VMX instructions), a guest
4217 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4218 * So basically the check on whether to allow nested VMX
4219 * is here.
4220 */
4221 if (!nested_vmx_allowed(vcpu))
4222 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004223 }
4224 if (to_vmx(vcpu)->nested.vmxon &&
4225 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004226 return 1;
4227
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004228 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004229 if (enable_ept) {
4230 if (!is_paging(vcpu)) {
4231 hw_cr4 &= ~X86_CR4_PAE;
4232 hw_cr4 |= X86_CR4_PSE;
4233 } else if (!(cr4 & X86_CR4_PAE)) {
4234 hw_cr4 &= ~X86_CR4_PAE;
4235 }
4236 }
Sheng Yang14394422008-04-28 12:24:45 +08004237
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004238 if (!enable_unrestricted_guest && !is_paging(vcpu))
4239 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004240 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4241 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4242 * to be manually disabled when guest switches to non-paging
4243 * mode.
4244 *
4245 * If !enable_unrestricted_guest, the CPU is always running
4246 * with CR0.PG=1 and CR4 needs to be modified.
4247 * If enable_unrestricted_guest, the CPU automatically
4248 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004249 */
Huaitong Handdba2622016-03-22 16:51:15 +08004250 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004251
Sheng Yang14394422008-04-28 12:24:45 +08004252 vmcs_writel(CR4_READ_SHADOW, cr4);
4253 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004254 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004255}
4256
Avi Kivity6aa8b732006-12-10 02:21:36 -08004257static void vmx_get_segment(struct kvm_vcpu *vcpu,
4258 struct kvm_segment *var, int seg)
4259{
Avi Kivitya9179492011-01-03 14:28:52 +02004260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261 u32 ar;
4262
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004263 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004264 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004265 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004266 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004267 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004268 var->base = vmx_read_guest_seg_base(vmx, seg);
4269 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4270 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004271 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004272 var->base = vmx_read_guest_seg_base(vmx, seg);
4273 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4274 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4275 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004276 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277 var->type = ar & 15;
4278 var->s = (ar >> 4) & 1;
4279 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004280 /*
4281 * Some userspaces do not preserve unusable property. Since usable
4282 * segment has to be present according to VMX spec we can use present
4283 * property to amend userspace bug by making unusable segment always
4284 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4285 * segment as unusable.
4286 */
4287 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004288 var->avl = (ar >> 12) & 1;
4289 var->l = (ar >> 13) & 1;
4290 var->db = (ar >> 14) & 1;
4291 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004292}
4293
Avi Kivitya9179492011-01-03 14:28:52 +02004294static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4295{
Avi Kivitya9179492011-01-03 14:28:52 +02004296 struct kvm_segment s;
4297
4298 if (to_vmx(vcpu)->rmode.vm86_active) {
4299 vmx_get_segment(vcpu, &s, seg);
4300 return s.base;
4301 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004302 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004303}
4304
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004305static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004306{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004307 struct vcpu_vmx *vmx = to_vmx(vcpu);
4308
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004309 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004310 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004311 else {
4312 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004313 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004314 }
Avi Kivity69c73022011-03-07 15:26:44 +02004315}
4316
Avi Kivity653e3102007-05-07 10:55:37 +03004317static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004318{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319 u32 ar;
4320
Avi Kivityf0495f92012-06-07 17:06:10 +03004321 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322 ar = 1 << 16;
4323 else {
4324 ar = var->type & 15;
4325 ar |= (var->s & 1) << 4;
4326 ar |= (var->dpl & 3) << 5;
4327 ar |= (var->present & 1) << 7;
4328 ar |= (var->avl & 1) << 12;
4329 ar |= (var->l & 1) << 13;
4330 ar |= (var->db & 1) << 14;
4331 ar |= (var->g & 1) << 15;
4332 }
Avi Kivity653e3102007-05-07 10:55:37 +03004333
4334 return ar;
4335}
4336
4337static void vmx_set_segment(struct kvm_vcpu *vcpu,
4338 struct kvm_segment *var, int seg)
4339{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004340 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004341 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004342
Avi Kivity2fb92db2011-04-27 19:42:18 +03004343 vmx_segment_cache_clear(vmx);
4344
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004345 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4346 vmx->rmode.segs[seg] = *var;
4347 if (seg == VCPU_SREG_TR)
4348 vmcs_write16(sf->selector, var->selector);
4349 else if (var->s)
4350 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004351 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004352 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004353
Avi Kivity653e3102007-05-07 10:55:37 +03004354 vmcs_writel(sf->base, var->base);
4355 vmcs_write32(sf->limit, var->limit);
4356 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004357
4358 /*
4359 * Fix the "Accessed" bit in AR field of segment registers for older
4360 * qemu binaries.
4361 * IA32 arch specifies that at the time of processor reset the
4362 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004363 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004364 * state vmexit when "unrestricted guest" mode is turned on.
4365 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4366 * tree. Newer qemu binaries with that qemu fix would not need this
4367 * kvm hack.
4368 */
4369 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004370 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004371
Gleb Natapovf924d662012-12-12 19:10:55 +02004372 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004373
4374out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004375 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004376}
4377
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4379{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004380 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004381
4382 *db = (ar >> 14) & 1;
4383 *l = (ar >> 13) & 1;
4384}
4385
Gleb Natapov89a27f42010-02-16 10:51:48 +02004386static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004388 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4389 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004390}
4391
Gleb Natapov89a27f42010-02-16 10:51:48 +02004392static void vmx_set_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 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4395 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396}
4397
Gleb Natapov89a27f42010-02-16 10:51:48 +02004398static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004400 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4401 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004402}
4403
Gleb Natapov89a27f42010-02-16 10:51:48 +02004404static void vmx_set_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 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4407 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004408}
4409
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004410static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4411{
4412 struct kvm_segment var;
4413 u32 ar;
4414
4415 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004416 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004417 if (seg == VCPU_SREG_CS)
4418 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004419 ar = vmx_segment_access_rights(&var);
4420
4421 if (var.base != (var.selector << 4))
4422 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004423 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004424 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004425 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004426 return false;
4427
4428 return true;
4429}
4430
4431static bool code_segment_valid(struct kvm_vcpu *vcpu)
4432{
4433 struct kvm_segment cs;
4434 unsigned int cs_rpl;
4435
4436 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004437 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004438
Avi Kivity1872a3f2009-01-04 23:26:52 +02004439 if (cs.unusable)
4440 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004441 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004442 return false;
4443 if (!cs.s)
4444 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004445 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004446 if (cs.dpl > cs_rpl)
4447 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004448 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004449 if (cs.dpl != cs_rpl)
4450 return false;
4451 }
4452 if (!cs.present)
4453 return false;
4454
4455 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4456 return true;
4457}
4458
4459static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4460{
4461 struct kvm_segment ss;
4462 unsigned int ss_rpl;
4463
4464 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004465 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004466
Avi Kivity1872a3f2009-01-04 23:26:52 +02004467 if (ss.unusable)
4468 return true;
4469 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004470 return false;
4471 if (!ss.s)
4472 return false;
4473 if (ss.dpl != ss_rpl) /* DPL != RPL */
4474 return false;
4475 if (!ss.present)
4476 return false;
4477
4478 return true;
4479}
4480
4481static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4482{
4483 struct kvm_segment var;
4484 unsigned int rpl;
4485
4486 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004487 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004488
Avi Kivity1872a3f2009-01-04 23:26:52 +02004489 if (var.unusable)
4490 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004491 if (!var.s)
4492 return false;
4493 if (!var.present)
4494 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004495 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004496 if (var.dpl < rpl) /* DPL < RPL */
4497 return false;
4498 }
4499
4500 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4501 * rights flags
4502 */
4503 return true;
4504}
4505
4506static bool tr_valid(struct kvm_vcpu *vcpu)
4507{
4508 struct kvm_segment tr;
4509
4510 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4511
Avi Kivity1872a3f2009-01-04 23:26:52 +02004512 if (tr.unusable)
4513 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004514 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004515 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004516 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004517 return false;
4518 if (!tr.present)
4519 return false;
4520
4521 return true;
4522}
4523
4524static bool ldtr_valid(struct kvm_vcpu *vcpu)
4525{
4526 struct kvm_segment ldtr;
4527
4528 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4529
Avi Kivity1872a3f2009-01-04 23:26:52 +02004530 if (ldtr.unusable)
4531 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004532 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004533 return false;
4534 if (ldtr.type != 2)
4535 return false;
4536 if (!ldtr.present)
4537 return false;
4538
4539 return true;
4540}
4541
4542static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4543{
4544 struct kvm_segment cs, ss;
4545
4546 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4547 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4548
Nadav Amitb32a9912015-03-29 16:33:04 +03004549 return ((cs.selector & SEGMENT_RPL_MASK) ==
4550 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004551}
4552
4553/*
4554 * Check if guest state is valid. Returns true if valid, false if
4555 * not.
4556 * We assume that registers are always usable
4557 */
4558static bool guest_state_valid(struct kvm_vcpu *vcpu)
4559{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004560 if (enable_unrestricted_guest)
4561 return true;
4562
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004563 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004564 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004565 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4566 return false;
4567 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4568 return false;
4569 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4570 return false;
4571 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4572 return false;
4573 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4574 return false;
4575 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4576 return false;
4577 } else {
4578 /* protected mode guest state checks */
4579 if (!cs_ss_rpl_check(vcpu))
4580 return false;
4581 if (!code_segment_valid(vcpu))
4582 return false;
4583 if (!stack_segment_valid(vcpu))
4584 return false;
4585 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4586 return false;
4587 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4588 return false;
4589 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4590 return false;
4591 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4592 return false;
4593 if (!tr_valid(vcpu))
4594 return false;
4595 if (!ldtr_valid(vcpu))
4596 return false;
4597 }
4598 /* TODO:
4599 * - Add checks on RIP
4600 * - Add checks on RFLAGS
4601 */
4602
4603 return true;
4604}
4605
Mike Dayd77c26f2007-10-08 09:02:08 -04004606static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004607{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004608 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004609 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004610 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004611
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004612 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004613 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004614 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4615 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004616 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004617 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004618 r = kvm_write_guest_page(kvm, fn++, &data,
4619 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004620 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004621 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004622 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4623 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004624 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004625 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4626 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004627 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004628 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004629 r = kvm_write_guest_page(kvm, fn, &data,
4630 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4631 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004632out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004633 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004634 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635}
4636
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004637static int init_rmode_identity_map(struct kvm *kvm)
4638{
Tang Chenf51770e2014-09-16 18:41:59 +08004639 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004640 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004641 u32 tmp;
4642
Avi Kivity089d0342009-03-23 18:26:32 +02004643 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004644 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004645
4646 /* Protect kvm->arch.ept_identity_pagetable_done. */
4647 mutex_lock(&kvm->slots_lock);
4648
Tang Chenf51770e2014-09-16 18:41:59 +08004649 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004650 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004651
Sheng Yangb927a3c2009-07-21 10:42:48 +08004652 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004653
4654 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004655 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004656 goto out2;
4657
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004658 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004659 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4660 if (r < 0)
4661 goto out;
4662 /* Set up identity-mapping pagetable for EPT in real mode */
4663 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4664 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4665 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4666 r = kvm_write_guest_page(kvm, identity_map_pfn,
4667 &tmp, i * sizeof(tmp), sizeof(tmp));
4668 if (r < 0)
4669 goto out;
4670 }
4671 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004672
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004673out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004674 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004675
4676out2:
4677 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004678 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004679}
4680
Avi Kivity6aa8b732006-12-10 02:21:36 -08004681static void seg_setup(int seg)
4682{
Mathias Krause772e0312012-08-30 01:30:19 +02004683 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004684 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004685
4686 vmcs_write16(sf->selector, 0);
4687 vmcs_writel(sf->base, 0);
4688 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004689 ar = 0x93;
4690 if (seg == VCPU_SREG_CS)
4691 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004692
4693 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694}
4695
Sheng Yangf78e0e22007-10-29 09:40:42 +08004696static int alloc_apic_access_page(struct kvm *kvm)
4697{
Xiao Guangrong44841412012-09-07 14:14:20 +08004698 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004699 int r = 0;
4700
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004701 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004702 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004703 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004704 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4705 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004706 if (r)
4707 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004708
Tang Chen73a6d942014-09-11 13:38:00 +08004709 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004710 if (is_error_page(page)) {
4711 r = -EFAULT;
4712 goto out;
4713 }
4714
Tang Chenc24ae0d2014-09-24 15:57:58 +08004715 /*
4716 * Do not pin the page in memory, so that memory hot-unplug
4717 * is able to migrate it.
4718 */
4719 put_page(page);
4720 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004721out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004722 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004723 return r;
4724}
4725
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004726static int alloc_identity_pagetable(struct kvm *kvm)
4727{
Tang Chena255d472014-09-16 18:41:58 +08004728 /* Called with kvm->slots_lock held. */
4729
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004730 int r = 0;
4731
Tang Chena255d472014-09-16 18:41:58 +08004732 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4733
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004734 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4735 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004736
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004737 return r;
4738}
4739
Wanpeng Li991e7a02015-09-16 17:30:05 +08004740static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004741{
4742 int vpid;
4743
Avi Kivity919818a2009-03-23 18:01:29 +02004744 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004745 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004746 spin_lock(&vmx_vpid_lock);
4747 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004748 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004749 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004750 else
4751 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004752 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004753 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004754}
4755
Wanpeng Li991e7a02015-09-16 17:30:05 +08004756static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004757{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004758 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004759 return;
4760 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004761 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004762 spin_unlock(&vmx_vpid_lock);
4763}
4764
Paolo Bonzini6236b782018-01-16 16:51:18 +01004765static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4766 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004767{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004768 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004769
4770 if (!cpu_has_vmx_msr_bitmap())
4771 return;
4772
4773 /*
4774 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4775 * have the write-low and read-high bitmap offsets the wrong way round.
4776 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4777 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004778 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004779 if (type & MSR_TYPE_R)
4780 /* read-low */
4781 __clear_bit(msr, msr_bitmap + 0x000 / f);
4782
4783 if (type & MSR_TYPE_W)
4784 /* write-low */
4785 __clear_bit(msr, msr_bitmap + 0x800 / f);
4786
Sheng Yang25c5f222008-03-28 13:18:56 +08004787 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4788 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004789 if (type & MSR_TYPE_R)
4790 /* read-high */
4791 __clear_bit(msr, msr_bitmap + 0x400 / f);
4792
4793 if (type & MSR_TYPE_W)
4794 /* write-high */
4795 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4796
4797 }
4798}
4799
Paolo Bonzini6236b782018-01-16 16:51:18 +01004800static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4801 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004802{
4803 int f = sizeof(unsigned long);
4804
4805 if (!cpu_has_vmx_msr_bitmap())
4806 return;
4807
4808 /*
4809 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4810 * have the write-low and read-high bitmap offsets the wrong way round.
4811 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4812 */
4813 if (msr <= 0x1fff) {
4814 if (type & MSR_TYPE_R)
4815 /* read-low */
4816 __set_bit(msr, msr_bitmap + 0x000 / f);
4817
4818 if (type & MSR_TYPE_W)
4819 /* write-low */
4820 __set_bit(msr, msr_bitmap + 0x800 / f);
4821
4822 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4823 msr &= 0x1fff;
4824 if (type & MSR_TYPE_R)
4825 /* read-high */
4826 __set_bit(msr, msr_bitmap + 0x400 / f);
4827
4828 if (type & MSR_TYPE_W)
4829 /* write-high */
4830 __set_bit(msr, msr_bitmap + 0xc00 / f);
4831
Sheng Yang25c5f222008-03-28 13:18:56 +08004832 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004833}
4834
Paolo Bonzini6236b782018-01-16 16:51:18 +01004835static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4836 u32 msr, int type, bool value)
4837{
4838 if (value)
4839 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4840 else
4841 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4842}
4843
Wincy Vanf2b93282015-02-03 23:56:03 +08004844/*
4845 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4846 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4847 */
4848static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4849 unsigned long *msr_bitmap_nested,
4850 u32 msr, int type)
4851{
4852 int f = sizeof(unsigned long);
4853
4854 if (!cpu_has_vmx_msr_bitmap()) {
4855 WARN_ON(1);
4856 return;
4857 }
4858
4859 /*
4860 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4861 * have the write-low and read-high bitmap offsets the wrong way round.
4862 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4863 */
4864 if (msr <= 0x1fff) {
4865 if (type & MSR_TYPE_R &&
4866 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4867 /* read-low */
4868 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4869
4870 if (type & MSR_TYPE_W &&
4871 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4872 /* write-low */
4873 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4874
4875 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4876 msr &= 0x1fff;
4877 if (type & MSR_TYPE_R &&
4878 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4879 /* read-high */
4880 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4881
4882 if (type & MSR_TYPE_W &&
4883 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4884 /* write-high */
4885 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4886
4887 }
4888}
4889
Paolo Bonzini6236b782018-01-16 16:51:18 +01004890static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004891{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004892 u8 mode = 0;
4893
4894 if (cpu_has_secondary_exec_ctrls() &&
4895 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4896 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4897 mode |= MSR_BITMAP_MODE_X2APIC;
4898 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4899 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4900 }
4901
4902 if (is_long_mode(vcpu))
4903 mode |= MSR_BITMAP_MODE_LM;
4904
4905 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004906}
4907
Paolo Bonzini6236b782018-01-16 16:51:18 +01004908#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
4909
4910static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4911 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08004912{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004913 int msr;
4914
4915 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4916 unsigned word = msr / BITS_PER_LONG;
4917 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4918 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4919 }
4920
4921 if (mode & MSR_BITMAP_MODE_X2APIC) {
4922 /*
4923 * TPR reads and writes can be virtualized even if virtual interrupt
4924 * delivery is not in use.
4925 */
4926 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4927 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4928 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4929 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4930 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4931 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004932 }
Yang Zhang8d146952013-01-25 10:18:50 +08004933}
4934
Paolo Bonzini6236b782018-01-16 16:51:18 +01004935static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08004936{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004937 struct vcpu_vmx *vmx = to_vmx(vcpu);
4938 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4939 u8 mode = vmx_msr_bitmap_mode(vcpu);
4940 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004941
Paolo Bonzini6236b782018-01-16 16:51:18 +01004942 if (!changed)
4943 return;
4944
4945 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
4946 !(mode & MSR_BITMAP_MODE_LM));
4947
4948 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4949 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4950
4951 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02004952}
4953
Andrey Smetanind62caab2015-11-10 15:36:33 +03004954static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004955{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004956 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004957}
4958
David Matlackb7649e12017-08-01 14:00:40 -07004959static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
4960{
4961 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4962 gfn_t gfn;
4963
4964 /*
4965 * Don't need to mark the APIC access page dirty; it is never
4966 * written to by the CPU during APIC virtualization.
4967 */
4968
4969 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
4970 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
4971 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4972 }
4973
4974 if (nested_cpu_has_posted_intr(vmcs12)) {
4975 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
4976 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4977 }
4978}
4979
4980
David Hildenbrand1edccf22017-01-25 11:58:58 +01004981static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08004982{
4983 struct vcpu_vmx *vmx = to_vmx(vcpu);
4984 int max_irr;
4985 void *vapic_page;
4986 u16 status;
4987
David Matlackb7649e12017-08-01 14:00:40 -07004988 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
4989 return;
Wincy Van705699a2015-02-03 23:58:17 +08004990
David Matlackb7649e12017-08-01 14:00:40 -07004991 vmx->nested.pi_pending = false;
4992 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4993 return;
Wincy Van705699a2015-02-03 23:58:17 +08004994
David Matlackb7649e12017-08-01 14:00:40 -07004995 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
4996 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08004997 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08004998 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4999 kunmap(vmx->nested.virtual_apic_page);
5000
5001 status = vmcs_read16(GUEST_INTR_STATUS);
5002 if ((u8)max_irr > ((u8)status & 0xff)) {
5003 status &= ~0xff;
5004 status |= (u8)max_irr;
5005 vmcs_write16(GUEST_INTR_STATUS, status);
5006 }
5007 }
David Matlackb7649e12017-08-01 14:00:40 -07005008
5009 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005010}
5011
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005012static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
5013{
5014#ifdef CONFIG_SMP
5015 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005016 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005017 * The vector of interrupt to be delivered to vcpu had
5018 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005019 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005020 * Following cases will be reached in this block, and
5021 * we always send a notification event in all cases as
5022 * explained below.
5023 *
5024 * Case 1: vcpu keeps in non-root mode. Sending a
5025 * notification event posts the interrupt to vcpu.
5026 *
5027 * Case 2: vcpu exits to root mode and is still
5028 * runnable. PIR will be synced to vIRR before the
5029 * next vcpu entry. Sending a notification event in
5030 * this case has no effect, as vcpu is not in root
5031 * mode.
5032 *
5033 * Case 3: vcpu exits to root mode and is blocked.
5034 * vcpu_block() has already synced PIR to vIRR and
5035 * never blocks vcpu if vIRR is not cleared. Therefore,
5036 * a blocked vcpu here does not wait for any requested
5037 * interrupts in PIR, and sending a notification event
5038 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005039 */
Feng Wu28b835d2015-09-18 22:29:54 +08005040
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005041 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
5042 POSTED_INTR_VECTOR);
5043 return true;
5044 }
5045#endif
5046 return false;
5047}
5048
Wincy Van705699a2015-02-03 23:58:17 +08005049static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5050 int vector)
5051{
5052 struct vcpu_vmx *vmx = to_vmx(vcpu);
5053
5054 if (is_guest_mode(vcpu) &&
5055 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005056 /*
5057 * If a posted intr is not recognized by hardware,
5058 * we will accomplish it in the next vmentry.
5059 */
5060 vmx->nested.pi_pending = true;
5061 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02005062 /* the PIR and ON have been set by L1. */
5063 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
5064 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005065 return 0;
5066 }
5067 return -1;
5068}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005069/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005070 * Send interrupt to vcpu via posted interrupt way.
5071 * 1. If target vcpu is running(non-root mode), send posted interrupt
5072 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5073 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5074 * interrupt from PIR in next vmentry.
5075 */
5076static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5077{
5078 struct vcpu_vmx *vmx = to_vmx(vcpu);
5079 int r;
5080
Wincy Van705699a2015-02-03 23:58:17 +08005081 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5082 if (!r)
5083 return;
5084
Yang Zhanga20ed542013-04-11 19:25:15 +08005085 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5086 return;
5087
5088 r = pi_test_and_set_on(&vmx->pi_desc);
5089 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005090 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005091 kvm_vcpu_kick(vcpu);
5092}
5093
5094static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5095{
5096 struct vcpu_vmx *vmx = to_vmx(vcpu);
5097
5098 if (!pi_test_and_clear_on(&vmx->pi_desc))
5099 return;
5100
5101 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5102}
5103
Avi Kivity6aa8b732006-12-10 02:21:36 -08005104/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005105 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5106 * will not change in the lifetime of the guest.
5107 * Note that host-state that does change is set elsewhere. E.g., host-state
5108 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5109 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005110static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005111{
5112 u32 low32, high32;
5113 unsigned long tmpl;
5114 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005115 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005116
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005117 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005118 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5119
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005120 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005121 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005122 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5123 vmx->host_state.vmcs_host_cr4 = cr4;
5124
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005125 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005126#ifdef CONFIG_X86_64
5127 /*
5128 * Load null selectors, so we can avoid reloading them in
5129 * __vmx_load_host_state(), in case userspace uses the null selectors
5130 * too (the expected case).
5131 */
5132 vmcs_write16(HOST_DS_SELECTOR, 0);
5133 vmcs_write16(HOST_ES_SELECTOR, 0);
5134#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005135 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5136 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005137#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005138 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5139 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5140
5141 native_store_idt(&dt);
5142 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005143 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005144
Avi Kivity83287ea422012-09-16 15:10:57 +03005145 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005146
5147 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5148 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5149 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5150 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5151
5152 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5153 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5154 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5155 }
5156}
5157
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005158static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5159{
5160 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5161 if (enable_ept)
5162 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005163 if (is_guest_mode(&vmx->vcpu))
5164 vmx->vcpu.arch.cr4_guest_owned_bits &=
5165 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005166 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5167}
5168
Yang Zhang01e439b2013-04-11 19:25:12 +08005169static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5170{
5171 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5172
Andrey Smetanind62caab2015-11-10 15:36:33 +03005173 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005174 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005175 /* Enable the preemption timer dynamically */
5176 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005177 return pin_based_exec_ctrl;
5178}
5179
Andrey Smetanind62caab2015-11-10 15:36:33 +03005180static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5181{
5182 struct vcpu_vmx *vmx = to_vmx(vcpu);
5183
5184 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005185 if (cpu_has_secondary_exec_ctrls()) {
5186 if (kvm_vcpu_apicv_active(vcpu))
5187 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5188 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5189 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5190 else
5191 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5192 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5193 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5194 }
5195
5196 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005197 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005198}
5199
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005200static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5201{
5202 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005203
5204 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5205 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5206
Paolo Bonzini35754c92015-07-29 12:05:37 +02005207 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005208 exec_control &= ~CPU_BASED_TPR_SHADOW;
5209#ifdef CONFIG_X86_64
5210 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5211 CPU_BASED_CR8_LOAD_EXITING;
5212#endif
5213 }
5214 if (!enable_ept)
5215 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5216 CPU_BASED_CR3_LOAD_EXITING |
5217 CPU_BASED_INVLPG_EXITING;
5218 return exec_control;
5219}
5220
5221static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5222{
5223 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005224 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005225 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5226 if (vmx->vpid == 0)
5227 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5228 if (!enable_ept) {
5229 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5230 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005231 /* Enable INVPCID for non-ept guests may cause performance regression. */
5232 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005233 }
5234 if (!enable_unrestricted_guest)
5235 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5236 if (!ple_gap)
5237 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005238 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005239 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5240 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005241 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005242 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5243 (handle_vmptrld).
5244 We can NOT enable shadow_vmcs here because we don't have yet
5245 a current VMCS12
5246 */
5247 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005248
5249 if (!enable_pml)
5250 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005251
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005252 return exec_control;
5253}
5254
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005255static void ept_set_mmio_spte_mask(void)
5256{
5257 /*
5258 * EPT Misconfigurations can be generated if the value of bits 2:0
5259 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005260 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005261 * spte.
5262 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005263 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005264}
5265
Wanpeng Lif53cd632014-12-02 19:14:58 +08005266#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005267/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005268 * Sets up the vmcs for emulated real mode.
5269 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005270static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005271{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005272#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005273 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005274#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005275 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005276
Avi Kivity6aa8b732006-12-10 02:21:36 -08005277 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005278 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5279 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005280
Abel Gordon4607c2d2013-04-18 14:35:55 +03005281 if (enable_shadow_vmcs) {
5282 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5283 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5284 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005285 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005286 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005287
Avi Kivity6aa8b732006-12-10 02:21:36 -08005288 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5289
Avi Kivity6aa8b732006-12-10 02:21:36 -08005290 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005291 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005292 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005293
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005294 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005295
Dan Williamsdfa169b2016-06-02 11:17:24 -07005296 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005297 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5298 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005299 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005300
Andrey Smetanind62caab2015-11-10 15:36:33 +03005301 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005302 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5303 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5304 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5305 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5306
5307 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005308
Li RongQing0bcf2612015-12-03 13:29:34 +08005309 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005310 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005311 }
5312
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005313 if (ple_gap) {
5314 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005315 vmx->ple_window = ple_window;
5316 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005317 }
5318
Xiao Guangrongc3707952011-07-12 03:28:04 +08005319 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5320 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005321 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5322
Avi Kivity9581d442010-10-19 16:46:55 +02005323 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5324 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005325 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005326#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005327 rdmsrl(MSR_FS_BASE, a);
5328 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5329 rdmsrl(MSR_GS_BASE, a);
5330 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5331#else
5332 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5333 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5334#endif
5335
Eddie Dong2cc51562007-05-21 07:28:09 +03005336 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5337 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005338 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03005339 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005340 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005341
Radim Krčmář74545702015-04-27 15:11:25 +02005342 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5343 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005344
Paolo Bonzini03916db2014-07-24 14:21:57 +02005345 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005346 u32 index = vmx_msr_index[i];
5347 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005348 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005349
5350 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5351 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005352 if (wrmsr_safe(index, data_low, data_high) < 0)
5353 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005354 vmx->guest_msrs[j].index = i;
5355 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005356 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005357 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005358 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01005360 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5361 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005362
5363 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005364
5365 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005366 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005367
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005368 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005369 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005370
Wanpeng Lif53cd632014-12-02 19:14:58 +08005371 if (vmx_xsaves_supported())
5372 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5373
Peter Feiner4e595162016-07-07 14:49:58 -07005374 if (enable_pml) {
5375 ASSERT(vmx->pml_pg);
5376 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5377 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5378 }
5379
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005380 return 0;
5381}
5382
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005383static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005384{
5385 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005386 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005387 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005388
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005389 vmx->rmode.vm86_active = 0;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005390 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005391
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005392 vmx->soft_vnmi_blocked = 0;
5393
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005394 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005395 kvm_set_cr8(vcpu, 0);
5396
5397 if (!init_event) {
5398 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5399 MSR_IA32_APICBASE_ENABLE;
5400 if (kvm_vcpu_is_reset_bsp(vcpu))
5401 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5402 apic_base_msr.host_initiated = true;
5403 kvm_set_apic_base(vcpu, &apic_base_msr);
5404 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005405
Avi Kivity2fb92db2011-04-27 19:42:18 +03005406 vmx_segment_cache_clear(vmx);
5407
Avi Kivity5706be02008-08-20 15:07:31 +03005408 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005409 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005410 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005411
5412 seg_setup(VCPU_SREG_DS);
5413 seg_setup(VCPU_SREG_ES);
5414 seg_setup(VCPU_SREG_FS);
5415 seg_setup(VCPU_SREG_GS);
5416 seg_setup(VCPU_SREG_SS);
5417
5418 vmcs_write16(GUEST_TR_SELECTOR, 0);
5419 vmcs_writel(GUEST_TR_BASE, 0);
5420 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5421 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5422
5423 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5424 vmcs_writel(GUEST_LDTR_BASE, 0);
5425 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5426 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5427
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005428 if (!init_event) {
5429 vmcs_write32(GUEST_SYSENTER_CS, 0);
5430 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5431 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5432 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5433 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005434
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005435 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005436 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005437
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005438 vmcs_writel(GUEST_GDTR_BASE, 0);
5439 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5440
5441 vmcs_writel(GUEST_IDTR_BASE, 0);
5442 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5443
Anthony Liguori443381a2010-12-06 10:53:38 -06005444 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005445 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005446 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005447
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005448 setup_msrs(vmx);
5449
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5451
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005452 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005453 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005454 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005455 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005456 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005457 vmcs_write32(TPR_THRESHOLD, 0);
5458 }
5459
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005460 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005461
Andrey Smetanind62caab2015-11-10 15:36:33 +03005462 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005463 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5464
Sheng Yang2384d2b2008-01-17 15:14:33 +08005465 if (vmx->vpid != 0)
5466 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5467
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005468 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005469 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005470 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005471 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005472 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005473 vmx_fpu_activate(vcpu);
5474 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005475
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005476 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005477}
5478
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005479/*
5480 * In nested virtualization, check if L1 asked to exit on external interrupts.
5481 * For most existing hypervisors, this will always return true.
5482 */
5483static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5484{
5485 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5486 PIN_BASED_EXT_INTR_MASK;
5487}
5488
Bandan Das77b0f5d2014-04-19 18:17:45 -04005489/*
5490 * In nested virtualization, check if L1 has set
5491 * VM_EXIT_ACK_INTR_ON_EXIT
5492 */
5493static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5494{
5495 return get_vmcs12(vcpu)->vm_exit_controls &
5496 VM_EXIT_ACK_INTR_ON_EXIT;
5497}
5498
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005499static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5500{
5501 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5502 PIN_BASED_NMI_EXITING;
5503}
5504
Jan Kiszkac9a79532014-03-07 20:03:15 +01005505static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005506{
5507 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005508
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005509 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5510 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5511 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5512}
5513
Jan Kiszkac9a79532014-03-07 20:03:15 +01005514static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005515{
5516 u32 cpu_based_vm_exec_control;
5517
Jan Kiszkac9a79532014-03-07 20:03:15 +01005518 if (!cpu_has_virtual_nmis() ||
5519 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5520 enable_irq_window(vcpu);
5521 return;
5522 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005523
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005524 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5525 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5526 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5527}
5528
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005529static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005530{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005531 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005532 uint32_t intr;
5533 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005534
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005535 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005536
Avi Kivityfa89a812008-09-01 15:57:51 +03005537 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005538 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005539 int inc_eip = 0;
5540 if (vcpu->arch.interrupt.soft)
5541 inc_eip = vcpu->arch.event_exit_inst_len;
5542 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005543 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005544 return;
5545 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005546 intr = irq | INTR_INFO_VALID_MASK;
5547 if (vcpu->arch.interrupt.soft) {
5548 intr |= INTR_TYPE_SOFT_INTR;
5549 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5550 vmx->vcpu.arch.event_exit_inst_len);
5551 } else
5552 intr |= INTR_TYPE_EXT_INTR;
5553 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005554}
5555
Sheng Yangf08864b2008-05-15 18:23:25 +08005556static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5557{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005558 struct vcpu_vmx *vmx = to_vmx(vcpu);
5559
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005560 if (!is_guest_mode(vcpu)) {
5561 if (!cpu_has_virtual_nmis()) {
5562 /*
5563 * Tracking the NMI-blocked state in software is built upon
5564 * finding the next open IRQ window. This, in turn, depends on
5565 * well-behaving guests: They have to keep IRQs disabled at
5566 * least as long as the NMI handler runs. Otherwise we may
5567 * cause NMI nesting, maybe breaking the guest. But as this is
5568 * highly unlikely, we can live with the residual risk.
5569 */
5570 vmx->soft_vnmi_blocked = 1;
5571 vmx->vnmi_blocked_time = 0;
5572 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005573
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005574 ++vcpu->stat.nmi_injections;
5575 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005576 }
5577
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005578 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005579 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005580 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005581 return;
5582 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005583
Sheng Yangf08864b2008-05-15 18:23:25 +08005584 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5585 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005586}
5587
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005588static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5589{
5590 if (!cpu_has_virtual_nmis())
5591 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005592 if (to_vmx(vcpu)->nmi_known_unmasked)
5593 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005594 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005595}
5596
5597static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5598{
5599 struct vcpu_vmx *vmx = to_vmx(vcpu);
5600
5601 if (!cpu_has_virtual_nmis()) {
5602 if (vmx->soft_vnmi_blocked != masked) {
5603 vmx->soft_vnmi_blocked = masked;
5604 vmx->vnmi_blocked_time = 0;
5605 }
5606 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005607 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005608 if (masked)
5609 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5610 GUEST_INTR_STATE_NMI);
5611 else
5612 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5613 GUEST_INTR_STATE_NMI);
5614 }
5615}
5616
Jan Kiszka2505dc92013-04-14 12:12:47 +02005617static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5618{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005619 if (to_vmx(vcpu)->nested.nested_run_pending)
5620 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005621
Jan Kiszka2505dc92013-04-14 12:12:47 +02005622 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5623 return 0;
5624
5625 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5626 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5627 | GUEST_INTR_STATE_NMI));
5628}
5629
Gleb Natapov78646122009-03-23 12:12:11 +02005630static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5631{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005632 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5633 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005634 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5635 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005636}
5637
Izik Eiduscbc94022007-10-25 00:29:55 +02005638static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5639{
5640 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005641
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005642 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5643 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005644 if (ret)
5645 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005646 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005647 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005648}
5649
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005650static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005651{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005652 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005653 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005654 /*
5655 * Update instruction length as we may reinject the exception
5656 * from user space while in guest debugging mode.
5657 */
5658 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5659 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005660 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005661 return false;
5662 /* fall through */
5663 case DB_VECTOR:
5664 if (vcpu->guest_debug &
5665 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5666 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005667 /* fall through */
5668 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005669 case OF_VECTOR:
5670 case BR_VECTOR:
5671 case UD_VECTOR:
5672 case DF_VECTOR:
5673 case SS_VECTOR:
5674 case GP_VECTOR:
5675 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005676 return true;
5677 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005678 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005679 return false;
5680}
5681
5682static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5683 int vec, u32 err_code)
5684{
5685 /*
5686 * Instruction with address size override prefix opcode 0x67
5687 * Cause the #SS fault with 0 error code in VM86 mode.
5688 */
5689 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5690 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5691 if (vcpu->arch.halt_request) {
5692 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005693 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005694 }
5695 return 1;
5696 }
5697 return 0;
5698 }
5699
5700 /*
5701 * Forward all other exceptions that are valid in real mode.
5702 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5703 * the required debugging infrastructure rework.
5704 */
5705 kvm_queue_exception(vcpu, vec);
5706 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005707}
5708
Andi Kleena0861c02009-06-08 17:37:09 +08005709/*
5710 * Trigger machine check on the host. We assume all the MSRs are already set up
5711 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5712 * We pass a fake environment to the machine check handler because we want
5713 * the guest to be always treated like user space, no matter what context
5714 * it used internally.
5715 */
5716static void kvm_machine_check(void)
5717{
5718#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5719 struct pt_regs regs = {
5720 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5721 .flags = X86_EFLAGS_IF,
5722 };
5723
5724 do_machine_check(&regs, 0);
5725#endif
5726}
5727
Avi Kivity851ba692009-08-24 11:10:17 +03005728static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005729{
5730 /* already handled by vcpu_run */
5731 return 1;
5732}
5733
Avi Kivity851ba692009-08-24 11:10:17 +03005734static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005735{
Avi Kivity1155f762007-11-22 11:30:47 +02005736 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005737 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005738 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005739 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005740 u32 vect_info;
5741 enum emulation_result er;
5742
Avi Kivity1155f762007-11-22 11:30:47 +02005743 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005744 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005745
Andi Kleena0861c02009-06-08 17:37:09 +08005746 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005747 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005748
Jim Mattson3f618a02016-12-12 11:01:37 -08005749 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005750 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005751
5752 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005753 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005754 return 1;
5755 }
5756
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005757 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005758 if (is_guest_mode(vcpu)) {
5759 kvm_queue_exception(vcpu, UD_VECTOR);
5760 return 1;
5761 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005762 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005763 if (er == EMULATE_USER_EXIT)
5764 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005765 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005766 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005767 return 1;
5768 }
5769
Avi Kivity6aa8b732006-12-10 02:21:36 -08005770 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005771 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005772 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005773
5774 /*
5775 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5776 * MMIO, it is better to report an internal error.
5777 * See the comments in vmx_handle_exit.
5778 */
5779 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5780 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5781 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5782 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005783 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005784 vcpu->run->internal.data[0] = vect_info;
5785 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005786 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005787 return 0;
5788 }
5789
Avi Kivity6aa8b732006-12-10 02:21:36 -08005790 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005791 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005792 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005793 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005794 trace_kvm_page_fault(cr2, error_code);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02005795 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005796
Gleb Natapov3298b752009-05-11 13:35:46 +03005797 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005798 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005799 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005800 }
5801
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005802 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005803
5804 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5805 return handle_rmode_exception(vcpu, ex_no, error_code);
5806
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005807 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005808 case AC_VECTOR:
5809 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5810 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005811 case DB_VECTOR:
5812 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5813 if (!(vcpu->guest_debug &
5814 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005815 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005816 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005817 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005818 skip_emulated_instruction(vcpu);
5819
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005820 kvm_queue_exception(vcpu, DB_VECTOR);
5821 return 1;
5822 }
5823 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5824 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5825 /* fall through */
5826 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005827 /*
5828 * Update instruction length as we may reinject #BP from
5829 * user space while in guest debugging mode. Reading it for
5830 * #DB as well causes no harm, it is not used in that case.
5831 */
5832 vmx->vcpu.arch.event_exit_inst_len =
5833 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005834 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005835 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005836 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5837 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005838 break;
5839 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005840 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5841 kvm_run->ex.exception = ex_no;
5842 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005843 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005844 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005845 return 0;
5846}
5847
Avi Kivity851ba692009-08-24 11:10:17 +03005848static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005849{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005850 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005851 return 1;
5852}
5853
Avi Kivity851ba692009-08-24 11:10:17 +03005854static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005855{
Avi Kivity851ba692009-08-24 11:10:17 +03005856 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005857 return 0;
5858}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005859
Avi Kivity851ba692009-08-24 11:10:17 +03005860static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005861{
He, Qingbfdaab02007-09-12 14:18:28 +08005862 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005863 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005864 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005865
He, Qingbfdaab02007-09-12 14:18:28 +08005866 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005867 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005868 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005869
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005870 ++vcpu->stat.io_exits;
5871
5872 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005873 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005874
5875 port = exit_qualification >> 16;
5876 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005877 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005878
5879 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005880}
5881
Ingo Molnar102d8322007-02-19 14:37:47 +02005882static void
5883vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5884{
5885 /*
5886 * Patch in the VMCALL instruction:
5887 */
5888 hypercall[0] = 0x0f;
5889 hypercall[1] = 0x01;
5890 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005891}
5892
Wincy Vanb9c237b2015-02-03 23:56:30 +08005893static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005894{
5895 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005896 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005897
Wincy Vanb9c237b2015-02-03 23:56:30 +08005898 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005899 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5900 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5901 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5902 return (val & always_on) == always_on;
5903}
5904
Guo Chao0fa06072012-06-28 15:16:19 +08005905/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005906static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5907{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005908 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005909 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5910 unsigned long orig_val = val;
5911
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005912 /*
5913 * We get here when L2 changed cr0 in a way that did not change
5914 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005915 * but did change L0 shadowed bits. So we first calculate the
5916 * effective cr0 value that L1 would like to write into the
5917 * hardware. It consists of the L2-owned bits from the new
5918 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005919 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005920 val = (val & ~vmcs12->cr0_guest_host_mask) |
5921 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5922
Wincy Vanb9c237b2015-02-03 23:56:30 +08005923 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005924 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005925
5926 if (kvm_set_cr0(vcpu, val))
5927 return 1;
5928 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005929 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005930 } else {
5931 if (to_vmx(vcpu)->nested.vmxon &&
5932 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5933 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005934 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005935 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005936}
5937
5938static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5939{
5940 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005941 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5942 unsigned long orig_val = val;
5943
5944 /* analogously to handle_set_cr0 */
5945 val = (val & ~vmcs12->cr4_guest_host_mask) |
5946 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5947 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005948 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005949 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005950 return 0;
5951 } else
5952 return kvm_set_cr4(vcpu, val);
5953}
5954
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005955/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005956static void handle_clts(struct kvm_vcpu *vcpu)
5957{
5958 if (is_guest_mode(vcpu)) {
5959 /*
5960 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5961 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5962 * just pretend it's off (also in arch.cr0 for fpu_activate).
5963 */
5964 vmcs_writel(CR0_READ_SHADOW,
5965 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5966 vcpu->arch.cr0 &= ~X86_CR0_TS;
5967 } else
5968 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5969}
5970
Avi Kivity851ba692009-08-24 11:10:17 +03005971static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005972{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005973 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005974 int cr;
5975 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005976 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005977
He, Qingbfdaab02007-09-12 14:18:28 +08005978 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005979 cr = exit_qualification & 15;
5980 reg = (exit_qualification >> 8) & 15;
5981 switch ((exit_qualification >> 4) & 3) {
5982 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005983 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005984 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005985 switch (cr) {
5986 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005987 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005988 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005989 return 1;
5990 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005991 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005992 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005993 return 1;
5994 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005995 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005996 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005997 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005998 case 8: {
5999 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03006000 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01006001 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01006002 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006003 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006004 return 1;
6005 if (cr8_prev <= cr8)
6006 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03006007 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03006008 return 0;
6009 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006010 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006011 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006012 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006013 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02006014 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03006015 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02006016 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03006017 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006018 case 1: /*mov from cr*/
6019 switch (cr) {
6020 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006021 val = kvm_read_cr3(vcpu);
6022 kvm_register_write(vcpu, reg, val);
6023 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006024 skip_emulated_instruction(vcpu);
6025 return 1;
6026 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006027 val = kvm_get_cr8(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 }
6033 break;
6034 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006035 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006036 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006037 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006038
6039 skip_emulated_instruction(vcpu);
6040 return 1;
6041 default:
6042 break;
6043 }
Avi Kivity851ba692009-08-24 11:10:17 +03006044 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006045 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006046 (int)(exit_qualification >> 4) & 3, cr);
6047 return 0;
6048}
6049
Avi Kivity851ba692009-08-24 11:10:17 +03006050static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006051{
He, Qingbfdaab02007-09-12 14:18:28 +08006052 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006053 int dr, dr7, reg;
6054
6055 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6056 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6057
6058 /* First, if DR does not exist, trigger UD */
6059 if (!kvm_require_dr(vcpu, dr))
6060 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006061
Jan Kiszkaf2483412010-01-20 18:20:20 +01006062 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006063 if (!kvm_require_cpl(vcpu, 0))
6064 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006065 dr7 = vmcs_readl(GUEST_DR7);
6066 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006067 /*
6068 * As the vm-exit takes precedence over the debug trap, we
6069 * need to emulate the latter, either for the host or the
6070 * guest debugging itself.
6071 */
6072 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006073 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006074 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006075 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006076 vcpu->run->debug.arch.exception = DB_VECTOR;
6077 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006078 return 0;
6079 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006080 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006081 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006082 kvm_queue_exception(vcpu, DB_VECTOR);
6083 return 1;
6084 }
6085 }
6086
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006087 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006088 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6089 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006090
6091 /*
6092 * No more DR vmexits; force a reload of the debug registers
6093 * and reenter on this instruction. The next vmexit will
6094 * retrieve the full state of the debug registers.
6095 */
6096 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6097 return 1;
6098 }
6099
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006100 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6101 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006102 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006103
6104 if (kvm_get_dr(vcpu, dr, &val))
6105 return 1;
6106 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006107 } else
Nadav Amit57773922014-06-18 17:19:23 +03006108 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006109 return 1;
6110
Avi Kivity6aa8b732006-12-10 02:21:36 -08006111 skip_emulated_instruction(vcpu);
6112 return 1;
6113}
6114
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006115static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6116{
6117 return vcpu->arch.dr6;
6118}
6119
6120static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6121{
6122}
6123
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006124static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6125{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006126 get_debugreg(vcpu->arch.db[0], 0);
6127 get_debugreg(vcpu->arch.db[1], 1);
6128 get_debugreg(vcpu->arch.db[2], 2);
6129 get_debugreg(vcpu->arch.db[3], 3);
6130 get_debugreg(vcpu->arch.dr6, 6);
6131 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6132
6133 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006134 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006135}
6136
Gleb Natapov020df072010-04-13 10:05:23 +03006137static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6138{
6139 vmcs_writel(GUEST_DR7, val);
6140}
6141
Avi Kivity851ba692009-08-24 11:10:17 +03006142static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006143{
Avi Kivity06465c52007-02-28 20:46:53 +02006144 kvm_emulate_cpuid(vcpu);
6145 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006146}
6147
Avi Kivity851ba692009-08-24 11:10:17 +03006148static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006149{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006150 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006151 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006152
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006153 msr_info.index = ecx;
6154 msr_info.host_initiated = false;
6155 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006156 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006157 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006158 return 1;
6159 }
6160
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006161 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006162
Avi Kivity6aa8b732006-12-10 02:21:36 -08006163 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006164 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6165 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006166 skip_emulated_instruction(vcpu);
6167 return 1;
6168}
6169
Avi Kivity851ba692009-08-24 11:10:17 +03006170static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006171{
Will Auld8fe8ab42012-11-29 12:42:12 -08006172 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006173 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6174 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6175 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006176
Will Auld8fe8ab42012-11-29 12:42:12 -08006177 msr.data = data;
6178 msr.index = ecx;
6179 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006180 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006181 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006182 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006183 return 1;
6184 }
6185
Avi Kivity59200272010-01-25 19:47:02 +02006186 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006187 skip_emulated_instruction(vcpu);
6188 return 1;
6189}
6190
Avi Kivity851ba692009-08-24 11:10:17 +03006191static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006192{
Avi Kivity3842d132010-07-27 12:30:24 +03006193 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006194 return 1;
6195}
6196
Avi Kivity851ba692009-08-24 11:10:17 +03006197static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006198{
Eddie Dong85f455f2007-07-06 12:20:49 +03006199 u32 cpu_based_vm_exec_control;
6200
6201 /* clear pending irq */
6202 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6203 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6204 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006205
Avi Kivity3842d132010-07-27 12:30:24 +03006206 kvm_make_request(KVM_REQ_EVENT, vcpu);
6207
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006208 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006209 return 1;
6210}
6211
Avi Kivity851ba692009-08-24 11:10:17 +03006212static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006213{
Avi Kivityd3bef152007-06-05 15:53:05 +03006214 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006215}
6216
Avi Kivity851ba692009-08-24 11:10:17 +03006217static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006218{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006219 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006220}
6221
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006222static int handle_invd(struct kvm_vcpu *vcpu)
6223{
Andre Przywara51d8b662010-12-21 11:12:02 +01006224 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006225}
6226
Avi Kivity851ba692009-08-24 11:10:17 +03006227static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006228{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006229 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006230
6231 kvm_mmu_invlpg(vcpu, exit_qualification);
6232 skip_emulated_instruction(vcpu);
6233 return 1;
6234}
6235
Avi Kivityfee84b02011-11-10 14:57:25 +02006236static int handle_rdpmc(struct kvm_vcpu *vcpu)
6237{
6238 int err;
6239
6240 err = kvm_rdpmc(vcpu);
6241 kvm_complete_insn_gp(vcpu, err);
6242
6243 return 1;
6244}
6245
Avi Kivity851ba692009-08-24 11:10:17 +03006246static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006247{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006248 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006249 return 1;
6250}
6251
Dexuan Cui2acf9232010-06-10 11:27:12 +08006252static int handle_xsetbv(struct kvm_vcpu *vcpu)
6253{
6254 u64 new_bv = kvm_read_edx_eax(vcpu);
6255 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6256
6257 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6258 skip_emulated_instruction(vcpu);
6259 return 1;
6260}
6261
Wanpeng Lif53cd632014-12-02 19:14:58 +08006262static int handle_xsaves(struct kvm_vcpu *vcpu)
6263{
6264 skip_emulated_instruction(vcpu);
6265 WARN(1, "this should never happen\n");
6266 return 1;
6267}
6268
6269static int handle_xrstors(struct kvm_vcpu *vcpu)
6270{
6271 skip_emulated_instruction(vcpu);
6272 WARN(1, "this should never happen\n");
6273 return 1;
6274}
6275
Avi Kivity851ba692009-08-24 11:10:17 +03006276static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006277{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006278 if (likely(fasteoi)) {
6279 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6280 int access_type, offset;
6281
6282 access_type = exit_qualification & APIC_ACCESS_TYPE;
6283 offset = exit_qualification & APIC_ACCESS_OFFSET;
6284 /*
6285 * Sane guest uses MOV to write EOI, with written value
6286 * not cared. So make a short-circuit here by avoiding
6287 * heavy instruction emulation.
6288 */
6289 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6290 (offset == APIC_EOI)) {
6291 kvm_lapic_set_eoi(vcpu);
6292 skip_emulated_instruction(vcpu);
6293 return 1;
6294 }
6295 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006296 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006297}
6298
Yang Zhangc7c9c562013-01-25 10:18:51 +08006299static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6300{
6301 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6302 int vector = exit_qualification & 0xff;
6303
6304 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6305 kvm_apic_set_eoi_accelerated(vcpu, vector);
6306 return 1;
6307}
6308
Yang Zhang83d4c282013-01-25 10:18:49 +08006309static int handle_apic_write(struct kvm_vcpu *vcpu)
6310{
6311 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6312 u32 offset = exit_qualification & 0xfff;
6313
6314 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6315 kvm_apic_write_nodecode(vcpu, offset);
6316 return 1;
6317}
6318
Avi Kivity851ba692009-08-24 11:10:17 +03006319static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006320{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006321 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006322 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006323 bool has_error_code = false;
6324 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006325 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006326 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006327
6328 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006329 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006330 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006331
6332 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6333
6334 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006335 if (reason == TASK_SWITCH_GATE && idt_v) {
6336 switch (type) {
6337 case INTR_TYPE_NMI_INTR:
6338 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006339 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006340 break;
6341 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006342 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006343 kvm_clear_interrupt_queue(vcpu);
6344 break;
6345 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006346 if (vmx->idt_vectoring_info &
6347 VECTORING_INFO_DELIVER_CODE_MASK) {
6348 has_error_code = true;
6349 error_code =
6350 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6351 }
6352 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006353 case INTR_TYPE_SOFT_EXCEPTION:
6354 kvm_clear_exception_queue(vcpu);
6355 break;
6356 default:
6357 break;
6358 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006359 }
Izik Eidus37817f22008-03-24 23:14:53 +02006360 tss_selector = exit_qualification;
6361
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006362 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6363 type != INTR_TYPE_EXT_INTR &&
6364 type != INTR_TYPE_NMI_INTR))
6365 skip_emulated_instruction(vcpu);
6366
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006367 if (kvm_task_switch(vcpu, tss_selector,
6368 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6369 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006370 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6371 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6372 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006373 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006374 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006375
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006376 /*
6377 * TODO: What about debug traps on tss switch?
6378 * Are we supposed to inject them and update dr6?
6379 */
6380
6381 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006382}
6383
Avi Kivity851ba692009-08-24 11:10:17 +03006384static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006385{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006386 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006387 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006388 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006389 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006390
Sheng Yangf9c617f2009-03-25 10:08:52 +08006391 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006392
Sheng Yang14394422008-04-28 12:24:45 +08006393 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006394 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006395 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6396 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6397 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006398 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006399 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6400 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006401 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6402 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006403 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006404 }
6405
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006406 /*
6407 * EPT violation happened while executing iret from NMI,
6408 * "blocked by NMI" bit has to be set before next VM entry.
6409 * There are errata that may cause this bit to not be set:
6410 * AAK134, BY25.
6411 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006412 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6413 cpu_has_virtual_nmis() &&
6414 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006415 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6416
Sheng Yang14394422008-04-28 12:24:45 +08006417 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006418 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006419
Bandan Dasd95c5562016-07-12 18:18:51 -04006420 /* it is a read fault? */
6421 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6422 /* it is a write fault? */
6423 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006424 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006425 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006426 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006427 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006428
Yang Zhang25d92082013-08-06 12:00:32 +03006429 vcpu->arch.exit_qualification = exit_qualification;
6430
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006431 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006432}
6433
Avi Kivity851ba692009-08-24 11:10:17 +03006434static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006435{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006436 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006437 gpa_t gpa;
6438
6439 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006440 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006441 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006442 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006443 return 1;
6444 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006445
Paolo Bonzini450869d2015-11-04 13:41:21 +01006446 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006447 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006448 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6449 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006450
6451 if (unlikely(ret == RET_MMIO_PF_INVALID))
6452 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6453
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006454 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006455 return 1;
6456
6457 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006458 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006459
Avi Kivity851ba692009-08-24 11:10:17 +03006460 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6461 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006462
6463 return 0;
6464}
6465
Avi Kivity851ba692009-08-24 11:10:17 +03006466static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006467{
6468 u32 cpu_based_vm_exec_control;
6469
6470 /* clear pending NMI */
6471 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6472 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6473 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6474 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006475 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006476
6477 return 1;
6478}
6479
Mohammed Gamal80ced182009-09-01 12:48:18 +02006480static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006481{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006482 struct vcpu_vmx *vmx = to_vmx(vcpu);
6483 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006484 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006485 u32 cpu_exec_ctrl;
6486 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006487 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006488
6489 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6490 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006491
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006492 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006493 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006494 return handle_interrupt_window(&vmx->vcpu);
6495
Avi Kivityde87dcd2012-06-12 20:21:38 +03006496 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6497 return 1;
6498
Liran Alon114de9b2017-11-05 16:56:34 +02006499 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006500
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006501 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006502 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006503 ret = 0;
6504 goto out;
6505 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006506
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006507 if (err != EMULATE_DONE)
6508 goto emulation_error;
6509
6510 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6511 vcpu->arch.exception.pending)
6512 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006513
Gleb Natapov8d76c492013-05-08 18:38:44 +03006514 if (vcpu->arch.halt_request) {
6515 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006516 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006517 goto out;
6518 }
6519
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006520 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006521 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006522 if (need_resched())
6523 schedule();
6524 }
6525
Mohammed Gamal80ced182009-09-01 12:48:18 +02006526out:
6527 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006528
6529emulation_error:
6530 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6531 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6532 vcpu->run->internal.ndata = 0;
6533 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006534}
6535
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006536static int __grow_ple_window(int val)
6537{
6538 if (ple_window_grow < 1)
6539 return ple_window;
6540
6541 val = min(val, ple_window_actual_max);
6542
6543 if (ple_window_grow < ple_window)
6544 val *= ple_window_grow;
6545 else
6546 val += ple_window_grow;
6547
6548 return val;
6549}
6550
6551static int __shrink_ple_window(int val, int modifier, int minimum)
6552{
6553 if (modifier < 1)
6554 return ple_window;
6555
6556 if (modifier < ple_window)
6557 val /= modifier;
6558 else
6559 val -= modifier;
6560
6561 return max(val, minimum);
6562}
6563
6564static void grow_ple_window(struct kvm_vcpu *vcpu)
6565{
6566 struct vcpu_vmx *vmx = to_vmx(vcpu);
6567 int old = vmx->ple_window;
6568
6569 vmx->ple_window = __grow_ple_window(old);
6570
6571 if (vmx->ple_window != old)
6572 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006573
6574 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006575}
6576
6577static void shrink_ple_window(struct kvm_vcpu *vcpu)
6578{
6579 struct vcpu_vmx *vmx = to_vmx(vcpu);
6580 int old = vmx->ple_window;
6581
6582 vmx->ple_window = __shrink_ple_window(old,
6583 ple_window_shrink, ple_window);
6584
6585 if (vmx->ple_window != old)
6586 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006587
6588 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006589}
6590
6591/*
6592 * ple_window_actual_max is computed to be one grow_ple_window() below
6593 * ple_window_max. (See __grow_ple_window for the reason.)
6594 * This prevents overflows, because ple_window_max is int.
6595 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6596 * this process.
6597 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6598 */
6599static void update_ple_window_actual_max(void)
6600{
6601 ple_window_actual_max =
6602 __shrink_ple_window(max(ple_window_max, ple_window),
6603 ple_window_grow, INT_MIN);
6604}
6605
Feng Wubf9f6ac2015-09-18 22:29:55 +08006606/*
6607 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6608 */
6609static void wakeup_handler(void)
6610{
6611 struct kvm_vcpu *vcpu;
6612 int cpu = smp_processor_id();
6613
6614 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6615 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6616 blocked_vcpu_list) {
6617 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6618
6619 if (pi_test_on(pi_desc) == 1)
6620 kvm_vcpu_kick(vcpu);
6621 }
6622 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6623}
6624
Tiejun Chenf2c76482014-10-28 10:14:47 +08006625static __init int hardware_setup(void)
6626{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006627 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006628
6629 rdmsrl_safe(MSR_EFER, &host_efer);
6630
6631 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6632 kvm_define_shared_msr(i, vmx_msr_index[i]);
6633
6634 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6635 if (!vmx_io_bitmap_a)
6636 return r;
6637
6638 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6639 if (!vmx_io_bitmap_b)
6640 goto out;
6641
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006642 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6643 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006644 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006645
6646 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6647 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006648 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006649
6650 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6651 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6652
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006653 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006654
6655 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6656
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006657 if (setup_vmcs_config(&vmcs_config) < 0) {
6658 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006659 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006660 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006661
6662 if (boot_cpu_has(X86_FEATURE_NX))
6663 kvm_enable_efer_bits(EFER_NX);
6664
Wanpeng Li2df19692017-03-23 05:30:08 -07006665 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6666 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006667 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006668
Tiejun Chenf2c76482014-10-28 10:14:47 +08006669 if (!cpu_has_vmx_shadow_vmcs())
6670 enable_shadow_vmcs = 0;
6671 if (enable_shadow_vmcs)
6672 init_vmcs_shadow_fields();
6673
6674 if (!cpu_has_vmx_ept() ||
6675 !cpu_has_vmx_ept_4levels()) {
6676 enable_ept = 0;
6677 enable_unrestricted_guest = 0;
6678 enable_ept_ad_bits = 0;
6679 }
6680
6681 if (!cpu_has_vmx_ept_ad_bits())
6682 enable_ept_ad_bits = 0;
6683
6684 if (!cpu_has_vmx_unrestricted_guest())
6685 enable_unrestricted_guest = 0;
6686
Paolo Bonziniad15a292015-01-30 16:18:49 +01006687 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006688 flexpriority_enabled = 0;
6689
Paolo Bonziniad15a292015-01-30 16:18:49 +01006690 /*
6691 * set_apic_access_page_addr() is used to reload apic access
6692 * page upon invalidation. No need to do anything if not
6693 * using the APIC_ACCESS_ADDR VMCS field.
6694 */
6695 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006696 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006697
6698 if (!cpu_has_vmx_tpr_shadow())
6699 kvm_x86_ops->update_cr8_intercept = NULL;
6700
6701 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6702 kvm_disable_largepages();
6703
6704 if (!cpu_has_vmx_ple())
6705 ple_gap = 0;
6706
6707 if (!cpu_has_vmx_apicv())
6708 enable_apicv = 0;
6709
Haozhong Zhang64903d62015-10-20 15:39:09 +08006710 if (cpu_has_vmx_tsc_scaling()) {
6711 kvm_has_tsc_control = true;
6712 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6713 kvm_tsc_scaling_ratio_frac_bits = 48;
6714 }
6715
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006716 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6717
Tiejun Chenbaa03522014-12-23 16:21:11 +08006718 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006719 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006720 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6721 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006722 0ull, VMX_EPT_EXECUTABLE_MASK,
6723 cpu_has_vmx_ept_execute_only() ?
6724 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006725 ept_set_mmio_spte_mask();
6726 kvm_enable_tdp();
6727 } else
6728 kvm_disable_tdp();
6729
6730 update_ple_window_actual_max();
6731
Kai Huang843e4332015-01-28 10:54:28 +08006732 /*
6733 * Only enable PML when hardware supports PML feature, and both EPT
6734 * and EPT A/D bit features are enabled -- PML depends on them to work.
6735 */
6736 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6737 enable_pml = 0;
6738
6739 if (!enable_pml) {
6740 kvm_x86_ops->slot_enable_log_dirty = NULL;
6741 kvm_x86_ops->slot_disable_log_dirty = NULL;
6742 kvm_x86_ops->flush_log_dirty = NULL;
6743 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6744 }
6745
Yunhong Jiang64672c92016-06-13 14:19:59 -07006746 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6747 u64 vmx_msr;
6748
6749 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6750 cpu_preemption_timer_multi =
6751 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6752 } else {
6753 kvm_x86_ops->set_hv_timer = NULL;
6754 kvm_x86_ops->cancel_hv_timer = NULL;
6755 }
6756
Feng Wubf9f6ac2015-09-18 22:29:55 +08006757 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6758
Ashok Rajc45dcc72016-06-22 14:59:56 +08006759 kvm_mce_cap_supported |= MCG_LMCE_P;
6760
Tiejun Chenf2c76482014-10-28 10:14:47 +08006761 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006762
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006763out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006764 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006765out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006766 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006767out1:
6768 free_page((unsigned long)vmx_io_bitmap_b);
6769out:
6770 free_page((unsigned long)vmx_io_bitmap_a);
6771
6772 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006773}
6774
6775static __exit void hardware_unsetup(void)
6776{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006777 free_page((unsigned long)vmx_io_bitmap_b);
6778 free_page((unsigned long)vmx_io_bitmap_a);
6779 free_page((unsigned long)vmx_vmwrite_bitmap);
6780 free_page((unsigned long)vmx_vmread_bitmap);
6781
Tiejun Chenf2c76482014-10-28 10:14:47 +08006782 free_kvm_area();
6783}
6784
Avi Kivity6aa8b732006-12-10 02:21:36 -08006785/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006786 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6787 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6788 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006789static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006790{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006791 if (ple_gap)
6792 grow_ple_window(vcpu);
6793
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006794 skip_emulated_instruction(vcpu);
6795 kvm_vcpu_on_spin(vcpu);
6796
6797 return 1;
6798}
6799
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006800static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006801{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006802 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006803 return 1;
6804}
6805
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006806static int handle_mwait(struct kvm_vcpu *vcpu)
6807{
6808 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6809 return handle_nop(vcpu);
6810}
6811
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006812static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6813{
6814 return 1;
6815}
6816
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006817static int handle_monitor(struct kvm_vcpu *vcpu)
6818{
6819 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6820 return handle_nop(vcpu);
6821}
6822
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006823/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006824 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6825 * set the success or error code of an emulated VMX instruction, as specified
6826 * by Vol 2B, VMX Instruction Reference, "Conventions".
6827 */
6828static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6829{
6830 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6831 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6832 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6833}
6834
6835static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6836{
6837 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6838 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6839 X86_EFLAGS_SF | X86_EFLAGS_OF))
6840 | X86_EFLAGS_CF);
6841}
6842
Abel Gordon145c28d2013-04-18 14:36:55 +03006843static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006844 u32 vm_instruction_error)
6845{
6846 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6847 /*
6848 * failValid writes the error number to the current VMCS, which
6849 * can't be done there isn't a current VMCS.
6850 */
6851 nested_vmx_failInvalid(vcpu);
6852 return;
6853 }
6854 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6855 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6856 X86_EFLAGS_SF | X86_EFLAGS_OF))
6857 | X86_EFLAGS_ZF);
6858 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6859 /*
6860 * We don't need to force a shadow sync because
6861 * VM_INSTRUCTION_ERROR is not shadowed
6862 */
6863}
Abel Gordon145c28d2013-04-18 14:36:55 +03006864
Wincy Vanff651cb2014-12-11 08:52:58 +03006865static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6866{
6867 /* TODO: not to reset guest simply here. */
6868 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006869 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006870}
6871
Jan Kiszkaf4124502014-03-07 20:03:13 +01006872static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6873{
6874 struct vcpu_vmx *vmx =
6875 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6876
6877 vmx->nested.preemption_timer_expired = true;
6878 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6879 kvm_vcpu_kick(&vmx->vcpu);
6880
6881 return HRTIMER_NORESTART;
6882}
6883
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006884/*
Bandan Das19677e32014-05-06 02:19:15 -04006885 * Decode the memory-address operand of a vmx instruction, as recorded on an
6886 * exit caused by such an instruction (run by a guest hypervisor).
6887 * On success, returns 0. When the operand is invalid, returns 1 and throws
6888 * #UD or #GP.
6889 */
6890static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6891 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006892 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006893{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006894 gva_t off;
6895 bool exn;
6896 struct kvm_segment s;
6897
Bandan Das19677e32014-05-06 02:19:15 -04006898 /*
6899 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6900 * Execution", on an exit, vmx_instruction_info holds most of the
6901 * addressing components of the operand. Only the displacement part
6902 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6903 * For how an actual address is calculated from all these components,
6904 * refer to Vol. 1, "Operand Addressing".
6905 */
6906 int scaling = vmx_instruction_info & 3;
6907 int addr_size = (vmx_instruction_info >> 7) & 7;
6908 bool is_reg = vmx_instruction_info & (1u << 10);
6909 int seg_reg = (vmx_instruction_info >> 15) & 7;
6910 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6911 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6912 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6913 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6914
6915 if (is_reg) {
6916 kvm_queue_exception(vcpu, UD_VECTOR);
6917 return 1;
6918 }
6919
6920 /* Addr = segment_base + offset */
6921 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006922 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006923 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006924 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006925 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006926 off += kvm_register_read(vcpu, index_reg)<<scaling;
6927 vmx_get_segment(vcpu, &s, seg_reg);
6928 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006929
6930 if (addr_size == 1) /* 32 bit */
6931 *ret &= 0xffffffff;
6932
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006933 /* Checks for #GP/#SS exceptions. */
6934 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006935 if (is_long_mode(vcpu)) {
6936 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6937 * non-canonical form. This is the only check on the memory
6938 * destination for long mode!
6939 */
6940 exn = is_noncanonical_address(*ret);
6941 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006942 /* Protected mode: apply checks for segment validity in the
6943 * following order:
6944 * - segment type check (#GP(0) may be thrown)
6945 * - usability check (#GP(0)/#SS(0))
6946 * - limit check (#GP(0)/#SS(0))
6947 */
6948 if (wr)
6949 /* #GP(0) if the destination operand is located in a
6950 * read-only data segment or any code segment.
6951 */
6952 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6953 else
6954 /* #GP(0) if the source operand is located in an
6955 * execute-only code segment
6956 */
6957 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006958 if (exn) {
6959 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6960 return 1;
6961 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006962 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6963 */
6964 exn = (s.unusable != 0);
6965 /* Protected mode: #GP(0)/#SS(0) if the memory
6966 * operand is outside the segment limit.
6967 */
6968 exn = exn || (off + sizeof(u64) > s.limit);
6969 }
6970 if (exn) {
6971 kvm_queue_exception_e(vcpu,
6972 seg_reg == VCPU_SREG_SS ?
6973 SS_VECTOR : GP_VECTOR,
6974 0);
6975 return 1;
6976 }
6977
Bandan Das19677e32014-05-06 02:19:15 -04006978 return 0;
6979}
6980
6981/*
Bandan Das3573e222014-05-06 02:19:16 -04006982 * This function performs the various checks including
6983 * - if it's 4KB aligned
6984 * - No bits beyond the physical address width are set
6985 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006986 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006987 */
Bandan Das4291b582014-05-06 02:19:18 -04006988static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6989 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006990{
6991 gva_t gva;
6992 gpa_t vmptr;
6993 struct x86_exception e;
6994 struct page *page;
6995 struct vcpu_vmx *vmx = to_vmx(vcpu);
6996 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6997
6998 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006999 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04007000 return 1;
7001
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007002 if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04007003 kvm_inject_page_fault(vcpu, &e);
7004 return 1;
7005 }
7006
7007 switch (exit_reason) {
7008 case EXIT_REASON_VMON:
7009 /*
7010 * SDM 3: 24.11.5
7011 * The first 4 bytes of VMXON region contain the supported
7012 * VMCS revision identifier
7013 *
7014 * Note - IA32_VMX_BASIC[48] will never be 1
7015 * for the nested case;
7016 * which replaces physical address width with 32
7017 *
7018 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007019 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04007020 nested_vmx_failInvalid(vcpu);
7021 skip_emulated_instruction(vcpu);
7022 return 1;
7023 }
7024
7025 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007026 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04007027 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007028 skip_emulated_instruction(vcpu);
7029 return 1;
7030 }
7031 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04007032 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007033 nested_release_page_clean(page);
7034 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04007035 skip_emulated_instruction(vcpu);
7036 return 1;
7037 }
7038 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007039 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04007040 vmx->nested.vmxon_ptr = vmptr;
7041 break;
Bandan Das4291b582014-05-06 02:19:18 -04007042 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007043 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007044 nested_vmx_failValid(vcpu,
7045 VMXERR_VMCLEAR_INVALID_ADDRESS);
7046 skip_emulated_instruction(vcpu);
7047 return 1;
7048 }
Bandan Das3573e222014-05-06 02:19:16 -04007049
Bandan Das4291b582014-05-06 02:19:18 -04007050 if (vmptr == vmx->nested.vmxon_ptr) {
7051 nested_vmx_failValid(vcpu,
7052 VMXERR_VMCLEAR_VMXON_POINTER);
7053 skip_emulated_instruction(vcpu);
7054 return 1;
7055 }
7056 break;
7057 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007058 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007059 nested_vmx_failValid(vcpu,
7060 VMXERR_VMPTRLD_INVALID_ADDRESS);
7061 skip_emulated_instruction(vcpu);
7062 return 1;
7063 }
7064
7065 if (vmptr == vmx->nested.vmxon_ptr) {
7066 nested_vmx_failValid(vcpu,
7067 VMXERR_VMCLEAR_VMXON_POINTER);
7068 skip_emulated_instruction(vcpu);
7069 return 1;
7070 }
7071 break;
Bandan Das3573e222014-05-06 02:19:16 -04007072 default:
7073 return 1; /* shouldn't happen */
7074 }
7075
Bandan Das4291b582014-05-06 02:19:18 -04007076 if (vmpointer)
7077 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007078 return 0;
7079}
7080
7081/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007082 * Emulate the VMXON instruction.
7083 * Currently, we just remember that VMX is active, and do not save or even
7084 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7085 * do not currently need to store anything in that guest-allocated memory
7086 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7087 * argument is different from the VMXON pointer (which the spec says they do).
7088 */
7089static int handle_vmon(struct kvm_vcpu *vcpu)
7090{
7091 struct kvm_segment cs;
7092 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007093 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007094 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7095 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007096 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007097
7098 /* The Intel VMX Instruction Reference lists a bunch of bits that
7099 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7100 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7101 * Otherwise, we should fail with #UD. We test these now:
7102 */
7103 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7104 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7105 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7106 kvm_queue_exception(vcpu, UD_VECTOR);
7107 return 1;
7108 }
7109
7110 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7111 if (is_long_mode(vcpu) && !cs.l) {
7112 kvm_queue_exception(vcpu, UD_VECTOR);
7113 return 1;
7114 }
7115
7116 if (vmx_get_cpl(vcpu)) {
7117 kvm_inject_gp(vcpu, 0);
7118 return 1;
7119 }
Bandan Das3573e222014-05-06 02:19:16 -04007120
Bandan Das4291b582014-05-06 02:19:18 -04007121 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007122 return 1;
7123
Abel Gordon145c28d2013-04-18 14:36:55 +03007124 if (vmx->nested.vmxon) {
7125 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7126 skip_emulated_instruction(vcpu);
7127 return 1;
7128 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007129
Haozhong Zhang3b840802016-06-22 14:59:54 +08007130 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007131 != VMXON_NEEDED_FEATURES) {
7132 kvm_inject_gp(vcpu, 0);
7133 return 1;
7134 }
7135
Paolo Bonziniff546f92018-01-11 12:16:15 +01007136 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7137 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007138 goto out_vmcs02;
Jim Mattson46e24df2017-11-27 17:22:25 -06007139
David Matlack4f2777b2016-07-13 17:16:37 -07007140 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7141 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007142 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007143
Abel Gordon8de48832013-04-18 14:37:25 +03007144 if (enable_shadow_vmcs) {
7145 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007146 if (!shadow_vmcs)
7147 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007148 /* mark vmcs as shadow */
7149 shadow_vmcs->revision_id |= (1u << 31);
7150 /* init shadow vmcs */
7151 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007152 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007153 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007154
Jan Kiszkaf4124502014-03-07 20:03:13 +01007155 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007156 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007157 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7158
Roman Kagan020a90f2018-07-19 21:59:07 +03007159 vmx->nested.vpid02 = allocate_vpid();
7160
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007161 vmx->nested.vmxon = true;
7162
7163 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007164 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007165 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007166
7167out_shadow_vmcs:
7168 kfree(vmx->nested.cached_vmcs12);
7169
7170out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007171 free_loaded_vmcs(&vmx->nested.vmcs02);
7172
7173out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007174 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007175}
7176
7177/*
7178 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7179 * for running VMX instructions (except VMXON, whose prerequisites are
7180 * slightly different). It also specifies what exception to inject otherwise.
7181 */
7182static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7183{
7184 struct kvm_segment cs;
7185 struct vcpu_vmx *vmx = to_vmx(vcpu);
7186
7187 if (!vmx->nested.vmxon) {
7188 kvm_queue_exception(vcpu, UD_VECTOR);
7189 return 0;
7190 }
7191
7192 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7193 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7194 (is_long_mode(vcpu) && !cs.l)) {
7195 kvm_queue_exception(vcpu, UD_VECTOR);
7196 return 0;
7197 }
7198
7199 if (vmx_get_cpl(vcpu)) {
7200 kvm_inject_gp(vcpu, 0);
7201 return 0;
7202 }
7203
7204 return 1;
7205}
7206
Abel Gordone7953d72013-04-18 14:37:55 +03007207static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7208{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007209 if (vmx->nested.current_vmptr == -1ull)
7210 return;
7211
7212 /* current_vmptr and current_vmcs12 are always set/reset together */
7213 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7214 return;
7215
Abel Gordon012f83c2013-04-18 14:39:25 +03007216 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007217 /* copy to memory all shadowed fields in case
7218 they were modified */
7219 copy_shadow_to_vmcs12(vmx);
7220 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007221 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7222 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007223 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007224 }
Wincy Van705699a2015-02-03 23:58:17 +08007225 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007226
7227 /* Flush VMCS12 to guest memory */
7228 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7229 VMCS12_SIZE);
7230
Abel Gordone7953d72013-04-18 14:37:55 +03007231 kunmap(vmx->nested.current_vmcs12_page);
7232 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007233 vmx->nested.current_vmptr = -1ull;
7234 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007235}
7236
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007237/*
7238 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7239 * just stops using VMX.
7240 */
7241static void free_nested(struct vcpu_vmx *vmx)
7242{
7243 if (!vmx->nested.vmxon)
7244 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007245
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007246 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007247 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007248 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007249 if (enable_shadow_vmcs) {
7250 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7251 free_vmcs(vmx->vmcs01.shadow_vmcs);
7252 vmx->vmcs01.shadow_vmcs = NULL;
7253 }
David Matlack4f2777b2016-07-13 17:16:37 -07007254 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007255 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007256 if (vmx->nested.apic_access_page) {
7257 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007258 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007259 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007260 if (vmx->nested.virtual_apic_page) {
7261 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007262 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007263 }
Wincy Van705699a2015-02-03 23:58:17 +08007264 if (vmx->nested.pi_desc_page) {
7265 kunmap(vmx->nested.pi_desc_page);
7266 nested_release_page(vmx->nested.pi_desc_page);
7267 vmx->nested.pi_desc_page = NULL;
7268 vmx->nested.pi_desc = NULL;
7269 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007270
Jim Mattson46e24df2017-11-27 17:22:25 -06007271 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007272}
7273
7274/* Emulate the VMXOFF instruction */
7275static int handle_vmoff(struct kvm_vcpu *vcpu)
7276{
7277 if (!nested_vmx_check_permission(vcpu))
7278 return 1;
7279 free_nested(to_vmx(vcpu));
7280 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007281 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007282 return 1;
7283}
7284
Nadav Har'El27d6c862011-05-25 23:06:59 +03007285/* Emulate the VMCLEAR instruction */
7286static int handle_vmclear(struct kvm_vcpu *vcpu)
7287{
7288 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007289 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007290 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007291
7292 if (!nested_vmx_check_permission(vcpu))
7293 return 1;
7294
Bandan Das4291b582014-05-06 02:19:18 -04007295 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007296 return 1;
7297
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007298 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007299 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007300
Jim Mattson29deec42017-03-02 12:41:48 -08007301 kvm_vcpu_write_guest(vcpu,
7302 vmptr + offsetof(struct vmcs12, launch_state),
7303 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007304
Nadav Har'El27d6c862011-05-25 23:06:59 +03007305 skip_emulated_instruction(vcpu);
7306 nested_vmx_succeed(vcpu);
7307 return 1;
7308}
7309
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007310static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7311
7312/* Emulate the VMLAUNCH instruction */
7313static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7314{
7315 return nested_vmx_run(vcpu, true);
7316}
7317
7318/* Emulate the VMRESUME instruction */
7319static int handle_vmresume(struct kvm_vcpu *vcpu)
7320{
7321
7322 return nested_vmx_run(vcpu, false);
7323}
7324
Nadav Har'El49f705c2011-05-25 23:08:30 +03007325enum vmcs_field_type {
7326 VMCS_FIELD_TYPE_U16 = 0,
7327 VMCS_FIELD_TYPE_U64 = 1,
7328 VMCS_FIELD_TYPE_U32 = 2,
7329 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7330};
7331
7332static inline int vmcs_field_type(unsigned long field)
7333{
7334 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7335 return VMCS_FIELD_TYPE_U32;
7336 return (field >> 13) & 0x3 ;
7337}
7338
7339static inline int vmcs_field_readonly(unsigned long field)
7340{
7341 return (((field >> 10) & 0x3) == 1);
7342}
7343
7344/*
7345 * Read a vmcs12 field. Since these can have varying lengths and we return
7346 * one type, we chose the biggest type (u64) and zero-extend the return value
7347 * to that size. Note that the caller, handle_vmread, might need to use only
7348 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7349 * 64-bit fields are to be returned).
7350 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007351static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7352 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007353{
7354 short offset = vmcs_field_to_offset(field);
7355 char *p;
7356
7357 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007358 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007359
7360 p = ((char *)(get_vmcs12(vcpu))) + offset;
7361
7362 switch (vmcs_field_type(field)) {
7363 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7364 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007365 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007366 case VMCS_FIELD_TYPE_U16:
7367 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007368 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007369 case VMCS_FIELD_TYPE_U32:
7370 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007371 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007372 case VMCS_FIELD_TYPE_U64:
7373 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007374 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007375 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007376 WARN_ON(1);
7377 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007378 }
7379}
7380
Abel Gordon20b97fe2013-04-18 14:36:25 +03007381
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007382static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7383 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007384 short offset = vmcs_field_to_offset(field);
7385 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7386 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007387 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007388
7389 switch (vmcs_field_type(field)) {
7390 case VMCS_FIELD_TYPE_U16:
7391 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007392 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007393 case VMCS_FIELD_TYPE_U32:
7394 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007395 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007396 case VMCS_FIELD_TYPE_U64:
7397 *(u64 *)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_NATURAL_WIDTH:
7400 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007401 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007402 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007403 WARN_ON(1);
7404 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007405 }
7406
7407}
7408
Abel Gordon16f5b902013-04-18 14:38:25 +03007409static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7410{
7411 int i;
7412 unsigned long field;
7413 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007414 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007415 const unsigned long *fields = shadow_read_write_fields;
7416 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007417
Jan Kiszka282da872014-10-08 18:05:39 +02007418 preempt_disable();
7419
Abel Gordon16f5b902013-04-18 14:38:25 +03007420 vmcs_load(shadow_vmcs);
7421
7422 for (i = 0; i < num_fields; i++) {
7423 field = fields[i];
7424 switch (vmcs_field_type(field)) {
7425 case VMCS_FIELD_TYPE_U16:
7426 field_value = vmcs_read16(field);
7427 break;
7428 case VMCS_FIELD_TYPE_U32:
7429 field_value = vmcs_read32(field);
7430 break;
7431 case VMCS_FIELD_TYPE_U64:
7432 field_value = vmcs_read64(field);
7433 break;
7434 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7435 field_value = vmcs_readl(field);
7436 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007437 default:
7438 WARN_ON(1);
7439 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007440 }
7441 vmcs12_write_any(&vmx->vcpu, field, field_value);
7442 }
7443
7444 vmcs_clear(shadow_vmcs);
7445 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007446
7447 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007448}
7449
Abel Gordonc3114422013-04-18 14:38:55 +03007450static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7451{
Mathias Krausec2bae892013-06-26 20:36:21 +02007452 const unsigned long *fields[] = {
7453 shadow_read_write_fields,
7454 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007455 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007456 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007457 max_shadow_read_write_fields,
7458 max_shadow_read_only_fields
7459 };
7460 int i, q;
7461 unsigned long field;
7462 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007463 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007464
7465 vmcs_load(shadow_vmcs);
7466
Mathias Krausec2bae892013-06-26 20:36:21 +02007467 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007468 for (i = 0; i < max_fields[q]; i++) {
7469 field = fields[q][i];
7470 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7471
7472 switch (vmcs_field_type(field)) {
7473 case VMCS_FIELD_TYPE_U16:
7474 vmcs_write16(field, (u16)field_value);
7475 break;
7476 case VMCS_FIELD_TYPE_U32:
7477 vmcs_write32(field, (u32)field_value);
7478 break;
7479 case VMCS_FIELD_TYPE_U64:
7480 vmcs_write64(field, (u64)field_value);
7481 break;
7482 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7483 vmcs_writel(field, (long)field_value);
7484 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007485 default:
7486 WARN_ON(1);
7487 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007488 }
7489 }
7490 }
7491
7492 vmcs_clear(shadow_vmcs);
7493 vmcs_load(vmx->loaded_vmcs->vmcs);
7494}
7495
Nadav Har'El49f705c2011-05-25 23:08:30 +03007496/*
7497 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7498 * used before) all generate the same failure when it is missing.
7499 */
7500static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7501{
7502 struct vcpu_vmx *vmx = to_vmx(vcpu);
7503 if (vmx->nested.current_vmptr == -1ull) {
7504 nested_vmx_failInvalid(vcpu);
7505 skip_emulated_instruction(vcpu);
7506 return 0;
7507 }
7508 return 1;
7509}
7510
7511static int handle_vmread(struct kvm_vcpu *vcpu)
7512{
7513 unsigned long field;
7514 u64 field_value;
7515 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7516 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7517 gva_t gva = 0;
7518
7519 if (!nested_vmx_check_permission(vcpu) ||
7520 !nested_vmx_check_vmcs12(vcpu))
7521 return 1;
7522
7523 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007524 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007525 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007526 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007527 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7528 skip_emulated_instruction(vcpu);
7529 return 1;
7530 }
7531 /*
7532 * Now copy part of this value to register or memory, as requested.
7533 * Note that the number of bits actually copied is 32 or 64 depending
7534 * on the guest's mode (32 or 64 bit), not on the given field's length.
7535 */
7536 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007537 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007538 field_value);
7539 } else {
7540 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007541 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007542 return 1;
7543 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007544 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7545 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007546 }
7547
7548 nested_vmx_succeed(vcpu);
7549 skip_emulated_instruction(vcpu);
7550 return 1;
7551}
7552
7553
7554static int handle_vmwrite(struct kvm_vcpu *vcpu)
7555{
7556 unsigned long field;
7557 gva_t gva;
7558 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7559 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007560 /* The value to write might be 32 or 64 bits, depending on L1's long
7561 * mode, and eventually we need to write that into a field of several
7562 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007563 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007564 * bits into the vmcs12 field.
7565 */
7566 u64 field_value = 0;
7567 struct x86_exception e;
7568
7569 if (!nested_vmx_check_permission(vcpu) ||
7570 !nested_vmx_check_vmcs12(vcpu))
7571 return 1;
7572
7573 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007574 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007575 (((vmx_instruction_info) >> 3) & 0xf));
7576 else {
7577 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007578 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007579 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007580 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7581 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007582 kvm_inject_page_fault(vcpu, &e);
7583 return 1;
7584 }
7585 }
7586
7587
Nadav Amit27e6fb52014-06-18 17:19:26 +03007588 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007589 if (vmcs_field_readonly(field)) {
7590 nested_vmx_failValid(vcpu,
7591 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7592 skip_emulated_instruction(vcpu);
7593 return 1;
7594 }
7595
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007596 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007597 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7598 skip_emulated_instruction(vcpu);
7599 return 1;
7600 }
7601
7602 nested_vmx_succeed(vcpu);
7603 skip_emulated_instruction(vcpu);
7604 return 1;
7605}
7606
Nadav Har'El63846662011-05-25 23:07:29 +03007607/* Emulate the VMPTRLD instruction */
7608static int handle_vmptrld(struct kvm_vcpu *vcpu)
7609{
7610 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007611 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007612
7613 if (!nested_vmx_check_permission(vcpu))
7614 return 1;
7615
Bandan Das4291b582014-05-06 02:19:18 -04007616 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007617 return 1;
7618
Nadav Har'El63846662011-05-25 23:07:29 +03007619 if (vmx->nested.current_vmptr != vmptr) {
7620 struct vmcs12 *new_vmcs12;
7621 struct page *page;
7622 page = nested_get_page(vcpu, vmptr);
7623 if (page == NULL) {
7624 nested_vmx_failInvalid(vcpu);
7625 skip_emulated_instruction(vcpu);
7626 return 1;
7627 }
7628 new_vmcs12 = kmap(page);
7629 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7630 kunmap(page);
7631 nested_release_page_clean(page);
7632 nested_vmx_failValid(vcpu,
7633 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7634 skip_emulated_instruction(vcpu);
7635 return 1;
7636 }
Nadav Har'El63846662011-05-25 23:07:29 +03007637
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007638 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007639 vmx->nested.current_vmptr = vmptr;
7640 vmx->nested.current_vmcs12 = new_vmcs12;
7641 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007642 /*
7643 * Load VMCS12 from guest memory since it is not already
7644 * cached.
7645 */
7646 memcpy(vmx->nested.cached_vmcs12,
7647 vmx->nested.current_vmcs12, VMCS12_SIZE);
7648
Abel Gordon012f83c2013-04-18 14:39:25 +03007649 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007650 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7651 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007652 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007653 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007654 vmx->nested.sync_shadow_vmcs = true;
7655 }
Nadav Har'El63846662011-05-25 23:07:29 +03007656 }
7657
7658 nested_vmx_succeed(vcpu);
7659 skip_emulated_instruction(vcpu);
7660 return 1;
7661}
7662
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007663/* Emulate the VMPTRST instruction */
7664static int handle_vmptrst(struct kvm_vcpu *vcpu)
7665{
7666 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7667 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7668 gva_t vmcs_gva;
7669 struct x86_exception e;
7670
7671 if (!nested_vmx_check_permission(vcpu))
7672 return 1;
7673
7674 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007675 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007676 return 1;
7677 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007678 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
7679 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7680 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007681 kvm_inject_page_fault(vcpu, &e);
7682 return 1;
7683 }
7684 nested_vmx_succeed(vcpu);
7685 skip_emulated_instruction(vcpu);
7686 return 1;
7687}
7688
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007689/* Emulate the INVEPT instruction */
7690static int handle_invept(struct kvm_vcpu *vcpu)
7691{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007692 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007693 u32 vmx_instruction_info, types;
7694 unsigned long type;
7695 gva_t gva;
7696 struct x86_exception e;
7697 struct {
7698 u64 eptp, gpa;
7699 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007700
Wincy Vanb9c237b2015-02-03 23:56:30 +08007701 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7702 SECONDARY_EXEC_ENABLE_EPT) ||
7703 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007704 kvm_queue_exception(vcpu, UD_VECTOR);
7705 return 1;
7706 }
7707
7708 if (!nested_vmx_check_permission(vcpu))
7709 return 1;
7710
7711 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7712 kvm_queue_exception(vcpu, UD_VECTOR);
7713 return 1;
7714 }
7715
7716 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007717 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007718
Wincy Vanb9c237b2015-02-03 23:56:30 +08007719 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007720
Jim Mattson85c856b2016-10-26 08:38:38 -07007721 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007722 nested_vmx_failValid(vcpu,
7723 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007724 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007725 return 1;
7726 }
7727
7728 /* According to the Intel VMX instruction reference, the memory
7729 * operand is read even if it isn't needed (e.g., for type==global)
7730 */
7731 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007732 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007733 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007734 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007735 kvm_inject_page_fault(vcpu, &e);
7736 return 1;
7737 }
7738
7739 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007740 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007741 /*
7742 * TODO: track mappings and invalidate
7743 * single context requests appropriately
7744 */
7745 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007746 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007747 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007748 nested_vmx_succeed(vcpu);
7749 break;
7750 default:
7751 BUG_ON(1);
7752 break;
7753 }
7754
7755 skip_emulated_instruction(vcpu);
7756 return 1;
7757}
7758
Petr Matouseka642fc32014-09-23 20:22:30 +02007759static int handle_invvpid(struct kvm_vcpu *vcpu)
7760{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007761 struct vcpu_vmx *vmx = to_vmx(vcpu);
7762 u32 vmx_instruction_info;
7763 unsigned long type, types;
7764 gva_t gva;
7765 struct x86_exception e;
7766 int vpid;
7767
7768 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7769 SECONDARY_EXEC_ENABLE_VPID) ||
7770 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7771 kvm_queue_exception(vcpu, UD_VECTOR);
7772 return 1;
7773 }
7774
7775 if (!nested_vmx_check_permission(vcpu))
7776 return 1;
7777
7778 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7779 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7780
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007781 types = (vmx->nested.nested_vmx_vpid_caps &
7782 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007783
Jim Mattson85c856b2016-10-26 08:38:38 -07007784 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007785 nested_vmx_failValid(vcpu,
7786 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007787 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007788 return 1;
7789 }
7790
7791 /* according to the intel vmx instruction reference, the memory
7792 * operand is read even if it isn't needed (e.g., for type==global)
7793 */
7794 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7795 vmx_instruction_info, false, &gva))
7796 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007797 if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007798 kvm_inject_page_fault(vcpu, &e);
7799 return 1;
7800 }
7801
7802 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007803 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007804 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007805 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7806 if (!vpid) {
7807 nested_vmx_failValid(vcpu,
7808 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7809 skip_emulated_instruction(vcpu);
7810 return 1;
7811 }
7812 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007813 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007814 break;
7815 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007816 WARN_ON_ONCE(1);
7817 skip_emulated_instruction(vcpu);
7818 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007819 }
7820
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007821 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7822 nested_vmx_succeed(vcpu);
7823
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007824 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007825 return 1;
7826}
7827
Kai Huang843e4332015-01-28 10:54:28 +08007828static int handle_pml_full(struct kvm_vcpu *vcpu)
7829{
7830 unsigned long exit_qualification;
7831
7832 trace_kvm_pml_full(vcpu->vcpu_id);
7833
7834 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7835
7836 /*
7837 * PML buffer FULL happened while executing iret from NMI,
7838 * "blocked by NMI" bit has to be set before next VM entry.
7839 */
7840 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7841 cpu_has_virtual_nmis() &&
7842 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7843 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7844 GUEST_INTR_STATE_NMI);
7845
7846 /*
7847 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7848 * here.., and there's no userspace involvement needed for PML.
7849 */
7850 return 1;
7851}
7852
Yunhong Jiang64672c92016-06-13 14:19:59 -07007853static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7854{
7855 kvm_lapic_expired_hv_timer(vcpu);
7856 return 1;
7857}
7858
Nadav Har'El0140cae2011-05-25 23:06:28 +03007859/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007860 * The exit handlers return 1 if the exit was handled fully and guest execution
7861 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7862 * to be done to userspace and return 0.
7863 */
Mathias Krause772e0312012-08-30 01:30:19 +02007864static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007865 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7866 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007867 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007868 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007869 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007870 [EXIT_REASON_CR_ACCESS] = handle_cr,
7871 [EXIT_REASON_DR_ACCESS] = handle_dr,
7872 [EXIT_REASON_CPUID] = handle_cpuid,
7873 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7874 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7875 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7876 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007877 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007878 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007879 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007880 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007881 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007882 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007883 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007884 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007885 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007886 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007887 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007888 [EXIT_REASON_VMOFF] = handle_vmoff,
7889 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007890 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7891 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007892 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007893 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007894 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007895 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007896 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007897 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007898 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7899 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007900 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007901 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007902 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007903 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007904 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007905 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007906 [EXIT_REASON_XSAVES] = handle_xsaves,
7907 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007908 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007909 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007910};
7911
7912static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007913 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007914
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007915static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7916 struct vmcs12 *vmcs12)
7917{
7918 unsigned long exit_qualification;
7919 gpa_t bitmap, last_bitmap;
7920 unsigned int port;
7921 int size;
7922 u8 b;
7923
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007924 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007925 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007926
7927 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7928
7929 port = exit_qualification >> 16;
7930 size = (exit_qualification & 7) + 1;
7931
7932 last_bitmap = (gpa_t)-1;
7933 b = -1;
7934
7935 while (size > 0) {
7936 if (port < 0x8000)
7937 bitmap = vmcs12->io_bitmap_a;
7938 else if (port < 0x10000)
7939 bitmap = vmcs12->io_bitmap_b;
7940 else
Joe Perches1d804d02015-03-30 16:46:09 -07007941 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007942 bitmap += (port & 0x7fff) / 8;
7943
7944 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007945 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007946 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007947 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007948 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007949
7950 port++;
7951 size--;
7952 last_bitmap = bitmap;
7953 }
7954
Joe Perches1d804d02015-03-30 16:46:09 -07007955 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007956}
7957
Nadav Har'El644d7112011-05-25 23:12:35 +03007958/*
7959 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7960 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7961 * disinterest in the current event (read or write a specific MSR) by using an
7962 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7963 */
7964static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7965 struct vmcs12 *vmcs12, u32 exit_reason)
7966{
7967 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7968 gpa_t bitmap;
7969
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007970 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007971 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007972
7973 /*
7974 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7975 * for the four combinations of read/write and low/high MSR numbers.
7976 * First we need to figure out which of the four to use:
7977 */
7978 bitmap = vmcs12->msr_bitmap;
7979 if (exit_reason == EXIT_REASON_MSR_WRITE)
7980 bitmap += 2048;
7981 if (msr_index >= 0xc0000000) {
7982 msr_index -= 0xc0000000;
7983 bitmap += 1024;
7984 }
7985
7986 /* Then read the msr_index'th bit from this bitmap: */
7987 if (msr_index < 1024*8) {
7988 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007989 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007990 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007991 return 1 & (b >> (msr_index & 7));
7992 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007993 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007994}
7995
7996/*
7997 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7998 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7999 * intercept (via guest_host_mask etc.) the current event.
8000 */
8001static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
8002 struct vmcs12 *vmcs12)
8003{
8004 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8005 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008006 int reg;
8007 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03008008
8009 switch ((exit_qualification >> 4) & 3) {
8010 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008011 reg = (exit_qualification >> 8) & 15;
8012 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008013 switch (cr) {
8014 case 0:
8015 if (vmcs12->cr0_guest_host_mask &
8016 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008017 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008018 break;
8019 case 3:
8020 if ((vmcs12->cr3_target_count >= 1 &&
8021 vmcs12->cr3_target_value0 == val) ||
8022 (vmcs12->cr3_target_count >= 2 &&
8023 vmcs12->cr3_target_value1 == val) ||
8024 (vmcs12->cr3_target_count >= 3 &&
8025 vmcs12->cr3_target_value2 == val) ||
8026 (vmcs12->cr3_target_count >= 4 &&
8027 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008028 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008029 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008030 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008031 break;
8032 case 4:
8033 if (vmcs12->cr4_guest_host_mask &
8034 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008035 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008036 break;
8037 case 8:
8038 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008039 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008040 break;
8041 }
8042 break;
8043 case 2: /* clts */
8044 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8045 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008046 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008047 break;
8048 case 1: /* mov from cr */
8049 switch (cr) {
8050 case 3:
8051 if (vmcs12->cpu_based_vm_exec_control &
8052 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008053 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008054 break;
8055 case 8:
8056 if (vmcs12->cpu_based_vm_exec_control &
8057 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008058 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008059 break;
8060 }
8061 break;
8062 case 3: /* lmsw */
8063 /*
8064 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8065 * cr0. Other attempted changes are ignored, with no exit.
8066 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008067 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008068 if (vmcs12->cr0_guest_host_mask & 0xe &
8069 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008070 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008071 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8072 !(vmcs12->cr0_read_shadow & 0x1) &&
8073 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008074 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008075 break;
8076 }
Joe Perches1d804d02015-03-30 16:46:09 -07008077 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008078}
8079
8080/*
8081 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8082 * should handle it ourselves in L0 (and then continue L2). Only call this
8083 * when in is_guest_mode (L2).
8084 */
8085static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8086{
Nadav Har'El644d7112011-05-25 23:12:35 +03008087 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8088 struct vcpu_vmx *vmx = to_vmx(vcpu);
8089 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008090 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008091
Jan Kiszka542060e2014-01-04 18:47:21 +01008092 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8093 vmcs_readl(EXIT_QUALIFICATION),
8094 vmx->idt_vectoring_info,
8095 intr_info,
8096 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8097 KVM_ISA_VMX);
8098
David Matlackb7649e12017-08-01 14:00:40 -07008099 /*
8100 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008101 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8102 * Page). The CPU may write to these pages via their host
8103 * physical address while L2 is running, bypassing any
8104 * address-translation-based dirty tracking (e.g. EPT write
8105 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008106 *
8107 * Mark them dirty on every exit from L2 to prevent them from
8108 * getting out of sync with dirty tracking.
8109 */
8110 nested_mark_vmcs12_pages_dirty(vcpu);
8111
Nadav Har'El644d7112011-05-25 23:12:35 +03008112 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008113 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008114
8115 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008116 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8117 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008118 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008119 }
8120
8121 switch (exit_reason) {
8122 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008123 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008124 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008125 else if (is_page_fault(intr_info))
8126 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008127 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008128 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008129 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008130 else if (is_debug(intr_info) &&
8131 vcpu->guest_debug &
8132 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8133 return false;
8134 else if (is_breakpoint(intr_info) &&
8135 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8136 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008137 return vmcs12->exception_bitmap &
8138 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8139 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008140 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008141 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008142 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008143 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008144 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008145 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008146 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008147 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008148 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008149 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008150 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008151 case EXIT_REASON_HLT:
8152 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8153 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008154 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008155 case EXIT_REASON_INVLPG:
8156 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8157 case EXIT_REASON_RDPMC:
8158 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008159 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008160 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8161 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8162 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8163 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8164 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8165 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008166 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008167 /*
8168 * VMX instructions trap unconditionally. This allows L1 to
8169 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8170 */
Joe Perches1d804d02015-03-30 16:46:09 -07008171 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008172 case EXIT_REASON_CR_ACCESS:
8173 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8174 case EXIT_REASON_DR_ACCESS:
8175 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8176 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008177 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008178 case EXIT_REASON_MSR_READ:
8179 case EXIT_REASON_MSR_WRITE:
8180 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8181 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008182 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008183 case EXIT_REASON_MWAIT_INSTRUCTION:
8184 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008185 case EXIT_REASON_MONITOR_TRAP_FLAG:
8186 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008187 case EXIT_REASON_MONITOR_INSTRUCTION:
8188 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8189 case EXIT_REASON_PAUSE_INSTRUCTION:
8190 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8191 nested_cpu_has2(vmcs12,
8192 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8193 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008194 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008195 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008196 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008197 case EXIT_REASON_APIC_ACCESS:
8198 return nested_cpu_has2(vmcs12,
8199 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008200 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008201 case EXIT_REASON_EOI_INDUCED:
8202 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008203 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008204 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008205 /*
8206 * L0 always deals with the EPT violation. If nested EPT is
8207 * used, and the nested mmu code discovers that the address is
8208 * missing in the guest EPT table (EPT12), the EPT violation
8209 * will be injected with nested_ept_inject_page_fault()
8210 */
Joe Perches1d804d02015-03-30 16:46:09 -07008211 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008212 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008213 /*
8214 * L2 never uses directly L1's EPT, but rather L0's own EPT
8215 * table (shadow on EPT) or a merged EPT table that L0 built
8216 * (EPT on EPT). So any problems with the structure of the
8217 * table is L0's fault.
8218 */
Joe Perches1d804d02015-03-30 16:46:09 -07008219 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008220 case EXIT_REASON_WBINVD:
8221 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8222 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008223 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008224 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8225 /*
8226 * This should never happen, since it is not possible to
8227 * set XSS to a non-zero value---neither in L1 nor in L2.
8228 * If if it were, XSS would have to be checked against
8229 * the XSS exit bitmap in vmcs12.
8230 */
8231 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008232 case EXIT_REASON_PREEMPTION_TIMER:
8233 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008234 case EXIT_REASON_PML_FULL:
8235 /* We don't expose PML support to L1. */
8236 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008237 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008238 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008239 }
8240}
8241
Avi Kivity586f9602010-11-18 13:09:54 +02008242static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8243{
8244 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8245 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8246}
8247
Kai Huanga3eaa862015-11-04 13:46:05 +08008248static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008249{
Kai Huanga3eaa862015-11-04 13:46:05 +08008250 if (vmx->pml_pg) {
8251 __free_page(vmx->pml_pg);
8252 vmx->pml_pg = NULL;
8253 }
Kai Huang843e4332015-01-28 10:54:28 +08008254}
8255
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008256static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008257{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008258 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008259 u64 *pml_buf;
8260 u16 pml_idx;
8261
8262 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8263
8264 /* Do nothing if PML buffer is empty */
8265 if (pml_idx == (PML_ENTITY_NUM - 1))
8266 return;
8267
8268 /* PML index always points to next available PML buffer entity */
8269 if (pml_idx >= PML_ENTITY_NUM)
8270 pml_idx = 0;
8271 else
8272 pml_idx++;
8273
8274 pml_buf = page_address(vmx->pml_pg);
8275 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8276 u64 gpa;
8277
8278 gpa = pml_buf[pml_idx];
8279 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008280 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008281 }
8282
8283 /* reset PML index */
8284 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8285}
8286
8287/*
8288 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8289 * Called before reporting dirty_bitmap to userspace.
8290 */
8291static void kvm_flush_pml_buffers(struct kvm *kvm)
8292{
8293 int i;
8294 struct kvm_vcpu *vcpu;
8295 /*
8296 * We only need to kick vcpu out of guest mode here, as PML buffer
8297 * is flushed at beginning of all VMEXITs, and it's obvious that only
8298 * vcpus running in guest are possible to have unflushed GPAs in PML
8299 * buffer.
8300 */
8301 kvm_for_each_vcpu(i, vcpu, kvm)
8302 kvm_vcpu_kick(vcpu);
8303}
8304
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008305static void vmx_dump_sel(char *name, uint32_t sel)
8306{
8307 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008308 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008309 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8310 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8311 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8312}
8313
8314static void vmx_dump_dtsel(char *name, uint32_t limit)
8315{
8316 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8317 name, vmcs_read32(limit),
8318 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8319}
8320
8321static void dump_vmcs(void)
8322{
8323 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8324 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8325 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8326 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8327 u32 secondary_exec_control = 0;
8328 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008329 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008330 int i, n;
8331
8332 if (cpu_has_secondary_exec_ctrls())
8333 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8334
8335 pr_err("*** Guest State ***\n");
8336 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8337 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8338 vmcs_readl(CR0_GUEST_HOST_MASK));
8339 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8340 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8341 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8342 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8343 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8344 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008345 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8346 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8347 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8348 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008349 }
8350 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8351 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8352 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8353 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8354 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8355 vmcs_readl(GUEST_SYSENTER_ESP),
8356 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8357 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8358 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8359 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8360 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8361 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8362 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8363 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8364 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8365 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8366 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8367 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8368 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008369 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8370 efer, vmcs_read64(GUEST_IA32_PAT));
8371 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8372 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008373 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8374 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008375 pr_err("PerfGlobCtl = 0x%016llx\n",
8376 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008377 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008378 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008379 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8380 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8381 vmcs_read32(GUEST_ACTIVITY_STATE));
8382 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8383 pr_err("InterruptStatus = %04x\n",
8384 vmcs_read16(GUEST_INTR_STATUS));
8385
8386 pr_err("*** Host State ***\n");
8387 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8388 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8389 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8390 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8391 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8392 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8393 vmcs_read16(HOST_TR_SELECTOR));
8394 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8395 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8396 vmcs_readl(HOST_TR_BASE));
8397 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8398 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8399 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8400 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8401 vmcs_readl(HOST_CR4));
8402 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8403 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8404 vmcs_read32(HOST_IA32_SYSENTER_CS),
8405 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8406 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008407 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8408 vmcs_read64(HOST_IA32_EFER),
8409 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008410 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008411 pr_err("PerfGlobCtl = 0x%016llx\n",
8412 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008413
8414 pr_err("*** Control State ***\n");
8415 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8416 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8417 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8418 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8419 vmcs_read32(EXCEPTION_BITMAP),
8420 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8421 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8422 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8423 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8424 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8425 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8426 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8427 vmcs_read32(VM_EXIT_INTR_INFO),
8428 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8429 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8430 pr_err(" reason=%08x qualification=%016lx\n",
8431 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8432 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8433 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8434 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008435 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008436 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008437 pr_err("TSC Multiplier = 0x%016llx\n",
8438 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008439 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8440 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8441 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8442 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8443 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008444 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008445 n = vmcs_read32(CR3_TARGET_COUNT);
8446 for (i = 0; i + 1 < n; i += 4)
8447 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8448 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8449 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8450 if (i < n)
8451 pr_err("CR3 target%u=%016lx\n",
8452 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8453 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8454 pr_err("PLE Gap=%08x Window=%08x\n",
8455 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8456 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8457 pr_err("Virtual processor ID = 0x%04x\n",
8458 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8459}
8460
Avi Kivity6aa8b732006-12-10 02:21:36 -08008461/*
8462 * The guest has exited. See if we can fix it or if we need userspace
8463 * assistance.
8464 */
Avi Kivity851ba692009-08-24 11:10:17 +03008465static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008466{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008467 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008468 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008469 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008470
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008471 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8472
Kai Huang843e4332015-01-28 10:54:28 +08008473 /*
8474 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8475 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8476 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8477 * mode as if vcpus is in root mode, the PML buffer must has been
8478 * flushed already.
8479 */
8480 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008481 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008482
Mohammed Gamal80ced182009-09-01 12:48:18 +02008483 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008484 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008485 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008486
Nadav Har'El644d7112011-05-25 23:12:35 +03008487 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008488 nested_vmx_vmexit(vcpu, exit_reason,
8489 vmcs_read32(VM_EXIT_INTR_INFO),
8490 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008491 return 1;
8492 }
8493
Mohammed Gamal51207022010-05-31 22:40:54 +03008494 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008495 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008496 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8497 vcpu->run->fail_entry.hardware_entry_failure_reason
8498 = exit_reason;
8499 return 0;
8500 }
8501
Avi Kivity29bd8a72007-09-10 17:27:03 +03008502 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008503 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8504 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008505 = vmcs_read32(VM_INSTRUCTION_ERROR);
8506 return 0;
8507 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008508
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008509 /*
8510 * Note:
8511 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8512 * delivery event since it indicates guest is accessing MMIO.
8513 * The vm-exit can be triggered again after return to guest that
8514 * will cause infinite loop.
8515 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008516 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008517 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008518 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008519 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008520 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8521 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8522 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8523 vcpu->run->internal.ndata = 2;
8524 vcpu->run->internal.data[0] = vectoring_info;
8525 vcpu->run->internal.data[1] = exit_reason;
8526 return 0;
8527 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008528
Nadav Har'El644d7112011-05-25 23:12:35 +03008529 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8530 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008531 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008532 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008533 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008534 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008535 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008536 /*
8537 * This CPU don't support us in finding the end of an
8538 * NMI-blocked window if the guest runs with IRQs
8539 * disabled. So we pull the trigger after 1 s of
8540 * futile waiting, but inform the user about this.
8541 */
8542 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8543 "state on VCPU %d after 1 s timeout\n",
8544 __func__, vcpu->vcpu_id);
8545 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008546 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008547 }
8548
Avi Kivity6aa8b732006-12-10 02:21:36 -08008549 if (exit_reason < kvm_vmx_max_exit_handlers
8550 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008551 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008552 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008553 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8554 kvm_queue_exception(vcpu, UD_VECTOR);
8555 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008556 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008557}
8558
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008559/*
8560 * Software based L1D cache flush which is used when microcode providing
8561 * the cache control MSR is not loaded.
8562 *
8563 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8564 * flush it is required to read in 64 KiB because the replacement algorithm
8565 * is not exactly LRU. This could be sized at runtime via topology
8566 * information but as all relevant affected CPUs have 32KiB L1D cache size
8567 * there is no point in doing so.
8568 */
8569#define L1D_CACHE_ORDER 4
8570static void *vmx_l1d_flush_pages;
8571
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008572static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008573{
8574 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008575 bool always;
8576
8577 /*
8578 * If the mitigation mode is 'flush always', keep the flush bit
8579 * set, otherwise clear it. It gets set again either from
8580 * vcpu_run() or from one of the unsafe VMEXIT handlers.
8581 */
8582 always = vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS;
8583 vcpu->arch.l1tf_flush_l1d = always;
8584
8585 vcpu->stat.l1d_flush++;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008586
Paolo Bonziniacca8a72018-07-02 13:03:48 +02008587 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8588 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8589 return;
8590 }
8591
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008592 asm volatile(
8593 /* First ensure the pages are in the TLB */
8594 "xorl %%eax, %%eax\n"
8595 ".Lpopulate_tlb:\n\t"
8596 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
8597 "addl $4096, %%eax\n\t"
8598 "cmpl %%eax, %[size]\n\t"
8599 "jne .Lpopulate_tlb\n\t"
8600 "xorl %%eax, %%eax\n\t"
8601 "cpuid\n\t"
8602 /* Now fill the cache */
8603 "xorl %%eax, %%eax\n"
8604 ".Lfill_cache:\n"
8605 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
8606 "addl $64, %%eax\n\t"
8607 "cmpl %%eax, %[size]\n\t"
8608 "jne .Lfill_cache\n\t"
8609 "lfence\n"
8610 :: [empty_zp] "r" (vmx_l1d_flush_pages),
8611 [size] "r" (size)
8612 : "eax", "ebx", "ecx", "edx");
8613}
8614
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008615static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008616{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008617 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8618
8619 if (is_guest_mode(vcpu) &&
8620 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8621 return;
8622
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008623 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008624 vmcs_write32(TPR_THRESHOLD, 0);
8625 return;
8626 }
8627
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008628 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008629}
8630
Yang Zhang8d146952013-01-25 10:18:50 +08008631static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8632{
8633 u32 sec_exec_control;
8634
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008635 /* Postpone execution until vmcs01 is the current VMCS. */
8636 if (is_guest_mode(vcpu)) {
8637 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8638 return;
8639 }
8640
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008641 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008642 return;
8643
Paolo Bonzini35754c92015-07-29 12:05:37 +02008644 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008645 return;
8646
8647 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8648
8649 if (set) {
8650 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8651 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8652 } else {
8653 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8654 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008655 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008656 }
8657 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8658
Paolo Bonzini6236b782018-01-16 16:51:18 +01008659 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008660}
8661
Tang Chen38b99172014-09-24 15:57:54 +08008662static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8663{
8664 struct vcpu_vmx *vmx = to_vmx(vcpu);
8665
8666 /*
8667 * Currently we do not handle the nested case where L2 has an
8668 * APIC access page of its own; that page is still pinned.
8669 * Hence, we skip the case where the VCPU is in guest mode _and_
8670 * L1 prepared an APIC access page for L2.
8671 *
8672 * For the case where L1 and L2 share the same APIC access page
8673 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8674 * in the vmcs12), this function will only update either the vmcs01
8675 * or the vmcs02. If the former, the vmcs02 will be updated by
8676 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8677 * the next L2->L1 exit.
8678 */
8679 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008680 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008681 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008682 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008683 vmx_flush_tlb_ept_only(vcpu);
8684 }
Tang Chen38b99172014-09-24 15:57:54 +08008685}
8686
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008687static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008688{
8689 u16 status;
8690 u8 old;
8691
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008692 if (max_isr == -1)
8693 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008694
8695 status = vmcs_read16(GUEST_INTR_STATUS);
8696 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008697 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008698 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008699 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008700 vmcs_write16(GUEST_INTR_STATUS, status);
8701 }
8702}
8703
8704static void vmx_set_rvi(int vector)
8705{
8706 u16 status;
8707 u8 old;
8708
Wei Wang4114c272014-11-05 10:53:43 +08008709 if (vector == -1)
8710 vector = 0;
8711
Yang Zhangc7c9c562013-01-25 10:18:51 +08008712 status = vmcs_read16(GUEST_INTR_STATUS);
8713 old = (u8)status & 0xff;
8714 if ((u8)vector != old) {
8715 status &= ~0xff;
8716 status |= (u8)vector;
8717 vmcs_write16(GUEST_INTR_STATUS, status);
8718 }
8719}
8720
8721static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8722{
Wanpeng Li963fee12014-07-17 19:03:00 +08008723 if (!is_guest_mode(vcpu)) {
8724 vmx_set_rvi(max_irr);
8725 return;
8726 }
8727
Wei Wang4114c272014-11-05 10:53:43 +08008728 if (max_irr == -1)
8729 return;
8730
Wanpeng Li963fee12014-07-17 19:03:00 +08008731 /*
Wei Wang4114c272014-11-05 10:53:43 +08008732 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8733 * handles it.
8734 */
8735 if (nested_exit_on_intr(vcpu))
8736 return;
8737
8738 /*
8739 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008740 * is run without virtual interrupt delivery.
8741 */
8742 if (!kvm_event_needs_reinjection(vcpu) &&
8743 vmx_interrupt_allowed(vcpu)) {
8744 kvm_queue_interrupt(vcpu, max_irr, false);
8745 vmx_inject_irq(vcpu);
8746 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008747}
8748
Andrey Smetanin63086302015-11-10 15:36:32 +03008749static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008750{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008751 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008752 return;
8753
Yang Zhangc7c9c562013-01-25 10:18:51 +08008754 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8755 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8756 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8757 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8758}
8759
Avi Kivity51aa01d2010-07-20 14:31:20 +03008760static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008761{
Avi Kivity00eba012011-03-07 17:24:54 +02008762 u32 exit_intr_info;
8763
8764 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8765 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8766 return;
8767
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008768 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008769 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008770
8771 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008772 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008773 kvm_machine_check();
8774
Gleb Natapov20f65982009-05-11 13:35:55 +03008775 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008776 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008777 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008778 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008779 kvm_after_handle_nmi(&vmx->vcpu);
8780 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008781}
Gleb Natapov20f65982009-05-11 13:35:55 +03008782
Yang Zhanga547c6d2013-04-11 19:25:10 +08008783static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8784{
8785 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008786 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008787
8788 /*
8789 * If external interrupt exists, IF bit is set in rflags/eflags on the
8790 * interrupt stack frame, and interrupt will be enabled on a return
8791 * from interrupt handler.
8792 */
8793 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8794 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8795 unsigned int vector;
8796 unsigned long entry;
8797 gate_desc *desc;
8798 struct vcpu_vmx *vmx = to_vmx(vcpu);
8799#ifdef CONFIG_X86_64
8800 unsigned long tmp;
8801#endif
8802
8803 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8804 desc = (gate_desc *)vmx->host_idt_base + vector;
8805 entry = gate_offset(*desc);
8806 asm volatile(
8807#ifdef CONFIG_X86_64
8808 "mov %%" _ASM_SP ", %[sp]\n\t"
8809 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8810 "push $%c[ss]\n\t"
8811 "push %[sp]\n\t"
8812#endif
8813 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008814 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008815 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008816 :
8817#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008818 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008819#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008820 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008821 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008822 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008823 [ss]"i"(__KERNEL_DS),
8824 [cs]"i"(__KERNEL_CS)
8825 );
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008826 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008827 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008828}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05008829STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008830
Tom Lendackyb9655922018-05-10 22:06:39 +02008831static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008832{
Tom Lendackyb9655922018-05-10 22:06:39 +02008833 switch (index) {
8834 case MSR_IA32_SMBASE:
8835 /*
8836 * We cannot do SMM unless we can run the guest in big
8837 * real mode.
8838 */
8839 return enable_unrestricted_guest || emulate_invalid_guest_state;
8840 case MSR_AMD64_VIRT_SPEC_CTRL:
8841 /* This is AMD only. */
8842 return false;
8843 default:
8844 return true;
8845 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008846}
8847
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008848static bool vmx_mpx_supported(void)
8849{
8850 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8851 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8852}
8853
Wanpeng Li55412b22014-12-02 19:21:30 +08008854static bool vmx_xsaves_supported(void)
8855{
8856 return vmcs_config.cpu_based_2nd_exec_ctrl &
8857 SECONDARY_EXEC_XSAVES;
8858}
8859
Avi Kivity51aa01d2010-07-20 14:31:20 +03008860static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8861{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008862 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008863 bool unblock_nmi;
8864 u8 vector;
8865 bool idtv_info_valid;
8866
8867 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008868
Avi Kivitycf393f72008-07-01 16:20:21 +03008869 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008870 if (vmx->nmi_known_unmasked)
8871 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008872 /*
8873 * Can't use vmx->exit_intr_info since we're not sure what
8874 * the exit reason is.
8875 */
8876 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008877 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8878 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8879 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008880 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008881 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8882 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008883 * SDM 3: 23.2.2 (September 2008)
8884 * Bit 12 is undefined in any of the following cases:
8885 * If the VM exit sets the valid bit in the IDT-vectoring
8886 * information field.
8887 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008888 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008889 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8890 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008891 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8892 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008893 else
8894 vmx->nmi_known_unmasked =
8895 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8896 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008897 } else if (unlikely(vmx->soft_vnmi_blocked))
8898 vmx->vnmi_blocked_time +=
8899 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008900}
8901
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008902static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008903 u32 idt_vectoring_info,
8904 int instr_len_field,
8905 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008906{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008907 u8 vector;
8908 int type;
8909 bool idtv_info_valid;
8910
8911 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008912
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008913 vcpu->arch.nmi_injected = false;
8914 kvm_clear_exception_queue(vcpu);
8915 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008916
8917 if (!idtv_info_valid)
8918 return;
8919
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008920 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008921
Avi Kivity668f6122008-07-02 09:28:55 +03008922 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8923 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008924
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008925 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008926 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008927 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008928 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008929 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008930 * Clear bit "block by NMI" before VM entry if a NMI
8931 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008932 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008933 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008934 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008935 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008936 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008937 /* fall through */
8938 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008939 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008940 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008941 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008942 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008943 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008944 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008945 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008946 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008947 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008948 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008949 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008950 break;
8951 default:
8952 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008953 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008954}
8955
Avi Kivity83422e12010-07-20 14:43:23 +03008956static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8957{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008958 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008959 VM_EXIT_INSTRUCTION_LEN,
8960 IDT_VECTORING_ERROR_CODE);
8961}
8962
Avi Kivityb463a6f2010-07-20 15:06:17 +03008963static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8964{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008965 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008966 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8967 VM_ENTRY_INSTRUCTION_LEN,
8968 VM_ENTRY_EXCEPTION_ERROR_CODE);
8969
8970 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8971}
8972
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008973static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8974{
8975 int i, nr_msrs;
8976 struct perf_guest_switch_msr *msrs;
8977
8978 msrs = perf_guest_get_msrs(&nr_msrs);
8979
8980 if (!msrs)
8981 return;
8982
8983 for (i = 0; i < nr_msrs; i++)
8984 if (msrs[i].host == msrs[i].guest)
8985 clear_atomic_switch_msr(vmx, msrs[i].msr);
8986 else
8987 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8988 msrs[i].host);
8989}
8990
Yunhong Jiang64672c92016-06-13 14:19:59 -07008991void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8992{
8993 struct vcpu_vmx *vmx = to_vmx(vcpu);
8994 u64 tscl;
8995 u32 delta_tsc;
8996
8997 if (vmx->hv_deadline_tsc == -1)
8998 return;
8999
9000 tscl = rdtsc();
9001 if (vmx->hv_deadline_tsc > tscl)
9002 /* sure to be 32 bit only because checked on set_hv_timer */
9003 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
9004 cpu_preemption_timer_multi);
9005 else
9006 delta_tsc = 0;
9007
9008 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9009}
9010
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009011static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009012{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009013 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009014 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009015
9016 /* Record the guest's net vcpu time for enforced NMI injections. */
9017 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
9018 vmx->entry_time = ktime_get();
9019
9020 /* Don't enter VMX if guest state is invalid, let the exit handler
9021 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009022 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009023 return;
9024
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009025 if (vmx->ple_window_dirty) {
9026 vmx->ple_window_dirty = false;
9027 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9028 }
9029
Abel Gordon012f83c2013-04-18 14:39:25 +03009030 if (vmx->nested.sync_shadow_vmcs) {
9031 copy_vmcs12_to_shadow(vmx);
9032 vmx->nested.sync_shadow_vmcs = false;
9033 }
9034
Avi Kivity104f2262010-11-18 13:12:52 +02009035 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9036 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9037 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9038 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9039
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009040 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009041 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9042 vmcs_writel(HOST_CR4, cr4);
9043 vmx->host_state.vmcs_host_cr4 = cr4;
9044 }
9045
Avi Kivity104f2262010-11-18 13:12:52 +02009046 /* When single-stepping over STI and MOV SS, we must clear the
9047 * corresponding interruptibility bits in the guest state. Otherwise
9048 * vmentry fails as it then expects bit 14 (BS) in pending debug
9049 * exceptions being set, but that's not correct for the guest debugging
9050 * case. */
9051 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9052 vmx_set_interrupt_shadow(vcpu, 0);
9053
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009054 if (vmx->guest_pkru_valid)
9055 __write_pkru(vmx->guest_pkru);
9056
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009057 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009058 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009059
Yunhong Jiang64672c92016-06-13 14:19:59 -07009060 vmx_arm_hv_timer(vcpu);
9061
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009062 /*
9063 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9064 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9065 * is no need to worry about the conditional branch over the wrmsr
9066 * being speculatively taken.
9067 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009068 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009069
Nadav Har'Eld462b812011-05-24 15:26:10 +03009070 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009071
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02009072 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
9073 if (vcpu->arch.l1tf_flush_l1d)
9074 vmx_l1d_flush(vcpu);
9075 }
9076
Avi Kivity104f2262010-11-18 13:12:52 +02009077 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009078 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009079 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9080 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9081 "push %%" _ASM_CX " \n\t"
9082 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009083 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009084 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009085 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009086 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009087 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009088 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9089 "mov %%cr2, %%" _ASM_DX " \n\t"
9090 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009091 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009092 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009093 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009094 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009095 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009096 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009097 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9098 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9099 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9100 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9101 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9102 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009103#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009104 "mov %c[r8](%0), %%r8 \n\t"
9105 "mov %c[r9](%0), %%r9 \n\t"
9106 "mov %c[r10](%0), %%r10 \n\t"
9107 "mov %c[r11](%0), %%r11 \n\t"
9108 "mov %c[r12](%0), %%r12 \n\t"
9109 "mov %c[r13](%0), %%r13 \n\t"
9110 "mov %c[r14](%0), %%r14 \n\t"
9111 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009112#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009113 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009114
Avi Kivity6aa8b732006-12-10 02:21:36 -08009115 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009116 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009117 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009118 "jmp 2f \n\t"
9119 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9120 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009121 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03009122 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009123 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009124 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009125 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9126 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9127 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9128 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9129 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9130 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9131 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009132#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009133 "mov %%r8, %c[r8](%0) \n\t"
9134 "mov %%r9, %c[r9](%0) \n\t"
9135 "mov %%r10, %c[r10](%0) \n\t"
9136 "mov %%r11, %c[r11](%0) \n\t"
9137 "mov %%r12, %c[r12](%0) \n\t"
9138 "mov %%r13, %c[r13](%0) \n\t"
9139 "mov %%r14, %c[r14](%0) \n\t"
9140 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009141 "xor %%r8d, %%r8d \n\t"
9142 "xor %%r9d, %%r9d \n\t"
9143 "xor %%r10d, %%r10d \n\t"
9144 "xor %%r11d, %%r11d \n\t"
9145 "xor %%r12d, %%r12d \n\t"
9146 "xor %%r13d, %%r13d \n\t"
9147 "xor %%r14d, %%r14d \n\t"
9148 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009149#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009150 "mov %%cr2, %%" _ASM_AX " \n\t"
9151 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009152
Jim Mattson491c0ca2018-01-03 14:31:38 -08009153 "xor %%eax, %%eax \n\t"
9154 "xor %%ebx, %%ebx \n\t"
9155 "xor %%esi, %%esi \n\t"
9156 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009157 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009158 ".pushsection .rodata \n\t"
9159 ".global vmx_return \n\t"
9160 "vmx_return: " _ASM_PTR " 2b \n\t"
9161 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009162 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009163 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009164 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009165 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009166 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9167 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9168 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9169 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9170 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9171 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9172 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009173#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009174 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9175 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9176 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9177 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9178 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9179 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9180 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9181 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009182#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009183 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9184 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009185 : "cc", "memory"
9186#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009187 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009188 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009189#else
9190 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009191#endif
9192 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009193
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009194 /*
9195 * We do not use IBRS in the kernel. If this vCPU has used the
9196 * SPEC_CTRL MSR it may have left it on; save the value and
9197 * turn it off. This is much more efficient than blindly adding
9198 * it to the atomic save/restore list. Especially as the former
9199 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9200 *
9201 * For non-nested case:
9202 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9203 * save it.
9204 *
9205 * For nested case:
9206 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9207 * save it.
9208 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009209 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009210 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009211
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009212 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009213
David Woodhousec1ddd992018-01-12 11:11:27 +00009214 /* Eliminate branch target predictions from guest mode */
9215 vmexit_fill_RSB();
9216
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009217 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9218 if (debugctlmsr)
9219 update_debugctlmsr(debugctlmsr);
9220
Avi Kivityaa67f602012-08-01 16:48:03 +03009221#ifndef CONFIG_X86_64
9222 /*
9223 * The sysexit path does not restore ds/es, so we must set them to
9224 * a reasonable value ourselves.
9225 *
9226 * We can't defer this to vmx_load_host_state() since that function
9227 * may be executed in interrupt context, which saves and restore segments
9228 * around it, nullifying its effect.
9229 */
9230 loadsegment(ds, __USER_DS);
9231 loadsegment(es, __USER_DS);
9232#endif
9233
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009234 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009235 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009236 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009237 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009238 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009239 vcpu->arch.regs_dirty = 0;
9240
Avi Kivity1155f762007-11-22 11:30:47 +02009241 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9242
Nadav Har'Eld462b812011-05-24 15:26:10 +03009243 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009244
Avi Kivity51aa01d2010-07-20 14:31:20 +03009245 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009246
Gleb Natapove0b890d2013-09-25 12:51:33 +03009247 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009248 * eager fpu is enabled if PKEY is supported and CR4 is switched
9249 * back on host, so it is safe to read guest PKRU from current
9250 * XSAVE.
9251 */
9252 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9253 vmx->guest_pkru = __read_pkru();
9254 if (vmx->guest_pkru != vmx->host_pkru) {
9255 vmx->guest_pkru_valid = true;
9256 __write_pkru(vmx->host_pkru);
9257 } else
9258 vmx->guest_pkru_valid = false;
9259 }
9260
9261 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009262 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9263 * we did not inject a still-pending event to L1 now because of
9264 * nested_run_pending, we need to re-enable this bit.
9265 */
9266 if (vmx->nested.nested_run_pending)
9267 kvm_make_request(KVM_REQ_EVENT, vcpu);
9268
9269 vmx->nested.nested_run_pending = 0;
9270
Avi Kivity51aa01d2010-07-20 14:31:20 +03009271 vmx_complete_atomic_exit(vmx);
9272 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009273 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009274}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05009275STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009276
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009277static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9278{
9279 struct vcpu_vmx *vmx = to_vmx(vcpu);
9280 int cpu;
9281
9282 if (vmx->loaded_vmcs == &vmx->vmcs01)
9283 return;
9284
9285 cpu = get_cpu();
9286 vmx->loaded_vmcs = &vmx->vmcs01;
9287 vmx_vcpu_put(vcpu);
9288 vmx_vcpu_load(vcpu, cpu);
9289 vcpu->cpu = cpu;
9290 put_cpu();
9291}
9292
Jim Mattson2f1fe812016-07-08 15:36:06 -07009293/*
9294 * Ensure that the current vmcs of the logical processor is the
9295 * vmcs01 of the vcpu before calling free_nested().
9296 */
9297static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9298{
9299 struct vcpu_vmx *vmx = to_vmx(vcpu);
9300 int r;
9301
9302 r = vcpu_load(vcpu);
9303 BUG_ON(r);
9304 vmx_load_vmcs01(vcpu);
9305 free_nested(vmx);
9306 vcpu_put(vcpu);
9307}
9308
Avi Kivity6aa8b732006-12-10 02:21:36 -08009309static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9310{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009311 struct vcpu_vmx *vmx = to_vmx(vcpu);
9312
Kai Huang843e4332015-01-28 10:54:28 +08009313 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009314 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009315 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009316 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009317 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009318 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009319 kfree(vmx->guest_msrs);
9320 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009321 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009322}
9323
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009324static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009325{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009326 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009327 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009328 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009329 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009330
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009331 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009332 return ERR_PTR(-ENOMEM);
9333
Wanpeng Li991e7a02015-09-16 17:30:05 +08009334 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009335
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009336 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9337 if (err)
9338 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009339
Peter Feiner4e595162016-07-07 14:49:58 -07009340 err = -ENOMEM;
9341
9342 /*
9343 * If PML is turned on, failure on enabling PML just results in failure
9344 * of creating the vcpu, therefore we can simplify PML logic (by
9345 * avoiding dealing with cases, such as enabling PML partially on vcpus
9346 * for the guest, etc.
9347 */
9348 if (enable_pml) {
9349 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9350 if (!vmx->pml_pg)
9351 goto uninit_vcpu;
9352 }
9353
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009354 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009355 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9356 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009357
Peter Feiner4e595162016-07-07 14:49:58 -07009358 if (!vmx->guest_msrs)
9359 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009360
Nadav Har'Eld462b812011-05-24 15:26:10 +03009361 if (!vmm_exclusive)
9362 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009363 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009364 if (!vmm_exclusive)
9365 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009366 if (err < 0)
9367 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009368
Paolo Bonzini6236b782018-01-16 16:51:18 +01009369 msr_bitmap = vmx->vmcs01.msr_bitmap;
9370 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9371 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9372 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9373 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9374 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9375 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9376 vmx->msr_bitmap_mode = 0;
9377
Paolo Bonziniff546f92018-01-11 12:16:15 +01009378 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009379 cpu = get_cpu();
9380 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009381 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009382 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009383 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009384 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009385 if (err)
9386 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009387 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009388 err = alloc_apic_access_page(kvm);
9389 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009390 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009391 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009392
Sheng Yangb927a3c2009-07-21 10:42:48 +08009393 if (enable_ept) {
9394 if (!kvm->arch.ept_identity_map_addr)
9395 kvm->arch.ept_identity_map_addr =
9396 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009397 err = init_rmode_identity_map(kvm);
9398 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009399 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009400 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009401
Roman Kagan020a90f2018-07-19 21:59:07 +03009402 if (nested)
Wincy Vanb9c237b2015-02-03 23:56:30 +08009403 nested_vmx_setup_ctls_msrs(vmx);
9404
Wincy Van705699a2015-02-03 23:58:17 +08009405 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009406 vmx->nested.current_vmptr = -1ull;
9407 vmx->nested.current_vmcs12 = NULL;
9408
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009409 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9410
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009411 /*
9412 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9413 * or POSTED_INTR_WAKEUP_VECTOR.
9414 */
9415 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9416 vmx->pi_desc.sn = 1;
9417
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009418 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009419
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009420free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009421 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009422free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009423 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009424free_pml:
9425 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009426uninit_vcpu:
9427 kvm_vcpu_uninit(&vmx->vcpu);
9428free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009429 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009430 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009431 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009432}
9433
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009434#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
9435
9436static int vmx_vm_init(struct kvm *kvm)
9437{
9438 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
9439 if (nosmt) {
9440 pr_err(L1TF_MSG);
9441 return -EOPNOTSUPP;
9442 }
9443 pr_warn(L1TF_MSG);
9444 }
9445 return 0;
9446}
9447
Yang, Sheng002c7f72007-07-31 14:23:01 +03009448static void __init vmx_check_processor_compat(void *rtn)
9449{
9450 struct vmcs_config vmcs_conf;
9451
9452 *(int *)rtn = 0;
9453 if (setup_vmcs_config(&vmcs_conf) < 0)
9454 *(int *)rtn = -EIO;
9455 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9456 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9457 smp_processor_id());
9458 *(int *)rtn = -EIO;
9459 }
9460}
9461
Sheng Yang67253af2008-04-25 10:20:22 +08009462static int get_ept_level(void)
9463{
9464 return VMX_EPT_DEFAULT_GAW + 1;
9465}
9466
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009467static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009468{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009469 u8 cache;
9470 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009471
Sheng Yang522c68c2009-04-27 20:35:43 +08009472 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009473 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009474 * 2. EPT with VT-d:
9475 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009476 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009477 * b. VT-d with snooping control feature: snooping control feature of
9478 * VT-d engine can guarantee the cache correctness. Just set it
9479 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009480 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009481 * consistent with host MTRR
9482 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009483 if (is_mmio) {
9484 cache = MTRR_TYPE_UNCACHABLE;
9485 goto exit;
9486 }
9487
9488 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009489 ipat = VMX_EPT_IPAT_BIT;
9490 cache = MTRR_TYPE_WRBACK;
9491 goto exit;
9492 }
9493
9494 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9495 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009496 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009497 cache = MTRR_TYPE_WRBACK;
9498 else
9499 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009500 goto exit;
9501 }
9502
Xiao Guangrongff536042015-06-15 16:55:22 +08009503 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009504
9505exit:
9506 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009507}
9508
Sheng Yang17cc3932010-01-05 19:02:27 +08009509static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009510{
Sheng Yang878403b2010-01-05 19:02:29 +08009511 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9512 return PT_DIRECTORY_LEVEL;
9513 else
9514 /* For shadow and EPT supported 1GB page */
9515 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009516}
9517
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009518static void vmcs_set_secondary_exec_control(u32 new_ctl)
9519{
9520 /*
9521 * These bits in the secondary execution controls field
9522 * are dynamic, the others are mostly based on the hypervisor
9523 * architecture and the guest's CPUID. Do not touch the
9524 * dynamic bits.
9525 */
9526 u32 mask =
9527 SECONDARY_EXEC_SHADOW_VMCS |
9528 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9529 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9530
9531 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9532
9533 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9534 (new_ctl & ~mask) | (cur_ctl & mask));
9535}
9536
Sheng Yang0e851882009-12-18 16:48:46 +08009537static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9538{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009539 struct kvm_cpuid_entry2 *best;
9540 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009541 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009542
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009543 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009544 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9545 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009546 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009547
Paolo Bonzini8b972652015-09-15 17:34:42 +02009548 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009549 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009550 vmx->nested.nested_vmx_secondary_ctls_high |=
9551 SECONDARY_EXEC_RDTSCP;
9552 else
9553 vmx->nested.nested_vmx_secondary_ctls_high &=
9554 ~SECONDARY_EXEC_RDTSCP;
9555 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009556 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009557
Mao, Junjiead756a12012-07-02 01:18:48 +00009558 /* Exposing INVPCID only when PCID is exposed */
9559 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9560 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009561 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9562 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009563 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009564
Mao, Junjiead756a12012-07-02 01:18:48 +00009565 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009566 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009567 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009568
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009569 if (cpu_has_secondary_exec_ctrls())
9570 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009571
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009572 if (nested_vmx_allowed(vcpu))
9573 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9574 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9575 else
9576 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9577 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009578}
9579
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009580static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9581{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009582 if (func == 1 && nested)
9583 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009584}
9585
Yang Zhang25d92082013-08-06 12:00:32 +03009586static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9587 struct x86_exception *fault)
9588{
Jan Kiszka533558b2014-01-04 18:47:20 +01009589 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9590 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009591
9592 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009593 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009594 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009595 exit_reason = EXIT_REASON_EPT_VIOLATION;
9596 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009597 vmcs12->guest_physical_address = fault->address;
9598}
9599
Nadav Har'El155a97a2013-08-05 11:07:16 +03009600/* Callbacks for nested_ept_init_mmu_context: */
9601
9602static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9603{
9604 /* return the page table to be shadowed - in our case, EPT12 */
9605 return get_vmcs12(vcpu)->ept_pointer;
9606}
9607
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009608static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009609{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009610 WARN_ON(mmu_is_nested(vcpu));
9611 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009612 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9613 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009614 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9615 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9616 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9617
9618 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009619}
9620
9621static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9622{
9623 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9624}
9625
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009626static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9627 u16 error_code)
9628{
9629 bool inequality, bit;
9630
9631 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9632 inequality =
9633 (error_code & vmcs12->page_fault_error_code_mask) !=
9634 vmcs12->page_fault_error_code_match;
9635 return inequality ^ bit;
9636}
9637
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009638static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9639 struct x86_exception *fault)
9640{
9641 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9642
9643 WARN_ON(!is_guest_mode(vcpu));
9644
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009645 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009646 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9647 vmcs_read32(VM_EXIT_INTR_INFO),
9648 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009649 else
9650 kvm_inject_page_fault(vcpu, fault);
9651}
9652
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009653static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9654 struct vmcs12 *vmcs12)
9655{
9656 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009657 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009658
9659 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009660 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9661 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009662 return false;
9663
9664 /*
9665 * Translate L1 physical address to host physical
9666 * address for vmcs02. Keep the page pinned, so this
9667 * physical address remains valid. We keep a reference
9668 * to it so we can release it later.
9669 */
9670 if (vmx->nested.apic_access_page) /* shouldn't happen */
9671 nested_release_page(vmx->nested.apic_access_page);
9672 vmx->nested.apic_access_page =
9673 nested_get_page(vcpu, vmcs12->apic_access_addr);
9674 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009675
9676 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009677 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9678 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009679 return false;
9680
9681 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9682 nested_release_page(vmx->nested.virtual_apic_page);
9683 vmx->nested.virtual_apic_page =
9684 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9685
9686 /*
9687 * Failing the vm entry is _not_ what the processor does
9688 * but it's basically the only possibility we have.
9689 * We could still enter the guest if CR8 load exits are
9690 * enabled, CR8 store exits are enabled, and virtualize APIC
9691 * access is disabled; in this case the processor would never
9692 * use the TPR shadow and we could simply clear the bit from
9693 * the execution control. But such a configuration is useless,
9694 * so let's keep the code simple.
9695 */
9696 if (!vmx->nested.virtual_apic_page)
9697 return false;
9698 }
9699
Wincy Van705699a2015-02-03 23:58:17 +08009700 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009701 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9702 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009703 return false;
9704
9705 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9706 kunmap(vmx->nested.pi_desc_page);
9707 nested_release_page(vmx->nested.pi_desc_page);
9708 }
9709 vmx->nested.pi_desc_page =
9710 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9711 if (!vmx->nested.pi_desc_page)
9712 return false;
9713
9714 vmx->nested.pi_desc =
9715 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9716 if (!vmx->nested.pi_desc) {
9717 nested_release_page_clean(vmx->nested.pi_desc_page);
9718 return false;
9719 }
9720 vmx->nested.pi_desc =
9721 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9722 (unsigned long)(vmcs12->posted_intr_desc_addr &
9723 (PAGE_SIZE - 1)));
9724 }
9725
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009726 return true;
9727}
9728
Jan Kiszkaf4124502014-03-07 20:03:13 +01009729static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9730{
9731 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9732 struct vcpu_vmx *vmx = to_vmx(vcpu);
9733
9734 if (vcpu->arch.virtual_tsc_khz == 0)
9735 return;
9736
9737 /* Make sure short timeouts reliably trigger an immediate vmexit.
9738 * hrtimer_start does not guarantee this. */
9739 if (preemption_timeout <= 1) {
9740 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9741 return;
9742 }
9743
9744 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9745 preemption_timeout *= 1000000;
9746 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9747 hrtimer_start(&vmx->nested.preemption_timer,
9748 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9749}
9750
Wincy Van3af18d92015-02-03 23:49:31 +08009751static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9752 struct vmcs12 *vmcs12)
9753{
9754 int maxphyaddr;
9755 u64 addr;
9756
9757 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9758 return 0;
9759
9760 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9761 WARN_ON(1);
9762 return -EINVAL;
9763 }
9764 maxphyaddr = cpuid_maxphyaddr(vcpu);
9765
9766 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9767 ((addr + PAGE_SIZE) >> maxphyaddr))
9768 return -EINVAL;
9769
9770 return 0;
9771}
9772
9773/*
9774 * Merge L0's and L1's MSR bitmap, return false to indicate that
9775 * we do not use the hardware.
9776 */
9777static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9778 struct vmcs12 *vmcs12)
9779{
Wincy Van82f0dd42015-02-03 23:57:18 +08009780 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009781 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009782 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009783 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009784 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009785 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009786 *
9787 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9788 * ensures that we do not accidentally generate an L02 MSR bitmap
9789 * from the L12 MSR bitmap that is too permissive.
9790 * 2. That L1 or L2s have actually used the MSR. This avoids
9791 * unnecessarily merging of the bitmap if the MSR is unused. This
9792 * works properly because we only update the L01 MSR bitmap lazily.
9793 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9794 * updated to reflect this when L1 (or its L2s) actually write to
9795 * the MSR.
9796 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009797 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9798 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009799
Ashok Raj70131292018-02-01 22:59:43 +01009800 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009801 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009802 return false;
9803
9804 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009805 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009806 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009807 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009808
Radim Krčmářd048c092016-08-08 20:16:22 +02009809 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9810
Wincy Vanf2b93282015-02-03 23:56:03 +08009811 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009812 if (nested_cpu_has_apic_reg_virt(vmcs12))
9813 for (msr = 0x800; msr <= 0x8ff; msr++)
9814 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009815 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009816 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009817
9818 nested_vmx_disable_intercept_for_msr(
9819 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009820 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9821 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009822
Wincy Van608406e2015-02-03 23:57:51 +08009823 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009824 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009825 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009826 APIC_BASE_MSR + (APIC_EOI >> 4),
9827 MSR_TYPE_W);
9828 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009829 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009830 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9831 MSR_TYPE_W);
9832 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009833 }
Ashok Raj70131292018-02-01 22:59:43 +01009834
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009835 if (spec_ctrl)
9836 nested_vmx_disable_intercept_for_msr(
9837 msr_bitmap_l1, msr_bitmap_l0,
9838 MSR_IA32_SPEC_CTRL,
9839 MSR_TYPE_R | MSR_TYPE_W);
9840
Ashok Raj70131292018-02-01 22:59:43 +01009841 if (pred_cmd)
9842 nested_vmx_disable_intercept_for_msr(
9843 msr_bitmap_l1, msr_bitmap_l0,
9844 MSR_IA32_PRED_CMD,
9845 MSR_TYPE_W);
9846
Wincy Vanf2b93282015-02-03 23:56:03 +08009847 kunmap(page);
9848 nested_release_page_clean(page);
9849
9850 return true;
9851}
9852
9853static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9854 struct vmcs12 *vmcs12)
9855{
Wincy Van82f0dd42015-02-03 23:57:18 +08009856 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009857 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009858 !nested_cpu_has_vid(vmcs12) &&
9859 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009860 return 0;
9861
9862 /*
9863 * If virtualize x2apic mode is enabled,
9864 * virtualize apic access must be disabled.
9865 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009866 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9867 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009868 return -EINVAL;
9869
Wincy Van608406e2015-02-03 23:57:51 +08009870 /*
9871 * If virtual interrupt delivery is enabled,
9872 * we must exit on external interrupts.
9873 */
9874 if (nested_cpu_has_vid(vmcs12) &&
9875 !nested_exit_on_intr(vcpu))
9876 return -EINVAL;
9877
Wincy Van705699a2015-02-03 23:58:17 +08009878 /*
9879 * bits 15:8 should be zero in posted_intr_nv,
9880 * the descriptor address has been already checked
9881 * in nested_get_vmcs12_pages.
9882 */
9883 if (nested_cpu_has_posted_intr(vmcs12) &&
9884 (!nested_cpu_has_vid(vmcs12) ||
9885 !nested_exit_intr_ack_set(vcpu) ||
9886 vmcs12->posted_intr_nv & 0xff00))
9887 return -EINVAL;
9888
Wincy Vanf2b93282015-02-03 23:56:03 +08009889 /* tpr shadow is needed by all apicv features. */
9890 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9891 return -EINVAL;
9892
9893 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009894}
9895
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009896static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9897 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009898 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009899{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009900 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009901 u64 count, addr;
9902
9903 if (vmcs12_read_any(vcpu, count_field, &count) ||
9904 vmcs12_read_any(vcpu, addr_field, &addr)) {
9905 WARN_ON(1);
9906 return -EINVAL;
9907 }
9908 if (count == 0)
9909 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009910 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009911 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9912 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009913 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009914 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9915 addr_field, maxphyaddr, count, addr);
9916 return -EINVAL;
9917 }
9918 return 0;
9919}
9920
9921static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9922 struct vmcs12 *vmcs12)
9923{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009924 if (vmcs12->vm_exit_msr_load_count == 0 &&
9925 vmcs12->vm_exit_msr_store_count == 0 &&
9926 vmcs12->vm_entry_msr_load_count == 0)
9927 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009928 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009929 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009930 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009931 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009932 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009933 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009934 return -EINVAL;
9935 return 0;
9936}
9937
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009938static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9939 struct vmx_msr_entry *e)
9940{
9941 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009942 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009943 return -EINVAL;
9944 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9945 e->index == MSR_IA32_UCODE_REV)
9946 return -EINVAL;
9947 if (e->reserved != 0)
9948 return -EINVAL;
9949 return 0;
9950}
9951
9952static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9953 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009954{
9955 if (e->index == MSR_FS_BASE ||
9956 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009957 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9958 nested_vmx_msr_check_common(vcpu, e))
9959 return -EINVAL;
9960 return 0;
9961}
9962
9963static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9964 struct vmx_msr_entry *e)
9965{
9966 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9967 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009968 return -EINVAL;
9969 return 0;
9970}
9971
9972/*
9973 * Load guest's/host's msr at nested entry/exit.
9974 * return 0 for success, entry index for failure.
9975 */
9976static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9977{
9978 u32 i;
9979 struct vmx_msr_entry e;
9980 struct msr_data msr;
9981
9982 msr.host_initiated = false;
9983 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009984 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9985 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009986 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009987 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9988 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009989 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009990 }
9991 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009992 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009993 "%s check failed (%u, 0x%x, 0x%x)\n",
9994 __func__, i, e.index, e.reserved);
9995 goto fail;
9996 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009997 msr.index = e.index;
9998 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009999 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010000 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010001 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
10002 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030010003 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010004 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010005 }
10006 return 0;
10007fail:
10008 return i + 1;
10009}
10010
10011static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10012{
10013 u32 i;
10014 struct vmx_msr_entry e;
10015
10016 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010017 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010018 if (kvm_vcpu_read_guest(vcpu,
10019 gpa + i * sizeof(e),
10020 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010021 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010022 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10023 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010024 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010025 }
10026 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010027 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010028 "%s check failed (%u, 0x%x, 0x%x)\n",
10029 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010030 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010031 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010032 msr_info.host_initiated = false;
10033 msr_info.index = e.index;
10034 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010035 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010036 "%s cannot read MSR (%u, 0x%x)\n",
10037 __func__, i, e.index);
10038 return -EINVAL;
10039 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010040 if (kvm_vcpu_write_guest(vcpu,
10041 gpa + i * sizeof(e) +
10042 offsetof(struct vmx_msr_entry, value),
10043 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010044 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010045 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010046 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010047 return -EINVAL;
10048 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010049 }
10050 return 0;
10051}
10052
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010053/*
10054 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10055 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010056 * 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 +030010057 * guest in a way that will both be appropriate to L1's requests, and our
10058 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10059 * function also has additional necessary side-effects, like setting various
10060 * vcpu->arch fields.
10061 */
10062static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10063{
10064 struct vcpu_vmx *vmx = to_vmx(vcpu);
10065 u32 exec_control;
10066
10067 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10068 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10069 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10070 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10071 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10072 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10073 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10074 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10075 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10076 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10077 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10078 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10079 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10080 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10081 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10082 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10083 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10084 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10085 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10086 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10087 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10088 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10089 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10090 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10091 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10092 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10093 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10094 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10095 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10096 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10097 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10098 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10099 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10100 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10101 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10102 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10103
Jan Kiszka2996fca2014-06-16 13:59:43 +020010104 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
10105 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10106 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10107 } else {
10108 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10109 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10110 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010111 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10112 vmcs12->vm_entry_intr_info_field);
10113 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10114 vmcs12->vm_entry_exception_error_code);
10115 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10116 vmcs12->vm_entry_instruction_len);
10117 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10118 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010119 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010120 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010121 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10122 vmcs12->guest_pending_dbg_exceptions);
10123 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10124 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10125
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010126 if (nested_cpu_has_xsaves(vmcs12))
10127 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010128 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10129
Jan Kiszkaf4124502014-03-07 20:03:13 +010010130 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010131
Paolo Bonzini93140062016-07-06 13:23:51 +020010132 /* Preemption timer setting is only taken from vmcs01. */
10133 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10134 exec_control |= vmcs_config.pin_based_exec_ctrl;
10135 if (vmx->hv_deadline_tsc == -1)
10136 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10137
10138 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010139 if (nested_cpu_has_posted_intr(vmcs12)) {
10140 /*
10141 * Note that we use L0's vector here and in
10142 * vmx_deliver_nested_posted_interrupt.
10143 */
10144 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10145 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010146 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010147 vmcs_write64(POSTED_INTR_DESC_ADDR,
10148 page_to_phys(vmx->nested.pi_desc_page) +
10149 (unsigned long)(vmcs12->posted_intr_desc_addr &
10150 (PAGE_SIZE - 1)));
10151 } else
10152 exec_control &= ~PIN_BASED_POSTED_INTR;
10153
Jan Kiszkaf4124502014-03-07 20:03:13 +010010154 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010155
Jan Kiszkaf4124502014-03-07 20:03:13 +010010156 vmx->nested.preemption_timer_expired = false;
10157 if (nested_cpu_has_preemption_timer(vmcs12))
10158 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010159
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010160 /*
10161 * Whether page-faults are trapped is determined by a combination of
10162 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10163 * If enable_ept, L0 doesn't care about page faults and we should
10164 * set all of these to L1's desires. However, if !enable_ept, L0 does
10165 * care about (at least some) page faults, and because it is not easy
10166 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10167 * to exit on each and every L2 page fault. This is done by setting
10168 * MASK=MATCH=0 and (see below) EB.PF=1.
10169 * Note that below we don't need special code to set EB.PF beyond the
10170 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10171 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10172 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10173 *
10174 * A problem with this approach (when !enable_ept) is that L1 may be
10175 * injected with more page faults than it asked for. This could have
10176 * caused problems, but in practice existing hypervisors don't care.
10177 * To fix this, we will need to emulate the PFEC checking (on the L1
10178 * page tables), using walk_addr(), when injecting PFs to L1.
10179 */
10180 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10181 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10182 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10183 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10184
10185 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010186 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010187
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010188 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010189 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010190 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010191 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010192 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010193 if (nested_cpu_has(vmcs12,
10194 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10195 exec_control |= vmcs12->secondary_vm_exec_control;
10196
10197 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10198 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010199 * If translation failed, no matter: This feature asks
10200 * to exit when accessing the given address, and if it
10201 * can never be accessed, this feature won't do
10202 * anything anyway.
10203 */
10204 if (!vmx->nested.apic_access_page)
10205 exec_control &=
10206 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10207 else
10208 vmcs_write64(APIC_ACCESS_ADDR,
10209 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010210 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010211 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010212 exec_control |=
10213 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010214 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010215 }
10216
Wincy Van608406e2015-02-03 23:57:51 +080010217 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10218 vmcs_write64(EOI_EXIT_BITMAP0,
10219 vmcs12->eoi_exit_bitmap0);
10220 vmcs_write64(EOI_EXIT_BITMAP1,
10221 vmcs12->eoi_exit_bitmap1);
10222 vmcs_write64(EOI_EXIT_BITMAP2,
10223 vmcs12->eoi_exit_bitmap2);
10224 vmcs_write64(EOI_EXIT_BITMAP3,
10225 vmcs12->eoi_exit_bitmap3);
10226 vmcs_write16(GUEST_INTR_STATUS,
10227 vmcs12->guest_intr_status);
10228 }
10229
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010230 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10231 }
10232
10233
10234 /*
10235 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10236 * Some constant fields are set here by vmx_set_constant_host_state().
10237 * Other fields are different per CPU, and will be set later when
10238 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10239 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010240 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010241
10242 /*
Jim Mattson69c25252016-10-04 10:48:38 -070010243 * Set the MSR load/store lists to match L0's settings.
10244 */
10245 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040010246 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10247 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10248 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10249 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Jim Mattson69c25252016-10-04 10:48:38 -070010250
10251 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010252 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10253 * entry, but only if the current (host) sp changed from the value
10254 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10255 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10256 * here we just force the write to happen on entry.
10257 */
10258 vmx->host_rsp = 0;
10259
10260 exec_control = vmx_exec_control(vmx); /* L0's desires */
10261 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10262 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10263 exec_control &= ~CPU_BASED_TPR_SHADOW;
10264 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010265
10266 if (exec_control & CPU_BASED_TPR_SHADOW) {
10267 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10268 page_to_phys(vmx->nested.virtual_apic_page));
10269 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010270 } else {
10271#ifdef CONFIG_X86_64
10272 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10273 CPU_BASED_CR8_STORE_EXITING;
10274#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010275 }
10276
Wincy Van3af18d92015-02-03 23:49:31 +080010277 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010278 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10279 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10280 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10281 else
Wincy Van3af18d92015-02-03 23:49:31 +080010282 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10283
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010284 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010285 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010286 * Rather, exit every time.
10287 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010288 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10289 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10290
10291 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10292
10293 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10294 * bitwise-or of what L1 wants to trap for L2, and what we want to
10295 * trap. Note that CR0.TS also needs updating - we do this later.
10296 */
10297 update_exception_bitmap(vcpu);
10298 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10299 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10300
Nadav Har'El8049d652013-08-05 11:07:06 +030010301 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10302 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10303 * bits are further modified by vmx_set_efer() below.
10304 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010305 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010306
10307 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10308 * emulated by vmx_set_efer(), below.
10309 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010310 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010311 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10312 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010313 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10314
Jan Kiszka44811c02013-08-04 17:17:27 +020010315 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010316 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010317 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10318 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010319 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10320
10321
10322 set_cr4_guest_host_mask(vmx);
10323
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010324 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10325 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10326
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010327 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10328 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010329 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010330 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010331 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010332 if (kvm_has_tsc_control)
10333 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010334
Paolo Bonzini6236b782018-01-16 16:51:18 +010010335 if (cpu_has_vmx_msr_bitmap())
10336 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10337
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010338 if (enable_vpid) {
10339 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010340 * There is no direct mapping between vpid02 and vpid12, the
10341 * vpid02 is per-vCPU for L0 and reused while the value of
10342 * vpid12 is changed w/ one invvpid during nested vmentry.
10343 * The vpid12 is allocated by L1 for L2, so it will not
10344 * influence global bitmap(for vpid01 and vpid02 allocation)
10345 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010346 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010347 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10348 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10349 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10350 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10351 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10352 }
10353 } else {
10354 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10355 vmx_flush_tlb(vcpu);
10356 }
10357
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010358 }
10359
Ladi Prosek560a9792017-04-04 14:18:53 +020010360 if (enable_pml) {
10361 /*
10362 * Conceptually we want to copy the PML address and index from
10363 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10364 * since we always flush the log on each vmexit, this happens
10365 * to be equivalent to simply resetting the fields in vmcs02.
10366 */
10367 ASSERT(vmx->pml_pg);
10368 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10369 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10370 }
10371
Nadav Har'El155a97a2013-08-05 11:07:16 +030010372 if (nested_cpu_has_ept(vmcs12)) {
10373 kvm_mmu_unload(vcpu);
10374 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010375 } else if (nested_cpu_has2(vmcs12,
10376 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10377 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010378 }
10379
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010380 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10381 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010382 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010383 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10384 else
10385 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10386 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10387 vmx_set_efer(vcpu, vcpu->arch.efer);
10388
10389 /*
10390 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10391 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10392 * The CR0_READ_SHADOW is what L2 should have expected to read given
10393 * the specifications by L1; It's not enough to take
10394 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10395 * have more bits than L1 expected.
10396 */
10397 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10398 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10399
10400 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10401 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10402
10403 /* shadow page tables on either EPT or shadow page tables */
10404 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10405 kvm_mmu_reset_context(vcpu);
10406
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010407 if (!enable_ept)
10408 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10409
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010410 /*
10411 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10412 */
10413 if (enable_ept) {
10414 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10415 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10416 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10417 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10418 }
10419
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010420 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10421 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10422}
10423
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010424/*
10425 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10426 * for running an L2 nested guest.
10427 */
10428static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10429{
10430 struct vmcs12 *vmcs12;
10431 struct vcpu_vmx *vmx = to_vmx(vcpu);
10432 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010433 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010434 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010435
10436 if (!nested_vmx_check_permission(vcpu) ||
10437 !nested_vmx_check_vmcs12(vcpu))
10438 return 1;
10439
10440 skip_emulated_instruction(vcpu);
10441 vmcs12 = get_vmcs12(vcpu);
10442
Abel Gordon012f83c2013-04-18 14:39:25 +030010443 if (enable_shadow_vmcs)
10444 copy_shadow_to_vmcs12(vmx);
10445
Nadav Har'El7c177932011-05-25 23:12:04 +030010446 /*
10447 * The nested entry process starts with enforcing various prerequisites
10448 * on vmcs12 as required by the Intel SDM, and act appropriately when
10449 * they fail: As the SDM explains, some conditions should cause the
10450 * instruction to fail, while others will cause the instruction to seem
10451 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10452 * To speed up the normal (success) code path, we should avoid checking
10453 * for misconfigurations which will anyway be caught by the processor
10454 * when using the merged vmcs02.
10455 */
10456 if (vmcs12->launch_state == launch) {
10457 nested_vmx_failValid(vcpu,
10458 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10459 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10460 return 1;
10461 }
10462
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010463 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10464 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010465 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10466 return 1;
10467 }
10468
Wincy Van3af18d92015-02-03 23:49:31 +080010469 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010470 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10471 return 1;
10472 }
10473
Wincy Van3af18d92015-02-03 23:49:31 +080010474 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010475 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10476 return 1;
10477 }
10478
Wincy Vanf2b93282015-02-03 23:56:03 +080010479 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10480 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10481 return 1;
10482 }
10483
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010484 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10485 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10486 return 1;
10487 }
10488
Nadav Har'El7c177932011-05-25 23:12:04 +030010489 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010490 vmx->nested.nested_vmx_true_procbased_ctls_low,
10491 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010492 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010493 vmx->nested.nested_vmx_secondary_ctls_low,
10494 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010495 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010496 vmx->nested.nested_vmx_pinbased_ctls_low,
10497 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010498 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010499 vmx->nested.nested_vmx_true_exit_ctls_low,
10500 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010501 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010502 vmx->nested.nested_vmx_true_entry_ctls_low,
10503 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010504 {
10505 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10506 return 1;
10507 }
10508
10509 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10510 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10511 nested_vmx_failValid(vcpu,
10512 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10513 return 1;
10514 }
10515
Wincy Vanb9c237b2015-02-03 23:56:30 +080010516 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010517 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10518 nested_vmx_entry_failure(vcpu, vmcs12,
10519 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10520 return 1;
10521 }
10522 if (vmcs12->vmcs_link_pointer != -1ull) {
10523 nested_vmx_entry_failure(vcpu, vmcs12,
10524 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10525 return 1;
10526 }
10527
10528 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010529 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010530 * are performed on the field for the IA32_EFER MSR:
10531 * - Bits reserved in the IA32_EFER MSR must be 0.
10532 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10533 * the IA-32e mode guest VM-exit control. It must also be identical
10534 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10535 * CR0.PG) is 1.
10536 */
10537 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10538 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10539 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10540 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10541 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10542 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10543 nested_vmx_entry_failure(vcpu, vmcs12,
10544 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10545 return 1;
10546 }
10547 }
10548
10549 /*
10550 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10551 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10552 * the values of the LMA and LME bits in the field must each be that of
10553 * the host address-space size VM-exit control.
10554 */
10555 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10556 ia32e = (vmcs12->vm_exit_controls &
10557 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10558 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10559 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10560 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10561 nested_vmx_entry_failure(vcpu, vmcs12,
10562 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10563 return 1;
10564 }
10565 }
10566
10567 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010568 * We're finally done with prerequisite checking, and can start with
10569 * the nested entry.
10570 */
10571
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010572 enter_guest_mode(vcpu);
10573
Jan Kiszka2996fca2014-06-16 13:59:43 +020010574 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10575 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10576
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010577 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010578 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010579 vmx_vcpu_put(vcpu);
10580 vmx_vcpu_load(vcpu, cpu);
10581 vcpu->cpu = cpu;
10582 put_cpu();
10583
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010584 vmx_segment_cache_clear(vmx);
10585
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010586 prepare_vmcs02(vcpu, vmcs12);
10587
Wincy Vanff651cb2014-12-11 08:52:58 +030010588 msr_entry_idx = nested_vmx_load_msr(vcpu,
10589 vmcs12->vm_entry_msr_load_addr,
10590 vmcs12->vm_entry_msr_load_count);
10591 if (msr_entry_idx) {
10592 leave_guest_mode(vcpu);
10593 vmx_load_vmcs01(vcpu);
10594 nested_vmx_entry_failure(vcpu, vmcs12,
10595 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10596 return 1;
10597 }
10598
10599 vmcs12->launch_state = 1;
10600
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +020010601 /* Hide L1D cache contents from the nested guest. */
10602 vmx->vcpu.arch.l1tf_flush_l1d = true;
10603
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010604 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010605 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010606
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010607 vmx->nested.nested_run_pending = 1;
10608
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010609 /*
10610 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10611 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10612 * returned as far as L1 is concerned. It will only return (and set
10613 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10614 */
10615 return 1;
10616}
10617
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010618/*
10619 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10620 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10621 * This function returns the new value we should put in vmcs12.guest_cr0.
10622 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10623 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10624 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10625 * didn't trap the bit, because if L1 did, so would L0).
10626 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10627 * been modified by L2, and L1 knows it. So just leave the old value of
10628 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10629 * isn't relevant, because if L0 traps this bit it can set it to anything.
10630 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10631 * changed these bits, and therefore they need to be updated, but L0
10632 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10633 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10634 */
10635static inline unsigned long
10636vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10637{
10638 return
10639 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10640 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10641 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10642 vcpu->arch.cr0_guest_owned_bits));
10643}
10644
10645static inline unsigned long
10646vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10647{
10648 return
10649 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10650 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10651 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10652 vcpu->arch.cr4_guest_owned_bits));
10653}
10654
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010655static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10656 struct vmcs12 *vmcs12)
10657{
10658 u32 idt_vectoring;
10659 unsigned int nr;
10660
Gleb Natapov851eb6672013-09-25 12:51:34 +030010661 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010662 nr = vcpu->arch.exception.nr;
10663 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10664
10665 if (kvm_exception_is_soft(nr)) {
10666 vmcs12->vm_exit_instruction_len =
10667 vcpu->arch.event_exit_inst_len;
10668 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10669 } else
10670 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10671
10672 if (vcpu->arch.exception.has_error_code) {
10673 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10674 vmcs12->idt_vectoring_error_code =
10675 vcpu->arch.exception.error_code;
10676 }
10677
10678 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010679 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010680 vmcs12->idt_vectoring_info_field =
10681 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10682 } else if (vcpu->arch.interrupt.pending) {
10683 nr = vcpu->arch.interrupt.nr;
10684 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10685
10686 if (vcpu->arch.interrupt.soft) {
10687 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10688 vmcs12->vm_entry_instruction_len =
10689 vcpu->arch.event_exit_inst_len;
10690 } else
10691 idt_vectoring |= INTR_TYPE_EXT_INTR;
10692
10693 vmcs12->idt_vectoring_info_field = idt_vectoring;
10694 }
10695}
10696
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010697static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10698{
10699 struct vcpu_vmx *vmx = to_vmx(vcpu);
10700
Jan Kiszkaf4124502014-03-07 20:03:13 +010010701 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10702 vmx->nested.preemption_timer_expired) {
10703 if (vmx->nested.nested_run_pending)
10704 return -EBUSY;
10705 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10706 return 0;
10707 }
10708
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010709 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010710 if (vmx->nested.nested_run_pending ||
10711 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010712 return -EBUSY;
10713 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10714 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10715 INTR_INFO_VALID_MASK, 0);
10716 /*
10717 * The NMI-triggered VM exit counts as injection:
10718 * clear this one and block further NMIs.
10719 */
10720 vcpu->arch.nmi_pending = 0;
10721 vmx_set_nmi_mask(vcpu, true);
10722 return 0;
10723 }
10724
10725 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10726 nested_exit_on_intr(vcpu)) {
10727 if (vmx->nested.nested_run_pending)
10728 return -EBUSY;
10729 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010730 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010731 }
10732
David Hildenbrand1edccf22017-01-25 11:58:58 +010010733 vmx_complete_nested_posted_interrupt(vcpu);
10734 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010735}
10736
Jan Kiszkaf4124502014-03-07 20:03:13 +010010737static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10738{
10739 ktime_t remaining =
10740 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10741 u64 value;
10742
10743 if (ktime_to_ns(remaining) <= 0)
10744 return 0;
10745
10746 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10747 do_div(value, 1000000);
10748 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10749}
10750
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010751/*
10752 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10753 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10754 * and this function updates it to reflect the changes to the guest state while
10755 * L2 was running (and perhaps made some exits which were handled directly by L0
10756 * without going back to L1), and to reflect the exit reason.
10757 * Note that we do not have to copy here all VMCS fields, just those that
10758 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10759 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10760 * which already writes to vmcs12 directly.
10761 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010762static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10763 u32 exit_reason, u32 exit_intr_info,
10764 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010765{
10766 /* update guest state fields: */
10767 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10768 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10769
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010770 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10771 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10772 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10773
10774 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10775 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10776 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10777 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10778 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10779 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10780 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10781 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10782 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10783 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10784 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10785 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10786 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10787 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10788 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10789 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10790 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10791 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10792 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10793 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10794 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10795 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10796 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10797 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10798 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10799 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10800 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10801 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10802 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10803 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10804 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10805 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10806 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10807 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10808 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10809 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10810
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010811 vmcs12->guest_interruptibility_info =
10812 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10813 vmcs12->guest_pending_dbg_exceptions =
10814 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010815 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10816 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10817 else
10818 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010819
Jan Kiszkaf4124502014-03-07 20:03:13 +010010820 if (nested_cpu_has_preemption_timer(vmcs12)) {
10821 if (vmcs12->vm_exit_controls &
10822 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10823 vmcs12->vmx_preemption_timer_value =
10824 vmx_get_preemption_timer_value(vcpu);
10825 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10826 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010827
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010828 /*
10829 * In some cases (usually, nested EPT), L2 is allowed to change its
10830 * own CR3 without exiting. If it has changed it, we must keep it.
10831 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10832 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10833 *
10834 * Additionally, restore L2's PDPTR to vmcs12.
10835 */
10836 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010837 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010838 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10839 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10840 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10841 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10842 }
10843
Jim Mattson4933e9f2017-06-01 12:44:46 -070010844 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010845
Wincy Van608406e2015-02-03 23:57:51 +080010846 if (nested_cpu_has_vid(vmcs12))
10847 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10848
Jan Kiszkac18911a2013-03-13 16:06:41 +010010849 vmcs12->vm_entry_controls =
10850 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010851 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010852
Jan Kiszka2996fca2014-06-16 13:59:43 +020010853 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10854 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10855 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10856 }
10857
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010858 /* TODO: These cannot have changed unless we have MSR bitmaps and
10859 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010860 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010861 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010862 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10863 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010864 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10865 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10866 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010867 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010868 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010869 if (nested_cpu_has_xsaves(vmcs12))
10870 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010871
10872 /* update exit information fields: */
10873
Jan Kiszka533558b2014-01-04 18:47:20 +010010874 vmcs12->vm_exit_reason = exit_reason;
10875 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010876
Jan Kiszka533558b2014-01-04 18:47:20 +010010877 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010878 if ((vmcs12->vm_exit_intr_info &
10879 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10880 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10881 vmcs12->vm_exit_intr_error_code =
10882 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010883 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010884 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10885 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10886
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010887 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10888 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10889 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010890 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010891
10892 /*
10893 * Transfer the event that L0 or L1 may wanted to inject into
10894 * L2 to IDT_VECTORING_INFO_FIELD.
10895 */
10896 vmcs12_save_pending_event(vcpu, vmcs12);
10897 }
10898
10899 /*
10900 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10901 * preserved above and would only end up incorrectly in L1.
10902 */
10903 vcpu->arch.nmi_injected = false;
10904 kvm_clear_exception_queue(vcpu);
10905 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010906}
10907
10908/*
10909 * A part of what we need to when the nested L2 guest exits and we want to
10910 * run its L1 parent, is to reset L1's guest state to the host state specified
10911 * in vmcs12.
10912 * This function is to be called not only on normal nested exit, but also on
10913 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10914 * Failures During or After Loading Guest State").
10915 * This function should be called when the active VMCS is L1's (vmcs01).
10916 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010917static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10918 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010919{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010920 struct kvm_segment seg;
10921
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010922 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10923 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010924 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010925 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10926 else
10927 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10928 vmx_set_efer(vcpu, vcpu->arch.efer);
10929
10930 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10931 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010932 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010933 /*
10934 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10935 * actually changed, because it depends on the current state of
10936 * fpu_active (which may have changed).
10937 * Note that vmx_set_cr0 refers to efer set above.
10938 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010939 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010940 /*
10941 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10942 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10943 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10944 */
10945 update_exception_bitmap(vcpu);
10946 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10947 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10948
10949 /*
10950 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10951 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10952 */
10953 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080010954 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010955
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010956 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010957
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010958 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10959 kvm_mmu_reset_context(vcpu);
10960
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010961 if (!enable_ept)
10962 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10963
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010964 if (enable_vpid) {
10965 /*
10966 * Trivially support vpid by letting L2s share their parent
10967 * L1's vpid. TODO: move to a more elaborate solution, giving
10968 * each L2 its own vpid and exposing the vpid feature to L1.
10969 */
10970 vmx_flush_tlb(vcpu);
10971 }
10972
10973
10974 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10975 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10976 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10977 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10978 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020010979 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
10980 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010981
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010982 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10983 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10984 vmcs_write64(GUEST_BNDCFGS, 0);
10985
Jan Kiszka44811c02013-08-04 17:17:27 +020010986 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010987 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010988 vcpu->arch.pat = vmcs12->host_ia32_pat;
10989 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010990 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10991 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10992 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010993
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010994 /* Set L1 segment info according to Intel SDM
10995 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10996 seg = (struct kvm_segment) {
10997 .base = 0,
10998 .limit = 0xFFFFFFFF,
10999 .selector = vmcs12->host_cs_selector,
11000 .type = 11,
11001 .present = 1,
11002 .s = 1,
11003 .g = 1
11004 };
11005 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
11006 seg.l = 1;
11007 else
11008 seg.db = 1;
11009 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11010 seg = (struct kvm_segment) {
11011 .base = 0,
11012 .limit = 0xFFFFFFFF,
11013 .type = 3,
11014 .present = 1,
11015 .s = 1,
11016 .db = 1,
11017 .g = 1
11018 };
11019 seg.selector = vmcs12->host_ds_selector;
11020 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11021 seg.selector = vmcs12->host_es_selector;
11022 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11023 seg.selector = vmcs12->host_ss_selector;
11024 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11025 seg.selector = vmcs12->host_fs_selector;
11026 seg.base = vmcs12->host_fs_base;
11027 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11028 seg.selector = vmcs12->host_gs_selector;
11029 seg.base = vmcs12->host_gs_base;
11030 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11031 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011032 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011033 .limit = 0x67,
11034 .selector = vmcs12->host_tr_selector,
11035 .type = 11,
11036 .present = 1
11037 };
11038 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11039
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011040 kvm_set_dr(vcpu, 7, 0x400);
11041 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011042
Wincy Van3af18d92015-02-03 23:49:31 +080011043 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010011044 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011045
Wincy Vanff651cb2014-12-11 08:52:58 +030011046 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11047 vmcs12->vm_exit_msr_load_count))
11048 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011049}
11050
11051/*
11052 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11053 * and modify vmcs12 to make it see what it would expect to see there if
11054 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11055 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011056static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11057 u32 exit_intr_info,
11058 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011059{
11060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011061 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11062
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011063 /* trying to cancel vmlaunch/vmresume is a bug */
11064 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11065
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011066 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011067 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11068 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011069
Wincy Vanff651cb2014-12-11 08:52:58 +030011070 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11071 vmcs12->vm_exit_msr_store_count))
11072 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11073
Wanpeng Lif3380ca2014-08-05 12:42:23 +080011074 vmx_load_vmcs01(vcpu);
11075
Bandan Das77b0f5d2014-04-19 18:17:45 -040011076 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
11077 && nested_exit_intr_ack_set(vcpu)) {
11078 int irq = kvm_cpu_get_interrupt(vcpu);
11079 WARN_ON(irq < 0);
11080 vmcs12->vm_exit_intr_info = irq |
11081 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11082 }
11083
Jan Kiszka542060e2014-01-04 18:47:21 +010011084 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11085 vmcs12->exit_qualification,
11086 vmcs12->idt_vectoring_info_field,
11087 vmcs12->vm_exit_intr_info,
11088 vmcs12->vm_exit_intr_error_code,
11089 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011090
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011091 vm_entry_controls_reset_shadow(vmx);
11092 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011093 vmx_segment_cache_clear(vmx);
11094
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011095 load_vmcs12_host_state(vcpu, vmcs12);
11096
Paolo Bonzini93140062016-07-06 13:23:51 +020011097 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040011098 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11099 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011100 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011101 if (vmx->hv_deadline_tsc == -1)
11102 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11103 PIN_BASED_VMX_PREEMPTION_TIMER);
11104 else
11105 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11106 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011107 if (kvm_has_tsc_control)
11108 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011109
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011110 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11111 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11112 vmx_set_virtual_x2apic_mode(vcpu,
11113 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070011114 } else if (!nested_cpu_has_ept(vmcs12) &&
11115 nested_cpu_has2(vmcs12,
11116 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11117 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011118 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011119
11120 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11121 vmx->host_rsp = 0;
11122
11123 /* Unpin physical memory we referred to in vmcs02 */
11124 if (vmx->nested.apic_access_page) {
11125 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011126 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011127 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011128 if (vmx->nested.virtual_apic_page) {
11129 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011130 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011131 }
Wincy Van705699a2015-02-03 23:58:17 +080011132 if (vmx->nested.pi_desc_page) {
11133 kunmap(vmx->nested.pi_desc_page);
11134 nested_release_page(vmx->nested.pi_desc_page);
11135 vmx->nested.pi_desc_page = NULL;
11136 vmx->nested.pi_desc = NULL;
11137 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011138
11139 /*
Tang Chen38b99172014-09-24 15:57:54 +080011140 * We are now running in L2, mmu_notifier will force to reload the
11141 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11142 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011143 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011144
11145 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011146 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11147 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11148 * success or failure flag accordingly.
11149 */
11150 if (unlikely(vmx->fail)) {
11151 vmx->fail = 0;
11152 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
11153 } else
11154 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011155 if (enable_shadow_vmcs)
11156 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011157
11158 /* in case we halted in L2 */
11159 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011160}
11161
Nadav Har'El7c177932011-05-25 23:12:04 +030011162/*
Jan Kiszka42124922014-01-04 18:47:19 +010011163 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11164 */
11165static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11166{
Wanpeng Lic886f282017-03-06 04:03:28 -080011167 if (is_guest_mode(vcpu)) {
11168 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011169 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011170 }
Jan Kiszka42124922014-01-04 18:47:19 +010011171 free_nested(to_vmx(vcpu));
11172}
11173
11174/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011175 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11176 * 23.7 "VM-entry failures during or after loading guest state" (this also
11177 * lists the acceptable exit-reason and exit-qualification parameters).
11178 * It should only be called before L2 actually succeeded to run, and when
11179 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11180 */
11181static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11182 struct vmcs12 *vmcs12,
11183 u32 reason, unsigned long qualification)
11184{
11185 load_vmcs12_host_state(vcpu, vmcs12);
11186 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11187 vmcs12->exit_qualification = qualification;
11188 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011189 if (enable_shadow_vmcs)
11190 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011191}
11192
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011193static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11194 struct x86_instruction_info *info,
11195 enum x86_intercept_stage stage)
11196{
11197 return X86EMUL_CONTINUE;
11198}
11199
Yunhong Jiang64672c92016-06-13 14:19:59 -070011200#ifdef CONFIG_X86_64
11201/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11202static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11203 u64 divisor, u64 *result)
11204{
11205 u64 low = a << shift, high = a >> (64 - shift);
11206
11207 /* To avoid the overflow on divq */
11208 if (high >= divisor)
11209 return 1;
11210
11211 /* Low hold the result, high hold rem which is discarded */
11212 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11213 "rm" (divisor), "0" (low), "1" (high));
11214 *result = low;
11215
11216 return 0;
11217}
11218
11219static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11220{
11221 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011222 u64 tscl = rdtsc();
11223 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11224 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011225
11226 /* Convert to host delta tsc if tsc scaling is enabled */
11227 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11228 u64_shl_div_u64(delta_tsc,
11229 kvm_tsc_scaling_ratio_frac_bits,
11230 vcpu->arch.tsc_scaling_ratio,
11231 &delta_tsc))
11232 return -ERANGE;
11233
11234 /*
11235 * If the delta tsc can't fit in the 32 bit after the multi shift,
11236 * we can't use the preemption timer.
11237 * It's possible that it fits on later vmentries, but checking
11238 * on every vmentry is costly so we just use an hrtimer.
11239 */
11240 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11241 return -ERANGE;
11242
11243 vmx->hv_deadline_tsc = tscl + delta_tsc;
11244 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11245 PIN_BASED_VMX_PREEMPTION_TIMER);
11246 return 0;
11247}
11248
11249static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11250{
11251 struct vcpu_vmx *vmx = to_vmx(vcpu);
11252 vmx->hv_deadline_tsc = -1;
11253 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11254 PIN_BASED_VMX_PREEMPTION_TIMER);
11255}
11256#endif
11257
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011258static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011259{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011260 if (ple_gap)
11261 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011262}
11263
Kai Huang843e4332015-01-28 10:54:28 +080011264static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11265 struct kvm_memory_slot *slot)
11266{
11267 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11268 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11269}
11270
11271static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11272 struct kvm_memory_slot *slot)
11273{
11274 kvm_mmu_slot_set_dirty(kvm, slot);
11275}
11276
11277static void vmx_flush_log_dirty(struct kvm *kvm)
11278{
11279 kvm_flush_pml_buffers(kvm);
11280}
11281
11282static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11283 struct kvm_memory_slot *memslot,
11284 gfn_t offset, unsigned long mask)
11285{
11286 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11287}
11288
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011289static void __pi_post_block(struct kvm_vcpu *vcpu)
11290{
11291 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11292 struct pi_desc old, new;
11293 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011294
11295 do {
11296 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011297 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11298 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011299
11300 dest = cpu_physical_id(vcpu->cpu);
11301
11302 if (x2apic_enabled())
11303 new.ndst = dest;
11304 else
11305 new.ndst = (dest << 8) & 0xFF00;
11306
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011307 /* set 'NV' to 'notification vector' */
11308 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011309 } while (cmpxchg64(&pi_desc->control, old.control,
11310 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011311
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011312 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11313 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011314 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011315 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011316 vcpu->pre_pcpu = -1;
11317 }
11318}
11319
Feng Wuefc64402015-09-18 22:29:51 +080011320/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011321 * This routine does the following things for vCPU which is going
11322 * to be blocked if VT-d PI is enabled.
11323 * - Store the vCPU to the wakeup list, so when interrupts happen
11324 * we can find the right vCPU to wake up.
11325 * - Change the Posted-interrupt descriptor as below:
11326 * 'NDST' <-- vcpu->pre_pcpu
11327 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11328 * - If 'ON' is set during this process, which means at least one
11329 * interrupt is posted for this vCPU, we cannot block it, in
11330 * this case, return 1, otherwise, return 0.
11331 *
11332 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011333static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011334{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011335 unsigned int dest;
11336 struct pi_desc old, new;
11337 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11338
11339 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011340 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11341 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011342 return 0;
11343
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011344 WARN_ON(irqs_disabled());
11345 local_irq_disable();
11346 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11347 vcpu->pre_pcpu = vcpu->cpu;
11348 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11349 list_add_tail(&vcpu->blocked_vcpu_list,
11350 &per_cpu(blocked_vcpu_on_cpu,
11351 vcpu->pre_pcpu));
11352 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11353 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011354
11355 do {
11356 old.control = new.control = pi_desc->control;
11357
Feng Wubf9f6ac2015-09-18 22:29:55 +080011358 WARN((pi_desc->sn == 1),
11359 "Warning: SN field of posted-interrupts "
11360 "is set before blocking\n");
11361
11362 /*
11363 * Since vCPU can be preempted during this process,
11364 * vcpu->cpu could be different with pre_pcpu, we
11365 * need to set pre_pcpu as the destination of wakeup
11366 * notification event, then we can find the right vCPU
11367 * to wakeup in wakeup handler if interrupts happen
11368 * when the vCPU is in blocked state.
11369 */
11370 dest = cpu_physical_id(vcpu->pre_pcpu);
11371
11372 if (x2apic_enabled())
11373 new.ndst = dest;
11374 else
11375 new.ndst = (dest << 8) & 0xFF00;
11376
11377 /* set 'NV' to 'wakeup vector' */
11378 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011379 } while (cmpxchg64(&pi_desc->control, old.control,
11380 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011381
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011382 /* We should not block the vCPU if an interrupt is posted for it. */
11383 if (pi_test_on(pi_desc) == 1)
11384 __pi_post_block(vcpu);
11385
11386 local_irq_enable();
11387 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011388}
11389
Yunhong Jiangbc225122016-06-13 14:19:58 -070011390static int vmx_pre_block(struct kvm_vcpu *vcpu)
11391{
11392 if (pi_pre_block(vcpu))
11393 return 1;
11394
Yunhong Jiang64672c92016-06-13 14:19:59 -070011395 if (kvm_lapic_hv_timer_in_use(vcpu))
11396 kvm_lapic_switch_to_sw_timer(vcpu);
11397
Yunhong Jiangbc225122016-06-13 14:19:58 -070011398 return 0;
11399}
11400
11401static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011402{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011403 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011404 return;
11405
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011406 WARN_ON(irqs_disabled());
11407 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011408 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011409 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011410}
11411
Yunhong Jiangbc225122016-06-13 14:19:58 -070011412static void vmx_post_block(struct kvm_vcpu *vcpu)
11413{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011414 if (kvm_x86_ops->set_hv_timer)
11415 kvm_lapic_switch_to_hv_timer(vcpu);
11416
Yunhong Jiangbc225122016-06-13 14:19:58 -070011417 pi_post_block(vcpu);
11418}
11419
Feng Wubf9f6ac2015-09-18 22:29:55 +080011420/*
Feng Wuefc64402015-09-18 22:29:51 +080011421 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11422 *
11423 * @kvm: kvm
11424 * @host_irq: host irq of the interrupt
11425 * @guest_irq: gsi of the interrupt
11426 * @set: set or unset PI
11427 * returns 0 on success, < 0 on failure
11428 */
11429static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11430 uint32_t guest_irq, bool set)
11431{
11432 struct kvm_kernel_irq_routing_entry *e;
11433 struct kvm_irq_routing_table *irq_rt;
11434 struct kvm_lapic_irq irq;
11435 struct kvm_vcpu *vcpu;
11436 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011437 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011438
11439 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011440 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11441 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011442 return 0;
11443
11444 idx = srcu_read_lock(&kvm->irq_srcu);
11445 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011446 if (guest_irq >= irq_rt->nr_rt_entries ||
11447 hlist_empty(&irq_rt->map[guest_irq])) {
11448 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11449 guest_irq, irq_rt->nr_rt_entries);
11450 goto out;
11451 }
Feng Wuefc64402015-09-18 22:29:51 +080011452
11453 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11454 if (e->type != KVM_IRQ_ROUTING_MSI)
11455 continue;
11456 /*
11457 * VT-d PI cannot support posting multicast/broadcast
11458 * interrupts to a vCPU, we still use interrupt remapping
11459 * for these kind of interrupts.
11460 *
11461 * For lowest-priority interrupts, we only support
11462 * those with single CPU as the destination, e.g. user
11463 * configures the interrupts via /proc/irq or uses
11464 * irqbalance to make the interrupts single-CPU.
11465 *
11466 * We will support full lowest-priority interrupt later.
11467 */
11468
Radim Krčmář371313132016-07-12 22:09:27 +020011469 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011470 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11471 /*
11472 * Make sure the IRTE is in remapped mode if
11473 * we don't handle it in posted mode.
11474 */
11475 ret = irq_set_vcpu_affinity(host_irq, NULL);
11476 if (ret < 0) {
11477 printk(KERN_INFO
11478 "failed to back to remapped mode, irq: %u\n",
11479 host_irq);
11480 goto out;
11481 }
11482
Feng Wuefc64402015-09-18 22:29:51 +080011483 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011484 }
Feng Wuefc64402015-09-18 22:29:51 +080011485
11486 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11487 vcpu_info.vector = irq.vector;
11488
Feng Wub6ce9782016-01-25 16:53:35 +080011489 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011490 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11491
11492 if (set)
11493 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011494 else
Feng Wuefc64402015-09-18 22:29:51 +080011495 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011496
11497 if (ret < 0) {
11498 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11499 __func__);
11500 goto out;
11501 }
11502 }
11503
11504 ret = 0;
11505out:
11506 srcu_read_unlock(&kvm->irq_srcu, idx);
11507 return ret;
11508}
11509
Ashok Rajc45dcc72016-06-22 14:59:56 +080011510static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11511{
11512 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11513 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11514 FEATURE_CONTROL_LMCE;
11515 else
11516 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11517 ~FEATURE_CONTROL_LMCE;
11518}
11519
Kees Cook404f6aa2016-08-08 16:29:06 -070011520static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011521 .cpu_has_kvm_support = cpu_has_kvm_support,
11522 .disabled_by_bios = vmx_disabled_by_bios,
11523 .hardware_setup = hardware_setup,
11524 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011525 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011526 .hardware_enable = hardware_enable,
11527 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011528 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011529 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011530
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040011531 .vm_init = vmx_vm_init,
11532
Avi Kivity6aa8b732006-12-10 02:21:36 -080011533 .vcpu_create = vmx_create_vcpu,
11534 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011535 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011536
Avi Kivity04d2cc72007-09-10 18:10:54 +030011537 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011538 .vcpu_load = vmx_vcpu_load,
11539 .vcpu_put = vmx_vcpu_put,
11540
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011541 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011542 .get_msr = vmx_get_msr,
11543 .set_msr = vmx_set_msr,
11544 .get_segment_base = vmx_get_segment_base,
11545 .get_segment = vmx_get_segment,
11546 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011547 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011548 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011549 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011550 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011551 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011552 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011553 .set_cr3 = vmx_set_cr3,
11554 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011555 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011556 .get_idt = vmx_get_idt,
11557 .set_idt = vmx_set_idt,
11558 .get_gdt = vmx_get_gdt,
11559 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011560 .get_dr6 = vmx_get_dr6,
11561 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011562 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011563 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011564 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011565 .get_rflags = vmx_get_rflags,
11566 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011567
11568 .get_pkru = vmx_get_pkru,
11569
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011570 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011571 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011572
11573 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011574
Avi Kivity6aa8b732006-12-10 02:21:36 -080011575 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011576 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011577 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011578 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11579 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011580 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011581 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011582 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011583 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011584 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011585 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011586 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011587 .get_nmi_mask = vmx_get_nmi_mask,
11588 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011589 .enable_nmi_window = enable_nmi_window,
11590 .enable_irq_window = enable_irq_window,
11591 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011592 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011593 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011594 .get_enable_apicv = vmx_get_enable_apicv,
11595 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011596 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11597 .hwapic_irr_update = vmx_hwapic_irr_update,
11598 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011599 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11600 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011601
Izik Eiduscbc94022007-10-25 00:29:55 +020011602 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011603 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011604 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011605
Avi Kivity586f9602010-11-18 13:09:54 +020011606 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011607
Sheng Yang17cc3932010-01-05 19:02:27 +080011608 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011609
11610 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011611
11612 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011613 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011614
11615 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011616
11617 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011618
11619 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011620
11621 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011622
11623 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011624 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011625 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011626 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011627
11628 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011629
11630 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011631
11632 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11633 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11634 .flush_log_dirty = vmx_flush_log_dirty,
11635 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011636
Feng Wubf9f6ac2015-09-18 22:29:55 +080011637 .pre_block = vmx_pre_block,
11638 .post_block = vmx_post_block,
11639
Wei Huang25462f72015-06-19 15:45:05 +020011640 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011641
11642 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011643
11644#ifdef CONFIG_X86_64
11645 .set_hv_timer = vmx_set_hv_timer,
11646 .cancel_hv_timer = vmx_cancel_hv_timer,
11647#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011648
11649 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011650};
11651
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011652static int __init vmx_setup_l1d_flush(void)
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011653{
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011654 struct page *page;
11655
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011656 if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER ||
11657 !boot_cpu_has_bug(X86_BUG_L1TF))
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011658 return 0;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011659
Paolo Bonziniacca8a72018-07-02 13:03:48 +020011660 if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
11661 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
11662 if (!page)
11663 return -ENOMEM;
11664 vmx_l1d_flush_pages = page_address(page);
11665 }
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011666
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011667 static_branch_enable(&vmx_l1d_should_flush);
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011668 return 0;
11669}
11670
11671static void vmx_free_l1d_flush_pages(void)
11672{
11673 if (vmx_l1d_flush_pages) {
11674 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
11675 vmx_l1d_flush_pages = NULL;
11676 }
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011677}
11678
Avi Kivity6aa8b732006-12-10 02:21:36 -080011679static int __init vmx_init(void)
11680{
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011681 int r;
11682
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011683 r = vmx_setup_l1d_flush();
11684 if (r)
11685 return r;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011686
11687 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11688 __alignof__(struct vcpu_vmx), THIS_MODULE);
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011689 if (r) {
11690 vmx_free_l1d_flush_pages();
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011691 return r;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011692 }
Sheng Yang25c5f222008-03-28 13:18:56 +080011693
Dave Young2965faa2015-09-09 15:38:55 -070011694#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011695 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11696 crash_vmclear_local_loaded_vmcss);
11697#endif
11698
He, Qingfdef3ad2007-04-30 09:45:24 +030011699 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011700}
11701
11702static void __exit vmx_exit(void)
11703{
Dave Young2965faa2015-09-09 15:38:55 -070011704#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011705 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011706 synchronize_rcu();
11707#endif
11708
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011709 kvm_exit();
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011710
11711 vmx_free_l1d_flush_pages();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011712}
11713
11714module_init(vmx_init)
11715module_exit(vmx_exit)