blob: c263708b6dc41b7d3767417914edf66a4eb26ee5 [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>
Dan Williamseb99bd62018-01-31 17:47:03 -080036#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030037#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030038#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040039
Feng Wu28b835d2015-09-18 22:29:54 +080040#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080041#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080042#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020043#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020044#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080045#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020046#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020047#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010048#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080049#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010050#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080051#include <asm/irq_remapping.h>
Paolo Bonzinia175d512018-02-22 16:43:17 +010052#include <asm/microcode.h>
Thomas Gleixner7a2d2352018-04-29 15:01:37 +020053#include <asm/spec-ctrl.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080054
Marcelo Tosatti229456f2009-06-17 09:22:14 -030055#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020056#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030057
Avi Kivity4ecac3f2008-05-13 13:23:38 +030058#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040059#define __ex_clear(x, reg) \
60 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030061
Avi Kivity6aa8b732006-12-10 02:21:36 -080062MODULE_AUTHOR("Qumranet");
63MODULE_LICENSE("GPL");
64
Josh Triplette9bda3b2012-03-20 23:33:51 -070065static const struct x86_cpu_id vmx_cpu_id[] = {
66 X86_FEATURE_MATCH(X86_FEATURE_VMX),
67 {}
68};
69MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
70
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020072module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080073
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020076
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020078module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080079
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070081module_param_named(unrestricted_guest,
82 enable_unrestricted_guest, bool, S_IRUGO);
83
Xudong Hao83c3a332012-05-28 19:33:35 +080084static bool __read_mostly enable_ept_ad_bits = 1;
85module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
86
Avi Kivitya27685c2012-06-12 20:30:18 +030087static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020088module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030089
Rusty Russell476bc002012-01-13 09:32:18 +103090static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080091module_param(vmm_exclusive, bool, S_IRUGO);
92
Rusty Russell476bc002012-01-13 09:32:18 +103093static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030094module_param(fasteoi, bool, S_IRUGO);
95
Yang Zhang5a717852013-04-11 19:25:16 +080096static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080097module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080098
Abel Gordonabc4fc52013-04-18 14:35:25 +030099static bool __read_mostly enable_shadow_vmcs = 1;
100module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300101/*
102 * If nested=1, nested virtualization is supported, i.e., guests may use
103 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
104 * use VMX instructions.
105 */
Rusty Russell476bc002012-01-13 09:32:18 +1030106static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300107module_param(nested, bool, S_IRUGO);
108
Wanpeng Li20300092014-12-02 19:14:59 +0800109static u64 __read_mostly host_xss;
110
Kai Huang843e4332015-01-28 10:54:28 +0800111static bool __read_mostly enable_pml = 1;
112module_param_named(pml, enable_pml, bool, S_IRUGO);
113
Paolo Bonzini6236b782018-01-16 16:51:18 +0100114#define MSR_TYPE_R 1
115#define MSR_TYPE_W 2
116#define MSR_TYPE_RW 3
117
118#define MSR_BITMAP_MODE_X2APIC 1
119#define MSR_BITMAP_MODE_X2APIC_APICV 2
120#define MSR_BITMAP_MODE_LM 4
121
Haozhong Zhang64903d62015-10-20 15:39:09 +0800122#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
123
Yunhong Jiang64672c92016-06-13 14:19:59 -0700124/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
125static int __read_mostly cpu_preemption_timer_multi;
126static bool __read_mostly enable_preemption_timer = 1;
127#ifdef CONFIG_X86_64
128module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
129#endif
130
Gleb Natapov50378782013-02-04 16:00:28 +0200131#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
132#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200133#define KVM_VM_CR0_ALWAYS_ON \
134 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200135#define KVM_CR4_GUEST_OWNED_BITS \
136 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700137 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200138
Avi Kivitycdc0e242009-12-06 17:21:14 +0200139#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
140#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
141
Avi Kivity78ac8b42010-04-08 18:19:35 +0300142#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
143
Jan Kiszkaf4124502014-03-07 20:03:13 +0100144#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
145
Jan Dakinevich04c776e2018-02-23 11:42:18 +0100146#define VMX_VPID_EXTENT_SUPPORTED_MASK \
147 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
148 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
149 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
150 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
151
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800152/*
153 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
154 * ple_gap: upper bound on the amount of time between two successive
155 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500156 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800157 * ple_window: upper bound on the amount of time a guest is allowed to execute
158 * in a PAUSE loop. Tests indicate that most spinlocks are held for
159 * less than 2^12 cycles
160 * Time is measured based on a counter that runs at the same rate as the TSC,
161 * refer SDM volume 3b section 21.6.13 & 22.1.3.
162 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200163#define KVM_VMX_DEFAULT_PLE_GAP 128
164#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
165#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
166#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
167#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
168 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
169
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800170static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
171module_param(ple_gap, int, S_IRUGO);
172
173static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
174module_param(ple_window, int, S_IRUGO);
175
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176/* Default doubles per-vcpu window every exit. */
177static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
178module_param(ple_window_grow, int, S_IRUGO);
179
180/* Default resets per-vcpu window every exit to ple_window. */
181static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
182module_param(ple_window_shrink, int, S_IRUGO);
183
184/* Default is to compute the maximum so we can never overflow. */
185static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
186static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, int, S_IRUGO);
188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200191#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300192
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400193struct vmcs {
194 u32 revision_id;
195 u32 abort;
196 char data[0];
197};
198
Nadav Har'Eld462b812011-05-24 15:26:10 +0300199/*
200 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
201 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
202 * loaded on this CPU (so we can clear them if the CPU goes down).
203 */
204struct loaded_vmcs {
205 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700206 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300207 int cpu;
208 int launched;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100209 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300210 struct list_head loaded_vmcss_on_cpu_link;
211};
212
Avi Kivity26bb0982009-09-07 11:14:12 +0300213struct shared_msr_entry {
214 unsigned index;
215 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200216 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300217};
218
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300219/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300220 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
221 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
222 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
223 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
224 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
225 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattson46e24df2017-11-27 17:22:25 -0600226 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300227 * underlying hardware which will be used to run L2.
228 * This structure is packed to ensure that its layout is identical across
229 * machines (necessary for live migration).
230 * If there are changes in this struct, VMCS12_REVISION must be changed.
231 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300232typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300233struct __packed vmcs12 {
234 /* According to the Intel spec, a VMCS region must start with the
235 * following two fields. Then follow implementation-specific data.
236 */
237 u32 revision_id;
238 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300239
Nadav Har'El27d6c862011-05-25 23:06:59 +0300240 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
241 u32 padding[7]; /* room for future expansion */
242
Nadav Har'El22bd0352011-05-25 23:05:57 +0300243 u64 io_bitmap_a;
244 u64 io_bitmap_b;
245 u64 msr_bitmap;
246 u64 vm_exit_msr_store_addr;
247 u64 vm_exit_msr_load_addr;
248 u64 vm_entry_msr_load_addr;
249 u64 tsc_offset;
250 u64 virtual_apic_page_addr;
251 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800252 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300253 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800254 u64 eoi_exit_bitmap0;
255 u64 eoi_exit_bitmap1;
256 u64 eoi_exit_bitmap2;
257 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800258 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300259 u64 guest_physical_address;
260 u64 vmcs_link_pointer;
261 u64 guest_ia32_debugctl;
262 u64 guest_ia32_pat;
263 u64 guest_ia32_efer;
264 u64 guest_ia32_perf_global_ctrl;
265 u64 guest_pdptr0;
266 u64 guest_pdptr1;
267 u64 guest_pdptr2;
268 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100269 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300270 u64 host_ia32_pat;
271 u64 host_ia32_efer;
272 u64 host_ia32_perf_global_ctrl;
273 u64 padding64[8]; /* room for future expansion */
274 /*
275 * To allow migration of L1 (complete with its L2 guests) between
276 * machines of different natural widths (32 or 64 bit), we cannot have
277 * unsigned long fields with no explict size. We use u64 (aliased
278 * natural_width) instead. Luckily, x86 is little-endian.
279 */
280 natural_width cr0_guest_host_mask;
281 natural_width cr4_guest_host_mask;
282 natural_width cr0_read_shadow;
283 natural_width cr4_read_shadow;
284 natural_width cr3_target_value0;
285 natural_width cr3_target_value1;
286 natural_width cr3_target_value2;
287 natural_width cr3_target_value3;
288 natural_width exit_qualification;
289 natural_width guest_linear_address;
290 natural_width guest_cr0;
291 natural_width guest_cr3;
292 natural_width guest_cr4;
293 natural_width guest_es_base;
294 natural_width guest_cs_base;
295 natural_width guest_ss_base;
296 natural_width guest_ds_base;
297 natural_width guest_fs_base;
298 natural_width guest_gs_base;
299 natural_width guest_ldtr_base;
300 natural_width guest_tr_base;
301 natural_width guest_gdtr_base;
302 natural_width guest_idtr_base;
303 natural_width guest_dr7;
304 natural_width guest_rsp;
305 natural_width guest_rip;
306 natural_width guest_rflags;
307 natural_width guest_pending_dbg_exceptions;
308 natural_width guest_sysenter_esp;
309 natural_width guest_sysenter_eip;
310 natural_width host_cr0;
311 natural_width host_cr3;
312 natural_width host_cr4;
313 natural_width host_fs_base;
314 natural_width host_gs_base;
315 natural_width host_tr_base;
316 natural_width host_gdtr_base;
317 natural_width host_idtr_base;
318 natural_width host_ia32_sysenter_esp;
319 natural_width host_ia32_sysenter_eip;
320 natural_width host_rsp;
321 natural_width host_rip;
322 natural_width paddingl[8]; /* room for future expansion */
323 u32 pin_based_vm_exec_control;
324 u32 cpu_based_vm_exec_control;
325 u32 exception_bitmap;
326 u32 page_fault_error_code_mask;
327 u32 page_fault_error_code_match;
328 u32 cr3_target_count;
329 u32 vm_exit_controls;
330 u32 vm_exit_msr_store_count;
331 u32 vm_exit_msr_load_count;
332 u32 vm_entry_controls;
333 u32 vm_entry_msr_load_count;
334 u32 vm_entry_intr_info_field;
335 u32 vm_entry_exception_error_code;
336 u32 vm_entry_instruction_len;
337 u32 tpr_threshold;
338 u32 secondary_vm_exec_control;
339 u32 vm_instruction_error;
340 u32 vm_exit_reason;
341 u32 vm_exit_intr_info;
342 u32 vm_exit_intr_error_code;
343 u32 idt_vectoring_info_field;
344 u32 idt_vectoring_error_code;
345 u32 vm_exit_instruction_len;
346 u32 vmx_instruction_info;
347 u32 guest_es_limit;
348 u32 guest_cs_limit;
349 u32 guest_ss_limit;
350 u32 guest_ds_limit;
351 u32 guest_fs_limit;
352 u32 guest_gs_limit;
353 u32 guest_ldtr_limit;
354 u32 guest_tr_limit;
355 u32 guest_gdtr_limit;
356 u32 guest_idtr_limit;
357 u32 guest_es_ar_bytes;
358 u32 guest_cs_ar_bytes;
359 u32 guest_ss_ar_bytes;
360 u32 guest_ds_ar_bytes;
361 u32 guest_fs_ar_bytes;
362 u32 guest_gs_ar_bytes;
363 u32 guest_ldtr_ar_bytes;
364 u32 guest_tr_ar_bytes;
365 u32 guest_interruptibility_info;
366 u32 guest_activity_state;
367 u32 guest_sysenter_cs;
368 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100369 u32 vmx_preemption_timer_value;
370 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300371 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800372 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300373 u16 guest_es_selector;
374 u16 guest_cs_selector;
375 u16 guest_ss_selector;
376 u16 guest_ds_selector;
377 u16 guest_fs_selector;
378 u16 guest_gs_selector;
379 u16 guest_ldtr_selector;
380 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800381 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300382 u16 host_es_selector;
383 u16 host_cs_selector;
384 u16 host_ss_selector;
385 u16 host_ds_selector;
386 u16 host_fs_selector;
387 u16 host_gs_selector;
388 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300389};
390
391/*
392 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
393 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
394 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
395 */
396#define VMCS12_REVISION 0x11e57ed0
397
398/*
399 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
400 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
401 * current implementation, 4K are reserved to avoid future complications.
402 */
403#define VMCS12_SIZE 0x1000
404
405/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300406 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
407 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
408 */
409struct nested_vmx {
410 /* Has the level1 guest done vmxon? */
411 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400412 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300413
414 /* The guest-physical address of the current VMCS L1 keeps for L2 */
415 gpa_t current_vmptr;
416 /* The host-usable pointer to the above */
417 struct page *current_vmcs12_page;
418 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700419 /*
420 * Cache of the guest's VMCS, existing outside of guest memory.
421 * Loaded from guest memory during VMPTRLD. Flushed to guest
422 * memory during VMXOFF, VMCLEAR, VMPTRLD.
423 */
424 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300425 /*
426 * Indicates if the shadow vmcs must be updated with the
427 * data hold by vmcs12
428 */
429 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300430
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200431 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300432 /* L2 must run next, and mustn't decide to exit to L1. */
433 bool nested_run_pending;
Jim Mattson46e24df2017-11-27 17:22:25 -0600434
435 struct loaded_vmcs vmcs02;
436
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300437 /*
Jim Mattson46e24df2017-11-27 17:22:25 -0600438 * Guest pages referred to in the vmcs02 with host-physical
439 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300440 */
441 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800442 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800443 struct page *pi_desc_page;
444 struct pi_desc *pi_desc;
445 bool pi_pending;
446 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100447
448 struct hrtimer preemption_timer;
449 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200450
451 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
452 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800453
Wanpeng Li5c614b32015-10-13 09:18:36 -0700454 u16 vpid02;
455 u16 last_vpid;
456
Wincy Vanb9c237b2015-02-03 23:56:30 +0800457 u32 nested_vmx_procbased_ctls_low;
458 u32 nested_vmx_procbased_ctls_high;
459 u32 nested_vmx_true_procbased_ctls_low;
460 u32 nested_vmx_secondary_ctls_low;
461 u32 nested_vmx_secondary_ctls_high;
462 u32 nested_vmx_pinbased_ctls_low;
463 u32 nested_vmx_pinbased_ctls_high;
464 u32 nested_vmx_exit_ctls_low;
465 u32 nested_vmx_exit_ctls_high;
466 u32 nested_vmx_true_exit_ctls_low;
467 u32 nested_vmx_entry_ctls_low;
468 u32 nested_vmx_entry_ctls_high;
469 u32 nested_vmx_true_entry_ctls_low;
470 u32 nested_vmx_misc_low;
471 u32 nested_vmx_misc_high;
472 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700473 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300474};
475
Yang Zhang01e439b2013-04-11 19:25:12 +0800476#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800477#define POSTED_INTR_SN 1
478
Yang Zhang01e439b2013-04-11 19:25:12 +0800479/* Posted-Interrupt Descriptor */
480struct pi_desc {
481 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800482 union {
483 struct {
484 /* bit 256 - Outstanding Notification */
485 u16 on : 1,
486 /* bit 257 - Suppress Notification */
487 sn : 1,
488 /* bit 271:258 - Reserved */
489 rsvd_1 : 14;
490 /* bit 279:272 - Notification Vector */
491 u8 nv;
492 /* bit 287:280 - Reserved */
493 u8 rsvd_2;
494 /* bit 319:288 - Notification Destination */
495 u32 ndst;
496 };
497 u64 control;
498 };
499 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800500} __aligned(64);
501
Yang Zhanga20ed542013-04-11 19:25:15 +0800502static bool pi_test_and_set_on(struct pi_desc *pi_desc)
503{
504 return test_and_set_bit(POSTED_INTR_ON,
505 (unsigned long *)&pi_desc->control);
506}
507
508static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
509{
510 return test_and_clear_bit(POSTED_INTR_ON,
511 (unsigned long *)&pi_desc->control);
512}
513
514static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
515{
516 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
517}
518
Feng Wuebbfc762015-09-18 22:29:46 +0800519static inline void pi_clear_sn(struct pi_desc *pi_desc)
520{
521 return clear_bit(POSTED_INTR_SN,
522 (unsigned long *)&pi_desc->control);
523}
524
525static inline void pi_set_sn(struct pi_desc *pi_desc)
526{
527 return set_bit(POSTED_INTR_SN,
528 (unsigned long *)&pi_desc->control);
529}
530
531static inline int pi_test_on(struct pi_desc *pi_desc)
532{
533 return test_bit(POSTED_INTR_ON,
534 (unsigned long *)&pi_desc->control);
535}
536
537static inline int pi_test_sn(struct pi_desc *pi_desc)
538{
539 return test_bit(POSTED_INTR_SN,
540 (unsigned long *)&pi_desc->control);
541}
542
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400543struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000544 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300545 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300546 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200547 bool nmi_known_unmasked;
Paolo Bonzini6236b782018-01-16 16:51:18 +0100548 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300549 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200550 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200551 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300552 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400553 int nmsrs;
554 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800555 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400556#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300557 u64 msr_host_kernel_gs_base;
558 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400559#endif
Ashok Raj70131292018-02-01 22:59:43 +0100560
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100561 u64 arch_capabilities;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +0100562 u64 spec_ctrl;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +0100563
Gleb Natapov2961e8762013-11-25 15:37:13 +0200564 u32 vm_entry_controls_shadow;
565 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300566 /*
567 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
568 * non-nested (L1) guest, it always points to vmcs01. For a nested
569 * guest (L2), it points to a different VMCS.
570 */
571 struct loaded_vmcs vmcs01;
572 struct loaded_vmcs *loaded_vmcs;
573 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300574 struct msr_autoload {
575 unsigned nr;
576 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
577 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
578 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400579 struct {
580 int loaded;
581 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300582#ifdef CONFIG_X86_64
583 u16 ds_sel, es_sel;
584#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200585 int gs_ldt_reload_needed;
586 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000587 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700588 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400589 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200590 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300591 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300592 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300593 struct kvm_segment segs[8];
594 } rmode;
595 struct {
596 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300597 struct kvm_save_segment {
598 u16 selector;
599 unsigned long base;
600 u32 limit;
601 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300602 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300603 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800604 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300605 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200606
607 /* Support for vnmi-less CPUs */
608 int soft_vnmi_blocked;
609 ktime_t entry_time;
610 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800611 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800612
Yang Zhang01e439b2013-04-11 19:25:12 +0800613 /* Posted interrupt descriptor */
614 struct pi_desc pi_desc;
615
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300616 /* Support for a guest hypervisor (nested VMX) */
617 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200618
619 /* Dynamic PLE window. */
620 int ple_window;
621 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800622
623 /* Support for PML */
624#define PML_ENTITY_NUM 512
625 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800626
Yunhong Jiang64672c92016-06-13 14:19:59 -0700627 /* apic deadline value in host tsc */
628 u64 hv_deadline_tsc;
629
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800630 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800631
632 bool guest_pkru_valid;
633 u32 guest_pkru;
634 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800635
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800636 /*
637 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
638 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
639 * in msr_ia32_feature_control_valid_bits.
640 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800641 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800642 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400643};
644
Avi Kivity2fb92db2011-04-27 19:42:18 +0300645enum segment_cache_field {
646 SEG_FIELD_SEL = 0,
647 SEG_FIELD_BASE = 1,
648 SEG_FIELD_LIMIT = 2,
649 SEG_FIELD_AR = 3,
650
651 SEG_FIELD_NR = 4
652};
653
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400654static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
655{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000656 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400657}
658
Feng Wuefc64402015-09-18 22:29:51 +0800659static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
660{
661 return &(to_vmx(vcpu)->pi_desc);
662}
663
Nadav Har'El22bd0352011-05-25 23:05:57 +0300664#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
665#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
666#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
667 [number##_HIGH] = VMCS12_OFFSET(name)+4
668
Abel Gordon4607c2d2013-04-18 14:35:55 +0300669
Bandan Dasfe2b2012014-04-21 15:20:14 -0400670static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300671 /*
672 * We do NOT shadow fields that are modified when L0
673 * traps and emulates any vmx instruction (e.g. VMPTRLD,
674 * VMXON...) executed by L1.
675 * For example, VM_INSTRUCTION_ERROR is read
676 * by L1 if a vmx instruction fails (part of the error path).
677 * Note the code assumes this logic. If for some reason
678 * we start shadowing these fields then we need to
679 * force a shadow sync when L0 emulates vmx instructions
680 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
681 * by nested_vmx_failValid)
682 */
683 VM_EXIT_REASON,
684 VM_EXIT_INTR_INFO,
685 VM_EXIT_INSTRUCTION_LEN,
686 IDT_VECTORING_INFO_FIELD,
687 IDT_VECTORING_ERROR_CODE,
688 VM_EXIT_INTR_ERROR_CODE,
689 EXIT_QUALIFICATION,
690 GUEST_LINEAR_ADDRESS,
691 GUEST_PHYSICAL_ADDRESS
692};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400693static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300694 ARRAY_SIZE(shadow_read_only_fields);
695
Bandan Dasfe2b2012014-04-21 15:20:14 -0400696static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800697 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300698 GUEST_RIP,
699 GUEST_RSP,
700 GUEST_CR0,
701 GUEST_CR3,
702 GUEST_CR4,
703 GUEST_INTERRUPTIBILITY_INFO,
704 GUEST_RFLAGS,
705 GUEST_CS_SELECTOR,
706 GUEST_CS_AR_BYTES,
707 GUEST_CS_LIMIT,
708 GUEST_CS_BASE,
709 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100710 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300711 CR0_GUEST_HOST_MASK,
712 CR0_READ_SHADOW,
713 CR4_READ_SHADOW,
714 TSC_OFFSET,
715 EXCEPTION_BITMAP,
716 CPU_BASED_VM_EXEC_CONTROL,
717 VM_ENTRY_EXCEPTION_ERROR_CODE,
718 VM_ENTRY_INTR_INFO_FIELD,
719 VM_ENTRY_INSTRUCTION_LEN,
720 VM_ENTRY_EXCEPTION_ERROR_CODE,
721 HOST_FS_BASE,
722 HOST_GS_BASE,
723 HOST_FS_SELECTOR,
724 HOST_GS_SELECTOR
725};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400726static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300727 ARRAY_SIZE(shadow_read_write_fields);
728
Mathias Krause772e0312012-08-30 01:30:19 +0200729static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300730 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800731 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300732 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
733 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
734 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
735 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
736 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
737 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
738 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
739 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800740 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300741 FIELD(HOST_ES_SELECTOR, host_es_selector),
742 FIELD(HOST_CS_SELECTOR, host_cs_selector),
743 FIELD(HOST_SS_SELECTOR, host_ss_selector),
744 FIELD(HOST_DS_SELECTOR, host_ds_selector),
745 FIELD(HOST_FS_SELECTOR, host_fs_selector),
746 FIELD(HOST_GS_SELECTOR, host_gs_selector),
747 FIELD(HOST_TR_SELECTOR, host_tr_selector),
748 FIELD64(IO_BITMAP_A, io_bitmap_a),
749 FIELD64(IO_BITMAP_B, io_bitmap_b),
750 FIELD64(MSR_BITMAP, msr_bitmap),
751 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
752 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
753 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
754 FIELD64(TSC_OFFSET, tsc_offset),
755 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
756 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800757 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300758 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800759 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
760 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
761 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
762 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800763 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300764 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
765 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
766 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
767 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
768 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
769 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
770 FIELD64(GUEST_PDPTR0, guest_pdptr0),
771 FIELD64(GUEST_PDPTR1, guest_pdptr1),
772 FIELD64(GUEST_PDPTR2, guest_pdptr2),
773 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100774 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300775 FIELD64(HOST_IA32_PAT, host_ia32_pat),
776 FIELD64(HOST_IA32_EFER, host_ia32_efer),
777 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
778 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
779 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
780 FIELD(EXCEPTION_BITMAP, exception_bitmap),
781 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
782 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
783 FIELD(CR3_TARGET_COUNT, cr3_target_count),
784 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
785 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
786 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
787 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
788 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
789 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
790 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
791 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
792 FIELD(TPR_THRESHOLD, tpr_threshold),
793 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
794 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
795 FIELD(VM_EXIT_REASON, vm_exit_reason),
796 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
797 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
798 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
799 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
800 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
801 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
802 FIELD(GUEST_ES_LIMIT, guest_es_limit),
803 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
804 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
805 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
806 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
807 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
808 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
809 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
810 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
811 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
812 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
813 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
814 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
815 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
816 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
817 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
818 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
819 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
820 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
821 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
822 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
823 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100824 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300825 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
826 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
827 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
828 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
829 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
830 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
831 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
832 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
833 FIELD(EXIT_QUALIFICATION, exit_qualification),
834 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
835 FIELD(GUEST_CR0, guest_cr0),
836 FIELD(GUEST_CR3, guest_cr3),
837 FIELD(GUEST_CR4, guest_cr4),
838 FIELD(GUEST_ES_BASE, guest_es_base),
839 FIELD(GUEST_CS_BASE, guest_cs_base),
840 FIELD(GUEST_SS_BASE, guest_ss_base),
841 FIELD(GUEST_DS_BASE, guest_ds_base),
842 FIELD(GUEST_FS_BASE, guest_fs_base),
843 FIELD(GUEST_GS_BASE, guest_gs_base),
844 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
845 FIELD(GUEST_TR_BASE, guest_tr_base),
846 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
847 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
848 FIELD(GUEST_DR7, guest_dr7),
849 FIELD(GUEST_RSP, guest_rsp),
850 FIELD(GUEST_RIP, guest_rip),
851 FIELD(GUEST_RFLAGS, guest_rflags),
852 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
853 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
854 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
855 FIELD(HOST_CR0, host_cr0),
856 FIELD(HOST_CR3, host_cr3),
857 FIELD(HOST_CR4, host_cr4),
858 FIELD(HOST_FS_BASE, host_fs_base),
859 FIELD(HOST_GS_BASE, host_gs_base),
860 FIELD(HOST_TR_BASE, host_tr_base),
861 FIELD(HOST_GDTR_BASE, host_gdtr_base),
862 FIELD(HOST_IDTR_BASE, host_idtr_base),
863 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
864 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
865 FIELD(HOST_RSP, host_rsp),
866 FIELD(HOST_RIP, host_rip),
867};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300868
869static inline short vmcs_field_to_offset(unsigned long field)
870{
Dan Williamseb99bd62018-01-31 17:47:03 -0800871 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
872 unsigned short offset;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100873
Dan Williamseb99bd62018-01-31 17:47:03 -0800874 BUILD_BUG_ON(size > SHRT_MAX);
875 if (field >= size)
Andrew Honig012df712018-01-10 10:12:03 -0800876 return -ENOENT;
877
Dan Williamseb99bd62018-01-31 17:47:03 -0800878 field = array_index_nospec(field, size);
879 offset = vmcs_field_to_offset_table[field];
880 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100881 return -ENOENT;
Dan Williamseb99bd62018-01-31 17:47:03 -0800882 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300883}
884
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300885static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
886{
David Matlack4f2777b2016-07-13 17:16:37 -0700887 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300888}
889
890static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
891{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200892 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800893 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300894 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800895
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300896 return page;
897}
898
899static void nested_release_page(struct page *page)
900{
901 kvm_release_page_dirty(page);
902}
903
904static void nested_release_page_clean(struct page *page)
905{
906 kvm_release_page_clean(page);
907}
908
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300909static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800910static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800911static void kvm_cpu_vmxon(u64 addr);
912static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800913static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200914static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300915static void vmx_set_segment(struct kvm_vcpu *vcpu,
916 struct kvm_segment *var, int seg);
917static void vmx_get_segment(struct kvm_vcpu *vcpu,
918 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200919static bool guest_state_valid(struct kvm_vcpu *vcpu);
920static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300921static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300922static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800923static int alloc_identity_pagetable(struct kvm *kvm);
Paolo Bonzini6236b782018-01-16 16:51:18 +0100924static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj70131292018-02-01 22:59:43 +0100925static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
926 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +0300927
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928static DEFINE_PER_CPU(struct vmcs *, vmxarea);
929static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300930/*
931 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
932 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
933 */
934static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300935static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
Feng Wubf9f6ac2015-09-18 22:29:55 +0800937/*
938 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
939 * can find which vCPU should be waken up.
940 */
941static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
942static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
943
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200944static unsigned long *vmx_io_bitmap_a;
945static unsigned long *vmx_io_bitmap_b;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300946static unsigned long *vmx_vmread_bitmap;
947static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300948
Avi Kivity110312c2010-12-21 12:54:20 +0200949static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200950static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200951
Sheng Yang2384d2b2008-01-17 15:14:33 +0800952static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
953static DEFINE_SPINLOCK(vmx_vpid_lock);
954
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300955static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800956 int size;
957 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300958 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300960 u32 pin_based_exec_ctrl;
961 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800962 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300963 u32 vmexit_ctrl;
964 u32 vmentry_ctrl;
965} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966
Hannes Ederefff9e52008-11-28 17:02:06 +0100967static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800968 u32 ept;
969 u32 vpid;
970} vmx_capability;
971
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972#define VMX_SEGMENT_FIELD(seg) \
973 [VCPU_SREG_##seg] = { \
974 .selector = GUEST_##seg##_SELECTOR, \
975 .base = GUEST_##seg##_BASE, \
976 .limit = GUEST_##seg##_LIMIT, \
977 .ar_bytes = GUEST_##seg##_AR_BYTES, \
978 }
979
Mathias Krause772e0312012-08-30 01:30:19 +0200980static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981 unsigned selector;
982 unsigned base;
983 unsigned limit;
984 unsigned ar_bytes;
985} kvm_vmx_segment_fields[] = {
986 VMX_SEGMENT_FIELD(CS),
987 VMX_SEGMENT_FIELD(DS),
988 VMX_SEGMENT_FIELD(ES),
989 VMX_SEGMENT_FIELD(FS),
990 VMX_SEGMENT_FIELD(GS),
991 VMX_SEGMENT_FIELD(SS),
992 VMX_SEGMENT_FIELD(TR),
993 VMX_SEGMENT_FIELD(LDTR),
994};
995
Avi Kivity26bb0982009-09-07 11:14:12 +0300996static u64 host_efer;
997
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300998static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
999
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001000/*
Brian Gerst8c065852010-07-17 09:03:26 -04001001 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001002 * away by decrementing the array size.
1003 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001005#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001006 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001008 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010
Jan Kiszka5bb16012016-02-09 20:14:21 +01001011static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012{
1013 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1014 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001015 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1016}
1017
Jan Kiszka6f054852016-02-09 20:15:18 +01001018static inline bool is_debug(u32 intr_info)
1019{
1020 return is_exception_n(intr_info, DB_VECTOR);
1021}
1022
1023static inline bool is_breakpoint(u32 intr_info)
1024{
1025 return is_exception_n(intr_info, BP_VECTOR);
1026}
1027
Jan Kiszka5bb16012016-02-09 20:14:21 +01001028static inline bool is_page_fault(u32 intr_info)
1029{
1030 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031}
1032
Gui Jianfeng31299942010-03-15 17:29:09 +08001033static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001034{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001035 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001036}
1037
Gui Jianfeng31299942010-03-15 17:29:09 +08001038static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001039{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001040 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001041}
1042
Gui Jianfeng31299942010-03-15 17:29:09 +08001043static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044{
1045 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1046 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1047}
1048
Gui Jianfeng31299942010-03-15 17:29:09 +08001049static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001050{
1051 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1052 INTR_INFO_VALID_MASK)) ==
1053 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1054}
1055
Linus Torvalds587da2b2018-03-20 12:16:59 -07001056/* Undocumented: icebp/int1 */
1057static inline bool is_icebp(u32 intr_info)
1058{
1059 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1060 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1061}
1062
Gui Jianfeng31299942010-03-15 17:29:09 +08001063static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001064{
Sheng Yang04547152009-04-01 15:52:31 +08001065 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001066}
1067
Gui Jianfeng31299942010-03-15 17:29:09 +08001068static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001069{
Sheng Yang04547152009-04-01 15:52:31 +08001070 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001071}
1072
Paolo Bonzini35754c92015-07-29 12:05:37 +02001073static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001074{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001075 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001076}
1077
Gui Jianfeng31299942010-03-15 17:29:09 +08001078static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001079{
Sheng Yang04547152009-04-01 15:52:31 +08001080 return vmcs_config.cpu_based_exec_ctrl &
1081 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001082}
1083
Avi Kivity774ead32007-12-26 13:57:04 +02001084static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001085{
Sheng Yang04547152009-04-01 15:52:31 +08001086 return vmcs_config.cpu_based_2nd_exec_ctrl &
1087 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1088}
1089
Yang Zhang8d146952013-01-25 10:18:50 +08001090static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1091{
1092 return vmcs_config.cpu_based_2nd_exec_ctrl &
1093 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1094}
1095
Yang Zhang83d4c282013-01-25 10:18:49 +08001096static inline bool cpu_has_vmx_apic_register_virt(void)
1097{
1098 return vmcs_config.cpu_based_2nd_exec_ctrl &
1099 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1100}
1101
Yang Zhangc7c9c562013-01-25 10:18:51 +08001102static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1103{
1104 return vmcs_config.cpu_based_2nd_exec_ctrl &
1105 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1106}
1107
Yunhong Jiang64672c92016-06-13 14:19:59 -07001108/*
1109 * Comment's format: document - errata name - stepping - processor name.
1110 * Refer from
1111 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1112 */
1113static u32 vmx_preemption_cpu_tfms[] = {
1114/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11150x000206E6,
1116/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1117/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1118/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11190x00020652,
1120/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11210x00020655,
1122/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1123/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1124/*
1125 * 320767.pdf - AAP86 - B1 -
1126 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1127 */
11280x000106E5,
1129/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11300x000106A0,
1131/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11320x000106A1,
1133/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11340x000106A4,
1135 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1136 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1137 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11380x000106A5,
1139};
1140
1141static inline bool cpu_has_broken_vmx_preemption_timer(void)
1142{
1143 u32 eax = cpuid_eax(0x00000001), i;
1144
1145 /* Clear the reserved bits */
1146 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001147 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001148 if (eax == vmx_preemption_cpu_tfms[i])
1149 return true;
1150
1151 return false;
1152}
1153
1154static inline bool cpu_has_vmx_preemption_timer(void)
1155{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001156 return vmcs_config.pin_based_exec_ctrl &
1157 PIN_BASED_VMX_PREEMPTION_TIMER;
1158}
1159
Yang Zhang01e439b2013-04-11 19:25:12 +08001160static inline bool cpu_has_vmx_posted_intr(void)
1161{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001162 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1163 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001164}
1165
1166static inline bool cpu_has_vmx_apicv(void)
1167{
1168 return cpu_has_vmx_apic_register_virt() &&
1169 cpu_has_vmx_virtual_intr_delivery() &&
1170 cpu_has_vmx_posted_intr();
1171}
1172
Sheng Yang04547152009-04-01 15:52:31 +08001173static inline bool cpu_has_vmx_flexpriority(void)
1174{
1175 return cpu_has_vmx_tpr_shadow() &&
1176 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001177}
1178
Marcelo Tosattie7997942009-06-11 12:07:40 -03001179static inline bool cpu_has_vmx_ept_execute_only(void)
1180{
Gui Jianfeng31299942010-03-15 17:29:09 +08001181 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001182}
1183
Marcelo Tosattie7997942009-06-11 12:07:40 -03001184static inline bool cpu_has_vmx_ept_2m_page(void)
1185{
Gui Jianfeng31299942010-03-15 17:29:09 +08001186 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001187}
1188
Sheng Yang878403b2010-01-05 19:02:29 +08001189static inline bool cpu_has_vmx_ept_1g_page(void)
1190{
Gui Jianfeng31299942010-03-15 17:29:09 +08001191 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001192}
1193
Sheng Yang4bc9b982010-06-02 14:05:24 +08001194static inline bool cpu_has_vmx_ept_4levels(void)
1195{
1196 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1197}
1198
Xudong Hao83c3a332012-05-28 19:33:35 +08001199static inline bool cpu_has_vmx_ept_ad_bits(void)
1200{
1201 return vmx_capability.ept & VMX_EPT_AD_BIT;
1202}
1203
Gui Jianfeng31299942010-03-15 17:29:09 +08001204static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001205{
Gui Jianfeng31299942010-03-15 17:29:09 +08001206 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001207}
1208
Gui Jianfeng31299942010-03-15 17:29:09 +08001209static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001210{
Gui Jianfeng31299942010-03-15 17:29:09 +08001211 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001212}
1213
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001214static inline bool cpu_has_vmx_invvpid_single(void)
1215{
1216 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1217}
1218
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001219static inline bool cpu_has_vmx_invvpid_global(void)
1220{
1221 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1222}
1223
Wanpeng Li2df19692017-03-23 05:30:08 -07001224static inline bool cpu_has_vmx_invvpid(void)
1225{
1226 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1227}
1228
Gui Jianfeng31299942010-03-15 17:29:09 +08001229static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001230{
Sheng Yang04547152009-04-01 15:52:31 +08001231 return vmcs_config.cpu_based_2nd_exec_ctrl &
1232 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001233}
1234
Gui Jianfeng31299942010-03-15 17:29:09 +08001235static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001236{
1237 return vmcs_config.cpu_based_2nd_exec_ctrl &
1238 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1239}
1240
Gui Jianfeng31299942010-03-15 17:29:09 +08001241static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001242{
1243 return vmcs_config.cpu_based_2nd_exec_ctrl &
1244 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1245}
1246
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001247static inline bool cpu_has_vmx_basic_inout(void)
1248{
1249 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1250}
1251
Paolo Bonzini35754c92015-07-29 12:05:37 +02001252static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001253{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001254 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001255}
1256
Gui Jianfeng31299942010-03-15 17:29:09 +08001257static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001258{
Sheng Yang04547152009-04-01 15:52:31 +08001259 return vmcs_config.cpu_based_2nd_exec_ctrl &
1260 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001261}
1262
Gui Jianfeng31299942010-03-15 17:29:09 +08001263static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001264{
1265 return vmcs_config.cpu_based_2nd_exec_ctrl &
1266 SECONDARY_EXEC_RDTSCP;
1267}
1268
Mao, Junjiead756a12012-07-02 01:18:48 +00001269static inline bool cpu_has_vmx_invpcid(void)
1270{
1271 return vmcs_config.cpu_based_2nd_exec_ctrl &
1272 SECONDARY_EXEC_ENABLE_INVPCID;
1273}
1274
Gui Jianfeng31299942010-03-15 17:29:09 +08001275static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001276{
1277 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1278}
1279
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001280static inline bool cpu_has_vmx_wbinvd_exit(void)
1281{
1282 return vmcs_config.cpu_based_2nd_exec_ctrl &
1283 SECONDARY_EXEC_WBINVD_EXITING;
1284}
1285
Abel Gordonabc4fc52013-04-18 14:35:25 +03001286static inline bool cpu_has_vmx_shadow_vmcs(void)
1287{
1288 u64 vmx_msr;
1289 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1290 /* check if the cpu supports writing r/o exit information fields */
1291 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1292 return false;
1293
1294 return vmcs_config.cpu_based_2nd_exec_ctrl &
1295 SECONDARY_EXEC_SHADOW_VMCS;
1296}
1297
Kai Huang843e4332015-01-28 10:54:28 +08001298static inline bool cpu_has_vmx_pml(void)
1299{
1300 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1301}
1302
Haozhong Zhang64903d62015-10-20 15:39:09 +08001303static inline bool cpu_has_vmx_tsc_scaling(void)
1304{
1305 return vmcs_config.cpu_based_2nd_exec_ctrl &
1306 SECONDARY_EXEC_TSC_SCALING;
1307}
1308
Sheng Yang04547152009-04-01 15:52:31 +08001309static inline bool report_flexpriority(void)
1310{
1311 return flexpriority_enabled;
1312}
1313
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001314static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1315{
1316 return vmcs12->cpu_based_vm_exec_control & bit;
1317}
1318
1319static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1320{
1321 return (vmcs12->cpu_based_vm_exec_control &
1322 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1323 (vmcs12->secondary_vm_exec_control & bit);
1324}
1325
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001326static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001327{
1328 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1329}
1330
Jan Kiszkaf4124502014-03-07 20:03:13 +01001331static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1332{
1333 return vmcs12->pin_based_vm_exec_control &
1334 PIN_BASED_VMX_PREEMPTION_TIMER;
1335}
1336
Nadav Har'El155a97a2013-08-05 11:07:16 +03001337static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1338{
1339 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1340}
1341
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001342static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1343{
1344 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1345 vmx_xsaves_supported();
1346}
1347
Wincy Vanf2b93282015-02-03 23:56:03 +08001348static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1349{
1350 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1351}
1352
Wanpeng Li5c614b32015-10-13 09:18:36 -07001353static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1354{
1355 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1356}
1357
Wincy Van82f0dd42015-02-03 23:57:18 +08001358static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1359{
1360 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1361}
1362
Wincy Van608406e2015-02-03 23:57:51 +08001363static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1364{
1365 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1366}
1367
Wincy Van705699a2015-02-03 23:58:17 +08001368static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1369{
1370 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1371}
1372
Jim Mattson3f618a02016-12-12 11:01:37 -08001373static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001374{
1375 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattson3f618a02016-12-12 11:01:37 -08001376 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001377}
1378
Jan Kiszka533558b2014-01-04 18:47:20 +01001379static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1380 u32 exit_intr_info,
1381 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001382static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1383 struct vmcs12 *vmcs12,
1384 u32 reason, unsigned long qualification);
1385
Rusty Russell8b9cf982007-07-30 16:31:43 +10001386static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001387{
1388 int i;
1389
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001390 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001391 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001392 return i;
1393 return -1;
1394}
1395
Sheng Yang2384d2b2008-01-17 15:14:33 +08001396static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1397{
1398 struct {
1399 u64 vpid : 16;
1400 u64 rsvd : 48;
1401 u64 gva;
1402 } operand = { vpid, 0, gva };
1403
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001404 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001405 /* CF==1 or ZF==1 --> rc = -1 */
1406 "; ja 1f ; ud2 ; 1:"
1407 : : "a"(&operand), "c"(ext) : "cc", "memory");
1408}
1409
Sheng Yang14394422008-04-28 12:24:45 +08001410static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1411{
1412 struct {
1413 u64 eptp, gpa;
1414 } operand = {eptp, gpa};
1415
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001416 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001417 /* CF==1 or ZF==1 --> rc = -1 */
1418 "; ja 1f ; ud2 ; 1:\n"
1419 : : "a" (&operand), "c" (ext) : "cc", "memory");
1420}
1421
Avi Kivity26bb0982009-09-07 11:14:12 +03001422static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001423{
1424 int i;
1425
Rusty Russell8b9cf982007-07-30 16:31:43 +10001426 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001427 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001428 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001429 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001430}
1431
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432static void vmcs_clear(struct vmcs *vmcs)
1433{
1434 u64 phys_addr = __pa(vmcs);
1435 u8 error;
1436
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001437 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001438 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 : "cc", "memory");
1440 if (error)
1441 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1442 vmcs, phys_addr);
1443}
1444
Nadav Har'Eld462b812011-05-24 15:26:10 +03001445static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1446{
1447 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001448 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1449 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001450 loaded_vmcs->cpu = -1;
1451 loaded_vmcs->launched = 0;
1452}
1453
Dongxiao Xu7725b892010-05-11 18:29:38 +08001454static void vmcs_load(struct vmcs *vmcs)
1455{
1456 u64 phys_addr = __pa(vmcs);
1457 u8 error;
1458
1459 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001460 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001461 : "cc", "memory");
1462 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001463 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001464 vmcs, phys_addr);
1465}
1466
Dave Young2965faa2015-09-09 15:38:55 -07001467#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001468/*
1469 * This bitmap is used to indicate whether the vmclear
1470 * operation is enabled on all cpus. All disabled by
1471 * default.
1472 */
1473static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1474
1475static inline void crash_enable_local_vmclear(int cpu)
1476{
1477 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1478}
1479
1480static inline void crash_disable_local_vmclear(int cpu)
1481{
1482 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1483}
1484
1485static inline int crash_local_vmclear_enabled(int cpu)
1486{
1487 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1488}
1489
1490static void crash_vmclear_local_loaded_vmcss(void)
1491{
1492 int cpu = raw_smp_processor_id();
1493 struct loaded_vmcs *v;
1494
1495 if (!crash_local_vmclear_enabled(cpu))
1496 return;
1497
1498 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1499 loaded_vmcss_on_cpu_link)
1500 vmcs_clear(v->vmcs);
1501}
1502#else
1503static inline void crash_enable_local_vmclear(int cpu) { }
1504static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001505#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001506
Nadav Har'Eld462b812011-05-24 15:26:10 +03001507static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001509 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001510 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511
Nadav Har'Eld462b812011-05-24 15:26:10 +03001512 if (loaded_vmcs->cpu != cpu)
1513 return; /* vcpu migration can race with cpu offline */
1514 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001516 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001517 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001518
1519 /*
1520 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1521 * is before setting loaded_vmcs->vcpu to -1 which is done in
1522 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1523 * then adds the vmcs into percpu list before it is deleted.
1524 */
1525 smp_wmb();
1526
Nadav Har'Eld462b812011-05-24 15:26:10 +03001527 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001528 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529}
1530
Nadav Har'Eld462b812011-05-24 15:26:10 +03001531static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001532{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001533 int cpu = loaded_vmcs->cpu;
1534
1535 if (cpu != -1)
1536 smp_call_function_single(cpu,
1537 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001538}
1539
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001540static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001541{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001542 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001543 return;
1544
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001545 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001546 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001547}
1548
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001549static inline void vpid_sync_vcpu_global(void)
1550{
1551 if (cpu_has_vmx_invvpid_global())
1552 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1553}
1554
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001555static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001556{
1557 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001558 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001559 else
1560 vpid_sync_vcpu_global();
1561}
1562
Sheng Yang14394422008-04-28 12:24:45 +08001563static inline void ept_sync_global(void)
1564{
1565 if (cpu_has_vmx_invept_global())
1566 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1567}
1568
1569static inline void ept_sync_context(u64 eptp)
1570{
Avi Kivity089d0342009-03-23 18:26:32 +02001571 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001572 if (cpu_has_vmx_invept_context())
1573 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1574 else
1575 ept_sync_global();
1576 }
1577}
1578
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001579static __always_inline void vmcs_check16(unsigned long field)
1580{
1581 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1582 "16-bit accessor invalid for 64-bit field");
1583 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1584 "16-bit accessor invalid for 64-bit high field");
1585 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1586 "16-bit accessor invalid for 32-bit high field");
1587 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1588 "16-bit accessor invalid for natural width field");
1589}
1590
1591static __always_inline void vmcs_check32(unsigned long field)
1592{
1593 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1594 "32-bit accessor invalid for 16-bit field");
1595 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1596 "32-bit accessor invalid for natural width field");
1597}
1598
1599static __always_inline void vmcs_check64(unsigned long field)
1600{
1601 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1602 "64-bit accessor invalid for 16-bit field");
1603 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1604 "64-bit accessor invalid for 64-bit high field");
1605 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1606 "64-bit accessor invalid for 32-bit field");
1607 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1608 "64-bit accessor invalid for natural width field");
1609}
1610
1611static __always_inline void vmcs_checkl(unsigned long field)
1612{
1613 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1614 "Natural width accessor invalid for 16-bit field");
1615 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1616 "Natural width accessor invalid for 64-bit field");
1617 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1618 "Natural width accessor invalid for 64-bit high field");
1619 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1620 "Natural width accessor invalid for 32-bit field");
1621}
1622
1623static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624{
Avi Kivity5e520e62011-05-15 10:13:12 -04001625 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626
Avi Kivity5e520e62011-05-15 10:13:12 -04001627 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1628 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 return value;
1630}
1631
Avi Kivity96304212011-05-15 10:13:13 -04001632static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001634 vmcs_check16(field);
1635 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636}
1637
Avi Kivity96304212011-05-15 10:13:13 -04001638static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001640 vmcs_check32(field);
1641 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642}
1643
Avi Kivity96304212011-05-15 10:13:13 -04001644static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001646 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001647#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001648 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001650 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651#endif
1652}
1653
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001654static __always_inline unsigned long vmcs_readl(unsigned long field)
1655{
1656 vmcs_checkl(field);
1657 return __vmcs_readl(field);
1658}
1659
Avi Kivitye52de1b2007-01-05 16:36:56 -08001660static noinline void vmwrite_error(unsigned long field, unsigned long value)
1661{
1662 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1663 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1664 dump_stack();
1665}
1666
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001667static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668{
1669 u8 error;
1670
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001671 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001672 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001673 if (unlikely(error))
1674 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675}
1676
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001677static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001678{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001679 vmcs_check16(field);
1680 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681}
1682
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001683static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001685 vmcs_check32(field);
1686 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687}
1688
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001689static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001691 vmcs_check64(field);
1692 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001693#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001695 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696#endif
1697}
1698
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001699static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001700{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001701 vmcs_checkl(field);
1702 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001703}
1704
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001705static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001706{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001707 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1708 "vmcs_clear_bits does not support 64-bit fields");
1709 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1710}
1711
1712static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1713{
1714 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1715 "vmcs_set_bits does not support 64-bit fields");
1716 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001717}
1718
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001719static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1720{
1721 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1722}
1723
Gleb Natapov2961e8762013-11-25 15:37:13 +02001724static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1725{
1726 vmcs_write32(VM_ENTRY_CONTROLS, val);
1727 vmx->vm_entry_controls_shadow = val;
1728}
1729
1730static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1731{
1732 if (vmx->vm_entry_controls_shadow != val)
1733 vm_entry_controls_init(vmx, val);
1734}
1735
1736static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1737{
1738 return vmx->vm_entry_controls_shadow;
1739}
1740
1741
1742static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1743{
1744 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1745}
1746
1747static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1748{
1749 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1750}
1751
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001752static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1753{
1754 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1755}
1756
Gleb Natapov2961e8762013-11-25 15:37:13 +02001757static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1758{
1759 vmcs_write32(VM_EXIT_CONTROLS, val);
1760 vmx->vm_exit_controls_shadow = val;
1761}
1762
1763static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1764{
1765 if (vmx->vm_exit_controls_shadow != val)
1766 vm_exit_controls_init(vmx, val);
1767}
1768
1769static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1770{
1771 return vmx->vm_exit_controls_shadow;
1772}
1773
1774
1775static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1776{
1777 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1778}
1779
1780static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1781{
1782 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1783}
1784
Avi Kivity2fb92db2011-04-27 19:42:18 +03001785static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1786{
1787 vmx->segment_cache.bitmask = 0;
1788}
1789
1790static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1791 unsigned field)
1792{
1793 bool ret;
1794 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1795
1796 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1797 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1798 vmx->segment_cache.bitmask = 0;
1799 }
1800 ret = vmx->segment_cache.bitmask & mask;
1801 vmx->segment_cache.bitmask |= mask;
1802 return ret;
1803}
1804
1805static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1806{
1807 u16 *p = &vmx->segment_cache.seg[seg].selector;
1808
1809 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1810 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1811 return *p;
1812}
1813
1814static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1815{
1816 ulong *p = &vmx->segment_cache.seg[seg].base;
1817
1818 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1819 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1820 return *p;
1821}
1822
1823static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1824{
1825 u32 *p = &vmx->segment_cache.seg[seg].limit;
1826
1827 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1828 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1829 return *p;
1830}
1831
1832static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1833{
1834 u32 *p = &vmx->segment_cache.seg[seg].ar;
1835
1836 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1837 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1838 return *p;
1839}
1840
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001841static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1842{
1843 u32 eb;
1844
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001845 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01001846 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001847 if ((vcpu->guest_debug &
1848 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1849 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1850 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001851 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001852 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001853 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001854 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001855 if (vcpu->fpu_active)
1856 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001857
1858 /* When we are running a nested L2 guest and L1 specified for it a
1859 * certain exception bitmap, we must trap the same exceptions and pass
1860 * them to L1. When running L2, we will only handle the exceptions
1861 * specified above if L1 did not want them.
1862 */
1863 if (is_guest_mode(vcpu))
1864 eb |= get_vmcs12(vcpu)->exception_bitmap;
1865
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001866 vmcs_write32(EXCEPTION_BITMAP, eb);
1867}
1868
Ashok Raj70131292018-02-01 22:59:43 +01001869/*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01001870 * Check if MSR is intercepted for currently loaded MSR bitmap.
1871 */
1872static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
1873{
1874 unsigned long *msr_bitmap;
1875 int f = sizeof(unsigned long);
1876
1877 if (!cpu_has_vmx_msr_bitmap())
1878 return true;
1879
1880 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
1881
1882 if (msr <= 0x1fff) {
1883 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1884 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1885 msr &= 0x1fff;
1886 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1887 }
1888
1889 return true;
1890}
1891
1892/*
Ashok Raj70131292018-02-01 22:59:43 +01001893 * Check if MSR is intercepted for L01 MSR bitmap.
1894 */
1895static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
1896{
1897 unsigned long *msr_bitmap;
1898 int f = sizeof(unsigned long);
1899
1900 if (!cpu_has_vmx_msr_bitmap())
1901 return true;
1902
1903 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
1904
1905 if (msr <= 0x1fff) {
1906 return !!test_bit(msr, msr_bitmap + 0x800 / f);
1907 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1908 msr &= 0x1fff;
1909 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1910 }
1911
1912 return true;
1913}
1914
Gleb Natapov2961e8762013-11-25 15:37:13 +02001915static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1916 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001917{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001918 vm_entry_controls_clearbit(vmx, entry);
1919 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001920}
1921
Avi Kivity61d2ef22010-04-28 16:40:38 +03001922static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1923{
1924 unsigned i;
1925 struct msr_autoload *m = &vmx->msr_autoload;
1926
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001927 switch (msr) {
1928 case MSR_EFER:
1929 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001930 clear_atomic_switch_msr_special(vmx,
1931 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001932 VM_EXIT_LOAD_IA32_EFER);
1933 return;
1934 }
1935 break;
1936 case MSR_CORE_PERF_GLOBAL_CTRL:
1937 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001938 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001939 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1940 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1941 return;
1942 }
1943 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001944 }
1945
Avi Kivity61d2ef22010-04-28 16:40:38 +03001946 for (i = 0; i < m->nr; ++i)
1947 if (m->guest[i].index == msr)
1948 break;
1949
1950 if (i == m->nr)
1951 return;
1952 --m->nr;
1953 m->guest[i] = m->guest[m->nr];
1954 m->host[i] = m->host[m->nr];
1955 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1956 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1957}
1958
Gleb Natapov2961e8762013-11-25 15:37:13 +02001959static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1960 unsigned long entry, unsigned long exit,
1961 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1962 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001963{
1964 vmcs_write64(guest_val_vmcs, guest_val);
1965 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001966 vm_entry_controls_setbit(vmx, entry);
1967 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001968}
1969
Avi Kivity61d2ef22010-04-28 16:40:38 +03001970static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1971 u64 guest_val, u64 host_val)
1972{
1973 unsigned i;
1974 struct msr_autoload *m = &vmx->msr_autoload;
1975
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001976 switch (msr) {
1977 case MSR_EFER:
1978 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001979 add_atomic_switch_msr_special(vmx,
1980 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001981 VM_EXIT_LOAD_IA32_EFER,
1982 GUEST_IA32_EFER,
1983 HOST_IA32_EFER,
1984 guest_val, host_val);
1985 return;
1986 }
1987 break;
1988 case MSR_CORE_PERF_GLOBAL_CTRL:
1989 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001990 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001991 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1992 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1993 GUEST_IA32_PERF_GLOBAL_CTRL,
1994 HOST_IA32_PERF_GLOBAL_CTRL,
1995 guest_val, host_val);
1996 return;
1997 }
1998 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001999 case MSR_IA32_PEBS_ENABLE:
2000 /* PEBS needs a quiescent period after being disabled (to write
2001 * a record). Disabling PEBS through VMX MSR swapping doesn't
2002 * provide that period, so a CPU could write host's record into
2003 * guest's memory.
2004 */
2005 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002006 }
2007
Avi Kivity61d2ef22010-04-28 16:40:38 +03002008 for (i = 0; i < m->nr; ++i)
2009 if (m->guest[i].index == msr)
2010 break;
2011
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002012 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002013 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002014 "Can't add msr %x\n", msr);
2015 return;
2016 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03002017 ++m->nr;
2018 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
2019 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
2020 }
2021
2022 m->guest[i].index = msr;
2023 m->guest[i].value = guest_val;
2024 m->host[i].index = msr;
2025 m->host[i].value = host_val;
2026}
2027
Avi Kivity33ed6322007-05-02 16:54:03 +03002028static void reload_tss(void)
2029{
Avi Kivity33ed6322007-05-02 16:54:03 +03002030 /*
2031 * VT restores TR but not its size. Useless.
2032 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05002033 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02002034 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03002035
Avi Kivityd3591922010-07-26 18:32:39 +03002036 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03002037 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
2038 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03002039}
2040
Avi Kivity92c0d902009-10-29 11:00:16 +02002041static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002042{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002043 u64 guest_efer = vmx->vcpu.arch.efer;
2044 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002045
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002046 if (!enable_ept) {
2047 /*
2048 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2049 * host CPUID is more efficient than testing guest CPUID
2050 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2051 */
2052 if (boot_cpu_has(X86_FEATURE_SMEP))
2053 guest_efer |= EFER_NX;
2054 else if (!(guest_efer & EFER_NX))
2055 ignore_bits |= EFER_NX;
2056 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002057
Avi Kivity51c6cf62007-08-29 03:48:05 +03002058 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002059 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002060 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002061 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002062#ifdef CONFIG_X86_64
2063 ignore_bits |= EFER_LMA | EFER_LME;
2064 /* SCE is meaningful only in long mode on Intel */
2065 if (guest_efer & EFER_LMA)
2066 ignore_bits &= ~(u64)EFER_SCE;
2067#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002068
2069 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002070
2071 /*
2072 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2073 * On CPUs that support "load IA32_EFER", always switch EFER
2074 * atomically, since it's faster than switching it manually.
2075 */
2076 if (cpu_has_load_ia32_efer ||
2077 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002078 if (!(guest_efer & EFER_LMA))
2079 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002080 if (guest_efer != host_efer)
2081 add_atomic_switch_msr(vmx, MSR_EFER,
2082 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002083 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002084 } else {
2085 guest_efer &= ~ignore_bits;
2086 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002087
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002088 vmx->guest_msrs[efer_offset].data = guest_efer;
2089 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2090
2091 return true;
2092 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002093}
2094
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002095static unsigned long segment_base(u16 selector)
2096{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002097 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002098 struct desc_struct *d;
2099 unsigned long table_base;
2100 unsigned long v;
2101
2102 if (!(selector & ~3))
2103 return 0;
2104
Avi Kivityd3591922010-07-26 18:32:39 +03002105 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002106
2107 if (selector & 4) { /* from ldt */
2108 u16 ldt_selector = kvm_read_ldt();
2109
2110 if (!(ldt_selector & ~3))
2111 return 0;
2112
2113 table_base = segment_base(ldt_selector);
2114 }
2115 d = (struct desc_struct *)(table_base + (selector & ~7));
2116 v = get_desc_base(d);
2117#ifdef CONFIG_X86_64
2118 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2119 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2120#endif
2121 return v;
2122}
2123
2124static inline unsigned long kvm_read_tr_base(void)
2125{
2126 u16 tr;
2127 asm("str %0" : "=g"(tr));
2128 return segment_base(tr);
2129}
2130
Avi Kivity04d2cc72007-09-10 18:10:54 +03002131static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002132{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002133 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002134 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002135
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002136 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002137 return;
2138
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002139 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002140 /*
2141 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2142 * allow segment selectors with cpl > 0 or ti == 1.
2143 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002144 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002145 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002146 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002147 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002148 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002149 vmx->host_state.fs_reload_needed = 0;
2150 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002151 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002152 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002153 }
Avi Kivity9581d442010-10-19 16:46:55 +02002154 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002155 if (!(vmx->host_state.gs_sel & 7))
2156 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002157 else {
2158 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002159 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002160 }
2161
2162#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002163 savesegment(ds, vmx->host_state.ds_sel);
2164 savesegment(es, vmx->host_state.es_sel);
2165#endif
2166
2167#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002168 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2169 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2170#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002171 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2172 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002173#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002174
2175#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002176 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2177 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002178 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002179#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002180 if (boot_cpu_has(X86_FEATURE_MPX))
2181 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002182 for (i = 0; i < vmx->save_nmsrs; ++i)
2183 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002184 vmx->guest_msrs[i].data,
2185 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002186}
2187
Avi Kivitya9b21b62008-06-24 11:48:49 +03002188static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002189{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002190 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002191 return;
2192
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002193 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002194 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002195#ifdef CONFIG_X86_64
2196 if (is_long_mode(&vmx->vcpu))
2197 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2198#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002199 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002200 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002201#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002202 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002203#else
2204 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002205#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002206 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002207 if (vmx->host_state.fs_reload_needed)
2208 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002209#ifdef CONFIG_X86_64
2210 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2211 loadsegment(ds, vmx->host_state.ds_sel);
2212 loadsegment(es, vmx->host_state.es_sel);
2213 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002214#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002215 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002216#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002217 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002218#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002219 if (vmx->host_state.msr_host_bndcfgs)
2220 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002221 /*
2222 * If the FPU is not active (through the host task or
2223 * the guest vcpu), then restore the cr0.TS bit.
2224 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002225 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002226 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002227 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002228}
2229
Avi Kivitya9b21b62008-06-24 11:48:49 +03002230static void vmx_load_host_state(struct vcpu_vmx *vmx)
2231{
2232 preempt_disable();
2233 __vmx_load_host_state(vmx);
2234 preempt_enable();
2235}
2236
Feng Wu28b835d2015-09-18 22:29:54 +08002237static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2238{
2239 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2240 struct pi_desc old, new;
2241 unsigned int dest;
2242
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002243 /*
2244 * In case of hot-plug or hot-unplug, we may have to undo
2245 * vmx_vcpu_pi_put even if there is no assigned device. And we
2246 * always keep PI.NDST up to date for simplicity: it makes the
2247 * code easier, and CPU migration is not a fast path.
2248 */
2249 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002250 return;
2251
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002252 /*
2253 * First handle the simple case where no cmpxchg is necessary; just
2254 * allow posting non-urgent interrupts.
2255 *
2256 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2257 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2258 * expects the VCPU to be on the blocked_vcpu_list that matches
2259 * PI.NDST.
2260 */
2261 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2262 vcpu->cpu == cpu) {
2263 pi_clear_sn(pi_desc);
2264 return;
2265 }
2266
2267 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002268 do {
2269 old.control = new.control = pi_desc->control;
2270
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002271 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002272
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002273 if (x2apic_enabled())
2274 new.ndst = dest;
2275 else
2276 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002277
Feng Wu28b835d2015-09-18 22:29:54 +08002278 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002279 } while (cmpxchg64(&pi_desc->control, old.control,
2280 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002281}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002282
Peter Feinerc95ba922016-08-17 09:36:47 -07002283static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2284{
2285 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2286 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2287}
2288
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289/*
2290 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2291 * vcpu mutex is already taken.
2292 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002293static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002295 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002296 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002297 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002299 if (!vmm_exclusive)
2300 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002301 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002302 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002304 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002305 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002306 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002307
2308 /*
2309 * Read loaded_vmcs->cpu should be before fetching
2310 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2311 * See the comments in __loaded_vmcs_clear().
2312 */
2313 smp_rmb();
2314
Nadav Har'Eld462b812011-05-24 15:26:10 +03002315 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2316 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002317 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002318 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002319 }
2320
2321 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2322 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2323 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj70131292018-02-01 22:59:43 +01002324 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002325 }
2326
2327 if (!already_loaded) {
2328 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2329 unsigned long sysenter_esp;
2330
2331 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002332
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 /*
2334 * Linux uses per-cpu TSS and GDT, so set these when switching
2335 * processors.
2336 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002337 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002338 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339
2340 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2341 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002342
Nadav Har'Eld462b812011-05-24 15:26:10 +03002343 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344 }
Feng Wu28b835d2015-09-18 22:29:54 +08002345
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002346 /* Setup TSC multiplier */
2347 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002348 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2349 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002350
Feng Wu28b835d2015-09-18 22:29:54 +08002351 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002352 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002353}
2354
2355static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2356{
2357 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2358
2359 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002360 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2361 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002362 return;
2363
2364 /* Set SN when the vCPU is preempted */
2365 if (vcpu->preempted)
2366 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367}
2368
2369static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2370{
Feng Wu28b835d2015-09-18 22:29:54 +08002371 vmx_vcpu_pi_put(vcpu);
2372
Avi Kivitya9b21b62008-06-24 11:48:49 +03002373 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002374 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002375 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2376 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002377 kvm_cpu_vmxoff();
2378 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379}
2380
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002381static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2382{
Avi Kivity81231c62010-01-24 16:26:40 +02002383 ulong cr0;
2384
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002385 if (vcpu->fpu_active)
2386 return;
2387 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002388 cr0 = vmcs_readl(GUEST_CR0);
2389 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2390 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2391 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002392 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002393 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002394 if (is_guest_mode(vcpu))
2395 vcpu->arch.cr0_guest_owned_bits &=
2396 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002397 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002398}
2399
Avi Kivityedcafe32009-12-30 18:07:40 +02002400static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2401
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002402/*
2403 * Return the cr0 value that a nested guest would read. This is a combination
2404 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2405 * its hypervisor (cr0_read_shadow).
2406 */
2407static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2408{
2409 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2410 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2411}
2412static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2413{
2414 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2415 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2416}
2417
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002418static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2419{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002420 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2421 * set this *before* calling this function.
2422 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002423 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002424 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002425 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002426 vcpu->arch.cr0_guest_owned_bits = 0;
2427 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002428 if (is_guest_mode(vcpu)) {
2429 /*
2430 * L1's specified read shadow might not contain the TS bit,
2431 * so now that we turned on shadowing of this bit, we need to
2432 * set this bit of the shadow. Like in nested_vmx_run we need
2433 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2434 * up-to-date here because we just decached cr0.TS (and we'll
2435 * only update vmcs12->guest_cr0 on nested exit).
2436 */
2437 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2438 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2439 (vcpu->arch.cr0 & X86_CR0_TS);
2440 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2441 } else
2442 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002443}
2444
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2446{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002447 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002448
Avi Kivity6de12732011-03-07 12:51:22 +02002449 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2450 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2451 rflags = vmcs_readl(GUEST_RFLAGS);
2452 if (to_vmx(vcpu)->rmode.vm86_active) {
2453 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2454 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2455 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2456 }
2457 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002458 }
Avi Kivity6de12732011-03-07 12:51:22 +02002459 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002460}
2461
2462static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2463{
Avi Kivity6de12732011-03-07 12:51:22 +02002464 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2465 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002466 if (to_vmx(vcpu)->rmode.vm86_active) {
2467 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002468 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002469 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470 vmcs_writel(GUEST_RFLAGS, rflags);
2471}
2472
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002473static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2474{
2475 return to_vmx(vcpu)->guest_pkru;
2476}
2477
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002478static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002479{
2480 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2481 int ret = 0;
2482
2483 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002484 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002485 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002486 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002487
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002488 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002489}
2490
2491static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2492{
2493 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2494 u32 interruptibility = interruptibility_old;
2495
2496 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2497
Jan Kiszka48005f62010-02-19 19:38:07 +01002498 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002499 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002500 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002501 interruptibility |= GUEST_INTR_STATE_STI;
2502
2503 if ((interruptibility != interruptibility_old))
2504 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2505}
2506
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2508{
2509 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002511 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002513 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002514
Glauber Costa2809f5d2009-05-12 16:21:05 -04002515 /* skipping an emulated instruction also counts */
2516 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517}
2518
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002519/*
2520 * KVM wants to inject page-faults which it got to the guest. This function
2521 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002522 */
Gleb Natapove011c662013-09-25 12:51:35 +03002523static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002524{
2525 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2526
Gleb Natapove011c662013-09-25 12:51:35 +03002527 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002528 return 0;
2529
Wanpeng Lia29fd272017-06-05 05:19:09 -07002530 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002531 vmcs_read32(VM_EXIT_INTR_INFO),
2532 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002533 return 1;
2534}
2535
Avi Kivity298101d2007-11-25 13:41:11 +02002536static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002537 bool has_error_code, u32 error_code,
2538 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002539{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002540 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002541 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002542
Gleb Natapove011c662013-09-25 12:51:35 +03002543 if (!reinject && is_guest_mode(vcpu) &&
2544 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002545 return;
2546
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002547 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002548 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002549 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2550 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002551
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002552 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002553 int inc_eip = 0;
2554 if (kvm_exception_is_soft(nr))
2555 inc_eip = vcpu->arch.event_exit_inst_len;
2556 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002557 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002558 return;
2559 }
2560
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002561 if (kvm_exception_is_soft(nr)) {
2562 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2563 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002564 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2565 } else
2566 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2567
2568 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002569}
2570
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002571static bool vmx_rdtscp_supported(void)
2572{
2573 return cpu_has_vmx_rdtscp();
2574}
2575
Mao, Junjiead756a12012-07-02 01:18:48 +00002576static bool vmx_invpcid_supported(void)
2577{
2578 return cpu_has_vmx_invpcid() && enable_ept;
2579}
2580
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581/*
Eddie Donga75beee2007-05-17 18:55:15 +03002582 * Swap MSR entry in host/guest MSR entry array.
2583 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002584static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002585{
Avi Kivity26bb0982009-09-07 11:14:12 +03002586 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002587
2588 tmp = vmx->guest_msrs[to];
2589 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2590 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002591}
2592
2593/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002594 * Set up the vmcs to automatically save and restore system
2595 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2596 * mode, as fiddling with msrs is very expensive.
2597 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002598static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002599{
Avi Kivity26bb0982009-09-07 11:14:12 +03002600 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002601
Eddie Donga75beee2007-05-17 18:55:15 +03002602 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002603#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002604 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002605 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002606 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002607 move_msr_up(vmx, index, save_nmsrs++);
2608 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002609 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002610 move_msr_up(vmx, index, save_nmsrs++);
2611 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002612 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002613 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002614 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002615 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002616 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002617 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002618 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002619 * if efer.sce is enabled.
2620 */
Brian Gerst8c065852010-07-17 09:03:26 -04002621 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002622 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002623 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002624 }
Eddie Donga75beee2007-05-17 18:55:15 +03002625#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002626 index = __find_msr_index(vmx, MSR_EFER);
2627 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002628 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002629
Avi Kivity26bb0982009-09-07 11:14:12 +03002630 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002631
Yang Zhang8d146952013-01-25 10:18:50 +08002632 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002633 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002634}
2635
2636/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002638 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2639 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002641static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642{
2643 u64 host_tsc, tsc_offset;
2644
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002645 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002647 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648}
2649
2650/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002651 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002653static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002655 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002656 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002657 * We're here if L1 chose not to trap WRMSR to TSC. According
2658 * to the spec, this should set L1's TSC; The offset that L1
2659 * set for L2 remains unchanged, and still needs to be added
2660 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002661 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002662 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002663 /* recalculate vmcs02.TSC_OFFSET: */
2664 vmcs12 = get_vmcs12(vcpu);
2665 vmcs_write64(TSC_OFFSET, offset +
2666 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2667 vmcs12->tsc_offset : 0));
2668 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002669 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2670 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002671 vmcs_write64(TSC_OFFSET, offset);
2672 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002673}
2674
Nadav Har'El801d3422011-05-25 23:02:23 +03002675static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2676{
2677 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2678 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2679}
2680
2681/*
2682 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2683 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2684 * all guests if the "nested" module option is off, and can also be disabled
2685 * for a single guest by disabling its VMX cpuid bit.
2686 */
2687static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2688{
2689 return nested && guest_cpuid_has_vmx(vcpu);
2690}
2691
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002693 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2694 * returned for the various VMX controls MSRs when nested VMX is enabled.
2695 * The same values should also be used to verify that vmcs12 control fields are
2696 * valid during nested entry from L1 to L2.
2697 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2698 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2699 * bit in the high half is on if the corresponding bit in the control field
2700 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002701 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002702static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002703{
2704 /*
2705 * Note that as a general rule, the high half of the MSRs (bits in
2706 * the control fields which may be 1) should be initialized by the
2707 * intersection of the underlying hardware's MSR (i.e., features which
2708 * can be supported) and the list of features we want to expose -
2709 * because they are known to be properly supported in our code.
2710 * Also, usually, the low half of the MSRs (bits which must be 1) can
2711 * be set to 0, meaning that L1 may turn off any of these bits. The
2712 * reason is that if one of these bits is necessary, it will appear
2713 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2714 * fields of vmcs01 and vmcs02, will turn these bits off - and
2715 * nested_vmx_exit_handled() will not pass related exits to L1.
2716 * These rules have exceptions below.
2717 */
2718
2719 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002720 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002721 vmx->nested.nested_vmx_pinbased_ctls_low,
2722 vmx->nested.nested_vmx_pinbased_ctls_high);
2723 vmx->nested.nested_vmx_pinbased_ctls_low |=
2724 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2725 vmx->nested.nested_vmx_pinbased_ctls_high &=
2726 PIN_BASED_EXT_INTR_MASK |
2727 PIN_BASED_NMI_EXITING |
2728 PIN_BASED_VIRTUAL_NMIS;
2729 vmx->nested.nested_vmx_pinbased_ctls_high |=
2730 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002731 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002732 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002733 vmx->nested.nested_vmx_pinbased_ctls_high |=
2734 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002735
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002736 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002737 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002738 vmx->nested.nested_vmx_exit_ctls_low,
2739 vmx->nested.nested_vmx_exit_ctls_high);
2740 vmx->nested.nested_vmx_exit_ctls_low =
2741 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002742
Wincy Vanb9c237b2015-02-03 23:56:30 +08002743 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002744#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002745 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002746#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002747 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002748 vmx->nested.nested_vmx_exit_ctls_high |=
2749 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002750 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002751 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2752
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002753 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002754 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002755
Jan Kiszka2996fca2014-06-16 13:59:43 +02002756 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002757 vmx->nested.nested_vmx_true_exit_ctls_low =
2758 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002759 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2760
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002761 /* entry controls */
2762 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002763 vmx->nested.nested_vmx_entry_ctls_low,
2764 vmx->nested.nested_vmx_entry_ctls_high);
2765 vmx->nested.nested_vmx_entry_ctls_low =
2766 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2767 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002768#ifdef CONFIG_X86_64
2769 VM_ENTRY_IA32E_MODE |
2770#endif
2771 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002772 vmx->nested.nested_vmx_entry_ctls_high |=
2773 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002774 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002775 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002776
Jan Kiszka2996fca2014-06-16 13:59:43 +02002777 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002778 vmx->nested.nested_vmx_true_entry_ctls_low =
2779 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002780 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2781
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002782 /* cpu-based controls */
2783 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002784 vmx->nested.nested_vmx_procbased_ctls_low,
2785 vmx->nested.nested_vmx_procbased_ctls_high);
2786 vmx->nested.nested_vmx_procbased_ctls_low =
2787 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2788 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002789 CPU_BASED_VIRTUAL_INTR_PENDING |
2790 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002791 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2792 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2793 CPU_BASED_CR3_STORE_EXITING |
2794#ifdef CONFIG_X86_64
2795 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2796#endif
2797 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002798 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2799 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2800 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2801 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002802 /*
2803 * We can allow some features even when not supported by the
2804 * hardware. For example, L1 can specify an MSR bitmap - and we
2805 * can use it to avoid exits to L1 - even when L0 runs L2
2806 * without MSR bitmaps.
2807 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002808 vmx->nested.nested_vmx_procbased_ctls_high |=
2809 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002810 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002811
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002812 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002813 vmx->nested.nested_vmx_true_procbased_ctls_low =
2814 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002815 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2816
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002817 /* secondary cpu-based controls */
2818 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002819 vmx->nested.nested_vmx_secondary_ctls_low,
2820 vmx->nested.nested_vmx_secondary_ctls_high);
2821 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2822 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002823 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002824 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002825 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002826 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002827 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002828 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002829 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002830 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002831
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002832 if (enable_ept) {
2833 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002834 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002835 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002836 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002837 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2838 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002839 if (cpu_has_vmx_ept_execute_only())
2840 vmx->nested.nested_vmx_ept_caps |=
2841 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002842 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002843 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2844 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002845 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002846 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002847
Paolo Bonzinief697a72016-03-18 16:58:38 +01002848 /*
2849 * Old versions of KVM use the single-context version without
2850 * checking for support, so declare that it is supported even
2851 * though it is treated as global context. The alternative is
2852 * not failing the single-context invvpid, and it is worse.
2853 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002854 if (enable_vpid)
2855 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01002856 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002857 else
2858 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002859
Radim Krčmář0790ec12015-03-17 14:02:32 +01002860 if (enable_unrestricted_guest)
2861 vmx->nested.nested_vmx_secondary_ctls_high |=
2862 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2863
Jan Kiszkac18911a2013-03-13 16:06:41 +01002864 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002865 rdmsr(MSR_IA32_VMX_MISC,
2866 vmx->nested.nested_vmx_misc_low,
2867 vmx->nested.nested_vmx_misc_high);
2868 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2869 vmx->nested.nested_vmx_misc_low |=
2870 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002871 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002872 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002873}
2874
2875static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2876{
2877 /*
2878 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2879 */
2880 return ((control & high) | low) == control;
2881}
2882
2883static inline u64 vmx_control_msr(u32 low, u32 high)
2884{
2885 return low | ((u64)high << 32);
2886}
2887
Jan Kiszkacae50132014-01-04 18:47:22 +01002888/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002889static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2890{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002891 struct vcpu_vmx *vmx = to_vmx(vcpu);
2892
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002893 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002894 case MSR_IA32_VMX_BASIC:
2895 /*
2896 * This MSR reports some information about VMX support. We
2897 * should return information about the VMX we emulate for the
2898 * guest, and the VMCS structure we give it - not about the
2899 * VMX support of the underlying hardware.
2900 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002901 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002902 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2903 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002904 if (cpu_has_vmx_basic_inout())
2905 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002906 break;
2907 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2908 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002909 *pdata = vmx_control_msr(
2910 vmx->nested.nested_vmx_pinbased_ctls_low,
2911 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002912 break;
2913 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002914 *pdata = vmx_control_msr(
2915 vmx->nested.nested_vmx_true_procbased_ctls_low,
2916 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002917 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002918 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002919 *pdata = vmx_control_msr(
2920 vmx->nested.nested_vmx_procbased_ctls_low,
2921 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002922 break;
2923 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002924 *pdata = vmx_control_msr(
2925 vmx->nested.nested_vmx_true_exit_ctls_low,
2926 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002927 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002928 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002929 *pdata = vmx_control_msr(
2930 vmx->nested.nested_vmx_exit_ctls_low,
2931 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002932 break;
2933 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002934 *pdata = vmx_control_msr(
2935 vmx->nested.nested_vmx_true_entry_ctls_low,
2936 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002937 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002938 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002939 *pdata = vmx_control_msr(
2940 vmx->nested.nested_vmx_entry_ctls_low,
2941 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002942 break;
2943 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002944 *pdata = vmx_control_msr(
2945 vmx->nested.nested_vmx_misc_low,
2946 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002947 break;
2948 /*
2949 * These MSRs specify bits which the guest must keep fixed (on or off)
2950 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2951 * We picked the standard core2 setting.
2952 */
2953#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2954#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2955 case MSR_IA32_VMX_CR0_FIXED0:
2956 *pdata = VMXON_CR0_ALWAYSON;
2957 break;
2958 case MSR_IA32_VMX_CR0_FIXED1:
2959 *pdata = -1ULL;
2960 break;
2961 case MSR_IA32_VMX_CR4_FIXED0:
2962 *pdata = VMXON_CR4_ALWAYSON;
2963 break;
2964 case MSR_IA32_VMX_CR4_FIXED1:
2965 *pdata = -1ULL;
2966 break;
2967 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002968 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002969 break;
2970 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002971 *pdata = vmx_control_msr(
2972 vmx->nested.nested_vmx_secondary_ctls_low,
2973 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002974 break;
2975 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07002976 *pdata = vmx->nested.nested_vmx_ept_caps |
2977 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002978 break;
2979 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002980 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002981 }
2982
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002983 return 0;
2984}
2985
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002986static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2987 uint64_t val)
2988{
2989 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2990
2991 return !(val & ~valid_bits);
2992}
2993
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002994/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002995 * Reads an msr value (of 'msr_index') into 'pdata'.
2996 * Returns 0 on success, non-0 otherwise.
2997 * Assumes vcpu_load() was already called.
2998 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002999static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003000{
Avi Kivity26bb0982009-09-07 11:14:12 +03003001 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003003 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003004#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003006 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 break;
3008 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003009 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003011 case MSR_KERNEL_GS_BASE:
3012 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003013 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003014 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003015#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003017 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303018 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003019 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003020 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003021 case MSR_IA32_SPEC_CTRL:
3022 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003023 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003024 return 1;
3025
3026 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3027 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003028 case MSR_IA32_ARCH_CAPABILITIES:
3029 if (!msr_info->host_initiated &&
3030 !guest_cpuid_has_arch_capabilities(vcpu))
3031 return 1;
3032 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3033 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003034 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003035 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036 break;
3037 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003038 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039 break;
3040 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003041 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003043 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003044 if (!kvm_mpx_supported() ||
3045 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003046 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003047 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003048 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003049 case MSR_IA32_MCG_EXT_CTL:
3050 if (!msr_info->host_initiated &&
3051 !(to_vmx(vcpu)->msr_ia32_feature_control &
3052 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003053 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003054 msr_info->data = vcpu->arch.mcg_ext_ctl;
3055 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003056 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003057 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003058 break;
3059 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3060 if (!nested_vmx_allowed(vcpu))
3061 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003062 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003063 case MSR_IA32_XSS:
3064 if (!vmx_xsaves_supported())
3065 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003066 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003067 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003068 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003069 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003070 return 1;
3071 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003073 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003074 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003075 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003076 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003078 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 }
3080
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 return 0;
3082}
3083
Jan Kiszkacae50132014-01-04 18:47:22 +01003084static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3085
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086/*
3087 * Writes msr value into into the appropriate "register".
3088 * Returns 0 on success, non-0 otherwise.
3089 * Assumes vcpu_load() was already called.
3090 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003091static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003093 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003094 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003095 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003096 u32 msr_index = msr_info->index;
3097 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003098
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003100 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003101 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003102 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003103#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003105 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 vmcs_writel(GUEST_FS_BASE, data);
3107 break;
3108 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003109 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110 vmcs_writel(GUEST_GS_BASE, data);
3111 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003112 case MSR_KERNEL_GS_BASE:
3113 vmx_load_host_state(vmx);
3114 vmx->msr_guest_kernel_gs_base = data;
3115 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116#endif
3117 case MSR_IA32_SYSENTER_CS:
3118 vmcs_write32(GUEST_SYSENTER_CS, data);
3119 break;
3120 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003121 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003122 break;
3123 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003124 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003126 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003127 if (!kvm_mpx_supported() ||
3128 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003129 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003130 if (is_noncanonical_address(data & PAGE_MASK) ||
3131 (data & MSR_IA32_BNDCFGS_RSVD))
3132 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003133 vmcs_write64(GUEST_BNDCFGS, data);
3134 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303135 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003136 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003138 case MSR_IA32_SPEC_CTRL:
3139 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003140 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003141 return 1;
3142
3143 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003144 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003145 return 1;
3146
3147 vmx->spec_ctrl = data;
3148
3149 if (!data)
3150 break;
3151
3152 /*
3153 * For non-nested:
3154 * When it's written (to non-zero) for the first time, pass
3155 * it through.
3156 *
3157 * For nested:
3158 * The handling of the MSR bitmap for L2 guests is done in
3159 * nested_vmx_merge_msr_bitmap. We should not touch the
3160 * vmcs02.msr_bitmap here since it gets completely overwritten
3161 * in the merging. We update the vmcs01 here for L1 as well
3162 * since it will end up touching the MSR anyway now.
3163 */
3164 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3165 MSR_IA32_SPEC_CTRL,
3166 MSR_TYPE_RW);
3167 break;
Ashok Raj70131292018-02-01 22:59:43 +01003168 case MSR_IA32_PRED_CMD:
3169 if (!msr_info->host_initiated &&
3170 !guest_cpuid_has_ibpb(vcpu))
3171 return 1;
3172
3173 if (data & ~PRED_CMD_IBPB)
3174 return 1;
3175
3176 if (!data)
3177 break;
3178
3179 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3180
3181 /*
3182 * For non-nested:
3183 * When it's written (to non-zero) for the first time, pass
3184 * it through.
3185 *
3186 * For nested:
3187 * The handling of the MSR bitmap for L2 guests is done in
3188 * nested_vmx_merge_msr_bitmap. We should not touch the
3189 * vmcs02.msr_bitmap here since it gets completely overwritten
3190 * in the merging.
3191 */
3192 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3193 MSR_TYPE_W);
3194 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003195 case MSR_IA32_ARCH_CAPABILITIES:
3196 if (!msr_info->host_initiated)
3197 return 1;
3198 vmx->arch_capabilities = data;
3199 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003200 case MSR_IA32_CR_PAT:
3201 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003202 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3203 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003204 vmcs_write64(GUEST_IA32_PAT, data);
3205 vcpu->arch.pat = data;
3206 break;
3207 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003208 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003209 break;
Will Auldba904632012-11-29 12:42:50 -08003210 case MSR_IA32_TSC_ADJUST:
3211 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003212 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003213 case MSR_IA32_MCG_EXT_CTL:
3214 if ((!msr_info->host_initiated &&
3215 !(to_vmx(vcpu)->msr_ia32_feature_control &
3216 FEATURE_CONTROL_LMCE)) ||
3217 (data & ~MCG_EXT_CTL_LMCE_EN))
3218 return 1;
3219 vcpu->arch.mcg_ext_ctl = data;
3220 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003221 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003222 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003223 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003224 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3225 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003226 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003227 if (msr_info->host_initiated && data == 0)
3228 vmx_leave_nested(vcpu);
3229 break;
3230 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3231 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003232 case MSR_IA32_XSS:
3233 if (!vmx_xsaves_supported())
3234 return 1;
3235 /*
3236 * The only supported bit as of Skylake is bit 8, but
3237 * it is not supported on KVM.
3238 */
3239 if (data != 0)
3240 return 1;
3241 vcpu->arch.ia32_xss = data;
3242 if (vcpu->arch.ia32_xss != host_xss)
3243 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3244 vcpu->arch.ia32_xss, host_xss);
3245 else
3246 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3247 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003248 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003249 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003250 return 1;
3251 /* Check reserved bit, higher 32 bits should be zero */
3252 if ((data >> 32) != 0)
3253 return 1;
3254 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003256 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003257 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003258 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003259 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003260 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3261 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003262 ret = kvm_set_shared_msr(msr->index, msr->data,
3263 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003264 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003265 if (ret)
3266 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003267 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003268 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003270 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 }
3272
Eddie Dong2cc51562007-05-21 07:28:09 +03003273 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274}
3275
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003276static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003278 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3279 switch (reg) {
3280 case VCPU_REGS_RSP:
3281 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3282 break;
3283 case VCPU_REGS_RIP:
3284 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3285 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003286 case VCPU_EXREG_PDPTR:
3287 if (enable_ept)
3288 ept_save_pdptrs(vcpu);
3289 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003290 default:
3291 break;
3292 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293}
3294
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295static __init int cpu_has_kvm_support(void)
3296{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003297 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003298}
3299
3300static __init int vmx_disabled_by_bios(void)
3301{
3302 u64 msr;
3303
3304 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003305 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003306 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003307 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3308 && tboot_enabled())
3309 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003310 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003311 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003312 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003313 && !tboot_enabled()) {
3314 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003315 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003316 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003317 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003318 /* launched w/o TXT and VMX disabled */
3319 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3320 && !tboot_enabled())
3321 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003322 }
3323
3324 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003325}
3326
Dongxiao Xu7725b892010-05-11 18:29:38 +08003327static void kvm_cpu_vmxon(u64 addr)
3328{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003329 intel_pt_handle_vmx(1);
3330
Dongxiao Xu7725b892010-05-11 18:29:38 +08003331 asm volatile (ASM_VMX_VMXON_RAX
3332 : : "a"(&addr), "m"(addr)
3333 : "memory", "cc");
3334}
3335
Radim Krčmář13a34e02014-08-28 15:13:03 +02003336static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337{
3338 int cpu = raw_smp_processor_id();
3339 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003340 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003341
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003342 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003343 return -EBUSY;
3344
Nadav Har'Eld462b812011-05-24 15:26:10 +03003345 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003346 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3347 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003348
3349 /*
3350 * Now we can enable the vmclear operation in kdump
3351 * since the loaded_vmcss_on_cpu list on this cpu
3352 * has been initialized.
3353 *
3354 * Though the cpu is not in VMX operation now, there
3355 * is no problem to enable the vmclear operation
3356 * for the loaded_vmcss_on_cpu list is empty!
3357 */
3358 crash_enable_local_vmclear(cpu);
3359
Avi Kivity6aa8b732006-12-10 02:21:36 -08003360 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003361
3362 test_bits = FEATURE_CONTROL_LOCKED;
3363 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3364 if (tboot_enabled())
3365 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3366
3367 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003369 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3370 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003371 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003372
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003373 if (vmm_exclusive) {
3374 kvm_cpu_vmxon(phys_addr);
3375 ept_sync_global();
3376 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003377
Christoph Lameter89cbc762014-08-17 12:30:40 -05003378 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003379
Alexander Graf10474ae2009-09-15 11:37:46 +02003380 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003381}
3382
Nadav Har'Eld462b812011-05-24 15:26:10 +03003383static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003384{
3385 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003386 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003387
Nadav Har'Eld462b812011-05-24 15:26:10 +03003388 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3389 loaded_vmcss_on_cpu_link)
3390 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003391}
3392
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003393
3394/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3395 * tricks.
3396 */
3397static void kvm_cpu_vmxoff(void)
3398{
3399 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003400
3401 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003402}
3403
Radim Krčmář13a34e02014-08-28 15:13:03 +02003404static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003406 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003407 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003408 kvm_cpu_vmxoff();
3409 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003410 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411}
3412
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003413static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003414 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415{
3416 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003417 u32 ctl = ctl_min | ctl_opt;
3418
3419 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3420
3421 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3422 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3423
3424 /* Ensure minimum (required) set of control bits are supported. */
3425 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003426 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003427
3428 *result = ctl;
3429 return 0;
3430}
3431
Avi Kivity110312c2010-12-21 12:54:20 +02003432static __init bool allow_1_setting(u32 msr, u32 ctl)
3433{
3434 u32 vmx_msr_low, vmx_msr_high;
3435
3436 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3437 return vmx_msr_high & ctl;
3438}
3439
Yang, Sheng002c7f72007-07-31 14:23:01 +03003440static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003441{
3442 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003443 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003444 u32 _pin_based_exec_control = 0;
3445 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003446 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003447 u32 _vmexit_control = 0;
3448 u32 _vmentry_control = 0;
3449
Raghavendra K T10166742012-02-07 23:19:20 +05303450 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003451#ifdef CONFIG_X86_64
3452 CPU_BASED_CR8_LOAD_EXITING |
3453 CPU_BASED_CR8_STORE_EXITING |
3454#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003455 CPU_BASED_CR3_LOAD_EXITING |
3456 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003457 CPU_BASED_USE_IO_BITMAPS |
3458 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003459 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003460 CPU_BASED_MWAIT_EXITING |
3461 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003462 CPU_BASED_INVLPG_EXITING |
3463 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003464
Sheng Yangf78e0e22007-10-29 09:40:42 +08003465 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003466 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003467 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003468 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3469 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003470 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003471#ifdef CONFIG_X86_64
3472 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3473 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3474 ~CPU_BASED_CR8_STORE_EXITING;
3475#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003476 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003477 min2 = 0;
3478 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003479 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003480 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003481 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003482 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003483 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003484 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003485 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003486 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003487 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003488 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003489 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003490 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003491 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003492 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003493 if (adjust_vmx_controls(min2, opt2,
3494 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003495 &_cpu_based_2nd_exec_control) < 0)
3496 return -EIO;
3497 }
3498#ifndef CONFIG_X86_64
3499 if (!(_cpu_based_2nd_exec_control &
3500 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3501 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3502#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003503
3504 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3505 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003506 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003507 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3508 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003509
Sheng Yangd56f5462008-04-25 10:13:16 +08003510 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003511 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3512 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003513 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3514 CPU_BASED_CR3_STORE_EXITING |
3515 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003516 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3517 vmx_capability.ept, vmx_capability.vpid);
3518 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003519
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003520 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003521#ifdef CONFIG_X86_64
3522 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3523#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003524 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003525 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003526 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3527 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003528 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003529
Yang Zhang01e439b2013-04-11 19:25:12 +08003530 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003531 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3532 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003533 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3534 &_pin_based_exec_control) < 0)
3535 return -EIO;
3536
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003537 if (cpu_has_broken_vmx_preemption_timer())
3538 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003539 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003540 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003541 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3542
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003543 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003544 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003545 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3546 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003547 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003548
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003549 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003550
3551 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3552 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003553 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003554
3555#ifdef CONFIG_X86_64
3556 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3557 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003558 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003559#endif
3560
3561 /* Require Write-Back (WB) memory type for VMCS accesses. */
3562 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003563 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003564
Yang, Sheng002c7f72007-07-31 14:23:01 +03003565 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003566 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003567 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003568 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003569
Yang, Sheng002c7f72007-07-31 14:23:01 +03003570 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3571 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003572 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003573 vmcs_conf->vmexit_ctrl = _vmexit_control;
3574 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003575
Avi Kivity110312c2010-12-21 12:54:20 +02003576 cpu_has_load_ia32_efer =
3577 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3578 VM_ENTRY_LOAD_IA32_EFER)
3579 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3580 VM_EXIT_LOAD_IA32_EFER);
3581
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003582 cpu_has_load_perf_global_ctrl =
3583 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3584 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3585 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3586 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3587
3588 /*
3589 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003590 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003591 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3592 *
3593 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3594 *
3595 * AAK155 (model 26)
3596 * AAP115 (model 30)
3597 * AAT100 (model 37)
3598 * BC86,AAY89,BD102 (model 44)
3599 * BA97 (model 46)
3600 *
3601 */
3602 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3603 switch (boot_cpu_data.x86_model) {
3604 case 26:
3605 case 30:
3606 case 37:
3607 case 44:
3608 case 46:
3609 cpu_has_load_perf_global_ctrl = false;
3610 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3611 "does not work properly. Using workaround\n");
3612 break;
3613 default:
3614 break;
3615 }
3616 }
3617
Borislav Petkov782511b2016-04-04 22:25:03 +02003618 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003619 rdmsrl(MSR_IA32_XSS, host_xss);
3620
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003621 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003622}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003623
3624static struct vmcs *alloc_vmcs_cpu(int cpu)
3625{
3626 int node = cpu_to_node(cpu);
3627 struct page *pages;
3628 struct vmcs *vmcs;
3629
Vlastimil Babka96db8002015-09-08 15:03:50 -07003630 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 if (!pages)
3632 return NULL;
3633 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003634 memset(vmcs, 0, vmcs_config.size);
3635 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636 return vmcs;
3637}
3638
Avi Kivity6aa8b732006-12-10 02:21:36 -08003639static void free_vmcs(struct vmcs *vmcs)
3640{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003641 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642}
3643
Nadav Har'Eld462b812011-05-24 15:26:10 +03003644/*
3645 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3646 */
3647static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3648{
3649 if (!loaded_vmcs->vmcs)
3650 return;
3651 loaded_vmcs_clear(loaded_vmcs);
3652 free_vmcs(loaded_vmcs->vmcs);
3653 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003654 if (loaded_vmcs->msr_bitmap)
3655 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003656 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003657}
3658
Paolo Bonziniff546f92018-01-11 12:16:15 +01003659static struct vmcs *alloc_vmcs(void)
3660{
3661 return alloc_vmcs_cpu(raw_smp_processor_id());
3662}
3663
3664static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3665{
3666 loaded_vmcs->vmcs = alloc_vmcs();
3667 if (!loaded_vmcs->vmcs)
3668 return -ENOMEM;
3669
3670 loaded_vmcs->shadow_vmcs = NULL;
3671 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003672
3673 if (cpu_has_vmx_msr_bitmap()) {
3674 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3675 if (!loaded_vmcs->msr_bitmap)
3676 goto out_vmcs;
3677 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3678 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003679 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003680
3681out_vmcs:
3682 free_loaded_vmcs(loaded_vmcs);
3683 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003684}
3685
Sam Ravnborg39959582007-06-01 00:47:13 -07003686static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687{
3688 int cpu;
3689
Zachary Amsden3230bb42009-09-29 11:38:37 -10003690 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003691 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003692 per_cpu(vmxarea, cpu) = NULL;
3693 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003694}
3695
Bandan Dasfe2b2012014-04-21 15:20:14 -04003696static void init_vmcs_shadow_fields(void)
3697{
3698 int i, j;
3699
3700 /* No checks for read only fields yet */
3701
3702 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3703 switch (shadow_read_write_fields[i]) {
3704 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003705 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003706 continue;
3707 break;
3708 default:
3709 break;
3710 }
3711
3712 if (j < i)
3713 shadow_read_write_fields[j] =
3714 shadow_read_write_fields[i];
3715 j++;
3716 }
3717 max_shadow_read_write_fields = j;
3718
3719 /* shadowed fields guest access without vmexit */
3720 for (i = 0; i < max_shadow_read_write_fields; i++) {
3721 clear_bit(shadow_read_write_fields[i],
3722 vmx_vmwrite_bitmap);
3723 clear_bit(shadow_read_write_fields[i],
3724 vmx_vmread_bitmap);
3725 }
3726 for (i = 0; i < max_shadow_read_only_fields; i++)
3727 clear_bit(shadow_read_only_fields[i],
3728 vmx_vmread_bitmap);
3729}
3730
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731static __init int alloc_kvm_area(void)
3732{
3733 int cpu;
3734
Zachary Amsden3230bb42009-09-29 11:38:37 -10003735 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736 struct vmcs *vmcs;
3737
3738 vmcs = alloc_vmcs_cpu(cpu);
3739 if (!vmcs) {
3740 free_kvm_area();
3741 return -ENOMEM;
3742 }
3743
3744 per_cpu(vmxarea, cpu) = vmcs;
3745 }
3746 return 0;
3747}
3748
Gleb Natapov14168782013-01-21 15:36:49 +02003749static bool emulation_required(struct kvm_vcpu *vcpu)
3750{
3751 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3752}
3753
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003754static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003755 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003757 if (!emulate_invalid_guest_state) {
3758 /*
3759 * CS and SS RPL should be equal during guest entry according
3760 * to VMX spec, but in reality it is not always so. Since vcpu
3761 * is in the middle of the transition from real mode to
3762 * protected mode it is safe to assume that RPL 0 is a good
3763 * default value.
3764 */
3765 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003766 save->selector &= ~SEGMENT_RPL_MASK;
3767 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003768 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003769 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003770 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771}
3772
3773static void enter_pmode(struct kvm_vcpu *vcpu)
3774{
3775 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003776 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003777
Gleb Natapovd99e4152012-12-20 16:57:45 +02003778 /*
3779 * Update real mode segment cache. It may be not up-to-date if sement
3780 * register was written while vcpu was in a guest mode.
3781 */
3782 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3783 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3784 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3785 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3786 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3787 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3788
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003789 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003790
Avi Kivity2fb92db2011-04-27 19:42:18 +03003791 vmx_segment_cache_clear(vmx);
3792
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003793 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794
3795 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003796 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3797 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798 vmcs_writel(GUEST_RFLAGS, flags);
3799
Rusty Russell66aee912007-07-17 23:34:16 +10003800 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3801 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802
3803 update_exception_bitmap(vcpu);
3804
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003805 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3806 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3807 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3808 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3809 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3810 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003811}
3812
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003813static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003814{
Mathias Krause772e0312012-08-30 01:30:19 +02003815 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003816 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003817
Gleb Natapovd99e4152012-12-20 16:57:45 +02003818 var.dpl = 0x3;
3819 if (seg == VCPU_SREG_CS)
3820 var.type = 0x3;
3821
3822 if (!emulate_invalid_guest_state) {
3823 var.selector = var.base >> 4;
3824 var.base = var.base & 0xffff0;
3825 var.limit = 0xffff;
3826 var.g = 0;
3827 var.db = 0;
3828 var.present = 1;
3829 var.s = 1;
3830 var.l = 0;
3831 var.unusable = 0;
3832 var.type = 0x3;
3833 var.avl = 0;
3834 if (save->base & 0xf)
3835 printk_once(KERN_WARNING "kvm: segment base is not "
3836 "paragraph aligned when entering "
3837 "protected mode (seg=%d)", seg);
3838 }
3839
3840 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05003841 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003842 vmcs_write32(sf->limit, var.limit);
3843 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844}
3845
3846static void enter_rmode(struct kvm_vcpu *vcpu)
3847{
3848 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003849 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003851 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3852 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3853 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3854 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3855 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003856 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3857 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003858
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003859 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860
Gleb Natapov776e58e2011-03-13 12:34:27 +02003861 /*
3862 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003863 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003864 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003865 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003866 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3867 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003868
Avi Kivity2fb92db2011-04-27 19:42:18 +03003869 vmx_segment_cache_clear(vmx);
3870
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003871 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003872 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3874
3875 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003876 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003878 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879
3880 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003881 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882 update_exception_bitmap(vcpu);
3883
Gleb Natapovd99e4152012-12-20 16:57:45 +02003884 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3885 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3886 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3887 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3888 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3889 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003890
Eddie Dong8668a3c2007-10-10 14:26:45 +08003891 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003892}
3893
Amit Shah401d10d2009-02-20 22:53:37 +05303894static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3895{
3896 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003897 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3898
3899 if (!msr)
3900 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303901
Avi Kivity44ea2b12009-09-06 15:55:37 +03003902 /*
3903 * Force kernel_gs_base reloading before EFER changes, as control
3904 * of this msr depends on is_long_mode().
3905 */
3906 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003907 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303908 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003909 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303910 msr->data = efer;
3911 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003912 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303913
3914 msr->data = efer & ~EFER_LME;
3915 }
3916 setup_msrs(vmx);
3917}
3918
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003919#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920
3921static void enter_lmode(struct kvm_vcpu *vcpu)
3922{
3923 u32 guest_tr_ar;
3924
Avi Kivity2fb92db2011-04-27 19:42:18 +03003925 vmx_segment_cache_clear(to_vmx(vcpu));
3926
Avi Kivity6aa8b732006-12-10 02:21:36 -08003927 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003928 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003929 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3930 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003931 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003932 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3933 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934 }
Avi Kivityda38f432010-07-06 11:30:49 +03003935 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936}
3937
3938static void exit_lmode(struct kvm_vcpu *vcpu)
3939{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003940 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003941 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942}
3943
3944#endif
3945
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003946static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003947{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003948 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003949 if (enable_ept) {
3950 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3951 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003952 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003953 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003954}
3955
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003956static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3957{
3958 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3959}
3960
Jim Mattson8386ff52017-03-16 13:53:59 -07003961static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
3962{
3963 if (enable_ept)
3964 vmx_flush_tlb(vcpu);
3965}
3966
Avi Kivitye8467fd2009-12-29 18:43:06 +02003967static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3968{
3969 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3970
3971 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3972 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3973}
3974
Avi Kivityaff48ba2010-12-05 18:56:11 +02003975static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3976{
3977 if (enable_ept && is_paging(vcpu))
3978 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3979 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3980}
3981
Anthony Liguori25c4c272007-04-27 09:29:21 +03003982static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003983{
Avi Kivityfc78f512009-12-07 12:16:48 +02003984 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3985
3986 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3987 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003988}
3989
Sheng Yang14394422008-04-28 12:24:45 +08003990static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3991{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003992 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3993
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003994 if (!test_bit(VCPU_EXREG_PDPTR,
3995 (unsigned long *)&vcpu->arch.regs_dirty))
3996 return;
3997
Sheng Yang14394422008-04-28 12:24:45 +08003998 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003999 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4000 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4001 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4002 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004003 }
4004}
4005
Avi Kivity8f5d5492009-05-31 18:41:29 +03004006static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4007{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004008 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4009
Avi Kivity8f5d5492009-05-31 18:41:29 +03004010 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004011 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4012 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4013 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4014 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004015 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004016
4017 __set_bit(VCPU_EXREG_PDPTR,
4018 (unsigned long *)&vcpu->arch.regs_avail);
4019 __set_bit(VCPU_EXREG_PDPTR,
4020 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004021}
4022
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004023static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004024
4025static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4026 unsigned long cr0,
4027 struct kvm_vcpu *vcpu)
4028{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004029 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4030 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004031 if (!(cr0 & X86_CR0_PG)) {
4032 /* From paging/starting to nonpaging */
4033 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004034 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004035 (CPU_BASED_CR3_LOAD_EXITING |
4036 CPU_BASED_CR3_STORE_EXITING));
4037 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004038 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004039 } else if (!is_paging(vcpu)) {
4040 /* From nonpaging to paging */
4041 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004042 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004043 ~(CPU_BASED_CR3_LOAD_EXITING |
4044 CPU_BASED_CR3_STORE_EXITING));
4045 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004046 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004047 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004048
4049 if (!(cr0 & X86_CR0_WP))
4050 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004051}
4052
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4054{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004055 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004056 unsigned long hw_cr0;
4057
Gleb Natapov50378782013-02-04 16:00:28 +02004058 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004059 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004060 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004061 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004062 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004063
Gleb Natapov218e7632013-01-21 15:36:45 +02004064 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4065 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004066
Gleb Natapov218e7632013-01-21 15:36:45 +02004067 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4068 enter_rmode(vcpu);
4069 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004071#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004072 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004073 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004075 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 exit_lmode(vcpu);
4077 }
4078#endif
4079
Avi Kivity089d0342009-03-23 18:26:32 +02004080 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004081 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4082
Avi Kivity02daab22009-12-30 12:40:26 +02004083 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004084 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004085
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004087 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004088 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004089
4090 /* depends on vcpu->arch.cr0 to be set to a new value */
4091 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092}
4093
Sheng Yang14394422008-04-28 12:24:45 +08004094static u64 construct_eptp(unsigned long root_hpa)
4095{
4096 u64 eptp;
4097
4098 /* TODO write the value reading from MSR */
4099 eptp = VMX_EPT_DEFAULT_MT |
4100 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004101 if (enable_ept_ad_bits)
4102 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004103 eptp |= (root_hpa & PAGE_MASK);
4104
4105 return eptp;
4106}
4107
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4109{
Sheng Yang14394422008-04-28 12:24:45 +08004110 unsigned long guest_cr3;
4111 u64 eptp;
4112
4113 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004114 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004115 eptp = construct_eptp(cr3);
4116 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004117 if (is_paging(vcpu) || is_guest_mode(vcpu))
4118 guest_cr3 = kvm_read_cr3(vcpu);
4119 else
4120 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004121 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004122 }
4123
Sheng Yang2384d2b2008-01-17 15:14:33 +08004124 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004125 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126}
4127
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004128static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004130 /*
4131 * Pass through host's Machine Check Enable value to hw_cr4, which
4132 * is in force while we are in guest mode. Do not let guests control
4133 * this bit, even if host CR4.MCE == 0.
4134 */
4135 unsigned long hw_cr4 =
4136 (cr4_read_shadow() & X86_CR4_MCE) |
4137 (cr4 & ~X86_CR4_MCE) |
4138 (to_vmx(vcpu)->rmode.vm86_active ?
4139 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004140
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004141 if (cr4 & X86_CR4_VMXE) {
4142 /*
4143 * To use VMXON (and later other VMX instructions), a guest
4144 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4145 * So basically the check on whether to allow nested VMX
4146 * is here.
4147 */
4148 if (!nested_vmx_allowed(vcpu))
4149 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004150 }
4151 if (to_vmx(vcpu)->nested.vmxon &&
4152 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004153 return 1;
4154
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004155 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004156 if (enable_ept) {
4157 if (!is_paging(vcpu)) {
4158 hw_cr4 &= ~X86_CR4_PAE;
4159 hw_cr4 |= X86_CR4_PSE;
4160 } else if (!(cr4 & X86_CR4_PAE)) {
4161 hw_cr4 &= ~X86_CR4_PAE;
4162 }
4163 }
Sheng Yang14394422008-04-28 12:24:45 +08004164
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004165 if (!enable_unrestricted_guest && !is_paging(vcpu))
4166 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004167 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4168 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4169 * to be manually disabled when guest switches to non-paging
4170 * mode.
4171 *
4172 * If !enable_unrestricted_guest, the CPU is always running
4173 * with CR0.PG=1 and CR4 needs to be modified.
4174 * If enable_unrestricted_guest, the CPU automatically
4175 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004176 */
Huaitong Handdba2622016-03-22 16:51:15 +08004177 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004178
Sheng Yang14394422008-04-28 12:24:45 +08004179 vmcs_writel(CR4_READ_SHADOW, cr4);
4180 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004181 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182}
4183
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184static void vmx_get_segment(struct kvm_vcpu *vcpu,
4185 struct kvm_segment *var, int seg)
4186{
Avi Kivitya9179492011-01-03 14:28:52 +02004187 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188 u32 ar;
4189
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004190 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004191 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004192 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004193 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004194 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004195 var->base = vmx_read_guest_seg_base(vmx, seg);
4196 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4197 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004198 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004199 var->base = vmx_read_guest_seg_base(vmx, seg);
4200 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4201 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4202 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004203 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004204 var->type = ar & 15;
4205 var->s = (ar >> 4) & 1;
4206 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004207 /*
4208 * Some userspaces do not preserve unusable property. Since usable
4209 * segment has to be present according to VMX spec we can use present
4210 * property to amend userspace bug by making unusable segment always
4211 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4212 * segment as unusable.
4213 */
4214 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215 var->avl = (ar >> 12) & 1;
4216 var->l = (ar >> 13) & 1;
4217 var->db = (ar >> 14) & 1;
4218 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004219}
4220
Avi Kivitya9179492011-01-03 14:28:52 +02004221static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4222{
Avi Kivitya9179492011-01-03 14:28:52 +02004223 struct kvm_segment s;
4224
4225 if (to_vmx(vcpu)->rmode.vm86_active) {
4226 vmx_get_segment(vcpu, &s, seg);
4227 return s.base;
4228 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004229 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004230}
4231
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004232static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004233{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004234 struct vcpu_vmx *vmx = to_vmx(vcpu);
4235
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004236 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004237 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004238 else {
4239 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004240 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004241 }
Avi Kivity69c73022011-03-07 15:26:44 +02004242}
4243
Avi Kivity653e3102007-05-07 10:55:37 +03004244static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004246 u32 ar;
4247
Avi Kivityf0495f92012-06-07 17:06:10 +03004248 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249 ar = 1 << 16;
4250 else {
4251 ar = var->type & 15;
4252 ar |= (var->s & 1) << 4;
4253 ar |= (var->dpl & 3) << 5;
4254 ar |= (var->present & 1) << 7;
4255 ar |= (var->avl & 1) << 12;
4256 ar |= (var->l & 1) << 13;
4257 ar |= (var->db & 1) << 14;
4258 ar |= (var->g & 1) << 15;
4259 }
Avi Kivity653e3102007-05-07 10:55:37 +03004260
4261 return ar;
4262}
4263
4264static void vmx_set_segment(struct kvm_vcpu *vcpu,
4265 struct kvm_segment *var, int seg)
4266{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004267 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004268 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004269
Avi Kivity2fb92db2011-04-27 19:42:18 +03004270 vmx_segment_cache_clear(vmx);
4271
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004272 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4273 vmx->rmode.segs[seg] = *var;
4274 if (seg == VCPU_SREG_TR)
4275 vmcs_write16(sf->selector, var->selector);
4276 else if (var->s)
4277 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004278 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004279 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004280
Avi Kivity653e3102007-05-07 10:55:37 +03004281 vmcs_writel(sf->base, var->base);
4282 vmcs_write32(sf->limit, var->limit);
4283 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004284
4285 /*
4286 * Fix the "Accessed" bit in AR field of segment registers for older
4287 * qemu binaries.
4288 * IA32 arch specifies that at the time of processor reset the
4289 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004290 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004291 * state vmexit when "unrestricted guest" mode is turned on.
4292 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4293 * tree. Newer qemu binaries with that qemu fix would not need this
4294 * kvm hack.
4295 */
4296 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004297 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004298
Gleb Natapovf924d662012-12-12 19:10:55 +02004299 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004300
4301out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004302 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004303}
4304
Avi Kivity6aa8b732006-12-10 02:21:36 -08004305static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4306{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004307 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004308
4309 *db = (ar >> 14) & 1;
4310 *l = (ar >> 13) & 1;
4311}
4312
Gleb Natapov89a27f42010-02-16 10:51:48 +02004313static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004314{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004315 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4316 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004317}
4318
Gleb Natapov89a27f42010-02-16 10:51:48 +02004319static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004320{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004321 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4322 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004323}
4324
Gleb Natapov89a27f42010-02-16 10:51:48 +02004325static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004326{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004327 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4328 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329}
4330
Gleb Natapov89a27f42010-02-16 10:51:48 +02004331static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004333 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4334 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335}
4336
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004337static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4338{
4339 struct kvm_segment var;
4340 u32 ar;
4341
4342 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004343 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004344 if (seg == VCPU_SREG_CS)
4345 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004346 ar = vmx_segment_access_rights(&var);
4347
4348 if (var.base != (var.selector << 4))
4349 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004350 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004351 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004352 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004353 return false;
4354
4355 return true;
4356}
4357
4358static bool code_segment_valid(struct kvm_vcpu *vcpu)
4359{
4360 struct kvm_segment cs;
4361 unsigned int cs_rpl;
4362
4363 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004364 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004365
Avi Kivity1872a3f2009-01-04 23:26:52 +02004366 if (cs.unusable)
4367 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004368 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004369 return false;
4370 if (!cs.s)
4371 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004372 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004373 if (cs.dpl > cs_rpl)
4374 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004375 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004376 if (cs.dpl != cs_rpl)
4377 return false;
4378 }
4379 if (!cs.present)
4380 return false;
4381
4382 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4383 return true;
4384}
4385
4386static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4387{
4388 struct kvm_segment ss;
4389 unsigned int ss_rpl;
4390
4391 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004392 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004393
Avi Kivity1872a3f2009-01-04 23:26:52 +02004394 if (ss.unusable)
4395 return true;
4396 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004397 return false;
4398 if (!ss.s)
4399 return false;
4400 if (ss.dpl != ss_rpl) /* DPL != RPL */
4401 return false;
4402 if (!ss.present)
4403 return false;
4404
4405 return true;
4406}
4407
4408static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4409{
4410 struct kvm_segment var;
4411 unsigned int rpl;
4412
4413 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004414 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004415
Avi Kivity1872a3f2009-01-04 23:26:52 +02004416 if (var.unusable)
4417 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004418 if (!var.s)
4419 return false;
4420 if (!var.present)
4421 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004422 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004423 if (var.dpl < rpl) /* DPL < RPL */
4424 return false;
4425 }
4426
4427 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4428 * rights flags
4429 */
4430 return true;
4431}
4432
4433static bool tr_valid(struct kvm_vcpu *vcpu)
4434{
4435 struct kvm_segment tr;
4436
4437 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4438
Avi Kivity1872a3f2009-01-04 23:26:52 +02004439 if (tr.unusable)
4440 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004441 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004442 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004443 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004444 return false;
4445 if (!tr.present)
4446 return false;
4447
4448 return true;
4449}
4450
4451static bool ldtr_valid(struct kvm_vcpu *vcpu)
4452{
4453 struct kvm_segment ldtr;
4454
4455 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4456
Avi Kivity1872a3f2009-01-04 23:26:52 +02004457 if (ldtr.unusable)
4458 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004459 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004460 return false;
4461 if (ldtr.type != 2)
4462 return false;
4463 if (!ldtr.present)
4464 return false;
4465
4466 return true;
4467}
4468
4469static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4470{
4471 struct kvm_segment cs, ss;
4472
4473 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4474 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4475
Nadav Amitb32a9912015-03-29 16:33:04 +03004476 return ((cs.selector & SEGMENT_RPL_MASK) ==
4477 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004478}
4479
4480/*
4481 * Check if guest state is valid. Returns true if valid, false if
4482 * not.
4483 * We assume that registers are always usable
4484 */
4485static bool guest_state_valid(struct kvm_vcpu *vcpu)
4486{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004487 if (enable_unrestricted_guest)
4488 return true;
4489
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004490 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004491 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004492 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4493 return false;
4494 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4495 return false;
4496 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4497 return false;
4498 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4499 return false;
4500 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4501 return false;
4502 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4503 return false;
4504 } else {
4505 /* protected mode guest state checks */
4506 if (!cs_ss_rpl_check(vcpu))
4507 return false;
4508 if (!code_segment_valid(vcpu))
4509 return false;
4510 if (!stack_segment_valid(vcpu))
4511 return false;
4512 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4513 return false;
4514 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4515 return false;
4516 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4517 return false;
4518 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4519 return false;
4520 if (!tr_valid(vcpu))
4521 return false;
4522 if (!ldtr_valid(vcpu))
4523 return false;
4524 }
4525 /* TODO:
4526 * - Add checks on RIP
4527 * - Add checks on RFLAGS
4528 */
4529
4530 return true;
4531}
4532
Mike Dayd77c26f2007-10-08 09:02:08 -04004533static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004534{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004535 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004536 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004537 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004538
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004539 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004540 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004541 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4542 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004543 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004544 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004545 r = kvm_write_guest_page(kvm, fn++, &data,
4546 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004547 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004548 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004549 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4550 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004551 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004552 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4553 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004554 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004555 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004556 r = kvm_write_guest_page(kvm, fn, &data,
4557 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4558 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004559out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004560 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004561 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004562}
4563
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004564static int init_rmode_identity_map(struct kvm *kvm)
4565{
Tang Chenf51770e2014-09-16 18:41:59 +08004566 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004567 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004568 u32 tmp;
4569
Avi Kivity089d0342009-03-23 18:26:32 +02004570 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004571 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004572
4573 /* Protect kvm->arch.ept_identity_pagetable_done. */
4574 mutex_lock(&kvm->slots_lock);
4575
Tang Chenf51770e2014-09-16 18:41:59 +08004576 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004577 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004578
Sheng Yangb927a3c2009-07-21 10:42:48 +08004579 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004580
4581 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004582 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004583 goto out2;
4584
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004585 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004586 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4587 if (r < 0)
4588 goto out;
4589 /* Set up identity-mapping pagetable for EPT in real mode */
4590 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4591 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4592 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4593 r = kvm_write_guest_page(kvm, identity_map_pfn,
4594 &tmp, i * sizeof(tmp), sizeof(tmp));
4595 if (r < 0)
4596 goto out;
4597 }
4598 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004599
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004600out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004601 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004602
4603out2:
4604 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004605 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004606}
4607
Avi Kivity6aa8b732006-12-10 02:21:36 -08004608static void seg_setup(int seg)
4609{
Mathias Krause772e0312012-08-30 01:30:19 +02004610 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004611 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612
4613 vmcs_write16(sf->selector, 0);
4614 vmcs_writel(sf->base, 0);
4615 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004616 ar = 0x93;
4617 if (seg == VCPU_SREG_CS)
4618 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004619
4620 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004621}
4622
Sheng Yangf78e0e22007-10-29 09:40:42 +08004623static int alloc_apic_access_page(struct kvm *kvm)
4624{
Xiao Guangrong44841412012-09-07 14:14:20 +08004625 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004626 int r = 0;
4627
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004628 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004629 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004630 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004631 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4632 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004633 if (r)
4634 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004635
Tang Chen73a6d942014-09-11 13:38:00 +08004636 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004637 if (is_error_page(page)) {
4638 r = -EFAULT;
4639 goto out;
4640 }
4641
Tang Chenc24ae0d2014-09-24 15:57:58 +08004642 /*
4643 * Do not pin the page in memory, so that memory hot-unplug
4644 * is able to migrate it.
4645 */
4646 put_page(page);
4647 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004648out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004649 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004650 return r;
4651}
4652
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004653static int alloc_identity_pagetable(struct kvm *kvm)
4654{
Tang Chena255d472014-09-16 18:41:58 +08004655 /* Called with kvm->slots_lock held. */
4656
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004657 int r = 0;
4658
Tang Chena255d472014-09-16 18:41:58 +08004659 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4660
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004661 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4662 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004663
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004664 return r;
4665}
4666
Wanpeng Li991e7a02015-09-16 17:30:05 +08004667static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004668{
4669 int vpid;
4670
Avi Kivity919818a2009-03-23 18:01:29 +02004671 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004672 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004673 spin_lock(&vmx_vpid_lock);
4674 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004675 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004676 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004677 else
4678 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004679 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004680 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004681}
4682
Wanpeng Li991e7a02015-09-16 17:30:05 +08004683static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004684{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004685 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004686 return;
4687 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004688 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004689 spin_unlock(&vmx_vpid_lock);
4690}
4691
Paolo Bonzini6236b782018-01-16 16:51:18 +01004692static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4693 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004694{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004695 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004696
4697 if (!cpu_has_vmx_msr_bitmap())
4698 return;
4699
4700 /*
4701 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4702 * have the write-low and read-high bitmap offsets the wrong way round.
4703 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4704 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004705 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004706 if (type & MSR_TYPE_R)
4707 /* read-low */
4708 __clear_bit(msr, msr_bitmap + 0x000 / f);
4709
4710 if (type & MSR_TYPE_W)
4711 /* write-low */
4712 __clear_bit(msr, msr_bitmap + 0x800 / f);
4713
Sheng Yang25c5f222008-03-28 13:18:56 +08004714 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4715 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004716 if (type & MSR_TYPE_R)
4717 /* read-high */
4718 __clear_bit(msr, msr_bitmap + 0x400 / f);
4719
4720 if (type & MSR_TYPE_W)
4721 /* write-high */
4722 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4723
4724 }
4725}
4726
Paolo Bonzini6236b782018-01-16 16:51:18 +01004727static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4728 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004729{
4730 int f = sizeof(unsigned long);
4731
4732 if (!cpu_has_vmx_msr_bitmap())
4733 return;
4734
4735 /*
4736 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4737 * have the write-low and read-high bitmap offsets the wrong way round.
4738 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4739 */
4740 if (msr <= 0x1fff) {
4741 if (type & MSR_TYPE_R)
4742 /* read-low */
4743 __set_bit(msr, msr_bitmap + 0x000 / f);
4744
4745 if (type & MSR_TYPE_W)
4746 /* write-low */
4747 __set_bit(msr, msr_bitmap + 0x800 / f);
4748
4749 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4750 msr &= 0x1fff;
4751 if (type & MSR_TYPE_R)
4752 /* read-high */
4753 __set_bit(msr, msr_bitmap + 0x400 / f);
4754
4755 if (type & MSR_TYPE_W)
4756 /* write-high */
4757 __set_bit(msr, msr_bitmap + 0xc00 / f);
4758
Sheng Yang25c5f222008-03-28 13:18:56 +08004759 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004760}
4761
Paolo Bonzini6236b782018-01-16 16:51:18 +01004762static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4763 u32 msr, int type, bool value)
4764{
4765 if (value)
4766 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4767 else
4768 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4769}
4770
Wincy Vanf2b93282015-02-03 23:56:03 +08004771/*
4772 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4773 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4774 */
4775static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4776 unsigned long *msr_bitmap_nested,
4777 u32 msr, int type)
4778{
4779 int f = sizeof(unsigned long);
4780
4781 if (!cpu_has_vmx_msr_bitmap()) {
4782 WARN_ON(1);
4783 return;
4784 }
4785
4786 /*
4787 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4788 * have the write-low and read-high bitmap offsets the wrong way round.
4789 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4790 */
4791 if (msr <= 0x1fff) {
4792 if (type & MSR_TYPE_R &&
4793 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4794 /* read-low */
4795 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4796
4797 if (type & MSR_TYPE_W &&
4798 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4799 /* write-low */
4800 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4801
4802 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4803 msr &= 0x1fff;
4804 if (type & MSR_TYPE_R &&
4805 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4806 /* read-high */
4807 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4808
4809 if (type & MSR_TYPE_W &&
4810 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4811 /* write-high */
4812 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4813
4814 }
4815}
4816
Paolo Bonzini6236b782018-01-16 16:51:18 +01004817static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004818{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004819 u8 mode = 0;
4820
4821 if (cpu_has_secondary_exec_ctrls() &&
4822 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4823 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4824 mode |= MSR_BITMAP_MODE_X2APIC;
4825 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4826 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4827 }
4828
4829 if (is_long_mode(vcpu))
4830 mode |= MSR_BITMAP_MODE_LM;
4831
4832 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004833}
4834
Paolo Bonzini6236b782018-01-16 16:51:18 +01004835#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
4836
4837static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4838 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08004839{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004840 int msr;
4841
4842 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4843 unsigned word = msr / BITS_PER_LONG;
4844 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4845 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4846 }
4847
4848 if (mode & MSR_BITMAP_MODE_X2APIC) {
4849 /*
4850 * TPR reads and writes can be virtualized even if virtual interrupt
4851 * delivery is not in use.
4852 */
4853 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4854 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4855 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4856 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4857 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4858 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004859 }
Yang Zhang8d146952013-01-25 10:18:50 +08004860}
4861
Paolo Bonzini6236b782018-01-16 16:51:18 +01004862static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08004863{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004864 struct vcpu_vmx *vmx = to_vmx(vcpu);
4865 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4866 u8 mode = vmx_msr_bitmap_mode(vcpu);
4867 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004868
Paolo Bonzini6236b782018-01-16 16:51:18 +01004869 if (!changed)
4870 return;
4871
4872 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
4873 !(mode & MSR_BITMAP_MODE_LM));
4874
4875 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4876 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4877
4878 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02004879}
4880
Andrey Smetanind62caab2015-11-10 15:36:33 +03004881static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004882{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004883 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004884}
4885
David Matlackb7649e12017-08-01 14:00:40 -07004886static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
4887{
4888 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4889 gfn_t gfn;
4890
4891 /*
4892 * Don't need to mark the APIC access page dirty; it is never
4893 * written to by the CPU during APIC virtualization.
4894 */
4895
4896 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
4897 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
4898 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4899 }
4900
4901 if (nested_cpu_has_posted_intr(vmcs12)) {
4902 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
4903 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4904 }
4905}
4906
4907
David Hildenbrand1edccf22017-01-25 11:58:58 +01004908static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08004909{
4910 struct vcpu_vmx *vmx = to_vmx(vcpu);
4911 int max_irr;
4912 void *vapic_page;
4913 u16 status;
4914
David Matlackb7649e12017-08-01 14:00:40 -07004915 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
4916 return;
Wincy Van705699a2015-02-03 23:58:17 +08004917
David Matlackb7649e12017-08-01 14:00:40 -07004918 vmx->nested.pi_pending = false;
4919 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4920 return;
Wincy Van705699a2015-02-03 23:58:17 +08004921
David Matlackb7649e12017-08-01 14:00:40 -07004922 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
4923 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08004924 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08004925 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4926 kunmap(vmx->nested.virtual_apic_page);
4927
4928 status = vmcs_read16(GUEST_INTR_STATUS);
4929 if ((u8)max_irr > ((u8)status & 0xff)) {
4930 status &= ~0xff;
4931 status |= (u8)max_irr;
4932 vmcs_write16(GUEST_INTR_STATUS, status);
4933 }
4934 }
David Matlackb7649e12017-08-01 14:00:40 -07004935
4936 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004937}
4938
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004939static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4940{
4941#ifdef CONFIG_SMP
4942 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004943 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08004944 * The vector of interrupt to be delivered to vcpu had
4945 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08004946 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08004947 * Following cases will be reached in this block, and
4948 * we always send a notification event in all cases as
4949 * explained below.
4950 *
4951 * Case 1: vcpu keeps in non-root mode. Sending a
4952 * notification event posts the interrupt to vcpu.
4953 *
4954 * Case 2: vcpu exits to root mode and is still
4955 * runnable. PIR will be synced to vIRR before the
4956 * next vcpu entry. Sending a notification event in
4957 * this case has no effect, as vcpu is not in root
4958 * mode.
4959 *
4960 * Case 3: vcpu exits to root mode and is blocked.
4961 * vcpu_block() has already synced PIR to vIRR and
4962 * never blocks vcpu if vIRR is not cleared. Therefore,
4963 * a blocked vcpu here does not wait for any requested
4964 * interrupts in PIR, and sending a notification event
4965 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08004966 */
Feng Wu28b835d2015-09-18 22:29:54 +08004967
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004968 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4969 POSTED_INTR_VECTOR);
4970 return true;
4971 }
4972#endif
4973 return false;
4974}
4975
Wincy Van705699a2015-02-03 23:58:17 +08004976static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4977 int vector)
4978{
4979 struct vcpu_vmx *vmx = to_vmx(vcpu);
4980
4981 if (is_guest_mode(vcpu) &&
4982 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08004983 /*
4984 * If a posted intr is not recognized by hardware,
4985 * we will accomplish it in the next vmentry.
4986 */
4987 vmx->nested.pi_pending = true;
4988 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02004989 /* the PIR and ON have been set by L1. */
4990 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
4991 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004992 return 0;
4993 }
4994 return -1;
4995}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004997 * Send interrupt to vcpu via posted interrupt way.
4998 * 1. If target vcpu is running(non-root mode), send posted interrupt
4999 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5000 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5001 * interrupt from PIR in next vmentry.
5002 */
5003static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5004{
5005 struct vcpu_vmx *vmx = to_vmx(vcpu);
5006 int r;
5007
Wincy Van705699a2015-02-03 23:58:17 +08005008 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5009 if (!r)
5010 return;
5011
Yang Zhanga20ed542013-04-11 19:25:15 +08005012 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5013 return;
5014
5015 r = pi_test_and_set_on(&vmx->pi_desc);
5016 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005017 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005018 kvm_vcpu_kick(vcpu);
5019}
5020
5021static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5022{
5023 struct vcpu_vmx *vmx = to_vmx(vcpu);
5024
5025 if (!pi_test_and_clear_on(&vmx->pi_desc))
5026 return;
5027
5028 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5029}
5030
Avi Kivity6aa8b732006-12-10 02:21:36 -08005031/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005032 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5033 * will not change in the lifetime of the guest.
5034 * Note that host-state that does change is set elsewhere. E.g., host-state
5035 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5036 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005037static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005038{
5039 u32 low32, high32;
5040 unsigned long tmpl;
5041 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005042 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005043
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005044 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005045 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5046
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005047 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005048 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005049 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5050 vmx->host_state.vmcs_host_cr4 = cr4;
5051
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005052 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005053#ifdef CONFIG_X86_64
5054 /*
5055 * Load null selectors, so we can avoid reloading them in
5056 * __vmx_load_host_state(), in case userspace uses the null selectors
5057 * too (the expected case).
5058 */
5059 vmcs_write16(HOST_DS_SELECTOR, 0);
5060 vmcs_write16(HOST_ES_SELECTOR, 0);
5061#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005062 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5063 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005064#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005065 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5066 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5067
5068 native_store_idt(&dt);
5069 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005070 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005071
Avi Kivity83287ea422012-09-16 15:10:57 +03005072 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005073
5074 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5075 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5076 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5077 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5078
5079 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5080 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5081 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5082 }
5083}
5084
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005085static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5086{
5087 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5088 if (enable_ept)
5089 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005090 if (is_guest_mode(&vmx->vcpu))
5091 vmx->vcpu.arch.cr4_guest_owned_bits &=
5092 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005093 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5094}
5095
Yang Zhang01e439b2013-04-11 19:25:12 +08005096static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5097{
5098 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5099
Andrey Smetanind62caab2015-11-10 15:36:33 +03005100 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005101 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005102 /* Enable the preemption timer dynamically */
5103 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005104 return pin_based_exec_ctrl;
5105}
5106
Andrey Smetanind62caab2015-11-10 15:36:33 +03005107static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5108{
5109 struct vcpu_vmx *vmx = to_vmx(vcpu);
5110
5111 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005112 if (cpu_has_secondary_exec_ctrls()) {
5113 if (kvm_vcpu_apicv_active(vcpu))
5114 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5115 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5116 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5117 else
5118 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5119 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5120 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5121 }
5122
5123 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005124 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005125}
5126
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005127static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5128{
5129 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005130
5131 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5132 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5133
Paolo Bonzini35754c92015-07-29 12:05:37 +02005134 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005135 exec_control &= ~CPU_BASED_TPR_SHADOW;
5136#ifdef CONFIG_X86_64
5137 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5138 CPU_BASED_CR8_LOAD_EXITING;
5139#endif
5140 }
5141 if (!enable_ept)
5142 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5143 CPU_BASED_CR3_LOAD_EXITING |
5144 CPU_BASED_INVLPG_EXITING;
5145 return exec_control;
5146}
5147
5148static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5149{
5150 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005151 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005152 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5153 if (vmx->vpid == 0)
5154 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5155 if (!enable_ept) {
5156 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5157 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005158 /* Enable INVPCID for non-ept guests may cause performance regression. */
5159 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005160 }
5161 if (!enable_unrestricted_guest)
5162 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5163 if (!ple_gap)
5164 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005165 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005166 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5167 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005168 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005169 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5170 (handle_vmptrld).
5171 We can NOT enable shadow_vmcs here because we don't have yet
5172 a current VMCS12
5173 */
5174 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005175
5176 if (!enable_pml)
5177 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005178
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005179 return exec_control;
5180}
5181
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005182static void ept_set_mmio_spte_mask(void)
5183{
5184 /*
5185 * EPT Misconfigurations can be generated if the value of bits 2:0
5186 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005187 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005188 * spte.
5189 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005190 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005191}
5192
Wanpeng Lif53cd632014-12-02 19:14:58 +08005193#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005194/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005195 * Sets up the vmcs for emulated real mode.
5196 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005197static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005198{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005199#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005200 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005201#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005202 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005203
Avi Kivity6aa8b732006-12-10 02:21:36 -08005204 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005205 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5206 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005207
Abel Gordon4607c2d2013-04-18 14:35:55 +03005208 if (enable_shadow_vmcs) {
5209 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5210 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5211 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005212 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005213 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005214
Avi Kivity6aa8b732006-12-10 02:21:36 -08005215 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5216
Avi Kivity6aa8b732006-12-10 02:21:36 -08005217 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005218 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005219 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005220
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005221 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005222
Dan Williamsdfa169b2016-06-02 11:17:24 -07005223 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005224 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5225 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005226 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005227
Andrey Smetanind62caab2015-11-10 15:36:33 +03005228 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005229 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5230 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5231 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5232 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5233
5234 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005235
Li RongQing0bcf2612015-12-03 13:29:34 +08005236 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005237 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005238 }
5239
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005240 if (ple_gap) {
5241 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005242 vmx->ple_window = ple_window;
5243 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005244 }
5245
Xiao Guangrongc3707952011-07-12 03:28:04 +08005246 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5247 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005248 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5249
Avi Kivity9581d442010-10-19 16:46:55 +02005250 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5251 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005252 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005253#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005254 rdmsrl(MSR_FS_BASE, a);
5255 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5256 rdmsrl(MSR_GS_BASE, a);
5257 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5258#else
5259 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5260 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5261#endif
5262
Eddie Dong2cc51562007-05-21 07:28:09 +03005263 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5264 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005265 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005266 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005267 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005268
Radim Krčmář74545702015-04-27 15:11:25 +02005269 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5270 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005271
Paolo Bonzini03916db2014-07-24 14:21:57 +02005272 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005273 u32 index = vmx_msr_index[i];
5274 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005275 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005276
5277 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5278 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005279 if (wrmsr_safe(index, data_low, data_high) < 0)
5280 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005281 vmx->guest_msrs[j].index = i;
5282 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005283 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005284 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005285 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005286
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01005287 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5288 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005289
5290 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005291
5292 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005293 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005294
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005295 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005296 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005297
Wanpeng Lif53cd632014-12-02 19:14:58 +08005298 if (vmx_xsaves_supported())
5299 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5300
Peter Feiner4e595162016-07-07 14:49:58 -07005301 if (enable_pml) {
5302 ASSERT(vmx->pml_pg);
5303 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5304 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5305 }
5306
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005307 return 0;
5308}
5309
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005310static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005311{
5312 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005313 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005314 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005315
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005316 vmx->rmode.vm86_active = 0;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005317 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005318
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005319 vmx->soft_vnmi_blocked = 0;
5320
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005321 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005322 kvm_set_cr8(vcpu, 0);
5323
5324 if (!init_event) {
5325 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5326 MSR_IA32_APICBASE_ENABLE;
5327 if (kvm_vcpu_is_reset_bsp(vcpu))
5328 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5329 apic_base_msr.host_initiated = true;
5330 kvm_set_apic_base(vcpu, &apic_base_msr);
5331 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005332
Avi Kivity2fb92db2011-04-27 19:42:18 +03005333 vmx_segment_cache_clear(vmx);
5334
Avi Kivity5706be02008-08-20 15:07:31 +03005335 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005336 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005337 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005338
5339 seg_setup(VCPU_SREG_DS);
5340 seg_setup(VCPU_SREG_ES);
5341 seg_setup(VCPU_SREG_FS);
5342 seg_setup(VCPU_SREG_GS);
5343 seg_setup(VCPU_SREG_SS);
5344
5345 vmcs_write16(GUEST_TR_SELECTOR, 0);
5346 vmcs_writel(GUEST_TR_BASE, 0);
5347 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5348 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5349
5350 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5351 vmcs_writel(GUEST_LDTR_BASE, 0);
5352 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5353 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5354
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005355 if (!init_event) {
5356 vmcs_write32(GUEST_SYSENTER_CS, 0);
5357 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5358 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5359 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5360 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005361
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005362 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005363 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005364
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005365 vmcs_writel(GUEST_GDTR_BASE, 0);
5366 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5367
5368 vmcs_writel(GUEST_IDTR_BASE, 0);
5369 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5370
Anthony Liguori443381a2010-12-06 10:53:38 -06005371 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005372 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005373 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005374
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005375 setup_msrs(vmx);
5376
Avi Kivity6aa8b732006-12-10 02:21:36 -08005377 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5378
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005379 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005380 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005381 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005382 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005383 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005384 vmcs_write32(TPR_THRESHOLD, 0);
5385 }
5386
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005387 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005388
Andrey Smetanind62caab2015-11-10 15:36:33 +03005389 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005390 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5391
Sheng Yang2384d2b2008-01-17 15:14:33 +08005392 if (vmx->vpid != 0)
5393 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5394
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005395 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005396 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005397 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005398 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005399 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005400 vmx_fpu_activate(vcpu);
5401 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005402
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005403 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005404}
5405
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005406/*
5407 * In nested virtualization, check if L1 asked to exit on external interrupts.
5408 * For most existing hypervisors, this will always return true.
5409 */
5410static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5411{
5412 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5413 PIN_BASED_EXT_INTR_MASK;
5414}
5415
Bandan Das77b0f5d2014-04-19 18:17:45 -04005416/*
5417 * In nested virtualization, check if L1 has set
5418 * VM_EXIT_ACK_INTR_ON_EXIT
5419 */
5420static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5421{
5422 return get_vmcs12(vcpu)->vm_exit_controls &
5423 VM_EXIT_ACK_INTR_ON_EXIT;
5424}
5425
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005426static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5427{
5428 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5429 PIN_BASED_NMI_EXITING;
5430}
5431
Jan Kiszkac9a79532014-03-07 20:03:15 +01005432static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005433{
5434 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005435
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005436 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5437 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5438 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5439}
5440
Jan Kiszkac9a79532014-03-07 20:03:15 +01005441static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005442{
5443 u32 cpu_based_vm_exec_control;
5444
Jan Kiszkac9a79532014-03-07 20:03:15 +01005445 if (!cpu_has_virtual_nmis() ||
5446 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5447 enable_irq_window(vcpu);
5448 return;
5449 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005450
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005451 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5452 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5453 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5454}
5455
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005456static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005457{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005458 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005459 uint32_t intr;
5460 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005461
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005462 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005463
Avi Kivityfa89a812008-09-01 15:57:51 +03005464 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005465 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005466 int inc_eip = 0;
5467 if (vcpu->arch.interrupt.soft)
5468 inc_eip = vcpu->arch.event_exit_inst_len;
5469 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005470 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005471 return;
5472 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005473 intr = irq | INTR_INFO_VALID_MASK;
5474 if (vcpu->arch.interrupt.soft) {
5475 intr |= INTR_TYPE_SOFT_INTR;
5476 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5477 vmx->vcpu.arch.event_exit_inst_len);
5478 } else
5479 intr |= INTR_TYPE_EXT_INTR;
5480 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005481}
5482
Sheng Yangf08864b2008-05-15 18:23:25 +08005483static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5484{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005485 struct vcpu_vmx *vmx = to_vmx(vcpu);
5486
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005487 if (!is_guest_mode(vcpu)) {
5488 if (!cpu_has_virtual_nmis()) {
5489 /*
5490 * Tracking the NMI-blocked state in software is built upon
5491 * finding the next open IRQ window. This, in turn, depends on
5492 * well-behaving guests: They have to keep IRQs disabled at
5493 * least as long as the NMI handler runs. Otherwise we may
5494 * cause NMI nesting, maybe breaking the guest. But as this is
5495 * highly unlikely, we can live with the residual risk.
5496 */
5497 vmx->soft_vnmi_blocked = 1;
5498 vmx->vnmi_blocked_time = 0;
5499 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005500
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005501 ++vcpu->stat.nmi_injections;
5502 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005503 }
5504
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005505 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005506 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005507 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005508 return;
5509 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005510
Sheng Yangf08864b2008-05-15 18:23:25 +08005511 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5512 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005513}
5514
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005515static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5516{
5517 if (!cpu_has_virtual_nmis())
5518 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005519 if (to_vmx(vcpu)->nmi_known_unmasked)
5520 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005521 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005522}
5523
5524static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5525{
5526 struct vcpu_vmx *vmx = to_vmx(vcpu);
5527
5528 if (!cpu_has_virtual_nmis()) {
5529 if (vmx->soft_vnmi_blocked != masked) {
5530 vmx->soft_vnmi_blocked = masked;
5531 vmx->vnmi_blocked_time = 0;
5532 }
5533 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005534 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005535 if (masked)
5536 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5537 GUEST_INTR_STATE_NMI);
5538 else
5539 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5540 GUEST_INTR_STATE_NMI);
5541 }
5542}
5543
Jan Kiszka2505dc92013-04-14 12:12:47 +02005544static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5545{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005546 if (to_vmx(vcpu)->nested.nested_run_pending)
5547 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005548
Jan Kiszka2505dc92013-04-14 12:12:47 +02005549 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5550 return 0;
5551
5552 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5553 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5554 | GUEST_INTR_STATE_NMI));
5555}
5556
Gleb Natapov78646122009-03-23 12:12:11 +02005557static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5558{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005559 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5560 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005561 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5562 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005563}
5564
Izik Eiduscbc94022007-10-25 00:29:55 +02005565static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5566{
5567 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005568
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005569 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5570 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005571 if (ret)
5572 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005573 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005574 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005575}
5576
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005577static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005578{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005579 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005580 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005581 /*
5582 * Update instruction length as we may reinject the exception
5583 * from user space while in guest debugging mode.
5584 */
5585 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5586 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005587 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005588 return false;
5589 /* fall through */
5590 case DB_VECTOR:
5591 if (vcpu->guest_debug &
5592 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5593 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005594 /* fall through */
5595 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005596 case OF_VECTOR:
5597 case BR_VECTOR:
5598 case UD_VECTOR:
5599 case DF_VECTOR:
5600 case SS_VECTOR:
5601 case GP_VECTOR:
5602 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005603 return true;
5604 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005605 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005606 return false;
5607}
5608
5609static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5610 int vec, u32 err_code)
5611{
5612 /*
5613 * Instruction with address size override prefix opcode 0x67
5614 * Cause the #SS fault with 0 error code in VM86 mode.
5615 */
5616 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5617 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5618 if (vcpu->arch.halt_request) {
5619 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005620 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005621 }
5622 return 1;
5623 }
5624 return 0;
5625 }
5626
5627 /*
5628 * Forward all other exceptions that are valid in real mode.
5629 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5630 * the required debugging infrastructure rework.
5631 */
5632 kvm_queue_exception(vcpu, vec);
5633 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005634}
5635
Andi Kleena0861c02009-06-08 17:37:09 +08005636/*
5637 * Trigger machine check on the host. We assume all the MSRs are already set up
5638 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5639 * We pass a fake environment to the machine check handler because we want
5640 * the guest to be always treated like user space, no matter what context
5641 * it used internally.
5642 */
5643static void kvm_machine_check(void)
5644{
5645#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5646 struct pt_regs regs = {
5647 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5648 .flags = X86_EFLAGS_IF,
5649 };
5650
5651 do_machine_check(&regs, 0);
5652#endif
5653}
5654
Avi Kivity851ba692009-08-24 11:10:17 +03005655static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005656{
5657 /* already handled by vcpu_run */
5658 return 1;
5659}
5660
Avi Kivity851ba692009-08-24 11:10:17 +03005661static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005662{
Avi Kivity1155f762007-11-22 11:30:47 +02005663 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005664 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005665 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005666 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005667 u32 vect_info;
5668 enum emulation_result er;
5669
Avi Kivity1155f762007-11-22 11:30:47 +02005670 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005671 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005672
Andi Kleena0861c02009-06-08 17:37:09 +08005673 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005674 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005675
Jim Mattson3f618a02016-12-12 11:01:37 -08005676 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005677 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005678
5679 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005680 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005681 return 1;
5682 }
5683
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005684 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005685 if (is_guest_mode(vcpu)) {
5686 kvm_queue_exception(vcpu, UD_VECTOR);
5687 return 1;
5688 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005689 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005690 if (er == EMULATE_USER_EXIT)
5691 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005692 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005693 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005694 return 1;
5695 }
5696
Avi Kivity6aa8b732006-12-10 02:21:36 -08005697 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005698 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005699 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005700
5701 /*
5702 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5703 * MMIO, it is better to report an internal error.
5704 * See the comments in vmx_handle_exit.
5705 */
5706 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5707 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5708 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5709 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005710 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005711 vcpu->run->internal.data[0] = vect_info;
5712 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005713 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005714 return 0;
5715 }
5716
Avi Kivity6aa8b732006-12-10 02:21:36 -08005717 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005718 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005719 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005720 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005721 trace_kvm_page_fault(cr2, error_code);
5722
Gleb Natapov3298b752009-05-11 13:35:46 +03005723 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005724 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005725 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005726 }
5727
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005728 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005729
5730 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5731 return handle_rmode_exception(vcpu, ex_no, error_code);
5732
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005733 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005734 case AC_VECTOR:
5735 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5736 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005737 case DB_VECTOR:
5738 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5739 if (!(vcpu->guest_debug &
5740 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005741 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005742 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005743 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005744 skip_emulated_instruction(vcpu);
5745
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005746 kvm_queue_exception(vcpu, DB_VECTOR);
5747 return 1;
5748 }
5749 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5750 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5751 /* fall through */
5752 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005753 /*
5754 * Update instruction length as we may reinject #BP from
5755 * user space while in guest debugging mode. Reading it for
5756 * #DB as well causes no harm, it is not used in that case.
5757 */
5758 vmx->vcpu.arch.event_exit_inst_len =
5759 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005760 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005761 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005762 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5763 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005764 break;
5765 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005766 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5767 kvm_run->ex.exception = ex_no;
5768 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005769 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005770 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005771 return 0;
5772}
5773
Avi Kivity851ba692009-08-24 11:10:17 +03005774static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005775{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005776 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005777 return 1;
5778}
5779
Avi Kivity851ba692009-08-24 11:10:17 +03005780static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005781{
Avi Kivity851ba692009-08-24 11:10:17 +03005782 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005783 return 0;
5784}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005785
Avi Kivity851ba692009-08-24 11:10:17 +03005786static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005787{
He, Qingbfdaab02007-09-12 14:18:28 +08005788 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005789 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005790 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005791
He, Qingbfdaab02007-09-12 14:18:28 +08005792 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005793 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005794 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005795
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005796 ++vcpu->stat.io_exits;
5797
5798 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005799 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005800
5801 port = exit_qualification >> 16;
5802 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005803 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005804
5805 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005806}
5807
Ingo Molnar102d8322007-02-19 14:37:47 +02005808static void
5809vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5810{
5811 /*
5812 * Patch in the VMCALL instruction:
5813 */
5814 hypercall[0] = 0x0f;
5815 hypercall[1] = 0x01;
5816 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005817}
5818
Wincy Vanb9c237b2015-02-03 23:56:30 +08005819static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005820{
5821 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005822 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005823
Wincy Vanb9c237b2015-02-03 23:56:30 +08005824 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005825 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5826 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5827 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5828 return (val & always_on) == always_on;
5829}
5830
Guo Chao0fa06072012-06-28 15:16:19 +08005831/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005832static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5833{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005834 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005835 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5836 unsigned long orig_val = val;
5837
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005838 /*
5839 * We get here when L2 changed cr0 in a way that did not change
5840 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005841 * but did change L0 shadowed bits. So we first calculate the
5842 * effective cr0 value that L1 would like to write into the
5843 * hardware. It consists of the L2-owned bits from the new
5844 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005845 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005846 val = (val & ~vmcs12->cr0_guest_host_mask) |
5847 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5848
Wincy Vanb9c237b2015-02-03 23:56:30 +08005849 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005850 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005851
5852 if (kvm_set_cr0(vcpu, val))
5853 return 1;
5854 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005855 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005856 } else {
5857 if (to_vmx(vcpu)->nested.vmxon &&
5858 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5859 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005860 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005861 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005862}
5863
5864static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5865{
5866 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005867 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5868 unsigned long orig_val = val;
5869
5870 /* analogously to handle_set_cr0 */
5871 val = (val & ~vmcs12->cr4_guest_host_mask) |
5872 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5873 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005874 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005875 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005876 return 0;
5877 } else
5878 return kvm_set_cr4(vcpu, val);
5879}
5880
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005881/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005882static void handle_clts(struct kvm_vcpu *vcpu)
5883{
5884 if (is_guest_mode(vcpu)) {
5885 /*
5886 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5887 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5888 * just pretend it's off (also in arch.cr0 for fpu_activate).
5889 */
5890 vmcs_writel(CR0_READ_SHADOW,
5891 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5892 vcpu->arch.cr0 &= ~X86_CR0_TS;
5893 } else
5894 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5895}
5896
Avi Kivity851ba692009-08-24 11:10:17 +03005897static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005898{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005899 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005900 int cr;
5901 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005902 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005903
He, Qingbfdaab02007-09-12 14:18:28 +08005904 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005905 cr = exit_qualification & 15;
5906 reg = (exit_qualification >> 8) & 15;
5907 switch ((exit_qualification >> 4) & 3) {
5908 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005909 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005910 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005911 switch (cr) {
5912 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005913 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005914 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005915 return 1;
5916 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005917 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005918 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005919 return 1;
5920 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005921 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005922 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005923 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005924 case 8: {
5925 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005926 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005927 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005928 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005929 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005930 return 1;
5931 if (cr8_prev <= cr8)
5932 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005933 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005934 return 0;
5935 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005936 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005937 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005938 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005939 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005940 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005941 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005942 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005943 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005944 case 1: /*mov from cr*/
5945 switch (cr) {
5946 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005947 val = kvm_read_cr3(vcpu);
5948 kvm_register_write(vcpu, reg, val);
5949 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005950 skip_emulated_instruction(vcpu);
5951 return 1;
5952 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005953 val = kvm_get_cr8(vcpu);
5954 kvm_register_write(vcpu, reg, val);
5955 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005956 skip_emulated_instruction(vcpu);
5957 return 1;
5958 }
5959 break;
5960 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005961 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005962 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005963 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005964
5965 skip_emulated_instruction(vcpu);
5966 return 1;
5967 default:
5968 break;
5969 }
Avi Kivity851ba692009-08-24 11:10:17 +03005970 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005971 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005972 (int)(exit_qualification >> 4) & 3, cr);
5973 return 0;
5974}
5975
Avi Kivity851ba692009-08-24 11:10:17 +03005976static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005977{
He, Qingbfdaab02007-09-12 14:18:28 +08005978 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005979 int dr, dr7, reg;
5980
5981 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5982 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5983
5984 /* First, if DR does not exist, trigger UD */
5985 if (!kvm_require_dr(vcpu, dr))
5986 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005987
Jan Kiszkaf2483412010-01-20 18:20:20 +01005988 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005989 if (!kvm_require_cpl(vcpu, 0))
5990 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005991 dr7 = vmcs_readl(GUEST_DR7);
5992 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005993 /*
5994 * As the vm-exit takes precedence over the debug trap, we
5995 * need to emulate the latter, either for the host or the
5996 * guest debugging itself.
5997 */
5998 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005999 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006000 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006001 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006002 vcpu->run->debug.arch.exception = DB_VECTOR;
6003 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006004 return 0;
6005 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006006 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006007 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006008 kvm_queue_exception(vcpu, DB_VECTOR);
6009 return 1;
6010 }
6011 }
6012
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006013 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006014 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6015 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006016
6017 /*
6018 * No more DR vmexits; force a reload of the debug registers
6019 * and reenter on this instruction. The next vmexit will
6020 * retrieve the full state of the debug registers.
6021 */
6022 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6023 return 1;
6024 }
6025
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006026 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6027 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006028 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006029
6030 if (kvm_get_dr(vcpu, dr, &val))
6031 return 1;
6032 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006033 } else
Nadav Amit57773922014-06-18 17:19:23 +03006034 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006035 return 1;
6036
Avi Kivity6aa8b732006-12-10 02:21:36 -08006037 skip_emulated_instruction(vcpu);
6038 return 1;
6039}
6040
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006041static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6042{
6043 return vcpu->arch.dr6;
6044}
6045
6046static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6047{
6048}
6049
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006050static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6051{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006052 get_debugreg(vcpu->arch.db[0], 0);
6053 get_debugreg(vcpu->arch.db[1], 1);
6054 get_debugreg(vcpu->arch.db[2], 2);
6055 get_debugreg(vcpu->arch.db[3], 3);
6056 get_debugreg(vcpu->arch.dr6, 6);
6057 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6058
6059 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006060 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006061}
6062
Gleb Natapov020df072010-04-13 10:05:23 +03006063static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6064{
6065 vmcs_writel(GUEST_DR7, val);
6066}
6067
Avi Kivity851ba692009-08-24 11:10:17 +03006068static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006069{
Avi Kivity06465c52007-02-28 20:46:53 +02006070 kvm_emulate_cpuid(vcpu);
6071 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006072}
6073
Avi Kivity851ba692009-08-24 11:10:17 +03006074static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006075{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006076 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006077 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006078
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006079 msr_info.index = ecx;
6080 msr_info.host_initiated = false;
6081 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006082 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006083 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006084 return 1;
6085 }
6086
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006087 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006088
Avi Kivity6aa8b732006-12-10 02:21:36 -08006089 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006090 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6091 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006092 skip_emulated_instruction(vcpu);
6093 return 1;
6094}
6095
Avi Kivity851ba692009-08-24 11:10:17 +03006096static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006097{
Will Auld8fe8ab42012-11-29 12:42:12 -08006098 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006099 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6100 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6101 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006102
Will Auld8fe8ab42012-11-29 12:42:12 -08006103 msr.data = data;
6104 msr.index = ecx;
6105 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006106 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006107 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006108 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006109 return 1;
6110 }
6111
Avi Kivity59200272010-01-25 19:47:02 +02006112 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006113 skip_emulated_instruction(vcpu);
6114 return 1;
6115}
6116
Avi Kivity851ba692009-08-24 11:10:17 +03006117static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006118{
Avi Kivity3842d132010-07-27 12:30:24 +03006119 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006120 return 1;
6121}
6122
Avi Kivity851ba692009-08-24 11:10:17 +03006123static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006124{
Eddie Dong85f455f2007-07-06 12:20:49 +03006125 u32 cpu_based_vm_exec_control;
6126
6127 /* clear pending irq */
6128 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6129 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6130 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006131
Avi Kivity3842d132010-07-27 12:30:24 +03006132 kvm_make_request(KVM_REQ_EVENT, vcpu);
6133
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006134 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006135 return 1;
6136}
6137
Avi Kivity851ba692009-08-24 11:10:17 +03006138static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006139{
Avi Kivityd3bef152007-06-05 15:53:05 +03006140 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006141}
6142
Avi Kivity851ba692009-08-24 11:10:17 +03006143static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006144{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006145 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006146}
6147
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006148static int handle_invd(struct kvm_vcpu *vcpu)
6149{
Andre Przywara51d8b662010-12-21 11:12:02 +01006150 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006151}
6152
Avi Kivity851ba692009-08-24 11:10:17 +03006153static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006154{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006155 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006156
6157 kvm_mmu_invlpg(vcpu, exit_qualification);
6158 skip_emulated_instruction(vcpu);
6159 return 1;
6160}
6161
Avi Kivityfee84b02011-11-10 14:57:25 +02006162static int handle_rdpmc(struct kvm_vcpu *vcpu)
6163{
6164 int err;
6165
6166 err = kvm_rdpmc(vcpu);
6167 kvm_complete_insn_gp(vcpu, err);
6168
6169 return 1;
6170}
6171
Avi Kivity851ba692009-08-24 11:10:17 +03006172static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006173{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006174 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006175 return 1;
6176}
6177
Dexuan Cui2acf9232010-06-10 11:27:12 +08006178static int handle_xsetbv(struct kvm_vcpu *vcpu)
6179{
6180 u64 new_bv = kvm_read_edx_eax(vcpu);
6181 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6182
6183 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6184 skip_emulated_instruction(vcpu);
6185 return 1;
6186}
6187
Wanpeng Lif53cd632014-12-02 19:14:58 +08006188static int handle_xsaves(struct kvm_vcpu *vcpu)
6189{
6190 skip_emulated_instruction(vcpu);
6191 WARN(1, "this should never happen\n");
6192 return 1;
6193}
6194
6195static int handle_xrstors(struct kvm_vcpu *vcpu)
6196{
6197 skip_emulated_instruction(vcpu);
6198 WARN(1, "this should never happen\n");
6199 return 1;
6200}
6201
Avi Kivity851ba692009-08-24 11:10:17 +03006202static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006203{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006204 if (likely(fasteoi)) {
6205 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6206 int access_type, offset;
6207
6208 access_type = exit_qualification & APIC_ACCESS_TYPE;
6209 offset = exit_qualification & APIC_ACCESS_OFFSET;
6210 /*
6211 * Sane guest uses MOV to write EOI, with written value
6212 * not cared. So make a short-circuit here by avoiding
6213 * heavy instruction emulation.
6214 */
6215 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6216 (offset == APIC_EOI)) {
6217 kvm_lapic_set_eoi(vcpu);
6218 skip_emulated_instruction(vcpu);
6219 return 1;
6220 }
6221 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006222 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006223}
6224
Yang Zhangc7c9c562013-01-25 10:18:51 +08006225static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6226{
6227 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6228 int vector = exit_qualification & 0xff;
6229
6230 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6231 kvm_apic_set_eoi_accelerated(vcpu, vector);
6232 return 1;
6233}
6234
Yang Zhang83d4c282013-01-25 10:18:49 +08006235static int handle_apic_write(struct kvm_vcpu *vcpu)
6236{
6237 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6238 u32 offset = exit_qualification & 0xfff;
6239
6240 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6241 kvm_apic_write_nodecode(vcpu, offset);
6242 return 1;
6243}
6244
Avi Kivity851ba692009-08-24 11:10:17 +03006245static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006246{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006247 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006248 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006249 bool has_error_code = false;
6250 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006251 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006252 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006253
6254 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006255 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006256 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006257
6258 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6259
6260 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006261 if (reason == TASK_SWITCH_GATE && idt_v) {
6262 switch (type) {
6263 case INTR_TYPE_NMI_INTR:
6264 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006265 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006266 break;
6267 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006268 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006269 kvm_clear_interrupt_queue(vcpu);
6270 break;
6271 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006272 if (vmx->idt_vectoring_info &
6273 VECTORING_INFO_DELIVER_CODE_MASK) {
6274 has_error_code = true;
6275 error_code =
6276 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6277 }
6278 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006279 case INTR_TYPE_SOFT_EXCEPTION:
6280 kvm_clear_exception_queue(vcpu);
6281 break;
6282 default:
6283 break;
6284 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006285 }
Izik Eidus37817f22008-03-24 23:14:53 +02006286 tss_selector = exit_qualification;
6287
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006288 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6289 type != INTR_TYPE_EXT_INTR &&
6290 type != INTR_TYPE_NMI_INTR))
6291 skip_emulated_instruction(vcpu);
6292
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006293 if (kvm_task_switch(vcpu, tss_selector,
6294 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6295 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006296 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6297 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6298 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006299 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006300 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006301
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006302 /*
6303 * TODO: What about debug traps on tss switch?
6304 * Are we supposed to inject them and update dr6?
6305 */
6306
6307 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006308}
6309
Avi Kivity851ba692009-08-24 11:10:17 +03006310static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006311{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006312 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006313 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006314 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006315 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006316
Sheng Yangf9c617f2009-03-25 10:08:52 +08006317 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006318
Sheng Yang14394422008-04-28 12:24:45 +08006319 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006320 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006321 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6322 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6323 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006324 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006325 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6326 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006327 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6328 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006329 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006330 }
6331
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006332 /*
6333 * EPT violation happened while executing iret from NMI,
6334 * "blocked by NMI" bit has to be set before next VM entry.
6335 * There are errata that may cause this bit to not be set:
6336 * AAK134, BY25.
6337 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006338 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6339 cpu_has_virtual_nmis() &&
6340 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006341 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6342
Sheng Yang14394422008-04-28 12:24:45 +08006343 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006344 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006345
Bandan Dasd95c5562016-07-12 18:18:51 -04006346 /* it is a read fault? */
6347 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6348 /* it is a write fault? */
6349 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006350 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006351 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006352 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006353 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006354
Yang Zhang25d92082013-08-06 12:00:32 +03006355 vcpu->arch.exit_qualification = exit_qualification;
6356
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006357 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006358}
6359
Avi Kivity851ba692009-08-24 11:10:17 +03006360static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006361{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006362 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006363 gpa_t gpa;
6364
6365 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006366 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006367 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006368 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006369 return 1;
6370 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006371
Paolo Bonzini450869d2015-11-04 13:41:21 +01006372 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006373 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006374 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6375 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006376
6377 if (unlikely(ret == RET_MMIO_PF_INVALID))
6378 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6379
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006380 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006381 return 1;
6382
6383 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006384 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006385
Avi Kivity851ba692009-08-24 11:10:17 +03006386 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6387 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006388
6389 return 0;
6390}
6391
Avi Kivity851ba692009-08-24 11:10:17 +03006392static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006393{
6394 u32 cpu_based_vm_exec_control;
6395
6396 /* clear pending NMI */
6397 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6398 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6399 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6400 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006401 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006402
6403 return 1;
6404}
6405
Mohammed Gamal80ced182009-09-01 12:48:18 +02006406static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006407{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006408 struct vcpu_vmx *vmx = to_vmx(vcpu);
6409 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006410 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006411 u32 cpu_exec_ctrl;
6412 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006413 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006414
6415 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6416 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006417
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006418 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006419 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006420 return handle_interrupt_window(&vmx->vcpu);
6421
Avi Kivityde87dcd2012-06-12 20:21:38 +03006422 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6423 return 1;
6424
Liran Alon114de9b2017-11-05 16:56:34 +02006425 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006426
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006427 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006428 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006429 ret = 0;
6430 goto out;
6431 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006432
Avi Kivityde5f70e2012-06-12 20:22:28 +03006433 if (err != EMULATE_DONE) {
6434 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6435 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6436 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006437 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006438 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006439
Gleb Natapov8d76c492013-05-08 18:38:44 +03006440 if (vcpu->arch.halt_request) {
6441 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006442 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006443 goto out;
6444 }
6445
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006446 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006447 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006448 if (need_resched())
6449 schedule();
6450 }
6451
Mohammed Gamal80ced182009-09-01 12:48:18 +02006452out:
6453 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006454}
6455
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006456static int __grow_ple_window(int val)
6457{
6458 if (ple_window_grow < 1)
6459 return ple_window;
6460
6461 val = min(val, ple_window_actual_max);
6462
6463 if (ple_window_grow < ple_window)
6464 val *= ple_window_grow;
6465 else
6466 val += ple_window_grow;
6467
6468 return val;
6469}
6470
6471static int __shrink_ple_window(int val, int modifier, int minimum)
6472{
6473 if (modifier < 1)
6474 return ple_window;
6475
6476 if (modifier < ple_window)
6477 val /= modifier;
6478 else
6479 val -= modifier;
6480
6481 return max(val, minimum);
6482}
6483
6484static void grow_ple_window(struct kvm_vcpu *vcpu)
6485{
6486 struct vcpu_vmx *vmx = to_vmx(vcpu);
6487 int old = vmx->ple_window;
6488
6489 vmx->ple_window = __grow_ple_window(old);
6490
6491 if (vmx->ple_window != old)
6492 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006493
6494 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006495}
6496
6497static void shrink_ple_window(struct kvm_vcpu *vcpu)
6498{
6499 struct vcpu_vmx *vmx = to_vmx(vcpu);
6500 int old = vmx->ple_window;
6501
6502 vmx->ple_window = __shrink_ple_window(old,
6503 ple_window_shrink, ple_window);
6504
6505 if (vmx->ple_window != old)
6506 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006507
6508 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006509}
6510
6511/*
6512 * ple_window_actual_max is computed to be one grow_ple_window() below
6513 * ple_window_max. (See __grow_ple_window for the reason.)
6514 * This prevents overflows, because ple_window_max is int.
6515 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6516 * this process.
6517 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6518 */
6519static void update_ple_window_actual_max(void)
6520{
6521 ple_window_actual_max =
6522 __shrink_ple_window(max(ple_window_max, ple_window),
6523 ple_window_grow, INT_MIN);
6524}
6525
Feng Wubf9f6ac2015-09-18 22:29:55 +08006526/*
6527 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6528 */
6529static void wakeup_handler(void)
6530{
6531 struct kvm_vcpu *vcpu;
6532 int cpu = smp_processor_id();
6533
6534 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6535 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6536 blocked_vcpu_list) {
6537 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6538
6539 if (pi_test_on(pi_desc) == 1)
6540 kvm_vcpu_kick(vcpu);
6541 }
6542 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6543}
6544
Tiejun Chenf2c76482014-10-28 10:14:47 +08006545static __init int hardware_setup(void)
6546{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006547 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006548
6549 rdmsrl_safe(MSR_EFER, &host_efer);
6550
6551 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6552 kvm_define_shared_msr(i, vmx_msr_index[i]);
6553
6554 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6555 if (!vmx_io_bitmap_a)
6556 return r;
6557
6558 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6559 if (!vmx_io_bitmap_b)
6560 goto out;
6561
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006562 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6563 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006564 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006565
6566 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6567 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006568 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006569
6570 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6571 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6572
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006573 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006574
6575 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6576
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006577 if (setup_vmcs_config(&vmcs_config) < 0) {
6578 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006579 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006580 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006581
6582 if (boot_cpu_has(X86_FEATURE_NX))
6583 kvm_enable_efer_bits(EFER_NX);
6584
Wanpeng Li2df19692017-03-23 05:30:08 -07006585 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6586 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006587 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006588
Tiejun Chenf2c76482014-10-28 10:14:47 +08006589 if (!cpu_has_vmx_shadow_vmcs())
6590 enable_shadow_vmcs = 0;
6591 if (enable_shadow_vmcs)
6592 init_vmcs_shadow_fields();
6593
6594 if (!cpu_has_vmx_ept() ||
6595 !cpu_has_vmx_ept_4levels()) {
6596 enable_ept = 0;
6597 enable_unrestricted_guest = 0;
6598 enable_ept_ad_bits = 0;
6599 }
6600
6601 if (!cpu_has_vmx_ept_ad_bits())
6602 enable_ept_ad_bits = 0;
6603
6604 if (!cpu_has_vmx_unrestricted_guest())
6605 enable_unrestricted_guest = 0;
6606
Paolo Bonziniad15a292015-01-30 16:18:49 +01006607 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006608 flexpriority_enabled = 0;
6609
Paolo Bonziniad15a292015-01-30 16:18:49 +01006610 /*
6611 * set_apic_access_page_addr() is used to reload apic access
6612 * page upon invalidation. No need to do anything if not
6613 * using the APIC_ACCESS_ADDR VMCS field.
6614 */
6615 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006616 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006617
6618 if (!cpu_has_vmx_tpr_shadow())
6619 kvm_x86_ops->update_cr8_intercept = NULL;
6620
6621 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6622 kvm_disable_largepages();
6623
6624 if (!cpu_has_vmx_ple())
6625 ple_gap = 0;
6626
6627 if (!cpu_has_vmx_apicv())
6628 enable_apicv = 0;
6629
Haozhong Zhang64903d62015-10-20 15:39:09 +08006630 if (cpu_has_vmx_tsc_scaling()) {
6631 kvm_has_tsc_control = true;
6632 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6633 kvm_tsc_scaling_ratio_frac_bits = 48;
6634 }
6635
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006636 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6637
Tiejun Chenbaa03522014-12-23 16:21:11 +08006638 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006639 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006640 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6641 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006642 0ull, VMX_EPT_EXECUTABLE_MASK,
6643 cpu_has_vmx_ept_execute_only() ?
6644 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006645 ept_set_mmio_spte_mask();
6646 kvm_enable_tdp();
6647 } else
6648 kvm_disable_tdp();
6649
6650 update_ple_window_actual_max();
6651
Kai Huang843e4332015-01-28 10:54:28 +08006652 /*
6653 * Only enable PML when hardware supports PML feature, and both EPT
6654 * and EPT A/D bit features are enabled -- PML depends on them to work.
6655 */
6656 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6657 enable_pml = 0;
6658
6659 if (!enable_pml) {
6660 kvm_x86_ops->slot_enable_log_dirty = NULL;
6661 kvm_x86_ops->slot_disable_log_dirty = NULL;
6662 kvm_x86_ops->flush_log_dirty = NULL;
6663 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6664 }
6665
Yunhong Jiang64672c92016-06-13 14:19:59 -07006666 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6667 u64 vmx_msr;
6668
6669 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6670 cpu_preemption_timer_multi =
6671 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6672 } else {
6673 kvm_x86_ops->set_hv_timer = NULL;
6674 kvm_x86_ops->cancel_hv_timer = NULL;
6675 }
6676
Feng Wubf9f6ac2015-09-18 22:29:55 +08006677 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6678
Ashok Rajc45dcc72016-06-22 14:59:56 +08006679 kvm_mce_cap_supported |= MCG_LMCE_P;
6680
Tiejun Chenf2c76482014-10-28 10:14:47 +08006681 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006682
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006683out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006684 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006685out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006686 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006687out1:
6688 free_page((unsigned long)vmx_io_bitmap_b);
6689out:
6690 free_page((unsigned long)vmx_io_bitmap_a);
6691
6692 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006693}
6694
6695static __exit void hardware_unsetup(void)
6696{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006697 free_page((unsigned long)vmx_io_bitmap_b);
6698 free_page((unsigned long)vmx_io_bitmap_a);
6699 free_page((unsigned long)vmx_vmwrite_bitmap);
6700 free_page((unsigned long)vmx_vmread_bitmap);
6701
Tiejun Chenf2c76482014-10-28 10:14:47 +08006702 free_kvm_area();
6703}
6704
Avi Kivity6aa8b732006-12-10 02:21:36 -08006705/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006706 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6707 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6708 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006709static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006710{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006711 if (ple_gap)
6712 grow_ple_window(vcpu);
6713
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006714 skip_emulated_instruction(vcpu);
6715 kvm_vcpu_on_spin(vcpu);
6716
6717 return 1;
6718}
6719
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006720static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006721{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006722 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006723 return 1;
6724}
6725
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006726static int handle_mwait(struct kvm_vcpu *vcpu)
6727{
6728 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6729 return handle_nop(vcpu);
6730}
6731
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006732static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6733{
6734 return 1;
6735}
6736
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006737static int handle_monitor(struct kvm_vcpu *vcpu)
6738{
6739 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6740 return handle_nop(vcpu);
6741}
6742
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006743/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006744 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6745 * set the success or error code of an emulated VMX instruction, as specified
6746 * by Vol 2B, VMX Instruction Reference, "Conventions".
6747 */
6748static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6749{
6750 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6751 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6752 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6753}
6754
6755static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6756{
6757 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6758 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6759 X86_EFLAGS_SF | X86_EFLAGS_OF))
6760 | X86_EFLAGS_CF);
6761}
6762
Abel Gordon145c28d2013-04-18 14:36:55 +03006763static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006764 u32 vm_instruction_error)
6765{
6766 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6767 /*
6768 * failValid writes the error number to the current VMCS, which
6769 * can't be done there isn't a current VMCS.
6770 */
6771 nested_vmx_failInvalid(vcpu);
6772 return;
6773 }
6774 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6775 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6776 X86_EFLAGS_SF | X86_EFLAGS_OF))
6777 | X86_EFLAGS_ZF);
6778 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6779 /*
6780 * We don't need to force a shadow sync because
6781 * VM_INSTRUCTION_ERROR is not shadowed
6782 */
6783}
Abel Gordon145c28d2013-04-18 14:36:55 +03006784
Wincy Vanff651cb2014-12-11 08:52:58 +03006785static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6786{
6787 /* TODO: not to reset guest simply here. */
6788 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006789 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006790}
6791
Jan Kiszkaf4124502014-03-07 20:03:13 +01006792static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6793{
6794 struct vcpu_vmx *vmx =
6795 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6796
6797 vmx->nested.preemption_timer_expired = true;
6798 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6799 kvm_vcpu_kick(&vmx->vcpu);
6800
6801 return HRTIMER_NORESTART;
6802}
6803
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006804/*
Bandan Das19677e32014-05-06 02:19:15 -04006805 * Decode the memory-address operand of a vmx instruction, as recorded on an
6806 * exit caused by such an instruction (run by a guest hypervisor).
6807 * On success, returns 0. When the operand is invalid, returns 1 and throws
6808 * #UD or #GP.
6809 */
6810static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6811 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006812 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006813{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006814 gva_t off;
6815 bool exn;
6816 struct kvm_segment s;
6817
Bandan Das19677e32014-05-06 02:19:15 -04006818 /*
6819 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6820 * Execution", on an exit, vmx_instruction_info holds most of the
6821 * addressing components of the operand. Only the displacement part
6822 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6823 * For how an actual address is calculated from all these components,
6824 * refer to Vol. 1, "Operand Addressing".
6825 */
6826 int scaling = vmx_instruction_info & 3;
6827 int addr_size = (vmx_instruction_info >> 7) & 7;
6828 bool is_reg = vmx_instruction_info & (1u << 10);
6829 int seg_reg = (vmx_instruction_info >> 15) & 7;
6830 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6831 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6832 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6833 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6834
6835 if (is_reg) {
6836 kvm_queue_exception(vcpu, UD_VECTOR);
6837 return 1;
6838 }
6839
6840 /* Addr = segment_base + offset */
6841 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006842 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006843 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006844 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006845 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006846 off += kvm_register_read(vcpu, index_reg)<<scaling;
6847 vmx_get_segment(vcpu, &s, seg_reg);
6848 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006849
6850 if (addr_size == 1) /* 32 bit */
6851 *ret &= 0xffffffff;
6852
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006853 /* Checks for #GP/#SS exceptions. */
6854 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006855 if (is_long_mode(vcpu)) {
6856 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6857 * non-canonical form. This is the only check on the memory
6858 * destination for long mode!
6859 */
6860 exn = is_noncanonical_address(*ret);
6861 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006862 /* Protected mode: apply checks for segment validity in the
6863 * following order:
6864 * - segment type check (#GP(0) may be thrown)
6865 * - usability check (#GP(0)/#SS(0))
6866 * - limit check (#GP(0)/#SS(0))
6867 */
6868 if (wr)
6869 /* #GP(0) if the destination operand is located in a
6870 * read-only data segment or any code segment.
6871 */
6872 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6873 else
6874 /* #GP(0) if the source operand is located in an
6875 * execute-only code segment
6876 */
6877 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006878 if (exn) {
6879 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6880 return 1;
6881 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006882 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6883 */
6884 exn = (s.unusable != 0);
6885 /* Protected mode: #GP(0)/#SS(0) if the memory
6886 * operand is outside the segment limit.
6887 */
6888 exn = exn || (off + sizeof(u64) > s.limit);
6889 }
6890 if (exn) {
6891 kvm_queue_exception_e(vcpu,
6892 seg_reg == VCPU_SREG_SS ?
6893 SS_VECTOR : GP_VECTOR,
6894 0);
6895 return 1;
6896 }
6897
Bandan Das19677e32014-05-06 02:19:15 -04006898 return 0;
6899}
6900
6901/*
Bandan Das3573e222014-05-06 02:19:16 -04006902 * This function performs the various checks including
6903 * - if it's 4KB aligned
6904 * - No bits beyond the physical address width are set
6905 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006906 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006907 */
Bandan Das4291b582014-05-06 02:19:18 -04006908static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6909 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006910{
6911 gva_t gva;
6912 gpa_t vmptr;
6913 struct x86_exception e;
6914 struct page *page;
6915 struct vcpu_vmx *vmx = to_vmx(vcpu);
6916 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6917
6918 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006919 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006920 return 1;
6921
6922 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6923 sizeof(vmptr), &e)) {
6924 kvm_inject_page_fault(vcpu, &e);
6925 return 1;
6926 }
6927
6928 switch (exit_reason) {
6929 case EXIT_REASON_VMON:
6930 /*
6931 * SDM 3: 24.11.5
6932 * The first 4 bytes of VMXON region contain the supported
6933 * VMCS revision identifier
6934 *
6935 * Note - IA32_VMX_BASIC[48] will never be 1
6936 * for the nested case;
6937 * which replaces physical address width with 32
6938 *
6939 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006940 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006941 nested_vmx_failInvalid(vcpu);
6942 skip_emulated_instruction(vcpu);
6943 return 1;
6944 }
6945
6946 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006947 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04006948 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006949 skip_emulated_instruction(vcpu);
6950 return 1;
6951 }
6952 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04006953 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006954 nested_release_page_clean(page);
6955 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04006956 skip_emulated_instruction(vcpu);
6957 return 1;
6958 }
6959 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006960 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04006961 vmx->nested.vmxon_ptr = vmptr;
6962 break;
Bandan Das4291b582014-05-06 02:19:18 -04006963 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006964 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006965 nested_vmx_failValid(vcpu,
6966 VMXERR_VMCLEAR_INVALID_ADDRESS);
6967 skip_emulated_instruction(vcpu);
6968 return 1;
6969 }
Bandan Das3573e222014-05-06 02:19:16 -04006970
Bandan Das4291b582014-05-06 02:19:18 -04006971 if (vmptr == vmx->nested.vmxon_ptr) {
6972 nested_vmx_failValid(vcpu,
6973 VMXERR_VMCLEAR_VMXON_POINTER);
6974 skip_emulated_instruction(vcpu);
6975 return 1;
6976 }
6977 break;
6978 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006979 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006980 nested_vmx_failValid(vcpu,
6981 VMXERR_VMPTRLD_INVALID_ADDRESS);
6982 skip_emulated_instruction(vcpu);
6983 return 1;
6984 }
6985
6986 if (vmptr == vmx->nested.vmxon_ptr) {
6987 nested_vmx_failValid(vcpu,
6988 VMXERR_VMCLEAR_VMXON_POINTER);
6989 skip_emulated_instruction(vcpu);
6990 return 1;
6991 }
6992 break;
Bandan Das3573e222014-05-06 02:19:16 -04006993 default:
6994 return 1; /* shouldn't happen */
6995 }
6996
Bandan Das4291b582014-05-06 02:19:18 -04006997 if (vmpointer)
6998 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006999 return 0;
7000}
7001
7002/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007003 * Emulate the VMXON instruction.
7004 * Currently, we just remember that VMX is active, and do not save or even
7005 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7006 * do not currently need to store anything in that guest-allocated memory
7007 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7008 * argument is different from the VMXON pointer (which the spec says they do).
7009 */
7010static int handle_vmon(struct kvm_vcpu *vcpu)
7011{
7012 struct kvm_segment cs;
7013 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007014 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007015 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7016 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007017 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007018
7019 /* The Intel VMX Instruction Reference lists a bunch of bits that
7020 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7021 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7022 * Otherwise, we should fail with #UD. We test these now:
7023 */
7024 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7025 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7026 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7027 kvm_queue_exception(vcpu, UD_VECTOR);
7028 return 1;
7029 }
7030
7031 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7032 if (is_long_mode(vcpu) && !cs.l) {
7033 kvm_queue_exception(vcpu, UD_VECTOR);
7034 return 1;
7035 }
7036
7037 if (vmx_get_cpl(vcpu)) {
7038 kvm_inject_gp(vcpu, 0);
7039 return 1;
7040 }
Bandan Das3573e222014-05-06 02:19:16 -04007041
Bandan Das4291b582014-05-06 02:19:18 -04007042 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007043 return 1;
7044
Abel Gordon145c28d2013-04-18 14:36:55 +03007045 if (vmx->nested.vmxon) {
7046 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7047 skip_emulated_instruction(vcpu);
7048 return 1;
7049 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007050
Haozhong Zhang3b840802016-06-22 14:59:54 +08007051 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007052 != VMXON_NEEDED_FEATURES) {
7053 kvm_inject_gp(vcpu, 0);
7054 return 1;
7055 }
7056
Paolo Bonziniff546f92018-01-11 12:16:15 +01007057 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7058 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007059 goto out_vmcs02;
Radim Krčmářd048c092016-08-08 20:16:22 +02007060
David Matlack4f2777b2016-07-13 17:16:37 -07007061 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7062 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007063 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007064
Abel Gordon8de48832013-04-18 14:37:25 +03007065 if (enable_shadow_vmcs) {
7066 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007067 if (!shadow_vmcs)
7068 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007069 /* mark vmcs as shadow */
7070 shadow_vmcs->revision_id |= (1u << 31);
7071 /* init shadow vmcs */
7072 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007073 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007074 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007075
Jan Kiszkaf4124502014-03-07 20:03:13 +01007076 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007077 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007078 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7079
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007080 vmx->nested.vmxon = true;
7081
7082 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007083 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007084 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007085
7086out_shadow_vmcs:
7087 kfree(vmx->nested.cached_vmcs12);
7088
7089out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007090 free_loaded_vmcs(&vmx->nested.vmcs02);
Radim Krčmářd048c092016-08-08 20:16:22 +02007091
Jim Mattson46e24df2017-11-27 17:22:25 -06007092out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007093 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007094}
7095
7096/*
7097 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7098 * for running VMX instructions (except VMXON, whose prerequisites are
7099 * slightly different). It also specifies what exception to inject otherwise.
7100 */
7101static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7102{
7103 struct kvm_segment cs;
7104 struct vcpu_vmx *vmx = to_vmx(vcpu);
7105
7106 if (!vmx->nested.vmxon) {
7107 kvm_queue_exception(vcpu, UD_VECTOR);
7108 return 0;
7109 }
7110
7111 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7112 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7113 (is_long_mode(vcpu) && !cs.l)) {
7114 kvm_queue_exception(vcpu, UD_VECTOR);
7115 return 0;
7116 }
7117
7118 if (vmx_get_cpl(vcpu)) {
7119 kvm_inject_gp(vcpu, 0);
7120 return 0;
7121 }
7122
7123 return 1;
7124}
7125
Abel Gordone7953d72013-04-18 14:37:55 +03007126static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7127{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007128 if (vmx->nested.current_vmptr == -1ull)
7129 return;
7130
7131 /* current_vmptr and current_vmcs12 are always set/reset together */
7132 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7133 return;
7134
Abel Gordon012f83c2013-04-18 14:39:25 +03007135 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007136 /* copy to memory all shadowed fields in case
7137 they were modified */
7138 copy_shadow_to_vmcs12(vmx);
7139 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007140 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7141 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007142 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007143 }
Wincy Van705699a2015-02-03 23:58:17 +08007144 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007145
7146 /* Flush VMCS12 to guest memory */
7147 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7148 VMCS12_SIZE);
7149
Abel Gordone7953d72013-04-18 14:37:55 +03007150 kunmap(vmx->nested.current_vmcs12_page);
7151 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007152 vmx->nested.current_vmptr = -1ull;
7153 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007154}
7155
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007156/*
7157 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7158 * just stops using VMX.
7159 */
7160static void free_nested(struct vcpu_vmx *vmx)
7161{
7162 if (!vmx->nested.vmxon)
7163 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007164
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007165 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007166 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007167 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007168 if (enable_shadow_vmcs) {
7169 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7170 free_vmcs(vmx->vmcs01.shadow_vmcs);
7171 vmx->vmcs01.shadow_vmcs = NULL;
7172 }
David Matlack4f2777b2016-07-13 17:16:37 -07007173 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007174 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007175 if (vmx->nested.apic_access_page) {
7176 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007177 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007178 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007179 if (vmx->nested.virtual_apic_page) {
7180 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007181 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007182 }
Wincy Van705699a2015-02-03 23:58:17 +08007183 if (vmx->nested.pi_desc_page) {
7184 kunmap(vmx->nested.pi_desc_page);
7185 nested_release_page(vmx->nested.pi_desc_page);
7186 vmx->nested.pi_desc_page = NULL;
7187 vmx->nested.pi_desc = NULL;
7188 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007189
Jim Mattson46e24df2017-11-27 17:22:25 -06007190 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007191}
7192
7193/* Emulate the VMXOFF instruction */
7194static int handle_vmoff(struct kvm_vcpu *vcpu)
7195{
7196 if (!nested_vmx_check_permission(vcpu))
7197 return 1;
7198 free_nested(to_vmx(vcpu));
7199 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007200 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007201 return 1;
7202}
7203
Nadav Har'El27d6c862011-05-25 23:06:59 +03007204/* Emulate the VMCLEAR instruction */
7205static int handle_vmclear(struct kvm_vcpu *vcpu)
7206{
7207 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007208 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007209 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007210
7211 if (!nested_vmx_check_permission(vcpu))
7212 return 1;
7213
Bandan Das4291b582014-05-06 02:19:18 -04007214 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007215 return 1;
7216
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007217 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007218 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007219
Jim Mattson29deec42017-03-02 12:41:48 -08007220 kvm_vcpu_write_guest(vcpu,
7221 vmptr + offsetof(struct vmcs12, launch_state),
7222 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007223
Nadav Har'El27d6c862011-05-25 23:06:59 +03007224 skip_emulated_instruction(vcpu);
7225 nested_vmx_succeed(vcpu);
7226 return 1;
7227}
7228
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007229static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7230
7231/* Emulate the VMLAUNCH instruction */
7232static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7233{
7234 return nested_vmx_run(vcpu, true);
7235}
7236
7237/* Emulate the VMRESUME instruction */
7238static int handle_vmresume(struct kvm_vcpu *vcpu)
7239{
7240
7241 return nested_vmx_run(vcpu, false);
7242}
7243
Nadav Har'El49f705c2011-05-25 23:08:30 +03007244enum vmcs_field_type {
7245 VMCS_FIELD_TYPE_U16 = 0,
7246 VMCS_FIELD_TYPE_U64 = 1,
7247 VMCS_FIELD_TYPE_U32 = 2,
7248 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7249};
7250
7251static inline int vmcs_field_type(unsigned long field)
7252{
7253 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7254 return VMCS_FIELD_TYPE_U32;
7255 return (field >> 13) & 0x3 ;
7256}
7257
7258static inline int vmcs_field_readonly(unsigned long field)
7259{
7260 return (((field >> 10) & 0x3) == 1);
7261}
7262
7263/*
7264 * Read a vmcs12 field. Since these can have varying lengths and we return
7265 * one type, we chose the biggest type (u64) and zero-extend the return value
7266 * to that size. Note that the caller, handle_vmread, might need to use only
7267 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7268 * 64-bit fields are to be returned).
7269 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007270static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7271 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007272{
7273 short offset = vmcs_field_to_offset(field);
7274 char *p;
7275
7276 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007277 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007278
7279 p = ((char *)(get_vmcs12(vcpu))) + offset;
7280
7281 switch (vmcs_field_type(field)) {
7282 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7283 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007284 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007285 case VMCS_FIELD_TYPE_U16:
7286 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007287 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007288 case VMCS_FIELD_TYPE_U32:
7289 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007290 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007291 case VMCS_FIELD_TYPE_U64:
7292 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007293 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007294 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007295 WARN_ON(1);
7296 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007297 }
7298}
7299
Abel Gordon20b97fe2013-04-18 14:36:25 +03007300
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007301static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7302 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007303 short offset = vmcs_field_to_offset(field);
7304 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7305 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007306 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007307
7308 switch (vmcs_field_type(field)) {
7309 case VMCS_FIELD_TYPE_U16:
7310 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007311 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007312 case VMCS_FIELD_TYPE_U32:
7313 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007314 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007315 case VMCS_FIELD_TYPE_U64:
7316 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007317 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007318 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7319 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007320 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007321 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007322 WARN_ON(1);
7323 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007324 }
7325
7326}
7327
Abel Gordon16f5b902013-04-18 14:38:25 +03007328static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7329{
7330 int i;
7331 unsigned long field;
7332 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007333 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007334 const unsigned long *fields = shadow_read_write_fields;
7335 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007336
Jan Kiszka282da872014-10-08 18:05:39 +02007337 preempt_disable();
7338
Abel Gordon16f5b902013-04-18 14:38:25 +03007339 vmcs_load(shadow_vmcs);
7340
7341 for (i = 0; i < num_fields; i++) {
7342 field = fields[i];
7343 switch (vmcs_field_type(field)) {
7344 case VMCS_FIELD_TYPE_U16:
7345 field_value = vmcs_read16(field);
7346 break;
7347 case VMCS_FIELD_TYPE_U32:
7348 field_value = vmcs_read32(field);
7349 break;
7350 case VMCS_FIELD_TYPE_U64:
7351 field_value = vmcs_read64(field);
7352 break;
7353 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7354 field_value = vmcs_readl(field);
7355 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007356 default:
7357 WARN_ON(1);
7358 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007359 }
7360 vmcs12_write_any(&vmx->vcpu, field, field_value);
7361 }
7362
7363 vmcs_clear(shadow_vmcs);
7364 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007365
7366 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007367}
7368
Abel Gordonc3114422013-04-18 14:38:55 +03007369static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7370{
Mathias Krausec2bae892013-06-26 20:36:21 +02007371 const unsigned long *fields[] = {
7372 shadow_read_write_fields,
7373 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007374 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007375 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007376 max_shadow_read_write_fields,
7377 max_shadow_read_only_fields
7378 };
7379 int i, q;
7380 unsigned long field;
7381 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007382 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007383
7384 vmcs_load(shadow_vmcs);
7385
Mathias Krausec2bae892013-06-26 20:36:21 +02007386 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007387 for (i = 0; i < max_fields[q]; i++) {
7388 field = fields[q][i];
7389 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7390
7391 switch (vmcs_field_type(field)) {
7392 case VMCS_FIELD_TYPE_U16:
7393 vmcs_write16(field, (u16)field_value);
7394 break;
7395 case VMCS_FIELD_TYPE_U32:
7396 vmcs_write32(field, (u32)field_value);
7397 break;
7398 case VMCS_FIELD_TYPE_U64:
7399 vmcs_write64(field, (u64)field_value);
7400 break;
7401 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7402 vmcs_writel(field, (long)field_value);
7403 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007404 default:
7405 WARN_ON(1);
7406 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007407 }
7408 }
7409 }
7410
7411 vmcs_clear(shadow_vmcs);
7412 vmcs_load(vmx->loaded_vmcs->vmcs);
7413}
7414
Nadav Har'El49f705c2011-05-25 23:08:30 +03007415/*
7416 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7417 * used before) all generate the same failure when it is missing.
7418 */
7419static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7420{
7421 struct vcpu_vmx *vmx = to_vmx(vcpu);
7422 if (vmx->nested.current_vmptr == -1ull) {
7423 nested_vmx_failInvalid(vcpu);
7424 skip_emulated_instruction(vcpu);
7425 return 0;
7426 }
7427 return 1;
7428}
7429
7430static int handle_vmread(struct kvm_vcpu *vcpu)
7431{
7432 unsigned long field;
7433 u64 field_value;
7434 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7435 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7436 gva_t gva = 0;
7437
7438 if (!nested_vmx_check_permission(vcpu) ||
7439 !nested_vmx_check_vmcs12(vcpu))
7440 return 1;
7441
7442 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007443 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007444 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007445 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007446 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7447 skip_emulated_instruction(vcpu);
7448 return 1;
7449 }
7450 /*
7451 * Now copy part of this value to register or memory, as requested.
7452 * Note that the number of bits actually copied is 32 or 64 depending
7453 * on the guest's mode (32 or 64 bit), not on the given field's length.
7454 */
7455 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007456 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007457 field_value);
7458 } else {
7459 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007460 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007461 return 1;
7462 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7463 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7464 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7465 }
7466
7467 nested_vmx_succeed(vcpu);
7468 skip_emulated_instruction(vcpu);
7469 return 1;
7470}
7471
7472
7473static int handle_vmwrite(struct kvm_vcpu *vcpu)
7474{
7475 unsigned long field;
7476 gva_t gva;
7477 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7478 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007479 /* The value to write might be 32 or 64 bits, depending on L1's long
7480 * mode, and eventually we need to write that into a field of several
7481 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007482 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007483 * bits into the vmcs12 field.
7484 */
7485 u64 field_value = 0;
7486 struct x86_exception e;
7487
7488 if (!nested_vmx_check_permission(vcpu) ||
7489 !nested_vmx_check_vmcs12(vcpu))
7490 return 1;
7491
7492 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007493 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007494 (((vmx_instruction_info) >> 3) & 0xf));
7495 else {
7496 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007497 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007498 return 1;
7499 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007500 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007501 kvm_inject_page_fault(vcpu, &e);
7502 return 1;
7503 }
7504 }
7505
7506
Nadav Amit27e6fb52014-06-18 17:19:26 +03007507 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007508 if (vmcs_field_readonly(field)) {
7509 nested_vmx_failValid(vcpu,
7510 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7511 skip_emulated_instruction(vcpu);
7512 return 1;
7513 }
7514
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007515 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007516 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7517 skip_emulated_instruction(vcpu);
7518 return 1;
7519 }
7520
7521 nested_vmx_succeed(vcpu);
7522 skip_emulated_instruction(vcpu);
7523 return 1;
7524}
7525
Nadav Har'El63846662011-05-25 23:07:29 +03007526/* Emulate the VMPTRLD instruction */
7527static int handle_vmptrld(struct kvm_vcpu *vcpu)
7528{
7529 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007530 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007531
7532 if (!nested_vmx_check_permission(vcpu))
7533 return 1;
7534
Bandan Das4291b582014-05-06 02:19:18 -04007535 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007536 return 1;
7537
Nadav Har'El63846662011-05-25 23:07:29 +03007538 if (vmx->nested.current_vmptr != vmptr) {
7539 struct vmcs12 *new_vmcs12;
7540 struct page *page;
7541 page = nested_get_page(vcpu, vmptr);
7542 if (page == NULL) {
7543 nested_vmx_failInvalid(vcpu);
7544 skip_emulated_instruction(vcpu);
7545 return 1;
7546 }
7547 new_vmcs12 = kmap(page);
7548 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7549 kunmap(page);
7550 nested_release_page_clean(page);
7551 nested_vmx_failValid(vcpu,
7552 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7553 skip_emulated_instruction(vcpu);
7554 return 1;
7555 }
Nadav Har'El63846662011-05-25 23:07:29 +03007556
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007557 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007558 vmx->nested.current_vmptr = vmptr;
7559 vmx->nested.current_vmcs12 = new_vmcs12;
7560 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007561 /*
7562 * Load VMCS12 from guest memory since it is not already
7563 * cached.
7564 */
7565 memcpy(vmx->nested.cached_vmcs12,
7566 vmx->nested.current_vmcs12, VMCS12_SIZE);
7567
Abel Gordon012f83c2013-04-18 14:39:25 +03007568 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007569 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7570 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007571 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007572 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007573 vmx->nested.sync_shadow_vmcs = true;
7574 }
Nadav Har'El63846662011-05-25 23:07:29 +03007575 }
7576
7577 nested_vmx_succeed(vcpu);
7578 skip_emulated_instruction(vcpu);
7579 return 1;
7580}
7581
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007582/* Emulate the VMPTRST instruction */
7583static int handle_vmptrst(struct kvm_vcpu *vcpu)
7584{
7585 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7586 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7587 gva_t vmcs_gva;
7588 struct x86_exception e;
7589
7590 if (!nested_vmx_check_permission(vcpu))
7591 return 1;
7592
7593 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007594 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007595 return 1;
7596 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7597 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7598 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7599 sizeof(u64), &e)) {
7600 kvm_inject_page_fault(vcpu, &e);
7601 return 1;
7602 }
7603 nested_vmx_succeed(vcpu);
7604 skip_emulated_instruction(vcpu);
7605 return 1;
7606}
7607
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007608/* Emulate the INVEPT instruction */
7609static int handle_invept(struct kvm_vcpu *vcpu)
7610{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007611 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007612 u32 vmx_instruction_info, types;
7613 unsigned long type;
7614 gva_t gva;
7615 struct x86_exception e;
7616 struct {
7617 u64 eptp, gpa;
7618 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007619
Wincy Vanb9c237b2015-02-03 23:56:30 +08007620 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7621 SECONDARY_EXEC_ENABLE_EPT) ||
7622 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007623 kvm_queue_exception(vcpu, UD_VECTOR);
7624 return 1;
7625 }
7626
7627 if (!nested_vmx_check_permission(vcpu))
7628 return 1;
7629
7630 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7631 kvm_queue_exception(vcpu, UD_VECTOR);
7632 return 1;
7633 }
7634
7635 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007636 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007637
Wincy Vanb9c237b2015-02-03 23:56:30 +08007638 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007639
Jim Mattson85c856b2016-10-26 08:38:38 -07007640 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007641 nested_vmx_failValid(vcpu,
7642 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007643 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007644 return 1;
7645 }
7646
7647 /* According to the Intel VMX instruction reference, the memory
7648 * operand is read even if it isn't needed (e.g., for type==global)
7649 */
7650 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007651 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007652 return 1;
7653 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7654 sizeof(operand), &e)) {
7655 kvm_inject_page_fault(vcpu, &e);
7656 return 1;
7657 }
7658
7659 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007660 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007661 /*
7662 * TODO: track mappings and invalidate
7663 * single context requests appropriately
7664 */
7665 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007666 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007667 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007668 nested_vmx_succeed(vcpu);
7669 break;
7670 default:
7671 BUG_ON(1);
7672 break;
7673 }
7674
7675 skip_emulated_instruction(vcpu);
7676 return 1;
7677}
7678
Petr Matouseka642fc32014-09-23 20:22:30 +02007679static int handle_invvpid(struct kvm_vcpu *vcpu)
7680{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007681 struct vcpu_vmx *vmx = to_vmx(vcpu);
7682 u32 vmx_instruction_info;
7683 unsigned long type, types;
7684 gva_t gva;
7685 struct x86_exception e;
7686 int vpid;
7687
7688 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7689 SECONDARY_EXEC_ENABLE_VPID) ||
7690 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7691 kvm_queue_exception(vcpu, UD_VECTOR);
7692 return 1;
7693 }
7694
7695 if (!nested_vmx_check_permission(vcpu))
7696 return 1;
7697
7698 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7699 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7700
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007701 types = (vmx->nested.nested_vmx_vpid_caps &
7702 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007703
Jim Mattson85c856b2016-10-26 08:38:38 -07007704 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007705 nested_vmx_failValid(vcpu,
7706 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007707 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007708 return 1;
7709 }
7710
7711 /* according to the intel vmx instruction reference, the memory
7712 * operand is read even if it isn't needed (e.g., for type==global)
7713 */
7714 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7715 vmx_instruction_info, false, &gva))
7716 return 1;
7717 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7718 sizeof(u32), &e)) {
7719 kvm_inject_page_fault(vcpu, &e);
7720 return 1;
7721 }
7722
7723 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007724 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007725 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007726 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7727 if (!vpid) {
7728 nested_vmx_failValid(vcpu,
7729 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7730 skip_emulated_instruction(vcpu);
7731 return 1;
7732 }
7733 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007734 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007735 break;
7736 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007737 WARN_ON_ONCE(1);
7738 skip_emulated_instruction(vcpu);
7739 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007740 }
7741
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007742 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7743 nested_vmx_succeed(vcpu);
7744
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007745 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007746 return 1;
7747}
7748
Kai Huang843e4332015-01-28 10:54:28 +08007749static int handle_pml_full(struct kvm_vcpu *vcpu)
7750{
7751 unsigned long exit_qualification;
7752
7753 trace_kvm_pml_full(vcpu->vcpu_id);
7754
7755 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7756
7757 /*
7758 * PML buffer FULL happened while executing iret from NMI,
7759 * "blocked by NMI" bit has to be set before next VM entry.
7760 */
7761 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7762 cpu_has_virtual_nmis() &&
7763 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7764 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7765 GUEST_INTR_STATE_NMI);
7766
7767 /*
7768 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7769 * here.., and there's no userspace involvement needed for PML.
7770 */
7771 return 1;
7772}
7773
Yunhong Jiang64672c92016-06-13 14:19:59 -07007774static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7775{
7776 kvm_lapic_expired_hv_timer(vcpu);
7777 return 1;
7778}
7779
Nadav Har'El0140cae2011-05-25 23:06:28 +03007780/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007781 * The exit handlers return 1 if the exit was handled fully and guest execution
7782 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7783 * to be done to userspace and return 0.
7784 */
Mathias Krause772e0312012-08-30 01:30:19 +02007785static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007786 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7787 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007788 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007789 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007790 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007791 [EXIT_REASON_CR_ACCESS] = handle_cr,
7792 [EXIT_REASON_DR_ACCESS] = handle_dr,
7793 [EXIT_REASON_CPUID] = handle_cpuid,
7794 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7795 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7796 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7797 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007798 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007799 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007800 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007801 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007802 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007803 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007804 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007805 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007806 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007807 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007808 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007809 [EXIT_REASON_VMOFF] = handle_vmoff,
7810 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007811 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7812 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007813 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007814 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007815 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007816 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007817 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007818 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007819 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7820 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007821 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007822 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007823 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007824 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007825 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007826 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007827 [EXIT_REASON_XSAVES] = handle_xsaves,
7828 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007829 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007830 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007831};
7832
7833static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007834 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007835
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007836static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7837 struct vmcs12 *vmcs12)
7838{
7839 unsigned long exit_qualification;
7840 gpa_t bitmap, last_bitmap;
7841 unsigned int port;
7842 int size;
7843 u8 b;
7844
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007845 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007846 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007847
7848 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7849
7850 port = exit_qualification >> 16;
7851 size = (exit_qualification & 7) + 1;
7852
7853 last_bitmap = (gpa_t)-1;
7854 b = -1;
7855
7856 while (size > 0) {
7857 if (port < 0x8000)
7858 bitmap = vmcs12->io_bitmap_a;
7859 else if (port < 0x10000)
7860 bitmap = vmcs12->io_bitmap_b;
7861 else
Joe Perches1d804d02015-03-30 16:46:09 -07007862 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007863 bitmap += (port & 0x7fff) / 8;
7864
7865 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007866 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007867 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007868 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007869 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007870
7871 port++;
7872 size--;
7873 last_bitmap = bitmap;
7874 }
7875
Joe Perches1d804d02015-03-30 16:46:09 -07007876 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007877}
7878
Nadav Har'El644d7112011-05-25 23:12:35 +03007879/*
7880 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7881 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7882 * disinterest in the current event (read or write a specific MSR) by using an
7883 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7884 */
7885static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7886 struct vmcs12 *vmcs12, u32 exit_reason)
7887{
7888 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7889 gpa_t bitmap;
7890
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007891 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007892 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007893
7894 /*
7895 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7896 * for the four combinations of read/write and low/high MSR numbers.
7897 * First we need to figure out which of the four to use:
7898 */
7899 bitmap = vmcs12->msr_bitmap;
7900 if (exit_reason == EXIT_REASON_MSR_WRITE)
7901 bitmap += 2048;
7902 if (msr_index >= 0xc0000000) {
7903 msr_index -= 0xc0000000;
7904 bitmap += 1024;
7905 }
7906
7907 /* Then read the msr_index'th bit from this bitmap: */
7908 if (msr_index < 1024*8) {
7909 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007910 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007911 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007912 return 1 & (b >> (msr_index & 7));
7913 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007914 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007915}
7916
7917/*
7918 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7919 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7920 * intercept (via guest_host_mask etc.) the current event.
7921 */
7922static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7923 struct vmcs12 *vmcs12)
7924{
7925 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7926 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02007927 int reg;
7928 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03007929
7930 switch ((exit_qualification >> 4) & 3) {
7931 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02007932 reg = (exit_qualification >> 8) & 15;
7933 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007934 switch (cr) {
7935 case 0:
7936 if (vmcs12->cr0_guest_host_mask &
7937 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007938 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007939 break;
7940 case 3:
7941 if ((vmcs12->cr3_target_count >= 1 &&
7942 vmcs12->cr3_target_value0 == val) ||
7943 (vmcs12->cr3_target_count >= 2 &&
7944 vmcs12->cr3_target_value1 == val) ||
7945 (vmcs12->cr3_target_count >= 3 &&
7946 vmcs12->cr3_target_value2 == val) ||
7947 (vmcs12->cr3_target_count >= 4 &&
7948 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007949 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007950 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007951 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007952 break;
7953 case 4:
7954 if (vmcs12->cr4_guest_host_mask &
7955 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007956 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007957 break;
7958 case 8:
7959 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007960 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007961 break;
7962 }
7963 break;
7964 case 2: /* clts */
7965 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7966 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007967 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007968 break;
7969 case 1: /* mov from cr */
7970 switch (cr) {
7971 case 3:
7972 if (vmcs12->cpu_based_vm_exec_control &
7973 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007974 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007975 break;
7976 case 8:
7977 if (vmcs12->cpu_based_vm_exec_control &
7978 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007979 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007980 break;
7981 }
7982 break;
7983 case 3: /* lmsw */
7984 /*
7985 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7986 * cr0. Other attempted changes are ignored, with no exit.
7987 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02007988 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03007989 if (vmcs12->cr0_guest_host_mask & 0xe &
7990 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007991 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007992 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7993 !(vmcs12->cr0_read_shadow & 0x1) &&
7994 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07007995 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007996 break;
7997 }
Joe Perches1d804d02015-03-30 16:46:09 -07007998 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007999}
8000
8001/*
8002 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8003 * should handle it ourselves in L0 (and then continue L2). Only call this
8004 * when in is_guest_mode (L2).
8005 */
8006static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8007{
Nadav Har'El644d7112011-05-25 23:12:35 +03008008 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8009 struct vcpu_vmx *vmx = to_vmx(vcpu);
8010 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008011 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008012
Jan Kiszka542060e2014-01-04 18:47:21 +01008013 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8014 vmcs_readl(EXIT_QUALIFICATION),
8015 vmx->idt_vectoring_info,
8016 intr_info,
8017 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8018 KVM_ISA_VMX);
8019
David Matlackb7649e12017-08-01 14:00:40 -07008020 /*
8021 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008022 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8023 * Page). The CPU may write to these pages via their host
8024 * physical address while L2 is running, bypassing any
8025 * address-translation-based dirty tracking (e.g. EPT write
8026 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008027 *
8028 * Mark them dirty on every exit from L2 to prevent them from
8029 * getting out of sync with dirty tracking.
8030 */
8031 nested_mark_vmcs12_pages_dirty(vcpu);
8032
Nadav Har'El644d7112011-05-25 23:12:35 +03008033 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008034 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008035
8036 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008037 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8038 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008039 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008040 }
8041
8042 switch (exit_reason) {
8043 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008044 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008045 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008046 else if (is_page_fault(intr_info))
8047 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008048 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008049 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008050 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008051 else if (is_debug(intr_info) &&
8052 vcpu->guest_debug &
8053 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8054 return false;
8055 else if (is_breakpoint(intr_info) &&
8056 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8057 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008058 return vmcs12->exception_bitmap &
8059 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8060 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008061 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008062 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008063 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008064 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008065 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008066 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008067 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008068 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008069 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008070 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008071 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008072 case EXIT_REASON_HLT:
8073 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8074 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008075 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008076 case EXIT_REASON_INVLPG:
8077 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8078 case EXIT_REASON_RDPMC:
8079 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008080 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008081 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8082 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8083 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8084 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8085 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8086 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008087 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008088 /*
8089 * VMX instructions trap unconditionally. This allows L1 to
8090 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8091 */
Joe Perches1d804d02015-03-30 16:46:09 -07008092 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008093 case EXIT_REASON_CR_ACCESS:
8094 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8095 case EXIT_REASON_DR_ACCESS:
8096 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8097 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008098 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008099 case EXIT_REASON_MSR_READ:
8100 case EXIT_REASON_MSR_WRITE:
8101 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8102 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008103 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008104 case EXIT_REASON_MWAIT_INSTRUCTION:
8105 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008106 case EXIT_REASON_MONITOR_TRAP_FLAG:
8107 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008108 case EXIT_REASON_MONITOR_INSTRUCTION:
8109 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8110 case EXIT_REASON_PAUSE_INSTRUCTION:
8111 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8112 nested_cpu_has2(vmcs12,
8113 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8114 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008115 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008116 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008117 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008118 case EXIT_REASON_APIC_ACCESS:
8119 return nested_cpu_has2(vmcs12,
8120 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008121 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008122 case EXIT_REASON_EOI_INDUCED:
8123 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008124 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008125 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008126 /*
8127 * L0 always deals with the EPT violation. If nested EPT is
8128 * used, and the nested mmu code discovers that the address is
8129 * missing in the guest EPT table (EPT12), the EPT violation
8130 * will be injected with nested_ept_inject_page_fault()
8131 */
Joe Perches1d804d02015-03-30 16:46:09 -07008132 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008133 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008134 /*
8135 * L2 never uses directly L1's EPT, but rather L0's own EPT
8136 * table (shadow on EPT) or a merged EPT table that L0 built
8137 * (EPT on EPT). So any problems with the structure of the
8138 * table is L0's fault.
8139 */
Joe Perches1d804d02015-03-30 16:46:09 -07008140 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008141 case EXIT_REASON_WBINVD:
8142 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8143 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008144 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008145 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8146 /*
8147 * This should never happen, since it is not possible to
8148 * set XSS to a non-zero value---neither in L1 nor in L2.
8149 * If if it were, XSS would have to be checked against
8150 * the XSS exit bitmap in vmcs12.
8151 */
8152 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008153 case EXIT_REASON_PREEMPTION_TIMER:
8154 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008155 case EXIT_REASON_PML_FULL:
8156 /* We don't expose PML support to L1. */
8157 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008158 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008159 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008160 }
8161}
8162
Avi Kivity586f9602010-11-18 13:09:54 +02008163static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8164{
8165 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8166 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8167}
8168
Kai Huanga3eaa862015-11-04 13:46:05 +08008169static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008170{
Kai Huanga3eaa862015-11-04 13:46:05 +08008171 if (vmx->pml_pg) {
8172 __free_page(vmx->pml_pg);
8173 vmx->pml_pg = NULL;
8174 }
Kai Huang843e4332015-01-28 10:54:28 +08008175}
8176
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008177static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008178{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008179 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008180 u64 *pml_buf;
8181 u16 pml_idx;
8182
8183 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8184
8185 /* Do nothing if PML buffer is empty */
8186 if (pml_idx == (PML_ENTITY_NUM - 1))
8187 return;
8188
8189 /* PML index always points to next available PML buffer entity */
8190 if (pml_idx >= PML_ENTITY_NUM)
8191 pml_idx = 0;
8192 else
8193 pml_idx++;
8194
8195 pml_buf = page_address(vmx->pml_pg);
8196 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8197 u64 gpa;
8198
8199 gpa = pml_buf[pml_idx];
8200 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008201 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008202 }
8203
8204 /* reset PML index */
8205 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8206}
8207
8208/*
8209 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8210 * Called before reporting dirty_bitmap to userspace.
8211 */
8212static void kvm_flush_pml_buffers(struct kvm *kvm)
8213{
8214 int i;
8215 struct kvm_vcpu *vcpu;
8216 /*
8217 * We only need to kick vcpu out of guest mode here, as PML buffer
8218 * is flushed at beginning of all VMEXITs, and it's obvious that only
8219 * vcpus running in guest are possible to have unflushed GPAs in PML
8220 * buffer.
8221 */
8222 kvm_for_each_vcpu(i, vcpu, kvm)
8223 kvm_vcpu_kick(vcpu);
8224}
8225
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008226static void vmx_dump_sel(char *name, uint32_t sel)
8227{
8228 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008229 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008230 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8231 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8232 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8233}
8234
8235static void vmx_dump_dtsel(char *name, uint32_t limit)
8236{
8237 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8238 name, vmcs_read32(limit),
8239 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8240}
8241
8242static void dump_vmcs(void)
8243{
8244 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8245 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8246 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8247 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8248 u32 secondary_exec_control = 0;
8249 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008250 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008251 int i, n;
8252
8253 if (cpu_has_secondary_exec_ctrls())
8254 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8255
8256 pr_err("*** Guest State ***\n");
8257 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8258 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8259 vmcs_readl(CR0_GUEST_HOST_MASK));
8260 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8261 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8262 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8263 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8264 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8265 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008266 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8267 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8268 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8269 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008270 }
8271 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8272 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8273 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8274 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8275 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8276 vmcs_readl(GUEST_SYSENTER_ESP),
8277 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8278 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8279 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8280 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8281 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8282 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8283 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8284 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8285 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8286 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8287 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8288 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8289 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008290 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8291 efer, vmcs_read64(GUEST_IA32_PAT));
8292 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8293 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008294 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8295 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008296 pr_err("PerfGlobCtl = 0x%016llx\n",
8297 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008298 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008299 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008300 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8301 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8302 vmcs_read32(GUEST_ACTIVITY_STATE));
8303 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8304 pr_err("InterruptStatus = %04x\n",
8305 vmcs_read16(GUEST_INTR_STATUS));
8306
8307 pr_err("*** Host State ***\n");
8308 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8309 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8310 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8311 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8312 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8313 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8314 vmcs_read16(HOST_TR_SELECTOR));
8315 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8316 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8317 vmcs_readl(HOST_TR_BASE));
8318 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8319 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8320 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8321 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8322 vmcs_readl(HOST_CR4));
8323 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8324 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8325 vmcs_read32(HOST_IA32_SYSENTER_CS),
8326 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8327 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008328 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8329 vmcs_read64(HOST_IA32_EFER),
8330 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008331 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008332 pr_err("PerfGlobCtl = 0x%016llx\n",
8333 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008334
8335 pr_err("*** Control State ***\n");
8336 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8337 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8338 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8339 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8340 vmcs_read32(EXCEPTION_BITMAP),
8341 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8342 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8343 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8344 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8345 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8346 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8347 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8348 vmcs_read32(VM_EXIT_INTR_INFO),
8349 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8350 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8351 pr_err(" reason=%08x qualification=%016lx\n",
8352 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8353 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8354 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8355 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008356 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008357 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008358 pr_err("TSC Multiplier = 0x%016llx\n",
8359 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008360 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8361 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8362 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8363 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8364 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008365 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008366 n = vmcs_read32(CR3_TARGET_COUNT);
8367 for (i = 0; i + 1 < n; i += 4)
8368 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8369 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8370 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8371 if (i < n)
8372 pr_err("CR3 target%u=%016lx\n",
8373 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8374 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8375 pr_err("PLE Gap=%08x Window=%08x\n",
8376 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8377 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8378 pr_err("Virtual processor ID = 0x%04x\n",
8379 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8380}
8381
Avi Kivity6aa8b732006-12-10 02:21:36 -08008382/*
8383 * The guest has exited. See if we can fix it or if we need userspace
8384 * assistance.
8385 */
Avi Kivity851ba692009-08-24 11:10:17 +03008386static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008387{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008388 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008389 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008390 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008391
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008392 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8393
Kai Huang843e4332015-01-28 10:54:28 +08008394 /*
8395 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8396 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8397 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8398 * mode as if vcpus is in root mode, the PML buffer must has been
8399 * flushed already.
8400 */
8401 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008402 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008403
Mohammed Gamal80ced182009-09-01 12:48:18 +02008404 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008405 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008406 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008407
Nadav Har'El644d7112011-05-25 23:12:35 +03008408 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008409 nested_vmx_vmexit(vcpu, exit_reason,
8410 vmcs_read32(VM_EXIT_INTR_INFO),
8411 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008412 return 1;
8413 }
8414
Mohammed Gamal51207022010-05-31 22:40:54 +03008415 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008416 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008417 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8418 vcpu->run->fail_entry.hardware_entry_failure_reason
8419 = exit_reason;
8420 return 0;
8421 }
8422
Avi Kivity29bd8a72007-09-10 17:27:03 +03008423 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008424 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8425 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008426 = vmcs_read32(VM_INSTRUCTION_ERROR);
8427 return 0;
8428 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008429
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008430 /*
8431 * Note:
8432 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8433 * delivery event since it indicates guest is accessing MMIO.
8434 * The vm-exit can be triggered again after return to guest that
8435 * will cause infinite loop.
8436 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008437 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008438 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008439 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008440 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008441 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8442 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8443 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8444 vcpu->run->internal.ndata = 2;
8445 vcpu->run->internal.data[0] = vectoring_info;
8446 vcpu->run->internal.data[1] = exit_reason;
8447 return 0;
8448 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008449
Nadav Har'El644d7112011-05-25 23:12:35 +03008450 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8451 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008452 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008453 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008454 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008455 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008456 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008457 /*
8458 * This CPU don't support us in finding the end of an
8459 * NMI-blocked window if the guest runs with IRQs
8460 * disabled. So we pull the trigger after 1 s of
8461 * futile waiting, but inform the user about this.
8462 */
8463 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8464 "state on VCPU %d after 1 s timeout\n",
8465 __func__, vcpu->vcpu_id);
8466 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008467 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008468 }
8469
Avi Kivity6aa8b732006-12-10 02:21:36 -08008470 if (exit_reason < kvm_vmx_max_exit_handlers
8471 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008472 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008473 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008474 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8475 kvm_queue_exception(vcpu, UD_VECTOR);
8476 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008477 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008478}
8479
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008480static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008481{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008482 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8483
8484 if (is_guest_mode(vcpu) &&
8485 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8486 return;
8487
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008488 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008489 vmcs_write32(TPR_THRESHOLD, 0);
8490 return;
8491 }
8492
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008493 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008494}
8495
Yang Zhang8d146952013-01-25 10:18:50 +08008496static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8497{
8498 u32 sec_exec_control;
8499
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008500 /* Postpone execution until vmcs01 is the current VMCS. */
8501 if (is_guest_mode(vcpu)) {
8502 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8503 return;
8504 }
8505
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008506 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008507 return;
8508
Paolo Bonzini35754c92015-07-29 12:05:37 +02008509 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008510 return;
8511
8512 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8513
8514 if (set) {
8515 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8516 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8517 } else {
8518 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8519 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008520 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008521 }
8522 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8523
Paolo Bonzini6236b782018-01-16 16:51:18 +01008524 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008525}
8526
Tang Chen38b99172014-09-24 15:57:54 +08008527static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8528{
8529 struct vcpu_vmx *vmx = to_vmx(vcpu);
8530
8531 /*
8532 * Currently we do not handle the nested case where L2 has an
8533 * APIC access page of its own; that page is still pinned.
8534 * Hence, we skip the case where the VCPU is in guest mode _and_
8535 * L1 prepared an APIC access page for L2.
8536 *
8537 * For the case where L1 and L2 share the same APIC access page
8538 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8539 * in the vmcs12), this function will only update either the vmcs01
8540 * or the vmcs02. If the former, the vmcs02 will be updated by
8541 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8542 * the next L2->L1 exit.
8543 */
8544 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008545 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008546 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008547 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008548 vmx_flush_tlb_ept_only(vcpu);
8549 }
Tang Chen38b99172014-09-24 15:57:54 +08008550}
8551
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008552static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008553{
8554 u16 status;
8555 u8 old;
8556
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008557 if (max_isr == -1)
8558 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008559
8560 status = vmcs_read16(GUEST_INTR_STATUS);
8561 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008562 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008563 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008564 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008565 vmcs_write16(GUEST_INTR_STATUS, status);
8566 }
8567}
8568
8569static void vmx_set_rvi(int vector)
8570{
8571 u16 status;
8572 u8 old;
8573
Wei Wang4114c272014-11-05 10:53:43 +08008574 if (vector == -1)
8575 vector = 0;
8576
Yang Zhangc7c9c562013-01-25 10:18:51 +08008577 status = vmcs_read16(GUEST_INTR_STATUS);
8578 old = (u8)status & 0xff;
8579 if ((u8)vector != old) {
8580 status &= ~0xff;
8581 status |= (u8)vector;
8582 vmcs_write16(GUEST_INTR_STATUS, status);
8583 }
8584}
8585
8586static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8587{
Wanpeng Li963fee12014-07-17 19:03:00 +08008588 if (!is_guest_mode(vcpu)) {
8589 vmx_set_rvi(max_irr);
8590 return;
8591 }
8592
Wei Wang4114c272014-11-05 10:53:43 +08008593 if (max_irr == -1)
8594 return;
8595
Wanpeng Li963fee12014-07-17 19:03:00 +08008596 /*
Wei Wang4114c272014-11-05 10:53:43 +08008597 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8598 * handles it.
8599 */
8600 if (nested_exit_on_intr(vcpu))
8601 return;
8602
8603 /*
8604 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008605 * is run without virtual interrupt delivery.
8606 */
8607 if (!kvm_event_needs_reinjection(vcpu) &&
8608 vmx_interrupt_allowed(vcpu)) {
8609 kvm_queue_interrupt(vcpu, max_irr, false);
8610 vmx_inject_irq(vcpu);
8611 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008612}
8613
Andrey Smetanin63086302015-11-10 15:36:32 +03008614static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008615{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008616 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008617 return;
8618
Yang Zhangc7c9c562013-01-25 10:18:51 +08008619 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8620 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8621 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8622 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8623}
8624
Avi Kivity51aa01d2010-07-20 14:31:20 +03008625static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008626{
Avi Kivity00eba012011-03-07 17:24:54 +02008627 u32 exit_intr_info;
8628
8629 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8630 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8631 return;
8632
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008633 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008634 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008635
8636 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008637 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008638 kvm_machine_check();
8639
Gleb Natapov20f65982009-05-11 13:35:55 +03008640 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008641 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008642 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008643 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008644 kvm_after_handle_nmi(&vmx->vcpu);
8645 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008646}
Gleb Natapov20f65982009-05-11 13:35:55 +03008647
Yang Zhanga547c6d2013-04-11 19:25:10 +08008648static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8649{
8650 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8651
8652 /*
8653 * If external interrupt exists, IF bit is set in rflags/eflags on the
8654 * interrupt stack frame, and interrupt will be enabled on a return
8655 * from interrupt handler.
8656 */
8657 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8658 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8659 unsigned int vector;
8660 unsigned long entry;
8661 gate_desc *desc;
8662 struct vcpu_vmx *vmx = to_vmx(vcpu);
8663#ifdef CONFIG_X86_64
8664 unsigned long tmp;
8665#endif
8666
8667 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8668 desc = (gate_desc *)vmx->host_idt_base + vector;
8669 entry = gate_offset(*desc);
8670 asm volatile(
8671#ifdef CONFIG_X86_64
8672 "mov %%" _ASM_SP ", %[sp]\n\t"
8673 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8674 "push $%c[ss]\n\t"
8675 "push %[sp]\n\t"
8676#endif
8677 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008678 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008679 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008680 :
8681#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008682 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008683#endif
Josh Poimboeufd5ea93e2017-09-20 16:24:33 -05008684 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +08008685 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008686 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008687 [ss]"i"(__KERNEL_DS),
8688 [cs]"i"(__KERNEL_CS)
8689 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008690 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008691}
8692
Tom Lendackyb9655922018-05-10 22:06:39 +02008693static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008694{
Tom Lendackyb9655922018-05-10 22:06:39 +02008695 switch (index) {
8696 case MSR_IA32_SMBASE:
8697 /*
8698 * We cannot do SMM unless we can run the guest in big
8699 * real mode.
8700 */
8701 return enable_unrestricted_guest || emulate_invalid_guest_state;
8702 case MSR_AMD64_VIRT_SPEC_CTRL:
8703 /* This is AMD only. */
8704 return false;
8705 default:
8706 return true;
8707 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008708}
8709
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008710static bool vmx_mpx_supported(void)
8711{
8712 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8713 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8714}
8715
Wanpeng Li55412b22014-12-02 19:21:30 +08008716static bool vmx_xsaves_supported(void)
8717{
8718 return vmcs_config.cpu_based_2nd_exec_ctrl &
8719 SECONDARY_EXEC_XSAVES;
8720}
8721
Avi Kivity51aa01d2010-07-20 14:31:20 +03008722static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8723{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008724 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008725 bool unblock_nmi;
8726 u8 vector;
8727 bool idtv_info_valid;
8728
8729 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008730
Avi Kivitycf393f72008-07-01 16:20:21 +03008731 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008732 if (vmx->nmi_known_unmasked)
8733 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008734 /*
8735 * Can't use vmx->exit_intr_info since we're not sure what
8736 * the exit reason is.
8737 */
8738 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008739 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8740 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8741 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008742 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008743 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8744 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008745 * SDM 3: 23.2.2 (September 2008)
8746 * Bit 12 is undefined in any of the following cases:
8747 * If the VM exit sets the valid bit in the IDT-vectoring
8748 * information field.
8749 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008750 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008751 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8752 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008753 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8754 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008755 else
8756 vmx->nmi_known_unmasked =
8757 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8758 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008759 } else if (unlikely(vmx->soft_vnmi_blocked))
8760 vmx->vnmi_blocked_time +=
8761 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008762}
8763
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008764static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008765 u32 idt_vectoring_info,
8766 int instr_len_field,
8767 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008768{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008769 u8 vector;
8770 int type;
8771 bool idtv_info_valid;
8772
8773 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008774
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008775 vcpu->arch.nmi_injected = false;
8776 kvm_clear_exception_queue(vcpu);
8777 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008778
8779 if (!idtv_info_valid)
8780 return;
8781
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008782 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008783
Avi Kivity668f6122008-07-02 09:28:55 +03008784 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8785 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008786
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008787 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008788 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008789 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008790 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008791 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008792 * Clear bit "block by NMI" before VM entry if a NMI
8793 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008794 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008795 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008796 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008797 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008798 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008799 /* fall through */
8800 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008801 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008802 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008803 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008804 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008805 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008806 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008807 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008808 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008809 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008810 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008811 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008812 break;
8813 default:
8814 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008815 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008816}
8817
Avi Kivity83422e12010-07-20 14:43:23 +03008818static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8819{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008820 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008821 VM_EXIT_INSTRUCTION_LEN,
8822 IDT_VECTORING_ERROR_CODE);
8823}
8824
Avi Kivityb463a6f2010-07-20 15:06:17 +03008825static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8826{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008827 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008828 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8829 VM_ENTRY_INSTRUCTION_LEN,
8830 VM_ENTRY_EXCEPTION_ERROR_CODE);
8831
8832 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8833}
8834
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008835static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8836{
8837 int i, nr_msrs;
8838 struct perf_guest_switch_msr *msrs;
8839
8840 msrs = perf_guest_get_msrs(&nr_msrs);
8841
8842 if (!msrs)
8843 return;
8844
8845 for (i = 0; i < nr_msrs; i++)
8846 if (msrs[i].host == msrs[i].guest)
8847 clear_atomic_switch_msr(vmx, msrs[i].msr);
8848 else
8849 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8850 msrs[i].host);
8851}
8852
Yunhong Jiang64672c92016-06-13 14:19:59 -07008853void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8854{
8855 struct vcpu_vmx *vmx = to_vmx(vcpu);
8856 u64 tscl;
8857 u32 delta_tsc;
8858
8859 if (vmx->hv_deadline_tsc == -1)
8860 return;
8861
8862 tscl = rdtsc();
8863 if (vmx->hv_deadline_tsc > tscl)
8864 /* sure to be 32 bit only because checked on set_hv_timer */
8865 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8866 cpu_preemption_timer_multi);
8867 else
8868 delta_tsc = 0;
8869
8870 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8871}
8872
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008873static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008874{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008875 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008876 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008877
8878 /* Record the guest's net vcpu time for enforced NMI injections. */
8879 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8880 vmx->entry_time = ktime_get();
8881
8882 /* Don't enter VMX if guest state is invalid, let the exit handler
8883 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008884 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008885 return;
8886
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008887 if (vmx->ple_window_dirty) {
8888 vmx->ple_window_dirty = false;
8889 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8890 }
8891
Abel Gordon012f83c2013-04-18 14:39:25 +03008892 if (vmx->nested.sync_shadow_vmcs) {
8893 copy_vmcs12_to_shadow(vmx);
8894 vmx->nested.sync_shadow_vmcs = false;
8895 }
8896
Avi Kivity104f2262010-11-18 13:12:52 +02008897 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8898 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8899 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8900 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8901
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008902 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008903 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8904 vmcs_writel(HOST_CR4, cr4);
8905 vmx->host_state.vmcs_host_cr4 = cr4;
8906 }
8907
Avi Kivity104f2262010-11-18 13:12:52 +02008908 /* When single-stepping over STI and MOV SS, we must clear the
8909 * corresponding interruptibility bits in the guest state. Otherwise
8910 * vmentry fails as it then expects bit 14 (BS) in pending debug
8911 * exceptions being set, but that's not correct for the guest debugging
8912 * case. */
8913 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8914 vmx_set_interrupt_shadow(vcpu, 0);
8915
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008916 if (vmx->guest_pkru_valid)
8917 __write_pkru(vmx->guest_pkru);
8918
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008919 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008920 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008921
Yunhong Jiang64672c92016-06-13 14:19:59 -07008922 vmx_arm_hv_timer(vcpu);
8923
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01008924 /*
8925 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
8926 * it's non-zero. Since vmentry is serialising on affected CPUs, there
8927 * is no need to worry about the conditional branch over the wrmsr
8928 * being speculatively taken.
8929 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02008930 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01008931
Nadav Har'Eld462b812011-05-24 15:26:10 +03008932 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02008933
Avi Kivity104f2262010-11-18 13:12:52 +02008934 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008935 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008936 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8937 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8938 "push %%" _ASM_CX " \n\t"
8939 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008940 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008941 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008942 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008943 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008944 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008945 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8946 "mov %%cr2, %%" _ASM_DX " \n\t"
8947 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008948 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008949 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008950 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008951 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008952 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008953 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008954 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8955 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8956 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8957 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8958 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8959 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008960#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008961 "mov %c[r8](%0), %%r8 \n\t"
8962 "mov %c[r9](%0), %%r9 \n\t"
8963 "mov %c[r10](%0), %%r10 \n\t"
8964 "mov %c[r11](%0), %%r11 \n\t"
8965 "mov %c[r12](%0), %%r12 \n\t"
8966 "mov %c[r13](%0), %%r13 \n\t"
8967 "mov %c[r14](%0), %%r14 \n\t"
8968 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008969#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008970 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008971
Avi Kivity6aa8b732006-12-10 02:21:36 -08008972 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008973 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008974 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008975 "jmp 2f \n\t"
8976 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8977 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008978 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008979 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008980 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08008981 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008982 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8983 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8984 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8985 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8986 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8987 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8988 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008989#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008990 "mov %%r8, %c[r8](%0) \n\t"
8991 "mov %%r9, %c[r9](%0) \n\t"
8992 "mov %%r10, %c[r10](%0) \n\t"
8993 "mov %%r11, %c[r11](%0) \n\t"
8994 "mov %%r12, %c[r12](%0) \n\t"
8995 "mov %%r13, %c[r13](%0) \n\t"
8996 "mov %%r14, %c[r14](%0) \n\t"
8997 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08008998 "xor %%r8d, %%r8d \n\t"
8999 "xor %%r9d, %%r9d \n\t"
9000 "xor %%r10d, %%r10d \n\t"
9001 "xor %%r11d, %%r11d \n\t"
9002 "xor %%r12d, %%r12d \n\t"
9003 "xor %%r13d, %%r13d \n\t"
9004 "xor %%r14d, %%r14d \n\t"
9005 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009006#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009007 "mov %%cr2, %%" _ASM_AX " \n\t"
9008 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009009
Jim Mattson491c0ca2018-01-03 14:31:38 -08009010 "xor %%eax, %%eax \n\t"
9011 "xor %%ebx, %%ebx \n\t"
9012 "xor %%esi, %%esi \n\t"
9013 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009014 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009015 ".pushsection .rodata \n\t"
9016 ".global vmx_return \n\t"
9017 "vmx_return: " _ASM_PTR " 2b \n\t"
9018 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009019 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009020 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009021 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009022 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009023 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9024 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9025 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9026 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9027 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9028 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9029 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009030#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009031 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9032 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9033 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9034 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9035 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9036 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9037 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9038 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009039#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009040 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9041 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009042 : "cc", "memory"
9043#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009044 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009045 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009046#else
9047 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009048#endif
9049 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009050
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009051 /*
9052 * We do not use IBRS in the kernel. If this vCPU has used the
9053 * SPEC_CTRL MSR it may have left it on; save the value and
9054 * turn it off. This is much more efficient than blindly adding
9055 * it to the atomic save/restore list. Especially as the former
9056 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9057 *
9058 * For non-nested case:
9059 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9060 * save it.
9061 *
9062 * For nested case:
9063 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9064 * save it.
9065 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009066 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009067 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009068
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009069 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009070
David Woodhousec1ddd992018-01-12 11:11:27 +00009071 /* Eliminate branch target predictions from guest mode */
9072 vmexit_fill_RSB();
9073
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009074 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9075 if (debugctlmsr)
9076 update_debugctlmsr(debugctlmsr);
9077
Avi Kivityaa67f602012-08-01 16:48:03 +03009078#ifndef CONFIG_X86_64
9079 /*
9080 * The sysexit path does not restore ds/es, so we must set them to
9081 * a reasonable value ourselves.
9082 *
9083 * We can't defer this to vmx_load_host_state() since that function
9084 * may be executed in interrupt context, which saves and restore segments
9085 * around it, nullifying its effect.
9086 */
9087 loadsegment(ds, __USER_DS);
9088 loadsegment(es, __USER_DS);
9089#endif
9090
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009091 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009092 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009093 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009094 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009095 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009096 vcpu->arch.regs_dirty = 0;
9097
Avi Kivity1155f762007-11-22 11:30:47 +02009098 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9099
Nadav Har'Eld462b812011-05-24 15:26:10 +03009100 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009101
Avi Kivity51aa01d2010-07-20 14:31:20 +03009102 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009103
Gleb Natapove0b890d2013-09-25 12:51:33 +03009104 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009105 * eager fpu is enabled if PKEY is supported and CR4 is switched
9106 * back on host, so it is safe to read guest PKRU from current
9107 * XSAVE.
9108 */
9109 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9110 vmx->guest_pkru = __read_pkru();
9111 if (vmx->guest_pkru != vmx->host_pkru) {
9112 vmx->guest_pkru_valid = true;
9113 __write_pkru(vmx->host_pkru);
9114 } else
9115 vmx->guest_pkru_valid = false;
9116 }
9117
9118 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009119 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9120 * we did not inject a still-pending event to L1 now because of
9121 * nested_run_pending, we need to re-enable this bit.
9122 */
9123 if (vmx->nested.nested_run_pending)
9124 kvm_make_request(KVM_REQ_EVENT, vcpu);
9125
9126 vmx->nested.nested_run_pending = 0;
9127
Avi Kivity51aa01d2010-07-20 14:31:20 +03009128 vmx_complete_atomic_exit(vmx);
9129 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009130 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009131}
9132
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009133static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9134{
9135 struct vcpu_vmx *vmx = to_vmx(vcpu);
9136 int cpu;
9137
9138 if (vmx->loaded_vmcs == &vmx->vmcs01)
9139 return;
9140
9141 cpu = get_cpu();
9142 vmx->loaded_vmcs = &vmx->vmcs01;
9143 vmx_vcpu_put(vcpu);
9144 vmx_vcpu_load(vcpu, cpu);
9145 vcpu->cpu = cpu;
9146 put_cpu();
9147}
9148
Jim Mattson2f1fe812016-07-08 15:36:06 -07009149/*
9150 * Ensure that the current vmcs of the logical processor is the
9151 * vmcs01 of the vcpu before calling free_nested().
9152 */
9153static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9154{
9155 struct vcpu_vmx *vmx = to_vmx(vcpu);
9156 int r;
9157
9158 r = vcpu_load(vcpu);
9159 BUG_ON(r);
9160 vmx_load_vmcs01(vcpu);
9161 free_nested(vmx);
9162 vcpu_put(vcpu);
9163}
9164
Avi Kivity6aa8b732006-12-10 02:21:36 -08009165static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9166{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009167 struct vcpu_vmx *vmx = to_vmx(vcpu);
9168
Kai Huang843e4332015-01-28 10:54:28 +08009169 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009170 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009171 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009172 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009173 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009174 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009175 kfree(vmx->guest_msrs);
9176 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009177 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009178}
9179
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009180static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009181{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009182 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009183 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009184 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009185 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009186
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009187 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009188 return ERR_PTR(-ENOMEM);
9189
Wanpeng Li991e7a02015-09-16 17:30:05 +08009190 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009191
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009192 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9193 if (err)
9194 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009195
Peter Feiner4e595162016-07-07 14:49:58 -07009196 err = -ENOMEM;
9197
9198 /*
9199 * If PML is turned on, failure on enabling PML just results in failure
9200 * of creating the vcpu, therefore we can simplify PML logic (by
9201 * avoiding dealing with cases, such as enabling PML partially on vcpus
9202 * for the guest, etc.
9203 */
9204 if (enable_pml) {
9205 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9206 if (!vmx->pml_pg)
9207 goto uninit_vcpu;
9208 }
9209
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009210 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009211 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9212 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009213
Peter Feiner4e595162016-07-07 14:49:58 -07009214 if (!vmx->guest_msrs)
9215 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009216
Nadav Har'Eld462b812011-05-24 15:26:10 +03009217 if (!vmm_exclusive)
9218 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009219 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009220 if (!vmm_exclusive)
9221 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009222 if (err < 0)
9223 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009224
Paolo Bonzini6236b782018-01-16 16:51:18 +01009225 msr_bitmap = vmx->vmcs01.msr_bitmap;
9226 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9227 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9228 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9229 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9230 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9231 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9232 vmx->msr_bitmap_mode = 0;
9233
Paolo Bonziniff546f92018-01-11 12:16:15 +01009234 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009235 cpu = get_cpu();
9236 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009237 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009238 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009239 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009240 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009241 if (err)
9242 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009243 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009244 err = alloc_apic_access_page(kvm);
9245 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009246 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009247 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009248
Sheng Yangb927a3c2009-07-21 10:42:48 +08009249 if (enable_ept) {
9250 if (!kvm->arch.ept_identity_map_addr)
9251 kvm->arch.ept_identity_map_addr =
9252 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009253 err = init_rmode_identity_map(kvm);
9254 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009255 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009256 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009257
Wanpeng Li5c614b32015-10-13 09:18:36 -07009258 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009259 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009260 vmx->nested.vpid02 = allocate_vpid();
9261 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009262
Wincy Van705699a2015-02-03 23:58:17 +08009263 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009264 vmx->nested.current_vmptr = -1ull;
9265 vmx->nested.current_vmcs12 = NULL;
9266
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009267 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9268
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009269 /*
9270 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9271 * or POSTED_INTR_WAKEUP_VECTOR.
9272 */
9273 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9274 vmx->pi_desc.sn = 1;
9275
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009276 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009277
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009278free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009279 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009280 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009281free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009282 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009283free_pml:
9284 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009285uninit_vcpu:
9286 kvm_vcpu_uninit(&vmx->vcpu);
9287free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009288 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009289 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009290 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009291}
9292
Yang, Sheng002c7f72007-07-31 14:23:01 +03009293static void __init vmx_check_processor_compat(void *rtn)
9294{
9295 struct vmcs_config vmcs_conf;
9296
9297 *(int *)rtn = 0;
9298 if (setup_vmcs_config(&vmcs_conf) < 0)
9299 *(int *)rtn = -EIO;
9300 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9301 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9302 smp_processor_id());
9303 *(int *)rtn = -EIO;
9304 }
9305}
9306
Sheng Yang67253af2008-04-25 10:20:22 +08009307static int get_ept_level(void)
9308{
9309 return VMX_EPT_DEFAULT_GAW + 1;
9310}
9311
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009312static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009313{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009314 u8 cache;
9315 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009316
Sheng Yang522c68c2009-04-27 20:35:43 +08009317 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009318 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009319 * 2. EPT with VT-d:
9320 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009321 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009322 * b. VT-d with snooping control feature: snooping control feature of
9323 * VT-d engine can guarantee the cache correctness. Just set it
9324 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009325 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009326 * consistent with host MTRR
9327 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009328 if (is_mmio) {
9329 cache = MTRR_TYPE_UNCACHABLE;
9330 goto exit;
9331 }
9332
9333 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009334 ipat = VMX_EPT_IPAT_BIT;
9335 cache = MTRR_TYPE_WRBACK;
9336 goto exit;
9337 }
9338
9339 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9340 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009341 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009342 cache = MTRR_TYPE_WRBACK;
9343 else
9344 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009345 goto exit;
9346 }
9347
Xiao Guangrongff536042015-06-15 16:55:22 +08009348 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009349
9350exit:
9351 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009352}
9353
Sheng Yang17cc3932010-01-05 19:02:27 +08009354static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009355{
Sheng Yang878403b2010-01-05 19:02:29 +08009356 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9357 return PT_DIRECTORY_LEVEL;
9358 else
9359 /* For shadow and EPT supported 1GB page */
9360 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009361}
9362
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009363static void vmcs_set_secondary_exec_control(u32 new_ctl)
9364{
9365 /*
9366 * These bits in the secondary execution controls field
9367 * are dynamic, the others are mostly based on the hypervisor
9368 * architecture and the guest's CPUID. Do not touch the
9369 * dynamic bits.
9370 */
9371 u32 mask =
9372 SECONDARY_EXEC_SHADOW_VMCS |
9373 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9374 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9375
9376 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9377
9378 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9379 (new_ctl & ~mask) | (cur_ctl & mask));
9380}
9381
Sheng Yang0e851882009-12-18 16:48:46 +08009382static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9383{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009384 struct kvm_cpuid_entry2 *best;
9385 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009386 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009387
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009388 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009389 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9390 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009391 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009392
Paolo Bonzini8b972652015-09-15 17:34:42 +02009393 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009394 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009395 vmx->nested.nested_vmx_secondary_ctls_high |=
9396 SECONDARY_EXEC_RDTSCP;
9397 else
9398 vmx->nested.nested_vmx_secondary_ctls_high &=
9399 ~SECONDARY_EXEC_RDTSCP;
9400 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009401 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009402
Mao, Junjiead756a12012-07-02 01:18:48 +00009403 /* Exposing INVPCID only when PCID is exposed */
9404 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9405 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009406 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9407 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009408 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009409
Mao, Junjiead756a12012-07-02 01:18:48 +00009410 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009411 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009412 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009413
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009414 if (cpu_has_secondary_exec_ctrls())
9415 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009416
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009417 if (nested_vmx_allowed(vcpu))
9418 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9419 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9420 else
9421 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9422 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009423}
9424
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009425static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9426{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009427 if (func == 1 && nested)
9428 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009429}
9430
Yang Zhang25d92082013-08-06 12:00:32 +03009431static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9432 struct x86_exception *fault)
9433{
Jan Kiszka533558b2014-01-04 18:47:20 +01009434 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9435 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009436
9437 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009438 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009439 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009440 exit_reason = EXIT_REASON_EPT_VIOLATION;
9441 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009442 vmcs12->guest_physical_address = fault->address;
9443}
9444
Nadav Har'El155a97a2013-08-05 11:07:16 +03009445/* Callbacks for nested_ept_init_mmu_context: */
9446
9447static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9448{
9449 /* return the page table to be shadowed - in our case, EPT12 */
9450 return get_vmcs12(vcpu)->ept_pointer;
9451}
9452
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009453static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009454{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009455 WARN_ON(mmu_is_nested(vcpu));
9456 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009457 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9458 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009459 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9460 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9461 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9462
9463 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009464}
9465
9466static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9467{
9468 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9469}
9470
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009471static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9472 u16 error_code)
9473{
9474 bool inequality, bit;
9475
9476 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9477 inequality =
9478 (error_code & vmcs12->page_fault_error_code_mask) !=
9479 vmcs12->page_fault_error_code_match;
9480 return inequality ^ bit;
9481}
9482
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009483static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9484 struct x86_exception *fault)
9485{
9486 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9487
9488 WARN_ON(!is_guest_mode(vcpu));
9489
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009490 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009491 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9492 vmcs_read32(VM_EXIT_INTR_INFO),
9493 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009494 else
9495 kvm_inject_page_fault(vcpu, fault);
9496}
9497
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009498static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9499 struct vmcs12 *vmcs12)
9500{
9501 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009502 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009503
9504 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009505 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9506 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009507 return false;
9508
9509 /*
9510 * Translate L1 physical address to host physical
9511 * address for vmcs02. Keep the page pinned, so this
9512 * physical address remains valid. We keep a reference
9513 * to it so we can release it later.
9514 */
9515 if (vmx->nested.apic_access_page) /* shouldn't happen */
9516 nested_release_page(vmx->nested.apic_access_page);
9517 vmx->nested.apic_access_page =
9518 nested_get_page(vcpu, vmcs12->apic_access_addr);
9519 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009520
9521 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009522 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9523 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009524 return false;
9525
9526 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9527 nested_release_page(vmx->nested.virtual_apic_page);
9528 vmx->nested.virtual_apic_page =
9529 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9530
9531 /*
9532 * Failing the vm entry is _not_ what the processor does
9533 * but it's basically the only possibility we have.
9534 * We could still enter the guest if CR8 load exits are
9535 * enabled, CR8 store exits are enabled, and virtualize APIC
9536 * access is disabled; in this case the processor would never
9537 * use the TPR shadow and we could simply clear the bit from
9538 * the execution control. But such a configuration is useless,
9539 * so let's keep the code simple.
9540 */
9541 if (!vmx->nested.virtual_apic_page)
9542 return false;
9543 }
9544
Wincy Van705699a2015-02-03 23:58:17 +08009545 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009546 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9547 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009548 return false;
9549
9550 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9551 kunmap(vmx->nested.pi_desc_page);
9552 nested_release_page(vmx->nested.pi_desc_page);
9553 }
9554 vmx->nested.pi_desc_page =
9555 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9556 if (!vmx->nested.pi_desc_page)
9557 return false;
9558
9559 vmx->nested.pi_desc =
9560 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9561 if (!vmx->nested.pi_desc) {
9562 nested_release_page_clean(vmx->nested.pi_desc_page);
9563 return false;
9564 }
9565 vmx->nested.pi_desc =
9566 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9567 (unsigned long)(vmcs12->posted_intr_desc_addr &
9568 (PAGE_SIZE - 1)));
9569 }
9570
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009571 return true;
9572}
9573
Jan Kiszkaf4124502014-03-07 20:03:13 +01009574static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9575{
9576 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9577 struct vcpu_vmx *vmx = to_vmx(vcpu);
9578
9579 if (vcpu->arch.virtual_tsc_khz == 0)
9580 return;
9581
9582 /* Make sure short timeouts reliably trigger an immediate vmexit.
9583 * hrtimer_start does not guarantee this. */
9584 if (preemption_timeout <= 1) {
9585 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9586 return;
9587 }
9588
9589 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9590 preemption_timeout *= 1000000;
9591 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9592 hrtimer_start(&vmx->nested.preemption_timer,
9593 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9594}
9595
Wincy Van3af18d92015-02-03 23:49:31 +08009596static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9597 struct vmcs12 *vmcs12)
9598{
9599 int maxphyaddr;
9600 u64 addr;
9601
9602 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9603 return 0;
9604
9605 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9606 WARN_ON(1);
9607 return -EINVAL;
9608 }
9609 maxphyaddr = cpuid_maxphyaddr(vcpu);
9610
9611 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9612 ((addr + PAGE_SIZE) >> maxphyaddr))
9613 return -EINVAL;
9614
9615 return 0;
9616}
9617
9618/*
9619 * Merge L0's and L1's MSR bitmap, return false to indicate that
9620 * we do not use the hardware.
9621 */
9622static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9623 struct vmcs12 *vmcs12)
9624{
Wincy Van82f0dd42015-02-03 23:57:18 +08009625 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009626 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009627 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009628 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009629 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009630 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009631 *
9632 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9633 * ensures that we do not accidentally generate an L02 MSR bitmap
9634 * from the L12 MSR bitmap that is too permissive.
9635 * 2. That L1 or L2s have actually used the MSR. This avoids
9636 * unnecessarily merging of the bitmap if the MSR is unused. This
9637 * works properly because we only update the L01 MSR bitmap lazily.
9638 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9639 * updated to reflect this when L1 (or its L2s) actually write to
9640 * the MSR.
9641 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009642 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9643 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009644
Ashok Raj70131292018-02-01 22:59:43 +01009645 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009646 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009647 return false;
9648
9649 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009650 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009651 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009652 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009653
Radim Krčmářd048c092016-08-08 20:16:22 +02009654 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9655
Wincy Vanf2b93282015-02-03 23:56:03 +08009656 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009657 if (nested_cpu_has_apic_reg_virt(vmcs12))
9658 for (msr = 0x800; msr <= 0x8ff; msr++)
9659 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009660 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009661 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009662
9663 nested_vmx_disable_intercept_for_msr(
9664 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009665 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9666 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009667
Wincy Van608406e2015-02-03 23:57:51 +08009668 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009669 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009670 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009671 APIC_BASE_MSR + (APIC_EOI >> 4),
9672 MSR_TYPE_W);
9673 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009674 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009675 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9676 MSR_TYPE_W);
9677 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009678 }
Ashok Raj70131292018-02-01 22:59:43 +01009679
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009680 if (spec_ctrl)
9681 nested_vmx_disable_intercept_for_msr(
9682 msr_bitmap_l1, msr_bitmap_l0,
9683 MSR_IA32_SPEC_CTRL,
9684 MSR_TYPE_R | MSR_TYPE_W);
9685
Ashok Raj70131292018-02-01 22:59:43 +01009686 if (pred_cmd)
9687 nested_vmx_disable_intercept_for_msr(
9688 msr_bitmap_l1, msr_bitmap_l0,
9689 MSR_IA32_PRED_CMD,
9690 MSR_TYPE_W);
9691
Wincy Vanf2b93282015-02-03 23:56:03 +08009692 kunmap(page);
9693 nested_release_page_clean(page);
9694
9695 return true;
9696}
9697
9698static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9699 struct vmcs12 *vmcs12)
9700{
Wincy Van82f0dd42015-02-03 23:57:18 +08009701 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009702 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009703 !nested_cpu_has_vid(vmcs12) &&
9704 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009705 return 0;
9706
9707 /*
9708 * If virtualize x2apic mode is enabled,
9709 * virtualize apic access must be disabled.
9710 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009711 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9712 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009713 return -EINVAL;
9714
Wincy Van608406e2015-02-03 23:57:51 +08009715 /*
9716 * If virtual interrupt delivery is enabled,
9717 * we must exit on external interrupts.
9718 */
9719 if (nested_cpu_has_vid(vmcs12) &&
9720 !nested_exit_on_intr(vcpu))
9721 return -EINVAL;
9722
Wincy Van705699a2015-02-03 23:58:17 +08009723 /*
9724 * bits 15:8 should be zero in posted_intr_nv,
9725 * the descriptor address has been already checked
9726 * in nested_get_vmcs12_pages.
9727 */
9728 if (nested_cpu_has_posted_intr(vmcs12) &&
9729 (!nested_cpu_has_vid(vmcs12) ||
9730 !nested_exit_intr_ack_set(vcpu) ||
9731 vmcs12->posted_intr_nv & 0xff00))
9732 return -EINVAL;
9733
Wincy Vanf2b93282015-02-03 23:56:03 +08009734 /* tpr shadow is needed by all apicv features. */
9735 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9736 return -EINVAL;
9737
9738 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009739}
9740
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009741static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9742 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009743 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009744{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009745 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009746 u64 count, addr;
9747
9748 if (vmcs12_read_any(vcpu, count_field, &count) ||
9749 vmcs12_read_any(vcpu, addr_field, &addr)) {
9750 WARN_ON(1);
9751 return -EINVAL;
9752 }
9753 if (count == 0)
9754 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009755 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009756 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9757 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009758 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009759 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9760 addr_field, maxphyaddr, count, addr);
9761 return -EINVAL;
9762 }
9763 return 0;
9764}
9765
9766static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9767 struct vmcs12 *vmcs12)
9768{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009769 if (vmcs12->vm_exit_msr_load_count == 0 &&
9770 vmcs12->vm_exit_msr_store_count == 0 &&
9771 vmcs12->vm_entry_msr_load_count == 0)
9772 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009773 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009774 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009775 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009776 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009777 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009778 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009779 return -EINVAL;
9780 return 0;
9781}
9782
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009783static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9784 struct vmx_msr_entry *e)
9785{
9786 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009787 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009788 return -EINVAL;
9789 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9790 e->index == MSR_IA32_UCODE_REV)
9791 return -EINVAL;
9792 if (e->reserved != 0)
9793 return -EINVAL;
9794 return 0;
9795}
9796
9797static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9798 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009799{
9800 if (e->index == MSR_FS_BASE ||
9801 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009802 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9803 nested_vmx_msr_check_common(vcpu, e))
9804 return -EINVAL;
9805 return 0;
9806}
9807
9808static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9809 struct vmx_msr_entry *e)
9810{
9811 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9812 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009813 return -EINVAL;
9814 return 0;
9815}
9816
9817/*
9818 * Load guest's/host's msr at nested entry/exit.
9819 * return 0 for success, entry index for failure.
9820 */
9821static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9822{
9823 u32 i;
9824 struct vmx_msr_entry e;
9825 struct msr_data msr;
9826
9827 msr.host_initiated = false;
9828 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009829 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9830 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009831 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009832 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9833 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009834 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009835 }
9836 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009837 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009838 "%s check failed (%u, 0x%x, 0x%x)\n",
9839 __func__, i, e.index, e.reserved);
9840 goto fail;
9841 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009842 msr.index = e.index;
9843 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009844 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009845 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009846 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9847 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009848 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009849 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009850 }
9851 return 0;
9852fail:
9853 return i + 1;
9854}
9855
9856static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9857{
9858 u32 i;
9859 struct vmx_msr_entry e;
9860
9861 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009862 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009863 if (kvm_vcpu_read_guest(vcpu,
9864 gpa + i * sizeof(e),
9865 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009866 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009867 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9868 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009869 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009870 }
9871 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009872 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009873 "%s check failed (%u, 0x%x, 0x%x)\n",
9874 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009875 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009876 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009877 msr_info.host_initiated = false;
9878 msr_info.index = e.index;
9879 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009880 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009881 "%s cannot read MSR (%u, 0x%x)\n",
9882 __func__, i, e.index);
9883 return -EINVAL;
9884 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009885 if (kvm_vcpu_write_guest(vcpu,
9886 gpa + i * sizeof(e) +
9887 offsetof(struct vmx_msr_entry, value),
9888 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009889 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009890 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009891 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009892 return -EINVAL;
9893 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009894 }
9895 return 0;
9896}
9897
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009898/*
9899 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9900 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009901 * 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 +03009902 * guest in a way that will both be appropriate to L1's requests, and our
9903 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9904 * function also has additional necessary side-effects, like setting various
9905 * vcpu->arch fields.
9906 */
9907static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9908{
9909 struct vcpu_vmx *vmx = to_vmx(vcpu);
9910 u32 exec_control;
9911
9912 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9913 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9914 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9915 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9916 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9917 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9918 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9919 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9920 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9921 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9922 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9923 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9924 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9925 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9926 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9927 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9928 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9929 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9930 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9931 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9932 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9933 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9934 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9935 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9936 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9937 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9938 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9939 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9940 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9941 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9942 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9943 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9944 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9945 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9946 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9947 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9948
Jan Kiszka2996fca2014-06-16 13:59:43 +02009949 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9950 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9951 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9952 } else {
9953 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9954 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9955 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009956 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9957 vmcs12->vm_entry_intr_info_field);
9958 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9959 vmcs12->vm_entry_exception_error_code);
9960 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9961 vmcs12->vm_entry_instruction_len);
9962 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9963 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009964 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009965 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009966 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9967 vmcs12->guest_pending_dbg_exceptions);
9968 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9969 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9970
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009971 if (nested_cpu_has_xsaves(vmcs12))
9972 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009973 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9974
Jan Kiszkaf4124502014-03-07 20:03:13 +01009975 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009976
Paolo Bonzini93140062016-07-06 13:23:51 +02009977 /* Preemption timer setting is only taken from vmcs01. */
9978 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9979 exec_control |= vmcs_config.pin_based_exec_ctrl;
9980 if (vmx->hv_deadline_tsc == -1)
9981 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9982
9983 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +08009984 if (nested_cpu_has_posted_intr(vmcs12)) {
9985 /*
9986 * Note that we use L0's vector here and in
9987 * vmx_deliver_nested_posted_interrupt.
9988 */
9989 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9990 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +08009991 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +08009992 vmcs_write64(POSTED_INTR_DESC_ADDR,
9993 page_to_phys(vmx->nested.pi_desc_page) +
9994 (unsigned long)(vmcs12->posted_intr_desc_addr &
9995 (PAGE_SIZE - 1)));
9996 } else
9997 exec_control &= ~PIN_BASED_POSTED_INTR;
9998
Jan Kiszkaf4124502014-03-07 20:03:13 +01009999 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010000
Jan Kiszkaf4124502014-03-07 20:03:13 +010010001 vmx->nested.preemption_timer_expired = false;
10002 if (nested_cpu_has_preemption_timer(vmcs12))
10003 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010004
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010005 /*
10006 * Whether page-faults are trapped is determined by a combination of
10007 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10008 * If enable_ept, L0 doesn't care about page faults and we should
10009 * set all of these to L1's desires. However, if !enable_ept, L0 does
10010 * care about (at least some) page faults, and because it is not easy
10011 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10012 * to exit on each and every L2 page fault. This is done by setting
10013 * MASK=MATCH=0 and (see below) EB.PF=1.
10014 * Note that below we don't need special code to set EB.PF beyond the
10015 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10016 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10017 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10018 *
10019 * A problem with this approach (when !enable_ept) is that L1 may be
10020 * injected with more page faults than it asked for. This could have
10021 * caused problems, but in practice existing hypervisors don't care.
10022 * To fix this, we will need to emulate the PFEC checking (on the L1
10023 * page tables), using walk_addr(), when injecting PFs to L1.
10024 */
10025 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10026 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10027 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10028 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10029
10030 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010031 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010032
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010033 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010034 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010035 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010036 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010037 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010038 if (nested_cpu_has(vmcs12,
10039 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10040 exec_control |= vmcs12->secondary_vm_exec_control;
10041
10042 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10043 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010044 * If translation failed, no matter: This feature asks
10045 * to exit when accessing the given address, and if it
10046 * can never be accessed, this feature won't do
10047 * anything anyway.
10048 */
10049 if (!vmx->nested.apic_access_page)
10050 exec_control &=
10051 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10052 else
10053 vmcs_write64(APIC_ACCESS_ADDR,
10054 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010055 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010056 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010057 exec_control |=
10058 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010059 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010060 }
10061
Wincy Van608406e2015-02-03 23:57:51 +080010062 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10063 vmcs_write64(EOI_EXIT_BITMAP0,
10064 vmcs12->eoi_exit_bitmap0);
10065 vmcs_write64(EOI_EXIT_BITMAP1,
10066 vmcs12->eoi_exit_bitmap1);
10067 vmcs_write64(EOI_EXIT_BITMAP2,
10068 vmcs12->eoi_exit_bitmap2);
10069 vmcs_write64(EOI_EXIT_BITMAP3,
10070 vmcs12->eoi_exit_bitmap3);
10071 vmcs_write16(GUEST_INTR_STATUS,
10072 vmcs12->guest_intr_status);
10073 }
10074
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010075 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10076 }
10077
10078
10079 /*
10080 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10081 * Some constant fields are set here by vmx_set_constant_host_state().
10082 * Other fields are different per CPU, and will be set later when
10083 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10084 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010085 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010086
10087 /*
10088 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10089 * entry, but only if the current (host) sp changed from the value
10090 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10091 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10092 * here we just force the write to happen on entry.
10093 */
10094 vmx->host_rsp = 0;
10095
10096 exec_control = vmx_exec_control(vmx); /* L0's desires */
10097 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10098 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10099 exec_control &= ~CPU_BASED_TPR_SHADOW;
10100 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010101
10102 if (exec_control & CPU_BASED_TPR_SHADOW) {
10103 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10104 page_to_phys(vmx->nested.virtual_apic_page));
10105 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010106 } else {
10107#ifdef CONFIG_X86_64
10108 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10109 CPU_BASED_CR8_STORE_EXITING;
10110#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010111 }
10112
Wincy Van3af18d92015-02-03 23:49:31 +080010113 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010114 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10115 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10116 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10117 else
Wincy Van3af18d92015-02-03 23:49:31 +080010118 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10119
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010120 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010121 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010122 * Rather, exit every time.
10123 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010124 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10125 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10126
10127 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10128
10129 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10130 * bitwise-or of what L1 wants to trap for L2, and what we want to
10131 * trap. Note that CR0.TS also needs updating - we do this later.
10132 */
10133 update_exception_bitmap(vcpu);
10134 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10135 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10136
Nadav Har'El8049d652013-08-05 11:07:06 +030010137 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10138 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10139 * bits are further modified by vmx_set_efer() below.
10140 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010141 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010142
10143 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10144 * emulated by vmx_set_efer(), below.
10145 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010146 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010147 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10148 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010149 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10150
Jan Kiszka44811c02013-08-04 17:17:27 +020010151 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010152 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010153 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10154 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010155 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10156
10157
10158 set_cr4_guest_host_mask(vmx);
10159
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010160 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10161 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10162
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010163 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10164 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010165 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010166 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010167 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010168 if (kvm_has_tsc_control)
10169 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010170
Paolo Bonzini6236b782018-01-16 16:51:18 +010010171 if (cpu_has_vmx_msr_bitmap())
10172 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10173
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010174 if (enable_vpid) {
10175 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010176 * There is no direct mapping between vpid02 and vpid12, the
10177 * vpid02 is per-vCPU for L0 and reused while the value of
10178 * vpid12 is changed w/ one invvpid during nested vmentry.
10179 * The vpid12 is allocated by L1 for L2, so it will not
10180 * influence global bitmap(for vpid01 and vpid02 allocation)
10181 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010182 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010183 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10184 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10185 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10186 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10187 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10188 }
10189 } else {
10190 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10191 vmx_flush_tlb(vcpu);
10192 }
10193
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010194 }
10195
Ladi Prosek560a9792017-04-04 14:18:53 +020010196 if (enable_pml) {
10197 /*
10198 * Conceptually we want to copy the PML address and index from
10199 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10200 * since we always flush the log on each vmexit, this happens
10201 * to be equivalent to simply resetting the fields in vmcs02.
10202 */
10203 ASSERT(vmx->pml_pg);
10204 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10205 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10206 }
10207
Nadav Har'El155a97a2013-08-05 11:07:16 +030010208 if (nested_cpu_has_ept(vmcs12)) {
10209 kvm_mmu_unload(vcpu);
10210 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010211 } else if (nested_cpu_has2(vmcs12,
10212 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10213 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010214 }
10215
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010216 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10217 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010218 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010219 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10220 else
10221 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10222 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10223 vmx_set_efer(vcpu, vcpu->arch.efer);
10224
10225 /*
10226 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10227 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10228 * The CR0_READ_SHADOW is what L2 should have expected to read given
10229 * the specifications by L1; It's not enough to take
10230 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10231 * have more bits than L1 expected.
10232 */
10233 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10234 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10235
10236 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10237 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10238
10239 /* shadow page tables on either EPT or shadow page tables */
10240 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10241 kvm_mmu_reset_context(vcpu);
10242
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010243 if (!enable_ept)
10244 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10245
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010246 /*
10247 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10248 */
10249 if (enable_ept) {
10250 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10251 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10252 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10253 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10254 }
10255
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010256 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10257 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10258}
10259
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010260/*
10261 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10262 * for running an L2 nested guest.
10263 */
10264static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10265{
10266 struct vmcs12 *vmcs12;
10267 struct vcpu_vmx *vmx = to_vmx(vcpu);
10268 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010269 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010270 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010271
10272 if (!nested_vmx_check_permission(vcpu) ||
10273 !nested_vmx_check_vmcs12(vcpu))
10274 return 1;
10275
10276 skip_emulated_instruction(vcpu);
10277 vmcs12 = get_vmcs12(vcpu);
10278
Abel Gordon012f83c2013-04-18 14:39:25 +030010279 if (enable_shadow_vmcs)
10280 copy_shadow_to_vmcs12(vmx);
10281
Nadav Har'El7c177932011-05-25 23:12:04 +030010282 /*
10283 * The nested entry process starts with enforcing various prerequisites
10284 * on vmcs12 as required by the Intel SDM, and act appropriately when
10285 * they fail: As the SDM explains, some conditions should cause the
10286 * instruction to fail, while others will cause the instruction to seem
10287 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10288 * To speed up the normal (success) code path, we should avoid checking
10289 * for misconfigurations which will anyway be caught by the processor
10290 * when using the merged vmcs02.
10291 */
10292 if (vmcs12->launch_state == launch) {
10293 nested_vmx_failValid(vcpu,
10294 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10295 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10296 return 1;
10297 }
10298
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010299 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10300 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010301 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10302 return 1;
10303 }
10304
Wincy Van3af18d92015-02-03 23:49:31 +080010305 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010306 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10307 return 1;
10308 }
10309
Wincy Van3af18d92015-02-03 23:49:31 +080010310 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010311 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10312 return 1;
10313 }
10314
Wincy Vanf2b93282015-02-03 23:56:03 +080010315 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10316 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10317 return 1;
10318 }
10319
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010320 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10321 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10322 return 1;
10323 }
10324
Nadav Har'El7c177932011-05-25 23:12:04 +030010325 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010326 vmx->nested.nested_vmx_true_procbased_ctls_low,
10327 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010328 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010329 vmx->nested.nested_vmx_secondary_ctls_low,
10330 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010331 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010332 vmx->nested.nested_vmx_pinbased_ctls_low,
10333 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010334 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010335 vmx->nested.nested_vmx_true_exit_ctls_low,
10336 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010337 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010338 vmx->nested.nested_vmx_true_entry_ctls_low,
10339 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010340 {
10341 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10342 return 1;
10343 }
10344
10345 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10346 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10347 nested_vmx_failValid(vcpu,
10348 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10349 return 1;
10350 }
10351
Wincy Vanb9c237b2015-02-03 23:56:30 +080010352 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010353 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10354 nested_vmx_entry_failure(vcpu, vmcs12,
10355 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10356 return 1;
10357 }
10358 if (vmcs12->vmcs_link_pointer != -1ull) {
10359 nested_vmx_entry_failure(vcpu, vmcs12,
10360 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10361 return 1;
10362 }
10363
10364 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010365 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010366 * are performed on the field for the IA32_EFER MSR:
10367 * - Bits reserved in the IA32_EFER MSR must be 0.
10368 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10369 * the IA-32e mode guest VM-exit control. It must also be identical
10370 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10371 * CR0.PG) is 1.
10372 */
10373 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10374 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10375 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10376 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10377 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10378 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10379 nested_vmx_entry_failure(vcpu, vmcs12,
10380 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10381 return 1;
10382 }
10383 }
10384
10385 /*
10386 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10387 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10388 * the values of the LMA and LME bits in the field must each be that of
10389 * the host address-space size VM-exit control.
10390 */
10391 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10392 ia32e = (vmcs12->vm_exit_controls &
10393 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10394 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10395 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10396 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10397 nested_vmx_entry_failure(vcpu, vmcs12,
10398 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10399 return 1;
10400 }
10401 }
10402
10403 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010404 * We're finally done with prerequisite checking, and can start with
10405 * the nested entry.
10406 */
10407
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010408 enter_guest_mode(vcpu);
10409
Jan Kiszka2996fca2014-06-16 13:59:43 +020010410 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10411 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10412
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010413 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010414 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010415 vmx_vcpu_put(vcpu);
10416 vmx_vcpu_load(vcpu, cpu);
10417 vcpu->cpu = cpu;
10418 put_cpu();
10419
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010420 vmx_segment_cache_clear(vmx);
10421
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010422 prepare_vmcs02(vcpu, vmcs12);
10423
Wincy Vanff651cb2014-12-11 08:52:58 +030010424 msr_entry_idx = nested_vmx_load_msr(vcpu,
10425 vmcs12->vm_entry_msr_load_addr,
10426 vmcs12->vm_entry_msr_load_count);
10427 if (msr_entry_idx) {
10428 leave_guest_mode(vcpu);
10429 vmx_load_vmcs01(vcpu);
10430 nested_vmx_entry_failure(vcpu, vmcs12,
10431 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10432 return 1;
10433 }
10434
10435 vmcs12->launch_state = 1;
10436
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010437 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010438 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010439
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010440 vmx->nested.nested_run_pending = 1;
10441
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010442 /*
10443 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10444 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10445 * returned as far as L1 is concerned. It will only return (and set
10446 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10447 */
10448 return 1;
10449}
10450
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010451/*
10452 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10453 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10454 * This function returns the new value we should put in vmcs12.guest_cr0.
10455 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10456 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10457 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10458 * didn't trap the bit, because if L1 did, so would L0).
10459 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10460 * been modified by L2, and L1 knows it. So just leave the old value of
10461 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10462 * isn't relevant, because if L0 traps this bit it can set it to anything.
10463 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10464 * changed these bits, and therefore they need to be updated, but L0
10465 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10466 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10467 */
10468static inline unsigned long
10469vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10470{
10471 return
10472 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10473 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10474 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10475 vcpu->arch.cr0_guest_owned_bits));
10476}
10477
10478static inline unsigned long
10479vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10480{
10481 return
10482 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10483 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10484 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10485 vcpu->arch.cr4_guest_owned_bits));
10486}
10487
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010488static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10489 struct vmcs12 *vmcs12)
10490{
10491 u32 idt_vectoring;
10492 unsigned int nr;
10493
Gleb Natapov851eb6672013-09-25 12:51:34 +030010494 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010495 nr = vcpu->arch.exception.nr;
10496 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10497
10498 if (kvm_exception_is_soft(nr)) {
10499 vmcs12->vm_exit_instruction_len =
10500 vcpu->arch.event_exit_inst_len;
10501 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10502 } else
10503 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10504
10505 if (vcpu->arch.exception.has_error_code) {
10506 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10507 vmcs12->idt_vectoring_error_code =
10508 vcpu->arch.exception.error_code;
10509 }
10510
10511 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010512 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010513 vmcs12->idt_vectoring_info_field =
10514 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10515 } else if (vcpu->arch.interrupt.pending) {
10516 nr = vcpu->arch.interrupt.nr;
10517 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10518
10519 if (vcpu->arch.interrupt.soft) {
10520 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10521 vmcs12->vm_entry_instruction_len =
10522 vcpu->arch.event_exit_inst_len;
10523 } else
10524 idt_vectoring |= INTR_TYPE_EXT_INTR;
10525
10526 vmcs12->idt_vectoring_info_field = idt_vectoring;
10527 }
10528}
10529
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010530static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10531{
10532 struct vcpu_vmx *vmx = to_vmx(vcpu);
10533
Jan Kiszkaf4124502014-03-07 20:03:13 +010010534 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10535 vmx->nested.preemption_timer_expired) {
10536 if (vmx->nested.nested_run_pending)
10537 return -EBUSY;
10538 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10539 return 0;
10540 }
10541
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010542 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010543 if (vmx->nested.nested_run_pending ||
10544 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010545 return -EBUSY;
10546 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10547 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10548 INTR_INFO_VALID_MASK, 0);
10549 /*
10550 * The NMI-triggered VM exit counts as injection:
10551 * clear this one and block further NMIs.
10552 */
10553 vcpu->arch.nmi_pending = 0;
10554 vmx_set_nmi_mask(vcpu, true);
10555 return 0;
10556 }
10557
10558 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10559 nested_exit_on_intr(vcpu)) {
10560 if (vmx->nested.nested_run_pending)
10561 return -EBUSY;
10562 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010563 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010564 }
10565
David Hildenbrand1edccf22017-01-25 11:58:58 +010010566 vmx_complete_nested_posted_interrupt(vcpu);
10567 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010568}
10569
Jan Kiszkaf4124502014-03-07 20:03:13 +010010570static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10571{
10572 ktime_t remaining =
10573 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10574 u64 value;
10575
10576 if (ktime_to_ns(remaining) <= 0)
10577 return 0;
10578
10579 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10580 do_div(value, 1000000);
10581 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10582}
10583
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010584/*
10585 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10586 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10587 * and this function updates it to reflect the changes to the guest state while
10588 * L2 was running (and perhaps made some exits which were handled directly by L0
10589 * without going back to L1), and to reflect the exit reason.
10590 * Note that we do not have to copy here all VMCS fields, just those that
10591 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10592 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10593 * which already writes to vmcs12 directly.
10594 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010595static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10596 u32 exit_reason, u32 exit_intr_info,
10597 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010598{
10599 /* update guest state fields: */
10600 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10601 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10602
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010603 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10604 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10605 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10606
10607 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10608 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10609 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10610 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10611 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10612 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10613 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10614 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10615 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10616 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10617 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10618 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10619 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10620 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10621 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10622 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10623 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10624 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10625 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10626 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10627 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10628 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10629 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10630 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10631 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10632 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10633 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10634 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10635 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10636 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10637 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10638 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10639 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10640 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10641 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10642 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10643
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010644 vmcs12->guest_interruptibility_info =
10645 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10646 vmcs12->guest_pending_dbg_exceptions =
10647 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010648 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10649 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10650 else
10651 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010652
Jan Kiszkaf4124502014-03-07 20:03:13 +010010653 if (nested_cpu_has_preemption_timer(vmcs12)) {
10654 if (vmcs12->vm_exit_controls &
10655 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10656 vmcs12->vmx_preemption_timer_value =
10657 vmx_get_preemption_timer_value(vcpu);
10658 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10659 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010660
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010661 /*
10662 * In some cases (usually, nested EPT), L2 is allowed to change its
10663 * own CR3 without exiting. If it has changed it, we must keep it.
10664 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10665 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10666 *
10667 * Additionally, restore L2's PDPTR to vmcs12.
10668 */
10669 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010670 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010671 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10672 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10673 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10674 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10675 }
10676
Jim Mattson4933e9f2017-06-01 12:44:46 -070010677 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010678
Wincy Van608406e2015-02-03 23:57:51 +080010679 if (nested_cpu_has_vid(vmcs12))
10680 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10681
Jan Kiszkac18911a2013-03-13 16:06:41 +010010682 vmcs12->vm_entry_controls =
10683 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010684 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010685
Jan Kiszka2996fca2014-06-16 13:59:43 +020010686 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10687 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10688 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10689 }
10690
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010691 /* TODO: These cannot have changed unless we have MSR bitmaps and
10692 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010693 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010694 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010695 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10696 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010697 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10698 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10699 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010700 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010701 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010702 if (nested_cpu_has_xsaves(vmcs12))
10703 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010704
10705 /* update exit information fields: */
10706
Jan Kiszka533558b2014-01-04 18:47:20 +010010707 vmcs12->vm_exit_reason = exit_reason;
10708 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010709
Jan Kiszka533558b2014-01-04 18:47:20 +010010710 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010711 if ((vmcs12->vm_exit_intr_info &
10712 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10713 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10714 vmcs12->vm_exit_intr_error_code =
10715 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010716 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010717 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10718 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10719
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010720 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10721 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10722 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010723 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010724
10725 /*
10726 * Transfer the event that L0 or L1 may wanted to inject into
10727 * L2 to IDT_VECTORING_INFO_FIELD.
10728 */
10729 vmcs12_save_pending_event(vcpu, vmcs12);
10730 }
10731
10732 /*
10733 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10734 * preserved above and would only end up incorrectly in L1.
10735 */
10736 vcpu->arch.nmi_injected = false;
10737 kvm_clear_exception_queue(vcpu);
10738 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010739}
10740
10741/*
10742 * A part of what we need to when the nested L2 guest exits and we want to
10743 * run its L1 parent, is to reset L1's guest state to the host state specified
10744 * in vmcs12.
10745 * This function is to be called not only on normal nested exit, but also on
10746 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10747 * Failures During or After Loading Guest State").
10748 * This function should be called when the active VMCS is L1's (vmcs01).
10749 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010750static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10751 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010752{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010753 struct kvm_segment seg;
10754
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010755 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10756 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010757 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010758 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10759 else
10760 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10761 vmx_set_efer(vcpu, vcpu->arch.efer);
10762
10763 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10764 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010765 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010766 /*
10767 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10768 * actually changed, because it depends on the current state of
10769 * fpu_active (which may have changed).
10770 * Note that vmx_set_cr0 refers to efer set above.
10771 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010772 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010773 /*
10774 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10775 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10776 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10777 */
10778 update_exception_bitmap(vcpu);
10779 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10780 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10781
10782 /*
10783 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10784 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10785 */
10786 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080010787 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010788
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010789 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010790
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010791 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10792 kvm_mmu_reset_context(vcpu);
10793
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010794 if (!enable_ept)
10795 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10796
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010797 if (enable_vpid) {
10798 /*
10799 * Trivially support vpid by letting L2s share their parent
10800 * L1's vpid. TODO: move to a more elaborate solution, giving
10801 * each L2 its own vpid and exposing the vpid feature to L1.
10802 */
10803 vmx_flush_tlb(vcpu);
10804 }
10805
10806
10807 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10808 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10809 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10810 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10811 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020010812 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
10813 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010814
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010815 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10816 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10817 vmcs_write64(GUEST_BNDCFGS, 0);
10818
Jan Kiszka44811c02013-08-04 17:17:27 +020010819 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010820 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010821 vcpu->arch.pat = vmcs12->host_ia32_pat;
10822 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010823 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10824 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10825 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010826
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010827 /* Set L1 segment info according to Intel SDM
10828 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10829 seg = (struct kvm_segment) {
10830 .base = 0,
10831 .limit = 0xFFFFFFFF,
10832 .selector = vmcs12->host_cs_selector,
10833 .type = 11,
10834 .present = 1,
10835 .s = 1,
10836 .g = 1
10837 };
10838 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10839 seg.l = 1;
10840 else
10841 seg.db = 1;
10842 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10843 seg = (struct kvm_segment) {
10844 .base = 0,
10845 .limit = 0xFFFFFFFF,
10846 .type = 3,
10847 .present = 1,
10848 .s = 1,
10849 .db = 1,
10850 .g = 1
10851 };
10852 seg.selector = vmcs12->host_ds_selector;
10853 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10854 seg.selector = vmcs12->host_es_selector;
10855 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10856 seg.selector = vmcs12->host_ss_selector;
10857 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10858 seg.selector = vmcs12->host_fs_selector;
10859 seg.base = vmcs12->host_fs_base;
10860 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10861 seg.selector = vmcs12->host_gs_selector;
10862 seg.base = vmcs12->host_gs_base;
10863 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10864 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010865 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010866 .limit = 0x67,
10867 .selector = vmcs12->host_tr_selector,
10868 .type = 11,
10869 .present = 1
10870 };
10871 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10872
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010873 kvm_set_dr(vcpu, 7, 0x400);
10874 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010875
Wincy Van3af18d92015-02-03 23:49:31 +080010876 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010010877 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080010878
Wincy Vanff651cb2014-12-11 08:52:58 +030010879 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10880 vmcs12->vm_exit_msr_load_count))
10881 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010882}
10883
10884/*
10885 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10886 * and modify vmcs12 to make it see what it would expect to see there if
10887 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10888 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010889static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10890 u32 exit_intr_info,
10891 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010892{
10893 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010894 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10895
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010896 /* trying to cancel vmlaunch/vmresume is a bug */
10897 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10898
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010899 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010900 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10901 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010902
Wincy Vanff651cb2014-12-11 08:52:58 +030010903 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10904 vmcs12->vm_exit_msr_store_count))
10905 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10906
Wanpeng Lif3380ca2014-08-05 12:42:23 +080010907 vmx_load_vmcs01(vcpu);
10908
Bandan Das77b0f5d2014-04-19 18:17:45 -040010909 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10910 && nested_exit_intr_ack_set(vcpu)) {
10911 int irq = kvm_cpu_get_interrupt(vcpu);
10912 WARN_ON(irq < 0);
10913 vmcs12->vm_exit_intr_info = irq |
10914 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10915 }
10916
Jan Kiszka542060e2014-01-04 18:47:21 +010010917 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10918 vmcs12->exit_qualification,
10919 vmcs12->idt_vectoring_info_field,
10920 vmcs12->vm_exit_intr_info,
10921 vmcs12->vm_exit_intr_error_code,
10922 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010923
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010924 vm_entry_controls_reset_shadow(vmx);
10925 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010926 vmx_segment_cache_clear(vmx);
10927
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010928 load_vmcs12_host_state(vcpu, vmcs12);
10929
Paolo Bonzini93140062016-07-06 13:23:51 +020010930 /* Update any VMCS fields that might have changed while L2 ran */
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010931 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020010932 if (vmx->hv_deadline_tsc == -1)
10933 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10934 PIN_BASED_VMX_PREEMPTION_TIMER);
10935 else
10936 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10937 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070010938 if (kvm_has_tsc_control)
10939 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010940
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010941 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10942 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10943 vmx_set_virtual_x2apic_mode(vcpu,
10944 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070010945 } else if (!nested_cpu_has_ept(vmcs12) &&
10946 nested_cpu_has2(vmcs12,
10947 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10948 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010949 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010950
10951 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10952 vmx->host_rsp = 0;
10953
10954 /* Unpin physical memory we referred to in vmcs02 */
10955 if (vmx->nested.apic_access_page) {
10956 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010957 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010958 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010959 if (vmx->nested.virtual_apic_page) {
10960 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010961 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010962 }
Wincy Van705699a2015-02-03 23:58:17 +080010963 if (vmx->nested.pi_desc_page) {
10964 kunmap(vmx->nested.pi_desc_page);
10965 nested_release_page(vmx->nested.pi_desc_page);
10966 vmx->nested.pi_desc_page = NULL;
10967 vmx->nested.pi_desc = NULL;
10968 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010969
10970 /*
Tang Chen38b99172014-09-24 15:57:54 +080010971 * We are now running in L2, mmu_notifier will force to reload the
10972 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10973 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080010974 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080010975
10976 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010977 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10978 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10979 * success or failure flag accordingly.
10980 */
10981 if (unlikely(vmx->fail)) {
10982 vmx->fail = 0;
10983 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
10984 } else
10985 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010986 if (enable_shadow_vmcs)
10987 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010988
10989 /* in case we halted in L2 */
10990 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010991}
10992
Nadav Har'El7c177932011-05-25 23:12:04 +030010993/*
Jan Kiszka42124922014-01-04 18:47:19 +010010994 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10995 */
10996static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10997{
Wanpeng Lic886f282017-03-06 04:03:28 -080010998 if (is_guest_mode(vcpu)) {
10999 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011000 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011001 }
Jan Kiszka42124922014-01-04 18:47:19 +010011002 free_nested(to_vmx(vcpu));
11003}
11004
11005/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011006 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11007 * 23.7 "VM-entry failures during or after loading guest state" (this also
11008 * lists the acceptable exit-reason and exit-qualification parameters).
11009 * It should only be called before L2 actually succeeded to run, and when
11010 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11011 */
11012static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11013 struct vmcs12 *vmcs12,
11014 u32 reason, unsigned long qualification)
11015{
11016 load_vmcs12_host_state(vcpu, vmcs12);
11017 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11018 vmcs12->exit_qualification = qualification;
11019 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011020 if (enable_shadow_vmcs)
11021 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011022}
11023
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011024static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11025 struct x86_instruction_info *info,
11026 enum x86_intercept_stage stage)
11027{
11028 return X86EMUL_CONTINUE;
11029}
11030
Yunhong Jiang64672c92016-06-13 14:19:59 -070011031#ifdef CONFIG_X86_64
11032/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11033static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11034 u64 divisor, u64 *result)
11035{
11036 u64 low = a << shift, high = a >> (64 - shift);
11037
11038 /* To avoid the overflow on divq */
11039 if (high >= divisor)
11040 return 1;
11041
11042 /* Low hold the result, high hold rem which is discarded */
11043 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11044 "rm" (divisor), "0" (low), "1" (high));
11045 *result = low;
11046
11047 return 0;
11048}
11049
11050static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11051{
11052 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011053 u64 tscl = rdtsc();
11054 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11055 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011056
11057 /* Convert to host delta tsc if tsc scaling is enabled */
11058 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11059 u64_shl_div_u64(delta_tsc,
11060 kvm_tsc_scaling_ratio_frac_bits,
11061 vcpu->arch.tsc_scaling_ratio,
11062 &delta_tsc))
11063 return -ERANGE;
11064
11065 /*
11066 * If the delta tsc can't fit in the 32 bit after the multi shift,
11067 * we can't use the preemption timer.
11068 * It's possible that it fits on later vmentries, but checking
11069 * on every vmentry is costly so we just use an hrtimer.
11070 */
11071 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11072 return -ERANGE;
11073
11074 vmx->hv_deadline_tsc = tscl + delta_tsc;
11075 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11076 PIN_BASED_VMX_PREEMPTION_TIMER);
11077 return 0;
11078}
11079
11080static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11081{
11082 struct vcpu_vmx *vmx = to_vmx(vcpu);
11083 vmx->hv_deadline_tsc = -1;
11084 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11085 PIN_BASED_VMX_PREEMPTION_TIMER);
11086}
11087#endif
11088
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011089static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011090{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011091 if (ple_gap)
11092 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011093}
11094
Kai Huang843e4332015-01-28 10:54:28 +080011095static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11096 struct kvm_memory_slot *slot)
11097{
11098 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11099 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11100}
11101
11102static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11103 struct kvm_memory_slot *slot)
11104{
11105 kvm_mmu_slot_set_dirty(kvm, slot);
11106}
11107
11108static void vmx_flush_log_dirty(struct kvm *kvm)
11109{
11110 kvm_flush_pml_buffers(kvm);
11111}
11112
11113static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11114 struct kvm_memory_slot *memslot,
11115 gfn_t offset, unsigned long mask)
11116{
11117 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11118}
11119
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011120static void __pi_post_block(struct kvm_vcpu *vcpu)
11121{
11122 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11123 struct pi_desc old, new;
11124 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011125
11126 do {
11127 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011128 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11129 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011130
11131 dest = cpu_physical_id(vcpu->cpu);
11132
11133 if (x2apic_enabled())
11134 new.ndst = dest;
11135 else
11136 new.ndst = (dest << 8) & 0xFF00;
11137
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011138 /* set 'NV' to 'notification vector' */
11139 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011140 } while (cmpxchg64(&pi_desc->control, old.control,
11141 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011142
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011143 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11144 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011145 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011146 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011147 vcpu->pre_pcpu = -1;
11148 }
11149}
11150
Feng Wuefc64402015-09-18 22:29:51 +080011151/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011152 * This routine does the following things for vCPU which is going
11153 * to be blocked if VT-d PI is enabled.
11154 * - Store the vCPU to the wakeup list, so when interrupts happen
11155 * we can find the right vCPU to wake up.
11156 * - Change the Posted-interrupt descriptor as below:
11157 * 'NDST' <-- vcpu->pre_pcpu
11158 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11159 * - If 'ON' is set during this process, which means at least one
11160 * interrupt is posted for this vCPU, we cannot block it, in
11161 * this case, return 1, otherwise, return 0.
11162 *
11163 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011164static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011165{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011166 unsigned int dest;
11167 struct pi_desc old, new;
11168 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11169
11170 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011171 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11172 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011173 return 0;
11174
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011175 WARN_ON(irqs_disabled());
11176 local_irq_disable();
11177 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11178 vcpu->pre_pcpu = vcpu->cpu;
11179 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11180 list_add_tail(&vcpu->blocked_vcpu_list,
11181 &per_cpu(blocked_vcpu_on_cpu,
11182 vcpu->pre_pcpu));
11183 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11184 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011185
11186 do {
11187 old.control = new.control = pi_desc->control;
11188
Feng Wubf9f6ac2015-09-18 22:29:55 +080011189 WARN((pi_desc->sn == 1),
11190 "Warning: SN field of posted-interrupts "
11191 "is set before blocking\n");
11192
11193 /*
11194 * Since vCPU can be preempted during this process,
11195 * vcpu->cpu could be different with pre_pcpu, we
11196 * need to set pre_pcpu as the destination of wakeup
11197 * notification event, then we can find the right vCPU
11198 * to wakeup in wakeup handler if interrupts happen
11199 * when the vCPU is in blocked state.
11200 */
11201 dest = cpu_physical_id(vcpu->pre_pcpu);
11202
11203 if (x2apic_enabled())
11204 new.ndst = dest;
11205 else
11206 new.ndst = (dest << 8) & 0xFF00;
11207
11208 /* set 'NV' to 'wakeup vector' */
11209 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011210 } while (cmpxchg64(&pi_desc->control, old.control,
11211 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011212
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011213 /* We should not block the vCPU if an interrupt is posted for it. */
11214 if (pi_test_on(pi_desc) == 1)
11215 __pi_post_block(vcpu);
11216
11217 local_irq_enable();
11218 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011219}
11220
Yunhong Jiangbc225122016-06-13 14:19:58 -070011221static int vmx_pre_block(struct kvm_vcpu *vcpu)
11222{
11223 if (pi_pre_block(vcpu))
11224 return 1;
11225
Yunhong Jiang64672c92016-06-13 14:19:59 -070011226 if (kvm_lapic_hv_timer_in_use(vcpu))
11227 kvm_lapic_switch_to_sw_timer(vcpu);
11228
Yunhong Jiangbc225122016-06-13 14:19:58 -070011229 return 0;
11230}
11231
11232static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011233{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011234 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011235 return;
11236
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011237 WARN_ON(irqs_disabled());
11238 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011239 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011240 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011241}
11242
Yunhong Jiangbc225122016-06-13 14:19:58 -070011243static void vmx_post_block(struct kvm_vcpu *vcpu)
11244{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011245 if (kvm_x86_ops->set_hv_timer)
11246 kvm_lapic_switch_to_hv_timer(vcpu);
11247
Yunhong Jiangbc225122016-06-13 14:19:58 -070011248 pi_post_block(vcpu);
11249}
11250
Feng Wubf9f6ac2015-09-18 22:29:55 +080011251/*
Feng Wuefc64402015-09-18 22:29:51 +080011252 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11253 *
11254 * @kvm: kvm
11255 * @host_irq: host irq of the interrupt
11256 * @guest_irq: gsi of the interrupt
11257 * @set: set or unset PI
11258 * returns 0 on success, < 0 on failure
11259 */
11260static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11261 uint32_t guest_irq, bool set)
11262{
11263 struct kvm_kernel_irq_routing_entry *e;
11264 struct kvm_irq_routing_table *irq_rt;
11265 struct kvm_lapic_irq irq;
11266 struct kvm_vcpu *vcpu;
11267 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011268 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011269
11270 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011271 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11272 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011273 return 0;
11274
11275 idx = srcu_read_lock(&kvm->irq_srcu);
11276 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011277 if (guest_irq >= irq_rt->nr_rt_entries ||
11278 hlist_empty(&irq_rt->map[guest_irq])) {
11279 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11280 guest_irq, irq_rt->nr_rt_entries);
11281 goto out;
11282 }
Feng Wuefc64402015-09-18 22:29:51 +080011283
11284 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11285 if (e->type != KVM_IRQ_ROUTING_MSI)
11286 continue;
11287 /*
11288 * VT-d PI cannot support posting multicast/broadcast
11289 * interrupts to a vCPU, we still use interrupt remapping
11290 * for these kind of interrupts.
11291 *
11292 * For lowest-priority interrupts, we only support
11293 * those with single CPU as the destination, e.g. user
11294 * configures the interrupts via /proc/irq or uses
11295 * irqbalance to make the interrupts single-CPU.
11296 *
11297 * We will support full lowest-priority interrupt later.
11298 */
11299
Radim Krčmář371313132016-07-12 22:09:27 +020011300 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011301 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11302 /*
11303 * Make sure the IRTE is in remapped mode if
11304 * we don't handle it in posted mode.
11305 */
11306 ret = irq_set_vcpu_affinity(host_irq, NULL);
11307 if (ret < 0) {
11308 printk(KERN_INFO
11309 "failed to back to remapped mode, irq: %u\n",
11310 host_irq);
11311 goto out;
11312 }
11313
Feng Wuefc64402015-09-18 22:29:51 +080011314 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011315 }
Feng Wuefc64402015-09-18 22:29:51 +080011316
11317 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11318 vcpu_info.vector = irq.vector;
11319
Feng Wub6ce9782016-01-25 16:53:35 +080011320 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011321 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11322
11323 if (set)
11324 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011325 else
Feng Wuefc64402015-09-18 22:29:51 +080011326 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011327
11328 if (ret < 0) {
11329 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11330 __func__);
11331 goto out;
11332 }
11333 }
11334
11335 ret = 0;
11336out:
11337 srcu_read_unlock(&kvm->irq_srcu, idx);
11338 return ret;
11339}
11340
Ashok Rajc45dcc72016-06-22 14:59:56 +080011341static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11342{
11343 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11344 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11345 FEATURE_CONTROL_LMCE;
11346 else
11347 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11348 ~FEATURE_CONTROL_LMCE;
11349}
11350
Kees Cook404f6aa2016-08-08 16:29:06 -070011351static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011352 .cpu_has_kvm_support = cpu_has_kvm_support,
11353 .disabled_by_bios = vmx_disabled_by_bios,
11354 .hardware_setup = hardware_setup,
11355 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011356 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011357 .hardware_enable = hardware_enable,
11358 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011359 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011360 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011361
11362 .vcpu_create = vmx_create_vcpu,
11363 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011364 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011365
Avi Kivity04d2cc72007-09-10 18:10:54 +030011366 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011367 .vcpu_load = vmx_vcpu_load,
11368 .vcpu_put = vmx_vcpu_put,
11369
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011370 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011371 .get_msr = vmx_get_msr,
11372 .set_msr = vmx_set_msr,
11373 .get_segment_base = vmx_get_segment_base,
11374 .get_segment = vmx_get_segment,
11375 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011376 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011377 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011378 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011379 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011380 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011381 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011382 .set_cr3 = vmx_set_cr3,
11383 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011384 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011385 .get_idt = vmx_get_idt,
11386 .set_idt = vmx_set_idt,
11387 .get_gdt = vmx_get_gdt,
11388 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011389 .get_dr6 = vmx_get_dr6,
11390 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011391 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011392 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011393 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011394 .get_rflags = vmx_get_rflags,
11395 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011396
11397 .get_pkru = vmx_get_pkru,
11398
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011399 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011400 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011401
11402 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011403
Avi Kivity6aa8b732006-12-10 02:21:36 -080011404 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011405 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011406 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011407 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11408 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011409 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011410 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011411 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011412 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011413 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011414 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011415 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011416 .get_nmi_mask = vmx_get_nmi_mask,
11417 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011418 .enable_nmi_window = enable_nmi_window,
11419 .enable_irq_window = enable_irq_window,
11420 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011421 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011422 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011423 .get_enable_apicv = vmx_get_enable_apicv,
11424 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011425 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11426 .hwapic_irr_update = vmx_hwapic_irr_update,
11427 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011428 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11429 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011430
Izik Eiduscbc94022007-10-25 00:29:55 +020011431 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011432 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011433 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011434
Avi Kivity586f9602010-11-18 13:09:54 +020011435 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011436
Sheng Yang17cc3932010-01-05 19:02:27 +080011437 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011438
11439 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011440
11441 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011442 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011443
11444 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011445
11446 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011447
11448 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011449
11450 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011451
11452 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011453 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011454 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011455 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011456
11457 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011458
11459 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011460
11461 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11462 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11463 .flush_log_dirty = vmx_flush_log_dirty,
11464 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011465
Feng Wubf9f6ac2015-09-18 22:29:55 +080011466 .pre_block = vmx_pre_block,
11467 .post_block = vmx_post_block,
11468
Wei Huang25462f72015-06-19 15:45:05 +020011469 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011470
11471 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011472
11473#ifdef CONFIG_X86_64
11474 .set_hv_timer = vmx_set_hv_timer,
11475 .cancel_hv_timer = vmx_cancel_hv_timer,
11476#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011477
11478 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011479};
11480
11481static int __init vmx_init(void)
11482{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011483 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11484 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011485 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011486 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011487
Dave Young2965faa2015-09-09 15:38:55 -070011488#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011489 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11490 crash_vmclear_local_loaded_vmcss);
11491#endif
11492
He, Qingfdef3ad2007-04-30 09:45:24 +030011493 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011494}
11495
11496static void __exit vmx_exit(void)
11497{
Dave Young2965faa2015-09-09 15:38:55 -070011498#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011499 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011500 synchronize_rcu();
11501#endif
11502
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011503 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011504}
11505
11506module_init(vmx_init)
11507module_exit(vmx_exit)