blob: 2827a9622d978fe735e14609c45131b4d9d5a737 [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
Sean Christopherson94b4fed2018-03-23 09:34:00 -07002561 WARN_ON_ONCE(vmx->emulation_required);
2562
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002563 if (kvm_exception_is_soft(nr)) {
2564 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2565 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002566 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2567 } else
2568 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2569
2570 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002571}
2572
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002573static bool vmx_rdtscp_supported(void)
2574{
2575 return cpu_has_vmx_rdtscp();
2576}
2577
Mao, Junjiead756a12012-07-02 01:18:48 +00002578static bool vmx_invpcid_supported(void)
2579{
2580 return cpu_has_vmx_invpcid() && enable_ept;
2581}
2582
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583/*
Eddie Donga75beee2007-05-17 18:55:15 +03002584 * Swap MSR entry in host/guest MSR entry array.
2585 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002586static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002587{
Avi Kivity26bb0982009-09-07 11:14:12 +03002588 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002589
2590 tmp = vmx->guest_msrs[to];
2591 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2592 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002593}
2594
2595/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002596 * Set up the vmcs to automatically save and restore system
2597 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2598 * mode, as fiddling with msrs is very expensive.
2599 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002600static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002601{
Avi Kivity26bb0982009-09-07 11:14:12 +03002602 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002603
Eddie Donga75beee2007-05-17 18:55:15 +03002604 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002605#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002606 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002607 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002608 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002609 move_msr_up(vmx, index, save_nmsrs++);
2610 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002611 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002612 move_msr_up(vmx, index, save_nmsrs++);
2613 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002614 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002615 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002616 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002617 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002618 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002619 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002620 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002621 * if efer.sce is enabled.
2622 */
Brian Gerst8c065852010-07-17 09:03:26 -04002623 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002624 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002625 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002626 }
Eddie Donga75beee2007-05-17 18:55:15 +03002627#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002628 index = __find_msr_index(vmx, MSR_EFER);
2629 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002630 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002631
Avi Kivity26bb0982009-09-07 11:14:12 +03002632 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002633
Yang Zhang8d146952013-01-25 10:18:50 +08002634 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01002635 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002636}
2637
2638/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002640 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2641 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002643static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644{
2645 u64 host_tsc, tsc_offset;
2646
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002647 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002649 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650}
2651
2652/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002653 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002655static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002656{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002657 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002658 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002659 * We're here if L1 chose not to trap WRMSR to TSC. According
2660 * to the spec, this should set L1's TSC; The offset that L1
2661 * set for L2 remains unchanged, and still needs to be added
2662 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002663 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002664 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002665 /* recalculate vmcs02.TSC_OFFSET: */
2666 vmcs12 = get_vmcs12(vcpu);
2667 vmcs_write64(TSC_OFFSET, offset +
2668 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2669 vmcs12->tsc_offset : 0));
2670 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002671 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2672 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002673 vmcs_write64(TSC_OFFSET, offset);
2674 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675}
2676
Nadav Har'El801d3422011-05-25 23:02:23 +03002677static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2678{
2679 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2680 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2681}
2682
2683/*
2684 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2685 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2686 * all guests if the "nested" module option is off, and can also be disabled
2687 * for a single guest by disabling its VMX cpuid bit.
2688 */
2689static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2690{
2691 return nested && guest_cpuid_has_vmx(vcpu);
2692}
2693
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002695 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2696 * returned for the various VMX controls MSRs when nested VMX is enabled.
2697 * The same values should also be used to verify that vmcs12 control fields are
2698 * valid during nested entry from L1 to L2.
2699 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2700 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2701 * bit in the high half is on if the corresponding bit in the control field
2702 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002703 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002704static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002705{
2706 /*
2707 * Note that as a general rule, the high half of the MSRs (bits in
2708 * the control fields which may be 1) should be initialized by the
2709 * intersection of the underlying hardware's MSR (i.e., features which
2710 * can be supported) and the list of features we want to expose -
2711 * because they are known to be properly supported in our code.
2712 * Also, usually, the low half of the MSRs (bits which must be 1) can
2713 * be set to 0, meaning that L1 may turn off any of these bits. The
2714 * reason is that if one of these bits is necessary, it will appear
2715 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2716 * fields of vmcs01 and vmcs02, will turn these bits off - and
2717 * nested_vmx_exit_handled() will not pass related exits to L1.
2718 * These rules have exceptions below.
2719 */
2720
2721 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002722 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002723 vmx->nested.nested_vmx_pinbased_ctls_low,
2724 vmx->nested.nested_vmx_pinbased_ctls_high);
2725 vmx->nested.nested_vmx_pinbased_ctls_low |=
2726 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2727 vmx->nested.nested_vmx_pinbased_ctls_high &=
2728 PIN_BASED_EXT_INTR_MASK |
2729 PIN_BASED_NMI_EXITING |
2730 PIN_BASED_VIRTUAL_NMIS;
2731 vmx->nested.nested_vmx_pinbased_ctls_high |=
2732 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002733 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002734 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002735 vmx->nested.nested_vmx_pinbased_ctls_high |=
2736 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002737
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002738 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002739 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002740 vmx->nested.nested_vmx_exit_ctls_low,
2741 vmx->nested.nested_vmx_exit_ctls_high);
2742 vmx->nested.nested_vmx_exit_ctls_low =
2743 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002744
Wincy Vanb9c237b2015-02-03 23:56:30 +08002745 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002746#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002747 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002748#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002749 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002750 vmx->nested.nested_vmx_exit_ctls_high |=
2751 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002752 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002753 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2754
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002755 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002756 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002757
Jan Kiszka2996fca2014-06-16 13:59:43 +02002758 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002759 vmx->nested.nested_vmx_true_exit_ctls_low =
2760 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002761 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2762
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002763 /* entry controls */
2764 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002765 vmx->nested.nested_vmx_entry_ctls_low,
2766 vmx->nested.nested_vmx_entry_ctls_high);
2767 vmx->nested.nested_vmx_entry_ctls_low =
2768 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2769 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002770#ifdef CONFIG_X86_64
2771 VM_ENTRY_IA32E_MODE |
2772#endif
2773 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002774 vmx->nested.nested_vmx_entry_ctls_high |=
2775 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002776 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002777 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002778
Jan Kiszka2996fca2014-06-16 13:59:43 +02002779 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002780 vmx->nested.nested_vmx_true_entry_ctls_low =
2781 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002782 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2783
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002784 /* cpu-based controls */
2785 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002786 vmx->nested.nested_vmx_procbased_ctls_low,
2787 vmx->nested.nested_vmx_procbased_ctls_high);
2788 vmx->nested.nested_vmx_procbased_ctls_low =
2789 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2790 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002791 CPU_BASED_VIRTUAL_INTR_PENDING |
2792 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002793 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2794 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2795 CPU_BASED_CR3_STORE_EXITING |
2796#ifdef CONFIG_X86_64
2797 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2798#endif
2799 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002800 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2801 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2802 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2803 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002804 /*
2805 * We can allow some features even when not supported by the
2806 * hardware. For example, L1 can specify an MSR bitmap - and we
2807 * can use it to avoid exits to L1 - even when L0 runs L2
2808 * without MSR bitmaps.
2809 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002810 vmx->nested.nested_vmx_procbased_ctls_high |=
2811 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002812 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002813
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002814 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002815 vmx->nested.nested_vmx_true_procbased_ctls_low =
2816 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002817 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2818
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002819 /* secondary cpu-based controls */
2820 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002821 vmx->nested.nested_vmx_secondary_ctls_low,
2822 vmx->nested.nested_vmx_secondary_ctls_high);
2823 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2824 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002825 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002826 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002827 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002828 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002829 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002830 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002831 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002832 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002833
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002834 if (enable_ept) {
2835 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002836 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002837 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002838 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002839 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2840 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002841 if (cpu_has_vmx_ept_execute_only())
2842 vmx->nested.nested_vmx_ept_caps |=
2843 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002844 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002845 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2846 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002847 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002848 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002849
Paolo Bonzinief697a72016-03-18 16:58:38 +01002850 /*
2851 * Old versions of KVM use the single-context version without
2852 * checking for support, so declare that it is supported even
2853 * though it is treated as global context. The alternative is
2854 * not failing the single-context invvpid, and it is worse.
2855 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002856 if (enable_vpid)
2857 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevich04c776e2018-02-23 11:42:18 +01002858 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002859 else
2860 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002861
Radim Krčmář0790ec12015-03-17 14:02:32 +01002862 if (enable_unrestricted_guest)
2863 vmx->nested.nested_vmx_secondary_ctls_high |=
2864 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2865
Jan Kiszkac18911a2013-03-13 16:06:41 +01002866 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002867 rdmsr(MSR_IA32_VMX_MISC,
2868 vmx->nested.nested_vmx_misc_low,
2869 vmx->nested.nested_vmx_misc_high);
2870 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2871 vmx->nested.nested_vmx_misc_low |=
2872 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002873 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002874 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002875}
2876
2877static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2878{
2879 /*
2880 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2881 */
2882 return ((control & high) | low) == control;
2883}
2884
2885static inline u64 vmx_control_msr(u32 low, u32 high)
2886{
2887 return low | ((u64)high << 32);
2888}
2889
Jan Kiszkacae50132014-01-04 18:47:22 +01002890/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002891static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2892{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002893 struct vcpu_vmx *vmx = to_vmx(vcpu);
2894
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002895 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002896 case MSR_IA32_VMX_BASIC:
2897 /*
2898 * This MSR reports some information about VMX support. We
2899 * should return information about the VMX we emulate for the
2900 * guest, and the VMCS structure we give it - not about the
2901 * VMX support of the underlying hardware.
2902 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002903 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002904 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2905 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002906 if (cpu_has_vmx_basic_inout())
2907 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002908 break;
2909 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2910 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002911 *pdata = vmx_control_msr(
2912 vmx->nested.nested_vmx_pinbased_ctls_low,
2913 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002914 break;
2915 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002916 *pdata = vmx_control_msr(
2917 vmx->nested.nested_vmx_true_procbased_ctls_low,
2918 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002919 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002920 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002921 *pdata = vmx_control_msr(
2922 vmx->nested.nested_vmx_procbased_ctls_low,
2923 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002924 break;
2925 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002926 *pdata = vmx_control_msr(
2927 vmx->nested.nested_vmx_true_exit_ctls_low,
2928 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002929 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002930 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002931 *pdata = vmx_control_msr(
2932 vmx->nested.nested_vmx_exit_ctls_low,
2933 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002934 break;
2935 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002936 *pdata = vmx_control_msr(
2937 vmx->nested.nested_vmx_true_entry_ctls_low,
2938 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002939 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002940 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002941 *pdata = vmx_control_msr(
2942 vmx->nested.nested_vmx_entry_ctls_low,
2943 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002944 break;
2945 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002946 *pdata = vmx_control_msr(
2947 vmx->nested.nested_vmx_misc_low,
2948 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002949 break;
2950 /*
2951 * These MSRs specify bits which the guest must keep fixed (on or off)
2952 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2953 * We picked the standard core2 setting.
2954 */
2955#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2956#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2957 case MSR_IA32_VMX_CR0_FIXED0:
2958 *pdata = VMXON_CR0_ALWAYSON;
2959 break;
2960 case MSR_IA32_VMX_CR0_FIXED1:
2961 *pdata = -1ULL;
2962 break;
2963 case MSR_IA32_VMX_CR4_FIXED0:
2964 *pdata = VMXON_CR4_ALWAYSON;
2965 break;
2966 case MSR_IA32_VMX_CR4_FIXED1:
2967 *pdata = -1ULL;
2968 break;
2969 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002970 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002971 break;
2972 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002973 *pdata = vmx_control_msr(
2974 vmx->nested.nested_vmx_secondary_ctls_low,
2975 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002976 break;
2977 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07002978 *pdata = vmx->nested.nested_vmx_ept_caps |
2979 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002980 break;
2981 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002982 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002983 }
2984
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002985 return 0;
2986}
2987
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002988static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2989 uint64_t val)
2990{
2991 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2992
2993 return !(val & ~valid_bits);
2994}
2995
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002996/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002997 * Reads an msr value (of 'msr_index') into 'pdata'.
2998 * Returns 0 on success, non-0 otherwise.
2999 * Assumes vcpu_load() was already called.
3000 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003001static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002{
Avi Kivity26bb0982009-09-07 11:14:12 +03003003 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003005 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003006#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003008 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 break;
3010 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003011 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003013 case MSR_KERNEL_GS_BASE:
3014 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003015 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003016 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003017#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003018 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003019 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303020 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003021 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003023 case MSR_IA32_SPEC_CTRL:
3024 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003025 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003026 return 1;
3027
3028 msr_info->data = to_vmx(vcpu)->spec_ctrl;
3029 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003030 case MSR_IA32_ARCH_CAPABILITIES:
3031 if (!msr_info->host_initiated &&
3032 !guest_cpuid_has_arch_capabilities(vcpu))
3033 return 1;
3034 msr_info->data = to_vmx(vcpu)->arch_capabilities;
3035 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003037 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 break;
3039 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003040 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041 break;
3042 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003043 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003045 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003046 if (!kvm_mpx_supported() ||
3047 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003048 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003049 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003050 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003051 case MSR_IA32_MCG_EXT_CTL:
3052 if (!msr_info->host_initiated &&
3053 !(to_vmx(vcpu)->msr_ia32_feature_control &
3054 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003055 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003056 msr_info->data = vcpu->arch.mcg_ext_ctl;
3057 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003058 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003059 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003060 break;
3061 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3062 if (!nested_vmx_allowed(vcpu))
3063 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003064 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003065 case MSR_IA32_XSS:
3066 if (!vmx_xsaves_supported())
3067 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003068 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003069 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003070 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003071 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003072 return 1;
3073 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003075 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003076 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003077 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003078 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003080 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 }
3082
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 return 0;
3084}
3085
Jan Kiszkacae50132014-01-04 18:47:22 +01003086static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3087
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088/*
3089 * Writes msr value into into the appropriate "register".
3090 * Returns 0 on success, non-0 otherwise.
3091 * Assumes vcpu_load() was already called.
3092 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003093static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003095 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003096 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003097 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003098 u32 msr_index = msr_info->index;
3099 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003100
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003102 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003103 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003104 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003105#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003107 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 vmcs_writel(GUEST_FS_BASE, data);
3109 break;
3110 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003111 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112 vmcs_writel(GUEST_GS_BASE, data);
3113 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003114 case MSR_KERNEL_GS_BASE:
3115 vmx_load_host_state(vmx);
3116 vmx->msr_guest_kernel_gs_base = data;
3117 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118#endif
3119 case MSR_IA32_SYSENTER_CS:
3120 vmcs_write32(GUEST_SYSENTER_CS, data);
3121 break;
3122 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003123 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 break;
3125 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003126 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003128 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003129 if (!kvm_mpx_supported() ||
3130 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003131 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003132 if (is_noncanonical_address(data & PAGE_MASK) ||
3133 (data & MSR_IA32_BNDCFGS_RSVD))
3134 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003135 vmcs_write64(GUEST_BNDCFGS, data);
3136 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303137 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003138 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139 break;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003140 case MSR_IA32_SPEC_CTRL:
3141 if (!msr_info->host_initiated &&
Konrad Rzeszutek Wilk99318ec2018-04-25 22:04:25 -04003142 !guest_cpuid_has_spec_ctrl(vcpu))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003143 return 1;
3144
3145 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilkbf3da842018-05-09 21:41:38 +02003146 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01003147 return 1;
3148
3149 vmx->spec_ctrl = data;
3150
3151 if (!data)
3152 break;
3153
3154 /*
3155 * For non-nested:
3156 * When it's written (to non-zero) for the first time, pass
3157 * it through.
3158 *
3159 * For nested:
3160 * The handling of the MSR bitmap for L2 guests is done in
3161 * nested_vmx_merge_msr_bitmap. We should not touch the
3162 * vmcs02.msr_bitmap here since it gets completely overwritten
3163 * in the merging. We update the vmcs01 here for L1 as well
3164 * since it will end up touching the MSR anyway now.
3165 */
3166 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
3167 MSR_IA32_SPEC_CTRL,
3168 MSR_TYPE_RW);
3169 break;
Ashok Raj70131292018-02-01 22:59:43 +01003170 case MSR_IA32_PRED_CMD:
3171 if (!msr_info->host_initiated &&
3172 !guest_cpuid_has_ibpb(vcpu))
3173 return 1;
3174
3175 if (data & ~PRED_CMD_IBPB)
3176 return 1;
3177
3178 if (!data)
3179 break;
3180
3181 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
3182
3183 /*
3184 * For non-nested:
3185 * When it's written (to non-zero) for the first time, pass
3186 * it through.
3187 *
3188 * For nested:
3189 * The handling of the MSR bitmap for L2 guests is done in
3190 * nested_vmx_merge_msr_bitmap. We should not touch the
3191 * vmcs02.msr_bitmap here since it gets completely overwritten
3192 * in the merging.
3193 */
3194 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
3195 MSR_TYPE_W);
3196 break;
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01003197 case MSR_IA32_ARCH_CAPABILITIES:
3198 if (!msr_info->host_initiated)
3199 return 1;
3200 vmx->arch_capabilities = data;
3201 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003202 case MSR_IA32_CR_PAT:
3203 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003204 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3205 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003206 vmcs_write64(GUEST_IA32_PAT, data);
3207 vcpu->arch.pat = data;
3208 break;
3209 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003210 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003211 break;
Will Auldba904632012-11-29 12:42:50 -08003212 case MSR_IA32_TSC_ADJUST:
3213 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003214 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003215 case MSR_IA32_MCG_EXT_CTL:
3216 if ((!msr_info->host_initiated &&
3217 !(to_vmx(vcpu)->msr_ia32_feature_control &
3218 FEATURE_CONTROL_LMCE)) ||
3219 (data & ~MCG_EXT_CTL_LMCE_EN))
3220 return 1;
3221 vcpu->arch.mcg_ext_ctl = data;
3222 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003223 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003224 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003225 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003226 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3227 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003228 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003229 if (msr_info->host_initiated && data == 0)
3230 vmx_leave_nested(vcpu);
3231 break;
3232 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3233 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003234 case MSR_IA32_XSS:
3235 if (!vmx_xsaves_supported())
3236 return 1;
3237 /*
3238 * The only supported bit as of Skylake is bit 8, but
3239 * it is not supported on KVM.
3240 */
3241 if (data != 0)
3242 return 1;
3243 vcpu->arch.ia32_xss = data;
3244 if (vcpu->arch.ia32_xss != host_xss)
3245 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3246 vcpu->arch.ia32_xss, host_xss);
3247 else
3248 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3249 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003250 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003251 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003252 return 1;
3253 /* Check reserved bit, higher 32 bits should be zero */
3254 if ((data >> 32) != 0)
3255 return 1;
3256 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003257 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003258 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003259 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003260 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003261 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003262 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3263 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003264 ret = kvm_set_shared_msr(msr->index, msr->data,
3265 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003266 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003267 if (ret)
3268 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003269 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003270 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003272 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273 }
3274
Eddie Dong2cc51562007-05-21 07:28:09 +03003275 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276}
3277
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003278static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003279{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003280 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3281 switch (reg) {
3282 case VCPU_REGS_RSP:
3283 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3284 break;
3285 case VCPU_REGS_RIP:
3286 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3287 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003288 case VCPU_EXREG_PDPTR:
3289 if (enable_ept)
3290 ept_save_pdptrs(vcpu);
3291 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003292 default:
3293 break;
3294 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295}
3296
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297static __init int cpu_has_kvm_support(void)
3298{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003299 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003300}
3301
3302static __init int vmx_disabled_by_bios(void)
3303{
3304 u64 msr;
3305
3306 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003307 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003308 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003309 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3310 && tboot_enabled())
3311 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003312 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003313 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003314 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003315 && !tboot_enabled()) {
3316 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003317 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003318 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003319 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003320 /* launched w/o TXT and VMX disabled */
3321 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3322 && !tboot_enabled())
3323 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003324 }
3325
3326 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003327}
3328
Dongxiao Xu7725b892010-05-11 18:29:38 +08003329static void kvm_cpu_vmxon(u64 addr)
3330{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003331 intel_pt_handle_vmx(1);
3332
Dongxiao Xu7725b892010-05-11 18:29:38 +08003333 asm volatile (ASM_VMX_VMXON_RAX
3334 : : "a"(&addr), "m"(addr)
3335 : "memory", "cc");
3336}
3337
Radim Krčmář13a34e02014-08-28 15:13:03 +02003338static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339{
3340 int cpu = raw_smp_processor_id();
3341 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003342 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003344 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003345 return -EBUSY;
3346
Nadav Har'Eld462b812011-05-24 15:26:10 +03003347 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003348 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3349 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003350
3351 /*
3352 * Now we can enable the vmclear operation in kdump
3353 * since the loaded_vmcss_on_cpu list on this cpu
3354 * has been initialized.
3355 *
3356 * Though the cpu is not in VMX operation now, there
3357 * is no problem to enable the vmclear operation
3358 * for the loaded_vmcss_on_cpu list is empty!
3359 */
3360 crash_enable_local_vmclear(cpu);
3361
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003363
3364 test_bits = FEATURE_CONTROL_LOCKED;
3365 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3366 if (tboot_enabled())
3367 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3368
3369 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003371 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3372 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003373 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003374
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003375 if (vmm_exclusive) {
3376 kvm_cpu_vmxon(phys_addr);
3377 ept_sync_global();
3378 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003379
Christoph Lameter89cbc762014-08-17 12:30:40 -05003380 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003381
Alexander Graf10474ae2009-09-15 11:37:46 +02003382 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383}
3384
Nadav Har'Eld462b812011-05-24 15:26:10 +03003385static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003386{
3387 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003388 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003389
Nadav Har'Eld462b812011-05-24 15:26:10 +03003390 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3391 loaded_vmcss_on_cpu_link)
3392 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003393}
3394
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003395
3396/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3397 * tricks.
3398 */
3399static void kvm_cpu_vmxoff(void)
3400{
3401 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003402
3403 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003404}
3405
Radim Krčmář13a34e02014-08-28 15:13:03 +02003406static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003408 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003409 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003410 kvm_cpu_vmxoff();
3411 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003412 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413}
3414
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003415static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003416 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417{
3418 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003419 u32 ctl = ctl_min | ctl_opt;
3420
3421 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3422
3423 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3424 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3425
3426 /* Ensure minimum (required) set of control bits are supported. */
3427 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003428 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003429
3430 *result = ctl;
3431 return 0;
3432}
3433
Avi Kivity110312c2010-12-21 12:54:20 +02003434static __init bool allow_1_setting(u32 msr, u32 ctl)
3435{
3436 u32 vmx_msr_low, vmx_msr_high;
3437
3438 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3439 return vmx_msr_high & ctl;
3440}
3441
Yang, Sheng002c7f72007-07-31 14:23:01 +03003442static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003443{
3444 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003445 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003446 u32 _pin_based_exec_control = 0;
3447 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003448 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003449 u32 _vmexit_control = 0;
3450 u32 _vmentry_control = 0;
3451
Raghavendra K T10166742012-02-07 23:19:20 +05303452 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003453#ifdef CONFIG_X86_64
3454 CPU_BASED_CR8_LOAD_EXITING |
3455 CPU_BASED_CR8_STORE_EXITING |
3456#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003457 CPU_BASED_CR3_LOAD_EXITING |
3458 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003459 CPU_BASED_USE_IO_BITMAPS |
3460 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003461 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003462 CPU_BASED_MWAIT_EXITING |
3463 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003464 CPU_BASED_INVLPG_EXITING |
3465 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003466
Sheng Yangf78e0e22007-10-29 09:40:42 +08003467 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003468 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003469 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003470 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3471 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003472 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003473#ifdef CONFIG_X86_64
3474 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3475 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3476 ~CPU_BASED_CR8_STORE_EXITING;
3477#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003478 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003479 min2 = 0;
3480 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003481 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003482 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003483 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003484 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003485 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003486 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003487 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003488 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003489 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003490 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003491 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003492 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003493 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003494 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003495 if (adjust_vmx_controls(min2, opt2,
3496 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003497 &_cpu_based_2nd_exec_control) < 0)
3498 return -EIO;
3499 }
3500#ifndef CONFIG_X86_64
3501 if (!(_cpu_based_2nd_exec_control &
3502 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3503 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3504#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003505
3506 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3507 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003508 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003509 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3510 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003511
Sheng Yangd56f5462008-04-25 10:13:16 +08003512 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003513 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3514 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003515 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3516 CPU_BASED_CR3_STORE_EXITING |
3517 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003518 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3519 vmx_capability.ept, vmx_capability.vpid);
3520 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003521
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003522 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003523#ifdef CONFIG_X86_64
3524 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3525#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003526 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003527 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003528 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3529 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003530 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003531
Yang Zhang01e439b2013-04-11 19:25:12 +08003532 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003533 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3534 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003535 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3536 &_pin_based_exec_control) < 0)
3537 return -EIO;
3538
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003539 if (cpu_has_broken_vmx_preemption_timer())
3540 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003541 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003542 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003543 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3544
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003545 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003546 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003547 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3548 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003549 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003550
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003551 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003552
3553 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3554 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003555 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003556
3557#ifdef CONFIG_X86_64
3558 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3559 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003560 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003561#endif
3562
3563 /* Require Write-Back (WB) memory type for VMCS accesses. */
3564 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003565 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003566
Yang, Sheng002c7f72007-07-31 14:23:01 +03003567 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003568 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003569 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003570 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003571
Yang, Sheng002c7f72007-07-31 14:23:01 +03003572 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3573 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003574 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003575 vmcs_conf->vmexit_ctrl = _vmexit_control;
3576 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003577
Avi Kivity110312c2010-12-21 12:54:20 +02003578 cpu_has_load_ia32_efer =
3579 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3580 VM_ENTRY_LOAD_IA32_EFER)
3581 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3582 VM_EXIT_LOAD_IA32_EFER);
3583
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003584 cpu_has_load_perf_global_ctrl =
3585 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3586 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3587 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3588 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3589
3590 /*
3591 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003592 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003593 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3594 *
3595 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3596 *
3597 * AAK155 (model 26)
3598 * AAP115 (model 30)
3599 * AAT100 (model 37)
3600 * BC86,AAY89,BD102 (model 44)
3601 * BA97 (model 46)
3602 *
3603 */
3604 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3605 switch (boot_cpu_data.x86_model) {
3606 case 26:
3607 case 30:
3608 case 37:
3609 case 44:
3610 case 46:
3611 cpu_has_load_perf_global_ctrl = false;
3612 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3613 "does not work properly. Using workaround\n");
3614 break;
3615 default:
3616 break;
3617 }
3618 }
3619
Borislav Petkov782511b2016-04-04 22:25:03 +02003620 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003621 rdmsrl(MSR_IA32_XSS, host_xss);
3622
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003623 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003624}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003625
3626static struct vmcs *alloc_vmcs_cpu(int cpu)
3627{
3628 int node = cpu_to_node(cpu);
3629 struct page *pages;
3630 struct vmcs *vmcs;
3631
Vlastimil Babka96db8002015-09-08 15:03:50 -07003632 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633 if (!pages)
3634 return NULL;
3635 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003636 memset(vmcs, 0, vmcs_config.size);
3637 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638 return vmcs;
3639}
3640
Avi Kivity6aa8b732006-12-10 02:21:36 -08003641static void free_vmcs(struct vmcs *vmcs)
3642{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003643 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003644}
3645
Nadav Har'Eld462b812011-05-24 15:26:10 +03003646/*
3647 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3648 */
3649static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3650{
3651 if (!loaded_vmcs->vmcs)
3652 return;
3653 loaded_vmcs_clear(loaded_vmcs);
3654 free_vmcs(loaded_vmcs->vmcs);
3655 loaded_vmcs->vmcs = NULL;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003656 if (loaded_vmcs->msr_bitmap)
3657 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07003658 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003659}
3660
Paolo Bonziniff546f92018-01-11 12:16:15 +01003661static struct vmcs *alloc_vmcs(void)
3662{
3663 return alloc_vmcs_cpu(raw_smp_processor_id());
3664}
3665
3666static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3667{
3668 loaded_vmcs->vmcs = alloc_vmcs();
3669 if (!loaded_vmcs->vmcs)
3670 return -ENOMEM;
3671
3672 loaded_vmcs->shadow_vmcs = NULL;
3673 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini6236b782018-01-16 16:51:18 +01003674
3675 if (cpu_has_vmx_msr_bitmap()) {
3676 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
3677 if (!loaded_vmcs->msr_bitmap)
3678 goto out_vmcs;
3679 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3680 }
Paolo Bonziniff546f92018-01-11 12:16:15 +01003681 return 0;
Paolo Bonzini6236b782018-01-16 16:51:18 +01003682
3683out_vmcs:
3684 free_loaded_vmcs(loaded_vmcs);
3685 return -ENOMEM;
Paolo Bonziniff546f92018-01-11 12:16:15 +01003686}
3687
Sam Ravnborg39959582007-06-01 00:47:13 -07003688static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003689{
3690 int cpu;
3691
Zachary Amsden3230bb42009-09-29 11:38:37 -10003692 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003693 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003694 per_cpu(vmxarea, cpu) = NULL;
3695 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696}
3697
Bandan Dasfe2b2012014-04-21 15:20:14 -04003698static void init_vmcs_shadow_fields(void)
3699{
3700 int i, j;
3701
3702 /* No checks for read only fields yet */
3703
3704 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3705 switch (shadow_read_write_fields[i]) {
3706 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003707 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003708 continue;
3709 break;
3710 default:
3711 break;
3712 }
3713
3714 if (j < i)
3715 shadow_read_write_fields[j] =
3716 shadow_read_write_fields[i];
3717 j++;
3718 }
3719 max_shadow_read_write_fields = j;
3720
3721 /* shadowed fields guest access without vmexit */
3722 for (i = 0; i < max_shadow_read_write_fields; i++) {
3723 clear_bit(shadow_read_write_fields[i],
3724 vmx_vmwrite_bitmap);
3725 clear_bit(shadow_read_write_fields[i],
3726 vmx_vmread_bitmap);
3727 }
3728 for (i = 0; i < max_shadow_read_only_fields; i++)
3729 clear_bit(shadow_read_only_fields[i],
3730 vmx_vmread_bitmap);
3731}
3732
Avi Kivity6aa8b732006-12-10 02:21:36 -08003733static __init int alloc_kvm_area(void)
3734{
3735 int cpu;
3736
Zachary Amsden3230bb42009-09-29 11:38:37 -10003737 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003738 struct vmcs *vmcs;
3739
3740 vmcs = alloc_vmcs_cpu(cpu);
3741 if (!vmcs) {
3742 free_kvm_area();
3743 return -ENOMEM;
3744 }
3745
3746 per_cpu(vmxarea, cpu) = vmcs;
3747 }
3748 return 0;
3749}
3750
Gleb Natapov14168782013-01-21 15:36:49 +02003751static bool emulation_required(struct kvm_vcpu *vcpu)
3752{
3753 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3754}
3755
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003756static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003757 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003758{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003759 if (!emulate_invalid_guest_state) {
3760 /*
3761 * CS and SS RPL should be equal during guest entry according
3762 * to VMX spec, but in reality it is not always so. Since vcpu
3763 * is in the middle of the transition from real mode to
3764 * protected mode it is safe to assume that RPL 0 is a good
3765 * default value.
3766 */
3767 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003768 save->selector &= ~SEGMENT_RPL_MASK;
3769 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003770 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003772 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773}
3774
3775static void enter_pmode(struct kvm_vcpu *vcpu)
3776{
3777 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003778 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779
Gleb Natapovd99e4152012-12-20 16:57:45 +02003780 /*
3781 * Update real mode segment cache. It may be not up-to-date if sement
3782 * register was written while vcpu was in a guest mode.
3783 */
3784 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3785 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3786 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3787 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3788 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3789 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3790
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003791 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003792
Avi Kivity2fb92db2011-04-27 19:42:18 +03003793 vmx_segment_cache_clear(vmx);
3794
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003795 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796
3797 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003798 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3799 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800 vmcs_writel(GUEST_RFLAGS, flags);
3801
Rusty Russell66aee912007-07-17 23:34:16 +10003802 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3803 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804
3805 update_exception_bitmap(vcpu);
3806
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003807 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3808 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3809 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3810 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3811 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3812 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003813}
3814
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003815static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816{
Mathias Krause772e0312012-08-30 01:30:19 +02003817 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003818 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819
Gleb Natapovd99e4152012-12-20 16:57:45 +02003820 var.dpl = 0x3;
3821 if (seg == VCPU_SREG_CS)
3822 var.type = 0x3;
3823
3824 if (!emulate_invalid_guest_state) {
3825 var.selector = var.base >> 4;
3826 var.base = var.base & 0xffff0;
3827 var.limit = 0xffff;
3828 var.g = 0;
3829 var.db = 0;
3830 var.present = 1;
3831 var.s = 1;
3832 var.l = 0;
3833 var.unusable = 0;
3834 var.type = 0x3;
3835 var.avl = 0;
3836 if (save->base & 0xf)
3837 printk_once(KERN_WARNING "kvm: segment base is not "
3838 "paragraph aligned when entering "
3839 "protected mode (seg=%d)", seg);
3840 }
3841
3842 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05003843 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003844 vmcs_write32(sf->limit, var.limit);
3845 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003846}
3847
3848static void enter_rmode(struct kvm_vcpu *vcpu)
3849{
3850 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003851 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003853 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3854 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3855 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3856 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3857 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003858 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3859 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003860
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003861 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862
Gleb Natapov776e58e2011-03-13 12:34:27 +02003863 /*
3864 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003865 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003866 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003867 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003868 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3869 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003870
Avi Kivity2fb92db2011-04-27 19:42:18 +03003871 vmx_segment_cache_clear(vmx);
3872
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003873 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003874 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3876
3877 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003878 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003879
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003880 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881
3882 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003883 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003884 update_exception_bitmap(vcpu);
3885
Gleb Natapovd99e4152012-12-20 16:57:45 +02003886 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3887 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3888 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3889 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3890 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3891 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003892
Eddie Dong8668a3c2007-10-10 14:26:45 +08003893 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003894}
3895
Amit Shah401d10d2009-02-20 22:53:37 +05303896static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3897{
3898 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003899 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3900
3901 if (!msr)
3902 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303903
Avi Kivity44ea2b12009-09-06 15:55:37 +03003904 /*
3905 * Force kernel_gs_base reloading before EFER changes, as control
3906 * of this msr depends on is_long_mode().
3907 */
3908 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003909 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303910 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003911 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303912 msr->data = efer;
3913 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003914 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303915
3916 msr->data = efer & ~EFER_LME;
3917 }
3918 setup_msrs(vmx);
3919}
3920
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003921#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922
3923static void enter_lmode(struct kvm_vcpu *vcpu)
3924{
3925 u32 guest_tr_ar;
3926
Avi Kivity2fb92db2011-04-27 19:42:18 +03003927 vmx_segment_cache_clear(to_vmx(vcpu));
3928
Avi Kivity6aa8b732006-12-10 02:21:36 -08003929 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003930 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003931 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3932 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003934 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3935 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 }
Avi Kivityda38f432010-07-06 11:30:49 +03003937 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938}
3939
3940static void exit_lmode(struct kvm_vcpu *vcpu)
3941{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003942 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003943 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944}
3945
3946#endif
3947
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003948static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003949{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003950 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003951 if (enable_ept) {
3952 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3953 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003954 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003955 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003956}
3957
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003958static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3959{
3960 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3961}
3962
Jim Mattson8386ff52017-03-16 13:53:59 -07003963static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
3964{
3965 if (enable_ept)
3966 vmx_flush_tlb(vcpu);
3967}
3968
Avi Kivitye8467fd2009-12-29 18:43:06 +02003969static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3970{
3971 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3972
3973 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3974 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3975}
3976
Avi Kivityaff48ba2010-12-05 18:56:11 +02003977static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3978{
3979 if (enable_ept && is_paging(vcpu))
3980 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3981 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3982}
3983
Anthony Liguori25c4c272007-04-27 09:29:21 +03003984static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003985{
Avi Kivityfc78f512009-12-07 12:16:48 +02003986 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3987
3988 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3989 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003990}
3991
Sheng Yang14394422008-04-28 12:24:45 +08003992static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3993{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003994 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3995
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003996 if (!test_bit(VCPU_EXREG_PDPTR,
3997 (unsigned long *)&vcpu->arch.regs_dirty))
3998 return;
3999
Sheng Yang14394422008-04-28 12:24:45 +08004000 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004001 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4002 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4003 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4004 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004005 }
4006}
4007
Avi Kivity8f5d5492009-05-31 18:41:29 +03004008static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4009{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004010 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4011
Avi Kivity8f5d5492009-05-31 18:41:29 +03004012 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004013 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4014 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4015 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4016 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004017 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004018
4019 __set_bit(VCPU_EXREG_PDPTR,
4020 (unsigned long *)&vcpu->arch.regs_avail);
4021 __set_bit(VCPU_EXREG_PDPTR,
4022 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004023}
4024
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004025static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004026
4027static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4028 unsigned long cr0,
4029 struct kvm_vcpu *vcpu)
4030{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004031 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4032 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004033 if (!(cr0 & X86_CR0_PG)) {
4034 /* From paging/starting to nonpaging */
4035 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004036 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004037 (CPU_BASED_CR3_LOAD_EXITING |
4038 CPU_BASED_CR3_STORE_EXITING));
4039 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004040 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004041 } else if (!is_paging(vcpu)) {
4042 /* From nonpaging to paging */
4043 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004044 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004045 ~(CPU_BASED_CR3_LOAD_EXITING |
4046 CPU_BASED_CR3_STORE_EXITING));
4047 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004048 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004049 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004050
4051 if (!(cr0 & X86_CR0_WP))
4052 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004053}
4054
Avi Kivity6aa8b732006-12-10 02:21:36 -08004055static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4056{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004057 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004058 unsigned long hw_cr0;
4059
Gleb Natapov50378782013-02-04 16:00:28 +02004060 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004061 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004062 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004063 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004064 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004065
Gleb Natapov218e7632013-01-21 15:36:45 +02004066 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4067 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068
Gleb Natapov218e7632013-01-21 15:36:45 +02004069 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4070 enter_rmode(vcpu);
4071 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004073#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004074 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004075 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004077 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 exit_lmode(vcpu);
4079 }
4080#endif
4081
Avi Kivity089d0342009-03-23 18:26:32 +02004082 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004083 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4084
Avi Kivity02daab22009-12-30 12:40:26 +02004085 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02004086 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02004087
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004089 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004090 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004091
4092 /* depends on vcpu->arch.cr0 to be set to a new value */
4093 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004094}
4095
Sheng Yang14394422008-04-28 12:24:45 +08004096static u64 construct_eptp(unsigned long root_hpa)
4097{
4098 u64 eptp;
4099
4100 /* TODO write the value reading from MSR */
4101 eptp = VMX_EPT_DEFAULT_MT |
4102 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004103 if (enable_ept_ad_bits)
4104 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004105 eptp |= (root_hpa & PAGE_MASK);
4106
4107 return eptp;
4108}
4109
Avi Kivity6aa8b732006-12-10 02:21:36 -08004110static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4111{
Sheng Yang14394422008-04-28 12:24:45 +08004112 unsigned long guest_cr3;
4113 u64 eptp;
4114
4115 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004116 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004117 eptp = construct_eptp(cr3);
4118 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004119 if (is_paging(vcpu) || is_guest_mode(vcpu))
4120 guest_cr3 = kvm_read_cr3(vcpu);
4121 else
4122 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02004123 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004124 }
4125
Sheng Yang2384d2b2008-01-17 15:14:33 +08004126 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004127 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128}
4129
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004130static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004132 /*
4133 * Pass through host's Machine Check Enable value to hw_cr4, which
4134 * is in force while we are in guest mode. Do not let guests control
4135 * this bit, even if host CR4.MCE == 0.
4136 */
4137 unsigned long hw_cr4 =
4138 (cr4_read_shadow() & X86_CR4_MCE) |
4139 (cr4 & ~X86_CR4_MCE) |
4140 (to_vmx(vcpu)->rmode.vm86_active ?
4141 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004142
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004143 if (cr4 & X86_CR4_VMXE) {
4144 /*
4145 * To use VMXON (and later other VMX instructions), a guest
4146 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4147 * So basically the check on whether to allow nested VMX
4148 * is here.
4149 */
4150 if (!nested_vmx_allowed(vcpu))
4151 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004152 }
4153 if (to_vmx(vcpu)->nested.vmxon &&
4154 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004155 return 1;
4156
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004157 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004158 if (enable_ept) {
4159 if (!is_paging(vcpu)) {
4160 hw_cr4 &= ~X86_CR4_PAE;
4161 hw_cr4 |= X86_CR4_PSE;
4162 } else if (!(cr4 & X86_CR4_PAE)) {
4163 hw_cr4 &= ~X86_CR4_PAE;
4164 }
4165 }
Sheng Yang14394422008-04-28 12:24:45 +08004166
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004167 if (!enable_unrestricted_guest && !is_paging(vcpu))
4168 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004169 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4170 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4171 * to be manually disabled when guest switches to non-paging
4172 * mode.
4173 *
4174 * If !enable_unrestricted_guest, the CPU is always running
4175 * with CR0.PG=1 and CR4 needs to be modified.
4176 * If enable_unrestricted_guest, the CPU automatically
4177 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004178 */
Huaitong Handdba2622016-03-22 16:51:15 +08004179 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004180
Sheng Yang14394422008-04-28 12:24:45 +08004181 vmcs_writel(CR4_READ_SHADOW, cr4);
4182 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004183 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184}
4185
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186static void vmx_get_segment(struct kvm_vcpu *vcpu,
4187 struct kvm_segment *var, int seg)
4188{
Avi Kivitya9179492011-01-03 14:28:52 +02004189 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190 u32 ar;
4191
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004192 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004193 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004194 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004195 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004196 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004197 var->base = vmx_read_guest_seg_base(vmx, seg);
4198 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4199 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004200 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004201 var->base = vmx_read_guest_seg_base(vmx, seg);
4202 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4203 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4204 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004205 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004206 var->type = ar & 15;
4207 var->s = (ar >> 4) & 1;
4208 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004209 /*
4210 * Some userspaces do not preserve unusable property. Since usable
4211 * segment has to be present according to VMX spec we can use present
4212 * property to amend userspace bug by making unusable segment always
4213 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4214 * segment as unusable.
4215 */
4216 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004217 var->avl = (ar >> 12) & 1;
4218 var->l = (ar >> 13) & 1;
4219 var->db = (ar >> 14) & 1;
4220 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221}
4222
Avi Kivitya9179492011-01-03 14:28:52 +02004223static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4224{
Avi Kivitya9179492011-01-03 14:28:52 +02004225 struct kvm_segment s;
4226
4227 if (to_vmx(vcpu)->rmode.vm86_active) {
4228 vmx_get_segment(vcpu, &s, seg);
4229 return s.base;
4230 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004231 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004232}
4233
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004234static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004235{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004236 struct vcpu_vmx *vmx = to_vmx(vcpu);
4237
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004238 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004239 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004240 else {
4241 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004242 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004243 }
Avi Kivity69c73022011-03-07 15:26:44 +02004244}
4245
Avi Kivity653e3102007-05-07 10:55:37 +03004246static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004247{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004248 u32 ar;
4249
Avi Kivityf0495f92012-06-07 17:06:10 +03004250 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004251 ar = 1 << 16;
4252 else {
4253 ar = var->type & 15;
4254 ar |= (var->s & 1) << 4;
4255 ar |= (var->dpl & 3) << 5;
4256 ar |= (var->present & 1) << 7;
4257 ar |= (var->avl & 1) << 12;
4258 ar |= (var->l & 1) << 13;
4259 ar |= (var->db & 1) << 14;
4260 ar |= (var->g & 1) << 15;
4261 }
Avi Kivity653e3102007-05-07 10:55:37 +03004262
4263 return ar;
4264}
4265
4266static void vmx_set_segment(struct kvm_vcpu *vcpu,
4267 struct kvm_segment *var, int seg)
4268{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004269 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004270 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004271
Avi Kivity2fb92db2011-04-27 19:42:18 +03004272 vmx_segment_cache_clear(vmx);
4273
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004274 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4275 vmx->rmode.segs[seg] = *var;
4276 if (seg == VCPU_SREG_TR)
4277 vmcs_write16(sf->selector, var->selector);
4278 else if (var->s)
4279 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004280 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004281 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004282
Avi Kivity653e3102007-05-07 10:55:37 +03004283 vmcs_writel(sf->base, var->base);
4284 vmcs_write32(sf->limit, var->limit);
4285 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004286
4287 /*
4288 * Fix the "Accessed" bit in AR field of segment registers for older
4289 * qemu binaries.
4290 * IA32 arch specifies that at the time of processor reset the
4291 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004292 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004293 * state vmexit when "unrestricted guest" mode is turned on.
4294 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4295 * tree. Newer qemu binaries with that qemu fix would not need this
4296 * kvm hack.
4297 */
4298 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004299 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004300
Gleb Natapovf924d662012-12-12 19:10:55 +02004301 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004302
4303out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004304 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004305}
4306
Avi Kivity6aa8b732006-12-10 02:21:36 -08004307static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4308{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004309 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004310
4311 *db = (ar >> 14) & 1;
4312 *l = (ar >> 13) & 1;
4313}
4314
Gleb Natapov89a27f42010-02-16 10:51:48 +02004315static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004317 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4318 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319}
4320
Gleb Natapov89a27f42010-02-16 10:51:48 +02004321static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004323 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4324 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325}
4326
Gleb Natapov89a27f42010-02-16 10:51:48 +02004327static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004328{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004329 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4330 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004331}
4332
Gleb Natapov89a27f42010-02-16 10:51:48 +02004333static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004334{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004335 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4336 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004337}
4338
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004339static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4340{
4341 struct kvm_segment var;
4342 u32 ar;
4343
4344 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004345 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004346 if (seg == VCPU_SREG_CS)
4347 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004348 ar = vmx_segment_access_rights(&var);
4349
4350 if (var.base != (var.selector << 4))
4351 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004352 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004353 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004354 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004355 return false;
4356
4357 return true;
4358}
4359
4360static bool code_segment_valid(struct kvm_vcpu *vcpu)
4361{
4362 struct kvm_segment cs;
4363 unsigned int cs_rpl;
4364
4365 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004366 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004367
Avi Kivity1872a3f2009-01-04 23:26:52 +02004368 if (cs.unusable)
4369 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004370 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004371 return false;
4372 if (!cs.s)
4373 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004374 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004375 if (cs.dpl > cs_rpl)
4376 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004377 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004378 if (cs.dpl != cs_rpl)
4379 return false;
4380 }
4381 if (!cs.present)
4382 return false;
4383
4384 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4385 return true;
4386}
4387
4388static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4389{
4390 struct kvm_segment ss;
4391 unsigned int ss_rpl;
4392
4393 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004394 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004395
Avi Kivity1872a3f2009-01-04 23:26:52 +02004396 if (ss.unusable)
4397 return true;
4398 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004399 return false;
4400 if (!ss.s)
4401 return false;
4402 if (ss.dpl != ss_rpl) /* DPL != RPL */
4403 return false;
4404 if (!ss.present)
4405 return false;
4406
4407 return true;
4408}
4409
4410static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4411{
4412 struct kvm_segment var;
4413 unsigned int rpl;
4414
4415 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004416 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004417
Avi Kivity1872a3f2009-01-04 23:26:52 +02004418 if (var.unusable)
4419 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004420 if (!var.s)
4421 return false;
4422 if (!var.present)
4423 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004424 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004425 if (var.dpl < rpl) /* DPL < RPL */
4426 return false;
4427 }
4428
4429 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4430 * rights flags
4431 */
4432 return true;
4433}
4434
4435static bool tr_valid(struct kvm_vcpu *vcpu)
4436{
4437 struct kvm_segment tr;
4438
4439 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4440
Avi Kivity1872a3f2009-01-04 23:26:52 +02004441 if (tr.unusable)
4442 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004443 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004444 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004445 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004446 return false;
4447 if (!tr.present)
4448 return false;
4449
4450 return true;
4451}
4452
4453static bool ldtr_valid(struct kvm_vcpu *vcpu)
4454{
4455 struct kvm_segment ldtr;
4456
4457 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4458
Avi Kivity1872a3f2009-01-04 23:26:52 +02004459 if (ldtr.unusable)
4460 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004461 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004462 return false;
4463 if (ldtr.type != 2)
4464 return false;
4465 if (!ldtr.present)
4466 return false;
4467
4468 return true;
4469}
4470
4471static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4472{
4473 struct kvm_segment cs, ss;
4474
4475 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4476 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4477
Nadav Amitb32a9912015-03-29 16:33:04 +03004478 return ((cs.selector & SEGMENT_RPL_MASK) ==
4479 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004480}
4481
4482/*
4483 * Check if guest state is valid. Returns true if valid, false if
4484 * not.
4485 * We assume that registers are always usable
4486 */
4487static bool guest_state_valid(struct kvm_vcpu *vcpu)
4488{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004489 if (enable_unrestricted_guest)
4490 return true;
4491
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004492 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004493 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004494 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4495 return false;
4496 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4497 return false;
4498 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4499 return false;
4500 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4501 return false;
4502 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4503 return false;
4504 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4505 return false;
4506 } else {
4507 /* protected mode guest state checks */
4508 if (!cs_ss_rpl_check(vcpu))
4509 return false;
4510 if (!code_segment_valid(vcpu))
4511 return false;
4512 if (!stack_segment_valid(vcpu))
4513 return false;
4514 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4515 return false;
4516 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4517 return false;
4518 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4519 return false;
4520 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4521 return false;
4522 if (!tr_valid(vcpu))
4523 return false;
4524 if (!ldtr_valid(vcpu))
4525 return false;
4526 }
4527 /* TODO:
4528 * - Add checks on RIP
4529 * - Add checks on RFLAGS
4530 */
4531
4532 return true;
4533}
4534
Mike Dayd77c26f2007-10-08 09:02:08 -04004535static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004536{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004537 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004538 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004539 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004540
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004541 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004542 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004543 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4544 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004545 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004546 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004547 r = kvm_write_guest_page(kvm, fn++, &data,
4548 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004549 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004550 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004551 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4552 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004553 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004554 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4555 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004556 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004557 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004558 r = kvm_write_guest_page(kvm, fn, &data,
4559 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4560 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004561out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004562 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004563 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564}
4565
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004566static int init_rmode_identity_map(struct kvm *kvm)
4567{
Tang Chenf51770e2014-09-16 18:41:59 +08004568 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004569 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004570 u32 tmp;
4571
Avi Kivity089d0342009-03-23 18:26:32 +02004572 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004573 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004574
4575 /* Protect kvm->arch.ept_identity_pagetable_done. */
4576 mutex_lock(&kvm->slots_lock);
4577
Tang Chenf51770e2014-09-16 18:41:59 +08004578 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004579 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004580
Sheng Yangb927a3c2009-07-21 10:42:48 +08004581 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004582
4583 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004584 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004585 goto out2;
4586
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004587 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004588 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4589 if (r < 0)
4590 goto out;
4591 /* Set up identity-mapping pagetable for EPT in real mode */
4592 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4593 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4594 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4595 r = kvm_write_guest_page(kvm, identity_map_pfn,
4596 &tmp, i * sizeof(tmp), sizeof(tmp));
4597 if (r < 0)
4598 goto out;
4599 }
4600 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004601
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004602out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004603 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004604
4605out2:
4606 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004607 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004608}
4609
Avi Kivity6aa8b732006-12-10 02:21:36 -08004610static void seg_setup(int seg)
4611{
Mathias Krause772e0312012-08-30 01:30:19 +02004612 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004613 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614
4615 vmcs_write16(sf->selector, 0);
4616 vmcs_writel(sf->base, 0);
4617 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004618 ar = 0x93;
4619 if (seg == VCPU_SREG_CS)
4620 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004621
4622 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004623}
4624
Sheng Yangf78e0e22007-10-29 09:40:42 +08004625static int alloc_apic_access_page(struct kvm *kvm)
4626{
Xiao Guangrong44841412012-09-07 14:14:20 +08004627 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004628 int r = 0;
4629
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004630 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004631 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004632 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004633 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4634 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004635 if (r)
4636 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004637
Tang Chen73a6d942014-09-11 13:38:00 +08004638 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004639 if (is_error_page(page)) {
4640 r = -EFAULT;
4641 goto out;
4642 }
4643
Tang Chenc24ae0d2014-09-24 15:57:58 +08004644 /*
4645 * Do not pin the page in memory, so that memory hot-unplug
4646 * is able to migrate it.
4647 */
4648 put_page(page);
4649 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004650out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004651 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004652 return r;
4653}
4654
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004655static int alloc_identity_pagetable(struct kvm *kvm)
4656{
Tang Chena255d472014-09-16 18:41:58 +08004657 /* Called with kvm->slots_lock held. */
4658
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004659 int r = 0;
4660
Tang Chena255d472014-09-16 18:41:58 +08004661 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4662
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004663 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4664 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004665
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004666 return r;
4667}
4668
Wanpeng Li991e7a02015-09-16 17:30:05 +08004669static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004670{
4671 int vpid;
4672
Avi Kivity919818a2009-03-23 18:01:29 +02004673 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004674 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004675 spin_lock(&vmx_vpid_lock);
4676 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004677 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004678 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004679 else
4680 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004681 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004682 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004683}
4684
Wanpeng Li991e7a02015-09-16 17:30:05 +08004685static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004686{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004687 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004688 return;
4689 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004690 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004691 spin_unlock(&vmx_vpid_lock);
4692}
4693
Paolo Bonzini6236b782018-01-16 16:51:18 +01004694static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4695 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004696{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004697 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004698
4699 if (!cpu_has_vmx_msr_bitmap())
4700 return;
4701
4702 /*
4703 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4704 * have the write-low and read-high bitmap offsets the wrong way round.
4705 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4706 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004707 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004708 if (type & MSR_TYPE_R)
4709 /* read-low */
4710 __clear_bit(msr, msr_bitmap + 0x000 / f);
4711
4712 if (type & MSR_TYPE_W)
4713 /* write-low */
4714 __clear_bit(msr, msr_bitmap + 0x800 / f);
4715
Sheng Yang25c5f222008-03-28 13:18:56 +08004716 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4717 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004718 if (type & MSR_TYPE_R)
4719 /* read-high */
4720 __clear_bit(msr, msr_bitmap + 0x400 / f);
4721
4722 if (type & MSR_TYPE_W)
4723 /* write-high */
4724 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4725
4726 }
4727}
4728
Paolo Bonzini6236b782018-01-16 16:51:18 +01004729static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4730 u32 msr, int type)
Yang Zhang8d146952013-01-25 10:18:50 +08004731{
4732 int f = sizeof(unsigned long);
4733
4734 if (!cpu_has_vmx_msr_bitmap())
4735 return;
4736
4737 /*
4738 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4739 * have the write-low and read-high bitmap offsets the wrong way round.
4740 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4741 */
4742 if (msr <= 0x1fff) {
4743 if (type & MSR_TYPE_R)
4744 /* read-low */
4745 __set_bit(msr, msr_bitmap + 0x000 / f);
4746
4747 if (type & MSR_TYPE_W)
4748 /* write-low */
4749 __set_bit(msr, msr_bitmap + 0x800 / f);
4750
4751 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4752 msr &= 0x1fff;
4753 if (type & MSR_TYPE_R)
4754 /* read-high */
4755 __set_bit(msr, msr_bitmap + 0x400 / f);
4756
4757 if (type & MSR_TYPE_W)
4758 /* write-high */
4759 __set_bit(msr, msr_bitmap + 0xc00 / f);
4760
Sheng Yang25c5f222008-03-28 13:18:56 +08004761 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004762}
4763
Paolo Bonzini6236b782018-01-16 16:51:18 +01004764static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4765 u32 msr, int type, bool value)
4766{
4767 if (value)
4768 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4769 else
4770 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4771}
4772
Wincy Vanf2b93282015-02-03 23:56:03 +08004773/*
4774 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4775 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4776 */
4777static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4778 unsigned long *msr_bitmap_nested,
4779 u32 msr, int type)
4780{
4781 int f = sizeof(unsigned long);
4782
4783 if (!cpu_has_vmx_msr_bitmap()) {
4784 WARN_ON(1);
4785 return;
4786 }
4787
4788 /*
4789 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4790 * have the write-low and read-high bitmap offsets the wrong way round.
4791 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4792 */
4793 if (msr <= 0x1fff) {
4794 if (type & MSR_TYPE_R &&
4795 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4796 /* read-low */
4797 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4798
4799 if (type & MSR_TYPE_W &&
4800 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4801 /* write-low */
4802 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4803
4804 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4805 msr &= 0x1fff;
4806 if (type & MSR_TYPE_R &&
4807 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4808 /* read-high */
4809 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4810
4811 if (type & MSR_TYPE_W &&
4812 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4813 /* write-high */
4814 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4815
4816 }
4817}
4818
Paolo Bonzini6236b782018-01-16 16:51:18 +01004819static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02004820{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004821 u8 mode = 0;
4822
4823 if (cpu_has_secondary_exec_ctrls() &&
4824 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
4825 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4826 mode |= MSR_BITMAP_MODE_X2APIC;
4827 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4828 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4829 }
4830
4831 if (is_long_mode(vcpu))
4832 mode |= MSR_BITMAP_MODE_LM;
4833
4834 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004835}
4836
Paolo Bonzini6236b782018-01-16 16:51:18 +01004837#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
4838
4839static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4840 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08004841{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004842 int msr;
4843
4844 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4845 unsigned word = msr / BITS_PER_LONG;
4846 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4847 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4848 }
4849
4850 if (mode & MSR_BITMAP_MODE_X2APIC) {
4851 /*
4852 * TPR reads and writes can be virtualized even if virtual interrupt
4853 * delivery is not in use.
4854 */
4855 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4856 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4857 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4858 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4859 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4860 }
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004861 }
Yang Zhang8d146952013-01-25 10:18:50 +08004862}
4863
Paolo Bonzini6236b782018-01-16 16:51:18 +01004864static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +08004865{
Paolo Bonzini6236b782018-01-16 16:51:18 +01004866 struct vcpu_vmx *vmx = to_vmx(vcpu);
4867 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4868 u8 mode = vmx_msr_bitmap_mode(vcpu);
4869 u8 changed = mode ^ vmx->msr_bitmap_mode;
Yang Zhang8d146952013-01-25 10:18:50 +08004870
Paolo Bonzini6236b782018-01-16 16:51:18 +01004871 if (!changed)
4872 return;
4873
4874 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
4875 !(mode & MSR_BITMAP_MODE_LM));
4876
4877 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4878 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4879
4880 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02004881}
4882
Andrey Smetanind62caab2015-11-10 15:36:33 +03004883static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004884{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004885 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004886}
4887
David Matlackb7649e12017-08-01 14:00:40 -07004888static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
4889{
4890 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4891 gfn_t gfn;
4892
4893 /*
4894 * Don't need to mark the APIC access page dirty; it is never
4895 * written to by the CPU during APIC virtualization.
4896 */
4897
4898 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
4899 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
4900 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4901 }
4902
4903 if (nested_cpu_has_posted_intr(vmcs12)) {
4904 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
4905 kvm_vcpu_mark_page_dirty(vcpu, gfn);
4906 }
4907}
4908
4909
David Hildenbrand1edccf22017-01-25 11:58:58 +01004910static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08004911{
4912 struct vcpu_vmx *vmx = to_vmx(vcpu);
4913 int max_irr;
4914 void *vapic_page;
4915 u16 status;
4916
David Matlackb7649e12017-08-01 14:00:40 -07004917 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
4918 return;
Wincy Van705699a2015-02-03 23:58:17 +08004919
David Matlackb7649e12017-08-01 14:00:40 -07004920 vmx->nested.pi_pending = false;
4921 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4922 return;
Wincy Van705699a2015-02-03 23:58:17 +08004923
David Matlackb7649e12017-08-01 14:00:40 -07004924 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
4925 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08004926 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08004927 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4928 kunmap(vmx->nested.virtual_apic_page);
4929
4930 status = vmcs_read16(GUEST_INTR_STATUS);
4931 if ((u8)max_irr > ((u8)status & 0xff)) {
4932 status &= ~0xff;
4933 status |= (u8)max_irr;
4934 vmcs_write16(GUEST_INTR_STATUS, status);
4935 }
4936 }
David Matlackb7649e12017-08-01 14:00:40 -07004937
4938 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004939}
4940
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004941static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4942{
4943#ifdef CONFIG_SMP
4944 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004945 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08004946 * The vector of interrupt to be delivered to vcpu had
4947 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08004948 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08004949 * Following cases will be reached in this block, and
4950 * we always send a notification event in all cases as
4951 * explained below.
4952 *
4953 * Case 1: vcpu keeps in non-root mode. Sending a
4954 * notification event posts the interrupt to vcpu.
4955 *
4956 * Case 2: vcpu exits to root mode and is still
4957 * runnable. PIR will be synced to vIRR before the
4958 * next vcpu entry. Sending a notification event in
4959 * this case has no effect, as vcpu is not in root
4960 * mode.
4961 *
4962 * Case 3: vcpu exits to root mode and is blocked.
4963 * vcpu_block() has already synced PIR to vIRR and
4964 * never blocks vcpu if vIRR is not cleared. Therefore,
4965 * a blocked vcpu here does not wait for any requested
4966 * interrupts in PIR, and sending a notification event
4967 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08004968 */
Feng Wu28b835d2015-09-18 22:29:54 +08004969
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004970 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4971 POSTED_INTR_VECTOR);
4972 return true;
4973 }
4974#endif
4975 return false;
4976}
4977
Wincy Van705699a2015-02-03 23:58:17 +08004978static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4979 int vector)
4980{
4981 struct vcpu_vmx *vmx = to_vmx(vcpu);
4982
4983 if (is_guest_mode(vcpu) &&
4984 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08004985 /*
4986 * If a posted intr is not recognized by hardware,
4987 * we will accomplish it in the next vmentry.
4988 */
4989 vmx->nested.pi_pending = true;
4990 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonba882892017-11-09 20:27:20 +02004991 /* the PIR and ON have been set by L1. */
4992 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
4993 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004994 return 0;
4995 }
4996 return -1;
4997}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004999 * Send interrupt to vcpu via posted interrupt way.
5000 * 1. If target vcpu is running(non-root mode), send posted interrupt
5001 * notification to vcpu and hardware will sync PIR to vIRR atomically.
5002 * 2. If target vcpu isn't running(root mode), kick it to pick up the
5003 * interrupt from PIR in next vmentry.
5004 */
5005static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
5006{
5007 struct vcpu_vmx *vmx = to_vmx(vcpu);
5008 int r;
5009
Wincy Van705699a2015-02-03 23:58:17 +08005010 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
5011 if (!r)
5012 return;
5013
Yang Zhanga20ed542013-04-11 19:25:15 +08005014 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
5015 return;
5016
5017 r = pi_test_and_set_on(&vmx->pi_desc);
5018 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01005019 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005020 kvm_vcpu_kick(vcpu);
5021}
5022
5023static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
5024{
5025 struct vcpu_vmx *vmx = to_vmx(vcpu);
5026
5027 if (!pi_test_and_clear_on(&vmx->pi_desc))
5028 return;
5029
5030 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
5031}
5032
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005034 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5035 * will not change in the lifetime of the guest.
5036 * Note that host-state that does change is set elsewhere. E.g., host-state
5037 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5038 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005039static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005040{
5041 u32 low32, high32;
5042 unsigned long tmpl;
5043 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005044 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005045
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07005046 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005047 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5048
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005049 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005050 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005051 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5052 vmx->host_state.vmcs_host_cr4 = cr4;
5053
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005054 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005055#ifdef CONFIG_X86_64
5056 /*
5057 * Load null selectors, so we can avoid reloading them in
5058 * __vmx_load_host_state(), in case userspace uses the null selectors
5059 * too (the expected case).
5060 */
5061 vmcs_write16(HOST_DS_SELECTOR, 0);
5062 vmcs_write16(HOST_ES_SELECTOR, 0);
5063#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005064 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5065 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005066#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005067 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5068 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5069
5070 native_store_idt(&dt);
5071 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005072 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005073
Avi Kivity83287ea422012-09-16 15:10:57 +03005074 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005075
5076 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5077 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5078 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5079 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5080
5081 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5082 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5083 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5084 }
5085}
5086
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005087static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5088{
5089 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5090 if (enable_ept)
5091 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005092 if (is_guest_mode(&vmx->vcpu))
5093 vmx->vcpu.arch.cr4_guest_owned_bits &=
5094 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005095 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5096}
5097
Yang Zhang01e439b2013-04-11 19:25:12 +08005098static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5099{
5100 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5101
Andrey Smetanind62caab2015-11-10 15:36:33 +03005102 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005103 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005104 /* Enable the preemption timer dynamically */
5105 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005106 return pin_based_exec_ctrl;
5107}
5108
Andrey Smetanind62caab2015-11-10 15:36:33 +03005109static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5110{
5111 struct vcpu_vmx *vmx = to_vmx(vcpu);
5112
5113 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005114 if (cpu_has_secondary_exec_ctrls()) {
5115 if (kvm_vcpu_apicv_active(vcpu))
5116 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5117 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5118 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5119 else
5120 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5121 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5122 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5123 }
5124
5125 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005126 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005127}
5128
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005129static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5130{
5131 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005132
5133 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5134 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5135
Paolo Bonzini35754c92015-07-29 12:05:37 +02005136 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005137 exec_control &= ~CPU_BASED_TPR_SHADOW;
5138#ifdef CONFIG_X86_64
5139 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5140 CPU_BASED_CR8_LOAD_EXITING;
5141#endif
5142 }
5143 if (!enable_ept)
5144 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5145 CPU_BASED_CR3_LOAD_EXITING |
5146 CPU_BASED_INVLPG_EXITING;
5147 return exec_control;
5148}
5149
5150static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5151{
5152 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005153 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005154 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5155 if (vmx->vpid == 0)
5156 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5157 if (!enable_ept) {
5158 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5159 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005160 /* Enable INVPCID for non-ept guests may cause performance regression. */
5161 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005162 }
5163 if (!enable_unrestricted_guest)
5164 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5165 if (!ple_gap)
5166 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005167 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005168 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5169 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005170 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005171 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5172 (handle_vmptrld).
5173 We can NOT enable shadow_vmcs here because we don't have yet
5174 a current VMCS12
5175 */
5176 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005177
5178 if (!enable_pml)
5179 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005180
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005181 return exec_control;
5182}
5183
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005184static void ept_set_mmio_spte_mask(void)
5185{
5186 /*
5187 * EPT Misconfigurations can be generated if the value of bits 2:0
5188 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005189 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005190 * spte.
5191 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005192 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005193}
5194
Wanpeng Lif53cd632014-12-02 19:14:58 +08005195#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005196/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005197 * Sets up the vmcs for emulated real mode.
5198 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005199static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005200{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005201#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005202 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005203#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005204 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005205
Avi Kivity6aa8b732006-12-10 02:21:36 -08005206 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005207 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5208 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005209
Abel Gordon4607c2d2013-04-18 14:35:55 +03005210 if (enable_shadow_vmcs) {
5211 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5212 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5213 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005214 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +01005215 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08005216
Avi Kivity6aa8b732006-12-10 02:21:36 -08005217 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5218
Avi Kivity6aa8b732006-12-10 02:21:36 -08005219 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005220 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005221 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005222
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005223 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005224
Dan Williamsdfa169b2016-06-02 11:17:24 -07005225 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005226 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5227 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005228 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005229
Andrey Smetanind62caab2015-11-10 15:36:33 +03005230 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005231 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5232 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5233 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5234 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5235
5236 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005237
Li RongQing0bcf2612015-12-03 13:29:34 +08005238 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005239 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005240 }
5241
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005242 if (ple_gap) {
5243 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005244 vmx->ple_window = ple_window;
5245 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005246 }
5247
Xiao Guangrongc3707952011-07-12 03:28:04 +08005248 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5249 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005250 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5251
Avi Kivity9581d442010-10-19 16:46:55 +02005252 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5253 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005254 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005255#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005256 rdmsrl(MSR_FS_BASE, a);
5257 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5258 rdmsrl(MSR_GS_BASE, a);
5259 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5260#else
5261 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5262 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5263#endif
5264
Eddie Dong2cc51562007-05-21 07:28:09 +03005265 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5266 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005267 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005268 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005269 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005270
Radim Krčmář74545702015-04-27 15:11:25 +02005271 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5272 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005273
Paolo Bonzini03916db2014-07-24 14:21:57 +02005274 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005275 u32 index = vmx_msr_index[i];
5276 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005277 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005278
5279 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5280 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005281 if (wrmsr_safe(index, data_low, data_high) < 0)
5282 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005283 vmx->guest_msrs[j].index = i;
5284 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005285 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005286 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005287 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005288
KarimAllah Ahmed755502f2018-02-01 22:59:44 +01005289 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
5290 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities);
Gleb Natapov2961e8762013-11-25 15:37:13 +02005291
5292 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005293
5294 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005295 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005296
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005297 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005298 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005299
Wanpeng Lif53cd632014-12-02 19:14:58 +08005300 if (vmx_xsaves_supported())
5301 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5302
Peter Feiner4e595162016-07-07 14:49:58 -07005303 if (enable_pml) {
5304 ASSERT(vmx->pml_pg);
5305 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5306 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5307 }
5308
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005309 return 0;
5310}
5311
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005312static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005313{
5314 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005315 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005316 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005317
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005318 vmx->rmode.vm86_active = 0;
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01005319 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005320
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005321 vmx->soft_vnmi_blocked = 0;
5322
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005323 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005324 kvm_set_cr8(vcpu, 0);
5325
5326 if (!init_event) {
5327 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5328 MSR_IA32_APICBASE_ENABLE;
5329 if (kvm_vcpu_is_reset_bsp(vcpu))
5330 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5331 apic_base_msr.host_initiated = true;
5332 kvm_set_apic_base(vcpu, &apic_base_msr);
5333 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005334
Avi Kivity2fb92db2011-04-27 19:42:18 +03005335 vmx_segment_cache_clear(vmx);
5336
Avi Kivity5706be02008-08-20 15:07:31 +03005337 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005338 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005339 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005340
5341 seg_setup(VCPU_SREG_DS);
5342 seg_setup(VCPU_SREG_ES);
5343 seg_setup(VCPU_SREG_FS);
5344 seg_setup(VCPU_SREG_GS);
5345 seg_setup(VCPU_SREG_SS);
5346
5347 vmcs_write16(GUEST_TR_SELECTOR, 0);
5348 vmcs_writel(GUEST_TR_BASE, 0);
5349 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5350 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5351
5352 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5353 vmcs_writel(GUEST_LDTR_BASE, 0);
5354 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5355 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5356
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005357 if (!init_event) {
5358 vmcs_write32(GUEST_SYSENTER_CS, 0);
5359 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5360 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5361 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5362 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005363
Wanpeng Li5c0b19b2017-11-20 14:52:21 -08005364 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01005365 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005366
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005367 vmcs_writel(GUEST_GDTR_BASE, 0);
5368 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5369
5370 vmcs_writel(GUEST_IDTR_BASE, 0);
5371 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5372
Anthony Liguori443381a2010-12-06 10:53:38 -06005373 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005374 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005375 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005376
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005377 setup_msrs(vmx);
5378
Avi Kivity6aa8b732006-12-10 02:21:36 -08005379 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5380
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005381 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005382 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005383 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005384 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005385 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005386 vmcs_write32(TPR_THRESHOLD, 0);
5387 }
5388
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005389 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005390
Andrey Smetanind62caab2015-11-10 15:36:33 +03005391 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005392 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5393
Sheng Yang2384d2b2008-01-17 15:14:33 +08005394 if (vmx->vpid != 0)
5395 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5396
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005397 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005398 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005399 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005400 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005401 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005402 vmx_fpu_activate(vcpu);
5403 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005404
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005405 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005406}
5407
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005408/*
5409 * In nested virtualization, check if L1 asked to exit on external interrupts.
5410 * For most existing hypervisors, this will always return true.
5411 */
5412static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5413{
5414 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5415 PIN_BASED_EXT_INTR_MASK;
5416}
5417
Bandan Das77b0f5d2014-04-19 18:17:45 -04005418/*
5419 * In nested virtualization, check if L1 has set
5420 * VM_EXIT_ACK_INTR_ON_EXIT
5421 */
5422static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5423{
5424 return get_vmcs12(vcpu)->vm_exit_controls &
5425 VM_EXIT_ACK_INTR_ON_EXIT;
5426}
5427
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005428static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5429{
5430 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5431 PIN_BASED_NMI_EXITING;
5432}
5433
Jan Kiszkac9a79532014-03-07 20:03:15 +01005434static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005435{
5436 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005437
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005438 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5439 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5440 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5441}
5442
Jan Kiszkac9a79532014-03-07 20:03:15 +01005443static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005444{
5445 u32 cpu_based_vm_exec_control;
5446
Jan Kiszkac9a79532014-03-07 20:03:15 +01005447 if (!cpu_has_virtual_nmis() ||
5448 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5449 enable_irq_window(vcpu);
5450 return;
5451 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005452
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005453 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5454 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5455 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5456}
5457
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005458static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005459{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005460 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005461 uint32_t intr;
5462 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005463
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005464 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005465
Avi Kivityfa89a812008-09-01 15:57:51 +03005466 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005467 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005468 int inc_eip = 0;
5469 if (vcpu->arch.interrupt.soft)
5470 inc_eip = vcpu->arch.event_exit_inst_len;
5471 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005472 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005473 return;
5474 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005475 intr = irq | INTR_INFO_VALID_MASK;
5476 if (vcpu->arch.interrupt.soft) {
5477 intr |= INTR_TYPE_SOFT_INTR;
5478 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5479 vmx->vcpu.arch.event_exit_inst_len);
5480 } else
5481 intr |= INTR_TYPE_EXT_INTR;
5482 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005483}
5484
Sheng Yangf08864b2008-05-15 18:23:25 +08005485static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5486{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005487 struct vcpu_vmx *vmx = to_vmx(vcpu);
5488
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005489 if (!is_guest_mode(vcpu)) {
5490 if (!cpu_has_virtual_nmis()) {
5491 /*
5492 * Tracking the NMI-blocked state in software is built upon
5493 * finding the next open IRQ window. This, in turn, depends on
5494 * well-behaving guests: They have to keep IRQs disabled at
5495 * least as long as the NMI handler runs. Otherwise we may
5496 * cause NMI nesting, maybe breaking the guest. But as this is
5497 * highly unlikely, we can live with the residual risk.
5498 */
5499 vmx->soft_vnmi_blocked = 1;
5500 vmx->vnmi_blocked_time = 0;
5501 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005502
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005503 ++vcpu->stat.nmi_injections;
5504 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005505 }
5506
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005507 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005508 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005509 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005510 return;
5511 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005512
Sheng Yangf08864b2008-05-15 18:23:25 +08005513 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5514 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005515}
5516
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005517static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5518{
5519 if (!cpu_has_virtual_nmis())
5520 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005521 if (to_vmx(vcpu)->nmi_known_unmasked)
5522 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005523 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005524}
5525
5526static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5527{
5528 struct vcpu_vmx *vmx = to_vmx(vcpu);
5529
5530 if (!cpu_has_virtual_nmis()) {
5531 if (vmx->soft_vnmi_blocked != masked) {
5532 vmx->soft_vnmi_blocked = masked;
5533 vmx->vnmi_blocked_time = 0;
5534 }
5535 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005536 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005537 if (masked)
5538 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5539 GUEST_INTR_STATE_NMI);
5540 else
5541 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5542 GUEST_INTR_STATE_NMI);
5543 }
5544}
5545
Jan Kiszka2505dc92013-04-14 12:12:47 +02005546static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5547{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005548 if (to_vmx(vcpu)->nested.nested_run_pending)
5549 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005550
Jan Kiszka2505dc92013-04-14 12:12:47 +02005551 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5552 return 0;
5553
5554 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5555 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5556 | GUEST_INTR_STATE_NMI));
5557}
5558
Gleb Natapov78646122009-03-23 12:12:11 +02005559static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5560{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005561 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5562 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005563 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5564 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005565}
5566
Izik Eiduscbc94022007-10-25 00:29:55 +02005567static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5568{
5569 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005570
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005571 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5572 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005573 if (ret)
5574 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005575 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005576 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005577}
5578
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005579static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005580{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005581 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005582 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005583 /*
5584 * Update instruction length as we may reinject the exception
5585 * from user space while in guest debugging mode.
5586 */
5587 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5588 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005589 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005590 return false;
5591 /* fall through */
5592 case DB_VECTOR:
5593 if (vcpu->guest_debug &
5594 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5595 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005596 /* fall through */
5597 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005598 case OF_VECTOR:
5599 case BR_VECTOR:
5600 case UD_VECTOR:
5601 case DF_VECTOR:
5602 case SS_VECTOR:
5603 case GP_VECTOR:
5604 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005605 return true;
5606 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005607 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005608 return false;
5609}
5610
5611static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5612 int vec, u32 err_code)
5613{
5614 /*
5615 * Instruction with address size override prefix opcode 0x67
5616 * Cause the #SS fault with 0 error code in VM86 mode.
5617 */
5618 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5619 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5620 if (vcpu->arch.halt_request) {
5621 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005622 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005623 }
5624 return 1;
5625 }
5626 return 0;
5627 }
5628
5629 /*
5630 * Forward all other exceptions that are valid in real mode.
5631 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5632 * the required debugging infrastructure rework.
5633 */
5634 kvm_queue_exception(vcpu, vec);
5635 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005636}
5637
Andi Kleena0861c02009-06-08 17:37:09 +08005638/*
5639 * Trigger machine check on the host. We assume all the MSRs are already set up
5640 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5641 * We pass a fake environment to the machine check handler because we want
5642 * the guest to be always treated like user space, no matter what context
5643 * it used internally.
5644 */
5645static void kvm_machine_check(void)
5646{
5647#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5648 struct pt_regs regs = {
5649 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5650 .flags = X86_EFLAGS_IF,
5651 };
5652
5653 do_machine_check(&regs, 0);
5654#endif
5655}
5656
Avi Kivity851ba692009-08-24 11:10:17 +03005657static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005658{
5659 /* already handled by vcpu_run */
5660 return 1;
5661}
5662
Avi Kivity851ba692009-08-24 11:10:17 +03005663static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005664{
Avi Kivity1155f762007-11-22 11:30:47 +02005665 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005666 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005667 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005668 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005669 u32 vect_info;
5670 enum emulation_result er;
5671
Avi Kivity1155f762007-11-22 11:30:47 +02005672 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005673 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005674
Andi Kleena0861c02009-06-08 17:37:09 +08005675 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005676 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005677
Jim Mattson3f618a02016-12-12 11:01:37 -08005678 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005679 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005680
5681 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005682 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005683 return 1;
5684 }
5685
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005686 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005687 if (is_guest_mode(vcpu)) {
5688 kvm_queue_exception(vcpu, UD_VECTOR);
5689 return 1;
5690 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005691 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Liran Alonc0a4c222017-11-05 16:56:32 +02005692 if (er == EMULATE_USER_EXIT)
5693 return 0;
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005694 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005695 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005696 return 1;
5697 }
5698
Avi Kivity6aa8b732006-12-10 02:21:36 -08005699 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005700 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005701 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005702
5703 /*
5704 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5705 * MMIO, it is better to report an internal error.
5706 * See the comments in vmx_handle_exit.
5707 */
5708 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5709 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5710 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5711 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005712 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005713 vcpu->run->internal.data[0] = vect_info;
5714 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005715 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005716 return 0;
5717 }
5718
Avi Kivity6aa8b732006-12-10 02:21:36 -08005719 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005720 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005721 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005722 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005723 trace_kvm_page_fault(cr2, error_code);
5724
Gleb Natapov3298b752009-05-11 13:35:46 +03005725 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005726 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005727 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005728 }
5729
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005730 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005731
5732 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5733 return handle_rmode_exception(vcpu, ex_no, error_code);
5734
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005735 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005736 case AC_VECTOR:
5737 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5738 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005739 case DB_VECTOR:
5740 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5741 if (!(vcpu->guest_debug &
5742 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005743 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005744 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds587da2b2018-03-20 12:16:59 -07005745 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01005746 skip_emulated_instruction(vcpu);
5747
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005748 kvm_queue_exception(vcpu, DB_VECTOR);
5749 return 1;
5750 }
5751 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5752 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5753 /* fall through */
5754 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005755 /*
5756 * Update instruction length as we may reinject #BP from
5757 * user space while in guest debugging mode. Reading it for
5758 * #DB as well causes no harm, it is not used in that case.
5759 */
5760 vmx->vcpu.arch.event_exit_inst_len =
5761 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005762 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005763 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005764 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5765 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005766 break;
5767 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005768 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5769 kvm_run->ex.exception = ex_no;
5770 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005771 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005772 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005773 return 0;
5774}
5775
Avi Kivity851ba692009-08-24 11:10:17 +03005776static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005777{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005778 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005779 return 1;
5780}
5781
Avi Kivity851ba692009-08-24 11:10:17 +03005782static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005783{
Avi Kivity851ba692009-08-24 11:10:17 +03005784 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005785 return 0;
5786}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005787
Avi Kivity851ba692009-08-24 11:10:17 +03005788static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005789{
He, Qingbfdaab02007-09-12 14:18:28 +08005790 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005791 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005792 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005793
He, Qingbfdaab02007-09-12 14:18:28 +08005794 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005795 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005796 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005797
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005798 ++vcpu->stat.io_exits;
5799
5800 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005801 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005802
5803 port = exit_qualification >> 16;
5804 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005805 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005806
5807 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005808}
5809
Ingo Molnar102d8322007-02-19 14:37:47 +02005810static void
5811vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5812{
5813 /*
5814 * Patch in the VMCALL instruction:
5815 */
5816 hypercall[0] = 0x0f;
5817 hypercall[1] = 0x01;
5818 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005819}
5820
Wincy Vanb9c237b2015-02-03 23:56:30 +08005821static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005822{
5823 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005824 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005825
Wincy Vanb9c237b2015-02-03 23:56:30 +08005826 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005827 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5828 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5829 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5830 return (val & always_on) == always_on;
5831}
5832
Guo Chao0fa06072012-06-28 15:16:19 +08005833/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005834static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5835{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005836 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005837 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5838 unsigned long orig_val = val;
5839
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005840 /*
5841 * We get here when L2 changed cr0 in a way that did not change
5842 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005843 * but did change L0 shadowed bits. So we first calculate the
5844 * effective cr0 value that L1 would like to write into the
5845 * hardware. It consists of the L2-owned bits from the new
5846 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005847 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005848 val = (val & ~vmcs12->cr0_guest_host_mask) |
5849 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5850
Wincy Vanb9c237b2015-02-03 23:56:30 +08005851 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005852 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005853
5854 if (kvm_set_cr0(vcpu, val))
5855 return 1;
5856 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005857 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005858 } else {
5859 if (to_vmx(vcpu)->nested.vmxon &&
5860 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5861 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005862 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005863 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005864}
5865
5866static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5867{
5868 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005869 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5870 unsigned long orig_val = val;
5871
5872 /* analogously to handle_set_cr0 */
5873 val = (val & ~vmcs12->cr4_guest_host_mask) |
5874 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5875 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005876 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005877 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005878 return 0;
5879 } else
5880 return kvm_set_cr4(vcpu, val);
5881}
5882
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005883/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005884static void handle_clts(struct kvm_vcpu *vcpu)
5885{
5886 if (is_guest_mode(vcpu)) {
5887 /*
5888 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5889 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5890 * just pretend it's off (also in arch.cr0 for fpu_activate).
5891 */
5892 vmcs_writel(CR0_READ_SHADOW,
5893 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5894 vcpu->arch.cr0 &= ~X86_CR0_TS;
5895 } else
5896 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5897}
5898
Avi Kivity851ba692009-08-24 11:10:17 +03005899static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005900{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005901 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005902 int cr;
5903 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005904 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005905
He, Qingbfdaab02007-09-12 14:18:28 +08005906 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005907 cr = exit_qualification & 15;
5908 reg = (exit_qualification >> 8) & 15;
5909 switch ((exit_qualification >> 4) & 3) {
5910 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005911 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005912 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005913 switch (cr) {
5914 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005915 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005916 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005917 return 1;
5918 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005919 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005920 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005921 return 1;
5922 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005923 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005924 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005925 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005926 case 8: {
5927 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005928 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005929 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005930 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005931 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005932 return 1;
5933 if (cr8_prev <= cr8)
5934 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005935 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005936 return 0;
5937 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005938 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005939 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005940 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005941 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005942 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005943 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005944 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005945 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005946 case 1: /*mov from cr*/
5947 switch (cr) {
5948 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005949 val = kvm_read_cr3(vcpu);
5950 kvm_register_write(vcpu, reg, val);
5951 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005952 skip_emulated_instruction(vcpu);
5953 return 1;
5954 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005955 val = kvm_get_cr8(vcpu);
5956 kvm_register_write(vcpu, reg, val);
5957 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005958 skip_emulated_instruction(vcpu);
5959 return 1;
5960 }
5961 break;
5962 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005963 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005964 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005965 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005966
5967 skip_emulated_instruction(vcpu);
5968 return 1;
5969 default:
5970 break;
5971 }
Avi Kivity851ba692009-08-24 11:10:17 +03005972 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005973 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005974 (int)(exit_qualification >> 4) & 3, cr);
5975 return 0;
5976}
5977
Avi Kivity851ba692009-08-24 11:10:17 +03005978static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005979{
He, Qingbfdaab02007-09-12 14:18:28 +08005980 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005981 int dr, dr7, reg;
5982
5983 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5984 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5985
5986 /* First, if DR does not exist, trigger UD */
5987 if (!kvm_require_dr(vcpu, dr))
5988 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005989
Jan Kiszkaf2483412010-01-20 18:20:20 +01005990 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005991 if (!kvm_require_cpl(vcpu, 0))
5992 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005993 dr7 = vmcs_readl(GUEST_DR7);
5994 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005995 /*
5996 * As the vm-exit takes precedence over the debug trap, we
5997 * need to emulate the latter, either for the host or the
5998 * guest debugging itself.
5999 */
6000 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03006001 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03006002 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02006003 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03006004 vcpu->run->debug.arch.exception = DB_VECTOR;
6005 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006006 return 0;
6007 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02006008 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03006009 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006010 kvm_queue_exception(vcpu, DB_VECTOR);
6011 return 1;
6012 }
6013 }
6014
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006015 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01006016 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6017 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006018
6019 /*
6020 * No more DR vmexits; force a reload of the debug registers
6021 * and reenter on this instruction. The next vmexit will
6022 * retrieve the full state of the debug registers.
6023 */
6024 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
6025 return 1;
6026 }
6027
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006028 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
6029 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03006030 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006031
6032 if (kvm_get_dr(vcpu, dr, &val))
6033 return 1;
6034 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03006035 } else
Nadav Amit57773922014-06-18 17:19:23 +03006036 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01006037 return 1;
6038
Avi Kivity6aa8b732006-12-10 02:21:36 -08006039 skip_emulated_instruction(vcpu);
6040 return 1;
6041}
6042
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01006043static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
6044{
6045 return vcpu->arch.dr6;
6046}
6047
6048static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
6049{
6050}
6051
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006052static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
6053{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006054 get_debugreg(vcpu->arch.db[0], 0);
6055 get_debugreg(vcpu->arch.db[1], 1);
6056 get_debugreg(vcpu->arch.db[2], 2);
6057 get_debugreg(vcpu->arch.db[3], 3);
6058 get_debugreg(vcpu->arch.dr6, 6);
6059 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
6060
6061 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01006062 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01006063}
6064
Gleb Natapov020df072010-04-13 10:05:23 +03006065static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
6066{
6067 vmcs_writel(GUEST_DR7, val);
6068}
6069
Avi Kivity851ba692009-08-24 11:10:17 +03006070static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006071{
Avi Kivity06465c52007-02-28 20:46:53 +02006072 kvm_emulate_cpuid(vcpu);
6073 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006074}
6075
Avi Kivity851ba692009-08-24 11:10:17 +03006076static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006077{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006078 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006079 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006080
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006081 msr_info.index = ecx;
6082 msr_info.host_initiated = false;
6083 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02006084 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006085 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006086 return 1;
6087 }
6088
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006089 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006090
Avi Kivity6aa8b732006-12-10 02:21:36 -08006091 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02006092 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
6093 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006094 skip_emulated_instruction(vcpu);
6095 return 1;
6096}
6097
Avi Kivity851ba692009-08-24 11:10:17 +03006098static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006099{
Will Auld8fe8ab42012-11-29 12:42:12 -08006100 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006101 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6102 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6103 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006104
Will Auld8fe8ab42012-11-29 12:42:12 -08006105 msr.data = data;
6106 msr.index = ecx;
6107 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006108 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006109 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006110 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006111 return 1;
6112 }
6113
Avi Kivity59200272010-01-25 19:47:02 +02006114 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006115 skip_emulated_instruction(vcpu);
6116 return 1;
6117}
6118
Avi Kivity851ba692009-08-24 11:10:17 +03006119static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006120{
Avi Kivity3842d132010-07-27 12:30:24 +03006121 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006122 return 1;
6123}
6124
Avi Kivity851ba692009-08-24 11:10:17 +03006125static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006126{
Eddie Dong85f455f2007-07-06 12:20:49 +03006127 u32 cpu_based_vm_exec_control;
6128
6129 /* clear pending irq */
6130 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6131 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6132 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006133
Avi Kivity3842d132010-07-27 12:30:24 +03006134 kvm_make_request(KVM_REQ_EVENT, vcpu);
6135
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006136 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006137 return 1;
6138}
6139
Avi Kivity851ba692009-08-24 11:10:17 +03006140static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006141{
Avi Kivityd3bef152007-06-05 15:53:05 +03006142 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006143}
6144
Avi Kivity851ba692009-08-24 11:10:17 +03006145static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006146{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006147 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006148}
6149
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006150static int handle_invd(struct kvm_vcpu *vcpu)
6151{
Andre Przywara51d8b662010-12-21 11:12:02 +01006152 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006153}
6154
Avi Kivity851ba692009-08-24 11:10:17 +03006155static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006156{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006157 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006158
6159 kvm_mmu_invlpg(vcpu, exit_qualification);
6160 skip_emulated_instruction(vcpu);
6161 return 1;
6162}
6163
Avi Kivityfee84b02011-11-10 14:57:25 +02006164static int handle_rdpmc(struct kvm_vcpu *vcpu)
6165{
6166 int err;
6167
6168 err = kvm_rdpmc(vcpu);
6169 kvm_complete_insn_gp(vcpu, err);
6170
6171 return 1;
6172}
6173
Avi Kivity851ba692009-08-24 11:10:17 +03006174static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006175{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08006176 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006177 return 1;
6178}
6179
Dexuan Cui2acf9232010-06-10 11:27:12 +08006180static int handle_xsetbv(struct kvm_vcpu *vcpu)
6181{
6182 u64 new_bv = kvm_read_edx_eax(vcpu);
6183 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6184
6185 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
6186 skip_emulated_instruction(vcpu);
6187 return 1;
6188}
6189
Wanpeng Lif53cd632014-12-02 19:14:58 +08006190static int handle_xsaves(struct kvm_vcpu *vcpu)
6191{
6192 skip_emulated_instruction(vcpu);
6193 WARN(1, "this should never happen\n");
6194 return 1;
6195}
6196
6197static int handle_xrstors(struct kvm_vcpu *vcpu)
6198{
6199 skip_emulated_instruction(vcpu);
6200 WARN(1, "this should never happen\n");
6201 return 1;
6202}
6203
Avi Kivity851ba692009-08-24 11:10:17 +03006204static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006205{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006206 if (likely(fasteoi)) {
6207 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6208 int access_type, offset;
6209
6210 access_type = exit_qualification & APIC_ACCESS_TYPE;
6211 offset = exit_qualification & APIC_ACCESS_OFFSET;
6212 /*
6213 * Sane guest uses MOV to write EOI, with written value
6214 * not cared. So make a short-circuit here by avoiding
6215 * heavy instruction emulation.
6216 */
6217 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6218 (offset == APIC_EOI)) {
6219 kvm_lapic_set_eoi(vcpu);
6220 skip_emulated_instruction(vcpu);
6221 return 1;
6222 }
6223 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006224 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006225}
6226
Yang Zhangc7c9c562013-01-25 10:18:51 +08006227static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6228{
6229 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6230 int vector = exit_qualification & 0xff;
6231
6232 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6233 kvm_apic_set_eoi_accelerated(vcpu, vector);
6234 return 1;
6235}
6236
Yang Zhang83d4c282013-01-25 10:18:49 +08006237static int handle_apic_write(struct kvm_vcpu *vcpu)
6238{
6239 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6240 u32 offset = exit_qualification & 0xfff;
6241
6242 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6243 kvm_apic_write_nodecode(vcpu, offset);
6244 return 1;
6245}
6246
Avi Kivity851ba692009-08-24 11:10:17 +03006247static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006248{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006249 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006250 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006251 bool has_error_code = false;
6252 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006253 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006254 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006255
6256 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006257 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006258 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006259
6260 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6261
6262 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006263 if (reason == TASK_SWITCH_GATE && idt_v) {
6264 switch (type) {
6265 case INTR_TYPE_NMI_INTR:
6266 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006267 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006268 break;
6269 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006270 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006271 kvm_clear_interrupt_queue(vcpu);
6272 break;
6273 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006274 if (vmx->idt_vectoring_info &
6275 VECTORING_INFO_DELIVER_CODE_MASK) {
6276 has_error_code = true;
6277 error_code =
6278 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6279 }
6280 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006281 case INTR_TYPE_SOFT_EXCEPTION:
6282 kvm_clear_exception_queue(vcpu);
6283 break;
6284 default:
6285 break;
6286 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006287 }
Izik Eidus37817f22008-03-24 23:14:53 +02006288 tss_selector = exit_qualification;
6289
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006290 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6291 type != INTR_TYPE_EXT_INTR &&
6292 type != INTR_TYPE_NMI_INTR))
6293 skip_emulated_instruction(vcpu);
6294
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006295 if (kvm_task_switch(vcpu, tss_selector,
6296 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6297 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006298 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6299 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6300 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006301 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006302 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006303
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006304 /*
6305 * TODO: What about debug traps on tss switch?
6306 * Are we supposed to inject them and update dr6?
6307 */
6308
6309 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006310}
6311
Avi Kivity851ba692009-08-24 11:10:17 +03006312static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006313{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006314 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006315 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006316 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006317 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006318
Sheng Yangf9c617f2009-03-25 10:08:52 +08006319 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006320
Sheng Yang14394422008-04-28 12:24:45 +08006321 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006322 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006323 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6324 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6325 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006326 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006327 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6328 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006329 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6330 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006331 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006332 }
6333
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006334 /*
6335 * EPT violation happened while executing iret from NMI,
6336 * "blocked by NMI" bit has to be set before next VM entry.
6337 * There are errata that may cause this bit to not be set:
6338 * AAK134, BY25.
6339 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006340 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6341 cpu_has_virtual_nmis() &&
6342 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006343 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6344
Sheng Yang14394422008-04-28 12:24:45 +08006345 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006346 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006347
Bandan Dasd95c5562016-07-12 18:18:51 -04006348 /* it is a read fault? */
6349 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6350 /* it is a write fault? */
6351 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006352 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006353 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006354 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006355 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006356
Yang Zhang25d92082013-08-06 12:00:32 +03006357 vcpu->arch.exit_qualification = exit_qualification;
6358
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006359 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006360}
6361
Avi Kivity851ba692009-08-24 11:10:17 +03006362static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006363{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006364 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006365 gpa_t gpa;
6366
6367 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006368 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006369 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006370 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006371 return 1;
6372 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006373
Paolo Bonzini450869d2015-11-04 13:41:21 +01006374 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006375 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006376 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6377 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006378
6379 if (unlikely(ret == RET_MMIO_PF_INVALID))
6380 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6381
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006382 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006383 return 1;
6384
6385 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006386 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006387
Avi Kivity851ba692009-08-24 11:10:17 +03006388 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6389 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006390
6391 return 0;
6392}
6393
Avi Kivity851ba692009-08-24 11:10:17 +03006394static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006395{
6396 u32 cpu_based_vm_exec_control;
6397
6398 /* clear pending NMI */
6399 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6400 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6401 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6402 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006403 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006404
6405 return 1;
6406}
6407
Mohammed Gamal80ced182009-09-01 12:48:18 +02006408static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006409{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006410 struct vcpu_vmx *vmx = to_vmx(vcpu);
6411 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006412 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006413 u32 cpu_exec_ctrl;
6414 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006415 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006416
6417 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6418 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006419
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006420 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006421 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006422 return handle_interrupt_window(&vmx->vcpu);
6423
Avi Kivityde87dcd2012-06-12 20:21:38 +03006424 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6425 return 1;
6426
Liran Alon114de9b2017-11-05 16:56:34 +02006427 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006428
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006429 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006430 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006431 ret = 0;
6432 goto out;
6433 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006434
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006435 if (err != EMULATE_DONE)
6436 goto emulation_error;
6437
6438 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6439 vcpu->arch.exception.pending)
6440 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006441
Gleb Natapov8d76c492013-05-08 18:38:44 +03006442 if (vcpu->arch.halt_request) {
6443 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006444 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006445 goto out;
6446 }
6447
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006448 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006449 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006450 if (need_resched())
6451 schedule();
6452 }
6453
Mohammed Gamal80ced182009-09-01 12:48:18 +02006454out:
6455 return ret;
Sean Christopherson94b4fed2018-03-23 09:34:00 -07006456
6457emulation_error:
6458 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6459 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6460 vcpu->run->internal.ndata = 0;
6461 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006462}
6463
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006464static int __grow_ple_window(int val)
6465{
6466 if (ple_window_grow < 1)
6467 return ple_window;
6468
6469 val = min(val, ple_window_actual_max);
6470
6471 if (ple_window_grow < ple_window)
6472 val *= ple_window_grow;
6473 else
6474 val += ple_window_grow;
6475
6476 return val;
6477}
6478
6479static int __shrink_ple_window(int val, int modifier, int minimum)
6480{
6481 if (modifier < 1)
6482 return ple_window;
6483
6484 if (modifier < ple_window)
6485 val /= modifier;
6486 else
6487 val -= modifier;
6488
6489 return max(val, minimum);
6490}
6491
6492static void grow_ple_window(struct kvm_vcpu *vcpu)
6493{
6494 struct vcpu_vmx *vmx = to_vmx(vcpu);
6495 int old = vmx->ple_window;
6496
6497 vmx->ple_window = __grow_ple_window(old);
6498
6499 if (vmx->ple_window != old)
6500 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006501
6502 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006503}
6504
6505static void shrink_ple_window(struct kvm_vcpu *vcpu)
6506{
6507 struct vcpu_vmx *vmx = to_vmx(vcpu);
6508 int old = vmx->ple_window;
6509
6510 vmx->ple_window = __shrink_ple_window(old,
6511 ple_window_shrink, ple_window);
6512
6513 if (vmx->ple_window != old)
6514 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006515
6516 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006517}
6518
6519/*
6520 * ple_window_actual_max is computed to be one grow_ple_window() below
6521 * ple_window_max. (See __grow_ple_window for the reason.)
6522 * This prevents overflows, because ple_window_max is int.
6523 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6524 * this process.
6525 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6526 */
6527static void update_ple_window_actual_max(void)
6528{
6529 ple_window_actual_max =
6530 __shrink_ple_window(max(ple_window_max, ple_window),
6531 ple_window_grow, INT_MIN);
6532}
6533
Feng Wubf9f6ac2015-09-18 22:29:55 +08006534/*
6535 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6536 */
6537static void wakeup_handler(void)
6538{
6539 struct kvm_vcpu *vcpu;
6540 int cpu = smp_processor_id();
6541
6542 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6543 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6544 blocked_vcpu_list) {
6545 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6546
6547 if (pi_test_on(pi_desc) == 1)
6548 kvm_vcpu_kick(vcpu);
6549 }
6550 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6551}
6552
Tiejun Chenf2c76482014-10-28 10:14:47 +08006553static __init int hardware_setup(void)
6554{
Paolo Bonzini6236b782018-01-16 16:51:18 +01006555 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006556
6557 rdmsrl_safe(MSR_EFER, &host_efer);
6558
6559 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6560 kvm_define_shared_msr(i, vmx_msr_index[i]);
6561
6562 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6563 if (!vmx_io_bitmap_a)
6564 return r;
6565
6566 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6567 if (!vmx_io_bitmap_b)
6568 goto out;
6569
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006570 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6571 if (!vmx_vmread_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006572 goto out1;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006573
6574 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6575 if (!vmx_vmwrite_bitmap)
Paolo Bonzini6236b782018-01-16 16:51:18 +01006576 goto out2;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006577
6578 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6579 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6580
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006581 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006582
6583 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6584
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006585 if (setup_vmcs_config(&vmcs_config) < 0) {
6586 r = -EIO;
Paolo Bonzini6236b782018-01-16 16:51:18 +01006587 goto out3;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006588 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006589
6590 if (boot_cpu_has(X86_FEATURE_NX))
6591 kvm_enable_efer_bits(EFER_NX);
6592
Wanpeng Li2df19692017-03-23 05:30:08 -07006593 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6594 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006595 enable_vpid = 0;
Wanpeng Li2df19692017-03-23 05:30:08 -07006596
Tiejun Chenf2c76482014-10-28 10:14:47 +08006597 if (!cpu_has_vmx_shadow_vmcs())
6598 enable_shadow_vmcs = 0;
6599 if (enable_shadow_vmcs)
6600 init_vmcs_shadow_fields();
6601
6602 if (!cpu_has_vmx_ept() ||
6603 !cpu_has_vmx_ept_4levels()) {
6604 enable_ept = 0;
6605 enable_unrestricted_guest = 0;
6606 enable_ept_ad_bits = 0;
6607 }
6608
6609 if (!cpu_has_vmx_ept_ad_bits())
6610 enable_ept_ad_bits = 0;
6611
6612 if (!cpu_has_vmx_unrestricted_guest())
6613 enable_unrestricted_guest = 0;
6614
Paolo Bonziniad15a292015-01-30 16:18:49 +01006615 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006616 flexpriority_enabled = 0;
6617
Paolo Bonziniad15a292015-01-30 16:18:49 +01006618 /*
6619 * set_apic_access_page_addr() is used to reload apic access
6620 * page upon invalidation. No need to do anything if not
6621 * using the APIC_ACCESS_ADDR VMCS field.
6622 */
6623 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006624 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006625
6626 if (!cpu_has_vmx_tpr_shadow())
6627 kvm_x86_ops->update_cr8_intercept = NULL;
6628
6629 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6630 kvm_disable_largepages();
6631
6632 if (!cpu_has_vmx_ple())
6633 ple_gap = 0;
6634
6635 if (!cpu_has_vmx_apicv())
6636 enable_apicv = 0;
6637
Haozhong Zhang64903d62015-10-20 15:39:09 +08006638 if (cpu_has_vmx_tsc_scaling()) {
6639 kvm_has_tsc_control = true;
6640 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6641 kvm_tsc_scaling_ratio_frac_bits = 48;
6642 }
6643
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006644 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6645
Tiejun Chenbaa03522014-12-23 16:21:11 +08006646 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006647 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006648 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6649 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006650 0ull, VMX_EPT_EXECUTABLE_MASK,
6651 cpu_has_vmx_ept_execute_only() ?
6652 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006653 ept_set_mmio_spte_mask();
6654 kvm_enable_tdp();
6655 } else
6656 kvm_disable_tdp();
6657
6658 update_ple_window_actual_max();
6659
Kai Huang843e4332015-01-28 10:54:28 +08006660 /*
6661 * Only enable PML when hardware supports PML feature, and both EPT
6662 * and EPT A/D bit features are enabled -- PML depends on them to work.
6663 */
6664 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6665 enable_pml = 0;
6666
6667 if (!enable_pml) {
6668 kvm_x86_ops->slot_enable_log_dirty = NULL;
6669 kvm_x86_ops->slot_disable_log_dirty = NULL;
6670 kvm_x86_ops->flush_log_dirty = NULL;
6671 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6672 }
6673
Yunhong Jiang64672c92016-06-13 14:19:59 -07006674 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6675 u64 vmx_msr;
6676
6677 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6678 cpu_preemption_timer_multi =
6679 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6680 } else {
6681 kvm_x86_ops->set_hv_timer = NULL;
6682 kvm_x86_ops->cancel_hv_timer = NULL;
6683 }
6684
Feng Wubf9f6ac2015-09-18 22:29:55 +08006685 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6686
Ashok Rajc45dcc72016-06-22 14:59:56 +08006687 kvm_mce_cap_supported |= MCG_LMCE_P;
6688
Tiejun Chenf2c76482014-10-28 10:14:47 +08006689 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006690
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006691out3:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006692 free_page((unsigned long)vmx_vmwrite_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006693out2:
Paolo Bonzini6236b782018-01-16 16:51:18 +01006694 free_page((unsigned long)vmx_vmread_bitmap);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006695out1:
6696 free_page((unsigned long)vmx_io_bitmap_b);
6697out:
6698 free_page((unsigned long)vmx_io_bitmap_a);
6699
6700 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006701}
6702
6703static __exit void hardware_unsetup(void)
6704{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006705 free_page((unsigned long)vmx_io_bitmap_b);
6706 free_page((unsigned long)vmx_io_bitmap_a);
6707 free_page((unsigned long)vmx_vmwrite_bitmap);
6708 free_page((unsigned long)vmx_vmread_bitmap);
6709
Tiejun Chenf2c76482014-10-28 10:14:47 +08006710 free_kvm_area();
6711}
6712
Avi Kivity6aa8b732006-12-10 02:21:36 -08006713/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006714 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6715 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6716 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006717static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006718{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006719 if (ple_gap)
6720 grow_ple_window(vcpu);
6721
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006722 skip_emulated_instruction(vcpu);
6723 kvm_vcpu_on_spin(vcpu);
6724
6725 return 1;
6726}
6727
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006728static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006729{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006730 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006731 return 1;
6732}
6733
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006734static int handle_mwait(struct kvm_vcpu *vcpu)
6735{
6736 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6737 return handle_nop(vcpu);
6738}
6739
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006740static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6741{
6742 return 1;
6743}
6744
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006745static int handle_monitor(struct kvm_vcpu *vcpu)
6746{
6747 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6748 return handle_nop(vcpu);
6749}
6750
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006751/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006752 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6753 * set the success or error code of an emulated VMX instruction, as specified
6754 * by Vol 2B, VMX Instruction Reference, "Conventions".
6755 */
6756static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6757{
6758 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6759 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6760 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6761}
6762
6763static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6764{
6765 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6766 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6767 X86_EFLAGS_SF | X86_EFLAGS_OF))
6768 | X86_EFLAGS_CF);
6769}
6770
Abel Gordon145c28d2013-04-18 14:36:55 +03006771static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006772 u32 vm_instruction_error)
6773{
6774 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6775 /*
6776 * failValid writes the error number to the current VMCS, which
6777 * can't be done there isn't a current VMCS.
6778 */
6779 nested_vmx_failInvalid(vcpu);
6780 return;
6781 }
6782 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6783 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6784 X86_EFLAGS_SF | X86_EFLAGS_OF))
6785 | X86_EFLAGS_ZF);
6786 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6787 /*
6788 * We don't need to force a shadow sync because
6789 * VM_INSTRUCTION_ERROR is not shadowed
6790 */
6791}
Abel Gordon145c28d2013-04-18 14:36:55 +03006792
Wincy Vanff651cb2014-12-11 08:52:58 +03006793static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6794{
6795 /* TODO: not to reset guest simply here. */
6796 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006797 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006798}
6799
Jan Kiszkaf4124502014-03-07 20:03:13 +01006800static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6801{
6802 struct vcpu_vmx *vmx =
6803 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6804
6805 vmx->nested.preemption_timer_expired = true;
6806 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6807 kvm_vcpu_kick(&vmx->vcpu);
6808
6809 return HRTIMER_NORESTART;
6810}
6811
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006812/*
Bandan Das19677e32014-05-06 02:19:15 -04006813 * Decode the memory-address operand of a vmx instruction, as recorded on an
6814 * exit caused by such an instruction (run by a guest hypervisor).
6815 * On success, returns 0. When the operand is invalid, returns 1 and throws
6816 * #UD or #GP.
6817 */
6818static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6819 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006820 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006821{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006822 gva_t off;
6823 bool exn;
6824 struct kvm_segment s;
6825
Bandan Das19677e32014-05-06 02:19:15 -04006826 /*
6827 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6828 * Execution", on an exit, vmx_instruction_info holds most of the
6829 * addressing components of the operand. Only the displacement part
6830 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6831 * For how an actual address is calculated from all these components,
6832 * refer to Vol. 1, "Operand Addressing".
6833 */
6834 int scaling = vmx_instruction_info & 3;
6835 int addr_size = (vmx_instruction_info >> 7) & 7;
6836 bool is_reg = vmx_instruction_info & (1u << 10);
6837 int seg_reg = (vmx_instruction_info >> 15) & 7;
6838 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6839 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6840 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6841 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6842
6843 if (is_reg) {
6844 kvm_queue_exception(vcpu, UD_VECTOR);
6845 return 1;
6846 }
6847
6848 /* Addr = segment_base + offset */
6849 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006850 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006851 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006852 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006853 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006854 off += kvm_register_read(vcpu, index_reg)<<scaling;
6855 vmx_get_segment(vcpu, &s, seg_reg);
6856 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006857
6858 if (addr_size == 1) /* 32 bit */
6859 *ret &= 0xffffffff;
6860
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006861 /* Checks for #GP/#SS exceptions. */
6862 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006863 if (is_long_mode(vcpu)) {
6864 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6865 * non-canonical form. This is the only check on the memory
6866 * destination for long mode!
6867 */
6868 exn = is_noncanonical_address(*ret);
6869 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006870 /* Protected mode: apply checks for segment validity in the
6871 * following order:
6872 * - segment type check (#GP(0) may be thrown)
6873 * - usability check (#GP(0)/#SS(0))
6874 * - limit check (#GP(0)/#SS(0))
6875 */
6876 if (wr)
6877 /* #GP(0) if the destination operand is located in a
6878 * read-only data segment or any code segment.
6879 */
6880 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6881 else
6882 /* #GP(0) if the source operand is located in an
6883 * execute-only code segment
6884 */
6885 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006886 if (exn) {
6887 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6888 return 1;
6889 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006890 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6891 */
6892 exn = (s.unusable != 0);
6893 /* Protected mode: #GP(0)/#SS(0) if the memory
6894 * operand is outside the segment limit.
6895 */
6896 exn = exn || (off + sizeof(u64) > s.limit);
6897 }
6898 if (exn) {
6899 kvm_queue_exception_e(vcpu,
6900 seg_reg == VCPU_SREG_SS ?
6901 SS_VECTOR : GP_VECTOR,
6902 0);
6903 return 1;
6904 }
6905
Bandan Das19677e32014-05-06 02:19:15 -04006906 return 0;
6907}
6908
6909/*
Bandan Das3573e222014-05-06 02:19:16 -04006910 * This function performs the various checks including
6911 * - if it's 4KB aligned
6912 * - No bits beyond the physical address width are set
6913 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006914 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006915 */
Bandan Das4291b582014-05-06 02:19:18 -04006916static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6917 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006918{
6919 gva_t gva;
6920 gpa_t vmptr;
6921 struct x86_exception e;
6922 struct page *page;
6923 struct vcpu_vmx *vmx = to_vmx(vcpu);
6924 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6925
6926 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006927 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006928 return 1;
6929
6930 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6931 sizeof(vmptr), &e)) {
6932 kvm_inject_page_fault(vcpu, &e);
6933 return 1;
6934 }
6935
6936 switch (exit_reason) {
6937 case EXIT_REASON_VMON:
6938 /*
6939 * SDM 3: 24.11.5
6940 * The first 4 bytes of VMXON region contain the supported
6941 * VMCS revision identifier
6942 *
6943 * Note - IA32_VMX_BASIC[48] will never be 1
6944 * for the nested case;
6945 * which replaces physical address width with 32
6946 *
6947 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006948 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006949 nested_vmx_failInvalid(vcpu);
6950 skip_emulated_instruction(vcpu);
6951 return 1;
6952 }
6953
6954 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006955 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04006956 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006957 skip_emulated_instruction(vcpu);
6958 return 1;
6959 }
6960 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04006961 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006962 nested_release_page_clean(page);
6963 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04006964 skip_emulated_instruction(vcpu);
6965 return 1;
6966 }
6967 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006968 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04006969 vmx->nested.vmxon_ptr = vmptr;
6970 break;
Bandan Das4291b582014-05-06 02:19:18 -04006971 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006972 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006973 nested_vmx_failValid(vcpu,
6974 VMXERR_VMCLEAR_INVALID_ADDRESS);
6975 skip_emulated_instruction(vcpu);
6976 return 1;
6977 }
Bandan Das3573e222014-05-06 02:19:16 -04006978
Bandan Das4291b582014-05-06 02:19:18 -04006979 if (vmptr == vmx->nested.vmxon_ptr) {
6980 nested_vmx_failValid(vcpu,
6981 VMXERR_VMCLEAR_VMXON_POINTER);
6982 skip_emulated_instruction(vcpu);
6983 return 1;
6984 }
6985 break;
6986 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006987 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006988 nested_vmx_failValid(vcpu,
6989 VMXERR_VMPTRLD_INVALID_ADDRESS);
6990 skip_emulated_instruction(vcpu);
6991 return 1;
6992 }
6993
6994 if (vmptr == vmx->nested.vmxon_ptr) {
6995 nested_vmx_failValid(vcpu,
6996 VMXERR_VMCLEAR_VMXON_POINTER);
6997 skip_emulated_instruction(vcpu);
6998 return 1;
6999 }
7000 break;
Bandan Das3573e222014-05-06 02:19:16 -04007001 default:
7002 return 1; /* shouldn't happen */
7003 }
7004
Bandan Das4291b582014-05-06 02:19:18 -04007005 if (vmpointer)
7006 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007007 return 0;
7008}
7009
7010/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007011 * Emulate the VMXON instruction.
7012 * Currently, we just remember that VMX is active, and do not save or even
7013 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7014 * do not currently need to store anything in that guest-allocated memory
7015 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7016 * argument is different from the VMXON pointer (which the spec says they do).
7017 */
7018static int handle_vmon(struct kvm_vcpu *vcpu)
7019{
7020 struct kvm_segment cs;
7021 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007022 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007023 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7024 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Paolo Bonziniff546f92018-01-11 12:16:15 +01007025 int r;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007026
7027 /* The Intel VMX Instruction Reference lists a bunch of bits that
7028 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7029 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7030 * Otherwise, we should fail with #UD. We test these now:
7031 */
7032 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7033 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7034 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7035 kvm_queue_exception(vcpu, UD_VECTOR);
7036 return 1;
7037 }
7038
7039 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7040 if (is_long_mode(vcpu) && !cs.l) {
7041 kvm_queue_exception(vcpu, UD_VECTOR);
7042 return 1;
7043 }
7044
7045 if (vmx_get_cpl(vcpu)) {
7046 kvm_inject_gp(vcpu, 0);
7047 return 1;
7048 }
Bandan Das3573e222014-05-06 02:19:16 -04007049
Bandan Das4291b582014-05-06 02:19:18 -04007050 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007051 return 1;
7052
Abel Gordon145c28d2013-04-18 14:36:55 +03007053 if (vmx->nested.vmxon) {
7054 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7055 skip_emulated_instruction(vcpu);
7056 return 1;
7057 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007058
Haozhong Zhang3b840802016-06-22 14:59:54 +08007059 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007060 != VMXON_NEEDED_FEATURES) {
7061 kvm_inject_gp(vcpu, 0);
7062 return 1;
7063 }
7064
Paolo Bonziniff546f92018-01-11 12:16:15 +01007065 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
7066 if (r < 0)
Jim Mattson46e24df2017-11-27 17:22:25 -06007067 goto out_vmcs02;
Jim Mattson46e24df2017-11-27 17:22:25 -06007068
David Matlack4f2777b2016-07-13 17:16:37 -07007069 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7070 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007071 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007072
Abel Gordon8de48832013-04-18 14:37:25 +03007073 if (enable_shadow_vmcs) {
7074 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007075 if (!shadow_vmcs)
7076 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007077 /* mark vmcs as shadow */
7078 shadow_vmcs->revision_id |= (1u << 31);
7079 /* init shadow vmcs */
7080 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007081 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007082 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007083
Jan Kiszkaf4124502014-03-07 20:03:13 +01007084 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007085 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007086 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7087
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007088 vmx->nested.vmxon = true;
7089
7090 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007091 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007092 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007093
7094out_shadow_vmcs:
7095 kfree(vmx->nested.cached_vmcs12);
7096
7097out_cached_vmcs12:
Jim Mattson46e24df2017-11-27 17:22:25 -06007098 free_loaded_vmcs(&vmx->nested.vmcs02);
7099
7100out_vmcs02:
Radim Krčmářd048c092016-08-08 20:16:22 +02007101 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007102}
7103
7104/*
7105 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7106 * for running VMX instructions (except VMXON, whose prerequisites are
7107 * slightly different). It also specifies what exception to inject otherwise.
7108 */
7109static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7110{
7111 struct kvm_segment cs;
7112 struct vcpu_vmx *vmx = to_vmx(vcpu);
7113
7114 if (!vmx->nested.vmxon) {
7115 kvm_queue_exception(vcpu, UD_VECTOR);
7116 return 0;
7117 }
7118
7119 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7120 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7121 (is_long_mode(vcpu) && !cs.l)) {
7122 kvm_queue_exception(vcpu, UD_VECTOR);
7123 return 0;
7124 }
7125
7126 if (vmx_get_cpl(vcpu)) {
7127 kvm_inject_gp(vcpu, 0);
7128 return 0;
7129 }
7130
7131 return 1;
7132}
7133
Abel Gordone7953d72013-04-18 14:37:55 +03007134static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7135{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007136 if (vmx->nested.current_vmptr == -1ull)
7137 return;
7138
7139 /* current_vmptr and current_vmcs12 are always set/reset together */
7140 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7141 return;
7142
Abel Gordon012f83c2013-04-18 14:39:25 +03007143 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007144 /* copy to memory all shadowed fields in case
7145 they were modified */
7146 copy_shadow_to_vmcs12(vmx);
7147 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007148 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7149 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007150 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007151 }
Wincy Van705699a2015-02-03 23:58:17 +08007152 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007153
7154 /* Flush VMCS12 to guest memory */
7155 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7156 VMCS12_SIZE);
7157
Abel Gordone7953d72013-04-18 14:37:55 +03007158 kunmap(vmx->nested.current_vmcs12_page);
7159 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007160 vmx->nested.current_vmptr = -1ull;
7161 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007162}
7163
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007164/*
7165 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7166 * just stops using VMX.
7167 */
7168static void free_nested(struct vcpu_vmx *vmx)
7169{
7170 if (!vmx->nested.vmxon)
7171 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007172
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007173 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007174 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007175 nested_release_vmcs12(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007176 if (enable_shadow_vmcs) {
7177 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7178 free_vmcs(vmx->vmcs01.shadow_vmcs);
7179 vmx->vmcs01.shadow_vmcs = NULL;
7180 }
David Matlack4f2777b2016-07-13 17:16:37 -07007181 kfree(vmx->nested.cached_vmcs12);
Jim Mattson46e24df2017-11-27 17:22:25 -06007182 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007183 if (vmx->nested.apic_access_page) {
7184 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007185 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007186 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007187 if (vmx->nested.virtual_apic_page) {
7188 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007189 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007190 }
Wincy Van705699a2015-02-03 23:58:17 +08007191 if (vmx->nested.pi_desc_page) {
7192 kunmap(vmx->nested.pi_desc_page);
7193 nested_release_page(vmx->nested.pi_desc_page);
7194 vmx->nested.pi_desc_page = NULL;
7195 vmx->nested.pi_desc = NULL;
7196 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007197
Jim Mattson46e24df2017-11-27 17:22:25 -06007198 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007199}
7200
7201/* Emulate the VMXOFF instruction */
7202static int handle_vmoff(struct kvm_vcpu *vcpu)
7203{
7204 if (!nested_vmx_check_permission(vcpu))
7205 return 1;
7206 free_nested(to_vmx(vcpu));
7207 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007208 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007209 return 1;
7210}
7211
Nadav Har'El27d6c862011-05-25 23:06:59 +03007212/* Emulate the VMCLEAR instruction */
7213static int handle_vmclear(struct kvm_vcpu *vcpu)
7214{
7215 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson29deec42017-03-02 12:41:48 -08007216 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007217 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007218
7219 if (!nested_vmx_check_permission(vcpu))
7220 return 1;
7221
Bandan Das4291b582014-05-06 02:19:18 -04007222 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007223 return 1;
7224
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007225 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007226 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007227
Jim Mattson29deec42017-03-02 12:41:48 -08007228 kvm_vcpu_write_guest(vcpu,
7229 vmptr + offsetof(struct vmcs12, launch_state),
7230 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007231
Nadav Har'El27d6c862011-05-25 23:06:59 +03007232 skip_emulated_instruction(vcpu);
7233 nested_vmx_succeed(vcpu);
7234 return 1;
7235}
7236
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007237static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7238
7239/* Emulate the VMLAUNCH instruction */
7240static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7241{
7242 return nested_vmx_run(vcpu, true);
7243}
7244
7245/* Emulate the VMRESUME instruction */
7246static int handle_vmresume(struct kvm_vcpu *vcpu)
7247{
7248
7249 return nested_vmx_run(vcpu, false);
7250}
7251
Nadav Har'El49f705c2011-05-25 23:08:30 +03007252enum vmcs_field_type {
7253 VMCS_FIELD_TYPE_U16 = 0,
7254 VMCS_FIELD_TYPE_U64 = 1,
7255 VMCS_FIELD_TYPE_U32 = 2,
7256 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7257};
7258
7259static inline int vmcs_field_type(unsigned long field)
7260{
7261 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7262 return VMCS_FIELD_TYPE_U32;
7263 return (field >> 13) & 0x3 ;
7264}
7265
7266static inline int vmcs_field_readonly(unsigned long field)
7267{
7268 return (((field >> 10) & 0x3) == 1);
7269}
7270
7271/*
7272 * Read a vmcs12 field. Since these can have varying lengths and we return
7273 * one type, we chose the biggest type (u64) and zero-extend the return value
7274 * to that size. Note that the caller, handle_vmread, might need to use only
7275 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7276 * 64-bit fields are to be returned).
7277 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007278static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7279 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007280{
7281 short offset = vmcs_field_to_offset(field);
7282 char *p;
7283
7284 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007285 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007286
7287 p = ((char *)(get_vmcs12(vcpu))) + offset;
7288
7289 switch (vmcs_field_type(field)) {
7290 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7291 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007292 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007293 case VMCS_FIELD_TYPE_U16:
7294 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007295 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007296 case VMCS_FIELD_TYPE_U32:
7297 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007298 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007299 case VMCS_FIELD_TYPE_U64:
7300 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007301 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007302 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007303 WARN_ON(1);
7304 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007305 }
7306}
7307
Abel Gordon20b97fe2013-04-18 14:36:25 +03007308
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007309static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7310 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007311 short offset = vmcs_field_to_offset(field);
7312 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7313 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007314 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007315
7316 switch (vmcs_field_type(field)) {
7317 case VMCS_FIELD_TYPE_U16:
7318 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007319 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007320 case VMCS_FIELD_TYPE_U32:
7321 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007322 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007323 case VMCS_FIELD_TYPE_U64:
7324 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007325 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007326 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7327 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007328 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007329 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007330 WARN_ON(1);
7331 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007332 }
7333
7334}
7335
Abel Gordon16f5b902013-04-18 14:38:25 +03007336static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7337{
7338 int i;
7339 unsigned long field;
7340 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007341 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007342 const unsigned long *fields = shadow_read_write_fields;
7343 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007344
Jan Kiszka282da872014-10-08 18:05:39 +02007345 preempt_disable();
7346
Abel Gordon16f5b902013-04-18 14:38:25 +03007347 vmcs_load(shadow_vmcs);
7348
7349 for (i = 0; i < num_fields; i++) {
7350 field = fields[i];
7351 switch (vmcs_field_type(field)) {
7352 case VMCS_FIELD_TYPE_U16:
7353 field_value = vmcs_read16(field);
7354 break;
7355 case VMCS_FIELD_TYPE_U32:
7356 field_value = vmcs_read32(field);
7357 break;
7358 case VMCS_FIELD_TYPE_U64:
7359 field_value = vmcs_read64(field);
7360 break;
7361 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7362 field_value = vmcs_readl(field);
7363 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007364 default:
7365 WARN_ON(1);
7366 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007367 }
7368 vmcs12_write_any(&vmx->vcpu, field, field_value);
7369 }
7370
7371 vmcs_clear(shadow_vmcs);
7372 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007373
7374 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007375}
7376
Abel Gordonc3114422013-04-18 14:38:55 +03007377static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7378{
Mathias Krausec2bae892013-06-26 20:36:21 +02007379 const unsigned long *fields[] = {
7380 shadow_read_write_fields,
7381 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007382 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007383 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007384 max_shadow_read_write_fields,
7385 max_shadow_read_only_fields
7386 };
7387 int i, q;
7388 unsigned long field;
7389 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007390 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007391
7392 vmcs_load(shadow_vmcs);
7393
Mathias Krausec2bae892013-06-26 20:36:21 +02007394 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007395 for (i = 0; i < max_fields[q]; i++) {
7396 field = fields[q][i];
7397 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7398
7399 switch (vmcs_field_type(field)) {
7400 case VMCS_FIELD_TYPE_U16:
7401 vmcs_write16(field, (u16)field_value);
7402 break;
7403 case VMCS_FIELD_TYPE_U32:
7404 vmcs_write32(field, (u32)field_value);
7405 break;
7406 case VMCS_FIELD_TYPE_U64:
7407 vmcs_write64(field, (u64)field_value);
7408 break;
7409 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7410 vmcs_writel(field, (long)field_value);
7411 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007412 default:
7413 WARN_ON(1);
7414 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007415 }
7416 }
7417 }
7418
7419 vmcs_clear(shadow_vmcs);
7420 vmcs_load(vmx->loaded_vmcs->vmcs);
7421}
7422
Nadav Har'El49f705c2011-05-25 23:08:30 +03007423/*
7424 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7425 * used before) all generate the same failure when it is missing.
7426 */
7427static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7428{
7429 struct vcpu_vmx *vmx = to_vmx(vcpu);
7430 if (vmx->nested.current_vmptr == -1ull) {
7431 nested_vmx_failInvalid(vcpu);
7432 skip_emulated_instruction(vcpu);
7433 return 0;
7434 }
7435 return 1;
7436}
7437
7438static int handle_vmread(struct kvm_vcpu *vcpu)
7439{
7440 unsigned long field;
7441 u64 field_value;
7442 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7443 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7444 gva_t gva = 0;
7445
7446 if (!nested_vmx_check_permission(vcpu) ||
7447 !nested_vmx_check_vmcs12(vcpu))
7448 return 1;
7449
7450 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007451 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007452 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007453 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007454 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7455 skip_emulated_instruction(vcpu);
7456 return 1;
7457 }
7458 /*
7459 * Now copy part of this value to register or memory, as requested.
7460 * Note that the number of bits actually copied is 32 or 64 depending
7461 * on the guest's mode (32 or 64 bit), not on the given field's length.
7462 */
7463 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007464 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007465 field_value);
7466 } else {
7467 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007468 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007469 return 1;
7470 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7471 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7472 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7473 }
7474
7475 nested_vmx_succeed(vcpu);
7476 skip_emulated_instruction(vcpu);
7477 return 1;
7478}
7479
7480
7481static int handle_vmwrite(struct kvm_vcpu *vcpu)
7482{
7483 unsigned long field;
7484 gva_t gva;
7485 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7486 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007487 /* The value to write might be 32 or 64 bits, depending on L1's long
7488 * mode, and eventually we need to write that into a field of several
7489 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007490 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007491 * bits into the vmcs12 field.
7492 */
7493 u64 field_value = 0;
7494 struct x86_exception e;
7495
7496 if (!nested_vmx_check_permission(vcpu) ||
7497 !nested_vmx_check_vmcs12(vcpu))
7498 return 1;
7499
7500 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007501 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007502 (((vmx_instruction_info) >> 3) & 0xf));
7503 else {
7504 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007505 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007506 return 1;
7507 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007508 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007509 kvm_inject_page_fault(vcpu, &e);
7510 return 1;
7511 }
7512 }
7513
7514
Nadav Amit27e6fb52014-06-18 17:19:26 +03007515 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007516 if (vmcs_field_readonly(field)) {
7517 nested_vmx_failValid(vcpu,
7518 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7519 skip_emulated_instruction(vcpu);
7520 return 1;
7521 }
7522
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007523 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007524 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7525 skip_emulated_instruction(vcpu);
7526 return 1;
7527 }
7528
7529 nested_vmx_succeed(vcpu);
7530 skip_emulated_instruction(vcpu);
7531 return 1;
7532}
7533
Nadav Har'El63846662011-05-25 23:07:29 +03007534/* Emulate the VMPTRLD instruction */
7535static int handle_vmptrld(struct kvm_vcpu *vcpu)
7536{
7537 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007538 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007539
7540 if (!nested_vmx_check_permission(vcpu))
7541 return 1;
7542
Bandan Das4291b582014-05-06 02:19:18 -04007543 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007544 return 1;
7545
Nadav Har'El63846662011-05-25 23:07:29 +03007546 if (vmx->nested.current_vmptr != vmptr) {
7547 struct vmcs12 *new_vmcs12;
7548 struct page *page;
7549 page = nested_get_page(vcpu, vmptr);
7550 if (page == NULL) {
7551 nested_vmx_failInvalid(vcpu);
7552 skip_emulated_instruction(vcpu);
7553 return 1;
7554 }
7555 new_vmcs12 = kmap(page);
7556 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7557 kunmap(page);
7558 nested_release_page_clean(page);
7559 nested_vmx_failValid(vcpu,
7560 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7561 skip_emulated_instruction(vcpu);
7562 return 1;
7563 }
Nadav Har'El63846662011-05-25 23:07:29 +03007564
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007565 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007566 vmx->nested.current_vmptr = vmptr;
7567 vmx->nested.current_vmcs12 = new_vmcs12;
7568 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007569 /*
7570 * Load VMCS12 from guest memory since it is not already
7571 * cached.
7572 */
7573 memcpy(vmx->nested.cached_vmcs12,
7574 vmx->nested.current_vmcs12, VMCS12_SIZE);
7575
Abel Gordon012f83c2013-04-18 14:39:25 +03007576 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007577 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7578 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007579 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007580 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007581 vmx->nested.sync_shadow_vmcs = true;
7582 }
Nadav Har'El63846662011-05-25 23:07:29 +03007583 }
7584
7585 nested_vmx_succeed(vcpu);
7586 skip_emulated_instruction(vcpu);
7587 return 1;
7588}
7589
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007590/* Emulate the VMPTRST instruction */
7591static int handle_vmptrst(struct kvm_vcpu *vcpu)
7592{
7593 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7594 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7595 gva_t vmcs_gva;
7596 struct x86_exception e;
7597
7598 if (!nested_vmx_check_permission(vcpu))
7599 return 1;
7600
7601 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007602 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007603 return 1;
7604 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7605 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7606 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7607 sizeof(u64), &e)) {
7608 kvm_inject_page_fault(vcpu, &e);
7609 return 1;
7610 }
7611 nested_vmx_succeed(vcpu);
7612 skip_emulated_instruction(vcpu);
7613 return 1;
7614}
7615
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007616/* Emulate the INVEPT instruction */
7617static int handle_invept(struct kvm_vcpu *vcpu)
7618{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007619 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007620 u32 vmx_instruction_info, types;
7621 unsigned long type;
7622 gva_t gva;
7623 struct x86_exception e;
7624 struct {
7625 u64 eptp, gpa;
7626 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007627
Wincy Vanb9c237b2015-02-03 23:56:30 +08007628 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7629 SECONDARY_EXEC_ENABLE_EPT) ||
7630 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007631 kvm_queue_exception(vcpu, UD_VECTOR);
7632 return 1;
7633 }
7634
7635 if (!nested_vmx_check_permission(vcpu))
7636 return 1;
7637
7638 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7639 kvm_queue_exception(vcpu, UD_VECTOR);
7640 return 1;
7641 }
7642
7643 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007644 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007645
Wincy Vanb9c237b2015-02-03 23:56:30 +08007646 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007647
Jim Mattson85c856b2016-10-26 08:38:38 -07007648 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007649 nested_vmx_failValid(vcpu,
7650 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007651 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007652 return 1;
7653 }
7654
7655 /* According to the Intel VMX instruction reference, the memory
7656 * operand is read even if it isn't needed (e.g., for type==global)
7657 */
7658 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007659 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007660 return 1;
7661 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7662 sizeof(operand), &e)) {
7663 kvm_inject_page_fault(vcpu, &e);
7664 return 1;
7665 }
7666
7667 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007668 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007669 /*
7670 * TODO: track mappings and invalidate
7671 * single context requests appropriately
7672 */
7673 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007674 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007675 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007676 nested_vmx_succeed(vcpu);
7677 break;
7678 default:
7679 BUG_ON(1);
7680 break;
7681 }
7682
7683 skip_emulated_instruction(vcpu);
7684 return 1;
7685}
7686
Petr Matouseka642fc32014-09-23 20:22:30 +02007687static int handle_invvpid(struct kvm_vcpu *vcpu)
7688{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007689 struct vcpu_vmx *vmx = to_vmx(vcpu);
7690 u32 vmx_instruction_info;
7691 unsigned long type, types;
7692 gva_t gva;
7693 struct x86_exception e;
7694 int vpid;
7695
7696 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7697 SECONDARY_EXEC_ENABLE_VPID) ||
7698 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7699 kvm_queue_exception(vcpu, UD_VECTOR);
7700 return 1;
7701 }
7702
7703 if (!nested_vmx_check_permission(vcpu))
7704 return 1;
7705
7706 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7707 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7708
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007709 types = (vmx->nested.nested_vmx_vpid_caps &
7710 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007711
Jim Mattson85c856b2016-10-26 08:38:38 -07007712 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007713 nested_vmx_failValid(vcpu,
7714 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007715 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007716 return 1;
7717 }
7718
7719 /* according to the intel vmx instruction reference, the memory
7720 * operand is read even if it isn't needed (e.g., for type==global)
7721 */
7722 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7723 vmx_instruction_info, false, &gva))
7724 return 1;
7725 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7726 sizeof(u32), &e)) {
7727 kvm_inject_page_fault(vcpu, &e);
7728 return 1;
7729 }
7730
7731 switch (type) {
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007732 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007733 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007734 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7735 if (!vpid) {
7736 nested_vmx_failValid(vcpu,
7737 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7738 skip_emulated_instruction(vcpu);
7739 return 1;
7740 }
7741 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007742 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007743 break;
7744 default:
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007745 WARN_ON_ONCE(1);
7746 skip_emulated_instruction(vcpu);
7747 return 1;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007748 }
7749
Jan Dakinevich04c776e2018-02-23 11:42:18 +01007750 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7751 nested_vmx_succeed(vcpu);
7752
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007753 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007754 return 1;
7755}
7756
Kai Huang843e4332015-01-28 10:54:28 +08007757static int handle_pml_full(struct kvm_vcpu *vcpu)
7758{
7759 unsigned long exit_qualification;
7760
7761 trace_kvm_pml_full(vcpu->vcpu_id);
7762
7763 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7764
7765 /*
7766 * PML buffer FULL happened while executing iret from NMI,
7767 * "blocked by NMI" bit has to be set before next VM entry.
7768 */
7769 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7770 cpu_has_virtual_nmis() &&
7771 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7772 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7773 GUEST_INTR_STATE_NMI);
7774
7775 /*
7776 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7777 * here.., and there's no userspace involvement needed for PML.
7778 */
7779 return 1;
7780}
7781
Yunhong Jiang64672c92016-06-13 14:19:59 -07007782static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7783{
7784 kvm_lapic_expired_hv_timer(vcpu);
7785 return 1;
7786}
7787
Nadav Har'El0140cae2011-05-25 23:06:28 +03007788/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007789 * The exit handlers return 1 if the exit was handled fully and guest execution
7790 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7791 * to be done to userspace and return 0.
7792 */
Mathias Krause772e0312012-08-30 01:30:19 +02007793static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007794 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7795 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007796 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007797 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007798 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007799 [EXIT_REASON_CR_ACCESS] = handle_cr,
7800 [EXIT_REASON_DR_ACCESS] = handle_dr,
7801 [EXIT_REASON_CPUID] = handle_cpuid,
7802 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7803 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7804 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7805 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007806 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007807 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007808 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007809 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007810 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007811 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007812 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007813 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007814 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007815 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007816 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007817 [EXIT_REASON_VMOFF] = handle_vmoff,
7818 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007819 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7820 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007821 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007822 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007823 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007824 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007825 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007826 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007827 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7828 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007829 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007830 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007831 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007832 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007833 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007834 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007835 [EXIT_REASON_XSAVES] = handle_xsaves,
7836 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007837 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007838 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007839};
7840
7841static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007842 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007843
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007844static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7845 struct vmcs12 *vmcs12)
7846{
7847 unsigned long exit_qualification;
7848 gpa_t bitmap, last_bitmap;
7849 unsigned int port;
7850 int size;
7851 u8 b;
7852
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007853 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007854 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007855
7856 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7857
7858 port = exit_qualification >> 16;
7859 size = (exit_qualification & 7) + 1;
7860
7861 last_bitmap = (gpa_t)-1;
7862 b = -1;
7863
7864 while (size > 0) {
7865 if (port < 0x8000)
7866 bitmap = vmcs12->io_bitmap_a;
7867 else if (port < 0x10000)
7868 bitmap = vmcs12->io_bitmap_b;
7869 else
Joe Perches1d804d02015-03-30 16:46:09 -07007870 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007871 bitmap += (port & 0x7fff) / 8;
7872
7873 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007874 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007875 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007876 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007877 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007878
7879 port++;
7880 size--;
7881 last_bitmap = bitmap;
7882 }
7883
Joe Perches1d804d02015-03-30 16:46:09 -07007884 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007885}
7886
Nadav Har'El644d7112011-05-25 23:12:35 +03007887/*
7888 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7889 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7890 * disinterest in the current event (read or write a specific MSR) by using an
7891 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7892 */
7893static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7894 struct vmcs12 *vmcs12, u32 exit_reason)
7895{
7896 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7897 gpa_t bitmap;
7898
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007899 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007900 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007901
7902 /*
7903 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7904 * for the four combinations of read/write and low/high MSR numbers.
7905 * First we need to figure out which of the four to use:
7906 */
7907 bitmap = vmcs12->msr_bitmap;
7908 if (exit_reason == EXIT_REASON_MSR_WRITE)
7909 bitmap += 2048;
7910 if (msr_index >= 0xc0000000) {
7911 msr_index -= 0xc0000000;
7912 bitmap += 1024;
7913 }
7914
7915 /* Then read the msr_index'th bit from this bitmap: */
7916 if (msr_index < 1024*8) {
7917 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007918 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007919 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007920 return 1 & (b >> (msr_index & 7));
7921 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007922 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007923}
7924
7925/*
7926 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7927 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7928 * intercept (via guest_host_mask etc.) the current event.
7929 */
7930static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7931 struct vmcs12 *vmcs12)
7932{
7933 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7934 int cr = exit_qualification & 15;
Jan H. Schönherra74bec42017-05-20 13:22:56 +02007935 int reg;
7936 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03007937
7938 switch ((exit_qualification >> 4) & 3) {
7939 case 0: /* mov to cr */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02007940 reg = (exit_qualification >> 8) & 15;
7941 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007942 switch (cr) {
7943 case 0:
7944 if (vmcs12->cr0_guest_host_mask &
7945 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007946 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007947 break;
7948 case 3:
7949 if ((vmcs12->cr3_target_count >= 1 &&
7950 vmcs12->cr3_target_value0 == val) ||
7951 (vmcs12->cr3_target_count >= 2 &&
7952 vmcs12->cr3_target_value1 == val) ||
7953 (vmcs12->cr3_target_count >= 3 &&
7954 vmcs12->cr3_target_value2 == val) ||
7955 (vmcs12->cr3_target_count >= 4 &&
7956 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007957 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007958 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007959 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007960 break;
7961 case 4:
7962 if (vmcs12->cr4_guest_host_mask &
7963 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007964 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007965 break;
7966 case 8:
7967 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007968 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007969 break;
7970 }
7971 break;
7972 case 2: /* clts */
7973 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7974 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007975 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007976 break;
7977 case 1: /* mov from cr */
7978 switch (cr) {
7979 case 3:
7980 if (vmcs12->cpu_based_vm_exec_control &
7981 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007982 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007983 break;
7984 case 8:
7985 if (vmcs12->cpu_based_vm_exec_control &
7986 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007987 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007988 break;
7989 }
7990 break;
7991 case 3: /* lmsw */
7992 /*
7993 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7994 * cr0. Other attempted changes are ignored, with no exit.
7995 */
Jan H. Schönherra74bec42017-05-20 13:22:56 +02007996 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03007997 if (vmcs12->cr0_guest_host_mask & 0xe &
7998 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007999 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008000 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8001 !(vmcs12->cr0_read_shadow & 0x1) &&
8002 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008003 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008004 break;
8005 }
Joe Perches1d804d02015-03-30 16:46:09 -07008006 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008007}
8008
8009/*
8010 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8011 * should handle it ourselves in L0 (and then continue L2). Only call this
8012 * when in is_guest_mode (L2).
8013 */
8014static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8015{
Nadav Har'El644d7112011-05-25 23:12:35 +03008016 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8017 struct vcpu_vmx *vmx = to_vmx(vcpu);
8018 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008019 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008020
Jan Kiszka542060e2014-01-04 18:47:21 +01008021 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8022 vmcs_readl(EXIT_QUALIFICATION),
8023 vmx->idt_vectoring_info,
8024 intr_info,
8025 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8026 KVM_ISA_VMX);
8027
David Matlackb7649e12017-08-01 14:00:40 -07008028 /*
8029 * The host physical addresses of some pages of guest memory
Jim Mattson46e24df2017-11-27 17:22:25 -06008030 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
8031 * Page). The CPU may write to these pages via their host
8032 * physical address while L2 is running, bypassing any
8033 * address-translation-based dirty tracking (e.g. EPT write
8034 * protection).
David Matlackb7649e12017-08-01 14:00:40 -07008035 *
8036 * Mark them dirty on every exit from L2 to prevent them from
8037 * getting out of sync with dirty tracking.
8038 */
8039 nested_mark_vmcs12_pages_dirty(vcpu);
8040
Nadav Har'El644d7112011-05-25 23:12:35 +03008041 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008042 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008043
8044 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008045 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8046 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008047 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008048 }
8049
8050 switch (exit_reason) {
8051 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008052 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008053 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008054 else if (is_page_fault(intr_info))
8055 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008056 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008057 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008058 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008059 else if (is_debug(intr_info) &&
8060 vcpu->guest_debug &
8061 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8062 return false;
8063 else if (is_breakpoint(intr_info) &&
8064 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8065 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008066 return vmcs12->exception_bitmap &
8067 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8068 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008069 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008070 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008071 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008072 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008073 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008074 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008075 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008076 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008077 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008078 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008079 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008080 case EXIT_REASON_HLT:
8081 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8082 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008083 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008084 case EXIT_REASON_INVLPG:
8085 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8086 case EXIT_REASON_RDPMC:
8087 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008088 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008089 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8090 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8091 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8092 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8093 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8094 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008095 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008096 /*
8097 * VMX instructions trap unconditionally. This allows L1 to
8098 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8099 */
Joe Perches1d804d02015-03-30 16:46:09 -07008100 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008101 case EXIT_REASON_CR_ACCESS:
8102 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8103 case EXIT_REASON_DR_ACCESS:
8104 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8105 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008106 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008107 case EXIT_REASON_MSR_READ:
8108 case EXIT_REASON_MSR_WRITE:
8109 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8110 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008111 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008112 case EXIT_REASON_MWAIT_INSTRUCTION:
8113 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008114 case EXIT_REASON_MONITOR_TRAP_FLAG:
8115 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008116 case EXIT_REASON_MONITOR_INSTRUCTION:
8117 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8118 case EXIT_REASON_PAUSE_INSTRUCTION:
8119 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8120 nested_cpu_has2(vmcs12,
8121 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8122 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008123 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008124 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008125 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008126 case EXIT_REASON_APIC_ACCESS:
8127 return nested_cpu_has2(vmcs12,
8128 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008129 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008130 case EXIT_REASON_EOI_INDUCED:
8131 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008132 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008133 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008134 /*
8135 * L0 always deals with the EPT violation. If nested EPT is
8136 * used, and the nested mmu code discovers that the address is
8137 * missing in the guest EPT table (EPT12), the EPT violation
8138 * will be injected with nested_ept_inject_page_fault()
8139 */
Joe Perches1d804d02015-03-30 16:46:09 -07008140 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008141 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008142 /*
8143 * L2 never uses directly L1's EPT, but rather L0's own EPT
8144 * table (shadow on EPT) or a merged EPT table that L0 built
8145 * (EPT on EPT). So any problems with the structure of the
8146 * table is L0's fault.
8147 */
Joe Perches1d804d02015-03-30 16:46:09 -07008148 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008149 case EXIT_REASON_WBINVD:
8150 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8151 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008152 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008153 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8154 /*
8155 * This should never happen, since it is not possible to
8156 * set XSS to a non-zero value---neither in L1 nor in L2.
8157 * If if it were, XSS would have to be checked against
8158 * the XSS exit bitmap in vmcs12.
8159 */
8160 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008161 case EXIT_REASON_PREEMPTION_TIMER:
8162 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008163 case EXIT_REASON_PML_FULL:
8164 /* We don't expose PML support to L1. */
8165 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008166 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008167 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008168 }
8169}
8170
Avi Kivity586f9602010-11-18 13:09:54 +02008171static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8172{
8173 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8174 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8175}
8176
Kai Huanga3eaa862015-11-04 13:46:05 +08008177static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008178{
Kai Huanga3eaa862015-11-04 13:46:05 +08008179 if (vmx->pml_pg) {
8180 __free_page(vmx->pml_pg);
8181 vmx->pml_pg = NULL;
8182 }
Kai Huang843e4332015-01-28 10:54:28 +08008183}
8184
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008185static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008186{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008187 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008188 u64 *pml_buf;
8189 u16 pml_idx;
8190
8191 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8192
8193 /* Do nothing if PML buffer is empty */
8194 if (pml_idx == (PML_ENTITY_NUM - 1))
8195 return;
8196
8197 /* PML index always points to next available PML buffer entity */
8198 if (pml_idx >= PML_ENTITY_NUM)
8199 pml_idx = 0;
8200 else
8201 pml_idx++;
8202
8203 pml_buf = page_address(vmx->pml_pg);
8204 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8205 u64 gpa;
8206
8207 gpa = pml_buf[pml_idx];
8208 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008209 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008210 }
8211
8212 /* reset PML index */
8213 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8214}
8215
8216/*
8217 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8218 * Called before reporting dirty_bitmap to userspace.
8219 */
8220static void kvm_flush_pml_buffers(struct kvm *kvm)
8221{
8222 int i;
8223 struct kvm_vcpu *vcpu;
8224 /*
8225 * We only need to kick vcpu out of guest mode here, as PML buffer
8226 * is flushed at beginning of all VMEXITs, and it's obvious that only
8227 * vcpus running in guest are possible to have unflushed GPAs in PML
8228 * buffer.
8229 */
8230 kvm_for_each_vcpu(i, vcpu, kvm)
8231 kvm_vcpu_kick(vcpu);
8232}
8233
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008234static void vmx_dump_sel(char *name, uint32_t sel)
8235{
8236 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008237 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008238 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8239 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8240 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8241}
8242
8243static void vmx_dump_dtsel(char *name, uint32_t limit)
8244{
8245 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8246 name, vmcs_read32(limit),
8247 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8248}
8249
8250static void dump_vmcs(void)
8251{
8252 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8253 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8254 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8255 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8256 u32 secondary_exec_control = 0;
8257 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008258 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008259 int i, n;
8260
8261 if (cpu_has_secondary_exec_ctrls())
8262 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8263
8264 pr_err("*** Guest State ***\n");
8265 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8266 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8267 vmcs_readl(CR0_GUEST_HOST_MASK));
8268 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8269 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8270 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8271 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8272 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8273 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008274 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8275 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8276 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8277 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008278 }
8279 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8280 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8281 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8282 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8283 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8284 vmcs_readl(GUEST_SYSENTER_ESP),
8285 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8286 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8287 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8288 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8289 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8290 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8291 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8292 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8293 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8294 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8295 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8296 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8297 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008298 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8299 efer, vmcs_read64(GUEST_IA32_PAT));
8300 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8301 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008302 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8303 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008304 pr_err("PerfGlobCtl = 0x%016llx\n",
8305 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008306 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008307 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008308 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8309 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8310 vmcs_read32(GUEST_ACTIVITY_STATE));
8311 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8312 pr_err("InterruptStatus = %04x\n",
8313 vmcs_read16(GUEST_INTR_STATUS));
8314
8315 pr_err("*** Host State ***\n");
8316 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8317 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8318 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8319 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8320 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8321 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8322 vmcs_read16(HOST_TR_SELECTOR));
8323 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8324 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8325 vmcs_readl(HOST_TR_BASE));
8326 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8327 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8328 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8329 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8330 vmcs_readl(HOST_CR4));
8331 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8332 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8333 vmcs_read32(HOST_IA32_SYSENTER_CS),
8334 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8335 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008336 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8337 vmcs_read64(HOST_IA32_EFER),
8338 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008339 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008340 pr_err("PerfGlobCtl = 0x%016llx\n",
8341 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008342
8343 pr_err("*** Control State ***\n");
8344 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8345 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8346 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8347 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8348 vmcs_read32(EXCEPTION_BITMAP),
8349 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8350 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8351 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8352 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8353 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8354 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8355 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8356 vmcs_read32(VM_EXIT_INTR_INFO),
8357 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8358 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8359 pr_err(" reason=%08x qualification=%016lx\n",
8360 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8361 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8362 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8363 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008364 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008365 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008366 pr_err("TSC Multiplier = 0x%016llx\n",
8367 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008368 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8369 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8370 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8371 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8372 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008373 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008374 n = vmcs_read32(CR3_TARGET_COUNT);
8375 for (i = 0; i + 1 < n; i += 4)
8376 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8377 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8378 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8379 if (i < n)
8380 pr_err("CR3 target%u=%016lx\n",
8381 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8382 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8383 pr_err("PLE Gap=%08x Window=%08x\n",
8384 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8385 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8386 pr_err("Virtual processor ID = 0x%04x\n",
8387 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8388}
8389
Avi Kivity6aa8b732006-12-10 02:21:36 -08008390/*
8391 * The guest has exited. See if we can fix it or if we need userspace
8392 * assistance.
8393 */
Avi Kivity851ba692009-08-24 11:10:17 +03008394static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008395{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008396 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008397 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008398 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008399
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008400 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8401
Kai Huang843e4332015-01-28 10:54:28 +08008402 /*
8403 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8404 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8405 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8406 * mode as if vcpus is in root mode, the PML buffer must has been
8407 * flushed already.
8408 */
8409 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008410 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008411
Mohammed Gamal80ced182009-09-01 12:48:18 +02008412 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008413 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008414 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008415
Nadav Har'El644d7112011-05-25 23:12:35 +03008416 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008417 nested_vmx_vmexit(vcpu, exit_reason,
8418 vmcs_read32(VM_EXIT_INTR_INFO),
8419 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008420 return 1;
8421 }
8422
Mohammed Gamal51207022010-05-31 22:40:54 +03008423 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008424 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008425 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8426 vcpu->run->fail_entry.hardware_entry_failure_reason
8427 = exit_reason;
8428 return 0;
8429 }
8430
Avi Kivity29bd8a72007-09-10 17:27:03 +03008431 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008432 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8433 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008434 = vmcs_read32(VM_INSTRUCTION_ERROR);
8435 return 0;
8436 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008437
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008438 /*
8439 * Note:
8440 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8441 * delivery event since it indicates guest is accessing MMIO.
8442 * The vm-exit can be triggered again after return to guest that
8443 * will cause infinite loop.
8444 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008445 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008446 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008447 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008448 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008449 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8450 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8451 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8452 vcpu->run->internal.ndata = 2;
8453 vcpu->run->internal.data[0] = vectoring_info;
8454 vcpu->run->internal.data[1] = exit_reason;
8455 return 0;
8456 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008457
Nadav Har'El644d7112011-05-25 23:12:35 +03008458 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8459 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008460 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008461 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008462 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008463 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008464 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008465 /*
8466 * This CPU don't support us in finding the end of an
8467 * NMI-blocked window if the guest runs with IRQs
8468 * disabled. So we pull the trigger after 1 s of
8469 * futile waiting, but inform the user about this.
8470 */
8471 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8472 "state on VCPU %d after 1 s timeout\n",
8473 __func__, vcpu->vcpu_id);
8474 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008475 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008476 }
8477
Avi Kivity6aa8b732006-12-10 02:21:36 -08008478 if (exit_reason < kvm_vmx_max_exit_handlers
8479 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008480 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008481 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008482 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8483 kvm_queue_exception(vcpu, UD_VECTOR);
8484 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008485 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008486}
8487
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008488static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008489{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008490 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8491
8492 if (is_guest_mode(vcpu) &&
8493 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8494 return;
8495
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008496 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008497 vmcs_write32(TPR_THRESHOLD, 0);
8498 return;
8499 }
8500
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008501 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008502}
8503
Yang Zhang8d146952013-01-25 10:18:50 +08008504static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8505{
8506 u32 sec_exec_control;
8507
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008508 /* Postpone execution until vmcs01 is the current VMCS. */
8509 if (is_guest_mode(vcpu)) {
8510 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8511 return;
8512 }
8513
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008514 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008515 return;
8516
Paolo Bonzini35754c92015-07-29 12:05:37 +02008517 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008518 return;
8519
8520 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8521
8522 if (set) {
8523 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8524 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8525 } else {
8526 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8527 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattson8386ff52017-03-16 13:53:59 -07008528 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008529 }
8530 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8531
Paolo Bonzini6236b782018-01-16 16:51:18 +01008532 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008533}
8534
Tang Chen38b99172014-09-24 15:57:54 +08008535static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8536{
8537 struct vcpu_vmx *vmx = to_vmx(vcpu);
8538
8539 /*
8540 * Currently we do not handle the nested case where L2 has an
8541 * APIC access page of its own; that page is still pinned.
8542 * Hence, we skip the case where the VCPU is in guest mode _and_
8543 * L1 prepared an APIC access page for L2.
8544 *
8545 * For the case where L1 and L2 share the same APIC access page
8546 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8547 * in the vmcs12), this function will only update either the vmcs01
8548 * or the vmcs02. If the former, the vmcs02 will be updated by
8549 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8550 * the next L2->L1 exit.
8551 */
8552 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008553 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattson8386ff52017-03-16 13:53:59 -07008554 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008555 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattson8386ff52017-03-16 13:53:59 -07008556 vmx_flush_tlb_ept_only(vcpu);
8557 }
Tang Chen38b99172014-09-24 15:57:54 +08008558}
8559
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008560static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008561{
8562 u16 status;
8563 u8 old;
8564
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008565 if (max_isr == -1)
8566 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008567
8568 status = vmcs_read16(GUEST_INTR_STATUS);
8569 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008570 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008571 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008572 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008573 vmcs_write16(GUEST_INTR_STATUS, status);
8574 }
8575}
8576
8577static void vmx_set_rvi(int vector)
8578{
8579 u16 status;
8580 u8 old;
8581
Wei Wang4114c272014-11-05 10:53:43 +08008582 if (vector == -1)
8583 vector = 0;
8584
Yang Zhangc7c9c562013-01-25 10:18:51 +08008585 status = vmcs_read16(GUEST_INTR_STATUS);
8586 old = (u8)status & 0xff;
8587 if ((u8)vector != old) {
8588 status &= ~0xff;
8589 status |= (u8)vector;
8590 vmcs_write16(GUEST_INTR_STATUS, status);
8591 }
8592}
8593
8594static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8595{
Wanpeng Li963fee12014-07-17 19:03:00 +08008596 if (!is_guest_mode(vcpu)) {
8597 vmx_set_rvi(max_irr);
8598 return;
8599 }
8600
Wei Wang4114c272014-11-05 10:53:43 +08008601 if (max_irr == -1)
8602 return;
8603
Wanpeng Li963fee12014-07-17 19:03:00 +08008604 /*
Wei Wang4114c272014-11-05 10:53:43 +08008605 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8606 * handles it.
8607 */
8608 if (nested_exit_on_intr(vcpu))
8609 return;
8610
8611 /*
8612 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008613 * is run without virtual interrupt delivery.
8614 */
8615 if (!kvm_event_needs_reinjection(vcpu) &&
8616 vmx_interrupt_allowed(vcpu)) {
8617 kvm_queue_interrupt(vcpu, max_irr, false);
8618 vmx_inject_irq(vcpu);
8619 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008620}
8621
Andrey Smetanin63086302015-11-10 15:36:32 +03008622static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008623{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008624 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008625 return;
8626
Yang Zhangc7c9c562013-01-25 10:18:51 +08008627 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8628 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8629 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8630 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8631}
8632
Avi Kivity51aa01d2010-07-20 14:31:20 +03008633static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008634{
Avi Kivity00eba012011-03-07 17:24:54 +02008635 u32 exit_intr_info;
8636
8637 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8638 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8639 return;
8640
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008641 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008642 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008643
8644 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008645 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008646 kvm_machine_check();
8647
Gleb Natapov20f65982009-05-11 13:35:55 +03008648 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008649 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008650 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008651 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008652 kvm_after_handle_nmi(&vmx->vcpu);
8653 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008654}
Gleb Natapov20f65982009-05-11 13:35:55 +03008655
Yang Zhanga547c6d2013-04-11 19:25:10 +08008656static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8657{
8658 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008659 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008660
8661 /*
8662 * If external interrupt exists, IF bit is set in rflags/eflags on the
8663 * interrupt stack frame, and interrupt will be enabled on a return
8664 * from interrupt handler.
8665 */
8666 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8667 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8668 unsigned int vector;
8669 unsigned long entry;
8670 gate_desc *desc;
8671 struct vcpu_vmx *vmx = to_vmx(vcpu);
8672#ifdef CONFIG_X86_64
8673 unsigned long tmp;
8674#endif
8675
8676 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8677 desc = (gate_desc *)vmx->host_idt_base + vector;
8678 entry = gate_offset(*desc);
8679 asm volatile(
8680#ifdef CONFIG_X86_64
8681 "mov %%" _ASM_SP ", %[sp]\n\t"
8682 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8683 "push $%c[ss]\n\t"
8684 "push %[sp]\n\t"
8685#endif
8686 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008687 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008688 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +08008689 :
8690#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008691 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008692#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008693 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008694 :
Peter Zijlstraec86a1d2018-01-25 10:58:14 +01008695 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008696 [ss]"i"(__KERNEL_DS),
8697 [cs]"i"(__KERNEL_CS)
8698 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008699 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008700}
8701
Tom Lendackyb9655922018-05-10 22:06:39 +02008702static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008703{
Tom Lendackyb9655922018-05-10 22:06:39 +02008704 switch (index) {
8705 case MSR_IA32_SMBASE:
8706 /*
8707 * We cannot do SMM unless we can run the guest in big
8708 * real mode.
8709 */
8710 return enable_unrestricted_guest || emulate_invalid_guest_state;
8711 case MSR_AMD64_VIRT_SPEC_CTRL:
8712 /* This is AMD only. */
8713 return false;
8714 default:
8715 return true;
8716 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008717}
8718
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008719static bool vmx_mpx_supported(void)
8720{
8721 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8722 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8723}
8724
Wanpeng Li55412b22014-12-02 19:21:30 +08008725static bool vmx_xsaves_supported(void)
8726{
8727 return vmcs_config.cpu_based_2nd_exec_ctrl &
8728 SECONDARY_EXEC_XSAVES;
8729}
8730
Avi Kivity51aa01d2010-07-20 14:31:20 +03008731static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8732{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008733 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008734 bool unblock_nmi;
8735 u8 vector;
8736 bool idtv_info_valid;
8737
8738 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008739
Avi Kivitycf393f72008-07-01 16:20:21 +03008740 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008741 if (vmx->nmi_known_unmasked)
8742 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008743 /*
8744 * Can't use vmx->exit_intr_info since we're not sure what
8745 * the exit reason is.
8746 */
8747 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008748 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8749 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8750 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008751 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008752 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8753 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008754 * SDM 3: 23.2.2 (September 2008)
8755 * Bit 12 is undefined in any of the following cases:
8756 * If the VM exit sets the valid bit in the IDT-vectoring
8757 * information field.
8758 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008759 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008760 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8761 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008762 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8763 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008764 else
8765 vmx->nmi_known_unmasked =
8766 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8767 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008768 } else if (unlikely(vmx->soft_vnmi_blocked))
8769 vmx->vnmi_blocked_time +=
8770 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008771}
8772
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008773static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008774 u32 idt_vectoring_info,
8775 int instr_len_field,
8776 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008777{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008778 u8 vector;
8779 int type;
8780 bool idtv_info_valid;
8781
8782 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008783
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008784 vcpu->arch.nmi_injected = false;
8785 kvm_clear_exception_queue(vcpu);
8786 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008787
8788 if (!idtv_info_valid)
8789 return;
8790
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008791 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008792
Avi Kivity668f6122008-07-02 09:28:55 +03008793 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8794 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008795
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008796 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008797 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008798 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008799 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008800 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008801 * Clear bit "block by NMI" before VM entry if a NMI
8802 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008803 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008804 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008805 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008806 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008807 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008808 /* fall through */
8809 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008810 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008811 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008812 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008813 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008814 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008815 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008816 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008817 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008818 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008819 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008820 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008821 break;
8822 default:
8823 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008824 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008825}
8826
Avi Kivity83422e12010-07-20 14:43:23 +03008827static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8828{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008829 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008830 VM_EXIT_INSTRUCTION_LEN,
8831 IDT_VECTORING_ERROR_CODE);
8832}
8833
Avi Kivityb463a6f2010-07-20 15:06:17 +03008834static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8835{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008836 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008837 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8838 VM_ENTRY_INSTRUCTION_LEN,
8839 VM_ENTRY_EXCEPTION_ERROR_CODE);
8840
8841 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8842}
8843
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008844static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8845{
8846 int i, nr_msrs;
8847 struct perf_guest_switch_msr *msrs;
8848
8849 msrs = perf_guest_get_msrs(&nr_msrs);
8850
8851 if (!msrs)
8852 return;
8853
8854 for (i = 0; i < nr_msrs; i++)
8855 if (msrs[i].host == msrs[i].guest)
8856 clear_atomic_switch_msr(vmx, msrs[i].msr);
8857 else
8858 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8859 msrs[i].host);
8860}
8861
Yunhong Jiang64672c92016-06-13 14:19:59 -07008862void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8863{
8864 struct vcpu_vmx *vmx = to_vmx(vcpu);
8865 u64 tscl;
8866 u32 delta_tsc;
8867
8868 if (vmx->hv_deadline_tsc == -1)
8869 return;
8870
8871 tscl = rdtsc();
8872 if (vmx->hv_deadline_tsc > tscl)
8873 /* sure to be 32 bit only because checked on set_hv_timer */
8874 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8875 cpu_preemption_timer_multi);
8876 else
8877 delta_tsc = 0;
8878
8879 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8880}
8881
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008882static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008883{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008884 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008885 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008886
8887 /* Record the guest's net vcpu time for enforced NMI injections. */
8888 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8889 vmx->entry_time = ktime_get();
8890
8891 /* Don't enter VMX if guest state is invalid, let the exit handler
8892 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008893 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008894 return;
8895
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008896 if (vmx->ple_window_dirty) {
8897 vmx->ple_window_dirty = false;
8898 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8899 }
8900
Abel Gordon012f83c2013-04-18 14:39:25 +03008901 if (vmx->nested.sync_shadow_vmcs) {
8902 copy_vmcs12_to_shadow(vmx);
8903 vmx->nested.sync_shadow_vmcs = false;
8904 }
8905
Avi Kivity104f2262010-11-18 13:12:52 +02008906 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8907 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8908 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8909 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8910
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008911 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008912 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8913 vmcs_writel(HOST_CR4, cr4);
8914 vmx->host_state.vmcs_host_cr4 = cr4;
8915 }
8916
Avi Kivity104f2262010-11-18 13:12:52 +02008917 /* When single-stepping over STI and MOV SS, we must clear the
8918 * corresponding interruptibility bits in the guest state. Otherwise
8919 * vmentry fails as it then expects bit 14 (BS) in pending debug
8920 * exceptions being set, but that's not correct for the guest debugging
8921 * case. */
8922 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8923 vmx_set_interrupt_shadow(vcpu, 0);
8924
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008925 if (vmx->guest_pkru_valid)
8926 __write_pkru(vmx->guest_pkru);
8927
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008928 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008929 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008930
Yunhong Jiang64672c92016-06-13 14:19:59 -07008931 vmx_arm_hv_timer(vcpu);
8932
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01008933 /*
8934 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
8935 * it's non-zero. Since vmentry is serialising on affected CPUs, there
8936 * is no need to worry about the conditional branch over the wrmsr
8937 * being speculatively taken.
8938 */
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02008939 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01008940
Nadav Har'Eld462b812011-05-24 15:26:10 +03008941 vmx->__launched = vmx->loaded_vmcs->launched;
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02008942
Avi Kivity104f2262010-11-18 13:12:52 +02008943 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008944 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008945 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8946 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8947 "push %%" _ASM_CX " \n\t"
8948 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008949 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008950 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008951 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008952 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008953 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008954 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8955 "mov %%cr2, %%" _ASM_DX " \n\t"
8956 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008957 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008958 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008959 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008960 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008961 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008962 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008963 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8964 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8965 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8966 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8967 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8968 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008969#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008970 "mov %c[r8](%0), %%r8 \n\t"
8971 "mov %c[r9](%0), %%r9 \n\t"
8972 "mov %c[r10](%0), %%r10 \n\t"
8973 "mov %c[r11](%0), %%r11 \n\t"
8974 "mov %c[r12](%0), %%r12 \n\t"
8975 "mov %c[r13](%0), %%r13 \n\t"
8976 "mov %c[r14](%0), %%r14 \n\t"
8977 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008978#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008979 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008980
Avi Kivity6aa8b732006-12-10 02:21:36 -08008981 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008982 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008983 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008984 "jmp 2f \n\t"
8985 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8986 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008987 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008988 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008989 "pop %0 \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08008990 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008991 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8992 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8993 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8994 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8995 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8996 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8997 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008998#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008999 "mov %%r8, %c[r8](%0) \n\t"
9000 "mov %%r9, %c[r9](%0) \n\t"
9001 "mov %%r10, %c[r10](%0) \n\t"
9002 "mov %%r11, %c[r11](%0) \n\t"
9003 "mov %%r12, %c[r12](%0) \n\t"
9004 "mov %%r13, %c[r13](%0) \n\t"
9005 "mov %%r14, %c[r14](%0) \n\t"
9006 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson491c0ca2018-01-03 14:31:38 -08009007 "xor %%r8d, %%r8d \n\t"
9008 "xor %%r9d, %%r9d \n\t"
9009 "xor %%r10d, %%r10d \n\t"
9010 "xor %%r11d, %%r11d \n\t"
9011 "xor %%r12d, %%r12d \n\t"
9012 "xor %%r13d, %%r13d \n\t"
9013 "xor %%r14d, %%r14d \n\t"
9014 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08009015#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03009016 "mov %%cr2, %%" _ASM_AX " \n\t"
9017 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03009018
Jim Mattson491c0ca2018-01-03 14:31:38 -08009019 "xor %%eax, %%eax \n\t"
9020 "xor %%ebx, %%ebx \n\t"
9021 "xor %%esi, %%esi \n\t"
9022 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009023 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03009024 ".pushsection .rodata \n\t"
9025 ".global vmx_return \n\t"
9026 "vmx_return: " _ASM_PTR " 2b \n\t"
9027 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02009028 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03009029 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02009030 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03009031 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009032 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
9033 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
9034 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
9035 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
9036 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
9037 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9038 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009039#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009040 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9041 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9042 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9043 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9044 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9045 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9046 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9047 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009048#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009049 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9050 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009051 : "cc", "memory"
9052#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009053 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009054 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009055#else
9056 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009057#endif
9058 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009059
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009060 /*
9061 * We do not use IBRS in the kernel. If this vCPU has used the
9062 * SPEC_CTRL MSR it may have left it on; save the value and
9063 * turn it off. This is much more efficient than blindly adding
9064 * it to the atomic save/restore list. Especially as the former
9065 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
9066 *
9067 * For non-nested case:
9068 * If the L01 MSR bitmap does not intercept the MSR, then we need to
9069 * save it.
9070 *
9071 * For nested case:
9072 * If the L02 MSR bitmap does not intercept the MSR, then we need to
9073 * save it.
9074 */
Paolo Bonzinif750e152018-02-22 16:43:18 +01009075 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonzinia175d512018-02-22 16:43:17 +01009076 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009077
Thomas Gleixner1189cbf2018-05-09 23:01:01 +02009078 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009079
David Woodhousec1ddd992018-01-12 11:11:27 +00009080 /* Eliminate branch target predictions from guest mode */
9081 vmexit_fill_RSB();
9082
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009083 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9084 if (debugctlmsr)
9085 update_debugctlmsr(debugctlmsr);
9086
Avi Kivityaa67f602012-08-01 16:48:03 +03009087#ifndef CONFIG_X86_64
9088 /*
9089 * The sysexit path does not restore ds/es, so we must set them to
9090 * a reasonable value ourselves.
9091 *
9092 * We can't defer this to vmx_load_host_state() since that function
9093 * may be executed in interrupt context, which saves and restore segments
9094 * around it, nullifying its effect.
9095 */
9096 loadsegment(ds, __USER_DS);
9097 loadsegment(es, __USER_DS);
9098#endif
9099
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009100 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009101 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009102 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009103 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009104 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009105 vcpu->arch.regs_dirty = 0;
9106
Avi Kivity1155f762007-11-22 11:30:47 +02009107 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9108
Nadav Har'Eld462b812011-05-24 15:26:10 +03009109 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009110
Avi Kivity51aa01d2010-07-20 14:31:20 +03009111 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009112
Gleb Natapove0b890d2013-09-25 12:51:33 +03009113 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009114 * eager fpu is enabled if PKEY is supported and CR4 is switched
9115 * back on host, so it is safe to read guest PKRU from current
9116 * XSAVE.
9117 */
9118 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9119 vmx->guest_pkru = __read_pkru();
9120 if (vmx->guest_pkru != vmx->host_pkru) {
9121 vmx->guest_pkru_valid = true;
9122 __write_pkru(vmx->host_pkru);
9123 } else
9124 vmx->guest_pkru_valid = false;
9125 }
9126
9127 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009128 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9129 * we did not inject a still-pending event to L1 now because of
9130 * nested_run_pending, we need to re-enable this bit.
9131 */
9132 if (vmx->nested.nested_run_pending)
9133 kvm_make_request(KVM_REQ_EVENT, vcpu);
9134
9135 vmx->nested.nested_run_pending = 0;
9136
Avi Kivity51aa01d2010-07-20 14:31:20 +03009137 vmx_complete_atomic_exit(vmx);
9138 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009139 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009140}
9141
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009142static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9143{
9144 struct vcpu_vmx *vmx = to_vmx(vcpu);
9145 int cpu;
9146
9147 if (vmx->loaded_vmcs == &vmx->vmcs01)
9148 return;
9149
9150 cpu = get_cpu();
9151 vmx->loaded_vmcs = &vmx->vmcs01;
9152 vmx_vcpu_put(vcpu);
9153 vmx_vcpu_load(vcpu, cpu);
9154 vcpu->cpu = cpu;
9155 put_cpu();
9156}
9157
Jim Mattson2f1fe812016-07-08 15:36:06 -07009158/*
9159 * Ensure that the current vmcs of the logical processor is the
9160 * vmcs01 of the vcpu before calling free_nested().
9161 */
9162static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9163{
9164 struct vcpu_vmx *vmx = to_vmx(vcpu);
9165 int r;
9166
9167 r = vcpu_load(vcpu);
9168 BUG_ON(r);
9169 vmx_load_vmcs01(vcpu);
9170 free_nested(vmx);
9171 vcpu_put(vcpu);
9172}
9173
Avi Kivity6aa8b732006-12-10 02:21:36 -08009174static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9175{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009176 struct vcpu_vmx *vmx = to_vmx(vcpu);
9177
Kai Huang843e4332015-01-28 10:54:28 +08009178 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009179 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009180 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009181 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009182 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009183 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009184 kfree(vmx->guest_msrs);
9185 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009186 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009187}
9188
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009189static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009190{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009191 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009192 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini6236b782018-01-16 16:51:18 +01009193 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +03009194 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009195
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009196 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009197 return ERR_PTR(-ENOMEM);
9198
Wanpeng Li991e7a02015-09-16 17:30:05 +08009199 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009200
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009201 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9202 if (err)
9203 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009204
Peter Feiner4e595162016-07-07 14:49:58 -07009205 err = -ENOMEM;
9206
9207 /*
9208 * If PML is turned on, failure on enabling PML just results in failure
9209 * of creating the vcpu, therefore we can simplify PML logic (by
9210 * avoiding dealing with cases, such as enabling PML partially on vcpus
9211 * for the guest, etc.
9212 */
9213 if (enable_pml) {
9214 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9215 if (!vmx->pml_pg)
9216 goto uninit_vcpu;
9217 }
9218
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009219 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009220 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9221 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009222
Peter Feiner4e595162016-07-07 14:49:58 -07009223 if (!vmx->guest_msrs)
9224 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009225
Nadav Har'Eld462b812011-05-24 15:26:10 +03009226 if (!vmm_exclusive)
9227 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
Paolo Bonziniff546f92018-01-11 12:16:15 +01009228 err = alloc_loaded_vmcs(&vmx->vmcs01);
Nadav Har'Eld462b812011-05-24 15:26:10 +03009229 if (!vmm_exclusive)
9230 kvm_cpu_vmxoff();
Paolo Bonziniff546f92018-01-11 12:16:15 +01009231 if (err < 0)
9232 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009233
Paolo Bonzini6236b782018-01-16 16:51:18 +01009234 msr_bitmap = vmx->vmcs01.msr_bitmap;
9235 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
9236 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
9237 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
9238 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
9239 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
9240 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
9241 vmx->msr_bitmap_mode = 0;
9242
Paolo Bonziniff546f92018-01-11 12:16:15 +01009243 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +03009244 cpu = get_cpu();
9245 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009246 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009247 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009248 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009249 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009250 if (err)
9251 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009252 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009253 err = alloc_apic_access_page(kvm);
9254 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009255 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009256 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009257
Sheng Yangb927a3c2009-07-21 10:42:48 +08009258 if (enable_ept) {
9259 if (!kvm->arch.ept_identity_map_addr)
9260 kvm->arch.ept_identity_map_addr =
9261 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009262 err = init_rmode_identity_map(kvm);
9263 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009264 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009265 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009266
Wanpeng Li5c614b32015-10-13 09:18:36 -07009267 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009268 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009269 vmx->nested.vpid02 = allocate_vpid();
9270 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009271
Wincy Van705699a2015-02-03 23:58:17 +08009272 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009273 vmx->nested.current_vmptr = -1ull;
9274 vmx->nested.current_vmcs12 = NULL;
9275
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009276 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9277
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009278 /*
9279 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9280 * or POSTED_INTR_WAKEUP_VECTOR.
9281 */
9282 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9283 vmx->pi_desc.sn = 1;
9284
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009285 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009286
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009287free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009288 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009289 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009290free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009291 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009292free_pml:
9293 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009294uninit_vcpu:
9295 kvm_vcpu_uninit(&vmx->vcpu);
9296free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009297 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009298 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009299 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009300}
9301
Yang, Sheng002c7f72007-07-31 14:23:01 +03009302static void __init vmx_check_processor_compat(void *rtn)
9303{
9304 struct vmcs_config vmcs_conf;
9305
9306 *(int *)rtn = 0;
9307 if (setup_vmcs_config(&vmcs_conf) < 0)
9308 *(int *)rtn = -EIO;
9309 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9310 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9311 smp_processor_id());
9312 *(int *)rtn = -EIO;
9313 }
9314}
9315
Sheng Yang67253af2008-04-25 10:20:22 +08009316static int get_ept_level(void)
9317{
9318 return VMX_EPT_DEFAULT_GAW + 1;
9319}
9320
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009321static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009322{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009323 u8 cache;
9324 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009325
Sheng Yang522c68c2009-04-27 20:35:43 +08009326 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009327 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009328 * 2. EPT with VT-d:
9329 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009330 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009331 * b. VT-d with snooping control feature: snooping control feature of
9332 * VT-d engine can guarantee the cache correctness. Just set it
9333 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009334 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009335 * consistent with host MTRR
9336 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009337 if (is_mmio) {
9338 cache = MTRR_TYPE_UNCACHABLE;
9339 goto exit;
9340 }
9341
9342 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009343 ipat = VMX_EPT_IPAT_BIT;
9344 cache = MTRR_TYPE_WRBACK;
9345 goto exit;
9346 }
9347
9348 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9349 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009350 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009351 cache = MTRR_TYPE_WRBACK;
9352 else
9353 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009354 goto exit;
9355 }
9356
Xiao Guangrongff536042015-06-15 16:55:22 +08009357 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009358
9359exit:
9360 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009361}
9362
Sheng Yang17cc3932010-01-05 19:02:27 +08009363static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009364{
Sheng Yang878403b2010-01-05 19:02:29 +08009365 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9366 return PT_DIRECTORY_LEVEL;
9367 else
9368 /* For shadow and EPT supported 1GB page */
9369 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009370}
9371
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009372static void vmcs_set_secondary_exec_control(u32 new_ctl)
9373{
9374 /*
9375 * These bits in the secondary execution controls field
9376 * are dynamic, the others are mostly based on the hypervisor
9377 * architecture and the guest's CPUID. Do not touch the
9378 * dynamic bits.
9379 */
9380 u32 mask =
9381 SECONDARY_EXEC_SHADOW_VMCS |
9382 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9383 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9384
9385 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9386
9387 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9388 (new_ctl & ~mask) | (cur_ctl & mask));
9389}
9390
Sheng Yang0e851882009-12-18 16:48:46 +08009391static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9392{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009393 struct kvm_cpuid_entry2 *best;
9394 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009395 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009396
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009397 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009398 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9399 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009400 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009401
Paolo Bonzini8b972652015-09-15 17:34:42 +02009402 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009403 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009404 vmx->nested.nested_vmx_secondary_ctls_high |=
9405 SECONDARY_EXEC_RDTSCP;
9406 else
9407 vmx->nested.nested_vmx_secondary_ctls_high &=
9408 ~SECONDARY_EXEC_RDTSCP;
9409 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009410 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009411
Mao, Junjiead756a12012-07-02 01:18:48 +00009412 /* Exposing INVPCID only when PCID is exposed */
9413 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9414 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009415 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9416 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009417 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009418
Mao, Junjiead756a12012-07-02 01:18:48 +00009419 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009420 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009421 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009422
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009423 if (cpu_has_secondary_exec_ctrls())
9424 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009425
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009426 if (nested_vmx_allowed(vcpu))
9427 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9428 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9429 else
9430 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9431 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009432}
9433
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009434static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9435{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009436 if (func == 1 && nested)
9437 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009438}
9439
Yang Zhang25d92082013-08-06 12:00:32 +03009440static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9441 struct x86_exception *fault)
9442{
Jan Kiszka533558b2014-01-04 18:47:20 +01009443 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9444 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009445
9446 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009447 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009448 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009449 exit_reason = EXIT_REASON_EPT_VIOLATION;
9450 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009451 vmcs12->guest_physical_address = fault->address;
9452}
9453
Nadav Har'El155a97a2013-08-05 11:07:16 +03009454/* Callbacks for nested_ept_init_mmu_context: */
9455
9456static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9457{
9458 /* return the page table to be shadowed - in our case, EPT12 */
9459 return get_vmcs12(vcpu)->ept_pointer;
9460}
9461
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009462static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009463{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009464 WARN_ON(mmu_is_nested(vcpu));
9465 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009466 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9467 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009468 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9469 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9470 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9471
9472 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009473}
9474
9475static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9476{
9477 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9478}
9479
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009480static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9481 u16 error_code)
9482{
9483 bool inequality, bit;
9484
9485 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9486 inequality =
9487 (error_code & vmcs12->page_fault_error_code_mask) !=
9488 vmcs12->page_fault_error_code_match;
9489 return inequality ^ bit;
9490}
9491
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009492static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9493 struct x86_exception *fault)
9494{
9495 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9496
9497 WARN_ON(!is_guest_mode(vcpu));
9498
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009499 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009500 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9501 vmcs_read32(VM_EXIT_INTR_INFO),
9502 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009503 else
9504 kvm_inject_page_fault(vcpu, fault);
9505}
9506
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009507static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9508 struct vmcs12 *vmcs12)
9509{
9510 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009511 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009512
9513 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009514 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9515 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009516 return false;
9517
9518 /*
9519 * Translate L1 physical address to host physical
9520 * address for vmcs02. Keep the page pinned, so this
9521 * physical address remains valid. We keep a reference
9522 * to it so we can release it later.
9523 */
9524 if (vmx->nested.apic_access_page) /* shouldn't happen */
9525 nested_release_page(vmx->nested.apic_access_page);
9526 vmx->nested.apic_access_page =
9527 nested_get_page(vcpu, vmcs12->apic_access_addr);
9528 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009529
9530 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009531 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9532 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009533 return false;
9534
9535 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9536 nested_release_page(vmx->nested.virtual_apic_page);
9537 vmx->nested.virtual_apic_page =
9538 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9539
9540 /*
9541 * Failing the vm entry is _not_ what the processor does
9542 * but it's basically the only possibility we have.
9543 * We could still enter the guest if CR8 load exits are
9544 * enabled, CR8 store exits are enabled, and virtualize APIC
9545 * access is disabled; in this case the processor would never
9546 * use the TPR shadow and we could simply clear the bit from
9547 * the execution control. But such a configuration is useless,
9548 * so let's keep the code simple.
9549 */
9550 if (!vmx->nested.virtual_apic_page)
9551 return false;
9552 }
9553
Wincy Van705699a2015-02-03 23:58:17 +08009554 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009555 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9556 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009557 return false;
9558
9559 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9560 kunmap(vmx->nested.pi_desc_page);
9561 nested_release_page(vmx->nested.pi_desc_page);
9562 }
9563 vmx->nested.pi_desc_page =
9564 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9565 if (!vmx->nested.pi_desc_page)
9566 return false;
9567
9568 vmx->nested.pi_desc =
9569 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9570 if (!vmx->nested.pi_desc) {
9571 nested_release_page_clean(vmx->nested.pi_desc_page);
9572 return false;
9573 }
9574 vmx->nested.pi_desc =
9575 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9576 (unsigned long)(vmcs12->posted_intr_desc_addr &
9577 (PAGE_SIZE - 1)));
9578 }
9579
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009580 return true;
9581}
9582
Jan Kiszkaf4124502014-03-07 20:03:13 +01009583static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9584{
9585 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9586 struct vcpu_vmx *vmx = to_vmx(vcpu);
9587
9588 if (vcpu->arch.virtual_tsc_khz == 0)
9589 return;
9590
9591 /* Make sure short timeouts reliably trigger an immediate vmexit.
9592 * hrtimer_start does not guarantee this. */
9593 if (preemption_timeout <= 1) {
9594 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9595 return;
9596 }
9597
9598 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9599 preemption_timeout *= 1000000;
9600 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9601 hrtimer_start(&vmx->nested.preemption_timer,
9602 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9603}
9604
Wincy Van3af18d92015-02-03 23:49:31 +08009605static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9606 struct vmcs12 *vmcs12)
9607{
9608 int maxphyaddr;
9609 u64 addr;
9610
9611 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9612 return 0;
9613
9614 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9615 WARN_ON(1);
9616 return -EINVAL;
9617 }
9618 maxphyaddr = cpuid_maxphyaddr(vcpu);
9619
9620 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9621 ((addr + PAGE_SIZE) >> maxphyaddr))
9622 return -EINVAL;
9623
9624 return 0;
9625}
9626
9627/*
9628 * Merge L0's and L1's MSR bitmap, return false to indicate that
9629 * we do not use the hardware.
9630 */
9631static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9632 struct vmcs12 *vmcs12)
9633{
Wincy Van82f0dd42015-02-03 23:57:18 +08009634 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009635 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009636 unsigned long *msr_bitmap_l1;
Paolo Bonzini6236b782018-01-16 16:51:18 +01009637 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj70131292018-02-01 22:59:43 +01009638 /*
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009639 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj70131292018-02-01 22:59:43 +01009640 *
9641 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
9642 * ensures that we do not accidentally generate an L02 MSR bitmap
9643 * from the L12 MSR bitmap that is too permissive.
9644 * 2. That L1 or L2s have actually used the MSR. This avoids
9645 * unnecessarily merging of the bitmap if the MSR is unused. This
9646 * works properly because we only update the L01 MSR bitmap lazily.
9647 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
9648 * updated to reflect this when L1 (or its L2s) actually write to
9649 * the MSR.
9650 */
KarimAllah Ahmed96652962018-02-10 23:39:25 +00009651 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
9652 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +08009653
Ashok Raj70131292018-02-01 22:59:43 +01009654 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009655 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +08009656 return false;
9657
9658 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář215df1f2017-03-07 17:51:49 +01009659 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009660 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009661 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009662
Radim Krčmářd048c092016-08-08 20:16:22 +02009663 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9664
Wincy Vanf2b93282015-02-03 23:56:03 +08009665 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009666 if (nested_cpu_has_apic_reg_virt(vmcs12))
9667 for (msr = 0x800; msr <= 0x8ff; msr++)
9668 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009669 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009670 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009671
9672 nested_vmx_disable_intercept_for_msr(
9673 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009674 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9675 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009676
Wincy Van608406e2015-02-03 23:57:51 +08009677 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009678 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009679 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009680 APIC_BASE_MSR + (APIC_EOI >> 4),
9681 MSR_TYPE_W);
9682 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009683 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009684 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9685 MSR_TYPE_W);
9686 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009687 }
Ashok Raj70131292018-02-01 22:59:43 +01009688
KarimAllah Ahmede5a83412018-02-01 22:59:45 +01009689 if (spec_ctrl)
9690 nested_vmx_disable_intercept_for_msr(
9691 msr_bitmap_l1, msr_bitmap_l0,
9692 MSR_IA32_SPEC_CTRL,
9693 MSR_TYPE_R | MSR_TYPE_W);
9694
Ashok Raj70131292018-02-01 22:59:43 +01009695 if (pred_cmd)
9696 nested_vmx_disable_intercept_for_msr(
9697 msr_bitmap_l1, msr_bitmap_l0,
9698 MSR_IA32_PRED_CMD,
9699 MSR_TYPE_W);
9700
Wincy Vanf2b93282015-02-03 23:56:03 +08009701 kunmap(page);
9702 nested_release_page_clean(page);
9703
9704 return true;
9705}
9706
9707static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9708 struct vmcs12 *vmcs12)
9709{
Wincy Van82f0dd42015-02-03 23:57:18 +08009710 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009711 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009712 !nested_cpu_has_vid(vmcs12) &&
9713 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009714 return 0;
9715
9716 /*
9717 * If virtualize x2apic mode is enabled,
9718 * virtualize apic access must be disabled.
9719 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009720 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9721 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009722 return -EINVAL;
9723
Wincy Van608406e2015-02-03 23:57:51 +08009724 /*
9725 * If virtual interrupt delivery is enabled,
9726 * we must exit on external interrupts.
9727 */
9728 if (nested_cpu_has_vid(vmcs12) &&
9729 !nested_exit_on_intr(vcpu))
9730 return -EINVAL;
9731
Wincy Van705699a2015-02-03 23:58:17 +08009732 /*
9733 * bits 15:8 should be zero in posted_intr_nv,
9734 * the descriptor address has been already checked
9735 * in nested_get_vmcs12_pages.
9736 */
9737 if (nested_cpu_has_posted_intr(vmcs12) &&
9738 (!nested_cpu_has_vid(vmcs12) ||
9739 !nested_exit_intr_ack_set(vcpu) ||
9740 vmcs12->posted_intr_nv & 0xff00))
9741 return -EINVAL;
9742
Wincy Vanf2b93282015-02-03 23:56:03 +08009743 /* tpr shadow is needed by all apicv features. */
9744 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9745 return -EINVAL;
9746
9747 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009748}
9749
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009750static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9751 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009752 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009753{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009754 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009755 u64 count, addr;
9756
9757 if (vmcs12_read_any(vcpu, count_field, &count) ||
9758 vmcs12_read_any(vcpu, addr_field, &addr)) {
9759 WARN_ON(1);
9760 return -EINVAL;
9761 }
9762 if (count == 0)
9763 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009764 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009765 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9766 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009767 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009768 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9769 addr_field, maxphyaddr, count, addr);
9770 return -EINVAL;
9771 }
9772 return 0;
9773}
9774
9775static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9776 struct vmcs12 *vmcs12)
9777{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009778 if (vmcs12->vm_exit_msr_load_count == 0 &&
9779 vmcs12->vm_exit_msr_store_count == 0 &&
9780 vmcs12->vm_entry_msr_load_count == 0)
9781 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009782 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009783 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009784 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009785 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009786 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009787 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009788 return -EINVAL;
9789 return 0;
9790}
9791
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009792static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9793 struct vmx_msr_entry *e)
9794{
9795 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009796 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009797 return -EINVAL;
9798 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9799 e->index == MSR_IA32_UCODE_REV)
9800 return -EINVAL;
9801 if (e->reserved != 0)
9802 return -EINVAL;
9803 return 0;
9804}
9805
9806static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9807 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009808{
9809 if (e->index == MSR_FS_BASE ||
9810 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009811 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9812 nested_vmx_msr_check_common(vcpu, e))
9813 return -EINVAL;
9814 return 0;
9815}
9816
9817static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9818 struct vmx_msr_entry *e)
9819{
9820 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9821 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009822 return -EINVAL;
9823 return 0;
9824}
9825
9826/*
9827 * Load guest's/host's msr at nested entry/exit.
9828 * return 0 for success, entry index for failure.
9829 */
9830static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9831{
9832 u32 i;
9833 struct vmx_msr_entry e;
9834 struct msr_data msr;
9835
9836 msr.host_initiated = false;
9837 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009838 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9839 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009840 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009841 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9842 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009843 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009844 }
9845 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009846 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009847 "%s check failed (%u, 0x%x, 0x%x)\n",
9848 __func__, i, e.index, e.reserved);
9849 goto fail;
9850 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009851 msr.index = e.index;
9852 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009853 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009854 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009855 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9856 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009857 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009858 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009859 }
9860 return 0;
9861fail:
9862 return i + 1;
9863}
9864
9865static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9866{
9867 u32 i;
9868 struct vmx_msr_entry e;
9869
9870 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009871 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009872 if (kvm_vcpu_read_guest(vcpu,
9873 gpa + i * sizeof(e),
9874 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009875 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009876 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9877 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009878 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009879 }
9880 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009881 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009882 "%s check failed (%u, 0x%x, 0x%x)\n",
9883 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009884 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009885 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009886 msr_info.host_initiated = false;
9887 msr_info.index = e.index;
9888 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009889 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009890 "%s cannot read MSR (%u, 0x%x)\n",
9891 __func__, i, e.index);
9892 return -EINVAL;
9893 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009894 if (kvm_vcpu_write_guest(vcpu,
9895 gpa + i * sizeof(e) +
9896 offsetof(struct vmx_msr_entry, value),
9897 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009898 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009899 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009900 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009901 return -EINVAL;
9902 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009903 }
9904 return 0;
9905}
9906
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009907/*
9908 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9909 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009910 * 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 +03009911 * guest in a way that will both be appropriate to L1's requests, and our
9912 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9913 * function also has additional necessary side-effects, like setting various
9914 * vcpu->arch fields.
9915 */
9916static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9917{
9918 struct vcpu_vmx *vmx = to_vmx(vcpu);
9919 u32 exec_control;
9920
9921 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9922 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9923 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9924 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9925 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9926 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9927 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9928 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9929 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9930 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9931 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9932 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9933 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9934 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9935 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9936 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9937 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9938 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9939 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9940 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9941 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9942 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9943 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9944 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9945 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9946 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9947 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9948 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9949 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9950 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9951 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9952 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9953 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9954 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9955 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9956 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9957
Jan Kiszka2996fca2014-06-16 13:59:43 +02009958 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9959 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9960 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9961 } else {
9962 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9963 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9964 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009965 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9966 vmcs12->vm_entry_intr_info_field);
9967 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9968 vmcs12->vm_entry_exception_error_code);
9969 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9970 vmcs12->vm_entry_instruction_len);
9971 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9972 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009973 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009974 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009975 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9976 vmcs12->guest_pending_dbg_exceptions);
9977 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9978 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9979
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009980 if (nested_cpu_has_xsaves(vmcs12))
9981 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009982 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9983
Jan Kiszkaf4124502014-03-07 20:03:13 +01009984 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009985
Paolo Bonzini93140062016-07-06 13:23:51 +02009986 /* Preemption timer setting is only taken from vmcs01. */
9987 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9988 exec_control |= vmcs_config.pin_based_exec_ctrl;
9989 if (vmx->hv_deadline_tsc == -1)
9990 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9991
9992 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +08009993 if (nested_cpu_has_posted_intr(vmcs12)) {
9994 /*
9995 * Note that we use L0's vector here and in
9996 * vmx_deliver_nested_posted_interrupt.
9997 */
9998 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9999 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010000 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +080010001 vmcs_write64(POSTED_INTR_DESC_ADDR,
10002 page_to_phys(vmx->nested.pi_desc_page) +
10003 (unsigned long)(vmcs12->posted_intr_desc_addr &
10004 (PAGE_SIZE - 1)));
10005 } else
10006 exec_control &= ~PIN_BASED_POSTED_INTR;
10007
Jan Kiszkaf4124502014-03-07 20:03:13 +010010008 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010009
Jan Kiszkaf4124502014-03-07 20:03:13 +010010010 vmx->nested.preemption_timer_expired = false;
10011 if (nested_cpu_has_preemption_timer(vmcs12))
10012 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010013
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010014 /*
10015 * Whether page-faults are trapped is determined by a combination of
10016 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10017 * If enable_ept, L0 doesn't care about page faults and we should
10018 * set all of these to L1's desires. However, if !enable_ept, L0 does
10019 * care about (at least some) page faults, and because it is not easy
10020 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10021 * to exit on each and every L2 page fault. This is done by setting
10022 * MASK=MATCH=0 and (see below) EB.PF=1.
10023 * Note that below we don't need special code to set EB.PF beyond the
10024 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10025 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10026 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10027 *
10028 * A problem with this approach (when !enable_ept) is that L1 may be
10029 * injected with more page faults than it asked for. This could have
10030 * caused problems, but in practice existing hypervisors don't care.
10031 * To fix this, we will need to emulate the PFEC checking (on the L1
10032 * page tables), using walk_addr(), when injecting PFs to L1.
10033 */
10034 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10035 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10036 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10037 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10038
10039 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010040 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010041
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010042 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010043 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010044 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010045 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010046 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010047 if (nested_cpu_has(vmcs12,
10048 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10049 exec_control |= vmcs12->secondary_vm_exec_control;
10050
10051 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
10052 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010053 * If translation failed, no matter: This feature asks
10054 * to exit when accessing the given address, and if it
10055 * can never be accessed, this feature won't do
10056 * anything anyway.
10057 */
10058 if (!vmx->nested.apic_access_page)
10059 exec_control &=
10060 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10061 else
10062 vmcs_write64(APIC_ACCESS_ADDR,
10063 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +080010064 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +020010065 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +010010066 exec_control |=
10067 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +080010068 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010069 }
10070
Wincy Van608406e2015-02-03 23:57:51 +080010071 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10072 vmcs_write64(EOI_EXIT_BITMAP0,
10073 vmcs12->eoi_exit_bitmap0);
10074 vmcs_write64(EOI_EXIT_BITMAP1,
10075 vmcs12->eoi_exit_bitmap1);
10076 vmcs_write64(EOI_EXIT_BITMAP2,
10077 vmcs12->eoi_exit_bitmap2);
10078 vmcs_write64(EOI_EXIT_BITMAP3,
10079 vmcs12->eoi_exit_bitmap3);
10080 vmcs_write16(GUEST_INTR_STATUS,
10081 vmcs12->guest_intr_status);
10082 }
10083
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010084 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10085 }
10086
10087
10088 /*
10089 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10090 * Some constant fields are set here by vmx_set_constant_host_state().
10091 * Other fields are different per CPU, and will be set later when
10092 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10093 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010094 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010095
10096 /*
10097 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10098 * entry, but only if the current (host) sp changed from the value
10099 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10100 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10101 * here we just force the write to happen on entry.
10102 */
10103 vmx->host_rsp = 0;
10104
10105 exec_control = vmx_exec_control(vmx); /* L0's desires */
10106 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10107 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10108 exec_control &= ~CPU_BASED_TPR_SHADOW;
10109 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010110
10111 if (exec_control & CPU_BASED_TPR_SHADOW) {
10112 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10113 page_to_phys(vmx->nested.virtual_apic_page));
10114 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010115 } else {
10116#ifdef CONFIG_X86_64
10117 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10118 CPU_BASED_CR8_STORE_EXITING;
10119#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010120 }
10121
Wincy Van3af18d92015-02-03 23:49:31 +080010122 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010123 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10124 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10125 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10126 else
Wincy Van3af18d92015-02-03 23:49:31 +080010127 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10128
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010129 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010130 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010131 * Rather, exit every time.
10132 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010133 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10134 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10135
10136 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10137
10138 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10139 * bitwise-or of what L1 wants to trap for L2, and what we want to
10140 * trap. Note that CR0.TS also needs updating - we do this later.
10141 */
10142 update_exception_bitmap(vcpu);
10143 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10144 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10145
Nadav Har'El8049d652013-08-05 11:07:06 +030010146 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10147 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10148 * bits are further modified by vmx_set_efer() below.
10149 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010150 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010151
10152 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10153 * emulated by vmx_set_efer(), below.
10154 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010155 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010156 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10157 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010158 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10159
Jan Kiszka44811c02013-08-04 17:17:27 +020010160 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010161 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010162 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10163 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010164 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10165
10166
10167 set_cr4_guest_host_mask(vmx);
10168
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010169 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10170 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10171
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010172 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10173 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010174 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010175 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010176 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010177 if (kvm_has_tsc_control)
10178 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010179
Paolo Bonzini6236b782018-01-16 16:51:18 +010010180 if (cpu_has_vmx_msr_bitmap())
10181 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
10182
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010183 if (enable_vpid) {
10184 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010185 * There is no direct mapping between vpid02 and vpid12, the
10186 * vpid02 is per-vCPU for L0 and reused while the value of
10187 * vpid12 is changed w/ one invvpid during nested vmentry.
10188 * The vpid12 is allocated by L1 for L2, so it will not
10189 * influence global bitmap(for vpid01 and vpid02 allocation)
10190 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010191 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010192 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10193 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10194 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10195 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10196 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10197 }
10198 } else {
10199 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10200 vmx_flush_tlb(vcpu);
10201 }
10202
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010203 }
10204
Ladi Prosek560a9792017-04-04 14:18:53 +020010205 if (enable_pml) {
10206 /*
10207 * Conceptually we want to copy the PML address and index from
10208 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10209 * since we always flush the log on each vmexit, this happens
10210 * to be equivalent to simply resetting the fields in vmcs02.
10211 */
10212 ASSERT(vmx->pml_pg);
10213 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10214 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10215 }
10216
Nadav Har'El155a97a2013-08-05 11:07:16 +030010217 if (nested_cpu_has_ept(vmcs12)) {
10218 kvm_mmu_unload(vcpu);
10219 nested_ept_init_mmu_context(vcpu);
Jim Mattson8386ff52017-03-16 13:53:59 -070010220 } else if (nested_cpu_has2(vmcs12,
10221 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10222 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010223 }
10224
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010225 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10226 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010227 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010228 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10229 else
10230 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10231 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10232 vmx_set_efer(vcpu, vcpu->arch.efer);
10233
10234 /*
10235 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10236 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10237 * The CR0_READ_SHADOW is what L2 should have expected to read given
10238 * the specifications by L1; It's not enough to take
10239 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10240 * have more bits than L1 expected.
10241 */
10242 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10243 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10244
10245 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10246 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10247
10248 /* shadow page tables on either EPT or shadow page tables */
10249 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10250 kvm_mmu_reset_context(vcpu);
10251
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010252 if (!enable_ept)
10253 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10254
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010255 /*
10256 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10257 */
10258 if (enable_ept) {
10259 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10260 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10261 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10262 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10263 }
10264
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010265 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10266 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10267}
10268
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010269/*
10270 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10271 * for running an L2 nested guest.
10272 */
10273static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10274{
10275 struct vmcs12 *vmcs12;
10276 struct vcpu_vmx *vmx = to_vmx(vcpu);
10277 int cpu;
Jan Kiszka384bb782013-04-20 10:52:36 +020010278 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010279 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010280
10281 if (!nested_vmx_check_permission(vcpu) ||
10282 !nested_vmx_check_vmcs12(vcpu))
10283 return 1;
10284
10285 skip_emulated_instruction(vcpu);
10286 vmcs12 = get_vmcs12(vcpu);
10287
Abel Gordon012f83c2013-04-18 14:39:25 +030010288 if (enable_shadow_vmcs)
10289 copy_shadow_to_vmcs12(vmx);
10290
Nadav Har'El7c177932011-05-25 23:12:04 +030010291 /*
10292 * The nested entry process starts with enforcing various prerequisites
10293 * on vmcs12 as required by the Intel SDM, and act appropriately when
10294 * they fail: As the SDM explains, some conditions should cause the
10295 * instruction to fail, while others will cause the instruction to seem
10296 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10297 * To speed up the normal (success) code path, we should avoid checking
10298 * for misconfigurations which will anyway be caught by the processor
10299 * when using the merged vmcs02.
10300 */
10301 if (vmcs12->launch_state == launch) {
10302 nested_vmx_failValid(vcpu,
10303 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10304 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10305 return 1;
10306 }
10307
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010308 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10309 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010310 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10311 return 1;
10312 }
10313
Wincy Van3af18d92015-02-03 23:49:31 +080010314 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010315 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10316 return 1;
10317 }
10318
Wincy Van3af18d92015-02-03 23:49:31 +080010319 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010320 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10321 return 1;
10322 }
10323
Wincy Vanf2b93282015-02-03 23:56:03 +080010324 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10325 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10326 return 1;
10327 }
10328
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010329 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10330 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10331 return 1;
10332 }
10333
Nadav Har'El7c177932011-05-25 23:12:04 +030010334 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010335 vmx->nested.nested_vmx_true_procbased_ctls_low,
10336 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010337 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010338 vmx->nested.nested_vmx_secondary_ctls_low,
10339 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010340 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010341 vmx->nested.nested_vmx_pinbased_ctls_low,
10342 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010343 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010344 vmx->nested.nested_vmx_true_exit_ctls_low,
10345 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010346 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010347 vmx->nested.nested_vmx_true_entry_ctls_low,
10348 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010349 {
10350 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10351 return 1;
10352 }
10353
10354 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10355 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10356 nested_vmx_failValid(vcpu,
10357 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10358 return 1;
10359 }
10360
Wincy Vanb9c237b2015-02-03 23:56:30 +080010361 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010362 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10363 nested_vmx_entry_failure(vcpu, vmcs12,
10364 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10365 return 1;
10366 }
10367 if (vmcs12->vmcs_link_pointer != -1ull) {
10368 nested_vmx_entry_failure(vcpu, vmcs12,
10369 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10370 return 1;
10371 }
10372
10373 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010374 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010375 * are performed on the field for the IA32_EFER MSR:
10376 * - Bits reserved in the IA32_EFER MSR must be 0.
10377 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10378 * the IA-32e mode guest VM-exit control. It must also be identical
10379 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10380 * CR0.PG) is 1.
10381 */
10382 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10383 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10384 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10385 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10386 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10387 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10388 nested_vmx_entry_failure(vcpu, vmcs12,
10389 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10390 return 1;
10391 }
10392 }
10393
10394 /*
10395 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10396 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10397 * the values of the LMA and LME bits in the field must each be that of
10398 * the host address-space size VM-exit control.
10399 */
10400 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10401 ia32e = (vmcs12->vm_exit_controls &
10402 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10403 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10404 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10405 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10406 nested_vmx_entry_failure(vcpu, vmcs12,
10407 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10408 return 1;
10409 }
10410 }
10411
10412 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010413 * We're finally done with prerequisite checking, and can start with
10414 * the nested entry.
10415 */
10416
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010417 enter_guest_mode(vcpu);
10418
Jan Kiszka2996fca2014-06-16 13:59:43 +020010419 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10420 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10421
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010422 cpu = get_cpu();
Jim Mattson46e24df2017-11-27 17:22:25 -060010423 vmx->loaded_vmcs = &vmx->nested.vmcs02;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010424 vmx_vcpu_put(vcpu);
10425 vmx_vcpu_load(vcpu, cpu);
10426 vcpu->cpu = cpu;
10427 put_cpu();
10428
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010429 vmx_segment_cache_clear(vmx);
10430
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010431 prepare_vmcs02(vcpu, vmcs12);
10432
Wincy Vanff651cb2014-12-11 08:52:58 +030010433 msr_entry_idx = nested_vmx_load_msr(vcpu,
10434 vmcs12->vm_entry_msr_load_addr,
10435 vmcs12->vm_entry_msr_load_count);
10436 if (msr_entry_idx) {
10437 leave_guest_mode(vcpu);
10438 vmx_load_vmcs01(vcpu);
10439 nested_vmx_entry_failure(vcpu, vmcs12,
10440 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10441 return 1;
10442 }
10443
10444 vmcs12->launch_state = 1;
10445
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010446 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010447 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010448
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010449 vmx->nested.nested_run_pending = 1;
10450
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010451 /*
10452 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10453 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10454 * returned as far as L1 is concerned. It will only return (and set
10455 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10456 */
10457 return 1;
10458}
10459
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010460/*
10461 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10462 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10463 * This function returns the new value we should put in vmcs12.guest_cr0.
10464 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10465 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10466 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10467 * didn't trap the bit, because if L1 did, so would L0).
10468 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10469 * been modified by L2, and L1 knows it. So just leave the old value of
10470 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10471 * isn't relevant, because if L0 traps this bit it can set it to anything.
10472 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10473 * changed these bits, and therefore they need to be updated, but L0
10474 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10475 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10476 */
10477static inline unsigned long
10478vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10479{
10480 return
10481 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10482 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10483 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10484 vcpu->arch.cr0_guest_owned_bits));
10485}
10486
10487static inline unsigned long
10488vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10489{
10490 return
10491 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10492 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10493 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10494 vcpu->arch.cr4_guest_owned_bits));
10495}
10496
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010497static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10498 struct vmcs12 *vmcs12)
10499{
10500 u32 idt_vectoring;
10501 unsigned int nr;
10502
Gleb Natapov851eb6672013-09-25 12:51:34 +030010503 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010504 nr = vcpu->arch.exception.nr;
10505 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10506
10507 if (kvm_exception_is_soft(nr)) {
10508 vmcs12->vm_exit_instruction_len =
10509 vcpu->arch.event_exit_inst_len;
10510 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10511 } else
10512 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10513
10514 if (vcpu->arch.exception.has_error_code) {
10515 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10516 vmcs12->idt_vectoring_error_code =
10517 vcpu->arch.exception.error_code;
10518 }
10519
10520 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010521 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010522 vmcs12->idt_vectoring_info_field =
10523 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10524 } else if (vcpu->arch.interrupt.pending) {
10525 nr = vcpu->arch.interrupt.nr;
10526 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10527
10528 if (vcpu->arch.interrupt.soft) {
10529 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10530 vmcs12->vm_entry_instruction_len =
10531 vcpu->arch.event_exit_inst_len;
10532 } else
10533 idt_vectoring |= INTR_TYPE_EXT_INTR;
10534
10535 vmcs12->idt_vectoring_info_field = idt_vectoring;
10536 }
10537}
10538
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010539static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10540{
10541 struct vcpu_vmx *vmx = to_vmx(vcpu);
10542
Jan Kiszkaf4124502014-03-07 20:03:13 +010010543 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10544 vmx->nested.preemption_timer_expired) {
10545 if (vmx->nested.nested_run_pending)
10546 return -EBUSY;
10547 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10548 return 0;
10549 }
10550
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010551 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010552 if (vmx->nested.nested_run_pending ||
10553 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010554 return -EBUSY;
10555 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10556 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10557 INTR_INFO_VALID_MASK, 0);
10558 /*
10559 * The NMI-triggered VM exit counts as injection:
10560 * clear this one and block further NMIs.
10561 */
10562 vcpu->arch.nmi_pending = 0;
10563 vmx_set_nmi_mask(vcpu, true);
10564 return 0;
10565 }
10566
10567 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10568 nested_exit_on_intr(vcpu)) {
10569 if (vmx->nested.nested_run_pending)
10570 return -EBUSY;
10571 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010572 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010573 }
10574
David Hildenbrand1edccf22017-01-25 11:58:58 +010010575 vmx_complete_nested_posted_interrupt(vcpu);
10576 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010577}
10578
Jan Kiszkaf4124502014-03-07 20:03:13 +010010579static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10580{
10581 ktime_t remaining =
10582 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10583 u64 value;
10584
10585 if (ktime_to_ns(remaining) <= 0)
10586 return 0;
10587
10588 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10589 do_div(value, 1000000);
10590 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10591}
10592
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010593/*
10594 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10595 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10596 * and this function updates it to reflect the changes to the guest state while
10597 * L2 was running (and perhaps made some exits which were handled directly by L0
10598 * without going back to L1), and to reflect the exit reason.
10599 * Note that we do not have to copy here all VMCS fields, just those that
10600 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10601 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10602 * which already writes to vmcs12 directly.
10603 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010604static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10605 u32 exit_reason, u32 exit_intr_info,
10606 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010607{
10608 /* update guest state fields: */
10609 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10610 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10611
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010612 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10613 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10614 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10615
10616 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10617 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10618 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10619 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10620 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10621 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10622 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10623 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10624 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10625 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10626 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10627 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10628 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10629 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10630 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10631 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10632 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10633 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10634 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10635 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10636 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10637 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10638 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10639 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10640 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10641 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10642 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10643 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10644 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10645 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10646 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10647 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10648 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10649 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10650 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10651 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10652
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010653 vmcs12->guest_interruptibility_info =
10654 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10655 vmcs12->guest_pending_dbg_exceptions =
10656 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010657 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10658 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10659 else
10660 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010661
Jan Kiszkaf4124502014-03-07 20:03:13 +010010662 if (nested_cpu_has_preemption_timer(vmcs12)) {
10663 if (vmcs12->vm_exit_controls &
10664 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10665 vmcs12->vmx_preemption_timer_value =
10666 vmx_get_preemption_timer_value(vcpu);
10667 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10668 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010669
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010670 /*
10671 * In some cases (usually, nested EPT), L2 is allowed to change its
10672 * own CR3 without exiting. If it has changed it, we must keep it.
10673 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10674 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10675 *
10676 * Additionally, restore L2's PDPTR to vmcs12.
10677 */
10678 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010679 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010680 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10681 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10682 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10683 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10684 }
10685
Jim Mattson4933e9f2017-06-01 12:44:46 -070010686 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010687
Wincy Van608406e2015-02-03 23:57:51 +080010688 if (nested_cpu_has_vid(vmcs12))
10689 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10690
Jan Kiszkac18911a2013-03-13 16:06:41 +010010691 vmcs12->vm_entry_controls =
10692 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010693 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010694
Jan Kiszka2996fca2014-06-16 13:59:43 +020010695 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10696 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10697 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10698 }
10699
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010700 /* TODO: These cannot have changed unless we have MSR bitmaps and
10701 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010702 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010703 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010704 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10705 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010706 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10707 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10708 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010709 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010710 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010711 if (nested_cpu_has_xsaves(vmcs12))
10712 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010713
10714 /* update exit information fields: */
10715
Jan Kiszka533558b2014-01-04 18:47:20 +010010716 vmcs12->vm_exit_reason = exit_reason;
10717 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010718
Jan Kiszka533558b2014-01-04 18:47:20 +010010719 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010720 if ((vmcs12->vm_exit_intr_info &
10721 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10722 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10723 vmcs12->vm_exit_intr_error_code =
10724 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010725 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010726 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10727 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10728
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010729 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10730 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10731 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010732 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010733
10734 /*
10735 * Transfer the event that L0 or L1 may wanted to inject into
10736 * L2 to IDT_VECTORING_INFO_FIELD.
10737 */
10738 vmcs12_save_pending_event(vcpu, vmcs12);
10739 }
10740
10741 /*
10742 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10743 * preserved above and would only end up incorrectly in L1.
10744 */
10745 vcpu->arch.nmi_injected = false;
10746 kvm_clear_exception_queue(vcpu);
10747 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010748}
10749
10750/*
10751 * A part of what we need to when the nested L2 guest exits and we want to
10752 * run its L1 parent, is to reset L1's guest state to the host state specified
10753 * in vmcs12.
10754 * This function is to be called not only on normal nested exit, but also on
10755 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10756 * Failures During or After Loading Guest State").
10757 * This function should be called when the active VMCS is L1's (vmcs01).
10758 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010759static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10760 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010761{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010762 struct kvm_segment seg;
10763
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010764 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10765 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010766 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010767 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10768 else
10769 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10770 vmx_set_efer(vcpu, vcpu->arch.efer);
10771
10772 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10773 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010774 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010775 /*
10776 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10777 * actually changed, because it depends on the current state of
10778 * fpu_active (which may have changed).
10779 * Note that vmx_set_cr0 refers to efer set above.
10780 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010781 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010782 /*
10783 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10784 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10785 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10786 */
10787 update_exception_bitmap(vcpu);
10788 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10789 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10790
10791 /*
10792 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10793 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10794 */
10795 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080010796 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010797
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010798 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010799
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010800 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10801 kvm_mmu_reset_context(vcpu);
10802
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010803 if (!enable_ept)
10804 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10805
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010806 if (enable_vpid) {
10807 /*
10808 * Trivially support vpid by letting L2s share their parent
10809 * L1's vpid. TODO: move to a more elaborate solution, giving
10810 * each L2 its own vpid and exposing the vpid feature to L1.
10811 */
10812 vmx_flush_tlb(vcpu);
10813 }
10814
10815
10816 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10817 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10818 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10819 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10820 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020010821 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
10822 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010823
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010824 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10825 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10826 vmcs_write64(GUEST_BNDCFGS, 0);
10827
Jan Kiszka44811c02013-08-04 17:17:27 +020010828 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010829 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010830 vcpu->arch.pat = vmcs12->host_ia32_pat;
10831 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010832 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10833 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10834 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010835
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010836 /* Set L1 segment info according to Intel SDM
10837 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10838 seg = (struct kvm_segment) {
10839 .base = 0,
10840 .limit = 0xFFFFFFFF,
10841 .selector = vmcs12->host_cs_selector,
10842 .type = 11,
10843 .present = 1,
10844 .s = 1,
10845 .g = 1
10846 };
10847 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10848 seg.l = 1;
10849 else
10850 seg.db = 1;
10851 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10852 seg = (struct kvm_segment) {
10853 .base = 0,
10854 .limit = 0xFFFFFFFF,
10855 .type = 3,
10856 .present = 1,
10857 .s = 1,
10858 .db = 1,
10859 .g = 1
10860 };
10861 seg.selector = vmcs12->host_ds_selector;
10862 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10863 seg.selector = vmcs12->host_es_selector;
10864 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10865 seg.selector = vmcs12->host_ss_selector;
10866 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10867 seg.selector = vmcs12->host_fs_selector;
10868 seg.base = vmcs12->host_fs_base;
10869 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10870 seg.selector = vmcs12->host_gs_selector;
10871 seg.base = vmcs12->host_gs_base;
10872 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10873 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010874 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010875 .limit = 0x67,
10876 .selector = vmcs12->host_tr_selector,
10877 .type = 11,
10878 .present = 1
10879 };
10880 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10881
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010882 kvm_set_dr(vcpu, 7, 0x400);
10883 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010884
Wincy Van3af18d92015-02-03 23:49:31 +080010885 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini6236b782018-01-16 16:51:18 +010010886 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080010887
Wincy Vanff651cb2014-12-11 08:52:58 +030010888 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10889 vmcs12->vm_exit_msr_load_count))
10890 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010891}
10892
10893/*
10894 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10895 * and modify vmcs12 to make it see what it would expect to see there if
10896 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10897 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010898static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10899 u32 exit_intr_info,
10900 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010901{
10902 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010903 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10904
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010905 /* trying to cancel vmlaunch/vmresume is a bug */
10906 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10907
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010908 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010909 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10910 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010911
Wincy Vanff651cb2014-12-11 08:52:58 +030010912 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10913 vmcs12->vm_exit_msr_store_count))
10914 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10915
Wanpeng Lif3380ca2014-08-05 12:42:23 +080010916 vmx_load_vmcs01(vcpu);
10917
Bandan Das77b0f5d2014-04-19 18:17:45 -040010918 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10919 && nested_exit_intr_ack_set(vcpu)) {
10920 int irq = kvm_cpu_get_interrupt(vcpu);
10921 WARN_ON(irq < 0);
10922 vmcs12->vm_exit_intr_info = irq |
10923 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10924 }
10925
Jan Kiszka542060e2014-01-04 18:47:21 +010010926 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10927 vmcs12->exit_qualification,
10928 vmcs12->idt_vectoring_info_field,
10929 vmcs12->vm_exit_intr_info,
10930 vmcs12->vm_exit_intr_error_code,
10931 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010932
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010933 vm_entry_controls_reset_shadow(vmx);
10934 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010935 vmx_segment_cache_clear(vmx);
10936
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010937 load_vmcs12_host_state(vcpu, vmcs12);
10938
Paolo Bonzini93140062016-07-06 13:23:51 +020010939 /* Update any VMCS fields that might have changed while L2 ran */
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010940 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020010941 if (vmx->hv_deadline_tsc == -1)
10942 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10943 PIN_BASED_VMX_PREEMPTION_TIMER);
10944 else
10945 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10946 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070010947 if (kvm_has_tsc_control)
10948 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010949
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010950 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10951 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10952 vmx_set_virtual_x2apic_mode(vcpu,
10953 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattson8386ff52017-03-16 13:53:59 -070010954 } else if (!nested_cpu_has_ept(vmcs12) &&
10955 nested_cpu_has2(vmcs12,
10956 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10957 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010958 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010959
10960 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10961 vmx->host_rsp = 0;
10962
10963 /* Unpin physical memory we referred to in vmcs02 */
10964 if (vmx->nested.apic_access_page) {
10965 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010966 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010967 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010968 if (vmx->nested.virtual_apic_page) {
10969 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010970 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010971 }
Wincy Van705699a2015-02-03 23:58:17 +080010972 if (vmx->nested.pi_desc_page) {
10973 kunmap(vmx->nested.pi_desc_page);
10974 nested_release_page(vmx->nested.pi_desc_page);
10975 vmx->nested.pi_desc_page = NULL;
10976 vmx->nested.pi_desc = NULL;
10977 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010978
10979 /*
Tang Chen38b99172014-09-24 15:57:54 +080010980 * We are now running in L2, mmu_notifier will force to reload the
10981 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10982 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080010983 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080010984
10985 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010986 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10987 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10988 * success or failure flag accordingly.
10989 */
10990 if (unlikely(vmx->fail)) {
10991 vmx->fail = 0;
10992 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
10993 } else
10994 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010995 if (enable_shadow_vmcs)
10996 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010997
10998 /* in case we halted in L2 */
10999 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011000}
11001
Nadav Har'El7c177932011-05-25 23:12:04 +030011002/*
Jan Kiszka42124922014-01-04 18:47:19 +010011003 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11004 */
11005static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11006{
Wanpeng Lic886f282017-03-06 04:03:28 -080011007 if (is_guest_mode(vcpu)) {
11008 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011009 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Lic886f282017-03-06 04:03:28 -080011010 }
Jan Kiszka42124922014-01-04 18:47:19 +010011011 free_nested(to_vmx(vcpu));
11012}
11013
11014/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011015 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11016 * 23.7 "VM-entry failures during or after loading guest state" (this also
11017 * lists the acceptable exit-reason and exit-qualification parameters).
11018 * It should only be called before L2 actually succeeded to run, and when
11019 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11020 */
11021static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11022 struct vmcs12 *vmcs12,
11023 u32 reason, unsigned long qualification)
11024{
11025 load_vmcs12_host_state(vcpu, vmcs12);
11026 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11027 vmcs12->exit_qualification = qualification;
11028 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011029 if (enable_shadow_vmcs)
11030 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011031}
11032
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011033static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11034 struct x86_instruction_info *info,
11035 enum x86_intercept_stage stage)
11036{
11037 return X86EMUL_CONTINUE;
11038}
11039
Yunhong Jiang64672c92016-06-13 14:19:59 -070011040#ifdef CONFIG_X86_64
11041/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11042static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11043 u64 divisor, u64 *result)
11044{
11045 u64 low = a << shift, high = a >> (64 - shift);
11046
11047 /* To avoid the overflow on divq */
11048 if (high >= divisor)
11049 return 1;
11050
11051 /* Low hold the result, high hold rem which is discarded */
11052 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11053 "rm" (divisor), "0" (low), "1" (high));
11054 *result = low;
11055
11056 return 0;
11057}
11058
11059static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11060{
11061 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011062 u64 tscl = rdtsc();
11063 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11064 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011065
11066 /* Convert to host delta tsc if tsc scaling is enabled */
11067 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11068 u64_shl_div_u64(delta_tsc,
11069 kvm_tsc_scaling_ratio_frac_bits,
11070 vcpu->arch.tsc_scaling_ratio,
11071 &delta_tsc))
11072 return -ERANGE;
11073
11074 /*
11075 * If the delta tsc can't fit in the 32 bit after the multi shift,
11076 * we can't use the preemption timer.
11077 * It's possible that it fits on later vmentries, but checking
11078 * on every vmentry is costly so we just use an hrtimer.
11079 */
11080 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11081 return -ERANGE;
11082
11083 vmx->hv_deadline_tsc = tscl + delta_tsc;
11084 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11085 PIN_BASED_VMX_PREEMPTION_TIMER);
11086 return 0;
11087}
11088
11089static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11090{
11091 struct vcpu_vmx *vmx = to_vmx(vcpu);
11092 vmx->hv_deadline_tsc = -1;
11093 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11094 PIN_BASED_VMX_PREEMPTION_TIMER);
11095}
11096#endif
11097
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011098static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011099{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011100 if (ple_gap)
11101 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011102}
11103
Kai Huang843e4332015-01-28 10:54:28 +080011104static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11105 struct kvm_memory_slot *slot)
11106{
11107 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11108 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11109}
11110
11111static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11112 struct kvm_memory_slot *slot)
11113{
11114 kvm_mmu_slot_set_dirty(kvm, slot);
11115}
11116
11117static void vmx_flush_log_dirty(struct kvm *kvm)
11118{
11119 kvm_flush_pml_buffers(kvm);
11120}
11121
11122static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11123 struct kvm_memory_slot *memslot,
11124 gfn_t offset, unsigned long mask)
11125{
11126 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11127}
11128
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011129static void __pi_post_block(struct kvm_vcpu *vcpu)
11130{
11131 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11132 struct pi_desc old, new;
11133 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011134
11135 do {
11136 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011137 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11138 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011139
11140 dest = cpu_physical_id(vcpu->cpu);
11141
11142 if (x2apic_enabled())
11143 new.ndst = dest;
11144 else
11145 new.ndst = (dest << 8) & 0xFF00;
11146
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011147 /* set 'NV' to 'notification vector' */
11148 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011149 } while (cmpxchg64(&pi_desc->control, old.control,
11150 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011151
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011152 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11153 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011154 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011155 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011156 vcpu->pre_pcpu = -1;
11157 }
11158}
11159
Feng Wuefc64402015-09-18 22:29:51 +080011160/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011161 * This routine does the following things for vCPU which is going
11162 * to be blocked if VT-d PI is enabled.
11163 * - Store the vCPU to the wakeup list, so when interrupts happen
11164 * we can find the right vCPU to wake up.
11165 * - Change the Posted-interrupt descriptor as below:
11166 * 'NDST' <-- vcpu->pre_pcpu
11167 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11168 * - If 'ON' is set during this process, which means at least one
11169 * interrupt is posted for this vCPU, we cannot block it, in
11170 * this case, return 1, otherwise, return 0.
11171 *
11172 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011173static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011174{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011175 unsigned int dest;
11176 struct pi_desc old, new;
11177 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11178
11179 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011180 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11181 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011182 return 0;
11183
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011184 WARN_ON(irqs_disabled());
11185 local_irq_disable();
11186 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11187 vcpu->pre_pcpu = vcpu->cpu;
11188 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11189 list_add_tail(&vcpu->blocked_vcpu_list,
11190 &per_cpu(blocked_vcpu_on_cpu,
11191 vcpu->pre_pcpu));
11192 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11193 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011194
11195 do {
11196 old.control = new.control = pi_desc->control;
11197
Feng Wubf9f6ac2015-09-18 22:29:55 +080011198 WARN((pi_desc->sn == 1),
11199 "Warning: SN field of posted-interrupts "
11200 "is set before blocking\n");
11201
11202 /*
11203 * Since vCPU can be preempted during this process,
11204 * vcpu->cpu could be different with pre_pcpu, we
11205 * need to set pre_pcpu as the destination of wakeup
11206 * notification event, then we can find the right vCPU
11207 * to wakeup in wakeup handler if interrupts happen
11208 * when the vCPU is in blocked state.
11209 */
11210 dest = cpu_physical_id(vcpu->pre_pcpu);
11211
11212 if (x2apic_enabled())
11213 new.ndst = dest;
11214 else
11215 new.ndst = (dest << 8) & 0xFF00;
11216
11217 /* set 'NV' to 'wakeup vector' */
11218 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011219 } while (cmpxchg64(&pi_desc->control, old.control,
11220 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011221
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011222 /* We should not block the vCPU if an interrupt is posted for it. */
11223 if (pi_test_on(pi_desc) == 1)
11224 __pi_post_block(vcpu);
11225
11226 local_irq_enable();
11227 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011228}
11229
Yunhong Jiangbc225122016-06-13 14:19:58 -070011230static int vmx_pre_block(struct kvm_vcpu *vcpu)
11231{
11232 if (pi_pre_block(vcpu))
11233 return 1;
11234
Yunhong Jiang64672c92016-06-13 14:19:59 -070011235 if (kvm_lapic_hv_timer_in_use(vcpu))
11236 kvm_lapic_switch_to_sw_timer(vcpu);
11237
Yunhong Jiangbc225122016-06-13 14:19:58 -070011238 return 0;
11239}
11240
11241static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011242{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011243 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011244 return;
11245
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011246 WARN_ON(irqs_disabled());
11247 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011248 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011249 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011250}
11251
Yunhong Jiangbc225122016-06-13 14:19:58 -070011252static void vmx_post_block(struct kvm_vcpu *vcpu)
11253{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011254 if (kvm_x86_ops->set_hv_timer)
11255 kvm_lapic_switch_to_hv_timer(vcpu);
11256
Yunhong Jiangbc225122016-06-13 14:19:58 -070011257 pi_post_block(vcpu);
11258}
11259
Feng Wubf9f6ac2015-09-18 22:29:55 +080011260/*
Feng Wuefc64402015-09-18 22:29:51 +080011261 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11262 *
11263 * @kvm: kvm
11264 * @host_irq: host irq of the interrupt
11265 * @guest_irq: gsi of the interrupt
11266 * @set: set or unset PI
11267 * returns 0 on success, < 0 on failure
11268 */
11269static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11270 uint32_t guest_irq, bool set)
11271{
11272 struct kvm_kernel_irq_routing_entry *e;
11273 struct kvm_irq_routing_table *irq_rt;
11274 struct kvm_lapic_irq irq;
11275 struct kvm_vcpu *vcpu;
11276 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011277 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011278
11279 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011280 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11281 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011282 return 0;
11283
11284 idx = srcu_read_lock(&kvm->irq_srcu);
11285 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011286 if (guest_irq >= irq_rt->nr_rt_entries ||
11287 hlist_empty(&irq_rt->map[guest_irq])) {
11288 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11289 guest_irq, irq_rt->nr_rt_entries);
11290 goto out;
11291 }
Feng Wuefc64402015-09-18 22:29:51 +080011292
11293 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11294 if (e->type != KVM_IRQ_ROUTING_MSI)
11295 continue;
11296 /*
11297 * VT-d PI cannot support posting multicast/broadcast
11298 * interrupts to a vCPU, we still use interrupt remapping
11299 * for these kind of interrupts.
11300 *
11301 * For lowest-priority interrupts, we only support
11302 * those with single CPU as the destination, e.g. user
11303 * configures the interrupts via /proc/irq or uses
11304 * irqbalance to make the interrupts single-CPU.
11305 *
11306 * We will support full lowest-priority interrupt later.
11307 */
11308
Radim Krčmář371313132016-07-12 22:09:27 +020011309 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011310 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11311 /*
11312 * Make sure the IRTE is in remapped mode if
11313 * we don't handle it in posted mode.
11314 */
11315 ret = irq_set_vcpu_affinity(host_irq, NULL);
11316 if (ret < 0) {
11317 printk(KERN_INFO
11318 "failed to back to remapped mode, irq: %u\n",
11319 host_irq);
11320 goto out;
11321 }
11322
Feng Wuefc64402015-09-18 22:29:51 +080011323 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011324 }
Feng Wuefc64402015-09-18 22:29:51 +080011325
11326 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11327 vcpu_info.vector = irq.vector;
11328
Feng Wub6ce9782016-01-25 16:53:35 +080011329 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011330 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11331
11332 if (set)
11333 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011334 else
Feng Wuefc64402015-09-18 22:29:51 +080011335 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011336
11337 if (ret < 0) {
11338 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11339 __func__);
11340 goto out;
11341 }
11342 }
11343
11344 ret = 0;
11345out:
11346 srcu_read_unlock(&kvm->irq_srcu, idx);
11347 return ret;
11348}
11349
Ashok Rajc45dcc72016-06-22 14:59:56 +080011350static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11351{
11352 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11353 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11354 FEATURE_CONTROL_LMCE;
11355 else
11356 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11357 ~FEATURE_CONTROL_LMCE;
11358}
11359
Kees Cook404f6aa2016-08-08 16:29:06 -070011360static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011361 .cpu_has_kvm_support = cpu_has_kvm_support,
11362 .disabled_by_bios = vmx_disabled_by_bios,
11363 .hardware_setup = hardware_setup,
11364 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011365 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011366 .hardware_enable = hardware_enable,
11367 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011368 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackyb9655922018-05-10 22:06:39 +020011369 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011370
11371 .vcpu_create = vmx_create_vcpu,
11372 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011373 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011374
Avi Kivity04d2cc72007-09-10 18:10:54 +030011375 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011376 .vcpu_load = vmx_vcpu_load,
11377 .vcpu_put = vmx_vcpu_put,
11378
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011379 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011380 .get_msr = vmx_get_msr,
11381 .set_msr = vmx_set_msr,
11382 .get_segment_base = vmx_get_segment_base,
11383 .get_segment = vmx_get_segment,
11384 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011385 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011386 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011387 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011388 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011389 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011390 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011391 .set_cr3 = vmx_set_cr3,
11392 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011393 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011394 .get_idt = vmx_get_idt,
11395 .set_idt = vmx_set_idt,
11396 .get_gdt = vmx_get_gdt,
11397 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011398 .get_dr6 = vmx_get_dr6,
11399 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011400 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011401 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011402 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011403 .get_rflags = vmx_get_rflags,
11404 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011405
11406 .get_pkru = vmx_get_pkru,
11407
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011408 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011409 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011410
11411 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011412
Avi Kivity6aa8b732006-12-10 02:21:36 -080011413 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011414 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011415 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011416 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11417 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011418 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011419 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011420 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011421 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011422 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011423 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011424 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011425 .get_nmi_mask = vmx_get_nmi_mask,
11426 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011427 .enable_nmi_window = enable_nmi_window,
11428 .enable_irq_window = enable_irq_window,
11429 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011430 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011431 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011432 .get_enable_apicv = vmx_get_enable_apicv,
11433 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011434 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11435 .hwapic_irr_update = vmx_hwapic_irr_update,
11436 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011437 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11438 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011439
Izik Eiduscbc94022007-10-25 00:29:55 +020011440 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011441 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011442 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011443
Avi Kivity586f9602010-11-18 13:09:54 +020011444 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011445
Sheng Yang17cc3932010-01-05 19:02:27 +080011446 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011447
11448 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011449
11450 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011451 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011452
11453 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011454
11455 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011456
11457 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011458
11459 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011460
11461 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011462 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011463 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011464 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011465
11466 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011467
11468 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011469
11470 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11471 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11472 .flush_log_dirty = vmx_flush_log_dirty,
11473 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011474
Feng Wubf9f6ac2015-09-18 22:29:55 +080011475 .pre_block = vmx_pre_block,
11476 .post_block = vmx_post_block,
11477
Wei Huang25462f72015-06-19 15:45:05 +020011478 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011479
11480 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011481
11482#ifdef CONFIG_X86_64
11483 .set_hv_timer = vmx_set_hv_timer,
11484 .cancel_hv_timer = vmx_cancel_hv_timer,
11485#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011486
11487 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011488};
11489
11490static int __init vmx_init(void)
11491{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011492 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11493 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011494 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011495 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011496
Dave Young2965faa2015-09-09 15:38:55 -070011497#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011498 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11499 crash_vmclear_local_loaded_vmcss);
11500#endif
11501
He, Qingfdef3ad2007-04-30 09:45:24 +030011502 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011503}
11504
11505static void __exit vmx_exit(void)
11506{
Dave Young2965faa2015-09-09 15:38:55 -070011507#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011508 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011509 synchronize_rcu();
11510#endif
11511
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011512 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011513}
11514
11515module_init(vmx_init)
11516module_exit(vmx_exit)