blob: 1efd6697337f403a1bc5fc752123ac7814a2014d [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)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002014 return;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002015 --m->guest.nr;
2016 --m->host.nr;
2017 m->guest.val[i] = m->guest.val[m->guest.nr];
2018 m->host.val[i] = m->host.val[m->host.nr];
2019 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2020 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002021}
2022
Gleb Natapov2961e8762013-11-25 15:37:13 +02002023static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2024 unsigned long entry, unsigned long exit,
2025 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2026 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002027{
2028 vmcs_write64(guest_val_vmcs, guest_val);
2029 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002030 vm_entry_controls_setbit(vmx, entry);
2031 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002032}
2033
Avi Kivity61d2ef22010-04-28 16:40:38 +03002034static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
2035 u64 guest_val, u64 host_val)
2036{
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002037 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002038 struct msr_autoload *m = &vmx->msr_autoload;
2039
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002040 switch (msr) {
2041 case MSR_EFER:
2042 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002043 add_atomic_switch_msr_special(vmx,
2044 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002045 VM_EXIT_LOAD_IA32_EFER,
2046 GUEST_IA32_EFER,
2047 HOST_IA32_EFER,
2048 guest_val, host_val);
2049 return;
2050 }
2051 break;
2052 case MSR_CORE_PERF_GLOBAL_CTRL:
2053 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002054 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002055 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2056 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2057 GUEST_IA32_PERF_GLOBAL_CTRL,
2058 HOST_IA32_PERF_GLOBAL_CTRL,
2059 guest_val, host_val);
2060 return;
2061 }
2062 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002063 case MSR_IA32_PEBS_ENABLE:
2064 /* PEBS needs a quiescent period after being disabled (to write
2065 * a record). Disabling PEBS through VMX MSR swapping doesn't
2066 * provide that period, so a CPU could write host's record into
2067 * guest's memory.
2068 */
2069 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002070 }
2071
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002072 i = find_msr(&m->guest, msr);
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002073 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002074 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002075 "Can't add msr %x\n", msr);
2076 return;
Konrad Rzeszutek Wilk1555f9e2018-06-20 20:11:39 -04002077 } else if (i < 0) {
2078 i = m->guest.nr++;
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002079 ++m->host.nr;
2080 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
2081 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002082 }
2083
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04002084 m->guest.val[i].index = msr;
2085 m->guest.val[i].value = guest_val;
2086 m->host.val[i].index = msr;
2087 m->host.val[i].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002088}
2089
Avi Kivity33ed6322007-05-02 16:54:03 +03002090static void reload_tss(void)
2091{
Avi Kivity33ed6322007-05-02 16:54:03 +03002092 /*
2093 * VT restores TR but not its size. Useless.
2094 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002095 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002096 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002097
Avi Kivityd3591922010-07-26 18:32:39 +03002098 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002099 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2100 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002101}
2102
Avi Kivity92c0d902009-10-29 11:00:16 +02002103static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002104{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002105 u64 guest_efer = vmx->vcpu.arch.efer;
2106 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002107
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002108 if (!enable_ept) {
2109 /*
2110 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2111 * host CPUID is more efficient than testing guest CPUID
2112 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2113 */
2114 if (boot_cpu_has(X86_FEATURE_SMEP))
2115 guest_efer |= EFER_NX;
2116 else if (!(guest_efer & EFER_NX))
2117 ignore_bits |= EFER_NX;
2118 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002119
Avi Kivity51c6cf62007-08-29 03:48:05 +03002120 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002121 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002122 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002123 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002124#ifdef CONFIG_X86_64
2125 ignore_bits |= EFER_LMA | EFER_LME;
2126 /* SCE is meaningful only in long mode on Intel */
2127 if (guest_efer & EFER_LMA)
2128 ignore_bits &= ~(u64)EFER_SCE;
2129#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002130
2131 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002132
2133 /*
2134 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2135 * On CPUs that support "load IA32_EFER", always switch EFER
2136 * atomically, since it's faster than switching it manually.
2137 */
2138 if (cpu_has_load_ia32_efer ||
2139 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002140 if (!(guest_efer & EFER_LMA))
2141 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002142 if (guest_efer != host_efer)
2143 add_atomic_switch_msr(vmx, MSR_EFER,
2144 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002145 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002146 } else {
2147 guest_efer &= ~ignore_bits;
2148 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002149
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002150 vmx->guest_msrs[efer_offset].data = guest_efer;
2151 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2152
2153 return true;
2154 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002155}
2156
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002157static unsigned long segment_base(u16 selector)
2158{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002159 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002160 struct desc_struct *d;
2161 unsigned long table_base;
2162 unsigned long v;
2163
2164 if (!(selector & ~3))
2165 return 0;
2166
Avi Kivityd3591922010-07-26 18:32:39 +03002167 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002168
2169 if (selector & 4) { /* from ldt */
2170 u16 ldt_selector = kvm_read_ldt();
2171
2172 if (!(ldt_selector & ~3))
2173 return 0;
2174
2175 table_base = segment_base(ldt_selector);
2176 }
2177 d = (struct desc_struct *)(table_base + (selector & ~7));
2178 v = get_desc_base(d);
2179#ifdef CONFIG_X86_64
2180 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2181 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2182#endif
2183 return v;
2184}
2185
2186static inline unsigned long kvm_read_tr_base(void)
2187{
2188 u16 tr;
2189 asm("str %0" : "=g"(tr));
2190 return segment_base(tr);
2191}
2192
Avi Kivity04d2cc72007-09-10 18:10:54 +03002193static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002194{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002195 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002196 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002197
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002198 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002199 return;
2200
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002201 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002202 /*
2203 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2204 * allow segment selectors with cpl > 0 or ti == 1.
2205 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002206 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002207 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002208 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002209 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002210 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002211 vmx->host_state.fs_reload_needed = 0;
2212 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002213 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002214 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002215 }
Avi Kivity9581d442010-10-19 16:46:55 +02002216 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002217 if (!(vmx->host_state.gs_sel & 7))
2218 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002219 else {
2220 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002221 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002222 }
2223
2224#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002225 savesegment(ds, vmx->host_state.ds_sel);
2226 savesegment(es, vmx->host_state.es_sel);
2227#endif
2228
2229#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002230 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2231 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2232#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002233 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2234 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002235#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002236
2237#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002238 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2239 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002240 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002241#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002242 if (boot_cpu_has(X86_FEATURE_MPX))
2243 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002244 for (i = 0; i < vmx->save_nmsrs; ++i)
2245 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002246 vmx->guest_msrs[i].data,
2247 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002248}
2249
Avi Kivitya9b21b62008-06-24 11:48:49 +03002250static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002251{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002252 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002253 return;
2254
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002255 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002256 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002257#ifdef CONFIG_X86_64
2258 if (is_long_mode(&vmx->vcpu))
2259 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2260#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002261 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002262 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002263#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002264 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002265#else
2266 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002267#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002268 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002269 if (vmx->host_state.fs_reload_needed)
2270 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002271#ifdef CONFIG_X86_64
2272 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2273 loadsegment(ds, vmx->host_state.ds_sel);
2274 loadsegment(es, vmx->host_state.es_sel);
2275 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002276#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002277 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002278#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002279 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002280#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002281 if (vmx->host_state.msr_host_bndcfgs)
2282 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002283 /*
2284 * If the FPU is not active (through the host task or
2285 * the guest vcpu), then restore the cr0.TS bit.
2286 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002287 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002288 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002289 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002290}
2291
Avi Kivitya9b21b62008-06-24 11:48:49 +03002292static void vmx_load_host_state(struct vcpu_vmx *vmx)
2293{
2294 preempt_disable();
2295 __vmx_load_host_state(vmx);
2296 preempt_enable();
2297}
2298
Feng Wu28b835d2015-09-18 22:29:54 +08002299static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2300{
2301 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2302 struct pi_desc old, new;
2303 unsigned int dest;
2304
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002305 /*
2306 * In case of hot-plug or hot-unplug, we may have to undo
2307 * vmx_vcpu_pi_put even if there is no assigned device. And we
2308 * always keep PI.NDST up to date for simplicity: it makes the
2309 * code easier, and CPU migration is not a fast path.
2310 */
2311 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002312 return;
2313
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002314 /*
2315 * First handle the simple case where no cmpxchg is necessary; just
2316 * allow posting non-urgent interrupts.
2317 *
2318 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2319 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2320 * expects the VCPU to be on the blocked_vcpu_list that matches
2321 * PI.NDST.
2322 */
2323 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2324 vcpu->cpu == cpu) {
2325 pi_clear_sn(pi_desc);
2326 return;
2327 }
2328
2329 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002330 do {
2331 old.control = new.control = pi_desc->control;
2332
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002333 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002334
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002335 if (x2apic_enabled())
2336 new.ndst = dest;
2337 else
2338 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002339
Feng Wu28b835d2015-09-18 22:29:54 +08002340 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002341 } while (cmpxchg64(&pi_desc->control, old.control,
2342 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002343}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002344
Peter Feinerc95ba922016-08-17 09:36:47 -07002345static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2346{
2347 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2348 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2349}
2350
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351/*
2352 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2353 * vcpu mutex is already taken.
2354 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002355static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002357 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002358 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002359 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002361 if (!vmm_exclusive)
2362 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002363 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002364 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002366 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002367 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002368 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002369
2370 /*
2371 * Read loaded_vmcs->cpu should be before fetching
2372 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2373 * See the comments in __loaded_vmcs_clear().
2374 */
2375 smp_rmb();
2376
Nadav Har'Eld462b812011-05-24 15:26:10 +03002377 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2378 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002379 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002380 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002381 }
2382
2383 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2384 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2385 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002386 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002387 }
2388
2389 if (!already_loaded) {
2390 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2391 unsigned long sysenter_esp;
2392
2393 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002394
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395 /*
2396 * Linux uses per-cpu TSS and GDT, so set these when switching
2397 * processors.
2398 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002399 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002400 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401
2402 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2403 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002404
Nadav Har'Eld462b812011-05-24 15:26:10 +03002405 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 }
Feng Wu28b835d2015-09-18 22:29:54 +08002407
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002408 /* Setup TSC multiplier */
2409 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002410 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2411 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002412
Feng Wu28b835d2015-09-18 22:29:54 +08002413 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002414 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002415}
2416
2417static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2418{
2419 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2420
2421 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002422 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2423 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002424 return;
2425
2426 /* Set SN when the vCPU is preempted */
2427 if (vcpu->preempted)
2428 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429}
2430
2431static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2432{
Feng Wu28b835d2015-09-18 22:29:54 +08002433 vmx_vcpu_pi_put(vcpu);
2434
Avi Kivitya9b21b62008-06-24 11:48:49 +03002435 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002436 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002437 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2438 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002439 kvm_cpu_vmxoff();
2440 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441}
2442
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002443static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2444{
Avi Kivity81231c62010-01-24 16:26:40 +02002445 ulong cr0;
2446
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002447 if (vcpu->fpu_active)
2448 return;
2449 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002450 cr0 = vmcs_readl(GUEST_CR0);
2451 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2452 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2453 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002454 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002455 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002456 if (is_guest_mode(vcpu))
2457 vcpu->arch.cr0_guest_owned_bits &=
2458 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002459 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002460}
2461
Avi Kivityedcafe32009-12-30 18:07:40 +02002462static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2463
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002464/*
2465 * Return the cr0 value that a nested guest would read. This is a combination
2466 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2467 * its hypervisor (cr0_read_shadow).
2468 */
2469static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2470{
2471 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2472 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2473}
2474static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2475{
2476 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2477 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2478}
2479
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002480static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2481{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002482 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2483 * set this *before* calling this function.
2484 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002485 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002486 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002487 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002488 vcpu->arch.cr0_guest_owned_bits = 0;
2489 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002490 if (is_guest_mode(vcpu)) {
2491 /*
2492 * L1's specified read shadow might not contain the TS bit,
2493 * so now that we turned on shadowing of this bit, we need to
2494 * set this bit of the shadow. Like in nested_vmx_run we need
2495 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2496 * up-to-date here because we just decached cr0.TS (and we'll
2497 * only update vmcs12->guest_cr0 on nested exit).
2498 */
2499 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2500 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2501 (vcpu->arch.cr0 & X86_CR0_TS);
2502 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2503 } else
2504 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002505}
2506
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2508{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002509 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002510
Avi Kivity6de12732011-03-07 12:51:22 +02002511 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2512 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2513 rflags = vmcs_readl(GUEST_RFLAGS);
2514 if (to_vmx(vcpu)->rmode.vm86_active) {
2515 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2516 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2517 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2518 }
2519 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002520 }
Avi Kivity6de12732011-03-07 12:51:22 +02002521 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522}
2523
2524static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2525{
Avi Kivity6de12732011-03-07 12:51:22 +02002526 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2527 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002528 if (to_vmx(vcpu)->rmode.vm86_active) {
2529 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002530 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002531 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532 vmcs_writel(GUEST_RFLAGS, rflags);
2533}
2534
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002535static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2536{
2537 return to_vmx(vcpu)->guest_pkru;
2538}
2539
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002540static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002541{
2542 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2543 int ret = 0;
2544
2545 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002546 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002547 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002548 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002549
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002550 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002551}
2552
2553static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2554{
2555 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2556 u32 interruptibility = interruptibility_old;
2557
2558 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2559
Jan Kiszka48005f62010-02-19 19:38:07 +01002560 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002561 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002562 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002563 interruptibility |= GUEST_INTR_STATE_STI;
2564
2565 if ((interruptibility != interruptibility_old))
2566 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2567}
2568
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2570{
2571 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002573 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002575 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576
Glauber Costa2809f5d2009-05-12 16:21:05 -04002577 /* skipping an emulated instruction also counts */
2578 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579}
2580
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002581/*
2582 * KVM wants to inject page-faults which it got to the guest. This function
2583 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002584 */
Gleb Natapove011c662013-09-25 12:51:35 +03002585static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002586{
2587 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2588
Gleb Natapove011c662013-09-25 12:51:35 +03002589 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002590 return 0;
2591
Wanpeng Lia29fd272017-06-05 05:19:09 -07002592 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002593 vmcs_read32(VM_EXIT_INTR_INFO),
2594 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002595 return 1;
2596}
2597
Avi Kivity298101d2007-11-25 13:41:11 +02002598static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002599 bool has_error_code, u32 error_code,
2600 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002601{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002602 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002603 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002604
Gleb Natapove011c662013-09-25 12:51:35 +03002605 if (!reinject && is_guest_mode(vcpu) &&
2606 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002607 return;
2608
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002609 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002610 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002611 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2612 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002613
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002614 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002615 int inc_eip = 0;
2616 if (kvm_exception_is_soft(nr))
2617 inc_eip = vcpu->arch.event_exit_inst_len;
2618 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002619 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002620 return;
2621 }
2622
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002623 WARN_ON_ONCE(vmx->emulation_required);
2624
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002625 if (kvm_exception_is_soft(nr)) {
2626 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2627 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002628 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2629 } else
2630 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2631
2632 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002633}
2634
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002635static bool vmx_rdtscp_supported(void)
2636{
2637 return cpu_has_vmx_rdtscp();
2638}
2639
Mao, Junjiead756a12012-07-02 01:18:48 +00002640static bool vmx_invpcid_supported(void)
2641{
2642 return cpu_has_vmx_invpcid() && enable_ept;
2643}
2644
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645/*
Eddie Donga75beee2007-05-17 18:55:15 +03002646 * Swap MSR entry in host/guest MSR entry array.
2647 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002648static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002649{
Avi Kivity26bb0982009-09-07 11:14:12 +03002650 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002651
2652 tmp = vmx->guest_msrs[to];
2653 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2654 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002655}
2656
2657/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002658 * Set up the vmcs to automatically save and restore system
2659 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2660 * mode, as fiddling with msrs is very expensive.
2661 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002662static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002663{
Avi Kivity26bb0982009-09-07 11:14:12 +03002664 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002665
Eddie Donga75beee2007-05-17 18:55:15 +03002666 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002667#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002668 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002669 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002670 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002671 move_msr_up(vmx, index, save_nmsrs++);
2672 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002673 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002674 move_msr_up(vmx, index, save_nmsrs++);
2675 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002676 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002677 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002678 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002679 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002680 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002681 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002682 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002683 * if efer.sce is enabled.
2684 */
Brian Gerst8c065852010-07-17 09:03:26 -04002685 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002686 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002687 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002688 }
Eddie Donga75beee2007-05-17 18:55:15 +03002689#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002690 index = __find_msr_index(vmx, MSR_EFER);
2691 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002692 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002693
Avi Kivity26bb0982009-09-07 11:14:12 +03002694 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002695
Yang Zhang8d146952013-01-25 10:18:50 +08002696 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002697 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002698}
2699
2700/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002702 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2703 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002705static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706{
2707 u64 host_tsc, tsc_offset;
2708
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002709 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002711 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712}
2713
2714/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002715 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002716 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002717static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002719 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002720 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002721 * We're here if L1 chose not to trap WRMSR to TSC. According
2722 * to the spec, this should set L1's TSC; The offset that L1
2723 * set for L2 remains unchanged, and still needs to be added
2724 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002725 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002726 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002727 /* recalculate vmcs02.TSC_OFFSET: */
2728 vmcs12 = get_vmcs12(vcpu);
2729 vmcs_write64(TSC_OFFSET, offset +
2730 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2731 vmcs12->tsc_offset : 0));
2732 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002733 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2734 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002735 vmcs_write64(TSC_OFFSET, offset);
2736 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737}
2738
Nadav Har'El801d3422011-05-25 23:02:23 +03002739static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2740{
2741 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2742 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2743}
2744
2745/*
2746 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2747 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2748 * all guests if the "nested" module option is off, and can also be disabled
2749 * for a single guest by disabling its VMX cpuid bit.
2750 */
2751static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2752{
2753 return nested && guest_cpuid_has_vmx(vcpu);
2754}
2755
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002757 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2758 * returned for the various VMX controls MSRs when nested VMX is enabled.
2759 * The same values should also be used to verify that vmcs12 control fields are
2760 * valid during nested entry from L1 to L2.
2761 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2762 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2763 * bit in the high half is on if the corresponding bit in the control field
2764 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002765 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002766static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002767{
2768 /*
2769 * Note that as a general rule, the high half of the MSRs (bits in
2770 * the control fields which may be 1) should be initialized by the
2771 * intersection of the underlying hardware's MSR (i.e., features which
2772 * can be supported) and the list of features we want to expose -
2773 * because they are known to be properly supported in our code.
2774 * Also, usually, the low half of the MSRs (bits which must be 1) can
2775 * be set to 0, meaning that L1 may turn off any of these bits. The
2776 * reason is that if one of these bits is necessary, it will appear
2777 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2778 * fields of vmcs01 and vmcs02, will turn these bits off - and
2779 * nested_vmx_exit_handled() will not pass related exits to L1.
2780 * These rules have exceptions below.
2781 */
2782
2783 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002784 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002785 vmx->nested.nested_vmx_pinbased_ctls_low,
2786 vmx->nested.nested_vmx_pinbased_ctls_high);
2787 vmx->nested.nested_vmx_pinbased_ctls_low |=
2788 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2789 vmx->nested.nested_vmx_pinbased_ctls_high &=
2790 PIN_BASED_EXT_INTR_MASK |
2791 PIN_BASED_NMI_EXITING |
2792 PIN_BASED_VIRTUAL_NMIS;
2793 vmx->nested.nested_vmx_pinbased_ctls_high |=
2794 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002795 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002796 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002797 vmx->nested.nested_vmx_pinbased_ctls_high |=
2798 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002799
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002800 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002801 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002802 vmx->nested.nested_vmx_exit_ctls_low,
2803 vmx->nested.nested_vmx_exit_ctls_high);
2804 vmx->nested.nested_vmx_exit_ctls_low =
2805 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002806
Wincy Vanb9c237b2015-02-03 23:56:30 +08002807 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002808#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002809 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002810#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002811 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002812 vmx->nested.nested_vmx_exit_ctls_high |=
2813 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002814 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002815 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2816
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002817 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002818 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002819
Jan Kiszka2996fca2014-06-16 13:59:43 +02002820 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002821 vmx->nested.nested_vmx_true_exit_ctls_low =
2822 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002823 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2824
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002825 /* entry controls */
2826 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002827 vmx->nested.nested_vmx_entry_ctls_low,
2828 vmx->nested.nested_vmx_entry_ctls_high);
2829 vmx->nested.nested_vmx_entry_ctls_low =
2830 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2831 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002832#ifdef CONFIG_X86_64
2833 VM_ENTRY_IA32E_MODE |
2834#endif
2835 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002836 vmx->nested.nested_vmx_entry_ctls_high |=
2837 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002838 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002839 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002840
Jan Kiszka2996fca2014-06-16 13:59:43 +02002841 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002842 vmx->nested.nested_vmx_true_entry_ctls_low =
2843 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002844 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2845
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002846 /* cpu-based controls */
2847 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002848 vmx->nested.nested_vmx_procbased_ctls_low,
2849 vmx->nested.nested_vmx_procbased_ctls_high);
2850 vmx->nested.nested_vmx_procbased_ctls_low =
2851 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2852 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002853 CPU_BASED_VIRTUAL_INTR_PENDING |
2854 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002855 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2856 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2857 CPU_BASED_CR3_STORE_EXITING |
2858#ifdef CONFIG_X86_64
2859 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2860#endif
2861 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002862 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2863 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2864 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2865 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002866 /*
2867 * We can allow some features even when not supported by the
2868 * hardware. For example, L1 can specify an MSR bitmap - and we
2869 * can use it to avoid exits to L1 - even when L0 runs L2
2870 * without MSR bitmaps.
2871 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002872 vmx->nested.nested_vmx_procbased_ctls_high |=
2873 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002874 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002875
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002876 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002877 vmx->nested.nested_vmx_true_procbased_ctls_low =
2878 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002879 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2880
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002881 /* secondary cpu-based controls */
2882 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002883 vmx->nested.nested_vmx_secondary_ctls_low,
2884 vmx->nested.nested_vmx_secondary_ctls_high);
2885 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2886 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002887 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002888 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002889 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002890 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002891 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002892 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002893 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002894 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002895
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002896 if (enable_ept) {
2897 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002898 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002899 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002900 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002901 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2902 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002903 if (cpu_has_vmx_ept_execute_only())
2904 vmx->nested.nested_vmx_ept_caps |=
2905 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002906 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002907 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2908 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002909 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002910 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002911
Paolo Bonzinief697a72016-03-18 16:58:38 +01002912 /*
2913 * Old versions of KVM use the single-context version without
2914 * checking for support, so declare that it is supported even
2915 * though it is treated as global context. The alternative is
2916 * not failing the single-context invvpid, and it is worse.
2917 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002918 if (enable_vpid)
2919 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01002920 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002921 else
2922 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002923
Radim Krčmář0790ec12015-03-17 14:02:32 +01002924 if (enable_unrestricted_guest)
2925 vmx->nested.nested_vmx_secondary_ctls_high |=
2926 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2927
Jan Kiszkac18911a2013-03-13 16:06:41 +01002928 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002929 rdmsr(MSR_IA32_VMX_MISC,
2930 vmx->nested.nested_vmx_misc_low,
2931 vmx->nested.nested_vmx_misc_high);
2932 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2933 vmx->nested.nested_vmx_misc_low |=
2934 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002935 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002936 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002937}
2938
2939static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2940{
2941 /*
2942 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2943 */
2944 return ((control & high) | low) == control;
2945}
2946
2947static inline u64 vmx_control_msr(u32 low, u32 high)
2948{
2949 return low | ((u64)high << 32);
2950}
2951
Jan Kiszkacae50132014-01-04 18:47:22 +01002952/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002953static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2954{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002955 struct vcpu_vmx *vmx = to_vmx(vcpu);
2956
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002957 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002958 case MSR_IA32_VMX_BASIC:
2959 /*
2960 * This MSR reports some information about VMX support. We
2961 * should return information about the VMX we emulate for the
2962 * guest, and the VMCS structure we give it - not about the
2963 * VMX support of the underlying hardware.
2964 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002965 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002966 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2967 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002968 if (cpu_has_vmx_basic_inout())
2969 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002970 break;
2971 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2972 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002973 *pdata = vmx_control_msr(
2974 vmx->nested.nested_vmx_pinbased_ctls_low,
2975 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002976 break;
2977 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002978 *pdata = vmx_control_msr(
2979 vmx->nested.nested_vmx_true_procbased_ctls_low,
2980 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002981 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002982 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002983 *pdata = vmx_control_msr(
2984 vmx->nested.nested_vmx_procbased_ctls_low,
2985 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002986 break;
2987 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002988 *pdata = vmx_control_msr(
2989 vmx->nested.nested_vmx_true_exit_ctls_low,
2990 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002991 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002992 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002993 *pdata = vmx_control_msr(
2994 vmx->nested.nested_vmx_exit_ctls_low,
2995 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002996 break;
2997 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002998 *pdata = vmx_control_msr(
2999 vmx->nested.nested_vmx_true_entry_ctls_low,
3000 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02003001 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003002 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003003 *pdata = vmx_control_msr(
3004 vmx->nested.nested_vmx_entry_ctls_low,
3005 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003006 break;
3007 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003008 *pdata = vmx_control_msr(
3009 vmx->nested.nested_vmx_misc_low,
3010 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003011 break;
3012 /*
3013 * These MSRs specify bits which the guest must keep fixed (on or off)
3014 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3015 * We picked the standard core2 setting.
3016 */
3017#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3018#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
3019 case MSR_IA32_VMX_CR0_FIXED0:
3020 *pdata = VMXON_CR0_ALWAYSON;
3021 break;
3022 case MSR_IA32_VMX_CR0_FIXED1:
3023 *pdata = -1ULL;
3024 break;
3025 case MSR_IA32_VMX_CR4_FIXED0:
3026 *pdata = VMXON_CR4_ALWAYSON;
3027 break;
3028 case MSR_IA32_VMX_CR4_FIXED1:
3029 *pdata = -1ULL;
3030 break;
3031 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02003032 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003033 break;
3034 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003035 *pdata = vmx_control_msr(
3036 vmx->nested.nested_vmx_secondary_ctls_low,
3037 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003038 break;
3039 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003040 *pdata = vmx->nested.nested_vmx_ept_caps |
3041 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003042 break;
3043 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003044 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003045 }
3046
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003047 return 0;
3048}
3049
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003050static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3051 uint64_t val)
3052{
3053 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3054
3055 return !(val & ~valid_bits);
3056}
3057
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003058/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 * Reads an msr value (of 'msr_index') into 'pdata'.
3060 * Returns 0 on success, non-0 otherwise.
3061 * Assumes vcpu_load() was already called.
3062 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003063static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064{
Avi Kivity26bb0982009-09-07 11:14:12 +03003065 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003067 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003068#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003070 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071 break;
3072 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003073 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003075 case MSR_KERNEL_GS_BASE:
3076 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003077 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003078 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003079#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003081 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303082 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003083 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003085 case MSR_IA32_SPEC_CTRL:
3086 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003087 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003088 return 1;
3089
3090 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3091 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003092 case MSR_IA32_ARCH_CAPABILITIES:
3093 if (!msr_info->host_initiated &&
3094 !guest_cpuid_has_arch_capabilities(vcpu))
3095 return 1;
3096 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3097 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003099 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 break;
3101 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003102 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 break;
3104 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003105 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003107 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003108 if (!kvm_mpx_supported() ||
3109 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003110 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003111 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003112 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003113 case MSR_IA32_MCG_EXT_CTL:
3114 if (!msr_info->host_initiated &&
3115 !(to_vmx(vcpu)->msr_ia32_feature_control &
3116 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003117 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003118 msr_info->data = vcpu->arch.mcg_ext_ctl;
3119 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003120 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003121 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003122 break;
3123 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3124 if (!nested_vmx_allowed(vcpu))
3125 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003126 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003127 case MSR_IA32_XSS:
3128 if (!vmx_xsaves_supported())
3129 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003130 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003131 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003132 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003133 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003134 return 1;
3135 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003137 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003138 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003139 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003140 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003142 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 }
3144
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 return 0;
3146}
3147
Jan Kiszkacae50132014-01-04 18:47:22 +01003148static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3149
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150/*
3151 * Writes msr value into into the appropriate "register".
3152 * Returns 0 on success, non-0 otherwise.
3153 * Assumes vcpu_load() was already called.
3154 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003155static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003157 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003158 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003159 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003160 u32 msr_index = msr_info->index;
3161 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003162
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003164 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003165 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003166 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003167#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003169 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 vmcs_writel(GUEST_FS_BASE, data);
3171 break;
3172 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003173 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 vmcs_writel(GUEST_GS_BASE, data);
3175 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003176 case MSR_KERNEL_GS_BASE:
3177 vmx_load_host_state(vmx);
3178 vmx->msr_guest_kernel_gs_base = data;
3179 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180#endif
3181 case MSR_IA32_SYSENTER_CS:
3182 vmcs_write32(GUEST_SYSENTER_CS, data);
3183 break;
3184 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003185 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 break;
3187 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003188 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003190 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003191 if (!kvm_mpx_supported() ||
3192 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003193 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003194 if (is_noncanonical_address(data & PAGE_MASK) ||
3195 (data & MSR_IA32_BNDCFGS_RSVD))
3196 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003197 vmcs_write64(GUEST_BNDCFGS, data);
3198 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303199 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003200 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003202 case MSR_IA32_SPEC_CTRL:
3203 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003204 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003205 return 1;
3206
3207 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003208 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003209 return 1;
3210
3211 vmx->spec_ctrl = data;
3212
3213 if (!data)
3214 break;
3215
3216 /*
3217 * For non-nested:
3218 * When it's written (to non-zero) for the first time, pass
3219 * it through.
3220 *
3221 * For nested:
3222 * The handling of the MSR bitmap for L2 guests is done in
3223 * nested_vmx_merge_msr_bitmap. We should not touch the
3224 * vmcs02.msr_bitmap here since it gets completely overwritten
3225 * in the merging. We update the vmcs01 here for L1 as well
3226 * since it will end up touching the MSR anyway now.
3227 */
3228 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3229 MSR_IA32_SPEC_CTRL,
3230 MSR_TYPE_RW);
3231 break;
Ashok Raj70131292018-02-01 22:59:43 +01003232 case MSR_IA32_PRED_CMD:
3233 if (!msr_info->host_initiated &&
3234 !guest_cpuid_has_ibpb(vcpu))
3235 return 1;
3236
3237 if (data & ~PRED_CMD_IBPB)
3238 return 1;
3239
3240 if (!data)
3241 break;
3242
3243 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3244
3245 /*
3246 * For non-nested:
3247 * When it's written (to non-zero) for the first time, pass
3248 * it through.
3249 *
3250 * For nested:
3251 * The handling of the MSR bitmap for L2 guests is done in
3252 * nested_vmx_merge_msr_bitmap. We should not touch the
3253 * vmcs02.msr_bitmap here since it gets completely overwritten
3254 * in the merging.
3255 */
3256 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3257 MSR_TYPE_W);
3258 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003259 case MSR_IA32_ARCH_CAPABILITIES:
3260 if (!msr_info->host_initiated)
3261 return 1;
3262 vmx->arch_capabilities = data;
3263 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003264 case MSR_IA32_CR_PAT:
3265 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003266 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3267 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003268 vmcs_write64(GUEST_IA32_PAT, data);
3269 vcpu->arch.pat = data;
3270 break;
3271 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003272 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003273 break;
Will Auldba904632012-11-29 12:42:50 -08003274 case MSR_IA32_TSC_ADJUST:
3275 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003276 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003277 case MSR_IA32_MCG_EXT_CTL:
3278 if ((!msr_info->host_initiated &&
3279 !(to_vmx(vcpu)->msr_ia32_feature_control &
3280 FEATURE_CONTROL_LMCE)) ||
3281 (data & ~MCG_EXT_CTL_LMCE_EN))
3282 return 1;
3283 vcpu->arch.mcg_ext_ctl = data;
3284 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003285 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003286 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003287 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003288 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3289 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003290 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003291 if (msr_info->host_initiated && data == 0)
3292 vmx_leave_nested(vcpu);
3293 break;
3294 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3295 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003296 case MSR_IA32_XSS:
3297 if (!vmx_xsaves_supported())
3298 return 1;
3299 /*
3300 * The only supported bit as of Skylake is bit 8, but
3301 * it is not supported on KVM.
3302 */
3303 if (data != 0)
3304 return 1;
3305 vcpu->arch.ia32_xss = data;
3306 if (vcpu->arch.ia32_xss != host_xss)
3307 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3308 vcpu->arch.ia32_xss, host_xss);
3309 else
3310 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3311 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003312 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003313 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003314 return 1;
3315 /* Check reserved bit, higher 32 bits should be zero */
3316 if ((data >> 32) != 0)
3317 return 1;
3318 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003320 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003321 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003322 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003323 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003324 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3325 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003326 ret = kvm_set_shared_msr(msr->index, msr->data,
3327 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003328 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003329 if (ret)
3330 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003331 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003332 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003333 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003334 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003335 }
3336
Eddie Dong2cc51562007-05-21 07:28:09 +03003337 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003338}
3339
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003340static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003341{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003342 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3343 switch (reg) {
3344 case VCPU_REGS_RSP:
3345 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3346 break;
3347 case VCPU_REGS_RIP:
3348 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3349 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003350 case VCPU_EXREG_PDPTR:
3351 if (enable_ept)
3352 ept_save_pdptrs(vcpu);
3353 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003354 default:
3355 break;
3356 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003357}
3358
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359static __init int cpu_has_kvm_support(void)
3360{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003361 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362}
3363
3364static __init int vmx_disabled_by_bios(void)
3365{
3366 u64 msr;
3367
3368 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003369 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003370 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003371 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3372 && tboot_enabled())
3373 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003374 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003375 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003376 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003377 && !tboot_enabled()) {
3378 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003379 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003380 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003381 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003382 /* launched w/o TXT and VMX disabled */
3383 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3384 && !tboot_enabled())
3385 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003386 }
3387
3388 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389}
3390
Dongxiao Xu7725b892010-05-11 18:29:38 +08003391static void kvm_cpu_vmxon(u64 addr)
3392{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003393 intel_pt_handle_vmx(1);
3394
Dongxiao Xu7725b892010-05-11 18:29:38 +08003395 asm volatile (ASM_VMX_VMXON_RAX
3396 : : "a"(&addr), "m"(addr)
3397 : "memory", "cc");
3398}
3399
Radim Krčmář13a34e02014-08-28 15:13:03 +02003400static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401{
3402 int cpu = raw_smp_processor_id();
3403 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003404 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003406 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003407 return -EBUSY;
3408
Nadav Har'Eld462b812011-05-24 15:26:10 +03003409 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003410 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3411 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003412
3413 /*
3414 * Now we can enable the vmclear operation in kdump
3415 * since the loaded_vmcss_on_cpu list on this cpu
3416 * has been initialized.
3417 *
3418 * Though the cpu is not in VMX operation now, there
3419 * is no problem to enable the vmclear operation
3420 * for the loaded_vmcss_on_cpu list is empty!
3421 */
3422 crash_enable_local_vmclear(cpu);
3423
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003425
3426 test_bits = FEATURE_CONTROL_LOCKED;
3427 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3428 if (tboot_enabled())
3429 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3430
3431 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003433 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3434 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003435 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003436
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003437 if (vmm_exclusive) {
3438 kvm_cpu_vmxon(phys_addr);
3439 ept_sync_global();
3440 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003441
Christoph Lameter89cbc762014-08-17 12:30:40 -05003442 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003443
Alexander Graf10474ae2009-09-15 11:37:46 +02003444 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445}
3446
Nadav Har'Eld462b812011-05-24 15:26:10 +03003447static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003448{
3449 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003450 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003451
Nadav Har'Eld462b812011-05-24 15:26:10 +03003452 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3453 loaded_vmcss_on_cpu_link)
3454 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003455}
3456
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003457
3458/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3459 * tricks.
3460 */
3461static void kvm_cpu_vmxoff(void)
3462{
3463 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003464
3465 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003466}
3467
Radim Krčmář13a34e02014-08-28 15:13:03 +02003468static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003470 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003471 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003472 kvm_cpu_vmxoff();
3473 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003474 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475}
3476
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003477static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003478 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479{
3480 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003481 u32 ctl = ctl_min | ctl_opt;
3482
3483 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3484
3485 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3486 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3487
3488 /* Ensure minimum (required) set of control bits are supported. */
3489 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003490 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003491
3492 *result = ctl;
3493 return 0;
3494}
3495
Avi Kivity110312c2010-12-21 12:54:20 +02003496static __init bool allow_1_setting(u32 msr, u32 ctl)
3497{
3498 u32 vmx_msr_low, vmx_msr_high;
3499
3500 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3501 return vmx_msr_high & ctl;
3502}
3503
Yang, Sheng002c7f72007-07-31 14:23:01 +03003504static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003505{
3506 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003507 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003508 u32 _pin_based_exec_control = 0;
3509 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003510 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003511 u32 _vmexit_control = 0;
3512 u32 _vmentry_control = 0;
3513
Raghavendra K T10166742012-02-07 23:19:20 +05303514 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003515#ifdef CONFIG_X86_64
3516 CPU_BASED_CR8_LOAD_EXITING |
3517 CPU_BASED_CR8_STORE_EXITING |
3518#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003519 CPU_BASED_CR3_LOAD_EXITING |
3520 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003521 CPU_BASED_USE_IO_BITMAPS |
3522 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003523 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003524 CPU_BASED_MWAIT_EXITING |
3525 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003526 CPU_BASED_INVLPG_EXITING |
3527 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003528
Sheng Yangf78e0e22007-10-29 09:40:42 +08003529 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003530 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003531 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003532 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3533 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003534 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003535#ifdef CONFIG_X86_64
3536 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3537 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3538 ~CPU_BASED_CR8_STORE_EXITING;
3539#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003540 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003541 min2 = 0;
3542 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003543 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003544 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003545 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003546 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003547 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003548 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003549 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003550 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003551 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003552 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003553 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003554 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003555 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003556 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003557 if (adjust_vmx_controls(min2, opt2,
3558 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003559 &_cpu_based_2nd_exec_control) < 0)
3560 return -EIO;
3561 }
3562#ifndef CONFIG_X86_64
3563 if (!(_cpu_based_2nd_exec_control &
3564 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3565 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3566#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003567
3568 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3569 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003570 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003571 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3572 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003573
Sheng Yangd56f5462008-04-25 10:13:16 +08003574 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003575 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3576 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003577 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3578 CPU_BASED_CR3_STORE_EXITING |
3579 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003580 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3581 vmx_capability.ept, vmx_capability.vpid);
3582 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003583
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003584 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003585#ifdef CONFIG_X86_64
3586 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3587#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003588 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003589 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003590 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3591 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003592 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003593
Yang Zhang01e439b2013-04-11 19:25:12 +08003594 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003595 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3596 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003597 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3598 &_pin_based_exec_control) < 0)
3599 return -EIO;
3600
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003601 if (cpu_has_broken_vmx_preemption_timer())
3602 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003603 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003604 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003605 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3606
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003607 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003608 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003609 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3610 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003611 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003612
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003613 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003614
3615 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3616 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003617 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003618
3619#ifdef CONFIG_X86_64
3620 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3621 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003622 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003623#endif
3624
3625 /* Require Write-Back (WB) memory type for VMCS accesses. */
3626 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003627 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003628
Yang, Sheng002c7f72007-07-31 14:23:01 +03003629 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003630 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003631 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003632 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003633
Yang, Sheng002c7f72007-07-31 14:23:01 +03003634 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3635 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003636 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003637 vmcs_conf->vmexit_ctrl = _vmexit_control;
3638 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003639
Avi Kivity110312c2010-12-21 12:54:20 +02003640 cpu_has_load_ia32_efer =
3641 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3642 VM_ENTRY_LOAD_IA32_EFER)
3643 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3644 VM_EXIT_LOAD_IA32_EFER);
3645
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003646 cpu_has_load_perf_global_ctrl =
3647 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3648 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3649 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3650 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3651
3652 /*
3653 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003654 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003655 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3656 *
3657 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3658 *
3659 * AAK155 (model 26)
3660 * AAP115 (model 30)
3661 * AAT100 (model 37)
3662 * BC86,AAY89,BD102 (model 44)
3663 * BA97 (model 46)
3664 *
3665 */
3666 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3667 switch (boot_cpu_data.x86_model) {
3668 case 26:
3669 case 30:
3670 case 37:
3671 case 44:
3672 case 46:
3673 cpu_has_load_perf_global_ctrl = false;
3674 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3675 "does not work properly. Using workaround\n");
3676 break;
3677 default:
3678 break;
3679 }
3680 }
3681
Borislav Petkov782511b2016-04-04 22:25:03 +02003682 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003683 rdmsrl(MSR_IA32_XSS, host_xss);
3684
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003685 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003686}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687
3688static struct vmcs *alloc_vmcs_cpu(int cpu)
3689{
3690 int node = cpu_to_node(cpu);
3691 struct page *pages;
3692 struct vmcs *vmcs;
3693
Vlastimil Babka96db8002015-09-08 15:03:50 -07003694 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003695 if (!pages)
3696 return NULL;
3697 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003698 memset(vmcs, 0, vmcs_config.size);
3699 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700 return vmcs;
3701}
3702
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703static void free_vmcs(struct vmcs *vmcs)
3704{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003705 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003706}
3707
Nadav Har'Eld462b812011-05-24 15:26:10 +03003708/*
3709 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3710 */
3711static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3712{
3713 if (!loaded_vmcs->vmcs)
3714 return;
3715 loaded_vmcs_clear(loaded_vmcs);
3716 free_vmcs(loaded_vmcs->vmcs);
3717 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003718 if (loaded_vmcs->msr_bitmap)
3719 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003720 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003721}
3722
Paolo Bonziniff546f92018-01-11 12:16:15 +01003723static struct vmcs *alloc_vmcs(void)
3724{
3725 return alloc_vmcs_cpu(raw_smp_processor_id());
3726}
3727
3728static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3729{
3730 loaded_vmcs->vmcs = alloc_vmcs();
3731 if (!loaded_vmcs->vmcs)
3732 return -ENOMEM;
3733
3734 loaded_vmcs->shadow_vmcs = NULL;
3735 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003736
3737 if (cpu_has_vmx_msr_bitmap()) {
3738 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3739 if (!loaded_vmcs->msr_bitmap)
3740 goto out_vmcs;
3741 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3742 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003743 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003744
3745out_vmcs:
3746 free_loaded_vmcs(loaded_vmcs);
3747 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003748}
3749
Sam Ravnborg39959582007-06-01 00:47:13 -07003750static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003751{
3752 int cpu;
3753
Zachary Amsden3230bb42009-09-29 11:38:37 -10003754 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003756 per_cpu(vmxarea, cpu) = NULL;
3757 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003758}
3759
Bandan Dasfe2b2012014-04-21 15:20:14 -04003760static void init_vmcs_shadow_fields(void)
3761{
3762 int i, j;
3763
3764 /* No checks for read only fields yet */
3765
3766 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3767 switch (shadow_read_write_fields[i]) {
3768 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003769 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003770 continue;
3771 break;
3772 default:
3773 break;
3774 }
3775
3776 if (j < i)
3777 shadow_read_write_fields[j] =
3778 shadow_read_write_fields[i];
3779 j++;
3780 }
3781 max_shadow_read_write_fields = j;
3782
3783 /* shadowed fields guest access without vmexit */
3784 for (i = 0; i < max_shadow_read_write_fields; i++) {
3785 clear_bit(shadow_read_write_fields[i],
3786 vmx_vmwrite_bitmap);
3787 clear_bit(shadow_read_write_fields[i],
3788 vmx_vmread_bitmap);
3789 }
3790 for (i = 0; i < max_shadow_read_only_fields; i++)
3791 clear_bit(shadow_read_only_fields[i],
3792 vmx_vmread_bitmap);
3793}
3794
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795static __init int alloc_kvm_area(void)
3796{
3797 int cpu;
3798
Zachary Amsden3230bb42009-09-29 11:38:37 -10003799 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800 struct vmcs *vmcs;
3801
3802 vmcs = alloc_vmcs_cpu(cpu);
3803 if (!vmcs) {
3804 free_kvm_area();
3805 return -ENOMEM;
3806 }
3807
3808 per_cpu(vmxarea, cpu) = vmcs;
3809 }
3810 return 0;
3811}
3812
Gleb Natapov14168782013-01-21 15:36:49 +02003813static bool emulation_required(struct kvm_vcpu *vcpu)
3814{
3815 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3816}
3817
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003818static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003819 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003820{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003821 if (!emulate_invalid_guest_state) {
3822 /*
3823 * CS and SS RPL should be equal during guest entry according
3824 * to VMX spec, but in reality it is not always so. Since vcpu
3825 * is in the middle of the transition from real mode to
3826 * protected mode it is safe to assume that RPL 0 is a good
3827 * default value.
3828 */
3829 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003830 save->selector &= ~SEGMENT_RPL_MASK;
3831 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003832 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003833 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003834 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835}
3836
3837static void enter_pmode(struct kvm_vcpu *vcpu)
3838{
3839 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003840 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841
Gleb Natapovd99e4152012-12-20 16:57:45 +02003842 /*
3843 * Update real mode segment cache. It may be not up-to-date if sement
3844 * register was written while vcpu was in a guest mode.
3845 */
3846 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3847 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3848 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3849 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3850 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3851 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3852
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003853 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854
Avi Kivity2fb92db2011-04-27 19:42:18 +03003855 vmx_segment_cache_clear(vmx);
3856
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003857 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003858
3859 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003860 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3861 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862 vmcs_writel(GUEST_RFLAGS, flags);
3863
Rusty Russell66aee912007-07-17 23:34:16 +10003864 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3865 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866
3867 update_exception_bitmap(vcpu);
3868
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003869 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3870 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3871 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3872 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3873 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3874 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875}
3876
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003877static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878{
Mathias Krause772e0312012-08-30 01:30:19 +02003879 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003880 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881
Gleb Natapovd99e4152012-12-20 16:57:45 +02003882 var.dpl = 0x3;
3883 if (seg == VCPU_SREG_CS)
3884 var.type = 0x3;
3885
3886 if (!emulate_invalid_guest_state) {
3887 var.selector = var.base >> 4;
3888 var.base = var.base & 0xffff0;
3889 var.limit = 0xffff;
3890 var.g = 0;
3891 var.db = 0;
3892 var.present = 1;
3893 var.s = 1;
3894 var.l = 0;
3895 var.unusable = 0;
3896 var.type = 0x3;
3897 var.avl = 0;
3898 if (save->base & 0xf)
3899 printk_once(KERN_WARNING "kvm: segment base is not "
3900 "paragraph aligned when entering "
3901 "protected mode (seg=%d)", seg);
3902 }
3903
3904 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05003905 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003906 vmcs_write32(sf->limit, var.limit);
3907 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908}
3909
3910static void enter_rmode(struct kvm_vcpu *vcpu)
3911{
3912 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003913 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003914
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003915 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3916 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3917 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3918 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3919 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003920 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3921 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003922
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003923 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924
Gleb Natapov776e58e2011-03-13 12:34:27 +02003925 /*
3926 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003927 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003928 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003929 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003930 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3931 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003932
Avi Kivity2fb92db2011-04-27 19:42:18 +03003933 vmx_segment_cache_clear(vmx);
3934
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003935 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3938
3939 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003940 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003942 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943
3944 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003945 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946 update_exception_bitmap(vcpu);
3947
Gleb Natapovd99e4152012-12-20 16:57:45 +02003948 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3949 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3950 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3951 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3952 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3953 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003954
Eddie Dong8668a3c2007-10-10 14:26:45 +08003955 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003956}
3957
Amit Shah401d10d2009-02-20 22:53:37 +05303958static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3959{
3960 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003961 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3962
3963 if (!msr)
3964 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303965
Avi Kivity44ea2b12009-09-06 15:55:37 +03003966 /*
3967 * Force kernel_gs_base reloading before EFER changes, as control
3968 * of this msr depends on is_long_mode().
3969 */
3970 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003971 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303972 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003973 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303974 msr->data = efer;
3975 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003976 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303977
3978 msr->data = efer & ~EFER_LME;
3979 }
3980 setup_msrs(vmx);
3981}
3982
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003983#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984
3985static void enter_lmode(struct kvm_vcpu *vcpu)
3986{
3987 u32 guest_tr_ar;
3988
Avi Kivity2fb92db2011-04-27 19:42:18 +03003989 vmx_segment_cache_clear(to_vmx(vcpu));
3990
Avi Kivity6aa8b732006-12-10 02:21:36 -08003991 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003992 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003993 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3994 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003996 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3997 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003998 }
Avi Kivityda38f432010-07-06 11:30:49 +03003999 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004000}
4001
4002static void exit_lmode(struct kvm_vcpu *vcpu)
4003{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004004 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004005 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006}
4007
4008#endif
4009
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004010static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004011{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004012 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004013 if (enable_ept) {
4014 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4015 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004016 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004017 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004018}
4019
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004020static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4021{
4022 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4023}
4024
Jim Mattson8386ff52017-03-16 13:53:59 -07004025static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4026{
4027 if (enable_ept)
4028 vmx_flush_tlb(vcpu);
4029}
4030
Avi Kivitye8467fd2009-12-29 18:43:06 +02004031static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4032{
4033 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4034
4035 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4036 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4037}
4038
Avi Kivityaff48ba2010-12-05 18:56:11 +02004039static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4040{
4041 if (enable_ept && is_paging(vcpu))
4042 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4043 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4044}
4045
Anthony Liguori25c4c272007-04-27 09:29:21 +03004046static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004047{
Avi Kivityfc78f512009-12-07 12:16:48 +02004048 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4049
4050 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4051 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004052}
4053
Sheng Yang14394422008-04-28 12:24:45 +08004054static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4055{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004056 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4057
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004058 if (!test_bit(VCPU_EXREG_PDPTR,
4059 (unsigned long *)&vcpu->arch.regs_dirty))
4060 return;
4061
Sheng Yang14394422008-04-28 12:24:45 +08004062 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004063 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4064 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4065 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4066 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004067 }
4068}
4069
Avi Kivity8f5d5492009-05-31 18:41:29 +03004070static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4071{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004072 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4073
Avi Kivity8f5d5492009-05-31 18:41:29 +03004074 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004075 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4076 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4077 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4078 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004079 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004080
4081 __set_bit(VCPU_EXREG_PDPTR,
4082 (unsigned long *)&vcpu->arch.regs_avail);
4083 __set_bit(VCPU_EXREG_PDPTR,
4084 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004085}
4086
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004087static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004088
4089static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4090 unsigned long cr0,
4091 struct kvm_vcpu *vcpu)
4092{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004093 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4094 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004095 if (!(cr0 & X86_CR0_PG)) {
4096 /* From paging/starting to nonpaging */
4097 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004098 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004099 (CPU_BASED_CR3_LOAD_EXITING |
4100 CPU_BASED_CR3_STORE_EXITING));
4101 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004102 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004103 } else if (!is_paging(vcpu)) {
4104 /* From nonpaging to paging */
4105 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004106 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004107 ~(CPU_BASED_CR3_LOAD_EXITING |
4108 CPU_BASED_CR3_STORE_EXITING));
4109 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004110 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004111 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004112
4113 if (!(cr0 & X86_CR0_WP))
4114 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004115}
4116
Avi Kivity6aa8b732006-12-10 02:21:36 -08004117static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4118{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004119 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004120 unsigned long hw_cr0;
4121
Gleb Natapov50378782013-02-04 16:00:28 +02004122 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004123 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004124 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004125 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004126 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004127
Gleb Natapov218e7632013-01-21 15:36:45 +02004128 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4129 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004130
Gleb Natapov218e7632013-01-21 15:36:45 +02004131 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4132 enter_rmode(vcpu);
4133 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004135#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004136 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10004137 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10004139 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140 exit_lmode(vcpu);
4141 }
4142#endif
4143
Avi Kivity089d0342009-03-23 18:26:32 +02004144 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004145 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4146
Avi Kivity02daab22009-12-30 12:40:26 +02004147 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004148 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004149
Avi Kivity6aa8b732006-12-10 02:21:36 -08004150 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004151 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004152 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004153
4154 /* depends on vcpu->arch.cr0 to be set to a new value */
4155 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156}
4157
Sheng Yang14394422008-04-28 12:24:45 +08004158static u64 construct_eptp(unsigned long root_hpa)
4159{
4160 u64 eptp;
4161
4162 /* TODO write the value reading from MSR */
4163 eptp = VMX_EPT_DEFAULT_MT |
4164 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004165 if (enable_ept_ad_bits)
4166 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004167 eptp |= (root_hpa & PAGE_MASK);
4168
4169 return eptp;
4170}
4171
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4173{
Sheng Yang14394422008-04-28 12:24:45 +08004174 unsigned long guest_cr3;
4175 u64 eptp;
4176
4177 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004178 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004179 eptp = construct_eptp(cr3);
4180 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004181 if (is_paging(vcpu) || is_guest_mode(vcpu))
4182 guest_cr3 = kvm_read_cr3(vcpu);
4183 else
4184 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004185 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004186 }
4187
Sheng Yang2384d2b2008-01-17 15:14:33 +08004188 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004189 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190}
4191
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004192static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004194 /*
4195 * Pass through host's Machine Check Enable value to hw_cr4, which
4196 * is in force while we are in guest mode. Do not let guests control
4197 * this bit, even if host CR4.MCE == 0.
4198 */
4199 unsigned long hw_cr4 =
4200 (cr4_read_shadow() & X86_CR4_MCE) |
4201 (cr4 & ~X86_CR4_MCE) |
4202 (to_vmx(vcpu)->rmode.vm86_active ?
4203 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004204
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004205 if (cr4 & X86_CR4_VMXE) {
4206 /*
4207 * To use VMXON (and later other VMX instructions), a guest
4208 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4209 * So basically the check on whether to allow nested VMX
4210 * is here.
4211 */
4212 if (!nested_vmx_allowed(vcpu))
4213 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004214 }
4215 if (to_vmx(vcpu)->nested.vmxon &&
4216 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004217 return 1;
4218
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004219 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004220 if (enable_ept) {
4221 if (!is_paging(vcpu)) {
4222 hw_cr4 &= ~X86_CR4_PAE;
4223 hw_cr4 |= X86_CR4_PSE;
4224 } else if (!(cr4 & X86_CR4_PAE)) {
4225 hw_cr4 &= ~X86_CR4_PAE;
4226 }
4227 }
Sheng Yang14394422008-04-28 12:24:45 +08004228
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004229 if (!enable_unrestricted_guest && !is_paging(vcpu))
4230 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004231 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4232 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4233 * to be manually disabled when guest switches to non-paging
4234 * mode.
4235 *
4236 * If !enable_unrestricted_guest, the CPU is always running
4237 * with CR0.PG=1 and CR4 needs to be modified.
4238 * If enable_unrestricted_guest, the CPU automatically
4239 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004240 */
Huaitong Handdba2622016-03-22 16:51:15 +08004241 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004242
Sheng Yang14394422008-04-28 12:24:45 +08004243 vmcs_writel(CR4_READ_SHADOW, cr4);
4244 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004245 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004246}
4247
Avi Kivity6aa8b732006-12-10 02:21:36 -08004248static void vmx_get_segment(struct kvm_vcpu *vcpu,
4249 struct kvm_segment *var, int seg)
4250{
Avi Kivitya9179492011-01-03 14:28:52 +02004251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004252 u32 ar;
4253
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004254 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004255 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004256 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004257 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004258 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004259 var->base = vmx_read_guest_seg_base(vmx, seg);
4260 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4261 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004262 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004263 var->base = vmx_read_guest_seg_base(vmx, seg);
4264 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4265 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4266 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004267 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004268 var->type = ar & 15;
4269 var->s = (ar >> 4) & 1;
4270 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004271 /*
4272 * Some userspaces do not preserve unusable property. Since usable
4273 * segment has to be present according to VMX spec we can use present
4274 * property to amend userspace bug by making unusable segment always
4275 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4276 * segment as unusable.
4277 */
4278 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004279 var->avl = (ar >> 12) & 1;
4280 var->l = (ar >> 13) & 1;
4281 var->db = (ar >> 14) & 1;
4282 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004283}
4284
Avi Kivitya9179492011-01-03 14:28:52 +02004285static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4286{
Avi Kivitya9179492011-01-03 14:28:52 +02004287 struct kvm_segment s;
4288
4289 if (to_vmx(vcpu)->rmode.vm86_active) {
4290 vmx_get_segment(vcpu, &s, seg);
4291 return s.base;
4292 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004293 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004294}
4295
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004296static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004297{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004298 struct vcpu_vmx *vmx = to_vmx(vcpu);
4299
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004300 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004301 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004302 else {
4303 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004304 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004305 }
Avi Kivity69c73022011-03-07 15:26:44 +02004306}
4307
Avi Kivity653e3102007-05-07 10:55:37 +03004308static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004309{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004310 u32 ar;
4311
Avi Kivityf0495f92012-06-07 17:06:10 +03004312 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313 ar = 1 << 16;
4314 else {
4315 ar = var->type & 15;
4316 ar |= (var->s & 1) << 4;
4317 ar |= (var->dpl & 3) << 5;
4318 ar |= (var->present & 1) << 7;
4319 ar |= (var->avl & 1) << 12;
4320 ar |= (var->l & 1) << 13;
4321 ar |= (var->db & 1) << 14;
4322 ar |= (var->g & 1) << 15;
4323 }
Avi Kivity653e3102007-05-07 10:55:37 +03004324
4325 return ar;
4326}
4327
4328static void vmx_set_segment(struct kvm_vcpu *vcpu,
4329 struct kvm_segment *var, int seg)
4330{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004331 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004332 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004333
Avi Kivity2fb92db2011-04-27 19:42:18 +03004334 vmx_segment_cache_clear(vmx);
4335
Gleb Natapov1ecd50a92012-12-12 19:10:54 +02004336 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4337 vmx->rmode.segs[seg] = *var;
4338 if (seg == VCPU_SREG_TR)
4339 vmcs_write16(sf->selector, var->selector);
4340 else if (var->s)
4341 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004342 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004343 }
Gleb Natapov1ecd50a92012-12-12 19:10:54 +02004344
Avi Kivity653e3102007-05-07 10:55:37 +03004345 vmcs_writel(sf->base, var->base);
4346 vmcs_write32(sf->limit, var->limit);
4347 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004348
4349 /*
4350 * Fix the "Accessed" bit in AR field of segment registers for older
4351 * qemu binaries.
4352 * IA32 arch specifies that at the time of processor reset the
4353 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004354 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004355 * state vmexit when "unrestricted guest" mode is turned on.
4356 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4357 * tree. Newer qemu binaries with that qemu fix would not need this
4358 * kvm hack.
4359 */
4360 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004361 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004362
Gleb Natapovf924d662012-12-12 19:10:55 +02004363 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004364
4365out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004366 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004367}
4368
Avi Kivity6aa8b732006-12-10 02:21:36 -08004369static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4370{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004371 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004372
4373 *db = (ar >> 14) & 1;
4374 *l = (ar >> 13) & 1;
4375}
4376
Gleb Natapov89a27f42010-02-16 10:51:48 +02004377static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004379 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4380 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004381}
4382
Gleb Natapov89a27f42010-02-16 10:51:48 +02004383static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004384{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004385 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4386 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387}
4388
Gleb Natapov89a27f42010-02-16 10:51:48 +02004389static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004390{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004391 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4392 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004393}
4394
Gleb Natapov89a27f42010-02-16 10:51:48 +02004395static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004397 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4398 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399}
4400
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004401static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4402{
4403 struct kvm_segment var;
4404 u32 ar;
4405
4406 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004407 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004408 if (seg == VCPU_SREG_CS)
4409 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004410 ar = vmx_segment_access_rights(&var);
4411
4412 if (var.base != (var.selector << 4))
4413 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004414 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004415 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004416 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004417 return false;
4418
4419 return true;
4420}
4421
4422static bool code_segment_valid(struct kvm_vcpu *vcpu)
4423{
4424 struct kvm_segment cs;
4425 unsigned int cs_rpl;
4426
4427 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004428 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004429
Avi Kivity1872a3f2009-01-04 23:26:52 +02004430 if (cs.unusable)
4431 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004432 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004433 return false;
4434 if (!cs.s)
4435 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004436 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004437 if (cs.dpl > cs_rpl)
4438 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004439 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004440 if (cs.dpl != cs_rpl)
4441 return false;
4442 }
4443 if (!cs.present)
4444 return false;
4445
4446 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4447 return true;
4448}
4449
4450static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4451{
4452 struct kvm_segment ss;
4453 unsigned int ss_rpl;
4454
4455 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004456 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004457
Avi Kivity1872a3f2009-01-04 23:26:52 +02004458 if (ss.unusable)
4459 return true;
4460 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004461 return false;
4462 if (!ss.s)
4463 return false;
4464 if (ss.dpl != ss_rpl) /* DPL != RPL */
4465 return false;
4466 if (!ss.present)
4467 return false;
4468
4469 return true;
4470}
4471
4472static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4473{
4474 struct kvm_segment var;
4475 unsigned int rpl;
4476
4477 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004478 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004479
Avi Kivity1872a3f2009-01-04 23:26:52 +02004480 if (var.unusable)
4481 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004482 if (!var.s)
4483 return false;
4484 if (!var.present)
4485 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004486 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004487 if (var.dpl < rpl) /* DPL < RPL */
4488 return false;
4489 }
4490
4491 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4492 * rights flags
4493 */
4494 return true;
4495}
4496
4497static bool tr_valid(struct kvm_vcpu *vcpu)
4498{
4499 struct kvm_segment tr;
4500
4501 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4502
Avi Kivity1872a3f2009-01-04 23:26:52 +02004503 if (tr.unusable)
4504 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004505 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004506 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004507 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004508 return false;
4509 if (!tr.present)
4510 return false;
4511
4512 return true;
4513}
4514
4515static bool ldtr_valid(struct kvm_vcpu *vcpu)
4516{
4517 struct kvm_segment ldtr;
4518
4519 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4520
Avi Kivity1872a3f2009-01-04 23:26:52 +02004521 if (ldtr.unusable)
4522 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004523 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004524 return false;
4525 if (ldtr.type != 2)
4526 return false;
4527 if (!ldtr.present)
4528 return false;
4529
4530 return true;
4531}
4532
4533static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4534{
4535 struct kvm_segment cs, ss;
4536
4537 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4538 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4539
Nadav Amitb32a9912015-03-29 16:33:04 +03004540 return ((cs.selector & SEGMENT_RPL_MASK) ==
4541 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004542}
4543
4544/*
4545 * Check if guest state is valid. Returns true if valid, false if
4546 * not.
4547 * We assume that registers are always usable
4548 */
4549static bool guest_state_valid(struct kvm_vcpu *vcpu)
4550{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004551 if (enable_unrestricted_guest)
4552 return true;
4553
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004554 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004555 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004556 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4557 return false;
4558 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4559 return false;
4560 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4561 return false;
4562 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4563 return false;
4564 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4565 return false;
4566 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4567 return false;
4568 } else {
4569 /* protected mode guest state checks */
4570 if (!cs_ss_rpl_check(vcpu))
4571 return false;
4572 if (!code_segment_valid(vcpu))
4573 return false;
4574 if (!stack_segment_valid(vcpu))
4575 return false;
4576 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4577 return false;
4578 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4579 return false;
4580 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4581 return false;
4582 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4583 return false;
4584 if (!tr_valid(vcpu))
4585 return false;
4586 if (!ldtr_valid(vcpu))
4587 return false;
4588 }
4589 /* TODO:
4590 * - Add checks on RIP
4591 * - Add checks on RFLAGS
4592 */
4593
4594 return true;
4595}
4596
Mike Dayd77c26f2007-10-08 09:02:08 -04004597static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004598{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004599 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004600 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004601 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004602
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004603 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004604 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004605 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4606 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004607 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004608 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004609 r = kvm_write_guest_page(kvm, fn++, &data,
4610 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004611 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004612 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004613 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4614 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004615 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004616 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4617 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004618 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004619 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004620 r = kvm_write_guest_page(kvm, fn, &data,
4621 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4622 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004623out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004624 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004625 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004626}
4627
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004628static int init_rmode_identity_map(struct kvm *kvm)
4629{
Tang Chenf51770e2014-09-16 18:41:59 +08004630 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004631 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004632 u32 tmp;
4633
Avi Kivity089d0342009-03-23 18:26:32 +02004634 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004635 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004636
4637 /* Protect kvm->arch.ept_identity_pagetable_done. */
4638 mutex_lock(&kvm->slots_lock);
4639
Tang Chenf51770e2014-09-16 18:41:59 +08004640 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004641 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004642
Sheng Yangb927a3c2009-07-21 10:42:48 +08004643 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004644
4645 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004646 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004647 goto out2;
4648
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004649 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004650 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4651 if (r < 0)
4652 goto out;
4653 /* Set up identity-mapping pagetable for EPT in real mode */
4654 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4655 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4656 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4657 r = kvm_write_guest_page(kvm, identity_map_pfn,
4658 &tmp, i * sizeof(tmp), sizeof(tmp));
4659 if (r < 0)
4660 goto out;
4661 }
4662 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004663
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004664out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004665 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004666
4667out2:
4668 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004669 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004670}
4671
Avi Kivity6aa8b732006-12-10 02:21:36 -08004672static void seg_setup(int seg)
4673{
Mathias Krause772e0312012-08-30 01:30:19 +02004674 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004675 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004676
4677 vmcs_write16(sf->selector, 0);
4678 vmcs_writel(sf->base, 0);
4679 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004680 ar = 0x93;
4681 if (seg == VCPU_SREG_CS)
4682 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004683
4684 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004685}
4686
Sheng Yangf78e0e22007-10-29 09:40:42 +08004687static int alloc_apic_access_page(struct kvm *kvm)
4688{
Xiao Guangrong44841412012-09-07 14:14:20 +08004689 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004690 int r = 0;
4691
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004692 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004693 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004694 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004695 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4696 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004697 if (r)
4698 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004699
Tang Chen73a6d942014-09-11 13:38:00 +08004700 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004701 if (is_error_page(page)) {
4702 r = -EFAULT;
4703 goto out;
4704 }
4705
Tang Chenc24ae0d2014-09-24 15:57:58 +08004706 /*
4707 * Do not pin the page in memory, so that memory hot-unplug
4708 * is able to migrate it.
4709 */
4710 put_page(page);
4711 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004712out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004713 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004714 return r;
4715}
4716
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004717static int alloc_identity_pagetable(struct kvm *kvm)
4718{
Tang Chena255d472014-09-16 18:41:58 +08004719 /* Called with kvm->slots_lock held. */
4720
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004721 int r = 0;
4722
Tang Chena255d472014-09-16 18:41:58 +08004723 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4724
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004725 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4726 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004727
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004728 return r;
4729}
4730
Wanpeng Li991e7a02015-09-16 17:30:05 +08004731static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004732{
4733 int vpid;
4734
Avi Kivity919818a2009-03-23 18:01:29 +02004735 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004736 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004737 spin_lock(&vmx_vpid_lock);
4738 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004739 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004740 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004741 else
4742 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004743 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004744 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004745}
4746
Wanpeng Li991e7a02015-09-16 17:30:05 +08004747static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004748{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004749 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004750 return;
4751 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004752 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004753 spin_unlock(&vmx_vpid_lock);
4754}
4755
Paolo Bonzini6236b782018-01-16 16:51:18 +01004756static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4757 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004758{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004759 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004760
4761 if (!cpu_has_vmx_msr_bitmap())
4762 return;
4763
4764 /*
4765 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4766 * have the write-low and read-high bitmap offsets the wrong way round.
4767 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4768 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004769 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004770 if (type & MSR_TYPE_R)
4771 /* read-low */
4772 __clear_bit(msr, msr_bitmap + 0x000 / f);
4773
4774 if (type & MSR_TYPE_W)
4775 /* write-low */
4776 __clear_bit(msr, msr_bitmap + 0x800 / f);
4777
Sheng Yang25c5f222008-03-28 13:18:56 +08004778 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4779 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004780 if (type & MSR_TYPE_R)
4781 /* read-high */
4782 __clear_bit(msr, msr_bitmap + 0x400 / f);
4783
4784 if (type & MSR_TYPE_W)
4785 /* write-high */
4786 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4787
4788 }
4789}
4790
Paolo Bonzini6236b782018-01-16 16:51:18 +01004791static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4792 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004793{
4794 int f = sizeof(unsigned long);
4795
4796 if (!cpu_has_vmx_msr_bitmap())
4797 return;
4798
4799 /*
4800 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4801 * have the write-low and read-high bitmap offsets the wrong way round.
4802 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4803 */
4804 if (msr <= 0x1fff) {
4805 if (type & MSR_TYPE_R)
4806 /* read-low */
4807 __set_bit(msr, msr_bitmap + 0x000 / f);
4808
4809 if (type & MSR_TYPE_W)
4810 /* write-low */
4811 __set_bit(msr, msr_bitmap + 0x800 / f);
4812
4813 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4814 msr &= 0x1fff;
4815 if (type & MSR_TYPE_R)
4816 /* read-high */
4817 __set_bit(msr, msr_bitmap + 0x400 / f);
4818
4819 if (type & MSR_TYPE_W)
4820 /* write-high */
4821 __set_bit(msr, msr_bitmap + 0xc00 / f);
4822
Sheng Yang25c5f222008-03-28 13:18:56 +08004823 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004824}
4825
Paolo Bonzini6236b782018-01-16 16:51:18 +01004826static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4827 u32 msr, int type, bool value)
4828{
4829 if (value)
4830 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4831 else
4832 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4833}
4834
Wincy Vanf2b93282015-02-03 23:56:03 +08004835/*
4836 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4837 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4838 */
4839static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4840 unsigned long *msr_bitmap_nested,
4841 u32 msr, int type)
4842{
4843 int f = sizeof(unsigned long);
4844
4845 if (!cpu_has_vmx_msr_bitmap()) {
4846 WARN_ON(1);
4847 return;
4848 }
4849
4850 /*
4851 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4852 * have the write-low and read-high bitmap offsets the wrong way round.
4853 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4854 */
4855 if (msr <= 0x1fff) {
4856 if (type & MSR_TYPE_R &&
4857 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4858 /* read-low */
4859 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4860
4861 if (type & MSR_TYPE_W &&
4862 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4863 /* write-low */
4864 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4865
4866 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4867 msr &= 0x1fff;
4868 if (type & MSR_TYPE_R &&
4869 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4870 /* read-high */
4871 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4872
4873 if (type & MSR_TYPE_W &&
4874 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4875 /* write-high */
4876 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4877
4878 }
4879}
4880
Paolo Bonzini6236b782018-01-16 16:51:18 +01004881static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004882{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004883 u8 mode = 0;
4884
4885 if (cpu_has_secondary_exec_ctrls() &&
4886 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4887 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4888 mode |= MSR_BITMAP_MODE_X2APIC;
4889 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4890 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4891 }
4892
4893 if (is_long_mode(vcpu))
4894 mode |= MSR_BITMAP_MODE_LM;
4895
4896 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004897}
4898
Paolo Bonzini6236b782018-01-16 16:51:18 +01004899#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
4900
4901static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4902 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08004903{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004904 int msr;
4905
4906 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4907 unsigned word = msr / BITS_PER_LONG;
4908 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4909 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4910 }
4911
4912 if (mode & MSR_BITMAP_MODE_X2APIC) {
4913 /*
4914 * TPR reads and writes can be virtualized even if virtual interrupt
4915 * delivery is not in use.
4916 */
4917 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4918 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4919 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4920 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4921 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4922 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004923 }
Yang Zhang8d146952013-01-25 10:18:50 +08004924}
4925
Paolo Bonzini6236b782018-01-16 16:51:18 +01004926static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08004927{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004928 struct vcpu_vmx *vmx = to_vmx(vcpu);
4929 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4930 u8 mode = vmx_msr_bitmap_mode(vcpu);
4931 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004932
Paolo Bonzini6236b782018-01-16 16:51:18 +01004933 if (!changed)
4934 return;
4935
4936 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
4937 !(mode & MSR_BITMAP_MODE_LM));
4938
4939 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4940 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4941
4942 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02004943}
4944
Andrey Smetanind62caab2015-11-10 15:36:33 +03004945static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004946{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004947 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004948}
4949
David Matlackb7649e12017-08-01 14:00:40 -07004950static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
4951{
4952 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4953 gfn_t gfn;
4954
4955 /*
4956 * Don't need to mark the APIC access page dirty; it is never
4957 * written to by the CPU during APIC virtualization.
4958 */
4959
4960 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
4961 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
4962 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4963 }
4964
4965 if (nested_cpu_has_posted_intr(vmcs12)) {
4966 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
4967 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4968 }
4969}
4970
4971
David Hildenbrand1edccf22017-01-25 11:58:58 +01004972static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08004973{
4974 struct vcpu_vmx *vmx = to_vmx(vcpu);
4975 int max_irr;
4976 void *vapic_page;
4977 u16 status;
4978
David Matlackb7649e12017-08-01 14:00:40 -07004979 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
4980 return;
Wincy Van705699a2015-02-03 23:58:17 +08004981
David Matlackb7649e12017-08-01 14:00:40 -07004982 vmx->nested.pi_pending = false;
4983 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4984 return;
Wincy Van705699a2015-02-03 23:58:17 +08004985
David Matlackb7649e12017-08-01 14:00:40 -07004986 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
4987 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08004988 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08004989 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4990 kunmap(vmx->nested.virtual_apic_page);
4991
4992 status = vmcs_read16(GUEST_INTR_STATUS);
4993 if ((u8)max_irr > ((u8)status & 0xff)) {
4994 status &= ~0xff;
4995 status |= (u8)max_irr;
4996 vmcs_write16(GUEST_INTR_STATUS, status);
4997 }
4998 }
David Matlackb7649e12017-08-01 14:00:40 -07004999
5000 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005001}
5002
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005003static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
5004{
5005#ifdef CONFIG_SMP
5006 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08005007 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005008 * The vector of interrupt to be delivered to vcpu had
5009 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08005010 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08005011 * Following cases will be reached in this block, and
5012 * we always send a notification event in all cases as
5013 * explained below.
5014 *
5015 * Case 1: vcpu keeps in non-root mode. Sending a
5016 * notification event posts the interrupt to vcpu.
5017 *
5018 * Case 2: vcpu exits to root mode and is still
5019 * runnable. PIR will be synced to vIRR before the
5020 * next vcpu entry. Sending a notification event in
5021 * this case has no effect, as vcpu is not in root
5022 * mode.
5023 *
5024 * Case 3: vcpu exits to root mode and is blocked.
5025 * vcpu_block() has already synced PIR to vIRR and
5026 * never blocks vcpu if vIRR is not cleared. Therefore,
5027 * a blocked vcpu here does not wait for any requested
5028 * interrupts in PIR, and sending a notification event
5029 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08005030 */
Feng Wu28b835d2015-09-18 22:29:54 +08005031
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005032 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
5033 POSTED_INTR_VECTOR);
5034 return true;
5035 }
5036#endif
5037 return false;
5038}
5039
Wincy Van705699a2015-02-03 23:58:17 +08005040static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
5041 int vector)
5042{
5043 struct vcpu_vmx *vmx = to_vmx(vcpu);
5044
5045 if (is_guest_mode(vcpu) &&
5046 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08005047 /*
5048 * If a posted intr is not recognized by hardware,
5049 * we will accomplish it in the next vmentry.
5050 */
5051 vmx->nested.pi_pending = true;
5052 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02005053 /* the PIR and ON have been set by L1. */
5054 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
5055 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08005056 return 0;
5057 }
5058 return -1;
5059}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005060/*
Yang Zhanga20ed542013-04-11 19:25:15 +08005061 * Send interrupt to vcpu via posted interrupt way.
5062 * 1. If target vcpu is running(non-root mode), send posted interrupt
5063 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5064 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5065 * interrupt from PIR in next vmentry.
5066 */
5067static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5068{
5069 struct vcpu_vmx *vmx = to_vmx(vcpu);
5070 int r;
5071
Wincy Van705699a2015-02-03 23:58:17 +08005072 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5073 if (!r)
5074 return;
5075
Yang Zhanga20ed542013-04-11 19:25:15 +08005076 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5077 return;
5078
5079 r = pi_test_and_set_on(&vmx->pi_desc);
5080 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005081 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005082 kvm_vcpu_kick(vcpu);
5083}
5084
5085static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5086{
5087 struct vcpu_vmx *vmx = to_vmx(vcpu);
5088
5089 if (!pi_test_and_clear_on(&vmx->pi_desc))
5090 return;
5091
5092 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5093}
5094
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005096 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5097 * will not change in the lifetime of the guest.
5098 * Note that host-state that does change is set elsewhere. E.g., host-state
5099 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5100 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005101static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005102{
5103 u32 low32, high32;
5104 unsigned long tmpl;
5105 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005106 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005107
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005108 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005109 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5110
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005111 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005112 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005113 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5114 vmx->host_state.vmcs_host_cr4 = cr4;
5115
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005116 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005117#ifdef CONFIG_X86_64
5118 /*
5119 * Load null selectors, so we can avoid reloading them in
5120 * __vmx_load_host_state(), in case userspace uses the null selectors
5121 * too (the expected case).
5122 */
5123 vmcs_write16(HOST_DS_SELECTOR, 0);
5124 vmcs_write16(HOST_ES_SELECTOR, 0);
5125#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005126 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5127 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005128#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005129 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5130 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5131
5132 native_store_idt(&dt);
5133 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005134 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005135
Avi Kivity83287ea422012-09-16 15:10:57 +03005136 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005137
5138 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5139 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5140 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5141 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5142
5143 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5144 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5145 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5146 }
5147}
5148
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005149static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5150{
5151 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5152 if (enable_ept)
5153 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005154 if (is_guest_mode(&vmx->vcpu))
5155 vmx->vcpu.arch.cr4_guest_owned_bits &=
5156 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005157 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5158}
5159
Yang Zhang01e439b2013-04-11 19:25:12 +08005160static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5161{
5162 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5163
Andrey Smetanind62caab2015-11-10 15:36:33 +03005164 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005165 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005166 /* Enable the preemption timer dynamically */
5167 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005168 return pin_based_exec_ctrl;
5169}
5170
Andrey Smetanind62caab2015-11-10 15:36:33 +03005171static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5172{
5173 struct vcpu_vmx *vmx = to_vmx(vcpu);
5174
5175 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005176 if (cpu_has_secondary_exec_ctrls()) {
5177 if (kvm_vcpu_apicv_active(vcpu))
5178 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5179 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5180 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5181 else
5182 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5183 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5184 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5185 }
5186
5187 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005188 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005189}
5190
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005191static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5192{
5193 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005194
5195 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5196 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5197
Paolo Bonzini35754c92015-07-29 12:05:37 +02005198 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005199 exec_control &= ~CPU_BASED_TPR_SHADOW;
5200#ifdef CONFIG_X86_64
5201 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5202 CPU_BASED_CR8_LOAD_EXITING;
5203#endif
5204 }
5205 if (!enable_ept)
5206 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5207 CPU_BASED_CR3_LOAD_EXITING |
5208 CPU_BASED_INVLPG_EXITING;
5209 return exec_control;
5210}
5211
5212static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5213{
5214 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005215 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005216 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5217 if (vmx->vpid == 0)
5218 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5219 if (!enable_ept) {
5220 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5221 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005222 /* Enable INVPCID for non-ept guests may cause performance regression. */
5223 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005224 }
5225 if (!enable_unrestricted_guest)
5226 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5227 if (!ple_gap)
5228 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005229 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005230 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5231 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005232 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005233 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5234 (handle_vmptrld).
5235 We can NOT enable shadow_vmcs here because we don't have yet
5236 a current VMCS12
5237 */
5238 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005239
5240 if (!enable_pml)
5241 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005242
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005243 return exec_control;
5244}
5245
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005246static void ept_set_mmio_spte_mask(void)
5247{
5248 /*
5249 * EPT Misconfigurations can be generated if the value of bits 2:0
5250 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005251 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005252 * spte.
5253 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005254 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005255}
5256
Wanpeng Lif53cd632014-12-02 19:14:58 +08005257#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005258/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005259 * Sets up the vmcs for emulated real mode.
5260 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005261static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005262{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005263#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005264 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005265#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005267
Avi Kivity6aa8b732006-12-10 02:21:36 -08005268 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005269 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5270 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005271
Abel Gordon4607c2d2013-04-18 14:35:55 +03005272 if (enable_shadow_vmcs) {
5273 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5274 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5275 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005276 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005277 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005278
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5280
Avi Kivity6aa8b732006-12-10 02:21:36 -08005281 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005282 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005283 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005284
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005285 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005286
Dan Williamsdfa169b2016-06-02 11:17:24 -07005287 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005288 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5289 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005290 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005291
Andrey Smetanind62caab2015-11-10 15:36:33 +03005292 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005293 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5294 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5295 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5296 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5297
5298 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005299
Li RongQing0bcf2612015-12-03 13:29:34 +08005300 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005301 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005302 }
5303
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005304 if (ple_gap) {
5305 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005306 vmx->ple_window = ple_window;
5307 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005308 }
5309
Xiao Guangrongc3707952011-07-12 03:28:04 +08005310 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5311 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005312 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5313
Avi Kivity9581d442010-10-19 16:46:55 +02005314 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5315 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005316 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005317#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005318 rdmsrl(MSR_FS_BASE, a);
5319 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5320 rdmsrl(MSR_GS_BASE, a);
5321 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5322#else
5323 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5324 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5325#endif
5326
Eddie Dong2cc51562007-05-21 07:28:09 +03005327 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5328 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005329 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03005330 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -04005331 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005332
Radim Krčmář74545702015-04-27 15:11:25 +02005333 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5334 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005335
Paolo Bonzini03916db2014-07-24 14:21:57 +02005336 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005337 u32 index = vmx_msr_index[i];
5338 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005339 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005340
5341 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5342 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005343 if (wrmsr_safe(index, data_low, data_high) < 0)
5344 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005345 vmx->guest_msrs[j].index = i;
5346 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005347 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005348 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005349 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005350
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01005351 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5352 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005353
5354 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005355
5356 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005357 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005358
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005359 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005360 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005361
Wanpeng Lif53cd632014-12-02 19:14:58 +08005362 if (vmx_xsaves_supported())
5363 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5364
Peter Feiner4e595162016-07-07 14:49:58 -07005365 if (enable_pml) {
5366 ASSERT(vmx->pml_pg);
5367 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5368 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5369 }
5370
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005371 return 0;
5372}
5373
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005374static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005375{
5376 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005377 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005378 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005379
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005380 vmx->rmode.vm86_active = 0;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005381 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005382
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005383 vmx->soft_vnmi_blocked = 0;
5384
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005385 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005386 kvm_set_cr8(vcpu, 0);
5387
5388 if (!init_event) {
5389 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5390 MSR_IA32_APICBASE_ENABLE;
5391 if (kvm_vcpu_is_reset_bsp(vcpu))
5392 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5393 apic_base_msr.host_initiated = true;
5394 kvm_set_apic_base(vcpu, &apic_base_msr);
5395 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005396
Avi Kivity2fb92db2011-04-27 19:42:18 +03005397 vmx_segment_cache_clear(vmx);
5398
Avi Kivity5706be02008-08-20 15:07:31 +03005399 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005400 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005401 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005402
5403 seg_setup(VCPU_SREG_DS);
5404 seg_setup(VCPU_SREG_ES);
5405 seg_setup(VCPU_SREG_FS);
5406 seg_setup(VCPU_SREG_GS);
5407 seg_setup(VCPU_SREG_SS);
5408
5409 vmcs_write16(GUEST_TR_SELECTOR, 0);
5410 vmcs_writel(GUEST_TR_BASE, 0);
5411 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5412 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5413
5414 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5415 vmcs_writel(GUEST_LDTR_BASE, 0);
5416 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5417 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5418
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005419 if (!init_event) {
5420 vmcs_write32(GUEST_SYSENTER_CS, 0);
5421 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5422 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5423 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5424 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005425
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005426 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005427 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005428
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005429 vmcs_writel(GUEST_GDTR_BASE, 0);
5430 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5431
5432 vmcs_writel(GUEST_IDTR_BASE, 0);
5433 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5434
Anthony Liguori443381a2010-12-06 10:53:38 -06005435 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005436 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005437 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005438
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005439 setup_msrs(vmx);
5440
Avi Kivity6aa8b732006-12-10 02:21:36 -08005441 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5442
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005443 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005444 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005445 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005446 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005447 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005448 vmcs_write32(TPR_THRESHOLD, 0);
5449 }
5450
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005451 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452
Andrey Smetanind62caab2015-11-10 15:36:33 +03005453 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005454 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5455
Sheng Yang2384d2b2008-01-17 15:14:33 +08005456 if (vmx->vpid != 0)
5457 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5458
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005459 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005460 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005461 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005462 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005463 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005464 vmx_fpu_activate(vcpu);
5465 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005466
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005467 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468}
5469
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005470/*
5471 * In nested virtualization, check if L1 asked to exit on external interrupts.
5472 * For most existing hypervisors, this will always return true.
5473 */
5474static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5475{
5476 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5477 PIN_BASED_EXT_INTR_MASK;
5478}
5479
Bandan Das77b0f5d2014-04-19 18:17:45 -04005480/*
5481 * In nested virtualization, check if L1 has set
5482 * VM_EXIT_ACK_INTR_ON_EXIT
5483 */
5484static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5485{
5486 return get_vmcs12(vcpu)->vm_exit_controls &
5487 VM_EXIT_ACK_INTR_ON_EXIT;
5488}
5489
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005490static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5491{
5492 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5493 PIN_BASED_NMI_EXITING;
5494}
5495
Jan Kiszkac9a79532014-03-07 20:03:15 +01005496static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005497{
5498 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005499
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005500 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5501 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5502 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5503}
5504
Jan Kiszkac9a79532014-03-07 20:03:15 +01005505static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005506{
5507 u32 cpu_based_vm_exec_control;
5508
Jan Kiszkac9a79532014-03-07 20:03:15 +01005509 if (!cpu_has_virtual_nmis() ||
5510 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5511 enable_irq_window(vcpu);
5512 return;
5513 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005514
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005515 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5516 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5517 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5518}
5519
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005520static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005521{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005522 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005523 uint32_t intr;
5524 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005525
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005526 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005527
Avi Kivityfa89a812008-09-01 15:57:51 +03005528 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005529 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005530 int inc_eip = 0;
5531 if (vcpu->arch.interrupt.soft)
5532 inc_eip = vcpu->arch.event_exit_inst_len;
5533 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005534 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005535 return;
5536 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005537 intr = irq | INTR_INFO_VALID_MASK;
5538 if (vcpu->arch.interrupt.soft) {
5539 intr |= INTR_TYPE_SOFT_INTR;
5540 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5541 vmx->vcpu.arch.event_exit_inst_len);
5542 } else
5543 intr |= INTR_TYPE_EXT_INTR;
5544 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005545}
5546
Sheng Yangf08864b2008-05-15 18:23:25 +08005547static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5548{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005549 struct vcpu_vmx *vmx = to_vmx(vcpu);
5550
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005551 if (!is_guest_mode(vcpu)) {
5552 if (!cpu_has_virtual_nmis()) {
5553 /*
5554 * Tracking the NMI-blocked state in software is built upon
5555 * finding the next open IRQ window. This, in turn, depends on
5556 * well-behaving guests: They have to keep IRQs disabled at
5557 * least as long as the NMI handler runs. Otherwise we may
5558 * cause NMI nesting, maybe breaking the guest. But as this is
5559 * highly unlikely, we can live with the residual risk.
5560 */
5561 vmx->soft_vnmi_blocked = 1;
5562 vmx->vnmi_blocked_time = 0;
5563 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005564
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005565 ++vcpu->stat.nmi_injections;
5566 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005567 }
5568
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005569 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005570 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005571 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005572 return;
5573 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005574
Sheng Yangf08864b2008-05-15 18:23:25 +08005575 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5576 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005577}
5578
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005579static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5580{
5581 if (!cpu_has_virtual_nmis())
5582 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005583 if (to_vmx(vcpu)->nmi_known_unmasked)
5584 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005585 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005586}
5587
5588static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5589{
5590 struct vcpu_vmx *vmx = to_vmx(vcpu);
5591
5592 if (!cpu_has_virtual_nmis()) {
5593 if (vmx->soft_vnmi_blocked != masked) {
5594 vmx->soft_vnmi_blocked = masked;
5595 vmx->vnmi_blocked_time = 0;
5596 }
5597 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005598 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005599 if (masked)
5600 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5601 GUEST_INTR_STATE_NMI);
5602 else
5603 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5604 GUEST_INTR_STATE_NMI);
5605 }
5606}
5607
Jan Kiszka2505dc92013-04-14 12:12:47 +02005608static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5609{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005610 if (to_vmx(vcpu)->nested.nested_run_pending)
5611 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005612
Jan Kiszka2505dc92013-04-14 12:12:47 +02005613 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5614 return 0;
5615
5616 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5617 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5618 | GUEST_INTR_STATE_NMI));
5619}
5620
Gleb Natapov78646122009-03-23 12:12:11 +02005621static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5622{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005623 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5624 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005625 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5626 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005627}
5628
Izik Eiduscbc94022007-10-25 00:29:55 +02005629static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5630{
5631 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005632
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005633 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5634 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005635 if (ret)
5636 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005637 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005638 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005639}
5640
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005641static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005642{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005643 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005644 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005645 /*
5646 * Update instruction length as we may reinject the exception
5647 * from user space while in guest debugging mode.
5648 */
5649 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5650 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005651 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005652 return false;
5653 /* fall through */
5654 case DB_VECTOR:
5655 if (vcpu->guest_debug &
5656 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5657 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005658 /* fall through */
5659 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005660 case OF_VECTOR:
5661 case BR_VECTOR:
5662 case UD_VECTOR:
5663 case DF_VECTOR:
5664 case SS_VECTOR:
5665 case GP_VECTOR:
5666 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005667 return true;
5668 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005669 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005670 return false;
5671}
5672
5673static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5674 int vec, u32 err_code)
5675{
5676 /*
5677 * Instruction with address size override prefix opcode 0x67
5678 * Cause the #SS fault with 0 error code in VM86 mode.
5679 */
5680 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5681 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5682 if (vcpu->arch.halt_request) {
5683 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005684 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005685 }
5686 return 1;
5687 }
5688 return 0;
5689 }
5690
5691 /*
5692 * Forward all other exceptions that are valid in real mode.
5693 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5694 * the required debugging infrastructure rework.
5695 */
5696 kvm_queue_exception(vcpu, vec);
5697 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005698}
5699
Andi Kleena0861c02009-06-08 17:37:09 +08005700/*
5701 * Trigger machine check on the host. We assume all the MSRs are already set up
5702 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5703 * We pass a fake environment to the machine check handler because we want
5704 * the guest to be always treated like user space, no matter what context
5705 * it used internally.
5706 */
5707static void kvm_machine_check(void)
5708{
5709#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5710 struct pt_regs regs = {
5711 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5712 .flags = X86_EFLAGS_IF,
5713 };
5714
5715 do_machine_check(&regs, 0);
5716#endif
5717}
5718
Avi Kivity851ba692009-08-24 11:10:17 +03005719static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005720{
5721 /* already handled by vcpu_run */
5722 return 1;
5723}
5724
Avi Kivity851ba692009-08-24 11:10:17 +03005725static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005726{
Avi Kivity1155f762007-11-22 11:30:47 +02005727 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005728 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005729 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005730 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005731 u32 vect_info;
5732 enum emulation_result er;
5733
Avi Kivity1155f762007-11-22 11:30:47 +02005734 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005735 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005736
Andi Kleena0861c02009-06-08 17:37:09 +08005737 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005738 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005739
Jim Mattson3f618a02016-12-12 11:01:37 -08005740 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005741 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005742
5743 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005744 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005745 return 1;
5746 }
5747
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005748 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005749 if (is_guest_mode(vcpu)) {
5750 kvm_queue_exception(vcpu, UD_VECTOR);
5751 return 1;
5752 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005753 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005754 if (er == EMULATE_USER_EXIT)
5755 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005756 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005757 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005758 return 1;
5759 }
5760
Avi Kivity6aa8b732006-12-10 02:21:36 -08005761 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005762 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005763 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005764
5765 /*
5766 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5767 * MMIO, it is better to report an internal error.
5768 * See the comments in vmx_handle_exit.
5769 */
5770 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5771 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5772 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5773 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005774 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005775 vcpu->run->internal.data[0] = vect_info;
5776 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005777 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005778 return 0;
5779 }
5780
Avi Kivity6aa8b732006-12-10 02:21:36 -08005781 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005782 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005783 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005784 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005785 trace_kvm_page_fault(cr2, error_code);
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02005786 vcpu->arch.l1tf_flush_l1d = true;
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005787
Gleb Natapov3298b752009-05-11 13:35:46 +03005788 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005789 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005790 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005791 }
5792
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005793 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005794
5795 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5796 return handle_rmode_exception(vcpu, ex_no, error_code);
5797
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005798 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005799 case AC_VECTOR:
5800 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5801 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005802 case DB_VECTOR:
5803 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5804 if (!(vcpu->guest_debug &
5805 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005806 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005807 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005808 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005809 skip_emulated_instruction(vcpu);
5810
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005811 kvm_queue_exception(vcpu, DB_VECTOR);
5812 return 1;
5813 }
5814 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5815 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5816 /* fall through */
5817 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005818 /*
5819 * Update instruction length as we may reinject #BP from
5820 * user space while in guest debugging mode. Reading it for
5821 * #DB as well causes no harm, it is not used in that case.
5822 */
5823 vmx->vcpu.arch.event_exit_inst_len =
5824 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005825 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005826 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005827 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5828 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005829 break;
5830 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005831 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5832 kvm_run->ex.exception = ex_no;
5833 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005834 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005835 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005836 return 0;
5837}
5838
Avi Kivity851ba692009-08-24 11:10:17 +03005839static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005840{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005841 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005842 return 1;
5843}
5844
Avi Kivity851ba692009-08-24 11:10:17 +03005845static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005846{
Avi Kivity851ba692009-08-24 11:10:17 +03005847 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005848 return 0;
5849}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005850
Avi Kivity851ba692009-08-24 11:10:17 +03005851static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005852{
He, Qingbfdaab02007-09-12 14:18:28 +08005853 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005854 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005855 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005856
He, Qingbfdaab02007-09-12 14:18:28 +08005857 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005858 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005859 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005860
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005861 ++vcpu->stat.io_exits;
5862
5863 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005864 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005865
5866 port = exit_qualification >> 16;
5867 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005868 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005869
5870 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005871}
5872
Ingo Molnar102d8322007-02-19 14:37:47 +02005873static void
5874vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5875{
5876 /*
5877 * Patch in the VMCALL instruction:
5878 */
5879 hypercall[0] = 0x0f;
5880 hypercall[1] = 0x01;
5881 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005882}
5883
Wincy Vanb9c237b2015-02-03 23:56:30 +08005884static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005885{
5886 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005887 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005888
Wincy Vanb9c237b2015-02-03 23:56:30 +08005889 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005890 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5891 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5892 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5893 return (val & always_on) == always_on;
5894}
5895
Guo Chao0fa06072012-06-28 15:16:19 +08005896/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005897static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5898{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005899 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005900 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5901 unsigned long orig_val = val;
5902
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005903 /*
5904 * We get here when L2 changed cr0 in a way that did not change
5905 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005906 * but did change L0 shadowed bits. So we first calculate the
5907 * effective cr0 value that L1 would like to write into the
5908 * hardware. It consists of the L2-owned bits from the new
5909 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005910 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005911 val = (val & ~vmcs12->cr0_guest_host_mask) |
5912 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5913
Wincy Vanb9c237b2015-02-03 23:56:30 +08005914 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005915 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005916
5917 if (kvm_set_cr0(vcpu, val))
5918 return 1;
5919 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005920 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005921 } else {
5922 if (to_vmx(vcpu)->nested.vmxon &&
5923 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5924 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005925 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005926 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005927}
5928
5929static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5930{
5931 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005932 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5933 unsigned long orig_val = val;
5934
5935 /* analogously to handle_set_cr0 */
5936 val = (val & ~vmcs12->cr4_guest_host_mask) |
5937 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5938 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005939 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005940 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005941 return 0;
5942 } else
5943 return kvm_set_cr4(vcpu, val);
5944}
5945
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005946/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005947static void handle_clts(struct kvm_vcpu *vcpu)
5948{
5949 if (is_guest_mode(vcpu)) {
5950 /*
5951 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5952 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5953 * just pretend it's off (also in arch.cr0 for fpu_activate).
5954 */
5955 vmcs_writel(CR0_READ_SHADOW,
5956 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5957 vcpu->arch.cr0 &= ~X86_CR0_TS;
5958 } else
5959 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5960}
5961
Avi Kivity851ba692009-08-24 11:10:17 +03005962static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005963{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005964 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005965 int cr;
5966 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005967 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005968
He, Qingbfdaab02007-09-12 14:18:28 +08005969 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005970 cr = exit_qualification & 15;
5971 reg = (exit_qualification >> 8) & 15;
5972 switch ((exit_qualification >> 4) & 3) {
5973 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005974 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005975 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005976 switch (cr) {
5977 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005978 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005979 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005980 return 1;
5981 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005982 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005983 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005984 return 1;
5985 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005986 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005987 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005988 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005989 case 8: {
5990 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005991 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005992 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005993 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005994 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005995 return 1;
5996 if (cr8_prev <= cr8)
5997 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005998 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005999 return 0;
6000 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02006001 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006002 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03006003 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03006004 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02006005 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03006006 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02006007 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03006008 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006009 case 1: /*mov from cr*/
6010 switch (cr) {
6011 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02006012 val = kvm_read_cr3(vcpu);
6013 kvm_register_write(vcpu, reg, val);
6014 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006015 skip_emulated_instruction(vcpu);
6016 return 1;
6017 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006018 val = kvm_get_cr8(vcpu);
6019 kvm_register_write(vcpu, reg, val);
6020 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006021 skip_emulated_instruction(vcpu);
6022 return 1;
6023 }
6024 break;
6025 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02006026 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02006027 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02006028 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006029
6030 skip_emulated_instruction(vcpu);
6031 return 1;
6032 default:
6033 break;
6034 }
Avi Kivity851ba692009-08-24 11:10:17 +03006035 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03006036 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08006037 (int)(exit_qualification >> 4) & 3, cr);
6038 return 0;
6039}
6040
Avi Kivity851ba692009-08-24 11:10:17 +03006041static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006042{
He, Qingbfdaab02007-09-12 14:18:28 +08006043 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006044 int dr, dr7, reg;
6045
6046 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6047 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
6048
6049 /* First, if DR does not exist, trigger UD */
6050 if (!kvm_require_dr(vcpu, dr))
6051 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006052
Jan Kiszkaf2483412010-01-20 18:20:20 +01006053 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03006054 if (!kvm_require_cpl(vcpu, 0))
6055 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006056 dr7 = vmcs_readl(GUEST_DR7);
6057 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006058 /*
6059 * As the vm-exit takes precedence over the debug trap, we
6060 * need to emulate the latter, either for the host or the
6061 * guest debugging itself.
6062 */
6063 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006064 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006065 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006066 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006067 vcpu->run->debug.arch.exception = DB_VECTOR;
6068 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006069 return 0;
6070 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006071 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006072 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006073 kvm_queue_exception(vcpu, DB_VECTOR);
6074 return 1;
6075 }
6076 }
6077
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006078 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006079 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6080 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006081
6082 /*
6083 * No more DR vmexits; force a reload of the debug registers
6084 * and reenter on this instruction. The next vmexit will
6085 * retrieve the full state of the debug registers.
6086 */
6087 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6088 return 1;
6089 }
6090
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006091 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6092 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006093 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006094
6095 if (kvm_get_dr(vcpu, dr, &val))
6096 return 1;
6097 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006098 } else
Nadav Amit57773922014-06-18 17:19:23 +03006099 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006100 return 1;
6101
Avi Kivity6aa8b732006-12-10 02:21:36 -08006102 skip_emulated_instruction(vcpu);
6103 return 1;
6104}
6105
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006106static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6107{
6108 return vcpu->arch.dr6;
6109}
6110
6111static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6112{
6113}
6114
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006115static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6116{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006117 get_debugreg(vcpu->arch.db[0], 0);
6118 get_debugreg(vcpu->arch.db[1], 1);
6119 get_debugreg(vcpu->arch.db[2], 2);
6120 get_debugreg(vcpu->arch.db[3], 3);
6121 get_debugreg(vcpu->arch.dr6, 6);
6122 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6123
6124 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006125 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006126}
6127
Gleb Natapov020df072010-04-13 10:05:23 +03006128static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6129{
6130 vmcs_writel(GUEST_DR7, val);
6131}
6132
Avi Kivity851ba692009-08-24 11:10:17 +03006133static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006134{
Avi Kivity06465c52007-02-28 20:46:53 +02006135 kvm_emulate_cpuid(vcpu);
6136 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006137}
6138
Avi Kivity851ba692009-08-24 11:10:17 +03006139static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006140{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006141 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006142 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006143
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006144 msr_info.index = ecx;
6145 msr_info.host_initiated = false;
6146 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006147 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006148 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006149 return 1;
6150 }
6151
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006152 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006153
Avi Kivity6aa8b732006-12-10 02:21:36 -08006154 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006155 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6156 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006157 skip_emulated_instruction(vcpu);
6158 return 1;
6159}
6160
Avi Kivity851ba692009-08-24 11:10:17 +03006161static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006162{
Will Auld8fe8ab42012-11-29 12:42:12 -08006163 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006164 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6165 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6166 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006167
Will Auld8fe8ab42012-11-29 12:42:12 -08006168 msr.data = data;
6169 msr.index = ecx;
6170 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006171 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006172 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006173 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006174 return 1;
6175 }
6176
Avi Kivity59200272010-01-25 19:47:02 +02006177 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006178 skip_emulated_instruction(vcpu);
6179 return 1;
6180}
6181
Avi Kivity851ba692009-08-24 11:10:17 +03006182static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006183{
Avi Kivity3842d132010-07-27 12:30:24 +03006184 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006185 return 1;
6186}
6187
Avi Kivity851ba692009-08-24 11:10:17 +03006188static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006189{
Eddie Dong85f455f2007-07-06 12:20:49 +03006190 u32 cpu_based_vm_exec_control;
6191
6192 /* clear pending irq */
6193 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6194 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6195 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006196
Avi Kivity3842d132010-07-27 12:30:24 +03006197 kvm_make_request(KVM_REQ_EVENT, vcpu);
6198
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006199 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006200 return 1;
6201}
6202
Avi Kivity851ba692009-08-24 11:10:17 +03006203static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006204{
Avi Kivityd3bef152007-06-05 15:53:05 +03006205 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006206}
6207
Avi Kivity851ba692009-08-24 11:10:17 +03006208static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006209{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006210 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006211}
6212
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006213static int handle_invd(struct kvm_vcpu *vcpu)
6214{
Andre Przywara51d8b662010-12-21 11:12:02 +01006215 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006216}
6217
Avi Kivity851ba692009-08-24 11:10:17 +03006218static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006219{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006220 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006221
6222 kvm_mmu_invlpg(vcpu, exit_qualification);
6223 skip_emulated_instruction(vcpu);
6224 return 1;
6225}
6226
Avi Kivityfee84b02011-11-10 14:57:25 +02006227static int handle_rdpmc(struct kvm_vcpu *vcpu)
6228{
6229 int err;
6230
6231 err = kvm_rdpmc(vcpu);
6232 kvm_complete_insn_gp(vcpu, err);
6233
6234 return 1;
6235}
6236
Avi Kivity851ba692009-08-24 11:10:17 +03006237static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006238{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006239 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006240 return 1;
6241}
6242
Dexuan Cui2acf9232010-06-10 11:27:12 +08006243static int handle_xsetbv(struct kvm_vcpu *vcpu)
6244{
6245 u64 new_bv = kvm_read_edx_eax(vcpu);
6246 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6247
6248 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6249 skip_emulated_instruction(vcpu);
6250 return 1;
6251}
6252
Wanpeng Lif53cd632014-12-02 19:14:58 +08006253static int handle_xsaves(struct kvm_vcpu *vcpu)
6254{
6255 skip_emulated_instruction(vcpu);
6256 WARN(1, "this should never happen\n");
6257 return 1;
6258}
6259
6260static int handle_xrstors(struct kvm_vcpu *vcpu)
6261{
6262 skip_emulated_instruction(vcpu);
6263 WARN(1, "this should never happen\n");
6264 return 1;
6265}
6266
Avi Kivity851ba692009-08-24 11:10:17 +03006267static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006268{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006269 if (likely(fasteoi)) {
6270 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6271 int access_type, offset;
6272
6273 access_type = exit_qualification & APIC_ACCESS_TYPE;
6274 offset = exit_qualification & APIC_ACCESS_OFFSET;
6275 /*
6276 * Sane guest uses MOV to write EOI, with written value
6277 * not cared. So make a short-circuit here by avoiding
6278 * heavy instruction emulation.
6279 */
6280 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6281 (offset == APIC_EOI)) {
6282 kvm_lapic_set_eoi(vcpu);
6283 skip_emulated_instruction(vcpu);
6284 return 1;
6285 }
6286 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006287 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006288}
6289
Yang Zhangc7c9c562013-01-25 10:18:51 +08006290static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6291{
6292 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6293 int vector = exit_qualification & 0xff;
6294
6295 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6296 kvm_apic_set_eoi_accelerated(vcpu, vector);
6297 return 1;
6298}
6299
Yang Zhang83d4c282013-01-25 10:18:49 +08006300static int handle_apic_write(struct kvm_vcpu *vcpu)
6301{
6302 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6303 u32 offset = exit_qualification & 0xfff;
6304
6305 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6306 kvm_apic_write_nodecode(vcpu, offset);
6307 return 1;
6308}
6309
Avi Kivity851ba692009-08-24 11:10:17 +03006310static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006311{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006312 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006313 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006314 bool has_error_code = false;
6315 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006316 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006317 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006318
6319 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006320 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006321 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006322
6323 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6324
6325 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006326 if (reason == TASK_SWITCH_GATE && idt_v) {
6327 switch (type) {
6328 case INTR_TYPE_NMI_INTR:
6329 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006330 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006331 break;
6332 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006333 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006334 kvm_clear_interrupt_queue(vcpu);
6335 break;
6336 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006337 if (vmx->idt_vectoring_info &
6338 VECTORING_INFO_DELIVER_CODE_MASK) {
6339 has_error_code = true;
6340 error_code =
6341 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6342 }
6343 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006344 case INTR_TYPE_SOFT_EXCEPTION:
6345 kvm_clear_exception_queue(vcpu);
6346 break;
6347 default:
6348 break;
6349 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006350 }
Izik Eidus37817f22008-03-24 23:14:53 +02006351 tss_selector = exit_qualification;
6352
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006353 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6354 type != INTR_TYPE_EXT_INTR &&
6355 type != INTR_TYPE_NMI_INTR))
6356 skip_emulated_instruction(vcpu);
6357
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006358 if (kvm_task_switch(vcpu, tss_selector,
6359 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6360 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006361 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6362 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6363 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006364 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006365 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006366
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006367 /*
6368 * TODO: What about debug traps on tss switch?
6369 * Are we supposed to inject them and update dr6?
6370 */
6371
6372 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006373}
6374
Avi Kivity851ba692009-08-24 11:10:17 +03006375static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006376{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006377 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006378 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006379 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006380 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006381
Sheng Yangf9c617f2009-03-25 10:08:52 +08006382 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006383
Sheng Yang14394422008-04-28 12:24:45 +08006384 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006385 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006386 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6387 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6388 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006389 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006390 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6391 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006392 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6393 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006394 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006395 }
6396
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006397 /*
6398 * EPT violation happened while executing iret from NMI,
6399 * "blocked by NMI" bit has to be set before next VM entry.
6400 * There are errata that may cause this bit to not be set:
6401 * AAK134, BY25.
6402 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006403 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6404 cpu_has_virtual_nmis() &&
6405 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006406 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6407
Sheng Yang14394422008-04-28 12:24:45 +08006408 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006409 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006410
Bandan Dasd95c5562016-07-12 18:18:51 -04006411 /* it is a read fault? */
6412 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6413 /* it is a write fault? */
6414 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006415 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006416 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006417 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006418 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006419
Yang Zhang25d92082013-08-06 12:00:32 +03006420 vcpu->arch.exit_qualification = exit_qualification;
6421
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006422 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006423}
6424
Avi Kivity851ba692009-08-24 11:10:17 +03006425static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006426{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006427 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006428 gpa_t gpa;
6429
6430 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006431 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006432 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006433 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006434 return 1;
6435 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006436
Paolo Bonzini450869d2015-11-04 13:41:21 +01006437 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006438 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006439 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6440 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006441
6442 if (unlikely(ret == RET_MMIO_PF_INVALID))
6443 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6444
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006445 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006446 return 1;
6447
6448 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006449 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006450
Avi Kivity851ba692009-08-24 11:10:17 +03006451 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6452 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006453
6454 return 0;
6455}
6456
Avi Kivity851ba692009-08-24 11:10:17 +03006457static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006458{
6459 u32 cpu_based_vm_exec_control;
6460
6461 /* clear pending NMI */
6462 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6463 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6464 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6465 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006466 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006467
6468 return 1;
6469}
6470
Mohammed Gamal80ced182009-09-01 12:48:18 +02006471static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006472{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006473 struct vcpu_vmx *vmx = to_vmx(vcpu);
6474 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006475 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006476 u32 cpu_exec_ctrl;
6477 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006478 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006479
6480 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6481 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006482
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006483 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006484 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006485 return handle_interrupt_window(&vmx->vcpu);
6486
Avi Kivityde87dcd2012-06-12 20:21:38 +03006487 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6488 return 1;
6489
Liran Alon114de9b2017-11-05 16:56:34 +02006490 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006491
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006492 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006493 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006494 ret = 0;
6495 goto out;
6496 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006497
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006498 if (err != EMULATE_DONE)
6499 goto emulation_error;
6500
6501 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6502 vcpu->arch.exception.pending)
6503 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006504
Gleb Natapov8d76c492013-05-08 18:38:44 +03006505 if (vcpu->arch.halt_request) {
6506 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006507 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006508 goto out;
6509 }
6510
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006511 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006512 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006513 if (need_resched())
6514 schedule();
6515 }
6516
Mohammed Gamal80ced182009-09-01 12:48:18 +02006517out:
6518 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006519
6520emulation_error:
6521 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6522 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6523 vcpu->run->internal.ndata = 0;
6524 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006525}
6526
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006527static int __grow_ple_window(int val)
6528{
6529 if (ple_window_grow < 1)
6530 return ple_window;
6531
6532 val = min(val, ple_window_actual_max);
6533
6534 if (ple_window_grow < ple_window)
6535 val *= ple_window_grow;
6536 else
6537 val += ple_window_grow;
6538
6539 return val;
6540}
6541
6542static int __shrink_ple_window(int val, int modifier, int minimum)
6543{
6544 if (modifier < 1)
6545 return ple_window;
6546
6547 if (modifier < ple_window)
6548 val /= modifier;
6549 else
6550 val -= modifier;
6551
6552 return max(val, minimum);
6553}
6554
6555static void grow_ple_window(struct kvm_vcpu *vcpu)
6556{
6557 struct vcpu_vmx *vmx = to_vmx(vcpu);
6558 int old = vmx->ple_window;
6559
6560 vmx->ple_window = __grow_ple_window(old);
6561
6562 if (vmx->ple_window != old)
6563 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006564
6565 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006566}
6567
6568static void shrink_ple_window(struct kvm_vcpu *vcpu)
6569{
6570 struct vcpu_vmx *vmx = to_vmx(vcpu);
6571 int old = vmx->ple_window;
6572
6573 vmx->ple_window = __shrink_ple_window(old,
6574 ple_window_shrink, ple_window);
6575
6576 if (vmx->ple_window != old)
6577 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006578
6579 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006580}
6581
6582/*
6583 * ple_window_actual_max is computed to be one grow_ple_window() below
6584 * ple_window_max. (See __grow_ple_window for the reason.)
6585 * This prevents overflows, because ple_window_max is int.
6586 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6587 * this process.
6588 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6589 */
6590static void update_ple_window_actual_max(void)
6591{
6592 ple_window_actual_max =
6593 __shrink_ple_window(max(ple_window_max, ple_window),
6594 ple_window_grow, INT_MIN);
6595}
6596
Feng Wubf9f6ac2015-09-18 22:29:55 +08006597/*
6598 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6599 */
6600static void wakeup_handler(void)
6601{
6602 struct kvm_vcpu *vcpu;
6603 int cpu = smp_processor_id();
6604
6605 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6606 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6607 blocked_vcpu_list) {
6608 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6609
6610 if (pi_test_on(pi_desc) == 1)
6611 kvm_vcpu_kick(vcpu);
6612 }
6613 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6614}
6615
Tiejun Chenf2c76482014-10-28 10:14:47 +08006616static __init int hardware_setup(void)
6617{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006618 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006619
6620 rdmsrl_safe(MSR_EFER, &host_efer);
6621
6622 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6623 kvm_define_shared_msr(i, vmx_msr_index[i]);
6624
6625 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6626 if (!vmx_io_bitmap_a)
6627 return r;
6628
6629 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6630 if (!vmx_io_bitmap_b)
6631 goto out;
6632
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006633 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6634 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006635 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006636
6637 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6638 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006639 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006640
6641 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6642 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6643
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006644 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006645
6646 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6647
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006648 if (setup_vmcs_config(&vmcs_config) < 0) {
6649 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006650 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006651 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006652
6653 if (boot_cpu_has(X86_FEATURE_NX))
6654 kvm_enable_efer_bits(EFER_NX);
6655
Wanpeng Li2df19692017-03-23 05:30:08 -07006656 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6657 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006658 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006659
Tiejun Chenf2c76482014-10-28 10:14:47 +08006660 if (!cpu_has_vmx_shadow_vmcs())
6661 enable_shadow_vmcs = 0;
6662 if (enable_shadow_vmcs)
6663 init_vmcs_shadow_fields();
6664
6665 if (!cpu_has_vmx_ept() ||
6666 !cpu_has_vmx_ept_4levels()) {
6667 enable_ept = 0;
6668 enable_unrestricted_guest = 0;
6669 enable_ept_ad_bits = 0;
6670 }
6671
6672 if (!cpu_has_vmx_ept_ad_bits())
6673 enable_ept_ad_bits = 0;
6674
6675 if (!cpu_has_vmx_unrestricted_guest())
6676 enable_unrestricted_guest = 0;
6677
Paolo Bonziniad15a292015-01-30 16:18:49 +01006678 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006679 flexpriority_enabled = 0;
6680
Paolo Bonziniad15a292015-01-30 16:18:49 +01006681 /*
6682 * set_apic_access_page_addr() is used to reload apic access
6683 * page upon invalidation. No need to do anything if not
6684 * using the APIC_ACCESS_ADDR VMCS field.
6685 */
6686 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006687 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006688
6689 if (!cpu_has_vmx_tpr_shadow())
6690 kvm_x86_ops->update_cr8_intercept = NULL;
6691
6692 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6693 kvm_disable_largepages();
6694
6695 if (!cpu_has_vmx_ple())
6696 ple_gap = 0;
6697
6698 if (!cpu_has_vmx_apicv())
6699 enable_apicv = 0;
6700
Haozhong Zhang64903d62015-10-20 15:39:09 +08006701 if (cpu_has_vmx_tsc_scaling()) {
6702 kvm_has_tsc_control = true;
6703 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6704 kvm_tsc_scaling_ratio_frac_bits = 48;
6705 }
6706
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006707 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6708
Tiejun Chenbaa03522014-12-23 16:21:11 +08006709 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006710 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006711 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6712 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006713 0ull, VMX_EPT_EXECUTABLE_MASK,
6714 cpu_has_vmx_ept_execute_only() ?
6715 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006716 ept_set_mmio_spte_mask();
6717 kvm_enable_tdp();
6718 } else
6719 kvm_disable_tdp();
6720
6721 update_ple_window_actual_max();
6722
Kai Huang843e4332015-01-28 10:54:28 +08006723 /*
6724 * Only enable PML when hardware supports PML feature, and both EPT
6725 * and EPT A/D bit features are enabled -- PML depends on them to work.
6726 */
6727 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6728 enable_pml = 0;
6729
6730 if (!enable_pml) {
6731 kvm_x86_ops->slot_enable_log_dirty = NULL;
6732 kvm_x86_ops->slot_disable_log_dirty = NULL;
6733 kvm_x86_ops->flush_log_dirty = NULL;
6734 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6735 }
6736
Yunhong Jiang64672c92016-06-13 14:19:59 -07006737 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6738 u64 vmx_msr;
6739
6740 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6741 cpu_preemption_timer_multi =
6742 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6743 } else {
6744 kvm_x86_ops->set_hv_timer = NULL;
6745 kvm_x86_ops->cancel_hv_timer = NULL;
6746 }
6747
Feng Wubf9f6ac2015-09-18 22:29:55 +08006748 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6749
Ashok Rajc45dcc72016-06-22 14:59:56 +08006750 kvm_mce_cap_supported |= MCG_LMCE_P;
6751
Tiejun Chenf2c76482014-10-28 10:14:47 +08006752 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006753
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006754out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006755 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006756out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006757 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006758out1:
6759 free_page((unsigned long)vmx_io_bitmap_b);
6760out:
6761 free_page((unsigned long)vmx_io_bitmap_a);
6762
6763 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006764}
6765
6766static __exit void hardware_unsetup(void)
6767{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006768 free_page((unsigned long)vmx_io_bitmap_b);
6769 free_page((unsigned long)vmx_io_bitmap_a);
6770 free_page((unsigned long)vmx_vmwrite_bitmap);
6771 free_page((unsigned long)vmx_vmread_bitmap);
6772
Tiejun Chenf2c76482014-10-28 10:14:47 +08006773 free_kvm_area();
6774}
6775
Avi Kivity6aa8b732006-12-10 02:21:36 -08006776/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006777 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6778 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6779 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006780static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006781{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006782 if (ple_gap)
6783 grow_ple_window(vcpu);
6784
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006785 skip_emulated_instruction(vcpu);
6786 kvm_vcpu_on_spin(vcpu);
6787
6788 return 1;
6789}
6790
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006791static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006792{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006793 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006794 return 1;
6795}
6796
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006797static int handle_mwait(struct kvm_vcpu *vcpu)
6798{
6799 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6800 return handle_nop(vcpu);
6801}
6802
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006803static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6804{
6805 return 1;
6806}
6807
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006808static int handle_monitor(struct kvm_vcpu *vcpu)
6809{
6810 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6811 return handle_nop(vcpu);
6812}
6813
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006814/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006815 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6816 * set the success or error code of an emulated VMX instruction, as specified
6817 * by Vol 2B, VMX Instruction Reference, "Conventions".
6818 */
6819static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6820{
6821 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6822 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6823 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6824}
6825
6826static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6827{
6828 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6829 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6830 X86_EFLAGS_SF | X86_EFLAGS_OF))
6831 | X86_EFLAGS_CF);
6832}
6833
Abel Gordon145c28d2013-04-18 14:36:55 +03006834static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006835 u32 vm_instruction_error)
6836{
6837 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6838 /*
6839 * failValid writes the error number to the current VMCS, which
6840 * can't be done there isn't a current VMCS.
6841 */
6842 nested_vmx_failInvalid(vcpu);
6843 return;
6844 }
6845 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6846 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6847 X86_EFLAGS_SF | X86_EFLAGS_OF))
6848 | X86_EFLAGS_ZF);
6849 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6850 /*
6851 * We don't need to force a shadow sync because
6852 * VM_INSTRUCTION_ERROR is not shadowed
6853 */
6854}
Abel Gordon145c28d2013-04-18 14:36:55 +03006855
Wincy Vanff651cb2014-12-11 08:52:58 +03006856static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6857{
6858 /* TODO: not to reset guest simply here. */
6859 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006860 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006861}
6862
Jan Kiszkaf4124502014-03-07 20:03:13 +01006863static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6864{
6865 struct vcpu_vmx *vmx =
6866 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6867
6868 vmx->nested.preemption_timer_expired = true;
6869 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6870 kvm_vcpu_kick(&vmx->vcpu);
6871
6872 return HRTIMER_NORESTART;
6873}
6874
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006875/*
Bandan Das19677e32014-05-06 02:19:15 -04006876 * Decode the memory-address operand of a vmx instruction, as recorded on an
6877 * exit caused by such an instruction (run by a guest hypervisor).
6878 * On success, returns 0. When the operand is invalid, returns 1 and throws
6879 * #UD or #GP.
6880 */
6881static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6882 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006883 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006884{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006885 gva_t off;
6886 bool exn;
6887 struct kvm_segment s;
6888
Bandan Das19677e32014-05-06 02:19:15 -04006889 /*
6890 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6891 * Execution", on an exit, vmx_instruction_info holds most of the
6892 * addressing components of the operand. Only the displacement part
6893 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6894 * For how an actual address is calculated from all these components,
6895 * refer to Vol. 1, "Operand Addressing".
6896 */
6897 int scaling = vmx_instruction_info & 3;
6898 int addr_size = (vmx_instruction_info >> 7) & 7;
6899 bool is_reg = vmx_instruction_info & (1u << 10);
6900 int seg_reg = (vmx_instruction_info >> 15) & 7;
6901 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6902 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6903 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6904 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6905
6906 if (is_reg) {
6907 kvm_queue_exception(vcpu, UD_VECTOR);
6908 return 1;
6909 }
6910
6911 /* Addr = segment_base + offset */
6912 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006913 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006914 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006915 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006916 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006917 off += kvm_register_read(vcpu, index_reg)<<scaling;
6918 vmx_get_segment(vcpu, &s, seg_reg);
6919 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006920
6921 if (addr_size == 1) /* 32 bit */
6922 *ret &= 0xffffffff;
6923
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006924 /* Checks for #GP/#SS exceptions. */
6925 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006926 if (is_long_mode(vcpu)) {
6927 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6928 * non-canonical form. This is the only check on the memory
6929 * destination for long mode!
6930 */
6931 exn = is_noncanonical_address(*ret);
6932 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006933 /* Protected mode: apply checks for segment validity in the
6934 * following order:
6935 * - segment type check (#GP(0) may be thrown)
6936 * - usability check (#GP(0)/#SS(0))
6937 * - limit check (#GP(0)/#SS(0))
6938 */
6939 if (wr)
6940 /* #GP(0) if the destination operand is located in a
6941 * read-only data segment or any code segment.
6942 */
6943 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6944 else
6945 /* #GP(0) if the source operand is located in an
6946 * execute-only code segment
6947 */
6948 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006949 if (exn) {
6950 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6951 return 1;
6952 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006953 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6954 */
6955 exn = (s.unusable != 0);
6956 /* Protected mode: #GP(0)/#SS(0) if the memory
6957 * operand is outside the segment limit.
6958 */
6959 exn = exn || (off + sizeof(u64) > s.limit);
6960 }
6961 if (exn) {
6962 kvm_queue_exception_e(vcpu,
6963 seg_reg == VCPU_SREG_SS ?
6964 SS_VECTOR : GP_VECTOR,
6965 0);
6966 return 1;
6967 }
6968
Bandan Das19677e32014-05-06 02:19:15 -04006969 return 0;
6970}
6971
6972/*
Bandan Das3573e222014-05-06 02:19:16 -04006973 * This function performs the various checks including
6974 * - if it's 4KB aligned
6975 * - No bits beyond the physical address width are set
6976 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006977 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006978 */
Bandan Das4291b582014-05-06 02:19:18 -04006979static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6980 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006981{
6982 gva_t gva;
6983 gpa_t vmptr;
6984 struct x86_exception e;
6985 struct page *page;
6986 struct vcpu_vmx *vmx = to_vmx(vcpu);
6987 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6988
6989 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006990 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006991 return 1;
6992
Paolo Bonzini838b0e92018-06-06 17:37:49 +02006993 if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04006994 kvm_inject_page_fault(vcpu, &e);
6995 return 1;
6996 }
6997
6998 switch (exit_reason) {
6999 case EXIT_REASON_VMON:
7000 /*
7001 * SDM 3: 24.11.5
7002 * The first 4 bytes of VMXON region contain the supported
7003 * VMCS revision identifier
7004 *
7005 * Note - IA32_VMX_BASIC[48] will never be 1
7006 * for the nested case;
7007 * which replaces physical address width with 32
7008 *
7009 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007010 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04007011 nested_vmx_failInvalid(vcpu);
7012 skip_emulated_instruction(vcpu);
7013 return 1;
7014 }
7015
7016 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007017 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04007018 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007019 skip_emulated_instruction(vcpu);
7020 return 1;
7021 }
7022 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04007023 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007024 nested_release_page_clean(page);
7025 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04007026 skip_emulated_instruction(vcpu);
7027 return 1;
7028 }
7029 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01007030 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04007031 vmx->nested.vmxon_ptr = vmptr;
7032 break;
Bandan Das4291b582014-05-06 02:19:18 -04007033 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007034 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007035 nested_vmx_failValid(vcpu,
7036 VMXERR_VMCLEAR_INVALID_ADDRESS);
7037 skip_emulated_instruction(vcpu);
7038 return 1;
7039 }
Bandan Das3573e222014-05-06 02:19:16 -04007040
Bandan Das4291b582014-05-06 02:19:18 -04007041 if (vmptr == vmx->nested.vmxon_ptr) {
7042 nested_vmx_failValid(vcpu,
7043 VMXERR_VMCLEAR_VMXON_POINTER);
7044 skip_emulated_instruction(vcpu);
7045 return 1;
7046 }
7047 break;
7048 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02007049 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04007050 nested_vmx_failValid(vcpu,
7051 VMXERR_VMPTRLD_INVALID_ADDRESS);
7052 skip_emulated_instruction(vcpu);
7053 return 1;
7054 }
7055
7056 if (vmptr == vmx->nested.vmxon_ptr) {
7057 nested_vmx_failValid(vcpu,
7058 VMXERR_VMCLEAR_VMXON_POINTER);
7059 skip_emulated_instruction(vcpu);
7060 return 1;
7061 }
7062 break;
Bandan Das3573e222014-05-06 02:19:16 -04007063 default:
7064 return 1; /* shouldn't happen */
7065 }
7066
Bandan Das4291b582014-05-06 02:19:18 -04007067 if (vmpointer)
7068 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007069 return 0;
7070}
7071
7072/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007073 * Emulate the VMXON instruction.
7074 * Currently, we just remember that VMX is active, and do not save or even
7075 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7076 * do not currently need to store anything in that guest-allocated memory
7077 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7078 * argument is different from the VMXON pointer (which the spec says they do).
7079 */
7080static int handle_vmon(struct kvm_vcpu *vcpu)
7081{
7082 struct kvm_segment cs;
7083 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007084 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007085 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7086 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007087 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007088
7089 /* The Intel VMX Instruction Reference lists a bunch of bits that
7090 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7091 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7092 * Otherwise, we should fail with #UD. We test these now:
7093 */
7094 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7095 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7096 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7097 kvm_queue_exception(vcpu, UD_VECTOR);
7098 return 1;
7099 }
7100
7101 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7102 if (is_long_mode(vcpu) && !cs.l) {
7103 kvm_queue_exception(vcpu, UD_VECTOR);
7104 return 1;
7105 }
7106
7107 if (vmx_get_cpl(vcpu)) {
7108 kvm_inject_gp(vcpu, 0);
7109 return 1;
7110 }
Bandan Das3573e222014-05-06 02:19:16 -04007111
Bandan Das4291b582014-05-06 02:19:18 -04007112 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007113 return 1;
7114
Abel Gordon145c28d2013-04-18 14:36:55 +03007115 if (vmx->nested.vmxon) {
7116 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7117 skip_emulated_instruction(vcpu);
7118 return 1;
7119 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007120
Haozhong Zhang3b840802016-06-22 14:59:54 +08007121 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007122 != VMXON_NEEDED_FEATURES) {
7123 kvm_inject_gp(vcpu, 0);
7124 return 1;
7125 }
7126
Paolo Bonziniff546f92018-01-11 12:16:15 +01007127 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7128 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007129 goto out_vmcs02;
Jim Mattson46e24df2017-11-27 17:22:25 -06007130
David Matlack4f2777b2016-07-13 17:16:37 -07007131 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7132 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007133 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007134
Abel Gordon8de48832013-04-18 14:37:25 +03007135 if (enable_shadow_vmcs) {
7136 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007137 if (!shadow_vmcs)
7138 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007139 /* mark vmcs as shadow */
7140 shadow_vmcs->revision_id |= (1u << 31);
7141 /* init shadow vmcs */
7142 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007143 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007144 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007145
Jan Kiszkaf4124502014-03-07 20:03:13 +01007146 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007147 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007148 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7149
Roman Kagan020a90f2018-07-19 21:59:07 +03007150 vmx->nested.vpid02 = allocate_vpid();
7151
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007152 vmx->nested.vmxon = true;
7153
7154 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007155 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007156 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007157
7158out_shadow_vmcs:
7159 kfree(vmx->nested.cached_vmcs12);
7160
7161out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007162 free_loaded_vmcs(&vmx->nested.vmcs02);
7163
7164out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007165 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007166}
7167
7168/*
7169 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7170 * for running VMX instructions (except VMXON, whose prerequisites are
7171 * slightly different). It also specifies what exception to inject otherwise.
7172 */
7173static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7174{
7175 struct kvm_segment cs;
7176 struct vcpu_vmx *vmx = to_vmx(vcpu);
7177
7178 if (!vmx->nested.vmxon) {
7179 kvm_queue_exception(vcpu, UD_VECTOR);
7180 return 0;
7181 }
7182
7183 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7184 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7185 (is_long_mode(vcpu) && !cs.l)) {
7186 kvm_queue_exception(vcpu, UD_VECTOR);
7187 return 0;
7188 }
7189
7190 if (vmx_get_cpl(vcpu)) {
7191 kvm_inject_gp(vcpu, 0);
7192 return 0;
7193 }
7194
7195 return 1;
7196}
7197
Abel Gordone7953d72013-04-18 14:37:55 +03007198static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7199{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007200 if (vmx->nested.current_vmptr == -1ull)
7201 return;
7202
7203 /* current_vmptr and current_vmcs12 are always set/reset together */
7204 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7205 return;
7206
Abel Gordon012f83c2013-04-18 14:39:25 +03007207 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007208 /* copy to memory all shadowed fields in case
7209 they were modified */
7210 copy_shadow_to_vmcs12(vmx);
7211 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007212 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7213 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007214 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007215 }
Wincy Van705699a2015-02-03 23:58:17 +08007216 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007217
7218 /* Flush VMCS12 to guest memory */
7219 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7220 VMCS12_SIZE);
7221
Abel Gordone7953d72013-04-18 14:37:55 +03007222 kunmap(vmx->nested.current_vmcs12_page);
7223 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007224 vmx->nested.current_vmptr = -1ull;
7225 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007226}
7227
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007228/*
7229 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7230 * just stops using VMX.
7231 */
7232static void free_nested(struct vcpu_vmx *vmx)
7233{
7234 if (!vmx->nested.vmxon)
7235 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007236
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007237 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007238 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007239 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007240 if (enable_shadow_vmcs) {
7241 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7242 free_vmcs(vmx->vmcs01.shadow_vmcs);
7243 vmx->vmcs01.shadow_vmcs = NULL;
7244 }
David Matlack4f2777b2016-07-13 17:16:37 -07007245 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007246 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007247 if (vmx->nested.apic_access_page) {
7248 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007249 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007250 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007251 if (vmx->nested.virtual_apic_page) {
7252 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007253 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007254 }
Wincy Van705699a2015-02-03 23:58:17 +08007255 if (vmx->nested.pi_desc_page) {
7256 kunmap(vmx->nested.pi_desc_page);
7257 nested_release_page(vmx->nested.pi_desc_page);
7258 vmx->nested.pi_desc_page = NULL;
7259 vmx->nested.pi_desc = NULL;
7260 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007261
Jim Mattson46e24df2017-11-27 17:22:25 -06007262 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007263}
7264
7265/* Emulate the VMXOFF instruction */
7266static int handle_vmoff(struct kvm_vcpu *vcpu)
7267{
7268 if (!nested_vmx_check_permission(vcpu))
7269 return 1;
7270 free_nested(to_vmx(vcpu));
7271 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007272 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007273 return 1;
7274}
7275
Nadav Har'El27d6c862011-05-25 23:06:59 +03007276/* Emulate the VMCLEAR instruction */
7277static int handle_vmclear(struct kvm_vcpu *vcpu)
7278{
7279 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007280 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007281 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007282
7283 if (!nested_vmx_check_permission(vcpu))
7284 return 1;
7285
Bandan Das4291b582014-05-06 02:19:18 -04007286 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007287 return 1;
7288
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007289 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007290 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007291
Jim Mattson29deec42017-03-02 12:41:48 -08007292 kvm_vcpu_write_guest(vcpu,
7293 vmptr + offsetof(struct vmcs12, launch_state),
7294 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007295
Nadav Har'El27d6c862011-05-25 23:06:59 +03007296 skip_emulated_instruction(vcpu);
7297 nested_vmx_succeed(vcpu);
7298 return 1;
7299}
7300
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007301static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7302
7303/* Emulate the VMLAUNCH instruction */
7304static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7305{
7306 return nested_vmx_run(vcpu, true);
7307}
7308
7309/* Emulate the VMRESUME instruction */
7310static int handle_vmresume(struct kvm_vcpu *vcpu)
7311{
7312
7313 return nested_vmx_run(vcpu, false);
7314}
7315
Nadav Har'El49f705c2011-05-25 23:08:30 +03007316enum vmcs_field_type {
7317 VMCS_FIELD_TYPE_U16 = 0,
7318 VMCS_FIELD_TYPE_U64 = 1,
7319 VMCS_FIELD_TYPE_U32 = 2,
7320 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7321};
7322
7323static inline int vmcs_field_type(unsigned long field)
7324{
7325 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7326 return VMCS_FIELD_TYPE_U32;
7327 return (field >> 13) & 0x3 ;
7328}
7329
7330static inline int vmcs_field_readonly(unsigned long field)
7331{
7332 return (((field >> 10) & 0x3) == 1);
7333}
7334
7335/*
7336 * Read a vmcs12 field. Since these can have varying lengths and we return
7337 * one type, we chose the biggest type (u64) and zero-extend the return value
7338 * to that size. Note that the caller, handle_vmread, might need to use only
7339 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7340 * 64-bit fields are to be returned).
7341 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007342static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7343 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007344{
7345 short offset = vmcs_field_to_offset(field);
7346 char *p;
7347
7348 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007349 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007350
7351 p = ((char *)(get_vmcs12(vcpu))) + offset;
7352
7353 switch (vmcs_field_type(field)) {
7354 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7355 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007356 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007357 case VMCS_FIELD_TYPE_U16:
7358 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007359 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007360 case VMCS_FIELD_TYPE_U32:
7361 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007362 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007363 case VMCS_FIELD_TYPE_U64:
7364 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007365 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007366 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007367 WARN_ON(1);
7368 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007369 }
7370}
7371
Abel Gordon20b97fe2013-04-18 14:36:25 +03007372
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007373static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7374 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007375 short offset = vmcs_field_to_offset(field);
7376 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7377 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007378 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007379
7380 switch (vmcs_field_type(field)) {
7381 case VMCS_FIELD_TYPE_U16:
7382 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007383 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007384 case VMCS_FIELD_TYPE_U32:
7385 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007386 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007387 case VMCS_FIELD_TYPE_U64:
7388 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007389 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007390 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7391 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007392 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007393 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007394 WARN_ON(1);
7395 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007396 }
7397
7398}
7399
Abel Gordon16f5b902013-04-18 14:38:25 +03007400static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7401{
7402 int i;
7403 unsigned long field;
7404 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007405 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007406 const unsigned long *fields = shadow_read_write_fields;
7407 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007408
Jan Kiszka282da872014-10-08 18:05:39 +02007409 preempt_disable();
7410
Abel Gordon16f5b902013-04-18 14:38:25 +03007411 vmcs_load(shadow_vmcs);
7412
7413 for (i = 0; i < num_fields; i++) {
7414 field = fields[i];
7415 switch (vmcs_field_type(field)) {
7416 case VMCS_FIELD_TYPE_U16:
7417 field_value = vmcs_read16(field);
7418 break;
7419 case VMCS_FIELD_TYPE_U32:
7420 field_value = vmcs_read32(field);
7421 break;
7422 case VMCS_FIELD_TYPE_U64:
7423 field_value = vmcs_read64(field);
7424 break;
7425 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7426 field_value = vmcs_readl(field);
7427 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007428 default:
7429 WARN_ON(1);
7430 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007431 }
7432 vmcs12_write_any(&vmx->vcpu, field, field_value);
7433 }
7434
7435 vmcs_clear(shadow_vmcs);
7436 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007437
7438 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007439}
7440
Abel Gordonc3114422013-04-18 14:38:55 +03007441static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7442{
Mathias Krausec2bae892013-06-26 20:36:21 +02007443 const unsigned long *fields[] = {
7444 shadow_read_write_fields,
7445 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007446 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007447 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007448 max_shadow_read_write_fields,
7449 max_shadow_read_only_fields
7450 };
7451 int i, q;
7452 unsigned long field;
7453 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007454 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007455
7456 vmcs_load(shadow_vmcs);
7457
Mathias Krausec2bae892013-06-26 20:36:21 +02007458 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007459 for (i = 0; i < max_fields[q]; i++) {
7460 field = fields[q][i];
7461 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7462
7463 switch (vmcs_field_type(field)) {
7464 case VMCS_FIELD_TYPE_U16:
7465 vmcs_write16(field, (u16)field_value);
7466 break;
7467 case VMCS_FIELD_TYPE_U32:
7468 vmcs_write32(field, (u32)field_value);
7469 break;
7470 case VMCS_FIELD_TYPE_U64:
7471 vmcs_write64(field, (u64)field_value);
7472 break;
7473 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7474 vmcs_writel(field, (long)field_value);
7475 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007476 default:
7477 WARN_ON(1);
7478 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007479 }
7480 }
7481 }
7482
7483 vmcs_clear(shadow_vmcs);
7484 vmcs_load(vmx->loaded_vmcs->vmcs);
7485}
7486
Nadav Har'El49f705c2011-05-25 23:08:30 +03007487/*
7488 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7489 * used before) all generate the same failure when it is missing.
7490 */
7491static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7492{
7493 struct vcpu_vmx *vmx = to_vmx(vcpu);
7494 if (vmx->nested.current_vmptr == -1ull) {
7495 nested_vmx_failInvalid(vcpu);
7496 skip_emulated_instruction(vcpu);
7497 return 0;
7498 }
7499 return 1;
7500}
7501
7502static int handle_vmread(struct kvm_vcpu *vcpu)
7503{
7504 unsigned long field;
7505 u64 field_value;
7506 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7507 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7508 gva_t gva = 0;
7509
7510 if (!nested_vmx_check_permission(vcpu) ||
7511 !nested_vmx_check_vmcs12(vcpu))
7512 return 1;
7513
7514 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007515 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007516 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007517 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007518 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7519 skip_emulated_instruction(vcpu);
7520 return 1;
7521 }
7522 /*
7523 * Now copy part of this value to register or memory, as requested.
7524 * Note that the number of bits actually copied is 32 or 64 depending
7525 * on the guest's mode (32 or 64 bit), not on the given field's length.
7526 */
7527 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007528 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007529 field_value);
7530 } else {
7531 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007532 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007533 return 1;
7534 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007535 kvm_write_guest_virt_system(vcpu, gva, &field_value,
7536 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007537 }
7538
7539 nested_vmx_succeed(vcpu);
7540 skip_emulated_instruction(vcpu);
7541 return 1;
7542}
7543
7544
7545static int handle_vmwrite(struct kvm_vcpu *vcpu)
7546{
7547 unsigned long field;
7548 gva_t gva;
7549 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7550 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007551 /* The value to write might be 32 or 64 bits, depending on L1's long
7552 * mode, and eventually we need to write that into a field of several
7553 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007554 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007555 * bits into the vmcs12 field.
7556 */
7557 u64 field_value = 0;
7558 struct x86_exception e;
7559
7560 if (!nested_vmx_check_permission(vcpu) ||
7561 !nested_vmx_check_vmcs12(vcpu))
7562 return 1;
7563
7564 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007565 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007566 (((vmx_instruction_info) >> 3) & 0xf));
7567 else {
7568 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007569 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007570 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007571 if (kvm_read_guest_virt(vcpu, gva, &field_value,
7572 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007573 kvm_inject_page_fault(vcpu, &e);
7574 return 1;
7575 }
7576 }
7577
7578
Nadav Amit27e6fb52014-06-18 17:19:26 +03007579 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007580 if (vmcs_field_readonly(field)) {
7581 nested_vmx_failValid(vcpu,
7582 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7583 skip_emulated_instruction(vcpu);
7584 return 1;
7585 }
7586
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007587 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007588 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7589 skip_emulated_instruction(vcpu);
7590 return 1;
7591 }
7592
7593 nested_vmx_succeed(vcpu);
7594 skip_emulated_instruction(vcpu);
7595 return 1;
7596}
7597
Nadav Har'El63846662011-05-25 23:07:29 +03007598/* Emulate the VMPTRLD instruction */
7599static int handle_vmptrld(struct kvm_vcpu *vcpu)
7600{
7601 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007602 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007603
7604 if (!nested_vmx_check_permission(vcpu))
7605 return 1;
7606
Bandan Das4291b582014-05-06 02:19:18 -04007607 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007608 return 1;
7609
Nadav Har'El63846662011-05-25 23:07:29 +03007610 if (vmx->nested.current_vmptr != vmptr) {
7611 struct vmcs12 *new_vmcs12;
7612 struct page *page;
7613 page = nested_get_page(vcpu, vmptr);
7614 if (page == NULL) {
7615 nested_vmx_failInvalid(vcpu);
7616 skip_emulated_instruction(vcpu);
7617 return 1;
7618 }
7619 new_vmcs12 = kmap(page);
7620 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7621 kunmap(page);
7622 nested_release_page_clean(page);
7623 nested_vmx_failValid(vcpu,
7624 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7625 skip_emulated_instruction(vcpu);
7626 return 1;
7627 }
Nadav Har'El63846662011-05-25 23:07:29 +03007628
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007629 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007630 vmx->nested.current_vmptr = vmptr;
7631 vmx->nested.current_vmcs12 = new_vmcs12;
7632 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007633 /*
7634 * Load VMCS12 from guest memory since it is not already
7635 * cached.
7636 */
7637 memcpy(vmx->nested.cached_vmcs12,
7638 vmx->nested.current_vmcs12, VMCS12_SIZE);
7639
Abel Gordon012f83c2013-04-18 14:39:25 +03007640 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007641 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7642 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007643 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007644 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007645 vmx->nested.sync_shadow_vmcs = true;
7646 }
Nadav Har'El63846662011-05-25 23:07:29 +03007647 }
7648
7649 nested_vmx_succeed(vcpu);
7650 skip_emulated_instruction(vcpu);
7651 return 1;
7652}
7653
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007654/* Emulate the VMPTRST instruction */
7655static int handle_vmptrst(struct kvm_vcpu *vcpu)
7656{
7657 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7658 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7659 gva_t vmcs_gva;
7660 struct x86_exception e;
7661
7662 if (!nested_vmx_check_permission(vcpu))
7663 return 1;
7664
7665 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007666 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007667 return 1;
7668 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007669 if (kvm_write_guest_virt_system(vcpu, vmcs_gva,
7670 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7671 sizeof(u64), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007672 kvm_inject_page_fault(vcpu, &e);
7673 return 1;
7674 }
7675 nested_vmx_succeed(vcpu);
7676 skip_emulated_instruction(vcpu);
7677 return 1;
7678}
7679
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007680/* Emulate the INVEPT instruction */
7681static int handle_invept(struct kvm_vcpu *vcpu)
7682{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007683 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007684 u32 vmx_instruction_info, types;
7685 unsigned long type;
7686 gva_t gva;
7687 struct x86_exception e;
7688 struct {
7689 u64 eptp, gpa;
7690 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007691
Wincy Vanb9c237b2015-02-03 23:56:30 +08007692 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7693 SECONDARY_EXEC_ENABLE_EPT) ||
7694 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007695 kvm_queue_exception(vcpu, UD_VECTOR);
7696 return 1;
7697 }
7698
7699 if (!nested_vmx_check_permission(vcpu))
7700 return 1;
7701
7702 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7703 kvm_queue_exception(vcpu, UD_VECTOR);
7704 return 1;
7705 }
7706
7707 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007708 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007709
Wincy Vanb9c237b2015-02-03 23:56:30 +08007710 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007711
Jim Mattson85c856b2016-10-26 08:38:38 -07007712 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007713 nested_vmx_failValid(vcpu,
7714 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007715 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007716 return 1;
7717 }
7718
7719 /* According to the Intel VMX instruction reference, the memory
7720 * operand is read even if it isn't needed (e.g., for type==global)
7721 */
7722 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007723 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007724 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007725 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007726 kvm_inject_page_fault(vcpu, &e);
7727 return 1;
7728 }
7729
7730 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007731 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007732 /*
7733 * TODO: track mappings and invalidate
7734 * single context requests appropriately
7735 */
7736 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007737 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007738 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007739 nested_vmx_succeed(vcpu);
7740 break;
7741 default:
7742 BUG_ON(1);
7743 break;
7744 }
7745
7746 skip_emulated_instruction(vcpu);
7747 return 1;
7748}
7749
Petr Matouseka642fc32014-09-23 20:22:30 +02007750static int handle_invvpid(struct kvm_vcpu *vcpu)
7751{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007752 struct vcpu_vmx *vmx = to_vmx(vcpu);
7753 u32 vmx_instruction_info;
7754 unsigned long type, types;
7755 gva_t gva;
7756 struct x86_exception e;
7757 int vpid;
7758
7759 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7760 SECONDARY_EXEC_ENABLE_VPID) ||
7761 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7762 kvm_queue_exception(vcpu, UD_VECTOR);
7763 return 1;
7764 }
7765
7766 if (!nested_vmx_check_permission(vcpu))
7767 return 1;
7768
7769 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7770 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7771
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007772 types = (vmx->nested.nested_vmx_vpid_caps &
7773 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007774
Jim Mattson85c856b2016-10-26 08:38:38 -07007775 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007776 nested_vmx_failValid(vcpu,
7777 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007778 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007779 return 1;
7780 }
7781
7782 /* according to the intel vmx instruction reference, the memory
7783 * operand is read even if it isn't needed (e.g., for type==global)
7784 */
7785 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7786 vmx_instruction_info, false, &gva))
7787 return 1;
Paolo Bonzini838b0e92018-06-06 17:37:49 +02007788 if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007789 kvm_inject_page_fault(vcpu, &e);
7790 return 1;
7791 }
7792
7793 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007794 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007795 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007796 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7797 if (!vpid) {
7798 nested_vmx_failValid(vcpu,
7799 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7800 skip_emulated_instruction(vcpu);
7801 return 1;
7802 }
7803 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007804 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007805 break;
7806 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007807 WARN_ON_ONCE(1);
7808 skip_emulated_instruction(vcpu);
7809 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007810 }
7811
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007812 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7813 nested_vmx_succeed(vcpu);
7814
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007815 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007816 return 1;
7817}
7818
Kai Huang843e4332015-01-28 10:54:28 +08007819static int handle_pml_full(struct kvm_vcpu *vcpu)
7820{
7821 unsigned long exit_qualification;
7822
7823 trace_kvm_pml_full(vcpu->vcpu_id);
7824
7825 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7826
7827 /*
7828 * PML buffer FULL happened while executing iret from NMI,
7829 * "blocked by NMI" bit has to be set before next VM entry.
7830 */
7831 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7832 cpu_has_virtual_nmis() &&
7833 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7834 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7835 GUEST_INTR_STATE_NMI);
7836
7837 /*
7838 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7839 * here.., and there's no userspace involvement needed for PML.
7840 */
7841 return 1;
7842}
7843
Yunhong Jiang64672c92016-06-13 14:19:59 -07007844static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7845{
7846 kvm_lapic_expired_hv_timer(vcpu);
7847 return 1;
7848}
7849
Nadav Har'El0140cae2011-05-25 23:06:28 +03007850/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007851 * The exit handlers return 1 if the exit was handled fully and guest execution
7852 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7853 * to be done to userspace and return 0.
7854 */
Mathias Krause772e0312012-08-30 01:30:19 +02007855static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007856 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7857 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007858 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007859 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007860 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007861 [EXIT_REASON_CR_ACCESS] = handle_cr,
7862 [EXIT_REASON_DR_ACCESS] = handle_dr,
7863 [EXIT_REASON_CPUID] = handle_cpuid,
7864 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7865 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7866 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7867 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007868 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007869 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007870 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007871 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007872 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007873 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007874 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007875 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007876 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007877 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007878 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007879 [EXIT_REASON_VMOFF] = handle_vmoff,
7880 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007881 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7882 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007883 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007884 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007885 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007886 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007887 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007888 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007889 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7890 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007891 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007892 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007893 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007894 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007895 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007896 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007897 [EXIT_REASON_XSAVES] = handle_xsaves,
7898 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007899 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007900 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007901};
7902
7903static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007904 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007905
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007906static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7907 struct vmcs12 *vmcs12)
7908{
7909 unsigned long exit_qualification;
7910 gpa_t bitmap, last_bitmap;
7911 unsigned int port;
7912 int size;
7913 u8 b;
7914
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007915 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007916 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007917
7918 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7919
7920 port = exit_qualification >> 16;
7921 size = (exit_qualification & 7) + 1;
7922
7923 last_bitmap = (gpa_t)-1;
7924 b = -1;
7925
7926 while (size > 0) {
7927 if (port < 0x8000)
7928 bitmap = vmcs12->io_bitmap_a;
7929 else if (port < 0x10000)
7930 bitmap = vmcs12->io_bitmap_b;
7931 else
Joe Perches1d804d02015-03-30 16:46:09 -07007932 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007933 bitmap += (port & 0x7fff) / 8;
7934
7935 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007936 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007937 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007938 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007939 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007940
7941 port++;
7942 size--;
7943 last_bitmap = bitmap;
7944 }
7945
Joe Perches1d804d02015-03-30 16:46:09 -07007946 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007947}
7948
Nadav Har'El644d7112011-05-25 23:12:35 +03007949/*
7950 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7951 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7952 * disinterest in the current event (read or write a specific MSR) by using an
7953 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7954 */
7955static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7956 struct vmcs12 *vmcs12, u32 exit_reason)
7957{
7958 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7959 gpa_t bitmap;
7960
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007961 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007962 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007963
7964 /*
7965 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7966 * for the four combinations of read/write and low/high MSR numbers.
7967 * First we need to figure out which of the four to use:
7968 */
7969 bitmap = vmcs12->msr_bitmap;
7970 if (exit_reason == EXIT_REASON_MSR_WRITE)
7971 bitmap += 2048;
7972 if (msr_index >= 0xc0000000) {
7973 msr_index -= 0xc0000000;
7974 bitmap += 1024;
7975 }
7976
7977 /* Then read the msr_index'th bit from this bitmap: */
7978 if (msr_index < 1024*8) {
7979 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007980 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007981 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007982 return 1 & (b >> (msr_index & 7));
7983 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007984 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007985}
7986
7987/*
7988 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7989 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7990 * intercept (via guest_host_mask etc.) the current event.
7991 */
7992static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7993 struct vmcs12 *vmcs12)
7994{
7995 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7996 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02007997 int reg;
7998 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03007999
8000 switch ((exit_qualification >> 4) & 3) {
8001 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008002 reg = (exit_qualification >> 8) & 15;
8003 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03008004 switch (cr) {
8005 case 0:
8006 if (vmcs12->cr0_guest_host_mask &
8007 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008008 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008009 break;
8010 case 3:
8011 if ((vmcs12->cr3_target_count >= 1 &&
8012 vmcs12->cr3_target_value0 == val) ||
8013 (vmcs12->cr3_target_count >= 2 &&
8014 vmcs12->cr3_target_value1 == val) ||
8015 (vmcs12->cr3_target_count >= 3 &&
8016 vmcs12->cr3_target_value2 == val) ||
8017 (vmcs12->cr3_target_count >= 4 &&
8018 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07008019 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008020 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008021 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008022 break;
8023 case 4:
8024 if (vmcs12->cr4_guest_host_mask &
8025 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07008026 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008027 break;
8028 case 8:
8029 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07008030 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008031 break;
8032 }
8033 break;
8034 case 2: /* clts */
8035 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
8036 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008037 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008038 break;
8039 case 1: /* mov from cr */
8040 switch (cr) {
8041 case 3:
8042 if (vmcs12->cpu_based_vm_exec_control &
8043 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008044 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008045 break;
8046 case 8:
8047 if (vmcs12->cpu_based_vm_exec_control &
8048 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07008049 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008050 break;
8051 }
8052 break;
8053 case 3: /* lmsw */
8054 /*
8055 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8056 * cr0. Other attempted changes are ignored, with no exit.
8057 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02008058 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03008059 if (vmcs12->cr0_guest_host_mask & 0xe &
8060 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008061 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008062 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8063 !(vmcs12->cr0_read_shadow & 0x1) &&
8064 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008065 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008066 break;
8067 }
Joe Perches1d804d02015-03-30 16:46:09 -07008068 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008069}
8070
8071/*
8072 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8073 * should handle it ourselves in L0 (and then continue L2). Only call this
8074 * when in is_guest_mode (L2).
8075 */
8076static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8077{
Nadav Har'El644d7112011-05-25 23:12:35 +03008078 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8079 struct vcpu_vmx *vmx = to_vmx(vcpu);
8080 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008081 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008082
Jan Kiszka542060e2014-01-04 18:47:21 +01008083 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8084 vmcs_readl(EXIT_QUALIFICATION),
8085 vmx->idt_vectoring_info,
8086 intr_info,
8087 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8088 KVM_ISA_VMX);
8089
David Matlackb7649e12017-08-01 14:00:40 -07008090 /*
8091 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008092 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8093 * Page). The CPU may write to these pages via their host
8094 * physical address while L2 is running, bypassing any
8095 * address-translation-based dirty tracking (e.g. EPT write
8096 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008097 *
8098 * Mark them dirty on every exit from L2 to prevent them from
8099 * getting out of sync with dirty tracking.
8100 */
8101 nested_mark_vmcs12_pages_dirty(vcpu);
8102
Nadav Har'El644d7112011-05-25 23:12:35 +03008103 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008104 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008105
8106 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008107 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8108 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008109 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008110 }
8111
8112 switch (exit_reason) {
8113 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008114 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008115 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008116 else if (is_page_fault(intr_info))
8117 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008118 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008119 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008120 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008121 else if (is_debug(intr_info) &&
8122 vcpu->guest_debug &
8123 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8124 return false;
8125 else if (is_breakpoint(intr_info) &&
8126 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8127 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008128 return vmcs12->exception_bitmap &
8129 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8130 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008131 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008132 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008133 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008134 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008135 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008136 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008137 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008138 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008139 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008140 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008141 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008142 case EXIT_REASON_HLT:
8143 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8144 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008145 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008146 case EXIT_REASON_INVLPG:
8147 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8148 case EXIT_REASON_RDPMC:
8149 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008150 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008151 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8152 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8153 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8154 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8155 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8156 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008157 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008158 /*
8159 * VMX instructions trap unconditionally. This allows L1 to
8160 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8161 */
Joe Perches1d804d02015-03-30 16:46:09 -07008162 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008163 case EXIT_REASON_CR_ACCESS:
8164 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8165 case EXIT_REASON_DR_ACCESS:
8166 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8167 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008168 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008169 case EXIT_REASON_MSR_READ:
8170 case EXIT_REASON_MSR_WRITE:
8171 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8172 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008173 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008174 case EXIT_REASON_MWAIT_INSTRUCTION:
8175 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008176 case EXIT_REASON_MONITOR_TRAP_FLAG:
8177 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008178 case EXIT_REASON_MONITOR_INSTRUCTION:
8179 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8180 case EXIT_REASON_PAUSE_INSTRUCTION:
8181 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8182 nested_cpu_has2(vmcs12,
8183 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8184 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008185 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008186 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008187 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008188 case EXIT_REASON_APIC_ACCESS:
8189 return nested_cpu_has2(vmcs12,
8190 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008191 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008192 case EXIT_REASON_EOI_INDUCED:
8193 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008194 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008195 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008196 /*
8197 * L0 always deals with the EPT violation. If nested EPT is
8198 * used, and the nested mmu code discovers that the address is
8199 * missing in the guest EPT table (EPT12), the EPT violation
8200 * will be injected with nested_ept_inject_page_fault()
8201 */
Joe Perches1d804d02015-03-30 16:46:09 -07008202 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008203 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008204 /*
8205 * L2 never uses directly L1's EPT, but rather L0's own EPT
8206 * table (shadow on EPT) or a merged EPT table that L0 built
8207 * (EPT on EPT). So any problems with the structure of the
8208 * table is L0's fault.
8209 */
Joe Perches1d804d02015-03-30 16:46:09 -07008210 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008211 case EXIT_REASON_WBINVD:
8212 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8213 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008214 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008215 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8216 /*
8217 * This should never happen, since it is not possible to
8218 * set XSS to a non-zero value---neither in L1 nor in L2.
8219 * If if it were, XSS would have to be checked against
8220 * the XSS exit bitmap in vmcs12.
8221 */
8222 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008223 case EXIT_REASON_PREEMPTION_TIMER:
8224 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008225 case EXIT_REASON_PML_FULL:
8226 /* We don't expose PML support to L1. */
8227 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008228 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008229 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008230 }
8231}
8232
Avi Kivity586f9602010-11-18 13:09:54 +02008233static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8234{
8235 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8236 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8237}
8238
Kai Huanga3eaa862015-11-04 13:46:05 +08008239static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008240{
Kai Huanga3eaa862015-11-04 13:46:05 +08008241 if (vmx->pml_pg) {
8242 __free_page(vmx->pml_pg);
8243 vmx->pml_pg = NULL;
8244 }
Kai Huang843e4332015-01-28 10:54:28 +08008245}
8246
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008247static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008248{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008249 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008250 u64 *pml_buf;
8251 u16 pml_idx;
8252
8253 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8254
8255 /* Do nothing if PML buffer is empty */
8256 if (pml_idx == (PML_ENTITY_NUM - 1))
8257 return;
8258
8259 /* PML index always points to next available PML buffer entity */
8260 if (pml_idx >= PML_ENTITY_NUM)
8261 pml_idx = 0;
8262 else
8263 pml_idx++;
8264
8265 pml_buf = page_address(vmx->pml_pg);
8266 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8267 u64 gpa;
8268
8269 gpa = pml_buf[pml_idx];
8270 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008271 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008272 }
8273
8274 /* reset PML index */
8275 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8276}
8277
8278/*
8279 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8280 * Called before reporting dirty_bitmap to userspace.
8281 */
8282static void kvm_flush_pml_buffers(struct kvm *kvm)
8283{
8284 int i;
8285 struct kvm_vcpu *vcpu;
8286 /*
8287 * We only need to kick vcpu out of guest mode here, as PML buffer
8288 * is flushed at beginning of all VMEXITs, and it's obvious that only
8289 * vcpus running in guest are possible to have unflushed GPAs in PML
8290 * buffer.
8291 */
8292 kvm_for_each_vcpu(i, vcpu, kvm)
8293 kvm_vcpu_kick(vcpu);
8294}
8295
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008296static void vmx_dump_sel(char *name, uint32_t sel)
8297{
8298 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008299 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008300 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8301 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8302 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8303}
8304
8305static void vmx_dump_dtsel(char *name, uint32_t limit)
8306{
8307 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8308 name, vmcs_read32(limit),
8309 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8310}
8311
8312static void dump_vmcs(void)
8313{
8314 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8315 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8316 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8317 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8318 u32 secondary_exec_control = 0;
8319 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008320 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008321 int i, n;
8322
8323 if (cpu_has_secondary_exec_ctrls())
8324 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8325
8326 pr_err("*** Guest State ***\n");
8327 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8328 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8329 vmcs_readl(CR0_GUEST_HOST_MASK));
8330 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8331 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8332 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8333 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8334 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8335 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008336 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8337 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8338 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8339 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008340 }
8341 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8342 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8343 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8344 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8345 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8346 vmcs_readl(GUEST_SYSENTER_ESP),
8347 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8348 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8349 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8350 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8351 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8352 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8353 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8354 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8355 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8356 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8357 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8358 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8359 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008360 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8361 efer, vmcs_read64(GUEST_IA32_PAT));
8362 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8363 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008364 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8365 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008366 pr_err("PerfGlobCtl = 0x%016llx\n",
8367 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008368 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008369 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008370 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8371 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8372 vmcs_read32(GUEST_ACTIVITY_STATE));
8373 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8374 pr_err("InterruptStatus = %04x\n",
8375 vmcs_read16(GUEST_INTR_STATUS));
8376
8377 pr_err("*** Host State ***\n");
8378 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8379 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8380 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8381 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8382 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8383 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8384 vmcs_read16(HOST_TR_SELECTOR));
8385 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8386 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8387 vmcs_readl(HOST_TR_BASE));
8388 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8389 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8390 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8391 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8392 vmcs_readl(HOST_CR4));
8393 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8394 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8395 vmcs_read32(HOST_IA32_SYSENTER_CS),
8396 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8397 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008398 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8399 vmcs_read64(HOST_IA32_EFER),
8400 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008401 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008402 pr_err("PerfGlobCtl = 0x%016llx\n",
8403 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008404
8405 pr_err("*** Control State ***\n");
8406 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8407 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8408 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8409 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8410 vmcs_read32(EXCEPTION_BITMAP),
8411 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8412 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8413 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8414 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8415 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8416 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8417 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8418 vmcs_read32(VM_EXIT_INTR_INFO),
8419 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8420 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8421 pr_err(" reason=%08x qualification=%016lx\n",
8422 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8423 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8424 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8425 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008426 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008427 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008428 pr_err("TSC Multiplier = 0x%016llx\n",
8429 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008430 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8431 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8432 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8433 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8434 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008435 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008436 n = vmcs_read32(CR3_TARGET_COUNT);
8437 for (i = 0; i + 1 < n; i += 4)
8438 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8439 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8440 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8441 if (i < n)
8442 pr_err("CR3 target%u=%016lx\n",
8443 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8444 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8445 pr_err("PLE Gap=%08x Window=%08x\n",
8446 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8447 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8448 pr_err("Virtual processor ID = 0x%04x\n",
8449 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8450}
8451
Avi Kivity6aa8b732006-12-10 02:21:36 -08008452/*
8453 * The guest has exited. See if we can fix it or if we need userspace
8454 * assistance.
8455 */
Avi Kivity851ba692009-08-24 11:10:17 +03008456static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008457{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008458 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008459 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008460 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008461
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008462 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8463
Kai Huang843e4332015-01-28 10:54:28 +08008464 /*
8465 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8466 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8467 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8468 * mode as if vcpus is in root mode, the PML buffer must has been
8469 * flushed already.
8470 */
8471 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008472 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008473
Mohammed Gamal80ced182009-09-01 12:48:18 +02008474 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008475 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008476 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008477
Nadav Har'El644d7112011-05-25 23:12:35 +03008478 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008479 nested_vmx_vmexit(vcpu, exit_reason,
8480 vmcs_read32(VM_EXIT_INTR_INFO),
8481 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008482 return 1;
8483 }
8484
Mohammed Gamal51207022010-05-31 22:40:54 +03008485 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008486 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008487 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8488 vcpu->run->fail_entry.hardware_entry_failure_reason
8489 = exit_reason;
8490 return 0;
8491 }
8492
Avi Kivity29bd8a72007-09-10 17:27:03 +03008493 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008494 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8495 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008496 = vmcs_read32(VM_INSTRUCTION_ERROR);
8497 return 0;
8498 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008499
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008500 /*
8501 * Note:
8502 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8503 * delivery event since it indicates guest is accessing MMIO.
8504 * The vm-exit can be triggered again after return to guest that
8505 * will cause infinite loop.
8506 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008507 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008508 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008509 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008510 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008511 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8512 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8513 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8514 vcpu->run->internal.ndata = 2;
8515 vcpu->run->internal.data[0] = vectoring_info;
8516 vcpu->run->internal.data[1] = exit_reason;
8517 return 0;
8518 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008519
Nadav Har'El644d7112011-05-25 23:12:35 +03008520 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8521 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008522 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008523 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008524 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008525 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008526 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008527 /*
8528 * This CPU don't support us in finding the end of an
8529 * NMI-blocked window if the guest runs with IRQs
8530 * disabled. So we pull the trigger after 1 s of
8531 * futile waiting, but inform the user about this.
8532 */
8533 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8534 "state on VCPU %d after 1 s timeout\n",
8535 __func__, vcpu->vcpu_id);
8536 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008537 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008538 }
8539
Avi Kivity6aa8b732006-12-10 02:21:36 -08008540 if (exit_reason < kvm_vmx_max_exit_handlers
8541 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008542 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008543 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008544 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8545 kvm_queue_exception(vcpu, UD_VECTOR);
8546 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008547 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008548}
8549
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008550/*
8551 * Software based L1D cache flush which is used when microcode providing
8552 * the cache control MSR is not loaded.
8553 *
8554 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
8555 * flush it is required to read in 64 KiB because the replacement algorithm
8556 * is not exactly LRU. This could be sized at runtime via topology
8557 * information but as all relevant affected CPUs have 32KiB L1D cache size
8558 * there is no point in doing so.
8559 */
8560#define L1D_CACHE_ORDER 4
8561static void *vmx_l1d_flush_pages;
8562
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008563static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008564{
8565 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008566 bool always;
8567
8568 /*
8569 * If the mitigation mode is 'flush always', keep the flush bit
8570 * set, otherwise clear it. It gets set again either from
8571 * vcpu_run() or from one of the unsafe VMEXIT handlers.
8572 */
8573 always = vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS;
8574 vcpu->arch.l1tf_flush_l1d = always;
8575
8576 vcpu->stat.l1d_flush++;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008577
Paolo Bonziniacca8a72018-07-02 13:03:48 +02008578 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
8579 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
8580 return;
8581 }
8582
Paolo Bonzinib3d648a2018-07-02 12:47:38 +02008583 asm volatile(
8584 /* First ensure the pages are in the TLB */
8585 "xorl %%eax, %%eax\n"
8586 ".Lpopulate_tlb:\n\t"
8587 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
8588 "addl $4096, %%eax\n\t"
8589 "cmpl %%eax, %[size]\n\t"
8590 "jne .Lpopulate_tlb\n\t"
8591 "xorl %%eax, %%eax\n\t"
8592 "cpuid\n\t"
8593 /* Now fill the cache */
8594 "xorl %%eax, %%eax\n"
8595 ".Lfill_cache:\n"
8596 "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t"
8597 "addl $64, %%eax\n\t"
8598 "cmpl %%eax, %[size]\n\t"
8599 "jne .Lfill_cache\n\t"
8600 "lfence\n"
8601 :: [empty_zp] "r" (vmx_l1d_flush_pages),
8602 [size] "r" (size)
8603 : "eax", "ebx", "ecx", "edx");
8604}
8605
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008606static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008607{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008608 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8609
8610 if (is_guest_mode(vcpu) &&
8611 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8612 return;
8613
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008614 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008615 vmcs_write32(TPR_THRESHOLD, 0);
8616 return;
8617 }
8618
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008619 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008620}
8621
Yang Zhang8d146952013-01-25 10:18:50 +08008622static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8623{
8624 u32 sec_exec_control;
8625
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008626 /* Postpone execution until vmcs01 is the current VMCS. */
8627 if (is_guest_mode(vcpu)) {
8628 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8629 return;
8630 }
8631
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008632 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008633 return;
8634
Paolo Bonzini35754c92015-07-29 12:05:37 +02008635 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008636 return;
8637
8638 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8639
8640 if (set) {
8641 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8642 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8643 } else {
8644 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8645 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008646 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008647 }
8648 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8649
Paolo Bonzini6236b782018-01-16 16:51:18 +01008650 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008651}
8652
Tang Chen38b99172014-09-24 15:57:54 +08008653static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8654{
8655 struct vcpu_vmx *vmx = to_vmx(vcpu);
8656
8657 /*
8658 * Currently we do not handle the nested case where L2 has an
8659 * APIC access page of its own; that page is still pinned.
8660 * Hence, we skip the case where the VCPU is in guest mode _and_
8661 * L1 prepared an APIC access page for L2.
8662 *
8663 * For the case where L1 and L2 share the same APIC access page
8664 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8665 * in the vmcs12), this function will only update either the vmcs01
8666 * or the vmcs02. If the former, the vmcs02 will be updated by
8667 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8668 * the next L2->L1 exit.
8669 */
8670 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008671 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008672 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008673 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008674 vmx_flush_tlb_ept_only(vcpu);
8675 }
Tang Chen38b99172014-09-24 15:57:54 +08008676}
8677
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008678static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008679{
8680 u16 status;
8681 u8 old;
8682
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008683 if (max_isr == -1)
8684 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008685
8686 status = vmcs_read16(GUEST_INTR_STATUS);
8687 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008688 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008689 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008690 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008691 vmcs_write16(GUEST_INTR_STATUS, status);
8692 }
8693}
8694
8695static void vmx_set_rvi(int vector)
8696{
8697 u16 status;
8698 u8 old;
8699
Wei Wang4114c272014-11-05 10:53:43 +08008700 if (vector == -1)
8701 vector = 0;
8702
Yang Zhangc7c9c562013-01-25 10:18:51 +08008703 status = vmcs_read16(GUEST_INTR_STATUS);
8704 old = (u8)status & 0xff;
8705 if ((u8)vector != old) {
8706 status &= ~0xff;
8707 status |= (u8)vector;
8708 vmcs_write16(GUEST_INTR_STATUS, status);
8709 }
8710}
8711
8712static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8713{
Wanpeng Li963fee12014-07-17 19:03:00 +08008714 if (!is_guest_mode(vcpu)) {
8715 vmx_set_rvi(max_irr);
8716 return;
8717 }
8718
Wei Wang4114c272014-11-05 10:53:43 +08008719 if (max_irr == -1)
8720 return;
8721
Wanpeng Li963fee12014-07-17 19:03:00 +08008722 /*
Wei Wang4114c272014-11-05 10:53:43 +08008723 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8724 * handles it.
8725 */
8726 if (nested_exit_on_intr(vcpu))
8727 return;
8728
8729 /*
8730 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008731 * is run without virtual interrupt delivery.
8732 */
8733 if (!kvm_event_needs_reinjection(vcpu) &&
8734 vmx_interrupt_allowed(vcpu)) {
8735 kvm_queue_interrupt(vcpu, max_irr, false);
8736 vmx_inject_irq(vcpu);
8737 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008738}
8739
Andrey Smetanin63086302015-11-10 15:36:32 +03008740static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008741{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008742 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008743 return;
8744
Yang Zhangc7c9c562013-01-25 10:18:51 +08008745 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8746 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8747 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8748 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8749}
8750
Avi Kivity51aa01d2010-07-20 14:31:20 +03008751static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008752{
Avi Kivity00eba012011-03-07 17:24:54 +02008753 u32 exit_intr_info;
8754
8755 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8756 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8757 return;
8758
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008759 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008760 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008761
8762 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008763 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008764 kvm_machine_check();
8765
Gleb Natapov20f65982009-05-11 13:35:55 +03008766 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008767 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008768 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008769 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008770 kvm_after_handle_nmi(&vmx->vcpu);
8771 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008772}
Gleb Natapov20f65982009-05-11 13:35:55 +03008773
Yang Zhanga547c6d2013-04-11 19:25:10 +08008774static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8775{
8776 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008777 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008778
8779 /*
8780 * If external interrupt exists, IF bit is set in rflags/eflags on the
8781 * interrupt stack frame, and interrupt will be enabled on a return
8782 * from interrupt handler.
8783 */
8784 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8785 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8786 unsigned int vector;
8787 unsigned long entry;
8788 gate_desc *desc;
8789 struct vcpu_vmx *vmx = to_vmx(vcpu);
8790#ifdef CONFIG_X86_64
8791 unsigned long tmp;
8792#endif
8793
8794 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8795 desc = (gate_desc *)vmx->host_idt_base + vector;
8796 entry = gate_offset(*desc);
8797 asm volatile(
8798#ifdef CONFIG_X86_64
8799 "mov %%" _ASM_SP ", %[sp]\n\t"
8800 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8801 "push $%c[ss]\n\t"
8802 "push %[sp]\n\t"
8803#endif
8804 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008805 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008806 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008807 :
8808#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008809 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008810#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008811 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008812 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008813 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008814 [ss]"i"(__KERNEL_DS),
8815 [cs]"i"(__KERNEL_CS)
8816 );
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02008817 vcpu->arch.l1tf_flush_l1d = true;
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008818 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008819}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05008820STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008821
Tom Lendackyb9655922018-05-10 22:06:39 +02008822static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008823{
Tom Lendackyb9655922018-05-10 22:06:39 +02008824 switch (index) {
8825 case MSR_IA32_SMBASE:
8826 /*
8827 * We cannot do SMM unless we can run the guest in big
8828 * real mode.
8829 */
8830 return enable_unrestricted_guest || emulate_invalid_guest_state;
8831 case MSR_AMD64_VIRT_SPEC_CTRL:
8832 /* This is AMD only. */
8833 return false;
8834 default:
8835 return true;
8836 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008837}
8838
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008839static bool vmx_mpx_supported(void)
8840{
8841 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8842 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8843}
8844
Wanpeng Li55412b22014-12-02 19:21:30 +08008845static bool vmx_xsaves_supported(void)
8846{
8847 return vmcs_config.cpu_based_2nd_exec_ctrl &
8848 SECONDARY_EXEC_XSAVES;
8849}
8850
Avi Kivity51aa01d2010-07-20 14:31:20 +03008851static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8852{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008853 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008854 bool unblock_nmi;
8855 u8 vector;
8856 bool idtv_info_valid;
8857
8858 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008859
Avi Kivitycf393f72008-07-01 16:20:21 +03008860 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008861 if (vmx->nmi_known_unmasked)
8862 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008863 /*
8864 * Can't use vmx->exit_intr_info since we're not sure what
8865 * the exit reason is.
8866 */
8867 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008868 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8869 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8870 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008871 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008872 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8873 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008874 * SDM 3: 23.2.2 (September 2008)
8875 * Bit 12 is undefined in any of the following cases:
8876 * If the VM exit sets the valid bit in the IDT-vectoring
8877 * information field.
8878 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008879 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008880 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8881 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008882 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8883 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008884 else
8885 vmx->nmi_known_unmasked =
8886 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8887 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008888 } else if (unlikely(vmx->soft_vnmi_blocked))
8889 vmx->vnmi_blocked_time +=
8890 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008891}
8892
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008893static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008894 u32 idt_vectoring_info,
8895 int instr_len_field,
8896 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008897{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008898 u8 vector;
8899 int type;
8900 bool idtv_info_valid;
8901
8902 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008903
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008904 vcpu->arch.nmi_injected = false;
8905 kvm_clear_exception_queue(vcpu);
8906 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008907
8908 if (!idtv_info_valid)
8909 return;
8910
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008911 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008912
Avi Kivity668f6122008-07-02 09:28:55 +03008913 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8914 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008915
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008916 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008917 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008918 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008919 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008920 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008921 * Clear bit "block by NMI" before VM entry if a NMI
8922 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008923 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008924 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008925 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008926 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008927 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008928 /* fall through */
8929 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008930 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008931 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008932 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008933 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008934 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008935 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008936 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008937 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008938 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008939 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008940 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008941 break;
8942 default:
8943 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008944 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008945}
8946
Avi Kivity83422e12010-07-20 14:43:23 +03008947static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8948{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008949 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008950 VM_EXIT_INSTRUCTION_LEN,
8951 IDT_VECTORING_ERROR_CODE);
8952}
8953
Avi Kivityb463a6f2010-07-20 15:06:17 +03008954static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8955{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008956 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008957 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8958 VM_ENTRY_INSTRUCTION_LEN,
8959 VM_ENTRY_EXCEPTION_ERROR_CODE);
8960
8961 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8962}
8963
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008964static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8965{
8966 int i, nr_msrs;
8967 struct perf_guest_switch_msr *msrs;
8968
8969 msrs = perf_guest_get_msrs(&nr_msrs);
8970
8971 if (!msrs)
8972 return;
8973
8974 for (i = 0; i < nr_msrs; i++)
8975 if (msrs[i].host == msrs[i].guest)
8976 clear_atomic_switch_msr(vmx, msrs[i].msr);
8977 else
8978 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8979 msrs[i].host);
8980}
8981
Yunhong Jiang64672c92016-06-13 14:19:59 -07008982void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8983{
8984 struct vcpu_vmx *vmx = to_vmx(vcpu);
8985 u64 tscl;
8986 u32 delta_tsc;
8987
8988 if (vmx->hv_deadline_tsc == -1)
8989 return;
8990
8991 tscl = rdtsc();
8992 if (vmx->hv_deadline_tsc > tscl)
8993 /* sure to be 32 bit only because checked on set_hv_timer */
8994 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8995 cpu_preemption_timer_multi);
8996 else
8997 delta_tsc = 0;
8998
8999 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
9000}
9001
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08009002static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009003{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009004 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009005 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02009006
9007 /* Record the guest's net vcpu time for enforced NMI injections. */
9008 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
9009 vmx->entry_time = ktime_get();
9010
9011 /* Don't enter VMX if guest state is invalid, let the exit handler
9012 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02009013 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02009014 return;
9015
Radim Krčmářa7653ec2014-08-21 18:08:07 +02009016 if (vmx->ple_window_dirty) {
9017 vmx->ple_window_dirty = false;
9018 vmcs_write32(PLE_WINDOW, vmx->ple_window);
9019 }
9020
Abel Gordon012f83c2013-04-18 14:39:25 +03009021 if (vmx->nested.sync_shadow_vmcs) {
9022 copy_vmcs12_to_shadow(vmx);
9023 vmx->nested.sync_shadow_vmcs = false;
9024 }
9025
Avi Kivity104f2262010-11-18 13:12:52 +02009026 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
9027 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
9028 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
9029 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
9030
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07009031 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07009032 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
9033 vmcs_writel(HOST_CR4, cr4);
9034 vmx->host_state.vmcs_host_cr4 = cr4;
9035 }
9036
Avi Kivity104f2262010-11-18 13:12:52 +02009037 /* When single-stepping over STI and MOV SS, we must clear the
9038 * corresponding interruptibility bits in the guest state. Otherwise
9039 * vmentry fails as it then expects bit 14 (BS) in pending debug
9040 * exceptions being set, but that's not correct for the guest debugging
9041 * case. */
9042 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
9043 vmx_set_interrupt_shadow(vcpu, 0);
9044
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009045 if (vmx->guest_pkru_valid)
9046 __write_pkru(vmx->guest_pkru);
9047
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009048 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009049 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02009050
Yunhong Jiang64672c92016-06-13 14:19:59 -07009051 vmx_arm_hv_timer(vcpu);
9052
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009053 /*
9054 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
9055 * it's non-zero. Since vmentry is serialising on affected CPUs, there
9056 * is no need to worry about the conditional branch over the wrmsr
9057 * being speculatively taken.
9058 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009059 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009060
Nadav Har'Eld462b812011-05-24 15:26:10 +03009061 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009062
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +02009063 if (static_branch_unlikely(&vmx_l1d_should_flush)) {
9064 if (vcpu->arch.l1tf_flush_l1d)
9065 vmx_l1d_flush(vcpu);
9066 }
9067
Avi Kivity104f2262010-11-18 13:12:52 +02009068 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08009069 /* Store host registers */
Avi Kivityb188c812012-09-16 15:10:58 +03009070 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
9071 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
9072 "push %%" _ASM_CX " \n\t"
9073 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009074 "je 1f \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009075 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009076 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03009077 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009078 /* Reload cr2 if changed */
Avi Kivityb188c812012-09-16 15:10:58 +03009079 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
9080 "mov %%cr2, %%" _ASM_DX " \n\t"
9081 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009082 "je 2f \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009083 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03009084 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009085 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02009086 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009087 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c812012-09-16 15:10:58 +03009088 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
9089 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
9090 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
9091 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
9092 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
9093 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009094#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009095 "mov %c[r8](%0), %%r8 \n\t"
9096 "mov %c[r9](%0), %%r9 \n\t"
9097 "mov %c[r10](%0), %%r10 \n\t"
9098 "mov %c[r11](%0), %%r11 \n\t"
9099 "mov %c[r12](%0), %%r12 \n\t"
9100 "mov %c[r13](%0), %%r13 \n\t"
9101 "mov %c[r14](%0), %%r14 \n\t"
9102 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009103#endif
Avi Kivityb188c812012-09-16 15:10:58 +03009104 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03009105
Avi Kivity6aa8b732006-12-10 02:21:36 -08009106 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03009107 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03009108 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009109 "jmp 2f \n\t"
9110 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
9111 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08009112 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c812012-09-16 15:10:58 +03009113 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02009114 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009115 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009116 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
9117 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
9118 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
9119 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
9120 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
9121 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
9122 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009123#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02009124 "mov %%r8, %c[r8](%0) \n\t"
9125 "mov %%r9, %c[r9](%0) \n\t"
9126 "mov %%r10, %c[r10](%0) \n\t"
9127 "mov %%r11, %c[r11](%0) \n\t"
9128 "mov %%r12, %c[r12](%0) \n\t"
9129 "mov %%r13, %c[r13](%0) \n\t"
9130 "mov %%r14, %c[r14](%0) \n\t"
9131 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009132 "xor %%r8d, %%r8d \n\t"
9133 "xor %%r9d, %%r9d \n\t"
9134 "xor %%r10d, %%r10d \n\t"
9135 "xor %%r11d, %%r11d \n\t"
9136 "xor %%r12d, %%r12d \n\t"
9137 "xor %%r13d, %%r13d \n\t"
9138 "xor %%r14d, %%r14d \n\t"
9139 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009140#endif
Avi Kivityb188c812012-09-16 15:10:58 +03009141 "mov %%cr2, %%" _ASM_AX " \n\t"
9142 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009143
Jim Mattson491c0ca2018-01-03 14:31:38 -08009144 "xor %%eax, %%eax \n\t"
9145 "xor %%ebx, %%ebx \n\t"
9146 "xor %%esi, %%esi \n\t"
9147 "xor %%edi, %%edi \n\t"
Avi Kivityb188c812012-09-16 15:10:58 +03009148 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009149 ".pushsection .rodata \n\t"
9150 ".global vmx_return \n\t"
9151 "vmx_return: " _ASM_PTR " 2b \n\t"
9152 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009153 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009154 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009155 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009156 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009157 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9158 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9159 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9160 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9161 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9162 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9163 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009164#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009165 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9166 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9167 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9168 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9169 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9170 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9171 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9172 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009173#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009174 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9175 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009176 : "cc", "memory"
9177#ifdef CONFIG_X86_64
Avi Kivityb188c812012-09-16 15:10:58 +03009178 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009179 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c812012-09-16 15:10:58 +03009180#else
9181 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009182#endif
9183 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009184
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009185 /*
9186 * We do not use IBRS in the kernel. If this vCPU has used the
9187 * SPEC_CTRL MSR it may have left it on; save the value and
9188 * turn it off. This is much more efficient than blindly adding
9189 * it to the atomic save/restore list. Especially as the former
9190 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9191 *
9192 * For non-nested case:
9193 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9194 * save it.
9195 *
9196 * For nested case:
9197 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9198 * save it.
9199 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009200 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009201 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009202
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009203 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009204
David Woodhousec1ddd992018-01-12 11:11:27 +00009205 /* Eliminate branch target predictions from guest mode */
9206 vmexit_fill_RSB();
9207
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009208 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9209 if (debugctlmsr)
9210 update_debugctlmsr(debugctlmsr);
9211
Avi Kivityaa67f602012-08-01 16:48:03 +03009212#ifndef CONFIG_X86_64
9213 /*
9214 * The sysexit path does not restore ds/es, so we must set them to
9215 * a reasonable value ourselves.
9216 *
9217 * We can't defer this to vmx_load_host_state() since that function
9218 * may be executed in interrupt context, which saves and restore segments
9219 * around it, nullifying its effect.
9220 */
9221 loadsegment(ds, __USER_DS);
9222 loadsegment(es, __USER_DS);
9223#endif
9224
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009225 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009226 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009227 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009228 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009229 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009230 vcpu->arch.regs_dirty = 0;
9231
Avi Kivity1155f762007-11-22 11:30:47 +02009232 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9233
Nadav Har'Eld462b812011-05-24 15:26:10 +03009234 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009235
Avi Kivity51aa01d2010-07-20 14:31:20 +03009236 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009237
Gleb Natapove0b890d2013-09-25 12:51:33 +03009238 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009239 * eager fpu is enabled if PKEY is supported and CR4 is switched
9240 * back on host, so it is safe to read guest PKRU from current
9241 * XSAVE.
9242 */
9243 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9244 vmx->guest_pkru = __read_pkru();
9245 if (vmx->guest_pkru != vmx->host_pkru) {
9246 vmx->guest_pkru_valid = true;
9247 __write_pkru(vmx->host_pkru);
9248 } else
9249 vmx->guest_pkru_valid = false;
9250 }
9251
9252 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009253 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9254 * we did not inject a still-pending event to L1 now because of
9255 * nested_run_pending, we need to re-enable this bit.
9256 */
9257 if (vmx->nested.nested_run_pending)
9258 kvm_make_request(KVM_REQ_EVENT, vcpu);
9259
9260 vmx->nested.nested_run_pending = 0;
9261
Avi Kivity51aa01d2010-07-20 14:31:20 +03009262 vmx_complete_atomic_exit(vmx);
9263 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009264 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009265}
Josh Poimboeuf935893a2017-06-28 10:11:06 -05009266STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009267
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009268static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9269{
9270 struct vcpu_vmx *vmx = to_vmx(vcpu);
9271 int cpu;
9272
9273 if (vmx->loaded_vmcs == &vmx->vmcs01)
9274 return;
9275
9276 cpu = get_cpu();
9277 vmx->loaded_vmcs = &vmx->vmcs01;
9278 vmx_vcpu_put(vcpu);
9279 vmx_vcpu_load(vcpu, cpu);
9280 vcpu->cpu = cpu;
9281 put_cpu();
9282}
9283
Jim Mattson2f1fe812016-07-08 15:36:06 -07009284/*
9285 * Ensure that the current vmcs of the logical processor is the
9286 * vmcs01 of the vcpu before calling free_nested().
9287 */
9288static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9289{
9290 struct vcpu_vmx *vmx = to_vmx(vcpu);
9291 int r;
9292
9293 r = vcpu_load(vcpu);
9294 BUG_ON(r);
9295 vmx_load_vmcs01(vcpu);
9296 free_nested(vmx);
9297 vcpu_put(vcpu);
9298}
9299
Avi Kivity6aa8b732006-12-10 02:21:36 -08009300static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9301{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009302 struct vcpu_vmx *vmx = to_vmx(vcpu);
9303
Kai Huang843e4332015-01-28 10:54:28 +08009304 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009305 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009306 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009307 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009308 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009309 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009310 kfree(vmx->guest_msrs);
9311 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009312 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009313}
9314
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009315static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009316{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009317 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009318 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009319 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009320 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009321
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009322 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009323 return ERR_PTR(-ENOMEM);
9324
Wanpeng Li991e7a02015-09-16 17:30:05 +08009325 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009326
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009327 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9328 if (err)
9329 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009330
Peter Feiner4e595162016-07-07 14:49:58 -07009331 err = -ENOMEM;
9332
9333 /*
9334 * If PML is turned on, failure on enabling PML just results in failure
9335 * of creating the vcpu, therefore we can simplify PML logic (by
9336 * avoiding dealing with cases, such as enabling PML partially on vcpus
9337 * for the guest, etc.
9338 */
9339 if (enable_pml) {
9340 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9341 if (!vmx->pml_pg)
9342 goto uninit_vcpu;
9343 }
9344
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009345 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009346 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9347 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009348
Peter Feiner4e595162016-07-07 14:49:58 -07009349 if (!vmx->guest_msrs)
9350 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009351
Nadav Har'Eld462b812011-05-24 15:26:10 +03009352 if (!vmm_exclusive)
9353 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009354 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009355 if (!vmm_exclusive)
9356 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009357 if (err < 0)
9358 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009359
Paolo Bonzini6236b782018-01-16 16:51:18 +01009360 msr_bitmap = vmx->vmcs01.msr_bitmap;
9361 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9362 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9363 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9364 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9365 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9366 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9367 vmx->msr_bitmap_mode = 0;
9368
Paolo Bonziniff546f92018-01-11 12:16:15 +01009369 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009370 cpu = get_cpu();
9371 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009372 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009373 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009374 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009375 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009376 if (err)
9377 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009378 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009379 err = alloc_apic_access_page(kvm);
9380 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009381 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009382 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009383
Sheng Yangb927a3c2009-07-21 10:42:48 +08009384 if (enable_ept) {
9385 if (!kvm->arch.ept_identity_map_addr)
9386 kvm->arch.ept_identity_map_addr =
9387 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009388 err = init_rmode_identity_map(kvm);
9389 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009390 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009391 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009392
Roman Kagan020a90f2018-07-19 21:59:07 +03009393 if (nested)
Wincy Vanb9c237b2015-02-03 23:56:30 +08009394 nested_vmx_setup_ctls_msrs(vmx);
9395
Wincy Van705699a2015-02-03 23:58:17 +08009396 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009397 vmx->nested.current_vmptr = -1ull;
9398 vmx->nested.current_vmcs12 = NULL;
9399
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009400 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9401
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009402 /*
9403 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9404 * or POSTED_INTR_WAKEUP_VECTOR.
9405 */
9406 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9407 vmx->pi_desc.sn = 1;
9408
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009409 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009410
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009411free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009412 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009413free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009414 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009415free_pml:
9416 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009417uninit_vcpu:
9418 kvm_vcpu_uninit(&vmx->vcpu);
9419free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009420 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009421 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009422 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009423}
9424
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -04009425#define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n"
9426
9427static int vmx_vm_init(struct kvm *kvm)
9428{
9429 if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) {
9430 if (nosmt) {
9431 pr_err(L1TF_MSG);
9432 return -EOPNOTSUPP;
9433 }
9434 pr_warn(L1TF_MSG);
9435 }
9436 return 0;
9437}
9438
Yang, Sheng002c7f72007-07-31 14:23:01 +03009439static void __init vmx_check_processor_compat(void *rtn)
9440{
9441 struct vmcs_config vmcs_conf;
9442
9443 *(int *)rtn = 0;
9444 if (setup_vmcs_config(&vmcs_conf) < 0)
9445 *(int *)rtn = -EIO;
9446 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9447 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9448 smp_processor_id());
9449 *(int *)rtn = -EIO;
9450 }
9451}
9452
Sheng Yang67253af2008-04-25 10:20:22 +08009453static int get_ept_level(void)
9454{
9455 return VMX_EPT_DEFAULT_GAW + 1;
9456}
9457
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009458static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009459{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009460 u8 cache;
9461 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009462
Sheng Yang522c68c2009-04-27 20:35:43 +08009463 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009464 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009465 * 2. EPT with VT-d:
9466 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009467 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009468 * b. VT-d with snooping control feature: snooping control feature of
9469 * VT-d engine can guarantee the cache correctness. Just set it
9470 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009471 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009472 * consistent with host MTRR
9473 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009474 if (is_mmio) {
9475 cache = MTRR_TYPE_UNCACHABLE;
9476 goto exit;
9477 }
9478
9479 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009480 ipat = VMX_EPT_IPAT_BIT;
9481 cache = MTRR_TYPE_WRBACK;
9482 goto exit;
9483 }
9484
9485 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9486 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009487 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009488 cache = MTRR_TYPE_WRBACK;
9489 else
9490 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009491 goto exit;
9492 }
9493
Xiao Guangrongff536042015-06-15 16:55:22 +08009494 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009495
9496exit:
9497 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009498}
9499
Sheng Yang17cc3932010-01-05 19:02:27 +08009500static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009501{
Sheng Yang878403b2010-01-05 19:02:29 +08009502 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9503 return PT_DIRECTORY_LEVEL;
9504 else
9505 /* For shadow and EPT supported 1GB page */
9506 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009507}
9508
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009509static void vmcs_set_secondary_exec_control(u32 new_ctl)
9510{
9511 /*
9512 * These bits in the secondary execution controls field
9513 * are dynamic, the others are mostly based on the hypervisor
9514 * architecture and the guest's CPUID. Do not touch the
9515 * dynamic bits.
9516 */
9517 u32 mask =
9518 SECONDARY_EXEC_SHADOW_VMCS |
9519 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9520 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9521
9522 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9523
9524 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9525 (new_ctl & ~mask) | (cur_ctl & mask));
9526}
9527
Sheng Yang0e851882009-12-18 16:48:46 +08009528static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9529{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009530 struct kvm_cpuid_entry2 *best;
9531 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009532 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009533
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009534 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009535 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9536 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009537 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009538
Paolo Bonzini8b972652015-09-15 17:34:42 +02009539 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009540 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009541 vmx->nested.nested_vmx_secondary_ctls_high |=
9542 SECONDARY_EXEC_RDTSCP;
9543 else
9544 vmx->nested.nested_vmx_secondary_ctls_high &=
9545 ~SECONDARY_EXEC_RDTSCP;
9546 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009547 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009548
Mao, Junjiead756a12012-07-02 01:18:48 +00009549 /* Exposing INVPCID only when PCID is exposed */
9550 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9551 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009552 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9553 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009554 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009555
Mao, Junjiead756a12012-07-02 01:18:48 +00009556 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009557 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009558 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009559
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009560 if (cpu_has_secondary_exec_ctrls())
9561 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009562
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009563 if (nested_vmx_allowed(vcpu))
9564 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9565 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9566 else
9567 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9568 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009569}
9570
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009571static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9572{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009573 if (func == 1 && nested)
9574 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009575}
9576
Yang Zhang25d92082013-08-06 12:00:32 +03009577static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9578 struct x86_exception *fault)
9579{
Jan Kiszka533558b2014-01-04 18:47:20 +01009580 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9581 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009582
9583 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009584 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009585 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009586 exit_reason = EXIT_REASON_EPT_VIOLATION;
9587 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009588 vmcs12->guest_physical_address = fault->address;
9589}
9590
Nadav Har'El155a97a2013-08-05 11:07:16 +03009591/* Callbacks for nested_ept_init_mmu_context: */
9592
9593static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9594{
9595 /* return the page table to be shadowed - in our case, EPT12 */
9596 return get_vmcs12(vcpu)->ept_pointer;
9597}
9598
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009599static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009600{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009601 WARN_ON(mmu_is_nested(vcpu));
9602 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009603 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9604 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009605 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9606 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9607 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9608
9609 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009610}
9611
9612static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9613{
9614 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9615}
9616
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009617static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9618 u16 error_code)
9619{
9620 bool inequality, bit;
9621
9622 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9623 inequality =
9624 (error_code & vmcs12->page_fault_error_code_mask) !=
9625 vmcs12->page_fault_error_code_match;
9626 return inequality ^ bit;
9627}
9628
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009629static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9630 struct x86_exception *fault)
9631{
9632 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9633
9634 WARN_ON(!is_guest_mode(vcpu));
9635
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009636 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009637 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9638 vmcs_read32(VM_EXIT_INTR_INFO),
9639 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009640 else
9641 kvm_inject_page_fault(vcpu, fault);
9642}
9643
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009644static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9645 struct vmcs12 *vmcs12)
9646{
9647 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009648 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009649
9650 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009651 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9652 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009653 return false;
9654
9655 /*
9656 * Translate L1 physical address to host physical
9657 * address for vmcs02. Keep the page pinned, so this
9658 * physical address remains valid. We keep a reference
9659 * to it so we can release it later.
9660 */
9661 if (vmx->nested.apic_access_page) /* shouldn't happen */
9662 nested_release_page(vmx->nested.apic_access_page);
9663 vmx->nested.apic_access_page =
9664 nested_get_page(vcpu, vmcs12->apic_access_addr);
9665 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009666
9667 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009668 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9669 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009670 return false;
9671
9672 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9673 nested_release_page(vmx->nested.virtual_apic_page);
9674 vmx->nested.virtual_apic_page =
9675 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9676
9677 /*
9678 * Failing the vm entry is _not_ what the processor does
9679 * but it's basically the only possibility we have.
9680 * We could still enter the guest if CR8 load exits are
9681 * enabled, CR8 store exits are enabled, and virtualize APIC
9682 * access is disabled; in this case the processor would never
9683 * use the TPR shadow and we could simply clear the bit from
9684 * the execution control. But such a configuration is useless,
9685 * so let's keep the code simple.
9686 */
9687 if (!vmx->nested.virtual_apic_page)
9688 return false;
9689 }
9690
Wincy Van705699a2015-02-03 23:58:17 +08009691 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009692 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9693 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009694 return false;
9695
9696 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9697 kunmap(vmx->nested.pi_desc_page);
9698 nested_release_page(vmx->nested.pi_desc_page);
9699 }
9700 vmx->nested.pi_desc_page =
9701 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9702 if (!vmx->nested.pi_desc_page)
9703 return false;
9704
9705 vmx->nested.pi_desc =
9706 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9707 if (!vmx->nested.pi_desc) {
9708 nested_release_page_clean(vmx->nested.pi_desc_page);
9709 return false;
9710 }
9711 vmx->nested.pi_desc =
9712 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9713 (unsigned long)(vmcs12->posted_intr_desc_addr &
9714 (PAGE_SIZE - 1)));
9715 }
9716
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009717 return true;
9718}
9719
Jan Kiszkaf4124502014-03-07 20:03:13 +01009720static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9721{
9722 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9723 struct vcpu_vmx *vmx = to_vmx(vcpu);
9724
9725 if (vcpu->arch.virtual_tsc_khz == 0)
9726 return;
9727
9728 /* Make sure short timeouts reliably trigger an immediate vmexit.
9729 * hrtimer_start does not guarantee this. */
9730 if (preemption_timeout <= 1) {
9731 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9732 return;
9733 }
9734
9735 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9736 preemption_timeout *= 1000000;
9737 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9738 hrtimer_start(&vmx->nested.preemption_timer,
9739 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9740}
9741
Wincy Van3af18d92015-02-03 23:49:31 +08009742static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9743 struct vmcs12 *vmcs12)
9744{
9745 int maxphyaddr;
9746 u64 addr;
9747
9748 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9749 return 0;
9750
9751 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9752 WARN_ON(1);
9753 return -EINVAL;
9754 }
9755 maxphyaddr = cpuid_maxphyaddr(vcpu);
9756
9757 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9758 ((addr + PAGE_SIZE) >> maxphyaddr))
9759 return -EINVAL;
9760
9761 return 0;
9762}
9763
9764/*
9765 * Merge L0's and L1's MSR bitmap, return false to indicate that
9766 * we do not use the hardware.
9767 */
9768static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9769 struct vmcs12 *vmcs12)
9770{
Wincy Van82f0dd42015-02-03 23:57:18 +08009771 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009772 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009773 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009774 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009775 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009776 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009777 *
9778 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9779 * ensures that we do not accidentally generate an L02 MSR bitmap
9780 * from the L12 MSR bitmap that is too permissive.
9781 * 2. That L1 or L2s have actually used the MSR. This avoids
9782 * unnecessarily merging of the bitmap if the MSR is unused. This
9783 * works properly because we only update the L01 MSR bitmap lazily.
9784 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9785 * updated to reflect this when L1 (or its L2s) actually write to
9786 * the MSR.
9787 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009788 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9789 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009790
Ashok Raj70131292018-02-01 22:59:43 +01009791 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009792 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009793 return false;
9794
9795 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009796 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009797 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009798 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009799
Radim Krčmářd048c092016-08-08 20:16:22 +02009800 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9801
Wincy Vanf2b93282015-02-03 23:56:03 +08009802 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009803 if (nested_cpu_has_apic_reg_virt(vmcs12))
9804 for (msr = 0x800; msr <= 0x8ff; msr++)
9805 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009806 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009807 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009808
9809 nested_vmx_disable_intercept_for_msr(
9810 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009811 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9812 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009813
Wincy Van608406e2015-02-03 23:57:51 +08009814 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009815 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009816 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009817 APIC_BASE_MSR + (APIC_EOI >> 4),
9818 MSR_TYPE_W);
9819 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009820 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009821 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9822 MSR_TYPE_W);
9823 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009824 }
Ashok Raj70131292018-02-01 22:59:43 +01009825
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009826 if (spec_ctrl)
9827 nested_vmx_disable_intercept_for_msr(
9828 msr_bitmap_l1, msr_bitmap_l0,
9829 MSR_IA32_SPEC_CTRL,
9830 MSR_TYPE_R | MSR_TYPE_W);
9831
Ashok Raj70131292018-02-01 22:59:43 +01009832 if (pred_cmd)
9833 nested_vmx_disable_intercept_for_msr(
9834 msr_bitmap_l1, msr_bitmap_l0,
9835 MSR_IA32_PRED_CMD,
9836 MSR_TYPE_W);
9837
Wincy Vanf2b93282015-02-03 23:56:03 +08009838 kunmap(page);
9839 nested_release_page_clean(page);
9840
9841 return true;
9842}
9843
9844static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9845 struct vmcs12 *vmcs12)
9846{
Wincy Van82f0dd42015-02-03 23:57:18 +08009847 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009848 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009849 !nested_cpu_has_vid(vmcs12) &&
9850 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009851 return 0;
9852
9853 /*
9854 * If virtualize x2apic mode is enabled,
9855 * virtualize apic access must be disabled.
9856 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009857 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9858 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009859 return -EINVAL;
9860
Wincy Van608406e2015-02-03 23:57:51 +08009861 /*
9862 * If virtual interrupt delivery is enabled,
9863 * we must exit on external interrupts.
9864 */
9865 if (nested_cpu_has_vid(vmcs12) &&
9866 !nested_exit_on_intr(vcpu))
9867 return -EINVAL;
9868
Wincy Van705699a2015-02-03 23:58:17 +08009869 /*
9870 * bits 15:8 should be zero in posted_intr_nv,
9871 * the descriptor address has been already checked
9872 * in nested_get_vmcs12_pages.
9873 */
9874 if (nested_cpu_has_posted_intr(vmcs12) &&
9875 (!nested_cpu_has_vid(vmcs12) ||
9876 !nested_exit_intr_ack_set(vcpu) ||
9877 vmcs12->posted_intr_nv & 0xff00))
9878 return -EINVAL;
9879
Wincy Vanf2b93282015-02-03 23:56:03 +08009880 /* tpr shadow is needed by all apicv features. */
9881 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9882 return -EINVAL;
9883
9884 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009885}
9886
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009887static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9888 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009889 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009890{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009891 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009892 u64 count, addr;
9893
9894 if (vmcs12_read_any(vcpu, count_field, &count) ||
9895 vmcs12_read_any(vcpu, addr_field, &addr)) {
9896 WARN_ON(1);
9897 return -EINVAL;
9898 }
9899 if (count == 0)
9900 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009901 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009902 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9903 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009904 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009905 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9906 addr_field, maxphyaddr, count, addr);
9907 return -EINVAL;
9908 }
9909 return 0;
9910}
9911
9912static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9913 struct vmcs12 *vmcs12)
9914{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009915 if (vmcs12->vm_exit_msr_load_count == 0 &&
9916 vmcs12->vm_exit_msr_store_count == 0 &&
9917 vmcs12->vm_entry_msr_load_count == 0)
9918 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009919 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009920 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009921 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009922 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009923 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009924 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009925 return -EINVAL;
9926 return 0;
9927}
9928
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009929static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9930 struct vmx_msr_entry *e)
9931{
9932 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009933 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009934 return -EINVAL;
9935 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9936 e->index == MSR_IA32_UCODE_REV)
9937 return -EINVAL;
9938 if (e->reserved != 0)
9939 return -EINVAL;
9940 return 0;
9941}
9942
9943static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9944 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009945{
9946 if (e->index == MSR_FS_BASE ||
9947 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009948 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9949 nested_vmx_msr_check_common(vcpu, e))
9950 return -EINVAL;
9951 return 0;
9952}
9953
9954static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9955 struct vmx_msr_entry *e)
9956{
9957 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9958 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009959 return -EINVAL;
9960 return 0;
9961}
9962
9963/*
9964 * Load guest's/host's msr at nested entry/exit.
9965 * return 0 for success, entry index for failure.
9966 */
9967static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9968{
9969 u32 i;
9970 struct vmx_msr_entry e;
9971 struct msr_data msr;
9972
9973 msr.host_initiated = false;
9974 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009975 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9976 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009977 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009978 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9979 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009980 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009981 }
9982 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009983 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009984 "%s check failed (%u, 0x%x, 0x%x)\n",
9985 __func__, i, e.index, e.reserved);
9986 goto fail;
9987 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009988 msr.index = e.index;
9989 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009990 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009991 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009992 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9993 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009994 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009995 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009996 }
9997 return 0;
9998fail:
9999 return i + 1;
10000}
10001
10002static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
10003{
10004 u32 i;
10005 struct vmx_msr_entry e;
10006
10007 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010008 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010009 if (kvm_vcpu_read_guest(vcpu,
10010 gpa + i * sizeof(e),
10011 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010012 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010013 "%s cannot read MSR entry (%u, 0x%08llx)\n",
10014 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030010015 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010016 }
10017 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010018 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010019 "%s check failed (%u, 0x%x, 0x%x)\n",
10020 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030010021 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010022 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010023 msr_info.host_initiated = false;
10024 msr_info.index = e.index;
10025 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010026 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010027 "%s cannot read MSR (%u, 0x%x)\n",
10028 __func__, i, e.index);
10029 return -EINVAL;
10030 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010031 if (kvm_vcpu_write_guest(vcpu,
10032 gpa + i * sizeof(e) +
10033 offsetof(struct vmx_msr_entry, value),
10034 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020010035 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010036 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020010037 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010038 return -EINVAL;
10039 }
Wincy Vanff651cb2014-12-11 08:52:58 +030010040 }
10041 return 0;
10042}
10043
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010044/*
10045 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
10046 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080010047 * 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 +030010048 * guest in a way that will both be appropriate to L1's requests, and our
10049 * needs. In addition to modifying the active vmcs (which is vmcs02), this
10050 * function also has additional necessary side-effects, like setting various
10051 * vcpu->arch fields.
10052 */
10053static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10054{
10055 struct vcpu_vmx *vmx = to_vmx(vcpu);
10056 u32 exec_control;
10057
10058 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
10059 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
10060 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
10061 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
10062 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
10063 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
10064 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
10065 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
10066 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
10067 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
10068 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
10069 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
10070 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
10071 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
10072 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
10073 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
10074 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
10075 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
10076 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
10077 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
10078 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
10079 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
10080 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
10081 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
10082 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
10083 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
10084 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
10085 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
10086 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
10087 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
10088 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
10089 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
10090 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
10091 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
10092 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
10093 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
10094
Jan Kiszka2996fca2014-06-16 13:59:43 +020010095 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
10096 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
10097 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
10098 } else {
10099 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
10100 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
10101 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010102 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
10103 vmcs12->vm_entry_intr_info_field);
10104 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
10105 vmcs12->vm_entry_exception_error_code);
10106 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
10107 vmcs12->vm_entry_instruction_len);
10108 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
10109 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010110 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +030010111 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010112 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
10113 vmcs12->guest_pending_dbg_exceptions);
10114 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
10115 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
10116
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010117 if (nested_cpu_has_xsaves(vmcs12))
10118 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010119 vmcs_write64(VMCS_LINK_POINTER, -1ull);
10120
Jan Kiszkaf4124502014-03-07 20:03:13 +010010121 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080010122
Paolo Bonzini93140062016-07-06 13:23:51 +020010123 /* Preemption timer setting is only taken from vmcs01. */
10124 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10125 exec_control |= vmcs_config.pin_based_exec_ctrl;
10126 if (vmx->hv_deadline_tsc == -1)
10127 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10128
10129 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010130 if (nested_cpu_has_posted_intr(vmcs12)) {
10131 /*
10132 * Note that we use L0's vector here and in
10133 * vmx_deliver_nested_posted_interrupt.
10134 */
10135 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10136 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010137 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010138 vmcs_write64(POSTED_INTR_DESC_ADDR,
10139 page_to_phys(vmx->nested.pi_desc_page) +
10140 (unsigned long)(vmcs12->posted_intr_desc_addr &
10141 (PAGE_SIZE - 1)));
10142 } else
10143 exec_control &= ~PIN_BASED_POSTED_INTR;
10144
Jan Kiszkaf4124502014-03-07 20:03:13 +010010145 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010146
Jan Kiszkaf4124502014-03-07 20:03:13 +010010147 vmx->nested.preemption_timer_expired = false;
10148 if (nested_cpu_has_preemption_timer(vmcs12))
10149 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010150
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010151 /*
10152 * Whether page-faults are trapped is determined by a combination of
10153 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10154 * If enable_ept, L0 doesn't care about page faults and we should
10155 * set all of these to L1's desires. However, if !enable_ept, L0 does
10156 * care about (at least some) page faults, and because it is not easy
10157 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10158 * to exit on each and every L2 page fault. This is done by setting
10159 * MASK=MATCH=0 and (see below) EB.PF=1.
10160 * Note that below we don't need special code to set EB.PF beyond the
10161 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10162 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10163 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10164 *
10165 * A problem with this approach (when !enable_ept) is that L1 may be
10166 * injected with more page faults than it asked for. This could have
10167 * caused problems, but in practice existing hypervisors don't care.
10168 * To fix this, we will need to emulate the PFEC checking (on the L1
10169 * page tables), using walk_addr(), when injecting PFs to L1.
10170 */
10171 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10172 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10173 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10174 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10175
10176 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010177 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010178
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010179 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010180 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010181 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010182 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010183 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010184 if (nested_cpu_has(vmcs12,
10185 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10186 exec_control |= vmcs12->secondary_vm_exec_control;
10187
10188 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10189 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010190 * If translation failed, no matter: This feature asks
10191 * to exit when accessing the given address, and if it
10192 * can never be accessed, this feature won't do
10193 * anything anyway.
10194 */
10195 if (!vmx->nested.apic_access_page)
10196 exec_control &=
10197 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10198 else
10199 vmcs_write64(APIC_ACCESS_ADDR,
10200 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010201 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010202 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010203 exec_control |=
10204 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010205 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010206 }
10207
Wincy Van608406e2015-02-03 23:57:51 +080010208 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10209 vmcs_write64(EOI_EXIT_BITMAP0,
10210 vmcs12->eoi_exit_bitmap0);
10211 vmcs_write64(EOI_EXIT_BITMAP1,
10212 vmcs12->eoi_exit_bitmap1);
10213 vmcs_write64(EOI_EXIT_BITMAP2,
10214 vmcs12->eoi_exit_bitmap2);
10215 vmcs_write64(EOI_EXIT_BITMAP3,
10216 vmcs12->eoi_exit_bitmap3);
10217 vmcs_write16(GUEST_INTR_STATUS,
10218 vmcs12->guest_intr_status);
10219 }
10220
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010221 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10222 }
10223
10224
10225 /*
10226 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10227 * Some constant fields are set here by vmx_set_constant_host_state().
10228 * Other fields are different per CPU, and will be set later when
10229 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10230 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010231 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010232
10233 /*
Jim Mattson69c25252016-10-04 10:48:38 -070010234 * Set the MSR load/store lists to match L0's settings.
10235 */
10236 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040010237 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
10238 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
10239 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
10240 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Jim Mattson69c25252016-10-04 10:48:38 -070010241
10242 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010243 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10244 * entry, but only if the current (host) sp changed from the value
10245 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10246 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10247 * here we just force the write to happen on entry.
10248 */
10249 vmx->host_rsp = 0;
10250
10251 exec_control = vmx_exec_control(vmx); /* L0's desires */
10252 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10253 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10254 exec_control &= ~CPU_BASED_TPR_SHADOW;
10255 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010256
10257 if (exec_control & CPU_BASED_TPR_SHADOW) {
10258 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10259 page_to_phys(vmx->nested.virtual_apic_page));
10260 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010261 } else {
10262#ifdef CONFIG_X86_64
10263 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10264 CPU_BASED_CR8_STORE_EXITING;
10265#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010266 }
10267
Wincy Van3af18d92015-02-03 23:49:31 +080010268 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010269 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10270 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10271 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10272 else
Wincy Van3af18d92015-02-03 23:49:31 +080010273 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10274
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010275 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010276 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010277 * Rather, exit every time.
10278 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010279 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10280 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10281
10282 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10283
10284 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10285 * bitwise-or of what L1 wants to trap for L2, and what we want to
10286 * trap. Note that CR0.TS also needs updating - we do this later.
10287 */
10288 update_exception_bitmap(vcpu);
10289 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10290 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10291
Nadav Har'El8049d652013-08-05 11:07:06 +030010292 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10293 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10294 * bits are further modified by vmx_set_efer() below.
10295 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010296 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010297
10298 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10299 * emulated by vmx_set_efer(), below.
10300 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010301 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010302 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10303 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010304 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10305
Jan Kiszka44811c02013-08-04 17:17:27 +020010306 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010307 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010308 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10309 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010310 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10311
10312
10313 set_cr4_guest_host_mask(vmx);
10314
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010315 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10316 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10317
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010318 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10319 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010320 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010321 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010322 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010323 if (kvm_has_tsc_control)
10324 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010325
Paolo Bonzini6236b782018-01-16 16:51:18 +010010326 if (cpu_has_vmx_msr_bitmap())
10327 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10328
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010329 if (enable_vpid) {
10330 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010331 * There is no direct mapping between vpid02 and vpid12, the
10332 * vpid02 is per-vCPU for L0 and reused while the value of
10333 * vpid12 is changed w/ one invvpid during nested vmentry.
10334 * The vpid12 is allocated by L1 for L2, so it will not
10335 * influence global bitmap(for vpid01 and vpid02 allocation)
10336 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010337 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010338 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10339 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10340 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10341 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10342 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10343 }
10344 } else {
10345 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10346 vmx_flush_tlb(vcpu);
10347 }
10348
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010349 }
10350
Ladi Prosek560a9792017-04-04 14:18:53 +020010351 if (enable_pml) {
10352 /*
10353 * Conceptually we want to copy the PML address and index from
10354 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10355 * since we always flush the log on each vmexit, this happens
10356 * to be equivalent to simply resetting the fields in vmcs02.
10357 */
10358 ASSERT(vmx->pml_pg);
10359 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10360 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10361 }
10362
Nadav Har'El155a97a2013-08-05 11:07:16 +030010363 if (nested_cpu_has_ept(vmcs12)) {
10364 kvm_mmu_unload(vcpu);
10365 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010366 } else if (nested_cpu_has2(vmcs12,
10367 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10368 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010369 }
10370
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010371 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10372 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010373 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010374 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10375 else
10376 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10377 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10378 vmx_set_efer(vcpu, vcpu->arch.efer);
10379
10380 /*
10381 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10382 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10383 * The CR0_READ_SHADOW is what L2 should have expected to read given
10384 * the specifications by L1; It's not enough to take
10385 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10386 * have more bits than L1 expected.
10387 */
10388 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10389 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10390
10391 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10392 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10393
10394 /* shadow page tables on either EPT or shadow page tables */
10395 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10396 kvm_mmu_reset_context(vcpu);
10397
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010398 if (!enable_ept)
10399 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10400
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010401 /*
10402 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10403 */
10404 if (enable_ept) {
10405 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10406 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10407 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10408 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10409 }
10410
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010411 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10412 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10413}
10414
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010415/*
10416 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10417 * for running an L2 nested guest.
10418 */
10419static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10420{
10421 struct vmcs12 *vmcs12;
10422 struct vcpu_vmx *vmx = to_vmx(vcpu);
10423 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010424 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010425 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010426
10427 if (!nested_vmx_check_permission(vcpu) ||
10428 !nested_vmx_check_vmcs12(vcpu))
10429 return 1;
10430
10431 skip_emulated_instruction(vcpu);
10432 vmcs12 = get_vmcs12(vcpu);
10433
Abel Gordon012f83c2013-04-18 14:39:25 +030010434 if (enable_shadow_vmcs)
10435 copy_shadow_to_vmcs12(vmx);
10436
Nadav Har'El7c177932011-05-25 23:12:04 +030010437 /*
10438 * The nested entry process starts with enforcing various prerequisites
10439 * on vmcs12 as required by the Intel SDM, and act appropriately when
10440 * they fail: As the SDM explains, some conditions should cause the
10441 * instruction to fail, while others will cause the instruction to seem
10442 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10443 * To speed up the normal (success) code path, we should avoid checking
10444 * for misconfigurations which will anyway be caught by the processor
10445 * when using the merged vmcs02.
10446 */
10447 if (vmcs12->launch_state == launch) {
10448 nested_vmx_failValid(vcpu,
10449 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10450 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10451 return 1;
10452 }
10453
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010454 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10455 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010456 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10457 return 1;
10458 }
10459
Wincy Van3af18d92015-02-03 23:49:31 +080010460 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010461 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10462 return 1;
10463 }
10464
Wincy Van3af18d92015-02-03 23:49:31 +080010465 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010466 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10467 return 1;
10468 }
10469
Wincy Vanf2b93282015-02-03 23:56:03 +080010470 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10471 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10472 return 1;
10473 }
10474
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010475 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10476 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10477 return 1;
10478 }
10479
Nadav Har'El7c177932011-05-25 23:12:04 +030010480 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010481 vmx->nested.nested_vmx_true_procbased_ctls_low,
10482 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010483 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010484 vmx->nested.nested_vmx_secondary_ctls_low,
10485 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010486 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010487 vmx->nested.nested_vmx_pinbased_ctls_low,
10488 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010489 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010490 vmx->nested.nested_vmx_true_exit_ctls_low,
10491 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010492 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010493 vmx->nested.nested_vmx_true_entry_ctls_low,
10494 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010495 {
10496 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10497 return 1;
10498 }
10499
10500 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10501 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10502 nested_vmx_failValid(vcpu,
10503 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10504 return 1;
10505 }
10506
Wincy Vanb9c237b2015-02-03 23:56:30 +080010507 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010508 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10509 nested_vmx_entry_failure(vcpu, vmcs12,
10510 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10511 return 1;
10512 }
10513 if (vmcs12->vmcs_link_pointer != -1ull) {
10514 nested_vmx_entry_failure(vcpu, vmcs12,
10515 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10516 return 1;
10517 }
10518
10519 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010520 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010521 * are performed on the field for the IA32_EFER MSR:
10522 * - Bits reserved in the IA32_EFER MSR must be 0.
10523 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10524 * the IA-32e mode guest VM-exit control. It must also be identical
10525 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10526 * CR0.PG) is 1.
10527 */
10528 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10529 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10530 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10531 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10532 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10533 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10534 nested_vmx_entry_failure(vcpu, vmcs12,
10535 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10536 return 1;
10537 }
10538 }
10539
10540 /*
10541 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10542 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10543 * the values of the LMA and LME bits in the field must each be that of
10544 * the host address-space size VM-exit control.
10545 */
10546 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10547 ia32e = (vmcs12->vm_exit_controls &
10548 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10549 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10550 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10551 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10552 nested_vmx_entry_failure(vcpu, vmcs12,
10553 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10554 return 1;
10555 }
10556 }
10557
10558 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010559 * We're finally done with prerequisite checking, and can start with
10560 * the nested entry.
10561 */
10562
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010563 enter_guest_mode(vcpu);
10564
Jan Kiszka2996fca2014-06-16 13:59:43 +020010565 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10566 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10567
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010568 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010569 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010570 vmx_vcpu_put(vcpu);
10571 vmx_vcpu_load(vcpu, cpu);
10572 vcpu->cpu = cpu;
10573 put_cpu();
10574
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010575 vmx_segment_cache_clear(vmx);
10576
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010577 prepare_vmcs02(vcpu, vmcs12);
10578
Wincy Vanff651cb2014-12-11 08:52:58 +030010579 msr_entry_idx = nested_vmx_load_msr(vcpu,
10580 vmcs12->vm_entry_msr_load_addr,
10581 vmcs12->vm_entry_msr_load_count);
10582 if (msr_entry_idx) {
10583 leave_guest_mode(vcpu);
10584 vmx_load_vmcs01(vcpu);
10585 nested_vmx_entry_failure(vcpu, vmcs12,
10586 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10587 return 1;
10588 }
10589
10590 vmcs12->launch_state = 1;
10591
Paolo Bonzinib3dc63c2018-07-02 13:07:14 +020010592 /* Hide L1D cache contents from the nested guest. */
10593 vmx->vcpu.arch.l1tf_flush_l1d = true;
10594
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010595 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010596 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010597
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010598 vmx->nested.nested_run_pending = 1;
10599
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010600 /*
10601 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10602 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10603 * returned as far as L1 is concerned. It will only return (and set
10604 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10605 */
10606 return 1;
10607}
10608
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010609/*
10610 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10611 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10612 * This function returns the new value we should put in vmcs12.guest_cr0.
10613 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10614 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10615 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10616 * didn't trap the bit, because if L1 did, so would L0).
10617 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10618 * been modified by L2, and L1 knows it. So just leave the old value of
10619 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10620 * isn't relevant, because if L0 traps this bit it can set it to anything.
10621 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10622 * changed these bits, and therefore they need to be updated, but L0
10623 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10624 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10625 */
10626static inline unsigned long
10627vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10628{
10629 return
10630 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10631 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10632 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10633 vcpu->arch.cr0_guest_owned_bits));
10634}
10635
10636static inline unsigned long
10637vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10638{
10639 return
10640 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10641 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10642 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10643 vcpu->arch.cr4_guest_owned_bits));
10644}
10645
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010646static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10647 struct vmcs12 *vmcs12)
10648{
10649 u32 idt_vectoring;
10650 unsigned int nr;
10651
Gleb Natapov851eb6672013-09-25 12:51:34 +030010652 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010653 nr = vcpu->arch.exception.nr;
10654 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10655
10656 if (kvm_exception_is_soft(nr)) {
10657 vmcs12->vm_exit_instruction_len =
10658 vcpu->arch.event_exit_inst_len;
10659 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10660 } else
10661 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10662
10663 if (vcpu->arch.exception.has_error_code) {
10664 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10665 vmcs12->idt_vectoring_error_code =
10666 vcpu->arch.exception.error_code;
10667 }
10668
10669 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010670 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010671 vmcs12->idt_vectoring_info_field =
10672 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10673 } else if (vcpu->arch.interrupt.pending) {
10674 nr = vcpu->arch.interrupt.nr;
10675 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10676
10677 if (vcpu->arch.interrupt.soft) {
10678 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10679 vmcs12->vm_entry_instruction_len =
10680 vcpu->arch.event_exit_inst_len;
10681 } else
10682 idt_vectoring |= INTR_TYPE_EXT_INTR;
10683
10684 vmcs12->idt_vectoring_info_field = idt_vectoring;
10685 }
10686}
10687
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010688static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10689{
10690 struct vcpu_vmx *vmx = to_vmx(vcpu);
10691
Jan Kiszkaf4124502014-03-07 20:03:13 +010010692 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10693 vmx->nested.preemption_timer_expired) {
10694 if (vmx->nested.nested_run_pending)
10695 return -EBUSY;
10696 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10697 return 0;
10698 }
10699
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010700 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010701 if (vmx->nested.nested_run_pending ||
10702 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010703 return -EBUSY;
10704 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10705 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10706 INTR_INFO_VALID_MASK, 0);
10707 /*
10708 * The NMI-triggered VM exit counts as injection:
10709 * clear this one and block further NMIs.
10710 */
10711 vcpu->arch.nmi_pending = 0;
10712 vmx_set_nmi_mask(vcpu, true);
10713 return 0;
10714 }
10715
10716 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10717 nested_exit_on_intr(vcpu)) {
10718 if (vmx->nested.nested_run_pending)
10719 return -EBUSY;
10720 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010721 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010722 }
10723
David Hildenbrand1edccf22017-01-25 11:58:58 +010010724 vmx_complete_nested_posted_interrupt(vcpu);
10725 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010726}
10727
Jan Kiszkaf4124502014-03-07 20:03:13 +010010728static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10729{
10730 ktime_t remaining =
10731 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10732 u64 value;
10733
10734 if (ktime_to_ns(remaining) <= 0)
10735 return 0;
10736
10737 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10738 do_div(value, 1000000);
10739 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10740}
10741
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010742/*
10743 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10744 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10745 * and this function updates it to reflect the changes to the guest state while
10746 * L2 was running (and perhaps made some exits which were handled directly by L0
10747 * without going back to L1), and to reflect the exit reason.
10748 * Note that we do not have to copy here all VMCS fields, just those that
10749 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10750 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10751 * which already writes to vmcs12 directly.
10752 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010753static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10754 u32 exit_reason, u32 exit_intr_info,
10755 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010756{
10757 /* update guest state fields: */
10758 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10759 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10760
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010761 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10762 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10763 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10764
10765 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10766 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10767 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10768 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10769 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10770 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10771 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10772 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10773 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10774 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10775 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10776 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10777 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10778 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10779 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10780 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10781 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10782 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10783 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10784 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10785 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10786 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10787 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10788 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10789 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10790 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10791 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10792 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10793 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10794 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10795 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10796 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10797 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10798 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10799 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10800 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10801
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010802 vmcs12->guest_interruptibility_info =
10803 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10804 vmcs12->guest_pending_dbg_exceptions =
10805 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010806 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10807 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10808 else
10809 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010810
Jan Kiszkaf4124502014-03-07 20:03:13 +010010811 if (nested_cpu_has_preemption_timer(vmcs12)) {
10812 if (vmcs12->vm_exit_controls &
10813 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10814 vmcs12->vmx_preemption_timer_value =
10815 vmx_get_preemption_timer_value(vcpu);
10816 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10817 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010818
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010819 /*
10820 * In some cases (usually, nested EPT), L2 is allowed to change its
10821 * own CR3 without exiting. If it has changed it, we must keep it.
10822 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10823 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10824 *
10825 * Additionally, restore L2's PDPTR to vmcs12.
10826 */
10827 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010828 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010829 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10830 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10831 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10832 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10833 }
10834
Jim Mattson4933e9f2017-06-01 12:44:46 -070010835 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010836
Wincy Van608406e2015-02-03 23:57:51 +080010837 if (nested_cpu_has_vid(vmcs12))
10838 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10839
Jan Kiszkac18911a2013-03-13 16:06:41 +010010840 vmcs12->vm_entry_controls =
10841 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010842 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010843
Jan Kiszka2996fca2014-06-16 13:59:43 +020010844 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10845 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10846 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10847 }
10848
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010849 /* TODO: These cannot have changed unless we have MSR bitmaps and
10850 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010851 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010852 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010853 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10854 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010855 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10856 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10857 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010858 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010859 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010860 if (nested_cpu_has_xsaves(vmcs12))
10861 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010862
10863 /* update exit information fields: */
10864
Jan Kiszka533558b2014-01-04 18:47:20 +010010865 vmcs12->vm_exit_reason = exit_reason;
10866 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010867
Jan Kiszka533558b2014-01-04 18:47:20 +010010868 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010869 if ((vmcs12->vm_exit_intr_info &
10870 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10871 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10872 vmcs12->vm_exit_intr_error_code =
10873 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010874 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010875 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10876 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10877
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010878 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10879 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10880 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010881 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010882
10883 /*
10884 * Transfer the event that L0 or L1 may wanted to inject into
10885 * L2 to IDT_VECTORING_INFO_FIELD.
10886 */
10887 vmcs12_save_pending_event(vcpu, vmcs12);
10888 }
10889
10890 /*
10891 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10892 * preserved above and would only end up incorrectly in L1.
10893 */
10894 vcpu->arch.nmi_injected = false;
10895 kvm_clear_exception_queue(vcpu);
10896 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010897}
10898
10899/*
10900 * A part of what we need to when the nested L2 guest exits and we want to
10901 * run its L1 parent, is to reset L1's guest state to the host state specified
10902 * in vmcs12.
10903 * This function is to be called not only on normal nested exit, but also on
10904 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10905 * Failures During or After Loading Guest State").
10906 * This function should be called when the active VMCS is L1's (vmcs01).
10907 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010908static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10909 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010910{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010911 struct kvm_segment seg;
10912
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010913 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10914 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010915 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010916 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10917 else
10918 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10919 vmx_set_efer(vcpu, vcpu->arch.efer);
10920
10921 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10922 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010923 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010924 /*
10925 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10926 * actually changed, because it depends on the current state of
10927 * fpu_active (which may have changed).
10928 * Note that vmx_set_cr0 refers to efer set above.
10929 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010930 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010931 /*
10932 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10933 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10934 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10935 */
10936 update_exception_bitmap(vcpu);
10937 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10938 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10939
10940 /*
10941 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10942 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10943 */
10944 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080010945 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010946
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010947 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010948
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010949 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10950 kvm_mmu_reset_context(vcpu);
10951
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010952 if (!enable_ept)
10953 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10954
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010955 if (enable_vpid) {
10956 /*
10957 * Trivially support vpid by letting L2s share their parent
10958 * L1's vpid. TODO: move to a more elaborate solution, giving
10959 * each L2 its own vpid and exposing the vpid feature to L1.
10960 */
10961 vmx_flush_tlb(vcpu);
10962 }
10963
10964
10965 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10966 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10967 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10968 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10969 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020010970 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
10971 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010972
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010973 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10974 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10975 vmcs_write64(GUEST_BNDCFGS, 0);
10976
Jan Kiszka44811c02013-08-04 17:17:27 +020010977 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010978 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010979 vcpu->arch.pat = vmcs12->host_ia32_pat;
10980 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010981 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10982 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10983 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010984
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010985 /* Set L1 segment info according to Intel SDM
10986 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10987 seg = (struct kvm_segment) {
10988 .base = 0,
10989 .limit = 0xFFFFFFFF,
10990 .selector = vmcs12->host_cs_selector,
10991 .type = 11,
10992 .present = 1,
10993 .s = 1,
10994 .g = 1
10995 };
10996 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10997 seg.l = 1;
10998 else
10999 seg.db = 1;
11000 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
11001 seg = (struct kvm_segment) {
11002 .base = 0,
11003 .limit = 0xFFFFFFFF,
11004 .type = 3,
11005 .present = 1,
11006 .s = 1,
11007 .db = 1,
11008 .g = 1
11009 };
11010 seg.selector = vmcs12->host_ds_selector;
11011 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
11012 seg.selector = vmcs12->host_es_selector;
11013 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
11014 seg.selector = vmcs12->host_ss_selector;
11015 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
11016 seg.selector = vmcs12->host_fs_selector;
11017 seg.base = vmcs12->host_fs_base;
11018 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
11019 seg.selector = vmcs12->host_gs_selector;
11020 seg.base = vmcs12->host_gs_base;
11021 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
11022 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030011023 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080011024 .limit = 0x67,
11025 .selector = vmcs12->host_tr_selector,
11026 .type = 11,
11027 .present = 1
11028 };
11029 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
11030
Jan Kiszka503cd0c2013-03-03 13:05:44 +010011031 kvm_set_dr(vcpu, 7, 0x400);
11032 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030011033
Wincy Van3af18d92015-02-03 23:49:31 +080011034 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010011035 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080011036
Wincy Vanff651cb2014-12-11 08:52:58 +030011037 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
11038 vmcs12->vm_exit_msr_load_count))
11039 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011040}
11041
11042/*
11043 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
11044 * and modify vmcs12 to make it see what it would expect to see there if
11045 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
11046 */
Jan Kiszka533558b2014-01-04 18:47:20 +010011047static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
11048 u32 exit_intr_info,
11049 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011050{
11051 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011052 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11053
Jan Kiszka5f3d5792013-04-14 12:12:46 +020011054 /* trying to cancel vmlaunch/vmresume is a bug */
11055 WARN_ON_ONCE(vmx->nested.nested_run_pending);
11056
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011057 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011058 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
11059 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011060
Wincy Vanff651cb2014-12-11 08:52:58 +030011061 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
11062 vmcs12->vm_exit_msr_store_count))
11063 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
11064
Wanpeng Lif3380ca2014-08-05 12:42:23 +080011065 vmx_load_vmcs01(vcpu);
11066
Bandan Das77b0f5d2014-04-19 18:17:45 -040011067 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
11068 && nested_exit_intr_ack_set(vcpu)) {
11069 int irq = kvm_cpu_get_interrupt(vcpu);
11070 WARN_ON(irq < 0);
11071 vmcs12->vm_exit_intr_info = irq |
11072 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
11073 }
11074
Jan Kiszka542060e2014-01-04 18:47:21 +010011075 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
11076 vmcs12->exit_qualification,
11077 vmcs12->idt_vectoring_info_field,
11078 vmcs12->vm_exit_intr_info,
11079 vmcs12->vm_exit_intr_error_code,
11080 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011081
Paolo Bonzini8391ce42016-07-07 14:58:33 +020011082 vm_entry_controls_reset_shadow(vmx);
11083 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010011084 vmx_segment_cache_clear(vmx);
11085
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011086 load_vmcs12_host_state(vcpu, vmcs12);
11087
Paolo Bonzini93140062016-07-06 13:23:51 +020011088 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk57e3ada2018-06-20 13:58:37 -040011089 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11090 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010011091 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020011092 if (vmx->hv_deadline_tsc == -1)
11093 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11094 PIN_BASED_VMX_PREEMPTION_TIMER);
11095 else
11096 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11097 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070011098 if (kvm_has_tsc_control)
11099 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011100
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011101 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
11102 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11103 vmx_set_virtual_x2apic_mode(vcpu,
11104 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070011105 } else if (!nested_cpu_has_ept(vmcs12) &&
11106 nested_cpu_has2(vmcs12,
11107 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11108 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011109 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011110
11111 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11112 vmx->host_rsp = 0;
11113
11114 /* Unpin physical memory we referred to in vmcs02 */
11115 if (vmx->nested.apic_access_page) {
11116 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011117 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011118 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011119 if (vmx->nested.virtual_apic_page) {
11120 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011121 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011122 }
Wincy Van705699a2015-02-03 23:58:17 +080011123 if (vmx->nested.pi_desc_page) {
11124 kunmap(vmx->nested.pi_desc_page);
11125 nested_release_page(vmx->nested.pi_desc_page);
11126 vmx->nested.pi_desc_page = NULL;
11127 vmx->nested.pi_desc = NULL;
11128 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011129
11130 /*
Tang Chen38b99172014-09-24 15:57:54 +080011131 * We are now running in L2, mmu_notifier will force to reload the
11132 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11133 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011134 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011135
11136 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011137 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11138 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11139 * success or failure flag accordingly.
11140 */
11141 if (unlikely(vmx->fail)) {
11142 vmx->fail = 0;
11143 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
11144 } else
11145 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011146 if (enable_shadow_vmcs)
11147 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011148
11149 /* in case we halted in L2 */
11150 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011151}
11152
Nadav Har'El7c177932011-05-25 23:12:04 +030011153/*
Jan Kiszka42124922014-01-04 18:47:19 +010011154 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11155 */
11156static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11157{
Wanpeng Lic886f282017-03-06 04:03:28 -080011158 if (is_guest_mode(vcpu)) {
11159 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011160 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011161 }
Jan Kiszka42124922014-01-04 18:47:19 +010011162 free_nested(to_vmx(vcpu));
11163}
11164
11165/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011166 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11167 * 23.7 "VM-entry failures during or after loading guest state" (this also
11168 * lists the acceptable exit-reason and exit-qualification parameters).
11169 * It should only be called before L2 actually succeeded to run, and when
11170 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11171 */
11172static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11173 struct vmcs12 *vmcs12,
11174 u32 reason, unsigned long qualification)
11175{
11176 load_vmcs12_host_state(vcpu, vmcs12);
11177 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11178 vmcs12->exit_qualification = qualification;
11179 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011180 if (enable_shadow_vmcs)
11181 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011182}
11183
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011184static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11185 struct x86_instruction_info *info,
11186 enum x86_intercept_stage stage)
11187{
11188 return X86EMUL_CONTINUE;
11189}
11190
Yunhong Jiang64672c92016-06-13 14:19:59 -070011191#ifdef CONFIG_X86_64
11192/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11193static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11194 u64 divisor, u64 *result)
11195{
11196 u64 low = a << shift, high = a >> (64 - shift);
11197
11198 /* To avoid the overflow on divq */
11199 if (high >= divisor)
11200 return 1;
11201
11202 /* Low hold the result, high hold rem which is discarded */
11203 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11204 "rm" (divisor), "0" (low), "1" (high));
11205 *result = low;
11206
11207 return 0;
11208}
11209
11210static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11211{
11212 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011213 u64 tscl = rdtsc();
11214 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11215 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011216
11217 /* Convert to host delta tsc if tsc scaling is enabled */
11218 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11219 u64_shl_div_u64(delta_tsc,
11220 kvm_tsc_scaling_ratio_frac_bits,
11221 vcpu->arch.tsc_scaling_ratio,
11222 &delta_tsc))
11223 return -ERANGE;
11224
11225 /*
11226 * If the delta tsc can't fit in the 32 bit after the multi shift,
11227 * we can't use the preemption timer.
11228 * It's possible that it fits on later vmentries, but checking
11229 * on every vmentry is costly so we just use an hrtimer.
11230 */
11231 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11232 return -ERANGE;
11233
11234 vmx->hv_deadline_tsc = tscl + delta_tsc;
11235 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11236 PIN_BASED_VMX_PREEMPTION_TIMER);
11237 return 0;
11238}
11239
11240static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11241{
11242 struct vcpu_vmx *vmx = to_vmx(vcpu);
11243 vmx->hv_deadline_tsc = -1;
11244 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11245 PIN_BASED_VMX_PREEMPTION_TIMER);
11246}
11247#endif
11248
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011249static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011250{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011251 if (ple_gap)
11252 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011253}
11254
Kai Huang843e4332015-01-28 10:54:28 +080011255static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11256 struct kvm_memory_slot *slot)
11257{
11258 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11259 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11260}
11261
11262static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11263 struct kvm_memory_slot *slot)
11264{
11265 kvm_mmu_slot_set_dirty(kvm, slot);
11266}
11267
11268static void vmx_flush_log_dirty(struct kvm *kvm)
11269{
11270 kvm_flush_pml_buffers(kvm);
11271}
11272
11273static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11274 struct kvm_memory_slot *memslot,
11275 gfn_t offset, unsigned long mask)
11276{
11277 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11278}
11279
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011280static void __pi_post_block(struct kvm_vcpu *vcpu)
11281{
11282 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11283 struct pi_desc old, new;
11284 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011285
11286 do {
11287 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011288 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11289 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011290
11291 dest = cpu_physical_id(vcpu->cpu);
11292
11293 if (x2apic_enabled())
11294 new.ndst = dest;
11295 else
11296 new.ndst = (dest << 8) & 0xFF00;
11297
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011298 /* set 'NV' to 'notification vector' */
11299 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011300 } while (cmpxchg64(&pi_desc->control, old.control,
11301 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011302
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011303 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11304 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011305 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011306 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011307 vcpu->pre_pcpu = -1;
11308 }
11309}
11310
Feng Wuefc64402015-09-18 22:29:51 +080011311/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011312 * This routine does the following things for vCPU which is going
11313 * to be blocked if VT-d PI is enabled.
11314 * - Store the vCPU to the wakeup list, so when interrupts happen
11315 * we can find the right vCPU to wake up.
11316 * - Change the Posted-interrupt descriptor as below:
11317 * 'NDST' <-- vcpu->pre_pcpu
11318 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11319 * - If 'ON' is set during this process, which means at least one
11320 * interrupt is posted for this vCPU, we cannot block it, in
11321 * this case, return 1, otherwise, return 0.
11322 *
11323 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011324static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011325{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011326 unsigned int dest;
11327 struct pi_desc old, new;
11328 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11329
11330 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011331 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11332 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011333 return 0;
11334
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011335 WARN_ON(irqs_disabled());
11336 local_irq_disable();
11337 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11338 vcpu->pre_pcpu = vcpu->cpu;
11339 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11340 list_add_tail(&vcpu->blocked_vcpu_list,
11341 &per_cpu(blocked_vcpu_on_cpu,
11342 vcpu->pre_pcpu));
11343 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11344 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011345
11346 do {
11347 old.control = new.control = pi_desc->control;
11348
Feng Wubf9f6ac2015-09-18 22:29:55 +080011349 WARN((pi_desc->sn == 1),
11350 "Warning: SN field of posted-interrupts "
11351 "is set before blocking\n");
11352
11353 /*
11354 * Since vCPU can be preempted during this process,
11355 * vcpu->cpu could be different with pre_pcpu, we
11356 * need to set pre_pcpu as the destination of wakeup
11357 * notification event, then we can find the right vCPU
11358 * to wakeup in wakeup handler if interrupts happen
11359 * when the vCPU is in blocked state.
11360 */
11361 dest = cpu_physical_id(vcpu->pre_pcpu);
11362
11363 if (x2apic_enabled())
11364 new.ndst = dest;
11365 else
11366 new.ndst = (dest << 8) & 0xFF00;
11367
11368 /* set 'NV' to 'wakeup vector' */
11369 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011370 } while (cmpxchg64(&pi_desc->control, old.control,
11371 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011372
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011373 /* We should not block the vCPU if an interrupt is posted for it. */
11374 if (pi_test_on(pi_desc) == 1)
11375 __pi_post_block(vcpu);
11376
11377 local_irq_enable();
11378 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011379}
11380
Yunhong Jiangbc225122016-06-13 14:19:58 -070011381static int vmx_pre_block(struct kvm_vcpu *vcpu)
11382{
11383 if (pi_pre_block(vcpu))
11384 return 1;
11385
Yunhong Jiang64672c92016-06-13 14:19:59 -070011386 if (kvm_lapic_hv_timer_in_use(vcpu))
11387 kvm_lapic_switch_to_sw_timer(vcpu);
11388
Yunhong Jiangbc225122016-06-13 14:19:58 -070011389 return 0;
11390}
11391
11392static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011393{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011394 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011395 return;
11396
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011397 WARN_ON(irqs_disabled());
11398 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011399 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011400 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011401}
11402
Yunhong Jiangbc225122016-06-13 14:19:58 -070011403static void vmx_post_block(struct kvm_vcpu *vcpu)
11404{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011405 if (kvm_x86_ops->set_hv_timer)
11406 kvm_lapic_switch_to_hv_timer(vcpu);
11407
Yunhong Jiangbc225122016-06-13 14:19:58 -070011408 pi_post_block(vcpu);
11409}
11410
Feng Wubf9f6ac2015-09-18 22:29:55 +080011411/*
Feng Wuefc64402015-09-18 22:29:51 +080011412 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11413 *
11414 * @kvm: kvm
11415 * @host_irq: host irq of the interrupt
11416 * @guest_irq: gsi of the interrupt
11417 * @set: set or unset PI
11418 * returns 0 on success, < 0 on failure
11419 */
11420static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11421 uint32_t guest_irq, bool set)
11422{
11423 struct kvm_kernel_irq_routing_entry *e;
11424 struct kvm_irq_routing_table *irq_rt;
11425 struct kvm_lapic_irq irq;
11426 struct kvm_vcpu *vcpu;
11427 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011428 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011429
11430 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011431 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11432 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011433 return 0;
11434
11435 idx = srcu_read_lock(&kvm->irq_srcu);
11436 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011437 if (guest_irq >= irq_rt->nr_rt_entries ||
11438 hlist_empty(&irq_rt->map[guest_irq])) {
11439 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11440 guest_irq, irq_rt->nr_rt_entries);
11441 goto out;
11442 }
Feng Wuefc64402015-09-18 22:29:51 +080011443
11444 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11445 if (e->type != KVM_IRQ_ROUTING_MSI)
11446 continue;
11447 /*
11448 * VT-d PI cannot support posting multicast/broadcast
11449 * interrupts to a vCPU, we still use interrupt remapping
11450 * for these kind of interrupts.
11451 *
11452 * For lowest-priority interrupts, we only support
11453 * those with single CPU as the destination, e.g. user
11454 * configures the interrupts via /proc/irq or uses
11455 * irqbalance to make the interrupts single-CPU.
11456 *
11457 * We will support full lowest-priority interrupt later.
11458 */
11459
Radim Krčmář371313132016-07-12 22:09:27 +020011460 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011461 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11462 /*
11463 * Make sure the IRTE is in remapped mode if
11464 * we don't handle it in posted mode.
11465 */
11466 ret = irq_set_vcpu_affinity(host_irq, NULL);
11467 if (ret < 0) {
11468 printk(KERN_INFO
11469 "failed to back to remapped mode, irq: %u\n",
11470 host_irq);
11471 goto out;
11472 }
11473
Feng Wuefc64402015-09-18 22:29:51 +080011474 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011475 }
Feng Wuefc64402015-09-18 22:29:51 +080011476
11477 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11478 vcpu_info.vector = irq.vector;
11479
Feng Wub6ce9782016-01-25 16:53:35 +080011480 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011481 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11482
11483 if (set)
11484 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011485 else
Feng Wuefc64402015-09-18 22:29:51 +080011486 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011487
11488 if (ret < 0) {
11489 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11490 __func__);
11491 goto out;
11492 }
11493 }
11494
11495 ret = 0;
11496out:
11497 srcu_read_unlock(&kvm->irq_srcu, idx);
11498 return ret;
11499}
11500
Ashok Rajc45dcc72016-06-22 14:59:56 +080011501static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11502{
11503 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11504 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11505 FEATURE_CONTROL_LMCE;
11506 else
11507 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11508 ~FEATURE_CONTROL_LMCE;
11509}
11510
Kees Cook404f6aa2016-08-08 16:29:06 -070011511static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011512 .cpu_has_kvm_support = cpu_has_kvm_support,
11513 .disabled_by_bios = vmx_disabled_by_bios,
11514 .hardware_setup = hardware_setup,
11515 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011516 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011517 .hardware_enable = hardware_enable,
11518 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011519 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011520 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011521
Konrad Rzeszutek Wilka0695af2018-06-20 11:29:53 -040011522 .vm_init = vmx_vm_init,
11523
Avi Kivity6aa8b732006-12-10 02:21:36 -080011524 .vcpu_create = vmx_create_vcpu,
11525 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011526 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011527
Avi Kivity04d2cc72007-09-10 18:10:54 +030011528 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011529 .vcpu_load = vmx_vcpu_load,
11530 .vcpu_put = vmx_vcpu_put,
11531
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011532 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011533 .get_msr = vmx_get_msr,
11534 .set_msr = vmx_set_msr,
11535 .get_segment_base = vmx_get_segment_base,
11536 .get_segment = vmx_get_segment,
11537 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011538 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011539 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011540 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011541 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011542 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011543 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011544 .set_cr3 = vmx_set_cr3,
11545 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011546 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011547 .get_idt = vmx_get_idt,
11548 .set_idt = vmx_set_idt,
11549 .get_gdt = vmx_get_gdt,
11550 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011551 .get_dr6 = vmx_get_dr6,
11552 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011553 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011554 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011555 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011556 .get_rflags = vmx_get_rflags,
11557 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011558
11559 .get_pkru = vmx_get_pkru,
11560
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011561 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011562 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011563
11564 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011565
Avi Kivity6aa8b732006-12-10 02:21:36 -080011566 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011567 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011568 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011569 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11570 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011571 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011572 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011573 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011574 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011575 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011576 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011577 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011578 .get_nmi_mask = vmx_get_nmi_mask,
11579 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011580 .enable_nmi_window = enable_nmi_window,
11581 .enable_irq_window = enable_irq_window,
11582 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011583 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011584 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011585 .get_enable_apicv = vmx_get_enable_apicv,
11586 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011587 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11588 .hwapic_irr_update = vmx_hwapic_irr_update,
11589 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011590 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11591 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011592
Izik Eiduscbc94022007-10-25 00:29:55 +020011593 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011594 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011595 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011596
Avi Kivity586f9602010-11-18 13:09:54 +020011597 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011598
Sheng Yang17cc3932010-01-05 19:02:27 +080011599 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011600
11601 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011602
11603 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011604 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011605
11606 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011607
11608 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011609
11610 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011611
11612 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011613
11614 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011615 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011616 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011617 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011618
11619 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011620
11621 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011622
11623 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11624 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11625 .flush_log_dirty = vmx_flush_log_dirty,
11626 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011627
Feng Wubf9f6ac2015-09-18 22:29:55 +080011628 .pre_block = vmx_pre_block,
11629 .post_block = vmx_post_block,
11630
Wei Huang25462f72015-06-19 15:45:05 +020011631 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011632
11633 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011634
11635#ifdef CONFIG_X86_64
11636 .set_hv_timer = vmx_set_hv_timer,
11637 .cancel_hv_timer = vmx_cancel_hv_timer,
11638#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011639
11640 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011641};
11642
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011643static int __init vmx_setup_l1d_flush(void)
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011644{
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011645 struct page *page;
11646
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011647 if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER ||
11648 !boot_cpu_has_bug(X86_BUG_L1TF))
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011649 return 0;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011650
Paolo Bonziniacca8a72018-07-02 13:03:48 +020011651 if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
11652 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
11653 if (!page)
11654 return -ENOMEM;
11655 vmx_l1d_flush_pages = page_address(page);
11656 }
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011657
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011658 static_branch_enable(&vmx_l1d_should_flush);
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011659 return 0;
11660}
11661
11662static void vmx_free_l1d_flush_pages(void)
11663{
11664 if (vmx_l1d_flush_pages) {
11665 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
11666 vmx_l1d_flush_pages = NULL;
11667 }
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011668}
11669
Avi Kivity6aa8b732006-12-10 02:21:36 -080011670static int __init vmx_init(void)
11671{
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011672 int r;
11673
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011674 r = vmx_setup_l1d_flush();
11675 if (r)
11676 return r;
Konrad Rzeszutek Wilkaf6ce922018-07-02 12:29:30 +020011677
11678 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11679 __alignof__(struct vcpu_vmx), THIS_MODULE);
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011680 if (r) {
11681 vmx_free_l1d_flush_pages();
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011682 return r;
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011683 }
Sheng Yang25c5f222008-03-28 13:18:56 +080011684
Dave Young2965faa2015-09-09 15:38:55 -070011685#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011686 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11687 crash_vmclear_local_loaded_vmcss);
11688#endif
11689
He, Qingfdef3ad2007-04-30 09:45:24 +030011690 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011691}
11692
11693static void __exit vmx_exit(void)
11694{
Dave Young2965faa2015-09-09 15:38:55 -070011695#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011696 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011697 synchronize_rcu();
11698#endif
11699
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011700 kvm_exit();
Paolo Bonzinib3d648a2018-07-02 12:47:38 +020011701
11702 vmx_free_l1d_flush_pages();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011703}
11704
11705module_init(vmx_init)
11706module_exit(vmx_exit)