blob: 0e61b9226bf23ab9625f22d5b0424fc7a7fbf405 [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>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030036#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030037#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040038
Feng Wu28b835d2015-09-18 22:29:54 +080039#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080040#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080041#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020042#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020043#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080044#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020045#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020046#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010047#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080048#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010049#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080050#include <asm/irq_remapping.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Marcelo Tosatti229456f2009-06-17 09:22:14 -030052#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020053#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030054
Avi Kivity4ecac3f2008-05-13 13:23:38 +030055#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040056#define __ex_clear(x, reg) \
57 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030058
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Josh Triplette9bda3b2012-03-20 23:33:51 -070062static const struct x86_cpu_id vmx_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_VMX),
64 {}
65};
66MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
67
Rusty Russell476bc002012-01-13 09:32:18 +103068static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020069module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080070
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020072module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020073
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080076
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070078module_param_named(unrestricted_guest,
79 enable_unrestricted_guest, bool, S_IRUGO);
80
Xudong Hao83c3a332012-05-28 19:33:35 +080081static bool __read_mostly enable_ept_ad_bits = 1;
82module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
83
Avi Kivitya27685c2012-06-12 20:30:18 +030084static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020085module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080088module_param(vmm_exclusive, bool, S_IRUGO);
89
Rusty Russell476bc002012-01-13 09:32:18 +103090static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030091module_param(fasteoi, bool, S_IRUGO);
92
Yang Zhang5a717852013-04-11 19:25:16 +080093static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080094module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080095
Abel Gordonabc4fc52013-04-18 14:35:25 +030096static bool __read_mostly enable_shadow_vmcs = 1;
97module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030098/*
99 * If nested=1, nested virtualization is supported, i.e., guests may use
100 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
101 * use VMX instructions.
102 */
Rusty Russell476bc002012-01-13 09:32:18 +1030103static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300104module_param(nested, bool, S_IRUGO);
105
Wanpeng Li20300092014-12-02 19:14:59 +0800106static u64 __read_mostly host_xss;
107
Kai Huang843e4332015-01-28 10:54:28 +0800108static bool __read_mostly enable_pml = 1;
109module_param_named(pml, enable_pml, bool, S_IRUGO);
110
Haozhong Zhang64903d62015-10-20 15:39:09 +0800111#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
112
Yunhong Jiang64672c92016-06-13 14:19:59 -0700113/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
114static int __read_mostly cpu_preemption_timer_multi;
115static bool __read_mostly enable_preemption_timer = 1;
116#ifdef CONFIG_X86_64
117module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
118#endif
119
Gleb Natapov50378782013-02-04 16:00:28 +0200120#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
121#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200122#define KVM_VM_CR0_ALWAYS_ON \
123 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200124#define KVM_CR4_GUEST_OWNED_BITS \
125 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700126 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200127
Avi Kivitycdc0e242009-12-06 17:21:14 +0200128#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
129#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
130
Avi Kivity78ac8b42010-04-08 18:19:35 +0300131#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
132
Jan Kiszkaf4124502014-03-07 20:03:13 +0100133#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
134
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800135/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300136 * Hyper-V requires all of these, so mark them as supported even though
137 * they are just treated the same as all-context.
138 */
139#define VMX_VPID_EXTENT_SUPPORTED_MASK \
140 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
141 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
142 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
143 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
144
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800145/*
146 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
147 * ple_gap: upper bound on the amount of time between two successive
148 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500149 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800150 * ple_window: upper bound on the amount of time a guest is allowed to execute
151 * in a PAUSE loop. Tests indicate that most spinlocks are held for
152 * less than 2^12 cycles
153 * Time is measured based on a counter that runs at the same rate as the TSC,
154 * refer SDM volume 3b section 21.6.13 & 22.1.3.
155 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200156#define KVM_VMX_DEFAULT_PLE_GAP 128
157#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
158#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
159#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
160#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
161 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
162
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800163static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
164module_param(ple_gap, int, S_IRUGO);
165
166static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
167module_param(ple_window, int, S_IRUGO);
168
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200169/* Default doubles per-vcpu window every exit. */
170static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
171module_param(ple_window_grow, int, S_IRUGO);
172
173/* Default resets per-vcpu window every exit to ple_window. */
174static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
175module_param(ple_window_shrink, int, S_IRUGO);
176
177/* Default is to compute the maximum so we can never overflow. */
178static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
179static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
180module_param(ple_window_max, int, S_IRUGO);
181
Avi Kivity83287ea422012-09-16 15:10:57 +0300182extern const ulong vmx_return;
183
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200184#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300185#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300186
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400187struct vmcs {
188 u32 revision_id;
189 u32 abort;
190 char data[0];
191};
192
Nadav Har'Eld462b812011-05-24 15:26:10 +0300193/*
194 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
195 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
196 * loaded on this CPU (so we can clear them if the CPU goes down).
197 */
198struct loaded_vmcs {
199 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700200 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300201 int cpu;
202 int launched;
203 struct list_head loaded_vmcss_on_cpu_link;
204};
205
Avi Kivity26bb0982009-09-07 11:14:12 +0300206struct shared_msr_entry {
207 unsigned index;
208 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200209 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300210};
211
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300212/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300213 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
214 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
215 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
216 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
217 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
218 * More than one of these structures may exist, if L1 runs multiple L2 guests.
219 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
220 * underlying hardware which will be used to run L2.
221 * This structure is packed to ensure that its layout is identical across
222 * machines (necessary for live migration).
223 * If there are changes in this struct, VMCS12_REVISION must be changed.
224 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300225typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300226struct __packed vmcs12 {
227 /* According to the Intel spec, a VMCS region must start with the
228 * following two fields. Then follow implementation-specific data.
229 */
230 u32 revision_id;
231 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300232
Nadav Har'El27d6c862011-05-25 23:06:59 +0300233 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
234 u32 padding[7]; /* room for future expansion */
235
Nadav Har'El22bd0352011-05-25 23:05:57 +0300236 u64 io_bitmap_a;
237 u64 io_bitmap_b;
238 u64 msr_bitmap;
239 u64 vm_exit_msr_store_addr;
240 u64 vm_exit_msr_load_addr;
241 u64 vm_entry_msr_load_addr;
242 u64 tsc_offset;
243 u64 virtual_apic_page_addr;
244 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800245 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300246 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800247 u64 eoi_exit_bitmap0;
248 u64 eoi_exit_bitmap1;
249 u64 eoi_exit_bitmap2;
250 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800251 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300252 u64 guest_physical_address;
253 u64 vmcs_link_pointer;
254 u64 guest_ia32_debugctl;
255 u64 guest_ia32_pat;
256 u64 guest_ia32_efer;
257 u64 guest_ia32_perf_global_ctrl;
258 u64 guest_pdptr0;
259 u64 guest_pdptr1;
260 u64 guest_pdptr2;
261 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100262 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300263 u64 host_ia32_pat;
264 u64 host_ia32_efer;
265 u64 host_ia32_perf_global_ctrl;
266 u64 padding64[8]; /* room for future expansion */
267 /*
268 * To allow migration of L1 (complete with its L2 guests) between
269 * machines of different natural widths (32 or 64 bit), we cannot have
270 * unsigned long fields with no explict size. We use u64 (aliased
271 * natural_width) instead. Luckily, x86 is little-endian.
272 */
273 natural_width cr0_guest_host_mask;
274 natural_width cr4_guest_host_mask;
275 natural_width cr0_read_shadow;
276 natural_width cr4_read_shadow;
277 natural_width cr3_target_value0;
278 natural_width cr3_target_value1;
279 natural_width cr3_target_value2;
280 natural_width cr3_target_value3;
281 natural_width exit_qualification;
282 natural_width guest_linear_address;
283 natural_width guest_cr0;
284 natural_width guest_cr3;
285 natural_width guest_cr4;
286 natural_width guest_es_base;
287 natural_width guest_cs_base;
288 natural_width guest_ss_base;
289 natural_width guest_ds_base;
290 natural_width guest_fs_base;
291 natural_width guest_gs_base;
292 natural_width guest_ldtr_base;
293 natural_width guest_tr_base;
294 natural_width guest_gdtr_base;
295 natural_width guest_idtr_base;
296 natural_width guest_dr7;
297 natural_width guest_rsp;
298 natural_width guest_rip;
299 natural_width guest_rflags;
300 natural_width guest_pending_dbg_exceptions;
301 natural_width guest_sysenter_esp;
302 natural_width guest_sysenter_eip;
303 natural_width host_cr0;
304 natural_width host_cr3;
305 natural_width host_cr4;
306 natural_width host_fs_base;
307 natural_width host_gs_base;
308 natural_width host_tr_base;
309 natural_width host_gdtr_base;
310 natural_width host_idtr_base;
311 natural_width host_ia32_sysenter_esp;
312 natural_width host_ia32_sysenter_eip;
313 natural_width host_rsp;
314 natural_width host_rip;
315 natural_width paddingl[8]; /* room for future expansion */
316 u32 pin_based_vm_exec_control;
317 u32 cpu_based_vm_exec_control;
318 u32 exception_bitmap;
319 u32 page_fault_error_code_mask;
320 u32 page_fault_error_code_match;
321 u32 cr3_target_count;
322 u32 vm_exit_controls;
323 u32 vm_exit_msr_store_count;
324 u32 vm_exit_msr_load_count;
325 u32 vm_entry_controls;
326 u32 vm_entry_msr_load_count;
327 u32 vm_entry_intr_info_field;
328 u32 vm_entry_exception_error_code;
329 u32 vm_entry_instruction_len;
330 u32 tpr_threshold;
331 u32 secondary_vm_exec_control;
332 u32 vm_instruction_error;
333 u32 vm_exit_reason;
334 u32 vm_exit_intr_info;
335 u32 vm_exit_intr_error_code;
336 u32 idt_vectoring_info_field;
337 u32 idt_vectoring_error_code;
338 u32 vm_exit_instruction_len;
339 u32 vmx_instruction_info;
340 u32 guest_es_limit;
341 u32 guest_cs_limit;
342 u32 guest_ss_limit;
343 u32 guest_ds_limit;
344 u32 guest_fs_limit;
345 u32 guest_gs_limit;
346 u32 guest_ldtr_limit;
347 u32 guest_tr_limit;
348 u32 guest_gdtr_limit;
349 u32 guest_idtr_limit;
350 u32 guest_es_ar_bytes;
351 u32 guest_cs_ar_bytes;
352 u32 guest_ss_ar_bytes;
353 u32 guest_ds_ar_bytes;
354 u32 guest_fs_ar_bytes;
355 u32 guest_gs_ar_bytes;
356 u32 guest_ldtr_ar_bytes;
357 u32 guest_tr_ar_bytes;
358 u32 guest_interruptibility_info;
359 u32 guest_activity_state;
360 u32 guest_sysenter_cs;
361 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100362 u32 vmx_preemption_timer_value;
363 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300364 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800365 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300366 u16 guest_es_selector;
367 u16 guest_cs_selector;
368 u16 guest_ss_selector;
369 u16 guest_ds_selector;
370 u16 guest_fs_selector;
371 u16 guest_gs_selector;
372 u16 guest_ldtr_selector;
373 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800374 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300375 u16 host_es_selector;
376 u16 host_cs_selector;
377 u16 host_ss_selector;
378 u16 host_ds_selector;
379 u16 host_fs_selector;
380 u16 host_gs_selector;
381 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300382};
383
384/*
385 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
386 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
387 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
388 */
389#define VMCS12_REVISION 0x11e57ed0
390
391/*
392 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
393 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
394 * current implementation, 4K are reserved to avoid future complications.
395 */
396#define VMCS12_SIZE 0x1000
397
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300398/* Used to remember the last vmcs02 used for some recently used vmcs12s */
399struct vmcs02_list {
400 struct list_head list;
401 gpa_t vmptr;
402 struct loaded_vmcs vmcs02;
403};
404
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300405/*
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
431 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
432 struct list_head vmcs02_pool;
433 int vmcs02_num;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200434 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300435 /* L2 must run next, and mustn't decide to exit to L1. */
436 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300437 /*
438 * Guest pages referred to in vmcs02 with host-physical pointers, so
439 * we must keep them pinned while L2 runs.
440 */
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
Radim Krčmářd048c092016-08-08 20:16:22 +0200448 unsigned long *msr_bitmap;
449
Jan Kiszkaf4124502014-03-07 20:03:13 +0100450 struct hrtimer preemption_timer;
451 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200452
453 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
454 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800455
Wanpeng Li5c614b32015-10-13 09:18:36 -0700456 u16 vpid02;
457 u16 last_vpid;
458
David Matlack0115f9c2016-11-29 18:14:06 -0800459 /*
460 * We only store the "true" versions of the VMX capability MSRs. We
461 * generate the "non-true" versions by setting the must-be-1 bits
462 * according to the SDM.
463 */
Wincy Vanb9c237b2015-02-03 23:56:30 +0800464 u32 nested_vmx_procbased_ctls_low;
465 u32 nested_vmx_procbased_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800466 u32 nested_vmx_secondary_ctls_low;
467 u32 nested_vmx_secondary_ctls_high;
468 u32 nested_vmx_pinbased_ctls_low;
469 u32 nested_vmx_pinbased_ctls_high;
470 u32 nested_vmx_exit_ctls_low;
471 u32 nested_vmx_exit_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800472 u32 nested_vmx_entry_ctls_low;
473 u32 nested_vmx_entry_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800474 u32 nested_vmx_misc_low;
475 u32 nested_vmx_misc_high;
476 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700477 u32 nested_vmx_vpid_caps;
David Matlack62cc6b9d2016-11-29 18:14:07 -0800478 u64 nested_vmx_basic;
479 u64 nested_vmx_cr0_fixed0;
480 u64 nested_vmx_cr0_fixed1;
481 u64 nested_vmx_cr4_fixed0;
482 u64 nested_vmx_cr4_fixed1;
483 u64 nested_vmx_vmcs_enum;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300484};
485
Yang Zhang01e439b2013-04-11 19:25:12 +0800486#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800487#define POSTED_INTR_SN 1
488
Yang Zhang01e439b2013-04-11 19:25:12 +0800489/* Posted-Interrupt Descriptor */
490struct pi_desc {
491 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800492 union {
493 struct {
494 /* bit 256 - Outstanding Notification */
495 u16 on : 1,
496 /* bit 257 - Suppress Notification */
497 sn : 1,
498 /* bit 271:258 - Reserved */
499 rsvd_1 : 14;
500 /* bit 279:272 - Notification Vector */
501 u8 nv;
502 /* bit 287:280 - Reserved */
503 u8 rsvd_2;
504 /* bit 319:288 - Notification Destination */
505 u32 ndst;
506 };
507 u64 control;
508 };
509 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800510} __aligned(64);
511
Yang Zhanga20ed542013-04-11 19:25:15 +0800512static bool pi_test_and_set_on(struct pi_desc *pi_desc)
513{
514 return test_and_set_bit(POSTED_INTR_ON,
515 (unsigned long *)&pi_desc->control);
516}
517
518static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
519{
520 return test_and_clear_bit(POSTED_INTR_ON,
521 (unsigned long *)&pi_desc->control);
522}
523
524static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
525{
526 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
527}
528
Feng Wuebbfc762015-09-18 22:29:46 +0800529static inline void pi_clear_sn(struct pi_desc *pi_desc)
530{
531 return clear_bit(POSTED_INTR_SN,
532 (unsigned long *)&pi_desc->control);
533}
534
535static inline void pi_set_sn(struct pi_desc *pi_desc)
536{
537 return set_bit(POSTED_INTR_SN,
538 (unsigned long *)&pi_desc->control);
539}
540
Paolo Bonziniad361092016-09-20 16:15:05 +0200541static inline void pi_clear_on(struct pi_desc *pi_desc)
542{
543 clear_bit(POSTED_INTR_ON,
544 (unsigned long *)&pi_desc->control);
545}
546
Feng Wuebbfc762015-09-18 22:29:46 +0800547static inline int pi_test_on(struct pi_desc *pi_desc)
548{
549 return test_bit(POSTED_INTR_ON,
550 (unsigned long *)&pi_desc->control);
551}
552
553static inline int pi_test_sn(struct pi_desc *pi_desc)
554{
555 return test_bit(POSTED_INTR_SN,
556 (unsigned long *)&pi_desc->control);
557}
558
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400559struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000560 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300561 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300562 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200563 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300564 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200565 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200566 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300567 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400568 int nmsrs;
569 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800570 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400571#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300572 u64 msr_host_kernel_gs_base;
573 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400574#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200575 u32 vm_entry_controls_shadow;
576 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300577 /*
578 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
579 * non-nested (L1) guest, it always points to vmcs01. For a nested
580 * guest (L2), it points to a different VMCS.
581 */
582 struct loaded_vmcs vmcs01;
583 struct loaded_vmcs *loaded_vmcs;
584 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300585 struct msr_autoload {
586 unsigned nr;
587 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
588 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
589 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400590 struct {
591 int loaded;
592 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300593#ifdef CONFIG_X86_64
594 u16 ds_sel, es_sel;
595#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200596 int gs_ldt_reload_needed;
597 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000598 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700599 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400600 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200601 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300602 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300603 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300604 struct kvm_segment segs[8];
605 } rmode;
606 struct {
607 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300608 struct kvm_save_segment {
609 u16 selector;
610 unsigned long base;
611 u32 limit;
612 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300613 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300614 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800615 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300616 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200617
Andi Kleena0861c02009-06-08 17:37:09 +0800618 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800619
Yang Zhang01e439b2013-04-11 19:25:12 +0800620 /* Posted interrupt descriptor */
621 struct pi_desc pi_desc;
622
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300623 /* Support for a guest hypervisor (nested VMX) */
624 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200625
626 /* Dynamic PLE window. */
627 int ple_window;
628 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800629
630 /* Support for PML */
631#define PML_ENTITY_NUM 512
632 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800633
Yunhong Jiang64672c92016-06-13 14:19:59 -0700634 /* apic deadline value in host tsc */
635 u64 hv_deadline_tsc;
636
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800637 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800638
639 bool guest_pkru_valid;
640 u32 guest_pkru;
641 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800642
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800643 /*
644 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
645 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
646 * in msr_ia32_feature_control_valid_bits.
647 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800648 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800649 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400650};
651
Avi Kivity2fb92db2011-04-27 19:42:18 +0300652enum segment_cache_field {
653 SEG_FIELD_SEL = 0,
654 SEG_FIELD_BASE = 1,
655 SEG_FIELD_LIMIT = 2,
656 SEG_FIELD_AR = 3,
657
658 SEG_FIELD_NR = 4
659};
660
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400661static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
662{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000663 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400664}
665
Feng Wuefc64402015-09-18 22:29:51 +0800666static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
667{
668 return &(to_vmx(vcpu)->pi_desc);
669}
670
Nadav Har'El22bd0352011-05-25 23:05:57 +0300671#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
672#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
673#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
674 [number##_HIGH] = VMCS12_OFFSET(name)+4
675
Abel Gordon4607c2d2013-04-18 14:35:55 +0300676
Bandan Dasfe2b2012014-04-21 15:20:14 -0400677static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300678 /*
679 * We do NOT shadow fields that are modified when L0
680 * traps and emulates any vmx instruction (e.g. VMPTRLD,
681 * VMXON...) executed by L1.
682 * For example, VM_INSTRUCTION_ERROR is read
683 * by L1 if a vmx instruction fails (part of the error path).
684 * Note the code assumes this logic. If for some reason
685 * we start shadowing these fields then we need to
686 * force a shadow sync when L0 emulates vmx instructions
687 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
688 * by nested_vmx_failValid)
689 */
690 VM_EXIT_REASON,
691 VM_EXIT_INTR_INFO,
692 VM_EXIT_INSTRUCTION_LEN,
693 IDT_VECTORING_INFO_FIELD,
694 IDT_VECTORING_ERROR_CODE,
695 VM_EXIT_INTR_ERROR_CODE,
696 EXIT_QUALIFICATION,
697 GUEST_LINEAR_ADDRESS,
698 GUEST_PHYSICAL_ADDRESS
699};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400700static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300701 ARRAY_SIZE(shadow_read_only_fields);
702
Bandan Dasfe2b2012014-04-21 15:20:14 -0400703static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800704 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300705 GUEST_RIP,
706 GUEST_RSP,
707 GUEST_CR0,
708 GUEST_CR3,
709 GUEST_CR4,
710 GUEST_INTERRUPTIBILITY_INFO,
711 GUEST_RFLAGS,
712 GUEST_CS_SELECTOR,
713 GUEST_CS_AR_BYTES,
714 GUEST_CS_LIMIT,
715 GUEST_CS_BASE,
716 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100717 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300718 CR0_GUEST_HOST_MASK,
719 CR0_READ_SHADOW,
720 CR4_READ_SHADOW,
721 TSC_OFFSET,
722 EXCEPTION_BITMAP,
723 CPU_BASED_VM_EXEC_CONTROL,
724 VM_ENTRY_EXCEPTION_ERROR_CODE,
725 VM_ENTRY_INTR_INFO_FIELD,
726 VM_ENTRY_INSTRUCTION_LEN,
727 VM_ENTRY_EXCEPTION_ERROR_CODE,
728 HOST_FS_BASE,
729 HOST_GS_BASE,
730 HOST_FS_SELECTOR,
731 HOST_GS_SELECTOR
732};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400733static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300734 ARRAY_SIZE(shadow_read_write_fields);
735
Mathias Krause772e0312012-08-30 01:30:19 +0200736static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300737 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800738 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300739 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
740 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
741 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
742 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
743 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
744 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
745 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
746 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800747 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300748 FIELD(HOST_ES_SELECTOR, host_es_selector),
749 FIELD(HOST_CS_SELECTOR, host_cs_selector),
750 FIELD(HOST_SS_SELECTOR, host_ss_selector),
751 FIELD(HOST_DS_SELECTOR, host_ds_selector),
752 FIELD(HOST_FS_SELECTOR, host_fs_selector),
753 FIELD(HOST_GS_SELECTOR, host_gs_selector),
754 FIELD(HOST_TR_SELECTOR, host_tr_selector),
755 FIELD64(IO_BITMAP_A, io_bitmap_a),
756 FIELD64(IO_BITMAP_B, io_bitmap_b),
757 FIELD64(MSR_BITMAP, msr_bitmap),
758 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
759 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
760 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
761 FIELD64(TSC_OFFSET, tsc_offset),
762 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
763 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800764 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300765 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800766 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
767 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
768 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
769 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800770 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300771 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
772 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
773 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
774 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
775 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
776 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
777 FIELD64(GUEST_PDPTR0, guest_pdptr0),
778 FIELD64(GUEST_PDPTR1, guest_pdptr1),
779 FIELD64(GUEST_PDPTR2, guest_pdptr2),
780 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100781 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300782 FIELD64(HOST_IA32_PAT, host_ia32_pat),
783 FIELD64(HOST_IA32_EFER, host_ia32_efer),
784 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
785 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
786 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
787 FIELD(EXCEPTION_BITMAP, exception_bitmap),
788 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
789 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
790 FIELD(CR3_TARGET_COUNT, cr3_target_count),
791 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
792 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
793 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
794 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
795 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
796 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
797 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
798 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
799 FIELD(TPR_THRESHOLD, tpr_threshold),
800 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
801 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
802 FIELD(VM_EXIT_REASON, vm_exit_reason),
803 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
804 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
805 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
806 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
807 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
808 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
809 FIELD(GUEST_ES_LIMIT, guest_es_limit),
810 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
811 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
812 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
813 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
814 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
815 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
816 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
817 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
818 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
819 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
820 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
821 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
822 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
823 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
824 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
825 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
826 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
827 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
828 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
829 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
830 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100831 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300832 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
833 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
834 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
835 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
836 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
837 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
838 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
839 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
840 FIELD(EXIT_QUALIFICATION, exit_qualification),
841 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
842 FIELD(GUEST_CR0, guest_cr0),
843 FIELD(GUEST_CR3, guest_cr3),
844 FIELD(GUEST_CR4, guest_cr4),
845 FIELD(GUEST_ES_BASE, guest_es_base),
846 FIELD(GUEST_CS_BASE, guest_cs_base),
847 FIELD(GUEST_SS_BASE, guest_ss_base),
848 FIELD(GUEST_DS_BASE, guest_ds_base),
849 FIELD(GUEST_FS_BASE, guest_fs_base),
850 FIELD(GUEST_GS_BASE, guest_gs_base),
851 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
852 FIELD(GUEST_TR_BASE, guest_tr_base),
853 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
854 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
855 FIELD(GUEST_DR7, guest_dr7),
856 FIELD(GUEST_RSP, guest_rsp),
857 FIELD(GUEST_RIP, guest_rip),
858 FIELD(GUEST_RFLAGS, guest_rflags),
859 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
860 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
861 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
862 FIELD(HOST_CR0, host_cr0),
863 FIELD(HOST_CR3, host_cr3),
864 FIELD(HOST_CR4, host_cr4),
865 FIELD(HOST_FS_BASE, host_fs_base),
866 FIELD(HOST_GS_BASE, host_gs_base),
867 FIELD(HOST_TR_BASE, host_tr_base),
868 FIELD(HOST_GDTR_BASE, host_gdtr_base),
869 FIELD(HOST_IDTR_BASE, host_idtr_base),
870 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
871 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
872 FIELD(HOST_RSP, host_rsp),
873 FIELD(HOST_RIP, host_rip),
874};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300875
876static inline short vmcs_field_to_offset(unsigned long field)
877{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100878 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
879
880 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
881 vmcs_field_to_offset_table[field] == 0)
882 return -ENOENT;
883
Nadav Har'El22bd0352011-05-25 23:05:57 +0300884 return vmcs_field_to_offset_table[field];
885}
886
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300887static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
888{
David Matlack4f2777b2016-07-13 17:16:37 -0700889 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300890}
891
892static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
893{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200894 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800895 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300896 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800897
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300898 return page;
899}
900
901static void nested_release_page(struct page *page)
902{
903 kvm_release_page_dirty(page);
904}
905
906static void nested_release_page_clean(struct page *page)
907{
908 kvm_release_page_clean(page);
909}
910
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300911static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800912static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800913static void kvm_cpu_vmxon(u64 addr);
914static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800915static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200916static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300917static void vmx_set_segment(struct kvm_vcpu *vcpu,
918 struct kvm_segment *var, int seg);
919static void vmx_get_segment(struct kvm_vcpu *vcpu,
920 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200921static bool guest_state_valid(struct kvm_vcpu *vcpu);
922static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300923static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300924static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800925static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300926
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927static DEFINE_PER_CPU(struct vmcs *, vmxarea);
928static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300929/*
930 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
931 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
932 */
933static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300934static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935
Feng Wubf9f6ac2015-09-18 22:29:55 +0800936/*
937 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
938 * can find which vCPU should be waken up.
939 */
940static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
941static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
942
Radim Krčmář23611332016-09-29 22:41:33 +0200943enum {
944 VMX_IO_BITMAP_A,
945 VMX_IO_BITMAP_B,
946 VMX_MSR_BITMAP_LEGACY,
947 VMX_MSR_BITMAP_LONGMODE,
948 VMX_MSR_BITMAP_LEGACY_X2APIC_APICV,
949 VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV,
950 VMX_MSR_BITMAP_LEGACY_X2APIC,
951 VMX_MSR_BITMAP_LONGMODE_X2APIC,
952 VMX_VMREAD_BITMAP,
953 VMX_VMWRITE_BITMAP,
954 VMX_BITMAP_NR
955};
956
957static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
958
959#define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A])
960#define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B])
961#define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
962#define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
963#define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
964#define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
965#define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
966#define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
967#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
968#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300969
Avi Kivity110312c2010-12-21 12:54:20 +0200970static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200971static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200972
Sheng Yang2384d2b2008-01-17 15:14:33 +0800973static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
974static DEFINE_SPINLOCK(vmx_vpid_lock);
975
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300976static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800977 int size;
978 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300979 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300981 u32 pin_based_exec_ctrl;
982 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800983 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300984 u32 vmexit_ctrl;
985 u32 vmentry_ctrl;
986} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987
Hannes Ederefff9e52008-11-28 17:02:06 +0100988static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800989 u32 ept;
990 u32 vpid;
991} vmx_capability;
992
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993#define VMX_SEGMENT_FIELD(seg) \
994 [VCPU_SREG_##seg] = { \
995 .selector = GUEST_##seg##_SELECTOR, \
996 .base = GUEST_##seg##_BASE, \
997 .limit = GUEST_##seg##_LIMIT, \
998 .ar_bytes = GUEST_##seg##_AR_BYTES, \
999 }
1000
Mathias Krause772e0312012-08-30 01:30:19 +02001001static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002 unsigned selector;
1003 unsigned base;
1004 unsigned limit;
1005 unsigned ar_bytes;
1006} kvm_vmx_segment_fields[] = {
1007 VMX_SEGMENT_FIELD(CS),
1008 VMX_SEGMENT_FIELD(DS),
1009 VMX_SEGMENT_FIELD(ES),
1010 VMX_SEGMENT_FIELD(FS),
1011 VMX_SEGMENT_FIELD(GS),
1012 VMX_SEGMENT_FIELD(SS),
1013 VMX_SEGMENT_FIELD(TR),
1014 VMX_SEGMENT_FIELD(LDTR),
1015};
1016
Avi Kivity26bb0982009-09-07 11:14:12 +03001017static u64 host_efer;
1018
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001019static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1020
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001021/*
Brian Gerst8c065852010-07-17 09:03:26 -04001022 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001023 * away by decrementing the array size.
1024 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001026#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001027 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001029 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031
Jan Kiszka5bb16012016-02-09 20:14:21 +01001032static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033{
1034 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1035 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001036 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1037}
1038
Jan Kiszka6f054852016-02-09 20:15:18 +01001039static inline bool is_debug(u32 intr_info)
1040{
1041 return is_exception_n(intr_info, DB_VECTOR);
1042}
1043
1044static inline bool is_breakpoint(u32 intr_info)
1045{
1046 return is_exception_n(intr_info, BP_VECTOR);
1047}
1048
Jan Kiszka5bb16012016-02-09 20:14:21 +01001049static inline bool is_page_fault(u32 intr_info)
1050{
1051 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052}
1053
Gui Jianfeng31299942010-03-15 17:29:09 +08001054static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001055{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001056 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001057}
1058
Gui Jianfeng31299942010-03-15 17:29:09 +08001059static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001060{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001061 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001062}
1063
Gui Jianfeng31299942010-03-15 17:29:09 +08001064static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001065{
1066 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1067 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1068}
1069
Gui Jianfeng31299942010-03-15 17:29:09 +08001070static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001071{
1072 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1073 INTR_INFO_VALID_MASK)) ==
1074 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1075}
1076
Gui Jianfeng31299942010-03-15 17:29:09 +08001077static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001078{
Sheng Yang04547152009-04-01 15:52:31 +08001079 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001080}
1081
Gui Jianfeng31299942010-03-15 17:29:09 +08001082static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001083{
Sheng Yang04547152009-04-01 15:52:31 +08001084 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001085}
1086
Paolo Bonzini35754c92015-07-29 12:05:37 +02001087static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001088{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001089 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001090}
1091
Gui Jianfeng31299942010-03-15 17:29:09 +08001092static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001093{
Sheng Yang04547152009-04-01 15:52:31 +08001094 return vmcs_config.cpu_based_exec_ctrl &
1095 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001096}
1097
Avi Kivity774ead32007-12-26 13:57:04 +02001098static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001099{
Sheng Yang04547152009-04-01 15:52:31 +08001100 return vmcs_config.cpu_based_2nd_exec_ctrl &
1101 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1102}
1103
Yang Zhang8d146952013-01-25 10:18:50 +08001104static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1105{
1106 return vmcs_config.cpu_based_2nd_exec_ctrl &
1107 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1108}
1109
Yang Zhang83d4c282013-01-25 10:18:49 +08001110static inline bool cpu_has_vmx_apic_register_virt(void)
1111{
1112 return vmcs_config.cpu_based_2nd_exec_ctrl &
1113 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1114}
1115
Yang Zhangc7c9c562013-01-25 10:18:51 +08001116static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1117{
1118 return vmcs_config.cpu_based_2nd_exec_ctrl &
1119 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1120}
1121
Yunhong Jiang64672c92016-06-13 14:19:59 -07001122/*
1123 * Comment's format: document - errata name - stepping - processor name.
1124 * Refer from
1125 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1126 */
1127static u32 vmx_preemption_cpu_tfms[] = {
1128/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11290x000206E6,
1130/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1131/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1132/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11330x00020652,
1134/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11350x00020655,
1136/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1137/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1138/*
1139 * 320767.pdf - AAP86 - B1 -
1140 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1141 */
11420x000106E5,
1143/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11440x000106A0,
1145/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11460x000106A1,
1147/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11480x000106A4,
1149 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1150 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1151 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11520x000106A5,
1153};
1154
1155static inline bool cpu_has_broken_vmx_preemption_timer(void)
1156{
1157 u32 eax = cpuid_eax(0x00000001), i;
1158
1159 /* Clear the reserved bits */
1160 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001161 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001162 if (eax == vmx_preemption_cpu_tfms[i])
1163 return true;
1164
1165 return false;
1166}
1167
1168static inline bool cpu_has_vmx_preemption_timer(void)
1169{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001170 return vmcs_config.pin_based_exec_ctrl &
1171 PIN_BASED_VMX_PREEMPTION_TIMER;
1172}
1173
Yang Zhang01e439b2013-04-11 19:25:12 +08001174static inline bool cpu_has_vmx_posted_intr(void)
1175{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001176 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1177 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001178}
1179
1180static inline bool cpu_has_vmx_apicv(void)
1181{
1182 return cpu_has_vmx_apic_register_virt() &&
1183 cpu_has_vmx_virtual_intr_delivery() &&
1184 cpu_has_vmx_posted_intr();
1185}
1186
Sheng Yang04547152009-04-01 15:52:31 +08001187static inline bool cpu_has_vmx_flexpriority(void)
1188{
1189 return cpu_has_vmx_tpr_shadow() &&
1190 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001191}
1192
Marcelo Tosattie7997942009-06-11 12:07:40 -03001193static inline bool cpu_has_vmx_ept_execute_only(void)
1194{
Gui Jianfeng31299942010-03-15 17:29:09 +08001195 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001196}
1197
Marcelo Tosattie7997942009-06-11 12:07:40 -03001198static inline bool cpu_has_vmx_ept_2m_page(void)
1199{
Gui Jianfeng31299942010-03-15 17:29:09 +08001200 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001201}
1202
Sheng Yang878403b2010-01-05 19:02:29 +08001203static inline bool cpu_has_vmx_ept_1g_page(void)
1204{
Gui Jianfeng31299942010-03-15 17:29:09 +08001205 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001206}
1207
Sheng Yang4bc9b982010-06-02 14:05:24 +08001208static inline bool cpu_has_vmx_ept_4levels(void)
1209{
1210 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1211}
1212
Xudong Hao83c3a332012-05-28 19:33:35 +08001213static inline bool cpu_has_vmx_ept_ad_bits(void)
1214{
1215 return vmx_capability.ept & VMX_EPT_AD_BIT;
1216}
1217
Gui Jianfeng31299942010-03-15 17:29:09 +08001218static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001219{
Gui Jianfeng31299942010-03-15 17:29:09 +08001220 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001221}
1222
Gui Jianfeng31299942010-03-15 17:29:09 +08001223static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001224{
Gui Jianfeng31299942010-03-15 17:29:09 +08001225 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001226}
1227
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001228static inline bool cpu_has_vmx_invvpid_single(void)
1229{
1230 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1231}
1232
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001233static inline bool cpu_has_vmx_invvpid_global(void)
1234{
1235 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1236}
1237
Wanpeng Li08d839c2017-03-23 05:30:08 -07001238static inline bool cpu_has_vmx_invvpid(void)
1239{
1240 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1241}
1242
Gui Jianfeng31299942010-03-15 17:29:09 +08001243static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001244{
Sheng Yang04547152009-04-01 15:52:31 +08001245 return vmcs_config.cpu_based_2nd_exec_ctrl &
1246 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001247}
1248
Gui Jianfeng31299942010-03-15 17:29:09 +08001249static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001250{
1251 return vmcs_config.cpu_based_2nd_exec_ctrl &
1252 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1253}
1254
Gui Jianfeng31299942010-03-15 17:29:09 +08001255static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001256{
1257 return vmcs_config.cpu_based_2nd_exec_ctrl &
1258 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1259}
1260
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001261static inline bool cpu_has_vmx_basic_inout(void)
1262{
1263 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1264}
1265
Paolo Bonzini35754c92015-07-29 12:05:37 +02001266static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001267{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001268 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001269}
1270
Gui Jianfeng31299942010-03-15 17:29:09 +08001271static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001272{
Sheng Yang04547152009-04-01 15:52:31 +08001273 return vmcs_config.cpu_based_2nd_exec_ctrl &
1274 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001275}
1276
Gui Jianfeng31299942010-03-15 17:29:09 +08001277static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001278{
1279 return vmcs_config.cpu_based_2nd_exec_ctrl &
1280 SECONDARY_EXEC_RDTSCP;
1281}
1282
Mao, Junjiead756a12012-07-02 01:18:48 +00001283static inline bool cpu_has_vmx_invpcid(void)
1284{
1285 return vmcs_config.cpu_based_2nd_exec_ctrl &
1286 SECONDARY_EXEC_ENABLE_INVPCID;
1287}
1288
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001289static inline bool cpu_has_vmx_wbinvd_exit(void)
1290{
1291 return vmcs_config.cpu_based_2nd_exec_ctrl &
1292 SECONDARY_EXEC_WBINVD_EXITING;
1293}
1294
Abel Gordonabc4fc52013-04-18 14:35:25 +03001295static inline bool cpu_has_vmx_shadow_vmcs(void)
1296{
1297 u64 vmx_msr;
1298 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1299 /* check if the cpu supports writing r/o exit information fields */
1300 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1301 return false;
1302
1303 return vmcs_config.cpu_based_2nd_exec_ctrl &
1304 SECONDARY_EXEC_SHADOW_VMCS;
1305}
1306
Kai Huang843e4332015-01-28 10:54:28 +08001307static inline bool cpu_has_vmx_pml(void)
1308{
1309 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1310}
1311
Haozhong Zhang64903d62015-10-20 15:39:09 +08001312static inline bool cpu_has_vmx_tsc_scaling(void)
1313{
1314 return vmcs_config.cpu_based_2nd_exec_ctrl &
1315 SECONDARY_EXEC_TSC_SCALING;
1316}
1317
Sheng Yang04547152009-04-01 15:52:31 +08001318static inline bool report_flexpriority(void)
1319{
1320 return flexpriority_enabled;
1321}
1322
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001323static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1324{
1325 return vmcs12->cpu_based_vm_exec_control & bit;
1326}
1327
1328static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1329{
1330 return (vmcs12->cpu_based_vm_exec_control &
1331 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1332 (vmcs12->secondary_vm_exec_control & bit);
1333}
1334
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001335static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001336{
1337 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1338}
1339
Jan Kiszkaf4124502014-03-07 20:03:13 +01001340static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1341{
1342 return vmcs12->pin_based_vm_exec_control &
1343 PIN_BASED_VMX_PREEMPTION_TIMER;
1344}
1345
Nadav Har'El155a97a2013-08-05 11:07:16 +03001346static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1347{
1348 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1349}
1350
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001351static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1352{
1353 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1354 vmx_xsaves_supported();
1355}
1356
Wincy Vanf2b93282015-02-03 23:56:03 +08001357static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1358{
1359 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1360}
1361
Wanpeng Li5c614b32015-10-13 09:18:36 -07001362static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1363{
1364 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1365}
1366
Wincy Van82f0dd42015-02-03 23:57:18 +08001367static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1368{
1369 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1370}
1371
Wincy Van608406e2015-02-03 23:57:51 +08001372static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1373{
1374 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1375}
1376
Wincy Van705699a2015-02-03 23:58:17 +08001377static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1378{
1379 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1380}
1381
Jim Mattsonef85b672016-12-12 11:01:37 -08001382static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001383{
1384 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001385 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001386}
1387
Jan Kiszka533558b2014-01-04 18:47:20 +01001388static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1389 u32 exit_intr_info,
1390 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001391static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1392 struct vmcs12 *vmcs12,
1393 u32 reason, unsigned long qualification);
1394
Rusty Russell8b9cf982007-07-30 16:31:43 +10001395static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001396{
1397 int i;
1398
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001399 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001400 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001401 return i;
1402 return -1;
1403}
1404
Sheng Yang2384d2b2008-01-17 15:14:33 +08001405static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1406{
1407 struct {
1408 u64 vpid : 16;
1409 u64 rsvd : 48;
1410 u64 gva;
1411 } operand = { vpid, 0, gva };
1412
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001413 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001414 /* CF==1 or ZF==1 --> rc = -1 */
1415 "; ja 1f ; ud2 ; 1:"
1416 : : "a"(&operand), "c"(ext) : "cc", "memory");
1417}
1418
Sheng Yang14394422008-04-28 12:24:45 +08001419static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1420{
1421 struct {
1422 u64 eptp, gpa;
1423 } operand = {eptp, gpa};
1424
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001425 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001426 /* CF==1 or ZF==1 --> rc = -1 */
1427 "; ja 1f ; ud2 ; 1:\n"
1428 : : "a" (&operand), "c" (ext) : "cc", "memory");
1429}
1430
Avi Kivity26bb0982009-09-07 11:14:12 +03001431static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001432{
1433 int i;
1434
Rusty Russell8b9cf982007-07-30 16:31:43 +10001435 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001436 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001437 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001438 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001439}
1440
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441static void vmcs_clear(struct vmcs *vmcs)
1442{
1443 u64 phys_addr = __pa(vmcs);
1444 u8 error;
1445
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001446 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001447 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448 : "cc", "memory");
1449 if (error)
1450 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1451 vmcs, phys_addr);
1452}
1453
Nadav Har'Eld462b812011-05-24 15:26:10 +03001454static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1455{
1456 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001457 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1458 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001459 loaded_vmcs->cpu = -1;
1460 loaded_vmcs->launched = 0;
1461}
1462
Dongxiao Xu7725b892010-05-11 18:29:38 +08001463static void vmcs_load(struct vmcs *vmcs)
1464{
1465 u64 phys_addr = __pa(vmcs);
1466 u8 error;
1467
1468 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001469 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001470 : "cc", "memory");
1471 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001472 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001473 vmcs, phys_addr);
1474}
1475
Dave Young2965faa2015-09-09 15:38:55 -07001476#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001477/*
1478 * This bitmap is used to indicate whether the vmclear
1479 * operation is enabled on all cpus. All disabled by
1480 * default.
1481 */
1482static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1483
1484static inline void crash_enable_local_vmclear(int cpu)
1485{
1486 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1487}
1488
1489static inline void crash_disable_local_vmclear(int cpu)
1490{
1491 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1492}
1493
1494static inline int crash_local_vmclear_enabled(int cpu)
1495{
1496 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1497}
1498
1499static void crash_vmclear_local_loaded_vmcss(void)
1500{
1501 int cpu = raw_smp_processor_id();
1502 struct loaded_vmcs *v;
1503
1504 if (!crash_local_vmclear_enabled(cpu))
1505 return;
1506
1507 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1508 loaded_vmcss_on_cpu_link)
1509 vmcs_clear(v->vmcs);
1510}
1511#else
1512static inline void crash_enable_local_vmclear(int cpu) { }
1513static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001514#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001515
Nadav Har'Eld462b812011-05-24 15:26:10 +03001516static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001518 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001519 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001520
Nadav Har'Eld462b812011-05-24 15:26:10 +03001521 if (loaded_vmcs->cpu != cpu)
1522 return; /* vcpu migration can race with cpu offline */
1523 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001525 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001526 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001527
1528 /*
1529 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1530 * is before setting loaded_vmcs->vcpu to -1 which is done in
1531 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1532 * then adds the vmcs into percpu list before it is deleted.
1533 */
1534 smp_wmb();
1535
Nadav Har'Eld462b812011-05-24 15:26:10 +03001536 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001537 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538}
1539
Nadav Har'Eld462b812011-05-24 15:26:10 +03001540static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001541{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001542 int cpu = loaded_vmcs->cpu;
1543
1544 if (cpu != -1)
1545 smp_call_function_single(cpu,
1546 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001547}
1548
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001549static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001550{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001551 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001552 return;
1553
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001554 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001555 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001556}
1557
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001558static inline void vpid_sync_vcpu_global(void)
1559{
1560 if (cpu_has_vmx_invvpid_global())
1561 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1562}
1563
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001564static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001565{
1566 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001567 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001568 else
1569 vpid_sync_vcpu_global();
1570}
1571
Sheng Yang14394422008-04-28 12:24:45 +08001572static inline void ept_sync_global(void)
1573{
1574 if (cpu_has_vmx_invept_global())
1575 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1576}
1577
1578static inline void ept_sync_context(u64 eptp)
1579{
Avi Kivity089d0342009-03-23 18:26:32 +02001580 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001581 if (cpu_has_vmx_invept_context())
1582 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1583 else
1584 ept_sync_global();
1585 }
1586}
1587
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001588static __always_inline void vmcs_check16(unsigned long field)
1589{
1590 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1591 "16-bit accessor invalid for 64-bit field");
1592 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1593 "16-bit accessor invalid for 64-bit high field");
1594 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1595 "16-bit accessor invalid for 32-bit high field");
1596 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1597 "16-bit accessor invalid for natural width field");
1598}
1599
1600static __always_inline void vmcs_check32(unsigned long field)
1601{
1602 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1603 "32-bit accessor invalid for 16-bit field");
1604 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1605 "32-bit accessor invalid for natural width field");
1606}
1607
1608static __always_inline void vmcs_check64(unsigned long field)
1609{
1610 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1611 "64-bit accessor invalid for 16-bit field");
1612 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1613 "64-bit accessor invalid for 64-bit high field");
1614 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1615 "64-bit accessor invalid for 32-bit field");
1616 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1617 "64-bit accessor invalid for natural width field");
1618}
1619
1620static __always_inline void vmcs_checkl(unsigned long field)
1621{
1622 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1623 "Natural width accessor invalid for 16-bit field");
1624 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1625 "Natural width accessor invalid for 64-bit field");
1626 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1627 "Natural width accessor invalid for 64-bit high field");
1628 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1629 "Natural width accessor invalid for 32-bit field");
1630}
1631
1632static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633{
Avi Kivity5e520e62011-05-15 10:13:12 -04001634 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635
Avi Kivity5e520e62011-05-15 10:13:12 -04001636 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1637 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638 return value;
1639}
1640
Avi Kivity96304212011-05-15 10:13:13 -04001641static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001643 vmcs_check16(field);
1644 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645}
1646
Avi Kivity96304212011-05-15 10:13:13 -04001647static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001649 vmcs_check32(field);
1650 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651}
1652
Avi Kivity96304212011-05-15 10:13:13 -04001653static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001655 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001656#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001657 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001659 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660#endif
1661}
1662
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001663static __always_inline unsigned long vmcs_readl(unsigned long field)
1664{
1665 vmcs_checkl(field);
1666 return __vmcs_readl(field);
1667}
1668
Avi Kivitye52de1b2007-01-05 16:36:56 -08001669static noinline void vmwrite_error(unsigned long field, unsigned long value)
1670{
1671 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1672 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1673 dump_stack();
1674}
1675
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001676static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677{
1678 u8 error;
1679
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001680 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001681 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001682 if (unlikely(error))
1683 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684}
1685
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001686static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001688 vmcs_check16(field);
1689 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690}
1691
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001692static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001694 vmcs_check32(field);
1695 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696}
1697
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001698static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001700 vmcs_check64(field);
1701 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001702#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001704 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705#endif
1706}
1707
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001708static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001709{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001710 vmcs_checkl(field);
1711 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001712}
1713
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001714static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001715{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001716 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1717 "vmcs_clear_bits does not support 64-bit fields");
1718 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1719}
1720
1721static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1722{
1723 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1724 "vmcs_set_bits does not support 64-bit fields");
1725 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001726}
1727
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001728static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1729{
1730 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1731}
1732
Gleb Natapov2961e8762013-11-25 15:37:13 +02001733static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1734{
1735 vmcs_write32(VM_ENTRY_CONTROLS, val);
1736 vmx->vm_entry_controls_shadow = val;
1737}
1738
1739static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1740{
1741 if (vmx->vm_entry_controls_shadow != val)
1742 vm_entry_controls_init(vmx, val);
1743}
1744
1745static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1746{
1747 return vmx->vm_entry_controls_shadow;
1748}
1749
1750
1751static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1752{
1753 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1754}
1755
1756static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1757{
1758 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1759}
1760
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001761static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1762{
1763 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1764}
1765
Gleb Natapov2961e8762013-11-25 15:37:13 +02001766static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1767{
1768 vmcs_write32(VM_EXIT_CONTROLS, val);
1769 vmx->vm_exit_controls_shadow = val;
1770}
1771
1772static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1773{
1774 if (vmx->vm_exit_controls_shadow != val)
1775 vm_exit_controls_init(vmx, val);
1776}
1777
1778static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1779{
1780 return vmx->vm_exit_controls_shadow;
1781}
1782
1783
1784static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1785{
1786 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1787}
1788
1789static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1790{
1791 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1792}
1793
Avi Kivity2fb92db2011-04-27 19:42:18 +03001794static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1795{
1796 vmx->segment_cache.bitmask = 0;
1797}
1798
1799static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1800 unsigned field)
1801{
1802 bool ret;
1803 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1804
1805 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1806 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1807 vmx->segment_cache.bitmask = 0;
1808 }
1809 ret = vmx->segment_cache.bitmask & mask;
1810 vmx->segment_cache.bitmask |= mask;
1811 return ret;
1812}
1813
1814static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1815{
1816 u16 *p = &vmx->segment_cache.seg[seg].selector;
1817
1818 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1819 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1820 return *p;
1821}
1822
1823static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1824{
1825 ulong *p = &vmx->segment_cache.seg[seg].base;
1826
1827 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1828 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1829 return *p;
1830}
1831
1832static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1833{
1834 u32 *p = &vmx->segment_cache.seg[seg].limit;
1835
1836 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1837 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1838 return *p;
1839}
1840
1841static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1842{
1843 u32 *p = &vmx->segment_cache.seg[seg].ar;
1844
1845 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1846 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1847 return *p;
1848}
1849
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001850static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1851{
1852 u32 eb;
1853
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001854 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001855 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001856 if ((vcpu->guest_debug &
1857 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1858 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1859 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001860 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001861 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001862 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001863 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001864
1865 /* When we are running a nested L2 guest and L1 specified for it a
1866 * certain exception bitmap, we must trap the same exceptions and pass
1867 * them to L1. When running L2, we will only handle the exceptions
1868 * specified above if L1 did not want them.
1869 */
1870 if (is_guest_mode(vcpu))
1871 eb |= get_vmcs12(vcpu)->exception_bitmap;
1872
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001873 vmcs_write32(EXCEPTION_BITMAP, eb);
1874}
1875
Gleb Natapov2961e8762013-11-25 15:37:13 +02001876static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1877 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001878{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001879 vm_entry_controls_clearbit(vmx, entry);
1880 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001881}
1882
Avi Kivity61d2ef22010-04-28 16:40:38 +03001883static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1884{
1885 unsigned i;
1886 struct msr_autoload *m = &vmx->msr_autoload;
1887
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001888 switch (msr) {
1889 case MSR_EFER:
1890 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001891 clear_atomic_switch_msr_special(vmx,
1892 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001893 VM_EXIT_LOAD_IA32_EFER);
1894 return;
1895 }
1896 break;
1897 case MSR_CORE_PERF_GLOBAL_CTRL:
1898 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001899 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001900 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1901 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1902 return;
1903 }
1904 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001905 }
1906
Avi Kivity61d2ef22010-04-28 16:40:38 +03001907 for (i = 0; i < m->nr; ++i)
1908 if (m->guest[i].index == msr)
1909 break;
1910
1911 if (i == m->nr)
1912 return;
1913 --m->nr;
1914 m->guest[i] = m->guest[m->nr];
1915 m->host[i] = m->host[m->nr];
1916 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1917 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1918}
1919
Gleb Natapov2961e8762013-11-25 15:37:13 +02001920static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1921 unsigned long entry, unsigned long exit,
1922 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1923 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001924{
1925 vmcs_write64(guest_val_vmcs, guest_val);
1926 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001927 vm_entry_controls_setbit(vmx, entry);
1928 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001929}
1930
Avi Kivity61d2ef22010-04-28 16:40:38 +03001931static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1932 u64 guest_val, u64 host_val)
1933{
1934 unsigned i;
1935 struct msr_autoload *m = &vmx->msr_autoload;
1936
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001937 switch (msr) {
1938 case MSR_EFER:
1939 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001940 add_atomic_switch_msr_special(vmx,
1941 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001942 VM_EXIT_LOAD_IA32_EFER,
1943 GUEST_IA32_EFER,
1944 HOST_IA32_EFER,
1945 guest_val, host_val);
1946 return;
1947 }
1948 break;
1949 case MSR_CORE_PERF_GLOBAL_CTRL:
1950 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001951 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001952 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1953 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1954 GUEST_IA32_PERF_GLOBAL_CTRL,
1955 HOST_IA32_PERF_GLOBAL_CTRL,
1956 guest_val, host_val);
1957 return;
1958 }
1959 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001960 case MSR_IA32_PEBS_ENABLE:
1961 /* PEBS needs a quiescent period after being disabled (to write
1962 * a record). Disabling PEBS through VMX MSR swapping doesn't
1963 * provide that period, so a CPU could write host's record into
1964 * guest's memory.
1965 */
1966 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001967 }
1968
Avi Kivity61d2ef22010-04-28 16:40:38 +03001969 for (i = 0; i < m->nr; ++i)
1970 if (m->guest[i].index == msr)
1971 break;
1972
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001973 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001974 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001975 "Can't add msr %x\n", msr);
1976 return;
1977 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001978 ++m->nr;
1979 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1980 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1981 }
1982
1983 m->guest[i].index = msr;
1984 m->guest[i].value = guest_val;
1985 m->host[i].index = msr;
1986 m->host[i].value = host_val;
1987}
1988
Avi Kivity92c0d902009-10-29 11:00:16 +02001989static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001990{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001991 u64 guest_efer = vmx->vcpu.arch.efer;
1992 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03001993
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001994 if (!enable_ept) {
1995 /*
1996 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1997 * host CPUID is more efficient than testing guest CPUID
1998 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
1999 */
2000 if (boot_cpu_has(X86_FEATURE_SMEP))
2001 guest_efer |= EFER_NX;
2002 else if (!(guest_efer & EFER_NX))
2003 ignore_bits |= EFER_NX;
2004 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002005
Avi Kivity51c6cf62007-08-29 03:48:05 +03002006 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002007 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002008 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002009 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002010#ifdef CONFIG_X86_64
2011 ignore_bits |= EFER_LMA | EFER_LME;
2012 /* SCE is meaningful only in long mode on Intel */
2013 if (guest_efer & EFER_LMA)
2014 ignore_bits &= ~(u64)EFER_SCE;
2015#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002016
2017 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002018
2019 /*
2020 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2021 * On CPUs that support "load IA32_EFER", always switch EFER
2022 * atomically, since it's faster than switching it manually.
2023 */
2024 if (cpu_has_load_ia32_efer ||
2025 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002026 if (!(guest_efer & EFER_LMA))
2027 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002028 if (guest_efer != host_efer)
2029 add_atomic_switch_msr(vmx, MSR_EFER,
2030 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002031 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002032 } else {
2033 guest_efer &= ~ignore_bits;
2034 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002035
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002036 vmx->guest_msrs[efer_offset].data = guest_efer;
2037 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2038
2039 return true;
2040 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002041}
2042
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002043#ifdef CONFIG_X86_32
2044/*
2045 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2046 * VMCS rather than the segment table. KVM uses this helper to figure
2047 * out the current bases to poke them into the VMCS before entry.
2048 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002049static unsigned long segment_base(u16 selector)
2050{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002051 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002052 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002053 unsigned long v;
2054
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002055 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002056 return 0;
2057
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002058 table = (struct desc_struct *)gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002059
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002060 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002061 u16 ldt_selector = kvm_read_ldt();
2062
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002063 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002064 return 0;
2065
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002066 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002067 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002068 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002069 return v;
2070}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002071#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002072
Avi Kivity04d2cc72007-09-10 18:10:54 +03002073static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002074{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002075 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002076 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002077
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002078 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002079 return;
2080
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002081 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002082 /*
2083 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2084 * allow segment selectors with cpl > 0 or ti == 1.
2085 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002086 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002087 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002088 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002089 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002090 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002091 vmx->host_state.fs_reload_needed = 0;
2092 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002093 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002094 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002095 }
Avi Kivity9581d442010-10-19 16:46:55 +02002096 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002097 if (!(vmx->host_state.gs_sel & 7))
2098 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002099 else {
2100 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002101 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002102 }
2103
2104#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002105 savesegment(ds, vmx->host_state.ds_sel);
2106 savesegment(es, vmx->host_state.es_sel);
2107#endif
2108
2109#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002110 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2111 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2112#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002113 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2114 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002115#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002116
2117#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002118 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2119 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002120 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002121#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002122 if (boot_cpu_has(X86_FEATURE_MPX))
2123 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002124 for (i = 0; i < vmx->save_nmsrs; ++i)
2125 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002126 vmx->guest_msrs[i].data,
2127 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002128}
2129
Avi Kivitya9b21b62008-06-24 11:48:49 +03002130static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002131{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002132 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002133 return;
2134
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002135 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002136 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002137#ifdef CONFIG_X86_64
2138 if (is_long_mode(&vmx->vcpu))
2139 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2140#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002141 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002142 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002143#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002144 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002145#else
2146 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002147#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002148 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002149 if (vmx->host_state.fs_reload_needed)
2150 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002151#ifdef CONFIG_X86_64
2152 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2153 loadsegment(ds, vmx->host_state.ds_sel);
2154 loadsegment(es, vmx->host_state.es_sel);
2155 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002156#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002157 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002158#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002159 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002160#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002161 if (vmx->host_state.msr_host_bndcfgs)
2162 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Christoph Lameter89cbc762014-08-17 12:30:40 -05002163 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002164}
2165
Avi Kivitya9b21b62008-06-24 11:48:49 +03002166static void vmx_load_host_state(struct vcpu_vmx *vmx)
2167{
2168 preempt_disable();
2169 __vmx_load_host_state(vmx);
2170 preempt_enable();
2171}
2172
Feng Wu28b835d2015-09-18 22:29:54 +08002173static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2174{
2175 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2176 struct pi_desc old, new;
2177 unsigned int dest;
2178
2179 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002180 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2181 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002182 return;
2183
2184 do {
2185 old.control = new.control = pi_desc->control;
2186
2187 /*
2188 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
2189 * are two possible cases:
2190 * 1. After running 'pre_block', context switch
2191 * happened. For this case, 'sn' was set in
2192 * vmx_vcpu_put(), so we need to clear it here.
2193 * 2. After running 'pre_block', we were blocked,
2194 * and woken up by some other guy. For this case,
2195 * we don't need to do anything, 'pi_post_block'
2196 * will do everything for us. However, we cannot
2197 * check whether it is case #1 or case #2 here
2198 * (maybe, not needed), so we also clear sn here,
2199 * I think it is not a big deal.
2200 */
2201 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
2202 if (vcpu->cpu != cpu) {
2203 dest = cpu_physical_id(cpu);
2204
2205 if (x2apic_enabled())
2206 new.ndst = dest;
2207 else
2208 new.ndst = (dest << 8) & 0xFF00;
2209 }
2210
2211 /* set 'NV' to 'notification vector' */
2212 new.nv = POSTED_INTR_VECTOR;
2213 }
2214
2215 /* Allow posting non-urgent interrupts */
2216 new.sn = 0;
2217 } while (cmpxchg(&pi_desc->control, old.control,
2218 new.control) != old.control);
2219}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002220
Peter Feinerc95ba922016-08-17 09:36:47 -07002221static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2222{
2223 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2224 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2225}
2226
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227/*
2228 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2229 * vcpu mutex is already taken.
2230 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002231static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002233 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002234 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002235 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002237 if (!vmm_exclusive)
2238 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002239 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002240 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002242 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002243 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002244 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002245
2246 /*
2247 * Read loaded_vmcs->cpu should be before fetching
2248 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2249 * See the comments in __loaded_vmcs_clear().
2250 */
2251 smp_rmb();
2252
Nadav Har'Eld462b812011-05-24 15:26:10 +03002253 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2254 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002255 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002256 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002257 }
2258
2259 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2260 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2261 vmcs_load(vmx->loaded_vmcs->vmcs);
2262 }
2263
2264 if (!already_loaded) {
2265 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2266 unsigned long sysenter_esp;
2267
2268 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002269
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 /*
2271 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002272 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002274 vmcs_writel(HOST_TR_BASE,
2275 (unsigned long)this_cpu_ptr(&cpu_tss));
2276 vmcs_writel(HOST_GDTR_BASE, gdt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002277
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002278 /*
2279 * VM exits change the host TR limit to 0x67 after a VM
2280 * exit. This is okay, since 0x67 covers everything except
2281 * the IO bitmap and have have code to handle the IO bitmap
2282 * being lost after a VM exit.
2283 */
2284 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2285
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2287 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002288
Nadav Har'Eld462b812011-05-24 15:26:10 +03002289 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290 }
Feng Wu28b835d2015-09-18 22:29:54 +08002291
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002292 /* Setup TSC multiplier */
2293 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002294 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2295 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002296
Feng Wu28b835d2015-09-18 22:29:54 +08002297 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002298 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002299}
2300
2301static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2302{
2303 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2304
2305 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002306 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2307 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002308 return;
2309
2310 /* Set SN when the vCPU is preempted */
2311 if (vcpu->preempted)
2312 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313}
2314
2315static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2316{
Feng Wu28b835d2015-09-18 22:29:54 +08002317 vmx_vcpu_pi_put(vcpu);
2318
Avi Kivitya9b21b62008-06-24 11:48:49 +03002319 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002320 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002321 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2322 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002323 kvm_cpu_vmxoff();
2324 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325}
2326
Avi Kivityedcafe32009-12-30 18:07:40 +02002327static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2328
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002329/*
2330 * Return the cr0 value that a nested guest would read. This is a combination
2331 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2332 * its hypervisor (cr0_read_shadow).
2333 */
2334static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2335{
2336 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2337 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2338}
2339static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2340{
2341 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2342 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2343}
2344
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2346{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002347 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002348
Avi Kivity6de12732011-03-07 12:51:22 +02002349 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2350 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2351 rflags = vmcs_readl(GUEST_RFLAGS);
2352 if (to_vmx(vcpu)->rmode.vm86_active) {
2353 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2354 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2355 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2356 }
2357 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002358 }
Avi Kivity6de12732011-03-07 12:51:22 +02002359 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360}
2361
2362static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2363{
Avi Kivity6de12732011-03-07 12:51:22 +02002364 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2365 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002366 if (to_vmx(vcpu)->rmode.vm86_active) {
2367 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002368 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002369 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370 vmcs_writel(GUEST_RFLAGS, rflags);
2371}
2372
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002373static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2374{
2375 return to_vmx(vcpu)->guest_pkru;
2376}
2377
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002378static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002379{
2380 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2381 int ret = 0;
2382
2383 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002384 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002385 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002386 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002387
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002388 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002389}
2390
2391static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2392{
2393 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2394 u32 interruptibility = interruptibility_old;
2395
2396 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2397
Jan Kiszka48005f62010-02-19 19:38:07 +01002398 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002399 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002400 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002401 interruptibility |= GUEST_INTR_STATE_STI;
2402
2403 if ((interruptibility != interruptibility_old))
2404 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2405}
2406
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2408{
2409 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002411 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002412 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002413 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414
Glauber Costa2809f5d2009-05-12 16:21:05 -04002415 /* skipping an emulated instruction also counts */
2416 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417}
2418
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002419/*
2420 * KVM wants to inject page-faults which it got to the guest. This function
2421 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002422 */
Gleb Natapove011c662013-09-25 12:51:35 +03002423static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002424{
2425 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2426
Gleb Natapove011c662013-09-25 12:51:35 +03002427 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002428 return 0;
2429
Jan Kiszka533558b2014-01-04 18:47:20 +01002430 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2431 vmcs_read32(VM_EXIT_INTR_INFO),
2432 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002433 return 1;
2434}
2435
Avi Kivity298101d2007-11-25 13:41:11 +02002436static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002437 bool has_error_code, u32 error_code,
2438 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002439{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002441 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002442
Gleb Natapove011c662013-09-25 12:51:35 +03002443 if (!reinject && is_guest_mode(vcpu) &&
2444 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002445 return;
2446
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002447 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002448 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002449 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2450 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002451
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002452 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002453 int inc_eip = 0;
2454 if (kvm_exception_is_soft(nr))
2455 inc_eip = vcpu->arch.event_exit_inst_len;
2456 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002457 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002458 return;
2459 }
2460
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002461 if (kvm_exception_is_soft(nr)) {
2462 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2463 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002464 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2465 } else
2466 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2467
2468 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002469}
2470
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002471static bool vmx_rdtscp_supported(void)
2472{
2473 return cpu_has_vmx_rdtscp();
2474}
2475
Mao, Junjiead756a12012-07-02 01:18:48 +00002476static bool vmx_invpcid_supported(void)
2477{
2478 return cpu_has_vmx_invpcid() && enable_ept;
2479}
2480
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481/*
Eddie Donga75beee2007-05-17 18:55:15 +03002482 * Swap MSR entry in host/guest MSR entry array.
2483 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002484static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002485{
Avi Kivity26bb0982009-09-07 11:14:12 +03002486 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002487
2488 tmp = vmx->guest_msrs[to];
2489 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2490 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002491}
2492
Yang Zhang8d146952013-01-25 10:18:50 +08002493static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2494{
2495 unsigned long *msr_bitmap;
2496
Wincy Van670125b2015-03-04 14:31:56 +08002497 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002498 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002499 else if (cpu_has_secondary_exec_ctrls() &&
2500 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2501 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002502 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2503 if (is_long_mode(vcpu))
Wanpeng Lic63e4562016-09-23 19:17:16 +08002504 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2505 else
2506 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2507 } else {
2508 if (is_long_mode(vcpu))
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002509 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2510 else
2511 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002512 }
Yang Zhang8d146952013-01-25 10:18:50 +08002513 } else {
2514 if (is_long_mode(vcpu))
2515 msr_bitmap = vmx_msr_bitmap_longmode;
2516 else
2517 msr_bitmap = vmx_msr_bitmap_legacy;
2518 }
2519
2520 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2521}
2522
Eddie Donga75beee2007-05-17 18:55:15 +03002523/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002524 * Set up the vmcs to automatically save and restore system
2525 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2526 * mode, as fiddling with msrs is very expensive.
2527 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002528static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002529{
Avi Kivity26bb0982009-09-07 11:14:12 +03002530 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002531
Eddie Donga75beee2007-05-17 18:55:15 +03002532 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002533#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002534 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002535 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002536 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002537 move_msr_up(vmx, index, save_nmsrs++);
2538 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002539 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002540 move_msr_up(vmx, index, save_nmsrs++);
2541 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002542 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002543 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002544 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002545 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002546 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002547 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002548 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002549 * if efer.sce is enabled.
2550 */
Brian Gerst8c065852010-07-17 09:03:26 -04002551 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002552 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002553 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002554 }
Eddie Donga75beee2007-05-17 18:55:15 +03002555#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002556 index = __find_msr_index(vmx, MSR_EFER);
2557 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002558 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002559
Avi Kivity26bb0982009-09-07 11:14:12 +03002560 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002561
Yang Zhang8d146952013-01-25 10:18:50 +08002562 if (cpu_has_vmx_msr_bitmap())
2563 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002564}
2565
2566/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002568 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2569 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002571static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572{
2573 u64 host_tsc, tsc_offset;
2574
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002575 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002577 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578}
2579
2580/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002581 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002583static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002585 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002586 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002587 * We're here if L1 chose not to trap WRMSR to TSC. According
2588 * to the spec, this should set L1's TSC; The offset that L1
2589 * set for L2 remains unchanged, and still needs to be added
2590 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002591 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002592 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002593 /* recalculate vmcs02.TSC_OFFSET: */
2594 vmcs12 = get_vmcs12(vcpu);
2595 vmcs_write64(TSC_OFFSET, offset +
2596 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2597 vmcs12->tsc_offset : 0));
2598 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002599 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2600 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002601 vmcs_write64(TSC_OFFSET, offset);
2602 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603}
2604
Nadav Har'El801d3422011-05-25 23:02:23 +03002605static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2606{
2607 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2608 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2609}
2610
2611/*
2612 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2613 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2614 * all guests if the "nested" module option is off, and can also be disabled
2615 * for a single guest by disabling its VMX cpuid bit.
2616 */
2617static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2618{
2619 return nested && guest_cpuid_has_vmx(vcpu);
2620}
2621
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002623 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2624 * returned for the various VMX controls MSRs when nested VMX is enabled.
2625 * The same values should also be used to verify that vmcs12 control fields are
2626 * valid during nested entry from L1 to L2.
2627 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2628 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2629 * bit in the high half is on if the corresponding bit in the control field
2630 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002631 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002632static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002633{
2634 /*
2635 * Note that as a general rule, the high half of the MSRs (bits in
2636 * the control fields which may be 1) should be initialized by the
2637 * intersection of the underlying hardware's MSR (i.e., features which
2638 * can be supported) and the list of features we want to expose -
2639 * because they are known to be properly supported in our code.
2640 * Also, usually, the low half of the MSRs (bits which must be 1) can
2641 * be set to 0, meaning that L1 may turn off any of these bits. The
2642 * reason is that if one of these bits is necessary, it will appear
2643 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2644 * fields of vmcs01 and vmcs02, will turn these bits off - and
2645 * nested_vmx_exit_handled() will not pass related exits to L1.
2646 * These rules have exceptions below.
2647 */
2648
2649 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002650 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002651 vmx->nested.nested_vmx_pinbased_ctls_low,
2652 vmx->nested.nested_vmx_pinbased_ctls_high);
2653 vmx->nested.nested_vmx_pinbased_ctls_low |=
2654 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2655 vmx->nested.nested_vmx_pinbased_ctls_high &=
2656 PIN_BASED_EXT_INTR_MASK |
2657 PIN_BASED_NMI_EXITING |
2658 PIN_BASED_VIRTUAL_NMIS;
2659 vmx->nested.nested_vmx_pinbased_ctls_high |=
2660 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002661 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002662 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002663 vmx->nested.nested_vmx_pinbased_ctls_high |=
2664 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002665
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002666 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002667 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002668 vmx->nested.nested_vmx_exit_ctls_low,
2669 vmx->nested.nested_vmx_exit_ctls_high);
2670 vmx->nested.nested_vmx_exit_ctls_low =
2671 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002672
Wincy Vanb9c237b2015-02-03 23:56:30 +08002673 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002674#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002675 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002676#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002677 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002678 vmx->nested.nested_vmx_exit_ctls_high |=
2679 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002680 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002681 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2682
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002683 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002684 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002685
Jan Kiszka2996fca2014-06-16 13:59:43 +02002686 /* We support free control of debug control saving. */
David Matlack0115f9c2016-11-29 18:14:06 -08002687 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002688
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002689 /* entry controls */
2690 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002691 vmx->nested.nested_vmx_entry_ctls_low,
2692 vmx->nested.nested_vmx_entry_ctls_high);
2693 vmx->nested.nested_vmx_entry_ctls_low =
2694 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2695 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002696#ifdef CONFIG_X86_64
2697 VM_ENTRY_IA32E_MODE |
2698#endif
2699 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002700 vmx->nested.nested_vmx_entry_ctls_high |=
2701 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002702 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002703 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002704
Jan Kiszka2996fca2014-06-16 13:59:43 +02002705 /* We support free control of debug control loading. */
David Matlack0115f9c2016-11-29 18:14:06 -08002706 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002707
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002708 /* cpu-based controls */
2709 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002710 vmx->nested.nested_vmx_procbased_ctls_low,
2711 vmx->nested.nested_vmx_procbased_ctls_high);
2712 vmx->nested.nested_vmx_procbased_ctls_low =
2713 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2714 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002715 CPU_BASED_VIRTUAL_INTR_PENDING |
2716 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002717 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2718 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2719 CPU_BASED_CR3_STORE_EXITING |
2720#ifdef CONFIG_X86_64
2721 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2722#endif
2723 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002724 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2725 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2726 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2727 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002728 /*
2729 * We can allow some features even when not supported by the
2730 * hardware. For example, L1 can specify an MSR bitmap - and we
2731 * can use it to avoid exits to L1 - even when L0 runs L2
2732 * without MSR bitmaps.
2733 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002734 vmx->nested.nested_vmx_procbased_ctls_high |=
2735 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002736 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002737
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002738 /* We support free control of CR3 access interception. */
David Matlack0115f9c2016-11-29 18:14:06 -08002739 vmx->nested.nested_vmx_procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002740 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2741
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002742 /* secondary cpu-based controls */
2743 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002744 vmx->nested.nested_vmx_secondary_ctls_low,
2745 vmx->nested.nested_vmx_secondary_ctls_high);
2746 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2747 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002748 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002749 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002750 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002751 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002752 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002753 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002754 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002755 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002756
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002757 if (enable_ept) {
2758 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002759 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002760 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002761 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002762 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002763 if (cpu_has_vmx_ept_execute_only())
2764 vmx->nested.nested_vmx_ept_caps |=
2765 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002766 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002767 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002768 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2769 VMX_EPT_1GB_PAGE_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002770 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002771 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002772
Paolo Bonzinief697a72016-03-18 16:58:38 +01002773 /*
2774 * Old versions of KVM use the single-context version without
2775 * checking for support, so declare that it is supported even
2776 * though it is treated as global context. The alternative is
2777 * not failing the single-context invvpid, and it is worse.
2778 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002779 if (enable_vpid) {
2780 vmx->nested.nested_vmx_secondary_ctls_high |=
2781 SECONDARY_EXEC_ENABLE_VPID;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002782 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002783 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002784 } else
Wanpeng Li089d7b62015-10-13 09:18:37 -07002785 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002786
Radim Krčmář0790ec12015-03-17 14:02:32 +01002787 if (enable_unrestricted_guest)
2788 vmx->nested.nested_vmx_secondary_ctls_high |=
2789 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2790
Jan Kiszkac18911a2013-03-13 16:06:41 +01002791 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002792 rdmsr(MSR_IA32_VMX_MISC,
2793 vmx->nested.nested_vmx_misc_low,
2794 vmx->nested.nested_vmx_misc_high);
2795 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2796 vmx->nested.nested_vmx_misc_low |=
2797 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002798 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002799 vmx->nested.nested_vmx_misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002800
2801 /*
2802 * This MSR reports some information about VMX support. We
2803 * should return information about the VMX we emulate for the
2804 * guest, and the VMCS structure we give it - not about the
2805 * VMX support of the underlying hardware.
2806 */
2807 vmx->nested.nested_vmx_basic =
2808 VMCS12_REVISION |
2809 VMX_BASIC_TRUE_CTLS |
2810 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2811 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2812
2813 if (cpu_has_vmx_basic_inout())
2814 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2815
2816 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002817 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002818 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2819 * We picked the standard core2 setting.
2820 */
2821#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2822#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2823 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002824 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002825
2826 /* These MSRs specify bits which the guest must keep fixed off. */
2827 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2828 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002829
2830 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
2831 vmx->nested.nested_vmx_vmcs_enum = 0x2e;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002832}
2833
David Matlack38991522016-11-29 18:14:08 -08002834/*
2835 * if fixed0[i] == 1: val[i] must be 1
2836 * if fixed1[i] == 0: val[i] must be 0
2837 */
2838static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2839{
2840 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002841}
2842
2843static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2844{
David Matlack38991522016-11-29 18:14:08 -08002845 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002846}
2847
2848static inline u64 vmx_control_msr(u32 low, u32 high)
2849{
2850 return low | ((u64)high << 32);
2851}
2852
David Matlack62cc6b9d2016-11-29 18:14:07 -08002853static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2854{
2855 superset &= mask;
2856 subset &= mask;
2857
2858 return (superset | subset) == superset;
2859}
2860
2861static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2862{
2863 const u64 feature_and_reserved =
2864 /* feature (except bit 48; see below) */
2865 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2866 /* reserved */
2867 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2868 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2869
2870 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2871 return -EINVAL;
2872
2873 /*
2874 * KVM does not emulate a version of VMX that constrains physical
2875 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2876 */
2877 if (data & BIT_ULL(48))
2878 return -EINVAL;
2879
2880 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2881 vmx_basic_vmcs_revision_id(data))
2882 return -EINVAL;
2883
2884 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2885 return -EINVAL;
2886
2887 vmx->nested.nested_vmx_basic = data;
2888 return 0;
2889}
2890
2891static int
2892vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2893{
2894 u64 supported;
2895 u32 *lowp, *highp;
2896
2897 switch (msr_index) {
2898 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2899 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
2900 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
2901 break;
2902 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2903 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
2904 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
2905 break;
2906 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2907 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
2908 highp = &vmx->nested.nested_vmx_exit_ctls_high;
2909 break;
2910 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2911 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
2912 highp = &vmx->nested.nested_vmx_entry_ctls_high;
2913 break;
2914 case MSR_IA32_VMX_PROCBASED_CTLS2:
2915 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
2916 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
2917 break;
2918 default:
2919 BUG();
2920 }
2921
2922 supported = vmx_control_msr(*lowp, *highp);
2923
2924 /* Check must-be-1 bits are still 1. */
2925 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
2926 return -EINVAL;
2927
2928 /* Check must-be-0 bits are still 0. */
2929 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
2930 return -EINVAL;
2931
2932 *lowp = data;
2933 *highp = data >> 32;
2934 return 0;
2935}
2936
2937static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
2938{
2939 const u64 feature_and_reserved_bits =
2940 /* feature */
2941 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
2942 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
2943 /* reserved */
2944 GENMASK_ULL(13, 9) | BIT_ULL(31);
2945 u64 vmx_misc;
2946
2947 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
2948 vmx->nested.nested_vmx_misc_high);
2949
2950 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
2951 return -EINVAL;
2952
2953 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
2954 PIN_BASED_VMX_PREEMPTION_TIMER) &&
2955 vmx_misc_preemption_timer_rate(data) !=
2956 vmx_misc_preemption_timer_rate(vmx_misc))
2957 return -EINVAL;
2958
2959 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
2960 return -EINVAL;
2961
2962 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
2963 return -EINVAL;
2964
2965 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
2966 return -EINVAL;
2967
2968 vmx->nested.nested_vmx_misc_low = data;
2969 vmx->nested.nested_vmx_misc_high = data >> 32;
2970 return 0;
2971}
2972
2973static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
2974{
2975 u64 vmx_ept_vpid_cap;
2976
2977 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
2978 vmx->nested.nested_vmx_vpid_caps);
2979
2980 /* Every bit is either reserved or a feature bit. */
2981 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
2982 return -EINVAL;
2983
2984 vmx->nested.nested_vmx_ept_caps = data;
2985 vmx->nested.nested_vmx_vpid_caps = data >> 32;
2986 return 0;
2987}
2988
2989static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2990{
2991 u64 *msr;
2992
2993 switch (msr_index) {
2994 case MSR_IA32_VMX_CR0_FIXED0:
2995 msr = &vmx->nested.nested_vmx_cr0_fixed0;
2996 break;
2997 case MSR_IA32_VMX_CR4_FIXED0:
2998 msr = &vmx->nested.nested_vmx_cr4_fixed0;
2999 break;
3000 default:
3001 BUG();
3002 }
3003
3004 /*
3005 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3006 * must be 1 in the restored value.
3007 */
3008 if (!is_bitwise_subset(data, *msr, -1ULL))
3009 return -EINVAL;
3010
3011 *msr = data;
3012 return 0;
3013}
3014
3015/*
3016 * Called when userspace is restoring VMX MSRs.
3017 *
3018 * Returns 0 on success, non-0 otherwise.
3019 */
3020static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3021{
3022 struct vcpu_vmx *vmx = to_vmx(vcpu);
3023
3024 switch (msr_index) {
3025 case MSR_IA32_VMX_BASIC:
3026 return vmx_restore_vmx_basic(vmx, data);
3027 case MSR_IA32_VMX_PINBASED_CTLS:
3028 case MSR_IA32_VMX_PROCBASED_CTLS:
3029 case MSR_IA32_VMX_EXIT_CTLS:
3030 case MSR_IA32_VMX_ENTRY_CTLS:
3031 /*
3032 * The "non-true" VMX capability MSRs are generated from the
3033 * "true" MSRs, so we do not support restoring them directly.
3034 *
3035 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3036 * should restore the "true" MSRs with the must-be-1 bits
3037 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3038 * DEFAULT SETTINGS".
3039 */
3040 return -EINVAL;
3041 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3042 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3043 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3044 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3045 case MSR_IA32_VMX_PROCBASED_CTLS2:
3046 return vmx_restore_control_msr(vmx, msr_index, data);
3047 case MSR_IA32_VMX_MISC:
3048 return vmx_restore_vmx_misc(vmx, data);
3049 case MSR_IA32_VMX_CR0_FIXED0:
3050 case MSR_IA32_VMX_CR4_FIXED0:
3051 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3052 case MSR_IA32_VMX_CR0_FIXED1:
3053 case MSR_IA32_VMX_CR4_FIXED1:
3054 /*
3055 * These MSRs are generated based on the vCPU's CPUID, so we
3056 * do not support restoring them directly.
3057 */
3058 return -EINVAL;
3059 case MSR_IA32_VMX_EPT_VPID_CAP:
3060 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3061 case MSR_IA32_VMX_VMCS_ENUM:
3062 vmx->nested.nested_vmx_vmcs_enum = data;
3063 return 0;
3064 default:
3065 /*
3066 * The rest of the VMX capability MSRs do not support restore.
3067 */
3068 return -EINVAL;
3069 }
3070}
3071
Jan Kiszkacae50132014-01-04 18:47:22 +01003072/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003073static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3074{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003075 struct vcpu_vmx *vmx = to_vmx(vcpu);
3076
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003077 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003078 case MSR_IA32_VMX_BASIC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003079 *pdata = vmx->nested.nested_vmx_basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003080 break;
3081 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3082 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003083 *pdata = vmx_control_msr(
3084 vmx->nested.nested_vmx_pinbased_ctls_low,
3085 vmx->nested.nested_vmx_pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003086 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3087 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003088 break;
3089 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3090 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003091 *pdata = vmx_control_msr(
3092 vmx->nested.nested_vmx_procbased_ctls_low,
3093 vmx->nested.nested_vmx_procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003094 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3095 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003096 break;
3097 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3098 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003099 *pdata = vmx_control_msr(
3100 vmx->nested.nested_vmx_exit_ctls_low,
3101 vmx->nested.nested_vmx_exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003102 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3103 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003104 break;
3105 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3106 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003107 *pdata = vmx_control_msr(
3108 vmx->nested.nested_vmx_entry_ctls_low,
3109 vmx->nested.nested_vmx_entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003110 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3111 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003112 break;
3113 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003114 *pdata = vmx_control_msr(
3115 vmx->nested.nested_vmx_misc_low,
3116 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003117 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003118 case MSR_IA32_VMX_CR0_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003119 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003120 break;
3121 case MSR_IA32_VMX_CR0_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003122 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003123 break;
3124 case MSR_IA32_VMX_CR4_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003125 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003126 break;
3127 case MSR_IA32_VMX_CR4_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003128 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003129 break;
3130 case MSR_IA32_VMX_VMCS_ENUM:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003131 *pdata = vmx->nested.nested_vmx_vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003132 break;
3133 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003134 *pdata = vmx_control_msr(
3135 vmx->nested.nested_vmx_secondary_ctls_low,
3136 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003137 break;
3138 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003139 *pdata = vmx->nested.nested_vmx_ept_caps |
3140 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003141 break;
3142 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003143 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003144 }
3145
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003146 return 0;
3147}
3148
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003149static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3150 uint64_t val)
3151{
3152 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3153
3154 return !(val & ~valid_bits);
3155}
3156
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003157/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 * Reads an msr value (of 'msr_index') into 'pdata'.
3159 * Returns 0 on success, non-0 otherwise.
3160 * Assumes vcpu_load() was already called.
3161 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003162static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163{
Avi Kivity26bb0982009-09-07 11:14:12 +03003164 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003166 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003167#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003169 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 break;
3171 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003172 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003174 case MSR_KERNEL_GS_BASE:
3175 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003176 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003177 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003178#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003180 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303181 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003182 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183 break;
3184 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003185 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186 break;
3187 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003188 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 break;
3190 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003191 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003193 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003194 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003195 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003196 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003197 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003198 case MSR_IA32_MCG_EXT_CTL:
3199 if (!msr_info->host_initiated &&
3200 !(to_vmx(vcpu)->msr_ia32_feature_control &
3201 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003202 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003203 msr_info->data = vcpu->arch.mcg_ext_ctl;
3204 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003205 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003206 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003207 break;
3208 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3209 if (!nested_vmx_allowed(vcpu))
3210 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003211 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003212 case MSR_IA32_XSS:
3213 if (!vmx_xsaves_supported())
3214 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003215 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003216 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003217 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003218 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003219 return 1;
3220 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003222 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003223 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003224 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003225 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003227 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 }
3229
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230 return 0;
3231}
3232
Jan Kiszkacae50132014-01-04 18:47:22 +01003233static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3234
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235/*
3236 * Writes msr value into into the appropriate "register".
3237 * Returns 0 on success, non-0 otherwise.
3238 * Assumes vcpu_load() was already called.
3239 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003240static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003242 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003243 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003244 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003245 u32 msr_index = msr_info->index;
3246 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003247
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003249 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003250 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003251 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003252#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003254 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255 vmcs_writel(GUEST_FS_BASE, data);
3256 break;
3257 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003258 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259 vmcs_writel(GUEST_GS_BASE, data);
3260 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003261 case MSR_KERNEL_GS_BASE:
3262 vmx_load_host_state(vmx);
3263 vmx->msr_guest_kernel_gs_base = data;
3264 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003265#endif
3266 case MSR_IA32_SYSENTER_CS:
3267 vmcs_write32(GUEST_SYSENTER_CS, data);
3268 break;
3269 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003270 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 break;
3272 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003273 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003275 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003276 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003277 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003278 vmcs_write64(GUEST_BNDCFGS, data);
3279 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303280 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003281 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003283 case MSR_IA32_CR_PAT:
3284 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003285 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3286 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003287 vmcs_write64(GUEST_IA32_PAT, data);
3288 vcpu->arch.pat = data;
3289 break;
3290 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003291 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003292 break;
Will Auldba904632012-11-29 12:42:50 -08003293 case MSR_IA32_TSC_ADJUST:
3294 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003295 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003296 case MSR_IA32_MCG_EXT_CTL:
3297 if ((!msr_info->host_initiated &&
3298 !(to_vmx(vcpu)->msr_ia32_feature_control &
3299 FEATURE_CONTROL_LMCE)) ||
3300 (data & ~MCG_EXT_CTL_LMCE_EN))
3301 return 1;
3302 vcpu->arch.mcg_ext_ctl = data;
3303 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003304 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003305 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003306 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003307 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3308 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003309 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003310 if (msr_info->host_initiated && data == 0)
3311 vmx_leave_nested(vcpu);
3312 break;
3313 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003314 if (!msr_info->host_initiated)
3315 return 1; /* they are read-only */
3316 if (!nested_vmx_allowed(vcpu))
3317 return 1;
3318 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003319 case MSR_IA32_XSS:
3320 if (!vmx_xsaves_supported())
3321 return 1;
3322 /*
3323 * The only supported bit as of Skylake is bit 8, but
3324 * it is not supported on KVM.
3325 */
3326 if (data != 0)
3327 return 1;
3328 vcpu->arch.ia32_xss = data;
3329 if (vcpu->arch.ia32_xss != host_xss)
3330 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3331 vcpu->arch.ia32_xss, host_xss);
3332 else
3333 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3334 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003335 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003336 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003337 return 1;
3338 /* Check reserved bit, higher 32 bits should be zero */
3339 if ((data >> 32) != 0)
3340 return 1;
3341 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003343 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003344 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003345 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003346 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003347 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3348 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003349 ret = kvm_set_shared_msr(msr->index, msr->data,
3350 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003351 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003352 if (ret)
3353 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003354 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003355 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003357 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003358 }
3359
Eddie Dong2cc51562007-05-21 07:28:09 +03003360 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361}
3362
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003363static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003365 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3366 switch (reg) {
3367 case VCPU_REGS_RSP:
3368 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3369 break;
3370 case VCPU_REGS_RIP:
3371 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3372 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003373 case VCPU_EXREG_PDPTR:
3374 if (enable_ept)
3375 ept_save_pdptrs(vcpu);
3376 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003377 default:
3378 break;
3379 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380}
3381
Avi Kivity6aa8b732006-12-10 02:21:36 -08003382static __init int cpu_has_kvm_support(void)
3383{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003384 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385}
3386
3387static __init int vmx_disabled_by_bios(void)
3388{
3389 u64 msr;
3390
3391 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003392 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003393 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003394 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3395 && tboot_enabled())
3396 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003397 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003398 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003399 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003400 && !tboot_enabled()) {
3401 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003402 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003403 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003404 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003405 /* launched w/o TXT and VMX disabled */
3406 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3407 && !tboot_enabled())
3408 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003409 }
3410
3411 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412}
3413
Dongxiao Xu7725b892010-05-11 18:29:38 +08003414static void kvm_cpu_vmxon(u64 addr)
3415{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003416 intel_pt_handle_vmx(1);
3417
Dongxiao Xu7725b892010-05-11 18:29:38 +08003418 asm volatile (ASM_VMX_VMXON_RAX
3419 : : "a"(&addr), "m"(addr)
3420 : "memory", "cc");
3421}
3422
Radim Krčmář13a34e02014-08-28 15:13:03 +02003423static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424{
3425 int cpu = raw_smp_processor_id();
3426 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003427 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003429 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003430 return -EBUSY;
3431
Nadav Har'Eld462b812011-05-24 15:26:10 +03003432 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003433 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3434 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003435
3436 /*
3437 * Now we can enable the vmclear operation in kdump
3438 * since the loaded_vmcss_on_cpu list on this cpu
3439 * has been initialized.
3440 *
3441 * Though the cpu is not in VMX operation now, there
3442 * is no problem to enable the vmclear operation
3443 * for the loaded_vmcss_on_cpu list is empty!
3444 */
3445 crash_enable_local_vmclear(cpu);
3446
Avi Kivity6aa8b732006-12-10 02:21:36 -08003447 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003448
3449 test_bits = FEATURE_CONTROL_LOCKED;
3450 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3451 if (tboot_enabled())
3452 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3453
3454 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003456 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3457 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003458 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003459
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003460 if (vmm_exclusive) {
3461 kvm_cpu_vmxon(phys_addr);
3462 ept_sync_global();
3463 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003464
Christoph Lameter89cbc762014-08-17 12:30:40 -05003465 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003466
Alexander Graf10474ae2009-09-15 11:37:46 +02003467 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003468}
3469
Nadav Har'Eld462b812011-05-24 15:26:10 +03003470static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003471{
3472 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003473 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003474
Nadav Har'Eld462b812011-05-24 15:26:10 +03003475 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3476 loaded_vmcss_on_cpu_link)
3477 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003478}
3479
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003480
3481/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3482 * tricks.
3483 */
3484static void kvm_cpu_vmxoff(void)
3485{
3486 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003487
3488 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003489}
3490
Radim Krčmář13a34e02014-08-28 15:13:03 +02003491static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003493 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003494 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003495 kvm_cpu_vmxoff();
3496 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003497 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498}
3499
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003500static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003501 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502{
3503 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003504 u32 ctl = ctl_min | ctl_opt;
3505
3506 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3507
3508 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3509 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3510
3511 /* Ensure minimum (required) set of control bits are supported. */
3512 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003513 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003514
3515 *result = ctl;
3516 return 0;
3517}
3518
Avi Kivity110312c2010-12-21 12:54:20 +02003519static __init bool allow_1_setting(u32 msr, u32 ctl)
3520{
3521 u32 vmx_msr_low, vmx_msr_high;
3522
3523 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3524 return vmx_msr_high & ctl;
3525}
3526
Yang, Sheng002c7f72007-07-31 14:23:01 +03003527static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003528{
3529 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003530 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003531 u32 _pin_based_exec_control = 0;
3532 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003533 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003534 u32 _vmexit_control = 0;
3535 u32 _vmentry_control = 0;
3536
Raghavendra K T10166742012-02-07 23:19:20 +05303537 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003538#ifdef CONFIG_X86_64
3539 CPU_BASED_CR8_LOAD_EXITING |
3540 CPU_BASED_CR8_STORE_EXITING |
3541#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003542 CPU_BASED_CR3_LOAD_EXITING |
3543 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003544 CPU_BASED_USE_IO_BITMAPS |
3545 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003546 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003547 CPU_BASED_MWAIT_EXITING |
3548 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003549 CPU_BASED_INVLPG_EXITING |
3550 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003551
Sheng Yangf78e0e22007-10-29 09:40:42 +08003552 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003553 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003554 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003555 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3556 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003557 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003558#ifdef CONFIG_X86_64
3559 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3560 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3561 ~CPU_BASED_CR8_STORE_EXITING;
3562#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003563 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003564 min2 = 0;
3565 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003566 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003567 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003568 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003569 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003570 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003571 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003572 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003573 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003574 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003575 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003576 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003577 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003578 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003579 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003580 if (adjust_vmx_controls(min2, opt2,
3581 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003582 &_cpu_based_2nd_exec_control) < 0)
3583 return -EIO;
3584 }
3585#ifndef CONFIG_X86_64
3586 if (!(_cpu_based_2nd_exec_control &
3587 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3588 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3589#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003590
3591 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3592 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003593 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003594 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3595 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003596
Sheng Yangd56f5462008-04-25 10:13:16 +08003597 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003598 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3599 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003600 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3601 CPU_BASED_CR3_STORE_EXITING |
3602 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003603 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3604 vmx_capability.ept, vmx_capability.vpid);
3605 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003606
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003607 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003608#ifdef CONFIG_X86_64
3609 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3610#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003611 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003612 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003613 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3614 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003615 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003616
Paolo Bonzini2c828782017-03-27 14:37:28 +02003617 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
3618 PIN_BASED_VIRTUAL_NMIS;
3619 opt = PIN_BASED_POSTED_INTR | PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003620 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3621 &_pin_based_exec_control) < 0)
3622 return -EIO;
3623
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003624 if (cpu_has_broken_vmx_preemption_timer())
3625 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003626 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003627 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003628 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3629
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003630 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003631 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003632 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3633 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003634 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003636 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003637
3638 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3639 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003640 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003641
3642#ifdef CONFIG_X86_64
3643 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3644 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003645 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003646#endif
3647
3648 /* Require Write-Back (WB) memory type for VMCS accesses. */
3649 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003650 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003651
Yang, Sheng002c7f72007-07-31 14:23:01 +03003652 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003653 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003654 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003655 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003656
Yang, Sheng002c7f72007-07-31 14:23:01 +03003657 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3658 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003659 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003660 vmcs_conf->vmexit_ctrl = _vmexit_control;
3661 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003662
Avi Kivity110312c2010-12-21 12:54:20 +02003663 cpu_has_load_ia32_efer =
3664 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3665 VM_ENTRY_LOAD_IA32_EFER)
3666 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3667 VM_EXIT_LOAD_IA32_EFER);
3668
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003669 cpu_has_load_perf_global_ctrl =
3670 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3671 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3672 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3673 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3674
3675 /*
3676 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003677 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003678 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3679 *
3680 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3681 *
3682 * AAK155 (model 26)
3683 * AAP115 (model 30)
3684 * AAT100 (model 37)
3685 * BC86,AAY89,BD102 (model 44)
3686 * BA97 (model 46)
3687 *
3688 */
3689 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3690 switch (boot_cpu_data.x86_model) {
3691 case 26:
3692 case 30:
3693 case 37:
3694 case 44:
3695 case 46:
3696 cpu_has_load_perf_global_ctrl = false;
3697 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3698 "does not work properly. Using workaround\n");
3699 break;
3700 default:
3701 break;
3702 }
3703 }
3704
Borislav Petkov782511b2016-04-04 22:25:03 +02003705 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003706 rdmsrl(MSR_IA32_XSS, host_xss);
3707
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003708 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003709}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710
3711static struct vmcs *alloc_vmcs_cpu(int cpu)
3712{
3713 int node = cpu_to_node(cpu);
3714 struct page *pages;
3715 struct vmcs *vmcs;
3716
Vlastimil Babka96db8002015-09-08 15:03:50 -07003717 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718 if (!pages)
3719 return NULL;
3720 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003721 memset(vmcs, 0, vmcs_config.size);
3722 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003723 return vmcs;
3724}
3725
3726static struct vmcs *alloc_vmcs(void)
3727{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003728 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003729}
3730
3731static void free_vmcs(struct vmcs *vmcs)
3732{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003733 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734}
3735
Nadav Har'Eld462b812011-05-24 15:26:10 +03003736/*
3737 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3738 */
3739static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3740{
3741 if (!loaded_vmcs->vmcs)
3742 return;
3743 loaded_vmcs_clear(loaded_vmcs);
3744 free_vmcs(loaded_vmcs->vmcs);
3745 loaded_vmcs->vmcs = NULL;
Jim Mattson355f4fb2016-10-28 08:29:39 -07003746 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003747}
3748
Sam Ravnborg39959582007-06-01 00:47:13 -07003749static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003750{
3751 int cpu;
3752
Zachary Amsden3230bb42009-09-29 11:38:37 -10003753 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003754 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003755 per_cpu(vmxarea, cpu) = NULL;
3756 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003757}
3758
Bandan Dasfe2b2012014-04-21 15:20:14 -04003759static void init_vmcs_shadow_fields(void)
3760{
3761 int i, j;
3762
3763 /* No checks for read only fields yet */
3764
3765 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3766 switch (shadow_read_write_fields[i]) {
3767 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003768 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003769 continue;
3770 break;
3771 default:
3772 break;
3773 }
3774
3775 if (j < i)
3776 shadow_read_write_fields[j] =
3777 shadow_read_write_fields[i];
3778 j++;
3779 }
3780 max_shadow_read_write_fields = j;
3781
3782 /* shadowed fields guest access without vmexit */
3783 for (i = 0; i < max_shadow_read_write_fields; i++) {
3784 clear_bit(shadow_read_write_fields[i],
3785 vmx_vmwrite_bitmap);
3786 clear_bit(shadow_read_write_fields[i],
3787 vmx_vmread_bitmap);
3788 }
3789 for (i = 0; i < max_shadow_read_only_fields; i++)
3790 clear_bit(shadow_read_only_fields[i],
3791 vmx_vmread_bitmap);
3792}
3793
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794static __init int alloc_kvm_area(void)
3795{
3796 int cpu;
3797
Zachary Amsden3230bb42009-09-29 11:38:37 -10003798 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799 struct vmcs *vmcs;
3800
3801 vmcs = alloc_vmcs_cpu(cpu);
3802 if (!vmcs) {
3803 free_kvm_area();
3804 return -ENOMEM;
3805 }
3806
3807 per_cpu(vmxarea, cpu) = vmcs;
3808 }
3809 return 0;
3810}
3811
Gleb Natapov14168782013-01-21 15:36:49 +02003812static bool emulation_required(struct kvm_vcpu *vcpu)
3813{
3814 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3815}
3816
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003817static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003818 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003820 if (!emulate_invalid_guest_state) {
3821 /*
3822 * CS and SS RPL should be equal during guest entry according
3823 * to VMX spec, but in reality it is not always so. Since vcpu
3824 * is in the middle of the transition from real mode to
3825 * protected mode it is safe to assume that RPL 0 is a good
3826 * default value.
3827 */
3828 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003829 save->selector &= ~SEGMENT_RPL_MASK;
3830 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003831 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003832 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003833 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834}
3835
3836static void enter_pmode(struct kvm_vcpu *vcpu)
3837{
3838 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003839 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003840
Gleb Natapovd99e4152012-12-20 16:57:45 +02003841 /*
3842 * Update real mode segment cache. It may be not up-to-date if sement
3843 * register was written while vcpu was in a guest mode.
3844 */
3845 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3846 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3847 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3848 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3849 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3850 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3851
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003852 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003853
Avi Kivity2fb92db2011-04-27 19:42:18 +03003854 vmx_segment_cache_clear(vmx);
3855
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003856 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003857
3858 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003859 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3860 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861 vmcs_writel(GUEST_RFLAGS, flags);
3862
Rusty Russell66aee912007-07-17 23:34:16 +10003863 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3864 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865
3866 update_exception_bitmap(vcpu);
3867
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003868 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3869 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3870 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3871 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3872 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3873 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003874}
3875
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003876static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877{
Mathias Krause772e0312012-08-30 01:30:19 +02003878 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003879 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880
Gleb Natapovd99e4152012-12-20 16:57:45 +02003881 var.dpl = 0x3;
3882 if (seg == VCPU_SREG_CS)
3883 var.type = 0x3;
3884
3885 if (!emulate_invalid_guest_state) {
3886 var.selector = var.base >> 4;
3887 var.base = var.base & 0xffff0;
3888 var.limit = 0xffff;
3889 var.g = 0;
3890 var.db = 0;
3891 var.present = 1;
3892 var.s = 1;
3893 var.l = 0;
3894 var.unusable = 0;
3895 var.type = 0x3;
3896 var.avl = 0;
3897 if (save->base & 0xf)
3898 printk_once(KERN_WARNING "kvm: segment base is not "
3899 "paragraph aligned when entering "
3900 "protected mode (seg=%d)", seg);
3901 }
3902
3903 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05003904 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003905 vmcs_write32(sf->limit, var.limit);
3906 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907}
3908
3909static void enter_rmode(struct kvm_vcpu *vcpu)
3910{
3911 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003912 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003913
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003914 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3915 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3916 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3917 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3918 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003919 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3920 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003921
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003922 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923
Gleb Natapov776e58e2011-03-13 12:34:27 +02003924 /*
3925 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003926 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003927 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003928 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003929 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3930 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003931
Avi Kivity2fb92db2011-04-27 19:42:18 +03003932 vmx_segment_cache_clear(vmx);
3933
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003934 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3937
3938 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003939 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003940
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003941 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942
3943 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003944 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003945 update_exception_bitmap(vcpu);
3946
Gleb Natapovd99e4152012-12-20 16:57:45 +02003947 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3948 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3949 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3950 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3951 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3952 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003953
Eddie Dong8668a3c2007-10-10 14:26:45 +08003954 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955}
3956
Amit Shah401d10d2009-02-20 22:53:37 +05303957static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3958{
3959 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003960 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3961
3962 if (!msr)
3963 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303964
Avi Kivity44ea2b12009-09-06 15:55:37 +03003965 /*
3966 * Force kernel_gs_base reloading before EFER changes, as control
3967 * of this msr depends on is_long_mode().
3968 */
3969 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003970 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303971 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003972 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303973 msr->data = efer;
3974 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003975 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303976
3977 msr->data = efer & ~EFER_LME;
3978 }
3979 setup_msrs(vmx);
3980}
3981
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003982#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003983
3984static void enter_lmode(struct kvm_vcpu *vcpu)
3985{
3986 u32 guest_tr_ar;
3987
Avi Kivity2fb92db2011-04-27 19:42:18 +03003988 vmx_segment_cache_clear(to_vmx(vcpu));
3989
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003991 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003992 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3993 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003995 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3996 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997 }
Avi Kivityda38f432010-07-06 11:30:49 +03003998 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999}
4000
4001static void exit_lmode(struct kvm_vcpu *vcpu)
4002{
Gleb Natapov2961e8762013-11-25 15:37:13 +02004003 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03004004 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004005}
4006
4007#endif
4008
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004009static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004010{
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004011 if (enable_ept) {
4012 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
4013 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08004014 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004015 } else {
4016 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004017 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004018}
4019
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004020static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4021{
4022 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4023}
4024
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004025static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4026{
4027 if (enable_ept)
4028 vmx_flush_tlb(vcpu);
4029}
4030
Avi Kivitye8467fd2009-12-29 18:43:06 +02004031static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4032{
4033 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4034
4035 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4036 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4037}
4038
Avi Kivityaff48ba2010-12-05 18:56:11 +02004039static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4040{
4041 if (enable_ept && is_paging(vcpu))
4042 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4043 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4044}
4045
Anthony Liguori25c4c272007-04-27 09:29:21 +03004046static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004047{
Avi Kivityfc78f512009-12-07 12:16:48 +02004048 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4049
4050 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4051 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004052}
4053
Sheng Yang14394422008-04-28 12:24:45 +08004054static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4055{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004056 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4057
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004058 if (!test_bit(VCPU_EXREG_PDPTR,
4059 (unsigned long *)&vcpu->arch.regs_dirty))
4060 return;
4061
Sheng Yang14394422008-04-28 12:24:45 +08004062 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004063 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4064 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4065 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4066 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004067 }
4068}
4069
Avi Kivity8f5d5492009-05-31 18:41:29 +03004070static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4071{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004072 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4073
Avi Kivity8f5d5492009-05-31 18:41:29 +03004074 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004075 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4076 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4077 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4078 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004079 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004080
4081 __set_bit(VCPU_EXREG_PDPTR,
4082 (unsigned long *)&vcpu->arch.regs_avail);
4083 __set_bit(VCPU_EXREG_PDPTR,
4084 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004085}
4086
David Matlack38991522016-11-29 18:14:08 -08004087static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4088{
4089 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4090 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4091 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4092
4093 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4094 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4095 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4096 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4097
4098 return fixed_bits_valid(val, fixed0, fixed1);
4099}
4100
4101static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4102{
4103 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4104 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4105
4106 return fixed_bits_valid(val, fixed0, fixed1);
4107}
4108
4109static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4110{
4111 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4112 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4113
4114 return fixed_bits_valid(val, fixed0, fixed1);
4115}
4116
4117/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4118#define nested_guest_cr4_valid nested_cr4_valid
4119#define nested_host_cr4_valid nested_cr4_valid
4120
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004121static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004122
4123static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4124 unsigned long cr0,
4125 struct kvm_vcpu *vcpu)
4126{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004127 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4128 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004129 if (!(cr0 & X86_CR0_PG)) {
4130 /* From paging/starting to nonpaging */
4131 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004132 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004133 (CPU_BASED_CR3_LOAD_EXITING |
4134 CPU_BASED_CR3_STORE_EXITING));
4135 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004136 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004137 } else if (!is_paging(vcpu)) {
4138 /* From nonpaging to paging */
4139 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004140 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004141 ~(CPU_BASED_CR3_LOAD_EXITING |
4142 CPU_BASED_CR3_STORE_EXITING));
4143 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004144 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004145 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004146
4147 if (!(cr0 & X86_CR0_WP))
4148 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004149}
4150
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4152{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004153 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004154 unsigned long hw_cr0;
4155
Gleb Natapov50378782013-02-04 16:00:28 +02004156 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004157 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004158 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004159 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004160 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004161
Gleb Natapov218e7632013-01-21 15:36:45 +02004162 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4163 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164
Gleb Natapov218e7632013-01-21 15:36:45 +02004165 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4166 enter_rmode(vcpu);
4167 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004169#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004170 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004171 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004173 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 exit_lmode(vcpu);
4175 }
4176#endif
4177
Avi Kivity089d0342009-03-23 18:26:32 +02004178 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004179 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4180
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004182 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004183 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004184
4185 /* depends on vcpu->arch.cr0 to be set to a new value */
4186 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187}
4188
Sheng Yang14394422008-04-28 12:24:45 +08004189static u64 construct_eptp(unsigned long root_hpa)
4190{
4191 u64 eptp;
4192
4193 /* TODO write the value reading from MSR */
4194 eptp = VMX_EPT_DEFAULT_MT |
4195 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004196 if (enable_ept_ad_bits)
4197 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004198 eptp |= (root_hpa & PAGE_MASK);
4199
4200 return eptp;
4201}
4202
Avi Kivity6aa8b732006-12-10 02:21:36 -08004203static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4204{
Sheng Yang14394422008-04-28 12:24:45 +08004205 unsigned long guest_cr3;
4206 u64 eptp;
4207
4208 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004209 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004210 eptp = construct_eptp(cr3);
4211 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004212 if (is_paging(vcpu) || is_guest_mode(vcpu))
4213 guest_cr3 = kvm_read_cr3(vcpu);
4214 else
4215 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004216 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004217 }
4218
Sheng Yang2384d2b2008-01-17 15:14:33 +08004219 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004220 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004221}
4222
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004223static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004224{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004225 /*
4226 * Pass through host's Machine Check Enable value to hw_cr4, which
4227 * is in force while we are in guest mode. Do not let guests control
4228 * this bit, even if host CR4.MCE == 0.
4229 */
4230 unsigned long hw_cr4 =
4231 (cr4_read_shadow() & X86_CR4_MCE) |
4232 (cr4 & ~X86_CR4_MCE) |
4233 (to_vmx(vcpu)->rmode.vm86_active ?
4234 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004235
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004236 if (cr4 & X86_CR4_VMXE) {
4237 /*
4238 * To use VMXON (and later other VMX instructions), a guest
4239 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4240 * So basically the check on whether to allow nested VMX
4241 * is here.
4242 */
4243 if (!nested_vmx_allowed(vcpu))
4244 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004245 }
David Matlack38991522016-11-29 18:14:08 -08004246
4247 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004248 return 1;
4249
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004250 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004251 if (enable_ept) {
4252 if (!is_paging(vcpu)) {
4253 hw_cr4 &= ~X86_CR4_PAE;
4254 hw_cr4 |= X86_CR4_PSE;
4255 } else if (!(cr4 & X86_CR4_PAE)) {
4256 hw_cr4 &= ~X86_CR4_PAE;
4257 }
4258 }
Sheng Yang14394422008-04-28 12:24:45 +08004259
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004260 if (!enable_unrestricted_guest && !is_paging(vcpu))
4261 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004262 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4263 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4264 * to be manually disabled when guest switches to non-paging
4265 * mode.
4266 *
4267 * If !enable_unrestricted_guest, the CPU is always running
4268 * with CR0.PG=1 and CR4 needs to be modified.
4269 * If enable_unrestricted_guest, the CPU automatically
4270 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004271 */
Huaitong Handdba2622016-03-22 16:51:15 +08004272 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004273
Sheng Yang14394422008-04-28 12:24:45 +08004274 vmcs_writel(CR4_READ_SHADOW, cr4);
4275 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004276 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277}
4278
Avi Kivity6aa8b732006-12-10 02:21:36 -08004279static void vmx_get_segment(struct kvm_vcpu *vcpu,
4280 struct kvm_segment *var, int seg)
4281{
Avi Kivitya9179492011-01-03 14:28:52 +02004282 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004283 u32 ar;
4284
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004285 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004286 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004287 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004288 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004289 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004290 var->base = vmx_read_guest_seg_base(vmx, seg);
4291 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4292 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004293 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004294 var->base = vmx_read_guest_seg_base(vmx, seg);
4295 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4296 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4297 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004298 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004299 var->type = ar & 15;
4300 var->s = (ar >> 4) & 1;
4301 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004302 /*
4303 * Some userspaces do not preserve unusable property. Since usable
4304 * segment has to be present according to VMX spec we can use present
4305 * property to amend userspace bug by making unusable segment always
4306 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4307 * segment as unusable.
4308 */
4309 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004310 var->avl = (ar >> 12) & 1;
4311 var->l = (ar >> 13) & 1;
4312 var->db = (ar >> 14) & 1;
4313 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004314}
4315
Avi Kivitya9179492011-01-03 14:28:52 +02004316static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4317{
Avi Kivitya9179492011-01-03 14:28:52 +02004318 struct kvm_segment s;
4319
4320 if (to_vmx(vcpu)->rmode.vm86_active) {
4321 vmx_get_segment(vcpu, &s, seg);
4322 return s.base;
4323 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004324 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004325}
4326
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004327static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004328{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004329 struct vcpu_vmx *vmx = to_vmx(vcpu);
4330
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004331 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004332 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004333 else {
4334 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004335 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004336 }
Avi Kivity69c73022011-03-07 15:26:44 +02004337}
4338
Avi Kivity653e3102007-05-07 10:55:37 +03004339static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004340{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341 u32 ar;
4342
Avi Kivityf0495f92012-06-07 17:06:10 +03004343 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004344 ar = 1 << 16;
4345 else {
4346 ar = var->type & 15;
4347 ar |= (var->s & 1) << 4;
4348 ar |= (var->dpl & 3) << 5;
4349 ar |= (var->present & 1) << 7;
4350 ar |= (var->avl & 1) << 12;
4351 ar |= (var->l & 1) << 13;
4352 ar |= (var->db & 1) << 14;
4353 ar |= (var->g & 1) << 15;
4354 }
Avi Kivity653e3102007-05-07 10:55:37 +03004355
4356 return ar;
4357}
4358
4359static void vmx_set_segment(struct kvm_vcpu *vcpu,
4360 struct kvm_segment *var, int seg)
4361{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004362 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004363 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004364
Avi Kivity2fb92db2011-04-27 19:42:18 +03004365 vmx_segment_cache_clear(vmx);
4366
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004367 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4368 vmx->rmode.segs[seg] = *var;
4369 if (seg == VCPU_SREG_TR)
4370 vmcs_write16(sf->selector, var->selector);
4371 else if (var->s)
4372 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004373 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004374 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004375
Avi Kivity653e3102007-05-07 10:55:37 +03004376 vmcs_writel(sf->base, var->base);
4377 vmcs_write32(sf->limit, var->limit);
4378 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004379
4380 /*
4381 * Fix the "Accessed" bit in AR field of segment registers for older
4382 * qemu binaries.
4383 * IA32 arch specifies that at the time of processor reset the
4384 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004385 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004386 * state vmexit when "unrestricted guest" mode is turned on.
4387 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4388 * tree. Newer qemu binaries with that qemu fix would not need this
4389 * kvm hack.
4390 */
4391 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004392 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004393
Gleb Natapovf924d662012-12-12 19:10:55 +02004394 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004395
4396out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004397 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004398}
4399
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4401{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004402 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403
4404 *db = (ar >> 14) & 1;
4405 *l = (ar >> 13) & 1;
4406}
4407
Gleb Natapov89a27f42010-02-16 10:51:48 +02004408static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004410 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4411 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004412}
4413
Gleb Natapov89a27f42010-02-16 10:51:48 +02004414static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004415{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004416 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4417 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418}
4419
Gleb Natapov89a27f42010-02-16 10:51:48 +02004420static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004421{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004422 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4423 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004424}
4425
Gleb Natapov89a27f42010-02-16 10:51:48 +02004426static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004428 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4429 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430}
4431
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004432static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4433{
4434 struct kvm_segment var;
4435 u32 ar;
4436
4437 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004438 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004439 if (seg == VCPU_SREG_CS)
4440 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004441 ar = vmx_segment_access_rights(&var);
4442
4443 if (var.base != (var.selector << 4))
4444 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004445 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004446 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004447 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004448 return false;
4449
4450 return true;
4451}
4452
4453static bool code_segment_valid(struct kvm_vcpu *vcpu)
4454{
4455 struct kvm_segment cs;
4456 unsigned int cs_rpl;
4457
4458 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004459 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004460
Avi Kivity1872a3f2009-01-04 23:26:52 +02004461 if (cs.unusable)
4462 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004463 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004464 return false;
4465 if (!cs.s)
4466 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004467 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004468 if (cs.dpl > cs_rpl)
4469 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004470 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004471 if (cs.dpl != cs_rpl)
4472 return false;
4473 }
4474 if (!cs.present)
4475 return false;
4476
4477 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4478 return true;
4479}
4480
4481static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4482{
4483 struct kvm_segment ss;
4484 unsigned int ss_rpl;
4485
4486 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004487 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004488
Avi Kivity1872a3f2009-01-04 23:26:52 +02004489 if (ss.unusable)
4490 return true;
4491 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004492 return false;
4493 if (!ss.s)
4494 return false;
4495 if (ss.dpl != ss_rpl) /* DPL != RPL */
4496 return false;
4497 if (!ss.present)
4498 return false;
4499
4500 return true;
4501}
4502
4503static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4504{
4505 struct kvm_segment var;
4506 unsigned int rpl;
4507
4508 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004509 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004510
Avi Kivity1872a3f2009-01-04 23:26:52 +02004511 if (var.unusable)
4512 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004513 if (!var.s)
4514 return false;
4515 if (!var.present)
4516 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004517 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004518 if (var.dpl < rpl) /* DPL < RPL */
4519 return false;
4520 }
4521
4522 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4523 * rights flags
4524 */
4525 return true;
4526}
4527
4528static bool tr_valid(struct kvm_vcpu *vcpu)
4529{
4530 struct kvm_segment tr;
4531
4532 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4533
Avi Kivity1872a3f2009-01-04 23:26:52 +02004534 if (tr.unusable)
4535 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004536 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004537 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004538 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004539 return false;
4540 if (!tr.present)
4541 return false;
4542
4543 return true;
4544}
4545
4546static bool ldtr_valid(struct kvm_vcpu *vcpu)
4547{
4548 struct kvm_segment ldtr;
4549
4550 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4551
Avi Kivity1872a3f2009-01-04 23:26:52 +02004552 if (ldtr.unusable)
4553 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004554 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004555 return false;
4556 if (ldtr.type != 2)
4557 return false;
4558 if (!ldtr.present)
4559 return false;
4560
4561 return true;
4562}
4563
4564static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4565{
4566 struct kvm_segment cs, ss;
4567
4568 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4569 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4570
Nadav Amitb32a9912015-03-29 16:33:04 +03004571 return ((cs.selector & SEGMENT_RPL_MASK) ==
4572 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004573}
4574
4575/*
4576 * Check if guest state is valid. Returns true if valid, false if
4577 * not.
4578 * We assume that registers are always usable
4579 */
4580static bool guest_state_valid(struct kvm_vcpu *vcpu)
4581{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004582 if (enable_unrestricted_guest)
4583 return true;
4584
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004585 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004586 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004587 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4588 return false;
4589 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4590 return false;
4591 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4592 return false;
4593 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4594 return false;
4595 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4596 return false;
4597 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4598 return false;
4599 } else {
4600 /* protected mode guest state checks */
4601 if (!cs_ss_rpl_check(vcpu))
4602 return false;
4603 if (!code_segment_valid(vcpu))
4604 return false;
4605 if (!stack_segment_valid(vcpu))
4606 return false;
4607 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4608 return false;
4609 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4610 return false;
4611 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4612 return false;
4613 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4614 return false;
4615 if (!tr_valid(vcpu))
4616 return false;
4617 if (!ldtr_valid(vcpu))
4618 return false;
4619 }
4620 /* TODO:
4621 * - Add checks on RIP
4622 * - Add checks on RFLAGS
4623 */
4624
4625 return true;
4626}
4627
Mike Dayd77c26f2007-10-08 09:02:08 -04004628static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004630 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004631 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004632 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004634 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004635 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004636 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4637 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004638 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004639 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004640 r = kvm_write_guest_page(kvm, fn++, &data,
4641 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004642 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004643 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004644 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4645 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004646 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004647 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4648 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004649 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004650 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004651 r = kvm_write_guest_page(kvm, fn, &data,
4652 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4653 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004654out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004655 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004656 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004657}
4658
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004659static int init_rmode_identity_map(struct kvm *kvm)
4660{
Tang Chenf51770e2014-09-16 18:41:59 +08004661 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004662 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004663 u32 tmp;
4664
Avi Kivity089d0342009-03-23 18:26:32 +02004665 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004666 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004667
4668 /* Protect kvm->arch.ept_identity_pagetable_done. */
4669 mutex_lock(&kvm->slots_lock);
4670
Tang Chenf51770e2014-09-16 18:41:59 +08004671 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004672 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004673
Sheng Yangb927a3c2009-07-21 10:42:48 +08004674 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004675
4676 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004677 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004678 goto out2;
4679
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004680 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004681 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4682 if (r < 0)
4683 goto out;
4684 /* Set up identity-mapping pagetable for EPT in real mode */
4685 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4686 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4687 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4688 r = kvm_write_guest_page(kvm, identity_map_pfn,
4689 &tmp, i * sizeof(tmp), sizeof(tmp));
4690 if (r < 0)
4691 goto out;
4692 }
4693 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004694
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004695out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004696 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004697
4698out2:
4699 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004700 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004701}
4702
Avi Kivity6aa8b732006-12-10 02:21:36 -08004703static void seg_setup(int seg)
4704{
Mathias Krause772e0312012-08-30 01:30:19 +02004705 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004706 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707
4708 vmcs_write16(sf->selector, 0);
4709 vmcs_writel(sf->base, 0);
4710 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004711 ar = 0x93;
4712 if (seg == VCPU_SREG_CS)
4713 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004714
4715 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004716}
4717
Sheng Yangf78e0e22007-10-29 09:40:42 +08004718static int alloc_apic_access_page(struct kvm *kvm)
4719{
Xiao Guangrong44841412012-09-07 14:14:20 +08004720 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004721 int r = 0;
4722
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004723 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004724 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004725 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004726 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4727 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004728 if (r)
4729 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004730
Tang Chen73a6d942014-09-11 13:38:00 +08004731 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004732 if (is_error_page(page)) {
4733 r = -EFAULT;
4734 goto out;
4735 }
4736
Tang Chenc24ae0d2014-09-24 15:57:58 +08004737 /*
4738 * Do not pin the page in memory, so that memory hot-unplug
4739 * is able to migrate it.
4740 */
4741 put_page(page);
4742 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004743out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004744 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004745 return r;
4746}
4747
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004748static int alloc_identity_pagetable(struct kvm *kvm)
4749{
Tang Chena255d472014-09-16 18:41:58 +08004750 /* Called with kvm->slots_lock held. */
4751
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004752 int r = 0;
4753
Tang Chena255d472014-09-16 18:41:58 +08004754 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4755
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004756 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4757 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004758
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004759 return r;
4760}
4761
Wanpeng Li991e7a02015-09-16 17:30:05 +08004762static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004763{
4764 int vpid;
4765
Avi Kivity919818a2009-03-23 18:01:29 +02004766 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004767 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004768 spin_lock(&vmx_vpid_lock);
4769 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004770 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004771 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004772 else
4773 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004774 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004775 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004776}
4777
Wanpeng Li991e7a02015-09-16 17:30:05 +08004778static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004779{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004780 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004781 return;
4782 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004783 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004784 spin_unlock(&vmx_vpid_lock);
4785}
4786
Yang Zhang8d146952013-01-25 10:18:50 +08004787#define MSR_TYPE_R 1
4788#define MSR_TYPE_W 2
4789static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4790 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004791{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004792 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004793
4794 if (!cpu_has_vmx_msr_bitmap())
4795 return;
4796
4797 /*
4798 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4799 * have the write-low and read-high bitmap offsets the wrong way round.
4800 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4801 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004802 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004803 if (type & MSR_TYPE_R)
4804 /* read-low */
4805 __clear_bit(msr, msr_bitmap + 0x000 / f);
4806
4807 if (type & MSR_TYPE_W)
4808 /* write-low */
4809 __clear_bit(msr, msr_bitmap + 0x800 / f);
4810
Sheng Yang25c5f222008-03-28 13:18:56 +08004811 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4812 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004813 if (type & MSR_TYPE_R)
4814 /* read-high */
4815 __clear_bit(msr, msr_bitmap + 0x400 / f);
4816
4817 if (type & MSR_TYPE_W)
4818 /* write-high */
4819 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4820
4821 }
4822}
4823
Wincy Vanf2b93282015-02-03 23:56:03 +08004824/*
4825 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4826 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4827 */
4828static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4829 unsigned long *msr_bitmap_nested,
4830 u32 msr, int type)
4831{
4832 int f = sizeof(unsigned long);
4833
4834 if (!cpu_has_vmx_msr_bitmap()) {
4835 WARN_ON(1);
4836 return;
4837 }
4838
4839 /*
4840 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4841 * have the write-low and read-high bitmap offsets the wrong way round.
4842 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4843 */
4844 if (msr <= 0x1fff) {
4845 if (type & MSR_TYPE_R &&
4846 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4847 /* read-low */
4848 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4849
4850 if (type & MSR_TYPE_W &&
4851 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4852 /* write-low */
4853 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4854
4855 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4856 msr &= 0x1fff;
4857 if (type & MSR_TYPE_R &&
4858 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4859 /* read-high */
4860 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4861
4862 if (type & MSR_TYPE_W &&
4863 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4864 /* write-high */
4865 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4866
4867 }
4868}
4869
Avi Kivity58972972009-02-24 22:26:47 +02004870static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4871{
4872 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004873 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4874 msr, MSR_TYPE_R | MSR_TYPE_W);
4875 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4876 msr, MSR_TYPE_R | MSR_TYPE_W);
4877}
4878
Radim Krčmář2e69f862016-09-29 22:41:32 +02004879static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004880{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004881 if (apicv_active) {
Wanpeng Lic63e4562016-09-23 19:17:16 +08004882 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004883 msr, type);
Wanpeng Lic63e4562016-09-23 19:17:16 +08004884 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004885 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004886 } else {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004887 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004888 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004889 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004890 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004891 }
Avi Kivity58972972009-02-24 22:26:47 +02004892}
4893
Andrey Smetanind62caab2015-11-10 15:36:33 +03004894static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004895{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004896 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004897}
4898
David Hildenbrand6342c502017-01-25 11:58:58 +01004899static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08004900{
4901 struct vcpu_vmx *vmx = to_vmx(vcpu);
4902 int max_irr;
4903 void *vapic_page;
4904 u16 status;
4905
4906 if (vmx->nested.pi_desc &&
4907 vmx->nested.pi_pending) {
4908 vmx->nested.pi_pending = false;
4909 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
David Hildenbrand6342c502017-01-25 11:58:58 +01004910 return;
Wincy Van705699a2015-02-03 23:58:17 +08004911
4912 max_irr = find_last_bit(
4913 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4914
4915 if (max_irr == 256)
David Hildenbrand6342c502017-01-25 11:58:58 +01004916 return;
Wincy Van705699a2015-02-03 23:58:17 +08004917
4918 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08004919 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4920 kunmap(vmx->nested.virtual_apic_page);
4921
4922 status = vmcs_read16(GUEST_INTR_STATUS);
4923 if ((u8)max_irr > ((u8)status & 0xff)) {
4924 status &= ~0xff;
4925 status |= (u8)max_irr;
4926 vmcs_write16(GUEST_INTR_STATUS, status);
4927 }
4928 }
Wincy Van705699a2015-02-03 23:58:17 +08004929}
4930
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004931static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4932{
4933#ifdef CONFIG_SMP
4934 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004935 struct vcpu_vmx *vmx = to_vmx(vcpu);
4936
4937 /*
4938 * Currently, we don't support urgent interrupt,
4939 * all interrupts are recognized as non-urgent
4940 * interrupt, so we cannot post interrupts when
4941 * 'SN' is set.
4942 *
4943 * If the vcpu is in guest mode, it means it is
4944 * running instead of being scheduled out and
4945 * waiting in the run queue, and that's the only
4946 * case when 'SN' is set currently, warning if
4947 * 'SN' is set.
4948 */
4949 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
4950
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004951 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4952 POSTED_INTR_VECTOR);
4953 return true;
4954 }
4955#endif
4956 return false;
4957}
4958
Wincy Van705699a2015-02-03 23:58:17 +08004959static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4960 int vector)
4961{
4962 struct vcpu_vmx *vmx = to_vmx(vcpu);
4963
4964 if (is_guest_mode(vcpu) &&
4965 vector == vmx->nested.posted_intr_nv) {
4966 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004967 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004968 /*
4969 * If a posted intr is not recognized by hardware,
4970 * we will accomplish it in the next vmentry.
4971 */
4972 vmx->nested.pi_pending = true;
4973 kvm_make_request(KVM_REQ_EVENT, vcpu);
4974 return 0;
4975 }
4976 return -1;
4977}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004979 * Send interrupt to vcpu via posted interrupt way.
4980 * 1. If target vcpu is running(non-root mode), send posted interrupt
4981 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4982 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4983 * interrupt from PIR in next vmentry.
4984 */
4985static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4986{
4987 struct vcpu_vmx *vmx = to_vmx(vcpu);
4988 int r;
4989
Wincy Van705699a2015-02-03 23:58:17 +08004990 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4991 if (!r)
4992 return;
4993
Yang Zhanga20ed542013-04-11 19:25:15 +08004994 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4995 return;
4996
Paolo Bonzinib95234c2016-12-19 13:57:33 +01004997 /* If a previous notification has sent the IPI, nothing to do. */
4998 if (pi_test_and_set_on(&vmx->pi_desc))
4999 return;
5000
5001 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08005002 kvm_vcpu_kick(vcpu);
5003}
5004
Avi Kivity6aa8b732006-12-10 02:21:36 -08005005/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005006 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
5007 * will not change in the lifetime of the guest.
5008 * Note that host-state that does change is set elsewhere. E.g., host-state
5009 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
5010 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005011static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005012{
5013 u32 low32, high32;
5014 unsigned long tmpl;
5015 struct desc_ptr dt;
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005016 unsigned long cr0, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005017
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005018 cr0 = read_cr0();
5019 WARN_ON(cr0 & X86_CR0_TS);
5020 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005021 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5022
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005023 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005024 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005025 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5026 vmx->host_state.vmcs_host_cr4 = cr4;
5027
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005028 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005029#ifdef CONFIG_X86_64
5030 /*
5031 * Load null selectors, so we can avoid reloading them in
5032 * __vmx_load_host_state(), in case userspace uses the null selectors
5033 * too (the expected case).
5034 */
5035 vmcs_write16(HOST_DS_SELECTOR, 0);
5036 vmcs_write16(HOST_ES_SELECTOR, 0);
5037#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005038 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5039 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005040#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005041 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5042 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5043
5044 native_store_idt(&dt);
5045 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005046 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005047
Avi Kivity83287ea422012-09-16 15:10:57 +03005048 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005049
5050 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5051 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5052 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5053 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5054
5055 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5056 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5057 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5058 }
5059}
5060
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005061static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5062{
5063 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5064 if (enable_ept)
5065 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005066 if (is_guest_mode(&vmx->vcpu))
5067 vmx->vcpu.arch.cr4_guest_owned_bits &=
5068 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005069 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5070}
5071
Yang Zhang01e439b2013-04-11 19:25:12 +08005072static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5073{
5074 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5075
Andrey Smetanind62caab2015-11-10 15:36:33 +03005076 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005077 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005078 /* Enable the preemption timer dynamically */
5079 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005080 return pin_based_exec_ctrl;
5081}
5082
Andrey Smetanind62caab2015-11-10 15:36:33 +03005083static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5084{
5085 struct vcpu_vmx *vmx = to_vmx(vcpu);
5086
5087 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005088 if (cpu_has_secondary_exec_ctrls()) {
5089 if (kvm_vcpu_apicv_active(vcpu))
5090 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5091 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5092 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5093 else
5094 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5095 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5096 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5097 }
5098
5099 if (cpu_has_vmx_msr_bitmap())
5100 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005101}
5102
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005103static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5104{
5105 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005106
5107 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5108 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5109
Paolo Bonzini35754c92015-07-29 12:05:37 +02005110 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005111 exec_control &= ~CPU_BASED_TPR_SHADOW;
5112#ifdef CONFIG_X86_64
5113 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5114 CPU_BASED_CR8_LOAD_EXITING;
5115#endif
5116 }
5117 if (!enable_ept)
5118 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5119 CPU_BASED_CR3_LOAD_EXITING |
5120 CPU_BASED_INVLPG_EXITING;
5121 return exec_control;
5122}
5123
5124static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5125{
5126 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005127 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005128 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5129 if (vmx->vpid == 0)
5130 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5131 if (!enable_ept) {
5132 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5133 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005134 /* Enable INVPCID for non-ept guests may cause performance regression. */
5135 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005136 }
5137 if (!enable_unrestricted_guest)
5138 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5139 if (!ple_gap)
5140 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005141 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005142 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5143 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005144 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005145 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5146 (handle_vmptrld).
5147 We can NOT enable shadow_vmcs here because we don't have yet
5148 a current VMCS12
5149 */
5150 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005151
5152 if (!enable_pml)
5153 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005154
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005155 return exec_control;
5156}
5157
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005158static void ept_set_mmio_spte_mask(void)
5159{
5160 /*
5161 * EPT Misconfigurations can be generated if the value of bits 2:0
5162 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005163 */
Junaid Shahid312b6162016-12-21 20:29:29 -08005164 kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005165}
5166
Wanpeng Lif53cd632014-12-02 19:14:58 +08005167#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005168/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005169 * Sets up the vmcs for emulated real mode.
5170 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005171static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005173#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005174 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005175#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005176 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177
Avi Kivity6aa8b732006-12-10 02:21:36 -08005178 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005179 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5180 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005181
Abel Gordon4607c2d2013-04-18 14:35:55 +03005182 if (enable_shadow_vmcs) {
5183 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5184 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5185 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005186 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005187 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005188
Avi Kivity6aa8b732006-12-10 02:21:36 -08005189 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5190
Avi Kivity6aa8b732006-12-10 02:21:36 -08005191 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005192 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005193 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005194
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005195 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005196
Dan Williamsdfa169b2016-06-02 11:17:24 -07005197 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005198 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5199 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005200 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005201
Andrey Smetanind62caab2015-11-10 15:36:33 +03005202 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005203 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5204 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5205 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5206 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5207
5208 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005209
Li RongQing0bcf2612015-12-03 13:29:34 +08005210 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005211 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005212 }
5213
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005214 if (ple_gap) {
5215 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005216 vmx->ple_window = ple_window;
5217 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005218 }
5219
Xiao Guangrongc3707952011-07-12 03:28:04 +08005220 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5221 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005222 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5223
Avi Kivity9581d442010-10-19 16:46:55 +02005224 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5225 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005226 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005227#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005228 rdmsrl(MSR_FS_BASE, a);
5229 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5230 rdmsrl(MSR_GS_BASE, a);
5231 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5232#else
5233 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5234 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5235#endif
5236
Eddie Dong2cc51562007-05-21 07:28:09 +03005237 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5238 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005239 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005240 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005241 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005242
Radim Krčmář74545702015-04-27 15:11:25 +02005243 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5244 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005245
Paolo Bonzini03916db2014-07-24 14:21:57 +02005246 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005247 u32 index = vmx_msr_index[i];
5248 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005249 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005250
5251 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5252 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005253 if (wrmsr_safe(index, data_low, data_high) < 0)
5254 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005255 vmx->guest_msrs[j].index = i;
5256 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005257 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005258 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005259 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260
Gleb Natapov2961e8762013-11-25 15:37:13 +02005261
5262 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263
5264 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005265 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005266
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005267 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5268 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5269
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005270 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005271
Wanpeng Lif53cd632014-12-02 19:14:58 +08005272 if (vmx_xsaves_supported())
5273 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5274
Peter Feiner4e595162016-07-07 14:49:58 -07005275 if (enable_pml) {
5276 ASSERT(vmx->pml_pg);
5277 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5278 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5279 }
5280
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005281 return 0;
5282}
5283
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005284static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005285{
5286 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005287 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005288 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005289
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005290 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005291
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005292 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005293 kvm_set_cr8(vcpu, 0);
5294
5295 if (!init_event) {
5296 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5297 MSR_IA32_APICBASE_ENABLE;
5298 if (kvm_vcpu_is_reset_bsp(vcpu))
5299 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5300 apic_base_msr.host_initiated = true;
5301 kvm_set_apic_base(vcpu, &apic_base_msr);
5302 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005303
Avi Kivity2fb92db2011-04-27 19:42:18 +03005304 vmx_segment_cache_clear(vmx);
5305
Avi Kivity5706be02008-08-20 15:07:31 +03005306 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005307 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005308 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005309
5310 seg_setup(VCPU_SREG_DS);
5311 seg_setup(VCPU_SREG_ES);
5312 seg_setup(VCPU_SREG_FS);
5313 seg_setup(VCPU_SREG_GS);
5314 seg_setup(VCPU_SREG_SS);
5315
5316 vmcs_write16(GUEST_TR_SELECTOR, 0);
5317 vmcs_writel(GUEST_TR_BASE, 0);
5318 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5319 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5320
5321 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5322 vmcs_writel(GUEST_LDTR_BASE, 0);
5323 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5324 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5325
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005326 if (!init_event) {
5327 vmcs_write32(GUEST_SYSENTER_CS, 0);
5328 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5329 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5330 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5331 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005332
5333 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005334 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005335
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005336 vmcs_writel(GUEST_GDTR_BASE, 0);
5337 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5338
5339 vmcs_writel(GUEST_IDTR_BASE, 0);
5340 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5341
Anthony Liguori443381a2010-12-06 10:53:38 -06005342 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005343 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005344 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005345
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005346 setup_msrs(vmx);
5347
Avi Kivity6aa8b732006-12-10 02:21:36 -08005348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5349
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005350 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005351 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005352 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005353 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005354 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005355 vmcs_write32(TPR_THRESHOLD, 0);
5356 }
5357
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005358 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359
Andrey Smetanind62caab2015-11-10 15:36:33 +03005360 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005361 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5362
Sheng Yang2384d2b2008-01-17 15:14:33 +08005363 if (vmx->vpid != 0)
5364 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5365
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005366 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005367 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005368 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005369 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005370 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005371
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005372 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005373
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005374 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005375}
5376
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005377/*
5378 * In nested virtualization, check if L1 asked to exit on external interrupts.
5379 * For most existing hypervisors, this will always return true.
5380 */
5381static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5382{
5383 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5384 PIN_BASED_EXT_INTR_MASK;
5385}
5386
Bandan Das77b0f5d2014-04-19 18:17:45 -04005387/*
5388 * In nested virtualization, check if L1 has set
5389 * VM_EXIT_ACK_INTR_ON_EXIT
5390 */
5391static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5392{
5393 return get_vmcs12(vcpu)->vm_exit_controls &
5394 VM_EXIT_ACK_INTR_ON_EXIT;
5395}
5396
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005397static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5398{
5399 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5400 PIN_BASED_NMI_EXITING;
5401}
5402
Jan Kiszkac9a79532014-03-07 20:03:15 +01005403static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005404{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005405 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5406 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005407}
5408
Jan Kiszkac9a79532014-03-07 20:03:15 +01005409static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005410{
Paolo Bonzini2c828782017-03-27 14:37:28 +02005411 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005412 enable_irq_window(vcpu);
5413 return;
5414 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005415
Paolo Bonzini47c01522016-12-19 11:44:07 +01005416 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5417 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005418}
5419
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005420static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005421{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005422 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005423 uint32_t intr;
5424 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005425
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005426 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005427
Avi Kivityfa89a812008-09-01 15:57:51 +03005428 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005429 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005430 int inc_eip = 0;
5431 if (vcpu->arch.interrupt.soft)
5432 inc_eip = vcpu->arch.event_exit_inst_len;
5433 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005434 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005435 return;
5436 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005437 intr = irq | INTR_INFO_VALID_MASK;
5438 if (vcpu->arch.interrupt.soft) {
5439 intr |= INTR_TYPE_SOFT_INTR;
5440 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5441 vmx->vcpu.arch.event_exit_inst_len);
5442 } else
5443 intr |= INTR_TYPE_EXT_INTR;
5444 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005445}
5446
Sheng Yangf08864b2008-05-15 18:23:25 +08005447static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5448{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005449 struct vcpu_vmx *vmx = to_vmx(vcpu);
5450
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005451 if (!is_guest_mode(vcpu)) {
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005452 ++vcpu->stat.nmi_injections;
5453 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005454 }
5455
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005456 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005457 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005458 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005459 return;
5460 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005461
Sheng Yangf08864b2008-05-15 18:23:25 +08005462 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5463 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005464}
5465
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005466static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5467{
Avi Kivity9d58b932011-03-07 16:52:07 +02005468 if (to_vmx(vcpu)->nmi_known_unmasked)
5469 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005470 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005471}
5472
5473static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5474{
5475 struct vcpu_vmx *vmx = to_vmx(vcpu);
5476
Paolo Bonzini2c828782017-03-27 14:37:28 +02005477 vmx->nmi_known_unmasked = !masked;
5478 if (masked)
5479 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5480 GUEST_INTR_STATE_NMI);
5481 else
5482 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5483 GUEST_INTR_STATE_NMI);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005484}
5485
Jan Kiszka2505dc92013-04-14 12:12:47 +02005486static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5487{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005488 if (to_vmx(vcpu)->nested.nested_run_pending)
5489 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005490
Jan Kiszka2505dc92013-04-14 12:12:47 +02005491 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5492 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5493 | GUEST_INTR_STATE_NMI));
5494}
5495
Gleb Natapov78646122009-03-23 12:12:11 +02005496static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5497{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005498 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5499 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005500 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5501 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005502}
5503
Izik Eiduscbc94022007-10-25 00:29:55 +02005504static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5505{
5506 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005507
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005508 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5509 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005510 if (ret)
5511 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005512 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005513 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005514}
5515
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005516static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005517{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005518 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005519 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005520 /*
5521 * Update instruction length as we may reinject the exception
5522 * from user space while in guest debugging mode.
5523 */
5524 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5525 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005526 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005527 return false;
5528 /* fall through */
5529 case DB_VECTOR:
5530 if (vcpu->guest_debug &
5531 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5532 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005533 /* fall through */
5534 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005535 case OF_VECTOR:
5536 case BR_VECTOR:
5537 case UD_VECTOR:
5538 case DF_VECTOR:
5539 case SS_VECTOR:
5540 case GP_VECTOR:
5541 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005542 return true;
5543 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005544 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005545 return false;
5546}
5547
5548static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5549 int vec, u32 err_code)
5550{
5551 /*
5552 * Instruction with address size override prefix opcode 0x67
5553 * Cause the #SS fault with 0 error code in VM86 mode.
5554 */
5555 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5556 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5557 if (vcpu->arch.halt_request) {
5558 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005559 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005560 }
5561 return 1;
5562 }
5563 return 0;
5564 }
5565
5566 /*
5567 * Forward all other exceptions that are valid in real mode.
5568 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5569 * the required debugging infrastructure rework.
5570 */
5571 kvm_queue_exception(vcpu, vec);
5572 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573}
5574
Andi Kleena0861c02009-06-08 17:37:09 +08005575/*
5576 * Trigger machine check on the host. We assume all the MSRs are already set up
5577 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5578 * We pass a fake environment to the machine check handler because we want
5579 * the guest to be always treated like user space, no matter what context
5580 * it used internally.
5581 */
5582static void kvm_machine_check(void)
5583{
5584#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5585 struct pt_regs regs = {
5586 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5587 .flags = X86_EFLAGS_IF,
5588 };
5589
5590 do_machine_check(&regs, 0);
5591#endif
5592}
5593
Avi Kivity851ba692009-08-24 11:10:17 +03005594static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005595{
5596 /* already handled by vcpu_run */
5597 return 1;
5598}
5599
Avi Kivity851ba692009-08-24 11:10:17 +03005600static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005601{
Avi Kivity1155f762007-11-22 11:30:47 +02005602 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005603 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005604 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005605 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005606 u32 vect_info;
5607 enum emulation_result er;
5608
Avi Kivity1155f762007-11-22 11:30:47 +02005609 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005610 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005611
Andi Kleena0861c02009-06-08 17:37:09 +08005612 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005613 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005614
Jim Mattsonef85b672016-12-12 11:01:37 -08005615 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005616 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005617
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005618 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005619 if (is_guest_mode(vcpu)) {
5620 kvm_queue_exception(vcpu, UD_VECTOR);
5621 return 1;
5622 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005623 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005624 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005625 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005626 return 1;
5627 }
5628
Avi Kivity6aa8b732006-12-10 02:21:36 -08005629 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005630 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005631 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005632
5633 /*
5634 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5635 * MMIO, it is better to report an internal error.
5636 * See the comments in vmx_handle_exit.
5637 */
5638 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5639 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5640 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5641 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005642 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005643 vcpu->run->internal.data[0] = vect_info;
5644 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005645 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005646 return 0;
5647 }
5648
Avi Kivity6aa8b732006-12-10 02:21:36 -08005649 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005650 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005651 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005652 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005653 trace_kvm_page_fault(cr2, error_code);
5654
Gleb Natapov3298b752009-05-11 13:35:46 +03005655 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005656 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005657 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005658 }
5659
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005660 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005661
5662 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5663 return handle_rmode_exception(vcpu, ex_no, error_code);
5664
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005665 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005666 case AC_VECTOR:
5667 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5668 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005669 case DB_VECTOR:
5670 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5671 if (!(vcpu->guest_debug &
5672 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005673 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005674 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005675 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5676 skip_emulated_instruction(vcpu);
5677
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005678 kvm_queue_exception(vcpu, DB_VECTOR);
5679 return 1;
5680 }
5681 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5682 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5683 /* fall through */
5684 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005685 /*
5686 * Update instruction length as we may reinject #BP from
5687 * user space while in guest debugging mode. Reading it for
5688 * #DB as well causes no harm, it is not used in that case.
5689 */
5690 vmx->vcpu.arch.event_exit_inst_len =
5691 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005692 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005693 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005694 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5695 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005696 break;
5697 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005698 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5699 kvm_run->ex.exception = ex_no;
5700 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005701 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005702 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005703 return 0;
5704}
5705
Avi Kivity851ba692009-08-24 11:10:17 +03005706static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005707{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005708 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005709 return 1;
5710}
5711
Avi Kivity851ba692009-08-24 11:10:17 +03005712static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005713{
Avi Kivity851ba692009-08-24 11:10:17 +03005714 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005715 return 0;
5716}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005717
Avi Kivity851ba692009-08-24 11:10:17 +03005718static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005719{
He, Qingbfdaab02007-09-12 14:18:28 +08005720 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005721 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02005722 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005723
He, Qingbfdaab02007-09-12 14:18:28 +08005724 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005725 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005726 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005727
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005728 ++vcpu->stat.io_exits;
5729
5730 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005731 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005732
5733 port = exit_qualification >> 16;
5734 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005735
Kyle Huey6affcbe2016-11-29 12:40:40 -08005736 ret = kvm_skip_emulated_instruction(vcpu);
5737
5738 /*
5739 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
5740 * KVM_EXIT_DEBUG here.
5741 */
5742 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005743}
5744
Ingo Molnar102d8322007-02-19 14:37:47 +02005745static void
5746vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5747{
5748 /*
5749 * Patch in the VMCALL instruction:
5750 */
5751 hypercall[0] = 0x0f;
5752 hypercall[1] = 0x01;
5753 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005754}
5755
Guo Chao0fa06072012-06-28 15:16:19 +08005756/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005757static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5758{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005759 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005760 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5761 unsigned long orig_val = val;
5762
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005763 /*
5764 * We get here when L2 changed cr0 in a way that did not change
5765 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005766 * but did change L0 shadowed bits. So we first calculate the
5767 * effective cr0 value that L1 would like to write into the
5768 * hardware. It consists of the L2-owned bits from the new
5769 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005770 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005771 val = (val & ~vmcs12->cr0_guest_host_mask) |
5772 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5773
David Matlack38991522016-11-29 18:14:08 -08005774 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005775 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005776
5777 if (kvm_set_cr0(vcpu, val))
5778 return 1;
5779 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005780 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005781 } else {
5782 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08005783 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005784 return 1;
David Matlack38991522016-11-29 18:14:08 -08005785
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005786 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005787 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005788}
5789
5790static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5791{
5792 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005793 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5794 unsigned long orig_val = val;
5795
5796 /* analogously to handle_set_cr0 */
5797 val = (val & ~vmcs12->cr4_guest_host_mask) |
5798 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5799 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005800 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005801 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005802 return 0;
5803 } else
5804 return kvm_set_cr4(vcpu, val);
5805}
5806
Avi Kivity851ba692009-08-24 11:10:17 +03005807static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005808{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005809 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005810 int cr;
5811 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005812 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005813 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005814
He, Qingbfdaab02007-09-12 14:18:28 +08005815 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005816 cr = exit_qualification & 15;
5817 reg = (exit_qualification >> 8) & 15;
5818 switch ((exit_qualification >> 4) & 3) {
5819 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005820 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005821 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005822 switch (cr) {
5823 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005824 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005825 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005826 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005827 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005828 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005829 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005830 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005831 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005832 case 8: {
5833 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005834 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005835 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005836 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005837 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005838 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005839 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005840 return ret;
5841 /*
5842 * TODO: we might be squashing a
5843 * KVM_GUESTDBG_SINGLESTEP-triggered
5844 * KVM_EXIT_DEBUG here.
5845 */
Avi Kivity851ba692009-08-24 11:10:17 +03005846 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005847 return 0;
5848 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005849 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005850 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005851 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005852 WARN_ONCE(1, "Guest should always own CR0.TS");
5853 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005854 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005855 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005856 case 1: /*mov from cr*/
5857 switch (cr) {
5858 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005859 val = kvm_read_cr3(vcpu);
5860 kvm_register_write(vcpu, reg, val);
5861 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005862 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005863 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005864 val = kvm_get_cr8(vcpu);
5865 kvm_register_write(vcpu, reg, val);
5866 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005867 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005868 }
5869 break;
5870 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005871 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005872 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005873 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005874
Kyle Huey6affcbe2016-11-29 12:40:40 -08005875 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005876 default:
5877 break;
5878 }
Avi Kivity851ba692009-08-24 11:10:17 +03005879 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005880 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005881 (int)(exit_qualification >> 4) & 3, cr);
5882 return 0;
5883}
5884
Avi Kivity851ba692009-08-24 11:10:17 +03005885static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005886{
He, Qingbfdaab02007-09-12 14:18:28 +08005887 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005888 int dr, dr7, reg;
5889
5890 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5891 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5892
5893 /* First, if DR does not exist, trigger UD */
5894 if (!kvm_require_dr(vcpu, dr))
5895 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005896
Jan Kiszkaf2483412010-01-20 18:20:20 +01005897 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005898 if (!kvm_require_cpl(vcpu, 0))
5899 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005900 dr7 = vmcs_readl(GUEST_DR7);
5901 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005902 /*
5903 * As the vm-exit takes precedence over the debug trap, we
5904 * need to emulate the latter, either for the host or the
5905 * guest debugging itself.
5906 */
5907 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005908 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005909 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005910 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005911 vcpu->run->debug.arch.exception = DB_VECTOR;
5912 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005913 return 0;
5914 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005915 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005916 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005917 kvm_queue_exception(vcpu, DB_VECTOR);
5918 return 1;
5919 }
5920 }
5921
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005922 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01005923 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5924 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005925
5926 /*
5927 * No more DR vmexits; force a reload of the debug registers
5928 * and reenter on this instruction. The next vmexit will
5929 * retrieve the full state of the debug registers.
5930 */
5931 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5932 return 1;
5933 }
5934
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005935 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5936 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005937 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005938
5939 if (kvm_get_dr(vcpu, dr, &val))
5940 return 1;
5941 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005942 } else
Nadav Amit57773922014-06-18 17:19:23 +03005943 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005944 return 1;
5945
Kyle Huey6affcbe2016-11-29 12:40:40 -08005946 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005947}
5948
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005949static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5950{
5951 return vcpu->arch.dr6;
5952}
5953
5954static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5955{
5956}
5957
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005958static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5959{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005960 get_debugreg(vcpu->arch.db[0], 0);
5961 get_debugreg(vcpu->arch.db[1], 1);
5962 get_debugreg(vcpu->arch.db[2], 2);
5963 get_debugreg(vcpu->arch.db[3], 3);
5964 get_debugreg(vcpu->arch.dr6, 6);
5965 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5966
5967 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01005968 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005969}
5970
Gleb Natapov020df072010-04-13 10:05:23 +03005971static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5972{
5973 vmcs_writel(GUEST_DR7, val);
5974}
5975
Avi Kivity851ba692009-08-24 11:10:17 +03005976static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005977{
Kyle Huey6a908b62016-11-29 12:40:37 -08005978 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005979}
5980
Avi Kivity851ba692009-08-24 11:10:17 +03005981static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005982{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005983 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005984 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005985
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005986 msr_info.index = ecx;
5987 msr_info.host_initiated = false;
5988 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005989 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005990 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005991 return 1;
5992 }
5993
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005994 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005995
Avi Kivity6aa8b732006-12-10 02:21:36 -08005996 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005997 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5998 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005999 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006000}
6001
Avi Kivity851ba692009-08-24 11:10:17 +03006002static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006003{
Will Auld8fe8ab42012-11-29 12:42:12 -08006004 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006005 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
6006 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
6007 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006008
Will Auld8fe8ab42012-11-29 12:42:12 -08006009 msr.data = data;
6010 msr.index = ecx;
6011 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03006012 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02006013 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02006014 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006015 return 1;
6016 }
6017
Avi Kivity59200272010-01-25 19:47:02 +02006018 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006019 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006020}
6021
Avi Kivity851ba692009-08-24 11:10:17 +03006022static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006023{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006024 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006025 return 1;
6026}
6027
Avi Kivity851ba692009-08-24 11:10:17 +03006028static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006029{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006030 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6031 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006032
Avi Kivity3842d132010-07-27 12:30:24 +03006033 kvm_make_request(KVM_REQ_EVENT, vcpu);
6034
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006035 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006036 return 1;
6037}
6038
Avi Kivity851ba692009-08-24 11:10:17 +03006039static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006040{
Avi Kivityd3bef152007-06-05 15:53:05 +03006041 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006042}
6043
Avi Kivity851ba692009-08-24 11:10:17 +03006044static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006045{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006046 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006047}
6048
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006049static int handle_invd(struct kvm_vcpu *vcpu)
6050{
Andre Przywara51d8b662010-12-21 11:12:02 +01006051 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006052}
6053
Avi Kivity851ba692009-08-24 11:10:17 +03006054static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006055{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006056 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006057
6058 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006059 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006060}
6061
Avi Kivityfee84b02011-11-10 14:57:25 +02006062static int handle_rdpmc(struct kvm_vcpu *vcpu)
6063{
6064 int err;
6065
6066 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006067 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006068}
6069
Avi Kivity851ba692009-08-24 11:10:17 +03006070static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006071{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006072 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006073}
6074
Dexuan Cui2acf9232010-06-10 11:27:12 +08006075static int handle_xsetbv(struct kvm_vcpu *vcpu)
6076{
6077 u64 new_bv = kvm_read_edx_eax(vcpu);
6078 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6079
6080 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006081 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006082 return 1;
6083}
6084
Wanpeng Lif53cd632014-12-02 19:14:58 +08006085static int handle_xsaves(struct kvm_vcpu *vcpu)
6086{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006087 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006088 WARN(1, "this should never happen\n");
6089 return 1;
6090}
6091
6092static int handle_xrstors(struct kvm_vcpu *vcpu)
6093{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006094 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006095 WARN(1, "this should never happen\n");
6096 return 1;
6097}
6098
Avi Kivity851ba692009-08-24 11:10:17 +03006099static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006100{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006101 if (likely(fasteoi)) {
6102 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6103 int access_type, offset;
6104
6105 access_type = exit_qualification & APIC_ACCESS_TYPE;
6106 offset = exit_qualification & APIC_ACCESS_OFFSET;
6107 /*
6108 * Sane guest uses MOV to write EOI, with written value
6109 * not cared. So make a short-circuit here by avoiding
6110 * heavy instruction emulation.
6111 */
6112 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6113 (offset == APIC_EOI)) {
6114 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006115 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006116 }
6117 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006118 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006119}
6120
Yang Zhangc7c9c562013-01-25 10:18:51 +08006121static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6122{
6123 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6124 int vector = exit_qualification & 0xff;
6125
6126 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6127 kvm_apic_set_eoi_accelerated(vcpu, vector);
6128 return 1;
6129}
6130
Yang Zhang83d4c282013-01-25 10:18:49 +08006131static int handle_apic_write(struct kvm_vcpu *vcpu)
6132{
6133 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6134 u32 offset = exit_qualification & 0xfff;
6135
6136 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6137 kvm_apic_write_nodecode(vcpu, offset);
6138 return 1;
6139}
6140
Avi Kivity851ba692009-08-24 11:10:17 +03006141static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006142{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006143 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006144 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006145 bool has_error_code = false;
6146 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006147 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006148 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006149
6150 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006151 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006152 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006153
6154 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6155
6156 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006157 if (reason == TASK_SWITCH_GATE && idt_v) {
6158 switch (type) {
6159 case INTR_TYPE_NMI_INTR:
6160 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006161 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006162 break;
6163 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006164 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006165 kvm_clear_interrupt_queue(vcpu);
6166 break;
6167 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006168 if (vmx->idt_vectoring_info &
6169 VECTORING_INFO_DELIVER_CODE_MASK) {
6170 has_error_code = true;
6171 error_code =
6172 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6173 }
6174 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006175 case INTR_TYPE_SOFT_EXCEPTION:
6176 kvm_clear_exception_queue(vcpu);
6177 break;
6178 default:
6179 break;
6180 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006181 }
Izik Eidus37817f22008-03-24 23:14:53 +02006182 tss_selector = exit_qualification;
6183
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006184 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6185 type != INTR_TYPE_EXT_INTR &&
6186 type != INTR_TYPE_NMI_INTR))
6187 skip_emulated_instruction(vcpu);
6188
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006189 if (kvm_task_switch(vcpu, tss_selector,
6190 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6191 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006192 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6193 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6194 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006195 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006196 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006197
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006198 /*
6199 * TODO: What about debug traps on tss switch?
6200 * Are we supposed to inject them and update dr6?
6201 */
6202
6203 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006204}
6205
Avi Kivity851ba692009-08-24 11:10:17 +03006206static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006207{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006208 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006209 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006210 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006211 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006212
Sheng Yangf9c617f2009-03-25 10:08:52 +08006213 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006214
Sheng Yang14394422008-04-28 12:24:45 +08006215 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006216 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006217 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6218 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6219 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006220 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006221 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6222 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006223 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6224 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006225 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006226 }
6227
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006228 /*
6229 * EPT violation happened while executing iret from NMI,
6230 * "blocked by NMI" bit has to be set before next VM entry.
6231 * There are errata that may cause this bit to not be set:
6232 * AAK134, BY25.
6233 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006234 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006235 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006236 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6237
Sheng Yang14394422008-04-28 12:24:45 +08006238 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006239 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006240
Junaid Shahid27959a42016-12-06 16:46:10 -08006241 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006242 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006243 ? PFERR_USER_MASK : 0;
6244 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006245 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006246 ? PFERR_WRITE_MASK : 0;
6247 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006248 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006249 ? PFERR_FETCH_MASK : 0;
6250 /* ept page table entry is present? */
6251 error_code |= (exit_qualification &
6252 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6253 EPT_VIOLATION_EXECUTABLE))
6254 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006255
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01006256 vcpu->arch.gpa_available = true;
Yang Zhang25d92082013-08-06 12:00:32 +03006257 vcpu->arch.exit_qualification = exit_qualification;
6258
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006259 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006260}
6261
Avi Kivity851ba692009-08-24 11:10:17 +03006262static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006263{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006264 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006265 gpa_t gpa;
6266
6267 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006268 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006269 trace_kvm_fast_mmio(gpa);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006270 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006271 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006272
Paolo Bonzini450869d2015-11-04 13:41:21 +01006273 ret = handle_mmio_page_fault(vcpu, gpa, true);
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01006274 vcpu->arch.gpa_available = true;
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006275 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006276 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6277 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006278
6279 if (unlikely(ret == RET_MMIO_PF_INVALID))
6280 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6281
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006282 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006283 return 1;
6284
6285 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006286 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006287
Avi Kivity851ba692009-08-24 11:10:17 +03006288 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6289 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006290
6291 return 0;
6292}
6293
Avi Kivity851ba692009-08-24 11:10:17 +03006294static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006295{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006296 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6297 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006298 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006299 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006300
6301 return 1;
6302}
6303
Mohammed Gamal80ced182009-09-01 12:48:18 +02006304static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006305{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006306 struct vcpu_vmx *vmx = to_vmx(vcpu);
6307 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006308 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006309 u32 cpu_exec_ctrl;
6310 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006311 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006312
6313 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6314 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006315
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006316 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006317 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006318 return handle_interrupt_window(&vmx->vcpu);
6319
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006320 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6321 return 1;
6322
Gleb Natapov991eebf2013-04-11 12:10:51 +03006323 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006324
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006325 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006326 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006327 ret = 0;
6328 goto out;
6329 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006330
Avi Kivityde5f70e2012-06-12 20:22:28 +03006331 if (err != EMULATE_DONE) {
6332 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6333 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6334 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006335 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006336 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006337
Gleb Natapov8d76c492013-05-08 18:38:44 +03006338 if (vcpu->arch.halt_request) {
6339 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006340 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006341 goto out;
6342 }
6343
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006344 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006345 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006346 if (need_resched())
6347 schedule();
6348 }
6349
Mohammed Gamal80ced182009-09-01 12:48:18 +02006350out:
6351 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006352}
6353
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006354static int __grow_ple_window(int val)
6355{
6356 if (ple_window_grow < 1)
6357 return ple_window;
6358
6359 val = min(val, ple_window_actual_max);
6360
6361 if (ple_window_grow < ple_window)
6362 val *= ple_window_grow;
6363 else
6364 val += ple_window_grow;
6365
6366 return val;
6367}
6368
6369static int __shrink_ple_window(int val, int modifier, int minimum)
6370{
6371 if (modifier < 1)
6372 return ple_window;
6373
6374 if (modifier < ple_window)
6375 val /= modifier;
6376 else
6377 val -= modifier;
6378
6379 return max(val, minimum);
6380}
6381
6382static void grow_ple_window(struct kvm_vcpu *vcpu)
6383{
6384 struct vcpu_vmx *vmx = to_vmx(vcpu);
6385 int old = vmx->ple_window;
6386
6387 vmx->ple_window = __grow_ple_window(old);
6388
6389 if (vmx->ple_window != old)
6390 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006391
6392 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006393}
6394
6395static void shrink_ple_window(struct kvm_vcpu *vcpu)
6396{
6397 struct vcpu_vmx *vmx = to_vmx(vcpu);
6398 int old = vmx->ple_window;
6399
6400 vmx->ple_window = __shrink_ple_window(old,
6401 ple_window_shrink, ple_window);
6402
6403 if (vmx->ple_window != old)
6404 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006405
6406 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006407}
6408
6409/*
6410 * ple_window_actual_max is computed to be one grow_ple_window() below
6411 * ple_window_max. (See __grow_ple_window for the reason.)
6412 * This prevents overflows, because ple_window_max is int.
6413 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6414 * this process.
6415 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6416 */
6417static void update_ple_window_actual_max(void)
6418{
6419 ple_window_actual_max =
6420 __shrink_ple_window(max(ple_window_max, ple_window),
6421 ple_window_grow, INT_MIN);
6422}
6423
Feng Wubf9f6ac2015-09-18 22:29:55 +08006424/*
6425 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6426 */
6427static void wakeup_handler(void)
6428{
6429 struct kvm_vcpu *vcpu;
6430 int cpu = smp_processor_id();
6431
6432 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6433 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6434 blocked_vcpu_list) {
6435 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6436
6437 if (pi_test_on(pi_desc) == 1)
6438 kvm_vcpu_kick(vcpu);
6439 }
6440 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6441}
6442
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006443void vmx_enable_tdp(void)
6444{
6445 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6446 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6447 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6448 0ull, VMX_EPT_EXECUTABLE_MASK,
6449 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Junaid Shahid312b6162016-12-21 20:29:29 -08006450 enable_ept_ad_bits ? 0ull : VMX_EPT_RWX_MASK);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006451
6452 ept_set_mmio_spte_mask();
6453 kvm_enable_tdp();
6454}
6455
Tiejun Chenf2c76482014-10-28 10:14:47 +08006456static __init int hardware_setup(void)
6457{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006458 int r = -ENOMEM, i, msr;
6459
6460 rdmsrl_safe(MSR_EFER, &host_efer);
6461
6462 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6463 kvm_define_shared_msr(i, vmx_msr_index[i]);
6464
Radim Krčmář23611332016-09-29 22:41:33 +02006465 for (i = 0; i < VMX_BITMAP_NR; i++) {
6466 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6467 if (!vmx_bitmap[i])
6468 goto out;
6469 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006470
6471 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006472 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6473 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6474
6475 /*
6476 * Allow direct access to the PC debug port (it is often used for I/O
6477 * delays, but the vmexits simply slow things down).
6478 */
6479 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6480 clear_bit(0x80, vmx_io_bitmap_a);
6481
6482 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6483
6484 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6485 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6486
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006487 if (setup_vmcs_config(&vmcs_config) < 0) {
6488 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006489 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006490 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006491
6492 if (boot_cpu_has(X86_FEATURE_NX))
6493 kvm_enable_efer_bits(EFER_NX);
6494
Wanpeng Li08d839c2017-03-23 05:30:08 -07006495 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6496 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006497 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006498
Tiejun Chenf2c76482014-10-28 10:14:47 +08006499 if (!cpu_has_vmx_shadow_vmcs())
6500 enable_shadow_vmcs = 0;
6501 if (enable_shadow_vmcs)
6502 init_vmcs_shadow_fields();
6503
6504 if (!cpu_has_vmx_ept() ||
6505 !cpu_has_vmx_ept_4levels()) {
6506 enable_ept = 0;
6507 enable_unrestricted_guest = 0;
6508 enable_ept_ad_bits = 0;
6509 }
6510
6511 if (!cpu_has_vmx_ept_ad_bits())
6512 enable_ept_ad_bits = 0;
6513
6514 if (!cpu_has_vmx_unrestricted_guest())
6515 enable_unrestricted_guest = 0;
6516
Paolo Bonziniad15a292015-01-30 16:18:49 +01006517 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006518 flexpriority_enabled = 0;
6519
Paolo Bonziniad15a292015-01-30 16:18:49 +01006520 /*
6521 * set_apic_access_page_addr() is used to reload apic access
6522 * page upon invalidation. No need to do anything if not
6523 * using the APIC_ACCESS_ADDR VMCS field.
6524 */
6525 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006526 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006527
6528 if (!cpu_has_vmx_tpr_shadow())
6529 kvm_x86_ops->update_cr8_intercept = NULL;
6530
6531 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6532 kvm_disable_largepages();
6533
6534 if (!cpu_has_vmx_ple())
6535 ple_gap = 0;
6536
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006537 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006538 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006539 kvm_x86_ops->sync_pir_to_irr = NULL;
6540 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006541
Haozhong Zhang64903d62015-10-20 15:39:09 +08006542 if (cpu_has_vmx_tsc_scaling()) {
6543 kvm_has_tsc_control = true;
6544 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6545 kvm_tsc_scaling_ratio_frac_bits = 48;
6546 }
6547
Tiejun Chenbaa03522014-12-23 16:21:11 +08006548 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6549 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6550 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6551 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6552 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6553 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6554 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6555
Wanpeng Lic63e4562016-09-23 19:17:16 +08006556 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6557 vmx_msr_bitmap_legacy, PAGE_SIZE);
6558 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6559 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006560 memcpy(vmx_msr_bitmap_legacy_x2apic,
6561 vmx_msr_bitmap_legacy, PAGE_SIZE);
6562 memcpy(vmx_msr_bitmap_longmode_x2apic,
6563 vmx_msr_bitmap_longmode, PAGE_SIZE);
6564
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006565 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6566
Radim Krčmář40d83382016-09-29 22:41:31 +02006567 for (msr = 0x800; msr <= 0x8ff; msr++) {
6568 if (msr == 0x839 /* TMCCT */)
6569 continue;
Radim Krčmář2e69f862016-09-29 22:41:32 +02006570 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
Radim Krčmář40d83382016-09-29 22:41:31 +02006571 }
Tiejun Chenbaa03522014-12-23 16:21:11 +08006572
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006573 /*
Radim Krčmář2e69f862016-09-29 22:41:32 +02006574 * TPR reads and writes can be virtualized even if virtual interrupt
6575 * delivery is not in use.
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006576 */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006577 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true);
6578 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false);
6579
Roman Kagan3ce424e2016-05-18 17:48:20 +03006580 /* EOI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006581 vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006582 /* SELF-IPI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006583 vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006584
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006585 if (enable_ept)
6586 vmx_enable_tdp();
6587 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08006588 kvm_disable_tdp();
6589
6590 update_ple_window_actual_max();
6591
Kai Huang843e4332015-01-28 10:54:28 +08006592 /*
6593 * Only enable PML when hardware supports PML feature, and both EPT
6594 * and EPT A/D bit features are enabled -- PML depends on them to work.
6595 */
6596 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6597 enable_pml = 0;
6598
6599 if (!enable_pml) {
6600 kvm_x86_ops->slot_enable_log_dirty = NULL;
6601 kvm_x86_ops->slot_disable_log_dirty = NULL;
6602 kvm_x86_ops->flush_log_dirty = NULL;
6603 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6604 }
6605
Yunhong Jiang64672c92016-06-13 14:19:59 -07006606 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6607 u64 vmx_msr;
6608
6609 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6610 cpu_preemption_timer_multi =
6611 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6612 } else {
6613 kvm_x86_ops->set_hv_timer = NULL;
6614 kvm_x86_ops->cancel_hv_timer = NULL;
6615 }
6616
Feng Wubf9f6ac2015-09-18 22:29:55 +08006617 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6618
Ashok Rajc45dcc72016-06-22 14:59:56 +08006619 kvm_mce_cap_supported |= MCG_LMCE_P;
6620
Tiejun Chenf2c76482014-10-28 10:14:47 +08006621 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006622
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006623out:
Radim Krčmář23611332016-09-29 22:41:33 +02006624 for (i = 0; i < VMX_BITMAP_NR; i++)
6625 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006626
6627 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006628}
6629
6630static __exit void hardware_unsetup(void)
6631{
Radim Krčmář23611332016-09-29 22:41:33 +02006632 int i;
6633
6634 for (i = 0; i < VMX_BITMAP_NR; i++)
6635 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006636
Tiejun Chenf2c76482014-10-28 10:14:47 +08006637 free_kvm_area();
6638}
6639
Avi Kivity6aa8b732006-12-10 02:21:36 -08006640/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006641 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6642 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6643 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006644static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006645{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006646 if (ple_gap)
6647 grow_ple_window(vcpu);
6648
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006649 kvm_vcpu_on_spin(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006650 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006651}
6652
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006653static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006654{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006655 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006656}
6657
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006658static int handle_mwait(struct kvm_vcpu *vcpu)
6659{
6660 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6661 return handle_nop(vcpu);
6662}
6663
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006664static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6665{
6666 return 1;
6667}
6668
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006669static int handle_monitor(struct kvm_vcpu *vcpu)
6670{
6671 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6672 return handle_nop(vcpu);
6673}
6674
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006675/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006676 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6677 * We could reuse a single VMCS for all the L2 guests, but we also want the
6678 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6679 * allows keeping them loaded on the processor, and in the future will allow
6680 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6681 * every entry if they never change.
6682 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6683 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6684 *
6685 * The following functions allocate and free a vmcs02 in this pool.
6686 */
6687
6688/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6689static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6690{
6691 struct vmcs02_list *item;
6692 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6693 if (item->vmptr == vmx->nested.current_vmptr) {
6694 list_move(&item->list, &vmx->nested.vmcs02_pool);
6695 return &item->vmcs02;
6696 }
6697
6698 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6699 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08006700 item = list_last_entry(&vmx->nested.vmcs02_pool,
6701 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006702 item->vmptr = vmx->nested.current_vmptr;
6703 list_move(&item->list, &vmx->nested.vmcs02_pool);
6704 return &item->vmcs02;
6705 }
6706
6707 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006708 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006709 if (!item)
6710 return NULL;
6711 item->vmcs02.vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07006712 item->vmcs02.shadow_vmcs = NULL;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006713 if (!item->vmcs02.vmcs) {
6714 kfree(item);
6715 return NULL;
6716 }
6717 loaded_vmcs_init(&item->vmcs02);
6718 item->vmptr = vmx->nested.current_vmptr;
6719 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6720 vmx->nested.vmcs02_num++;
6721 return &item->vmcs02;
6722}
6723
6724/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6725static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6726{
6727 struct vmcs02_list *item;
6728 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6729 if (item->vmptr == vmptr) {
6730 free_loaded_vmcs(&item->vmcs02);
6731 list_del(&item->list);
6732 kfree(item);
6733 vmx->nested.vmcs02_num--;
6734 return;
6735 }
6736}
6737
6738/*
6739 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006740 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6741 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006742 */
6743static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6744{
6745 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006746
6747 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006748 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006749 /*
6750 * Something will leak if the above WARN triggers. Better than
6751 * a use-after-free.
6752 */
6753 if (vmx->loaded_vmcs == &item->vmcs02)
6754 continue;
6755
6756 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006757 list_del(&item->list);
6758 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006759 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006760 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006761}
6762
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006763/*
6764 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6765 * set the success or error code of an emulated VMX instruction, as specified
6766 * by Vol 2B, VMX Instruction Reference, "Conventions".
6767 */
6768static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6769{
6770 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6771 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6772 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6773}
6774
6775static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6776{
6777 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6778 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6779 X86_EFLAGS_SF | X86_EFLAGS_OF))
6780 | X86_EFLAGS_CF);
6781}
6782
Abel Gordon145c28d2013-04-18 14:36:55 +03006783static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006784 u32 vm_instruction_error)
6785{
6786 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6787 /*
6788 * failValid writes the error number to the current VMCS, which
6789 * can't be done there isn't a current VMCS.
6790 */
6791 nested_vmx_failInvalid(vcpu);
6792 return;
6793 }
6794 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6795 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6796 X86_EFLAGS_SF | X86_EFLAGS_OF))
6797 | X86_EFLAGS_ZF);
6798 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6799 /*
6800 * We don't need to force a shadow sync because
6801 * VM_INSTRUCTION_ERROR is not shadowed
6802 */
6803}
Abel Gordon145c28d2013-04-18 14:36:55 +03006804
Wincy Vanff651cb2014-12-11 08:52:58 +03006805static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6806{
6807 /* TODO: not to reset guest simply here. */
6808 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006809 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006810}
6811
Jan Kiszkaf4124502014-03-07 20:03:13 +01006812static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6813{
6814 struct vcpu_vmx *vmx =
6815 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6816
6817 vmx->nested.preemption_timer_expired = true;
6818 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6819 kvm_vcpu_kick(&vmx->vcpu);
6820
6821 return HRTIMER_NORESTART;
6822}
6823
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006824/*
Bandan Das19677e32014-05-06 02:19:15 -04006825 * Decode the memory-address operand of a vmx instruction, as recorded on an
6826 * exit caused by such an instruction (run by a guest hypervisor).
6827 * On success, returns 0. When the operand is invalid, returns 1 and throws
6828 * #UD or #GP.
6829 */
6830static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6831 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006832 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006833{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006834 gva_t off;
6835 bool exn;
6836 struct kvm_segment s;
6837
Bandan Das19677e32014-05-06 02:19:15 -04006838 /*
6839 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6840 * Execution", on an exit, vmx_instruction_info holds most of the
6841 * addressing components of the operand. Only the displacement part
6842 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6843 * For how an actual address is calculated from all these components,
6844 * refer to Vol. 1, "Operand Addressing".
6845 */
6846 int scaling = vmx_instruction_info & 3;
6847 int addr_size = (vmx_instruction_info >> 7) & 7;
6848 bool is_reg = vmx_instruction_info & (1u << 10);
6849 int seg_reg = (vmx_instruction_info >> 15) & 7;
6850 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6851 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6852 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6853 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6854
6855 if (is_reg) {
6856 kvm_queue_exception(vcpu, UD_VECTOR);
6857 return 1;
6858 }
6859
6860 /* Addr = segment_base + offset */
6861 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006862 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006863 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006864 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006865 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006866 off += kvm_register_read(vcpu, index_reg)<<scaling;
6867 vmx_get_segment(vcpu, &s, seg_reg);
6868 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006869
6870 if (addr_size == 1) /* 32 bit */
6871 *ret &= 0xffffffff;
6872
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006873 /* Checks for #GP/#SS exceptions. */
6874 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006875 if (is_long_mode(vcpu)) {
6876 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6877 * non-canonical form. This is the only check on the memory
6878 * destination for long mode!
6879 */
6880 exn = is_noncanonical_address(*ret);
6881 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006882 /* Protected mode: apply checks for segment validity in the
6883 * following order:
6884 * - segment type check (#GP(0) may be thrown)
6885 * - usability check (#GP(0)/#SS(0))
6886 * - limit check (#GP(0)/#SS(0))
6887 */
6888 if (wr)
6889 /* #GP(0) if the destination operand is located in a
6890 * read-only data segment or any code segment.
6891 */
6892 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6893 else
6894 /* #GP(0) if the source operand is located in an
6895 * execute-only code segment
6896 */
6897 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006898 if (exn) {
6899 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6900 return 1;
6901 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006902 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6903 */
6904 exn = (s.unusable != 0);
6905 /* Protected mode: #GP(0)/#SS(0) if the memory
6906 * operand is outside the segment limit.
6907 */
6908 exn = exn || (off + sizeof(u64) > s.limit);
6909 }
6910 if (exn) {
6911 kvm_queue_exception_e(vcpu,
6912 seg_reg == VCPU_SREG_SS ?
6913 SS_VECTOR : GP_VECTOR,
6914 0);
6915 return 1;
6916 }
6917
Bandan Das19677e32014-05-06 02:19:15 -04006918 return 0;
6919}
6920
6921/*
Bandan Das3573e222014-05-06 02:19:16 -04006922 * This function performs the various checks including
6923 * - if it's 4KB aligned
6924 * - No bits beyond the physical address width are set
6925 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006926 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006927 */
Bandan Das4291b582014-05-06 02:19:18 -04006928static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6929 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006930{
6931 gva_t gva;
6932 gpa_t vmptr;
6933 struct x86_exception e;
6934 struct page *page;
6935 struct vcpu_vmx *vmx = to_vmx(vcpu);
6936 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6937
6938 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006939 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006940 return 1;
6941
6942 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6943 sizeof(vmptr), &e)) {
6944 kvm_inject_page_fault(vcpu, &e);
6945 return 1;
6946 }
6947
6948 switch (exit_reason) {
6949 case EXIT_REASON_VMON:
6950 /*
6951 * SDM 3: 24.11.5
6952 * The first 4 bytes of VMXON region contain the supported
6953 * VMCS revision identifier
6954 *
6955 * Note - IA32_VMX_BASIC[48] will never be 1
6956 * for the nested case;
6957 * which replaces physical address width with 32
6958 *
6959 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006960 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006961 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006962 return kvm_skip_emulated_instruction(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04006963 }
6964
6965 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini06ce5212017-01-24 11:56:21 +01006966 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04006967 nested_vmx_failInvalid(vcpu);
Paolo Bonzini06ce5212017-01-24 11:56:21 +01006968 return kvm_skip_emulated_instruction(vcpu);
6969 }
6970 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04006971 kunmap(page);
Paolo Bonzini06ce5212017-01-24 11:56:21 +01006972 nested_release_page_clean(page);
6973 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006974 return kvm_skip_emulated_instruction(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04006975 }
6976 kunmap(page);
Paolo Bonzini06ce5212017-01-24 11:56:21 +01006977 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04006978 vmx->nested.vmxon_ptr = vmptr;
6979 break;
Bandan Das4291b582014-05-06 02:19:18 -04006980 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006981 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006982 nested_vmx_failValid(vcpu,
6983 VMXERR_VMCLEAR_INVALID_ADDRESS);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006984 return kvm_skip_emulated_instruction(vcpu);
Bandan Das4291b582014-05-06 02:19:18 -04006985 }
Bandan Das3573e222014-05-06 02:19:16 -04006986
Bandan Das4291b582014-05-06 02:19:18 -04006987 if (vmptr == vmx->nested.vmxon_ptr) {
6988 nested_vmx_failValid(vcpu,
6989 VMXERR_VMCLEAR_VMXON_POINTER);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006990 return kvm_skip_emulated_instruction(vcpu);
Bandan Das4291b582014-05-06 02:19:18 -04006991 }
6992 break;
6993 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006994 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006995 nested_vmx_failValid(vcpu,
6996 VMXERR_VMPTRLD_INVALID_ADDRESS);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006997 return kvm_skip_emulated_instruction(vcpu);
Bandan Das4291b582014-05-06 02:19:18 -04006998 }
6999
7000 if (vmptr == vmx->nested.vmxon_ptr) {
7001 nested_vmx_failValid(vcpu,
GanShun37b9a672016-11-30 10:28:19 -08007002 VMXERR_VMPTRLD_VMXON_POINTER);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007003 return kvm_skip_emulated_instruction(vcpu);
Bandan Das4291b582014-05-06 02:19:18 -04007004 }
7005 break;
Bandan Das3573e222014-05-06 02:19:16 -04007006 default:
7007 return 1; /* shouldn't happen */
7008 }
7009
Bandan Das4291b582014-05-06 02:19:18 -04007010 if (vmpointer)
7011 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04007012 return 0;
7013}
7014
Jim Mattsone29acc52016-11-30 12:03:43 -08007015static int enter_vmx_operation(struct kvm_vcpu *vcpu)
7016{
7017 struct vcpu_vmx *vmx = to_vmx(vcpu);
7018 struct vmcs *shadow_vmcs;
7019
7020 if (cpu_has_vmx_msr_bitmap()) {
7021 vmx->nested.msr_bitmap =
7022 (unsigned long *)__get_free_page(GFP_KERNEL);
7023 if (!vmx->nested.msr_bitmap)
7024 goto out_msr_bitmap;
7025 }
7026
7027 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7028 if (!vmx->nested.cached_vmcs12)
7029 goto out_cached_vmcs12;
7030
7031 if (enable_shadow_vmcs) {
7032 shadow_vmcs = alloc_vmcs();
7033 if (!shadow_vmcs)
7034 goto out_shadow_vmcs;
7035 /* mark vmcs as shadow */
7036 shadow_vmcs->revision_id |= (1u << 31);
7037 /* init shadow vmcs */
7038 vmcs_clear(shadow_vmcs);
7039 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7040 }
7041
7042 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7043 vmx->nested.vmcs02_num = 0;
7044
7045 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7046 HRTIMER_MODE_REL_PINNED);
7047 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7048
7049 vmx->nested.vmxon = true;
7050 return 0;
7051
7052out_shadow_vmcs:
7053 kfree(vmx->nested.cached_vmcs12);
7054
7055out_cached_vmcs12:
7056 free_page((unsigned long)vmx->nested.msr_bitmap);
7057
7058out_msr_bitmap:
7059 return -ENOMEM;
7060}
7061
Bandan Das3573e222014-05-06 02:19:16 -04007062/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007063 * Emulate the VMXON instruction.
7064 * Currently, we just remember that VMX is active, and do not save or even
7065 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7066 * do not currently need to store anything in that guest-allocated memory
7067 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7068 * argument is different from the VMXON pointer (which the spec says they do).
7069 */
7070static int handle_vmon(struct kvm_vcpu *vcpu)
7071{
Jim Mattsone29acc52016-11-30 12:03:43 -08007072 int ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007073 struct kvm_segment cs;
7074 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007075 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7076 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007077
7078 /* The Intel VMX Instruction Reference lists a bunch of bits that
7079 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7080 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7081 * Otherwise, we should fail with #UD. We test these now:
7082 */
7083 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7084 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7085 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7086 kvm_queue_exception(vcpu, UD_VECTOR);
7087 return 1;
7088 }
7089
7090 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7091 if (is_long_mode(vcpu) && !cs.l) {
7092 kvm_queue_exception(vcpu, UD_VECTOR);
7093 return 1;
7094 }
7095
7096 if (vmx_get_cpl(vcpu)) {
7097 kvm_inject_gp(vcpu, 0);
7098 return 1;
7099 }
Bandan Das3573e222014-05-06 02:19:16 -04007100
Abel Gordon145c28d2013-04-18 14:36:55 +03007101 if (vmx->nested.vmxon) {
7102 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007103 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007104 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007105
Haozhong Zhang3b840802016-06-22 14:59:54 +08007106 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007107 != VMXON_NEEDED_FEATURES) {
7108 kvm_inject_gp(vcpu, 0);
7109 return 1;
7110 }
7111
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007112 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
7113 return 1;
Jim Mattsone29acc52016-11-30 12:03:43 -08007114
7115 ret = enter_vmx_operation(vcpu);
7116 if (ret)
7117 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007118
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007119 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007120 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007121}
7122
7123/*
7124 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7125 * for running VMX instructions (except VMXON, whose prerequisites are
7126 * slightly different). It also specifies what exception to inject otherwise.
7127 */
7128static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7129{
7130 struct kvm_segment cs;
7131 struct vcpu_vmx *vmx = to_vmx(vcpu);
7132
7133 if (!vmx->nested.vmxon) {
7134 kvm_queue_exception(vcpu, UD_VECTOR);
7135 return 0;
7136 }
7137
7138 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7139 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7140 (is_long_mode(vcpu) && !cs.l)) {
7141 kvm_queue_exception(vcpu, UD_VECTOR);
7142 return 0;
7143 }
7144
7145 if (vmx_get_cpl(vcpu)) {
7146 kvm_inject_gp(vcpu, 0);
7147 return 0;
7148 }
7149
7150 return 1;
7151}
7152
Abel Gordone7953d72013-04-18 14:37:55 +03007153static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7154{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007155 if (vmx->nested.current_vmptr == -1ull)
7156 return;
7157
7158 /* current_vmptr and current_vmcs12 are always set/reset together */
7159 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7160 return;
7161
Abel Gordon012f83c2013-04-18 14:39:25 +03007162 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007163 /* copy to memory all shadowed fields in case
7164 they were modified */
7165 copy_shadow_to_vmcs12(vmx);
7166 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007167 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7168 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007169 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007170 }
Wincy Van705699a2015-02-03 23:58:17 +08007171 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007172
7173 /* Flush VMCS12 to guest memory */
7174 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7175 VMCS12_SIZE);
7176
Abel Gordone7953d72013-04-18 14:37:55 +03007177 kunmap(vmx->nested.current_vmcs12_page);
7178 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007179 vmx->nested.current_vmptr = -1ull;
7180 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007181}
7182
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007183/*
7184 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7185 * just stops using VMX.
7186 */
7187static void free_nested(struct vcpu_vmx *vmx)
7188{
7189 if (!vmx->nested.vmxon)
7190 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007191
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007192 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007193 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007194 nested_release_vmcs12(vmx);
Radim Krčmářd048c092016-08-08 20:16:22 +02007195 if (vmx->nested.msr_bitmap) {
7196 free_page((unsigned long)vmx->nested.msr_bitmap);
7197 vmx->nested.msr_bitmap = NULL;
7198 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007199 if (enable_shadow_vmcs) {
7200 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7201 free_vmcs(vmx->vmcs01.shadow_vmcs);
7202 vmx->vmcs01.shadow_vmcs = NULL;
7203 }
David Matlack4f2777b2016-07-13 17:16:37 -07007204 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007205 /* Unpin physical memory we referred to in current vmcs02 */
7206 if (vmx->nested.apic_access_page) {
7207 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007208 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007209 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007210 if (vmx->nested.virtual_apic_page) {
7211 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007212 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007213 }
Wincy Van705699a2015-02-03 23:58:17 +08007214 if (vmx->nested.pi_desc_page) {
7215 kunmap(vmx->nested.pi_desc_page);
7216 nested_release_page(vmx->nested.pi_desc_page);
7217 vmx->nested.pi_desc_page = NULL;
7218 vmx->nested.pi_desc = NULL;
7219 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007220
7221 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007222}
7223
7224/* Emulate the VMXOFF instruction */
7225static int handle_vmoff(struct kvm_vcpu *vcpu)
7226{
7227 if (!nested_vmx_check_permission(vcpu))
7228 return 1;
7229 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007230 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007231 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007232}
7233
Nadav Har'El27d6c862011-05-25 23:06:59 +03007234/* Emulate the VMCLEAR instruction */
7235static int handle_vmclear(struct kvm_vcpu *vcpu)
7236{
7237 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007238 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007239 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007240
7241 if (!nested_vmx_check_permission(vcpu))
7242 return 1;
7243
Bandan Das4291b582014-05-06 02:19:18 -04007244 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007245 return 1;
7246
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007247 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007248 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007249
Jim Mattson587d7e722017-03-02 12:41:48 -08007250 kvm_vcpu_write_guest(vcpu,
7251 vmptr + offsetof(struct vmcs12, launch_state),
7252 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007253
7254 nested_free_vmcs02(vmx, vmptr);
7255
Nadav Har'El27d6c862011-05-25 23:06:59 +03007256 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007257 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007258}
7259
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007260static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7261
7262/* Emulate the VMLAUNCH instruction */
7263static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7264{
7265 return nested_vmx_run(vcpu, true);
7266}
7267
7268/* Emulate the VMRESUME instruction */
7269static int handle_vmresume(struct kvm_vcpu *vcpu)
7270{
7271
7272 return nested_vmx_run(vcpu, false);
7273}
7274
Nadav Har'El49f705c2011-05-25 23:08:30 +03007275enum vmcs_field_type {
7276 VMCS_FIELD_TYPE_U16 = 0,
7277 VMCS_FIELD_TYPE_U64 = 1,
7278 VMCS_FIELD_TYPE_U32 = 2,
7279 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7280};
7281
7282static inline int vmcs_field_type(unsigned long field)
7283{
7284 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7285 return VMCS_FIELD_TYPE_U32;
7286 return (field >> 13) & 0x3 ;
7287}
7288
7289static inline int vmcs_field_readonly(unsigned long field)
7290{
7291 return (((field >> 10) & 0x3) == 1);
7292}
7293
7294/*
7295 * Read a vmcs12 field. Since these can have varying lengths and we return
7296 * one type, we chose the biggest type (u64) and zero-extend the return value
7297 * to that size. Note that the caller, handle_vmread, might need to use only
7298 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7299 * 64-bit fields are to be returned).
7300 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007301static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7302 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007303{
7304 short offset = vmcs_field_to_offset(field);
7305 char *p;
7306
7307 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007308 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007309
7310 p = ((char *)(get_vmcs12(vcpu))) + offset;
7311
7312 switch (vmcs_field_type(field)) {
7313 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7314 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007315 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007316 case VMCS_FIELD_TYPE_U16:
7317 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007318 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007319 case VMCS_FIELD_TYPE_U32:
7320 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007321 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007322 case VMCS_FIELD_TYPE_U64:
7323 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007324 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007325 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007326 WARN_ON(1);
7327 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007328 }
7329}
7330
Abel Gordon20b97fe2013-04-18 14:36:25 +03007331
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007332static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7333 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007334 short offset = vmcs_field_to_offset(field);
7335 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7336 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007337 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007338
7339 switch (vmcs_field_type(field)) {
7340 case VMCS_FIELD_TYPE_U16:
7341 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007342 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007343 case VMCS_FIELD_TYPE_U32:
7344 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007345 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007346 case VMCS_FIELD_TYPE_U64:
7347 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007348 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007349 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7350 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007351 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007352 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007353 WARN_ON(1);
7354 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007355 }
7356
7357}
7358
Abel Gordon16f5b902013-04-18 14:38:25 +03007359static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7360{
7361 int i;
7362 unsigned long field;
7363 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007364 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007365 const unsigned long *fields = shadow_read_write_fields;
7366 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007367
Jan Kiszka282da872014-10-08 18:05:39 +02007368 preempt_disable();
7369
Abel Gordon16f5b902013-04-18 14:38:25 +03007370 vmcs_load(shadow_vmcs);
7371
7372 for (i = 0; i < num_fields; i++) {
7373 field = fields[i];
7374 switch (vmcs_field_type(field)) {
7375 case VMCS_FIELD_TYPE_U16:
7376 field_value = vmcs_read16(field);
7377 break;
7378 case VMCS_FIELD_TYPE_U32:
7379 field_value = vmcs_read32(field);
7380 break;
7381 case VMCS_FIELD_TYPE_U64:
7382 field_value = vmcs_read64(field);
7383 break;
7384 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7385 field_value = vmcs_readl(field);
7386 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007387 default:
7388 WARN_ON(1);
7389 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007390 }
7391 vmcs12_write_any(&vmx->vcpu, field, field_value);
7392 }
7393
7394 vmcs_clear(shadow_vmcs);
7395 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007396
7397 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007398}
7399
Abel Gordonc3114422013-04-18 14:38:55 +03007400static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7401{
Mathias Krausec2bae892013-06-26 20:36:21 +02007402 const unsigned long *fields[] = {
7403 shadow_read_write_fields,
7404 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007405 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007406 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007407 max_shadow_read_write_fields,
7408 max_shadow_read_only_fields
7409 };
7410 int i, q;
7411 unsigned long field;
7412 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007413 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007414
7415 vmcs_load(shadow_vmcs);
7416
Mathias Krausec2bae892013-06-26 20:36:21 +02007417 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007418 for (i = 0; i < max_fields[q]; i++) {
7419 field = fields[q][i];
7420 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7421
7422 switch (vmcs_field_type(field)) {
7423 case VMCS_FIELD_TYPE_U16:
7424 vmcs_write16(field, (u16)field_value);
7425 break;
7426 case VMCS_FIELD_TYPE_U32:
7427 vmcs_write32(field, (u32)field_value);
7428 break;
7429 case VMCS_FIELD_TYPE_U64:
7430 vmcs_write64(field, (u64)field_value);
7431 break;
7432 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7433 vmcs_writel(field, (long)field_value);
7434 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007435 default:
7436 WARN_ON(1);
7437 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007438 }
7439 }
7440 }
7441
7442 vmcs_clear(shadow_vmcs);
7443 vmcs_load(vmx->loaded_vmcs->vmcs);
7444}
7445
Nadav Har'El49f705c2011-05-25 23:08:30 +03007446/*
7447 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7448 * used before) all generate the same failure when it is missing.
7449 */
7450static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7451{
7452 struct vcpu_vmx *vmx = to_vmx(vcpu);
7453 if (vmx->nested.current_vmptr == -1ull) {
7454 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007455 return 0;
7456 }
7457 return 1;
7458}
7459
7460static int handle_vmread(struct kvm_vcpu *vcpu)
7461{
7462 unsigned long field;
7463 u64 field_value;
7464 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7465 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7466 gva_t gva = 0;
7467
Kyle Hueyeb277562016-11-29 12:40:39 -08007468 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007469 return 1;
7470
Kyle Huey6affcbe2016-11-29 12:40:40 -08007471 if (!nested_vmx_check_vmcs12(vcpu))
7472 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007473
Nadav Har'El49f705c2011-05-25 23:08:30 +03007474 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007475 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007476 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007477 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007478 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007479 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007480 }
7481 /*
7482 * Now copy part of this value to register or memory, as requested.
7483 * Note that the number of bits actually copied is 32 or 64 depending
7484 * on the guest's mode (32 or 64 bit), not on the given field's length.
7485 */
7486 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007487 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007488 field_value);
7489 } else {
7490 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007491 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007492 return 1;
7493 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7494 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7495 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7496 }
7497
7498 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007499 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007500}
7501
7502
7503static int handle_vmwrite(struct kvm_vcpu *vcpu)
7504{
7505 unsigned long field;
7506 gva_t gva;
7507 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7508 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007509 /* The value to write might be 32 or 64 bits, depending on L1's long
7510 * mode, and eventually we need to write that into a field of several
7511 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007512 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007513 * bits into the vmcs12 field.
7514 */
7515 u64 field_value = 0;
7516 struct x86_exception e;
7517
Kyle Hueyeb277562016-11-29 12:40:39 -08007518 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007519 return 1;
7520
Kyle Huey6affcbe2016-11-29 12:40:40 -08007521 if (!nested_vmx_check_vmcs12(vcpu))
7522 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007523
Nadav Har'El49f705c2011-05-25 23:08:30 +03007524 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007525 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007526 (((vmx_instruction_info) >> 3) & 0xf));
7527 else {
7528 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007529 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007530 return 1;
7531 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007532 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007533 kvm_inject_page_fault(vcpu, &e);
7534 return 1;
7535 }
7536 }
7537
7538
Nadav Amit27e6fb52014-06-18 17:19:26 +03007539 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007540 if (vmcs_field_readonly(field)) {
7541 nested_vmx_failValid(vcpu,
7542 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007543 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007544 }
7545
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007546 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007547 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007548 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007549 }
7550
7551 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007552 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007553}
7554
Jim Mattsona8bc2842016-11-30 12:03:44 -08007555static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7556{
7557 vmx->nested.current_vmptr = vmptr;
7558 if (enable_shadow_vmcs) {
7559 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7560 SECONDARY_EXEC_SHADOW_VMCS);
7561 vmcs_write64(VMCS_LINK_POINTER,
7562 __pa(vmx->vmcs01.shadow_vmcs));
7563 vmx->nested.sync_shadow_vmcs = true;
7564 }
7565}
7566
Nadav Har'El63846662011-05-25 23:07:29 +03007567/* Emulate the VMPTRLD instruction */
7568static int handle_vmptrld(struct kvm_vcpu *vcpu)
7569{
7570 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007571 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007572
7573 if (!nested_vmx_check_permission(vcpu))
7574 return 1;
7575
Bandan Das4291b582014-05-06 02:19:18 -04007576 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007577 return 1;
7578
Nadav Har'El63846662011-05-25 23:07:29 +03007579 if (vmx->nested.current_vmptr != vmptr) {
7580 struct vmcs12 *new_vmcs12;
7581 struct page *page;
7582 page = nested_get_page(vcpu, vmptr);
7583 if (page == NULL) {
7584 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007585 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007586 }
7587 new_vmcs12 = kmap(page);
7588 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7589 kunmap(page);
7590 nested_release_page_clean(page);
7591 nested_vmx_failValid(vcpu,
7592 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007593 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007594 }
Nadav Har'El63846662011-05-25 23:07:29 +03007595
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007596 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007597 vmx->nested.current_vmcs12 = new_vmcs12;
7598 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007599 /*
7600 * Load VMCS12 from guest memory since it is not already
7601 * cached.
7602 */
7603 memcpy(vmx->nested.cached_vmcs12,
7604 vmx->nested.current_vmcs12, VMCS12_SIZE);
Jim Mattsona8bc2842016-11-30 12:03:44 -08007605 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007606 }
7607
7608 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007609 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007610}
7611
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007612/* Emulate the VMPTRST instruction */
7613static int handle_vmptrst(struct kvm_vcpu *vcpu)
7614{
7615 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7616 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7617 gva_t vmcs_gva;
7618 struct x86_exception e;
7619
7620 if (!nested_vmx_check_permission(vcpu))
7621 return 1;
7622
7623 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007624 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007625 return 1;
7626 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7627 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7628 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7629 sizeof(u64), &e)) {
7630 kvm_inject_page_fault(vcpu, &e);
7631 return 1;
7632 }
7633 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007634 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007635}
7636
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007637/* Emulate the INVEPT instruction */
7638static int handle_invept(struct kvm_vcpu *vcpu)
7639{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007640 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007641 u32 vmx_instruction_info, types;
7642 unsigned long type;
7643 gva_t gva;
7644 struct x86_exception e;
7645 struct {
7646 u64 eptp, gpa;
7647 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007648
Wincy Vanb9c237b2015-02-03 23:56:30 +08007649 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7650 SECONDARY_EXEC_ENABLE_EPT) ||
7651 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007652 kvm_queue_exception(vcpu, UD_VECTOR);
7653 return 1;
7654 }
7655
7656 if (!nested_vmx_check_permission(vcpu))
7657 return 1;
7658
7659 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7660 kvm_queue_exception(vcpu, UD_VECTOR);
7661 return 1;
7662 }
7663
7664 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007665 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007666
Wincy Vanb9c237b2015-02-03 23:56:30 +08007667 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007668
Jim Mattson85c856b2016-10-26 08:38:38 -07007669 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007670 nested_vmx_failValid(vcpu,
7671 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007672 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007673 }
7674
7675 /* According to the Intel VMX instruction reference, the memory
7676 * operand is read even if it isn't needed (e.g., for type==global)
7677 */
7678 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007679 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007680 return 1;
7681 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7682 sizeof(operand), &e)) {
7683 kvm_inject_page_fault(vcpu, &e);
7684 return 1;
7685 }
7686
7687 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007688 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007689 /*
7690 * TODO: track mappings and invalidate
7691 * single context requests appropriately
7692 */
7693 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007694 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007695 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007696 nested_vmx_succeed(vcpu);
7697 break;
7698 default:
7699 BUG_ON(1);
7700 break;
7701 }
7702
Kyle Huey6affcbe2016-11-29 12:40:40 -08007703 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007704}
7705
Petr Matouseka642fc32014-09-23 20:22:30 +02007706static int handle_invvpid(struct kvm_vcpu *vcpu)
7707{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007708 struct vcpu_vmx *vmx = to_vmx(vcpu);
7709 u32 vmx_instruction_info;
7710 unsigned long type, types;
7711 gva_t gva;
7712 struct x86_exception e;
7713 int vpid;
7714
7715 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7716 SECONDARY_EXEC_ENABLE_VPID) ||
7717 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7718 kvm_queue_exception(vcpu, UD_VECTOR);
7719 return 1;
7720 }
7721
7722 if (!nested_vmx_check_permission(vcpu))
7723 return 1;
7724
7725 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7726 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7727
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007728 types = (vmx->nested.nested_vmx_vpid_caps &
7729 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007730
Jim Mattson85c856b2016-10-26 08:38:38 -07007731 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007732 nested_vmx_failValid(vcpu,
7733 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007734 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007735 }
7736
7737 /* according to the intel vmx instruction reference, the memory
7738 * operand is read even if it isn't needed (e.g., for type==global)
7739 */
7740 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7741 vmx_instruction_info, false, &gva))
7742 return 1;
7743 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7744 sizeof(u32), &e)) {
7745 kvm_inject_page_fault(vcpu, &e);
7746 return 1;
7747 }
7748
7749 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007750 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007751 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007752 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7753 if (!vpid) {
7754 nested_vmx_failValid(vcpu,
7755 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007756 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007757 }
7758 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007759 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007760 break;
7761 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007762 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007763 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007764 }
7765
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007766 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7767 nested_vmx_succeed(vcpu);
7768
Kyle Huey6affcbe2016-11-29 12:40:40 -08007769 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007770}
7771
Kai Huang843e4332015-01-28 10:54:28 +08007772static int handle_pml_full(struct kvm_vcpu *vcpu)
7773{
7774 unsigned long exit_qualification;
7775
7776 trace_kvm_pml_full(vcpu->vcpu_id);
7777
7778 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7779
7780 /*
7781 * PML buffer FULL happened while executing iret from NMI,
7782 * "blocked by NMI" bit has to be set before next VM entry.
7783 */
7784 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Kai Huang843e4332015-01-28 10:54:28 +08007785 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7786 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7787 GUEST_INTR_STATE_NMI);
7788
7789 /*
7790 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7791 * here.., and there's no userspace involvement needed for PML.
7792 */
7793 return 1;
7794}
7795
Yunhong Jiang64672c92016-06-13 14:19:59 -07007796static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7797{
7798 kvm_lapic_expired_hv_timer(vcpu);
7799 return 1;
7800}
7801
Nadav Har'El0140cae2011-05-25 23:06:28 +03007802/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007803 * The exit handlers return 1 if the exit was handled fully and guest execution
7804 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7805 * to be done to userspace and return 0.
7806 */
Mathias Krause772e0312012-08-30 01:30:19 +02007807static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007808 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7809 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007810 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007811 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007812 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007813 [EXIT_REASON_CR_ACCESS] = handle_cr,
7814 [EXIT_REASON_DR_ACCESS] = handle_dr,
7815 [EXIT_REASON_CPUID] = handle_cpuid,
7816 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7817 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7818 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7819 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007820 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007821 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007822 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007823 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007824 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007825 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007826 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007827 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007828 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007829 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007830 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007831 [EXIT_REASON_VMOFF] = handle_vmoff,
7832 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007833 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7834 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007835 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007836 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007837 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007838 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007839 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007840 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007841 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7842 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007843 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007844 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007845 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007846 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007847 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007848 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007849 [EXIT_REASON_XSAVES] = handle_xsaves,
7850 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007851 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007852 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007853};
7854
7855static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007856 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007857
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007858static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7859 struct vmcs12 *vmcs12)
7860{
7861 unsigned long exit_qualification;
7862 gpa_t bitmap, last_bitmap;
7863 unsigned int port;
7864 int size;
7865 u8 b;
7866
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007867 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007868 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007869
7870 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7871
7872 port = exit_qualification >> 16;
7873 size = (exit_qualification & 7) + 1;
7874
7875 last_bitmap = (gpa_t)-1;
7876 b = -1;
7877
7878 while (size > 0) {
7879 if (port < 0x8000)
7880 bitmap = vmcs12->io_bitmap_a;
7881 else if (port < 0x10000)
7882 bitmap = vmcs12->io_bitmap_b;
7883 else
Joe Perches1d804d02015-03-30 16:46:09 -07007884 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007885 bitmap += (port & 0x7fff) / 8;
7886
7887 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007888 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007889 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007890 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007891 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007892
7893 port++;
7894 size--;
7895 last_bitmap = bitmap;
7896 }
7897
Joe Perches1d804d02015-03-30 16:46:09 -07007898 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007899}
7900
Nadav Har'El644d7112011-05-25 23:12:35 +03007901/*
7902 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7903 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7904 * disinterest in the current event (read or write a specific MSR) by using an
7905 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7906 */
7907static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7908 struct vmcs12 *vmcs12, u32 exit_reason)
7909{
7910 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7911 gpa_t bitmap;
7912
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007913 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007914 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007915
7916 /*
7917 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7918 * for the four combinations of read/write and low/high MSR numbers.
7919 * First we need to figure out which of the four to use:
7920 */
7921 bitmap = vmcs12->msr_bitmap;
7922 if (exit_reason == EXIT_REASON_MSR_WRITE)
7923 bitmap += 2048;
7924 if (msr_index >= 0xc0000000) {
7925 msr_index -= 0xc0000000;
7926 bitmap += 1024;
7927 }
7928
7929 /* Then read the msr_index'th bit from this bitmap: */
7930 if (msr_index < 1024*8) {
7931 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007932 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007933 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007934 return 1 & (b >> (msr_index & 7));
7935 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007936 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007937}
7938
7939/*
7940 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7941 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7942 * intercept (via guest_host_mask etc.) the current event.
7943 */
7944static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7945 struct vmcs12 *vmcs12)
7946{
7947 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7948 int cr = exit_qualification & 15;
7949 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007950 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007951
7952 switch ((exit_qualification >> 4) & 3) {
7953 case 0: /* mov to cr */
7954 switch (cr) {
7955 case 0:
7956 if (vmcs12->cr0_guest_host_mask &
7957 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007958 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007959 break;
7960 case 3:
7961 if ((vmcs12->cr3_target_count >= 1 &&
7962 vmcs12->cr3_target_value0 == val) ||
7963 (vmcs12->cr3_target_count >= 2 &&
7964 vmcs12->cr3_target_value1 == val) ||
7965 (vmcs12->cr3_target_count >= 3 &&
7966 vmcs12->cr3_target_value2 == val) ||
7967 (vmcs12->cr3_target_count >= 4 &&
7968 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007969 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007970 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007971 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007972 break;
7973 case 4:
7974 if (vmcs12->cr4_guest_host_mask &
7975 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007976 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007977 break;
7978 case 8:
7979 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007980 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007981 break;
7982 }
7983 break;
7984 case 2: /* clts */
7985 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7986 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007987 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007988 break;
7989 case 1: /* mov from cr */
7990 switch (cr) {
7991 case 3:
7992 if (vmcs12->cpu_based_vm_exec_control &
7993 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007994 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007995 break;
7996 case 8:
7997 if (vmcs12->cpu_based_vm_exec_control &
7998 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007999 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008000 break;
8001 }
8002 break;
8003 case 3: /* lmsw */
8004 /*
8005 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
8006 * cr0. Other attempted changes are ignored, with no exit.
8007 */
8008 if (vmcs12->cr0_guest_host_mask & 0xe &
8009 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07008010 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008011 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8012 !(vmcs12->cr0_read_shadow & 0x1) &&
8013 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008014 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008015 break;
8016 }
Joe Perches1d804d02015-03-30 16:46:09 -07008017 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008018}
8019
8020/*
8021 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8022 * should handle it ourselves in L0 (and then continue L2). Only call this
8023 * when in is_guest_mode (L2).
8024 */
8025static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8026{
Nadav Har'El644d7112011-05-25 23:12:35 +03008027 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8028 struct vcpu_vmx *vmx = to_vmx(vcpu);
8029 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008030 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008031
Jan Kiszka542060e2014-01-04 18:47:21 +01008032 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8033 vmcs_readl(EXIT_QUALIFICATION),
8034 vmx->idt_vectoring_info,
8035 intr_info,
8036 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8037 KVM_ISA_VMX);
8038
Nadav Har'El644d7112011-05-25 23:12:35 +03008039 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008040 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008041
8042 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008043 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8044 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008045 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008046 }
8047
8048 switch (exit_reason) {
8049 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008050 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008051 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008052 else if (is_page_fault(intr_info))
8053 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008054 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008055 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008056 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008057 else if (is_debug(intr_info) &&
8058 vcpu->guest_debug &
8059 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8060 return false;
8061 else if (is_breakpoint(intr_info) &&
8062 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8063 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008064 return vmcs12->exception_bitmap &
8065 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8066 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008067 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008068 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008069 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008070 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008071 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008072 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008073 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008074 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008075 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008076 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008077 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008078 case EXIT_REASON_HLT:
8079 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8080 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008081 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008082 case EXIT_REASON_INVLPG:
8083 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8084 case EXIT_REASON_RDPMC:
8085 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008086 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008087 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8088 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8089 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8090 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8091 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8092 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008093 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008094 /*
8095 * VMX instructions trap unconditionally. This allows L1 to
8096 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8097 */
Joe Perches1d804d02015-03-30 16:46:09 -07008098 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008099 case EXIT_REASON_CR_ACCESS:
8100 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8101 case EXIT_REASON_DR_ACCESS:
8102 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8103 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008104 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008105 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8106 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
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;
Nadav Har'El644d7112011-05-25 23:12:35 +03008163 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008164 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008165 }
8166}
8167
Avi Kivity586f9602010-11-18 13:09:54 +02008168static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8169{
8170 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8171 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8172}
8173
Kai Huanga3eaa862015-11-04 13:46:05 +08008174static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008175{
Kai Huanga3eaa862015-11-04 13:46:05 +08008176 if (vmx->pml_pg) {
8177 __free_page(vmx->pml_pg);
8178 vmx->pml_pg = NULL;
8179 }
Kai Huang843e4332015-01-28 10:54:28 +08008180}
8181
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008182static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008183{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008184 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008185 u64 *pml_buf;
8186 u16 pml_idx;
8187
8188 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8189
8190 /* Do nothing if PML buffer is empty */
8191 if (pml_idx == (PML_ENTITY_NUM - 1))
8192 return;
8193
8194 /* PML index always points to next available PML buffer entity */
8195 if (pml_idx >= PML_ENTITY_NUM)
8196 pml_idx = 0;
8197 else
8198 pml_idx++;
8199
8200 pml_buf = page_address(vmx->pml_pg);
8201 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8202 u64 gpa;
8203
8204 gpa = pml_buf[pml_idx];
8205 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008206 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008207 }
8208
8209 /* reset PML index */
8210 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8211}
8212
8213/*
8214 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8215 * Called before reporting dirty_bitmap to userspace.
8216 */
8217static void kvm_flush_pml_buffers(struct kvm *kvm)
8218{
8219 int i;
8220 struct kvm_vcpu *vcpu;
8221 /*
8222 * We only need to kick vcpu out of guest mode here, as PML buffer
8223 * is flushed at beginning of all VMEXITs, and it's obvious that only
8224 * vcpus running in guest are possible to have unflushed GPAs in PML
8225 * buffer.
8226 */
8227 kvm_for_each_vcpu(i, vcpu, kvm)
8228 kvm_vcpu_kick(vcpu);
8229}
8230
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008231static void vmx_dump_sel(char *name, uint32_t sel)
8232{
8233 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008234 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008235 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8236 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8237 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8238}
8239
8240static void vmx_dump_dtsel(char *name, uint32_t limit)
8241{
8242 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8243 name, vmcs_read32(limit),
8244 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8245}
8246
8247static void dump_vmcs(void)
8248{
8249 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8250 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8251 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8252 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8253 u32 secondary_exec_control = 0;
8254 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008255 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008256 int i, n;
8257
8258 if (cpu_has_secondary_exec_ctrls())
8259 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8260
8261 pr_err("*** Guest State ***\n");
8262 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8263 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8264 vmcs_readl(CR0_GUEST_HOST_MASK));
8265 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8266 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8267 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8268 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8269 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8270 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008271 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8272 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8273 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8274 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008275 }
8276 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8277 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8278 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8279 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8280 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8281 vmcs_readl(GUEST_SYSENTER_ESP),
8282 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8283 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8284 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8285 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8286 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8287 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8288 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8289 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8290 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8291 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8292 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8293 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8294 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008295 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8296 efer, vmcs_read64(GUEST_IA32_PAT));
8297 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8298 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008299 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8300 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008301 pr_err("PerfGlobCtl = 0x%016llx\n",
8302 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008303 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008304 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008305 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8306 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8307 vmcs_read32(GUEST_ACTIVITY_STATE));
8308 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8309 pr_err("InterruptStatus = %04x\n",
8310 vmcs_read16(GUEST_INTR_STATUS));
8311
8312 pr_err("*** Host State ***\n");
8313 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8314 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8315 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8316 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8317 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8318 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8319 vmcs_read16(HOST_TR_SELECTOR));
8320 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8321 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8322 vmcs_readl(HOST_TR_BASE));
8323 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8324 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8325 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8326 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8327 vmcs_readl(HOST_CR4));
8328 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8329 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8330 vmcs_read32(HOST_IA32_SYSENTER_CS),
8331 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8332 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008333 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8334 vmcs_read64(HOST_IA32_EFER),
8335 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008336 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008337 pr_err("PerfGlobCtl = 0x%016llx\n",
8338 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008339
8340 pr_err("*** Control State ***\n");
8341 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8342 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8343 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8344 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8345 vmcs_read32(EXCEPTION_BITMAP),
8346 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8347 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8348 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8349 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8350 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8351 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8352 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8353 vmcs_read32(VM_EXIT_INTR_INFO),
8354 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8355 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8356 pr_err(" reason=%08x qualification=%016lx\n",
8357 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8358 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8359 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8360 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008361 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008362 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008363 pr_err("TSC Multiplier = 0x%016llx\n",
8364 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008365 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8366 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8367 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8368 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8369 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008370 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008371 n = vmcs_read32(CR3_TARGET_COUNT);
8372 for (i = 0; i + 1 < n; i += 4)
8373 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8374 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8375 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8376 if (i < n)
8377 pr_err("CR3 target%u=%016lx\n",
8378 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8379 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8380 pr_err("PLE Gap=%08x Window=%08x\n",
8381 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8382 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8383 pr_err("Virtual processor ID = 0x%04x\n",
8384 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8385}
8386
Avi Kivity6aa8b732006-12-10 02:21:36 -08008387/*
8388 * The guest has exited. See if we can fix it or if we need userspace
8389 * assistance.
8390 */
Avi Kivity851ba692009-08-24 11:10:17 +03008391static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008392{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008393 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008394 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008395 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008396
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008397 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01008398 vcpu->arch.gpa_available = false;
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008399
Kai Huang843e4332015-01-28 10:54:28 +08008400 /*
8401 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8402 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8403 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8404 * mode as if vcpus is in root mode, the PML buffer must has been
8405 * flushed already.
8406 */
8407 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008408 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008409
Mohammed Gamal80ced182009-09-01 12:48:18 +02008410 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008411 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008412 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008413
Nadav Har'El644d7112011-05-25 23:12:35 +03008414 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008415 nested_vmx_vmexit(vcpu, exit_reason,
8416 vmcs_read32(VM_EXIT_INTR_INFO),
8417 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008418 return 1;
8419 }
8420
Mohammed Gamal51207022010-05-31 22:40:54 +03008421 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008422 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008423 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8424 vcpu->run->fail_entry.hardware_entry_failure_reason
8425 = exit_reason;
8426 return 0;
8427 }
8428
Avi Kivity29bd8a72007-09-10 17:27:03 +03008429 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008430 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8431 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008432 = vmcs_read32(VM_INSTRUCTION_ERROR);
8433 return 0;
8434 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008435
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008436 /*
8437 * Note:
8438 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8439 * delivery event since it indicates guest is accessing MMIO.
8440 * The vm-exit can be triggered again after return to guest that
8441 * will cause infinite loop.
8442 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008443 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008444 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008445 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008446 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008447 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8448 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8449 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8450 vcpu->run->internal.ndata = 2;
8451 vcpu->run->internal.data[0] = vectoring_info;
8452 vcpu->run->internal.data[1] = exit_reason;
8453 return 0;
8454 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008455
Avi Kivity6aa8b732006-12-10 02:21:36 -08008456 if (exit_reason < kvm_vmx_max_exit_handlers
8457 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008458 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008459 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008460 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8461 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008462 kvm_queue_exception(vcpu, UD_VECTOR);
8463 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008464 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008465}
8466
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008467static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008468{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008469 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8470
8471 if (is_guest_mode(vcpu) &&
8472 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8473 return;
8474
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008475 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008476 vmcs_write32(TPR_THRESHOLD, 0);
8477 return;
8478 }
8479
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008480 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008481}
8482
Yang Zhang8d146952013-01-25 10:18:50 +08008483static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8484{
8485 u32 sec_exec_control;
8486
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008487 /* Postpone execution until vmcs01 is the current VMCS. */
8488 if (is_guest_mode(vcpu)) {
8489 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8490 return;
8491 }
8492
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008493 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008494 return;
8495
Paolo Bonzini35754c92015-07-29 12:05:37 +02008496 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008497 return;
8498
8499 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8500
8501 if (set) {
8502 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8503 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8504 } else {
8505 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8506 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008507 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008508 }
8509 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8510
8511 vmx_set_msr_bitmap(vcpu);
8512}
8513
Tang Chen38b99172014-09-24 15:57:54 +08008514static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8515{
8516 struct vcpu_vmx *vmx = to_vmx(vcpu);
8517
8518 /*
8519 * Currently we do not handle the nested case where L2 has an
8520 * APIC access page of its own; that page is still pinned.
8521 * Hence, we skip the case where the VCPU is in guest mode _and_
8522 * L1 prepared an APIC access page for L2.
8523 *
8524 * For the case where L1 and L2 share the same APIC access page
8525 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8526 * in the vmcs12), this function will only update either the vmcs01
8527 * or the vmcs02. If the former, the vmcs02 will be updated by
8528 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8529 * the next L2->L1 exit.
8530 */
8531 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008532 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008533 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008534 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008535 vmx_flush_tlb_ept_only(vcpu);
8536 }
Tang Chen38b99172014-09-24 15:57:54 +08008537}
8538
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008539static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008540{
8541 u16 status;
8542 u8 old;
8543
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008544 if (max_isr == -1)
8545 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008546
8547 status = vmcs_read16(GUEST_INTR_STATUS);
8548 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008549 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008550 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008551 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008552 vmcs_write16(GUEST_INTR_STATUS, status);
8553 }
8554}
8555
8556static void vmx_set_rvi(int vector)
8557{
8558 u16 status;
8559 u8 old;
8560
Wei Wang4114c272014-11-05 10:53:43 +08008561 if (vector == -1)
8562 vector = 0;
8563
Yang Zhangc7c9c562013-01-25 10:18:51 +08008564 status = vmcs_read16(GUEST_INTR_STATUS);
8565 old = (u8)status & 0xff;
8566 if ((u8)vector != old) {
8567 status &= ~0xff;
8568 status |= (u8)vector;
8569 vmcs_write16(GUEST_INTR_STATUS, status);
8570 }
8571}
8572
8573static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8574{
Wanpeng Li963fee12014-07-17 19:03:00 +08008575 if (!is_guest_mode(vcpu)) {
8576 vmx_set_rvi(max_irr);
8577 return;
8578 }
8579
Wei Wang4114c272014-11-05 10:53:43 +08008580 if (max_irr == -1)
8581 return;
8582
Wanpeng Li963fee12014-07-17 19:03:00 +08008583 /*
Wei Wang4114c272014-11-05 10:53:43 +08008584 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8585 * handles it.
8586 */
8587 if (nested_exit_on_intr(vcpu))
8588 return;
8589
8590 /*
8591 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008592 * is run without virtual interrupt delivery.
8593 */
8594 if (!kvm_event_needs_reinjection(vcpu) &&
8595 vmx_interrupt_allowed(vcpu)) {
8596 kvm_queue_interrupt(vcpu, max_irr, false);
8597 vmx_inject_irq(vcpu);
8598 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008599}
8600
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008601static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008602{
8603 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008604 int max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008605
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008606 WARN_ON(!vcpu->arch.apicv_active);
8607 if (pi_test_on(&vmx->pi_desc)) {
8608 pi_clear_on(&vmx->pi_desc);
8609 /*
8610 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8611 * But on x86 this is just a compiler barrier anyway.
8612 */
8613 smp_mb__after_atomic();
8614 max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
8615 } else {
8616 max_irr = kvm_lapic_find_highest_irr(vcpu);
8617 }
8618 vmx_hwapic_irr_update(vcpu, max_irr);
8619 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008620}
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
Paolo Bonzini967235d2016-12-19 14:03:45 +01008633static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
8634{
8635 struct vcpu_vmx *vmx = to_vmx(vcpu);
8636
8637 pi_clear_on(&vmx->pi_desc);
8638 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
8639}
8640
Avi Kivity51aa01d2010-07-20 14:31:20 +03008641static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008642{
Avi Kivity00eba012011-03-07 17:24:54 +02008643 u32 exit_intr_info;
8644
8645 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8646 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8647 return;
8648
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008649 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008650 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008651
8652 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008653 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008654 kvm_machine_check();
8655
Gleb Natapov20f65982009-05-11 13:35:55 +03008656 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08008657 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008658 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008659 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008660 kvm_after_handle_nmi(&vmx->vcpu);
8661 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008662}
Gleb Natapov20f65982009-05-11 13:35:55 +03008663
Yang Zhanga547c6d2013-04-11 19:25:10 +08008664static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8665{
8666 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008667 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008668
Yang Zhanga547c6d2013-04-11 19:25:10 +08008669 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8670 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8671 unsigned int vector;
8672 unsigned long entry;
8673 gate_desc *desc;
8674 struct vcpu_vmx *vmx = to_vmx(vcpu);
8675#ifdef CONFIG_X86_64
8676 unsigned long tmp;
8677#endif
8678
8679 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8680 desc = (gate_desc *)vmx->host_idt_base + vector;
8681 entry = gate_offset(*desc);
8682 asm volatile(
8683#ifdef CONFIG_X86_64
8684 "mov %%" _ASM_SP ", %[sp]\n\t"
8685 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8686 "push $%c[ss]\n\t"
8687 "push %[sp]\n\t"
8688#endif
8689 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008690 __ASM_SIZE(push) " $%c[cs]\n\t"
8691 "call *%[entry]\n\t"
8692 :
8693#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008694 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008695#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008696 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008697 :
8698 [entry]"r"(entry),
8699 [ss]"i"(__KERNEL_DS),
8700 [cs]"i"(__KERNEL_CS)
8701 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008702 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008703}
8704
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008705static bool vmx_has_high_real_mode_segbase(void)
8706{
8707 return enable_unrestricted_guest || emulate_invalid_guest_state;
8708}
8709
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008710static bool vmx_mpx_supported(void)
8711{
8712 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8713 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8714}
8715
Wanpeng Li55412b22014-12-02 19:21:30 +08008716static bool vmx_xsaves_supported(void)
8717{
8718 return vmcs_config.cpu_based_2nd_exec_ctrl &
8719 SECONDARY_EXEC_XSAVES;
8720}
8721
Avi Kivity51aa01d2010-07-20 14:31:20 +03008722static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8723{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008724 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008725 bool unblock_nmi;
8726 u8 vector;
8727 bool idtv_info_valid;
8728
8729 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008730
Paolo Bonzini2c828782017-03-27 14:37:28 +02008731 if (vmx->nmi_known_unmasked)
8732 return;
8733 /*
8734 * Can't use vmx->exit_intr_info since we're not sure what
8735 * the exit reason is.
8736 */
8737 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8738 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8739 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8740 /*
8741 * SDM 3: 27.7.1.2 (September 2008)
8742 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8743 * a guest IRET fault.
8744 * SDM 3: 23.2.2 (September 2008)
8745 * Bit 12 is undefined in any of the following cases:
8746 * If the VM exit sets the valid bit in the IDT-vectoring
8747 * information field.
8748 * If the VM exit is due to a double fault.
8749 */
8750 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8751 vector != DF_VECTOR && !idtv_info_valid)
8752 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8753 GUEST_INTR_STATE_NMI);
8754 else
8755 vmx->nmi_known_unmasked =
8756 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8757 & GUEST_INTR_STATE_NMI);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008758}
8759
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008760static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008761 u32 idt_vectoring_info,
8762 int instr_len_field,
8763 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008764{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008765 u8 vector;
8766 int type;
8767 bool idtv_info_valid;
8768
8769 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008770
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008771 vcpu->arch.nmi_injected = false;
8772 kvm_clear_exception_queue(vcpu);
8773 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008774
8775 if (!idtv_info_valid)
8776 return;
8777
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008778 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008779
Avi Kivity668f6122008-07-02 09:28:55 +03008780 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8781 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008782
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008783 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008784 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008785 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008786 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008787 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008788 * Clear bit "block by NMI" before VM entry if a NMI
8789 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008790 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008791 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008792 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008793 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008794 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008795 /* fall through */
8796 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008797 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008798 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008799 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008800 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008801 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008802 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008803 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008804 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008805 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008806 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008807 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008808 break;
8809 default:
8810 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008811 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008812}
8813
Avi Kivity83422e12010-07-20 14:43:23 +03008814static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8815{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008816 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008817 VM_EXIT_INSTRUCTION_LEN,
8818 IDT_VECTORING_ERROR_CODE);
8819}
8820
Avi Kivityb463a6f2010-07-20 15:06:17 +03008821static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8822{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008823 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008824 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8825 VM_ENTRY_INSTRUCTION_LEN,
8826 VM_ENTRY_EXCEPTION_ERROR_CODE);
8827
8828 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8829}
8830
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008831static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8832{
8833 int i, nr_msrs;
8834 struct perf_guest_switch_msr *msrs;
8835
8836 msrs = perf_guest_get_msrs(&nr_msrs);
8837
8838 if (!msrs)
8839 return;
8840
8841 for (i = 0; i < nr_msrs; i++)
8842 if (msrs[i].host == msrs[i].guest)
8843 clear_atomic_switch_msr(vmx, msrs[i].msr);
8844 else
8845 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8846 msrs[i].host);
8847}
8848
Jiang Biao33365e72016-11-03 15:03:37 +08008849static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07008850{
8851 struct vcpu_vmx *vmx = to_vmx(vcpu);
8852 u64 tscl;
8853 u32 delta_tsc;
8854
8855 if (vmx->hv_deadline_tsc == -1)
8856 return;
8857
8858 tscl = rdtsc();
8859 if (vmx->hv_deadline_tsc > tscl)
8860 /* sure to be 32 bit only because checked on set_hv_timer */
8861 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8862 cpu_preemption_timer_multi);
8863 else
8864 delta_tsc = 0;
8865
8866 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8867}
8868
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008869static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008870{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008871 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008872 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008873
Avi Kivity104f2262010-11-18 13:12:52 +02008874 /* Don't enter VMX if guest state is invalid, let the exit handler
8875 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008876 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008877 return;
8878
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008879 if (vmx->ple_window_dirty) {
8880 vmx->ple_window_dirty = false;
8881 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8882 }
8883
Abel Gordon012f83c2013-04-18 14:39:25 +03008884 if (vmx->nested.sync_shadow_vmcs) {
8885 copy_vmcs12_to_shadow(vmx);
8886 vmx->nested.sync_shadow_vmcs = false;
8887 }
8888
Avi Kivity104f2262010-11-18 13:12:52 +02008889 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8890 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8891 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8892 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8893
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008894 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008895 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8896 vmcs_writel(HOST_CR4, cr4);
8897 vmx->host_state.vmcs_host_cr4 = cr4;
8898 }
8899
Avi Kivity104f2262010-11-18 13:12:52 +02008900 /* When single-stepping over STI and MOV SS, we must clear the
8901 * corresponding interruptibility bits in the guest state. Otherwise
8902 * vmentry fails as it then expects bit 14 (BS) in pending debug
8903 * exceptions being set, but that's not correct for the guest debugging
8904 * case. */
8905 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8906 vmx_set_interrupt_shadow(vcpu, 0);
8907
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008908 if (vmx->guest_pkru_valid)
8909 __write_pkru(vmx->guest_pkru);
8910
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008911 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008912 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008913
Yunhong Jiang64672c92016-06-13 14:19:59 -07008914 vmx_arm_hv_timer(vcpu);
8915
Nadav Har'Eld462b812011-05-24 15:26:10 +03008916 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008917 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008918 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008919 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8920 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8921 "push %%" _ASM_CX " \n\t"
8922 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03008923 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008924 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008925 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03008926 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008927 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008928 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8929 "mov %%cr2, %%" _ASM_DX " \n\t"
8930 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008931 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008932 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008933 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008934 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008935 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008936 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008937 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8938 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8939 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8940 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8941 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8942 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008943#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008944 "mov %c[r8](%0), %%r8 \n\t"
8945 "mov %c[r9](%0), %%r9 \n\t"
8946 "mov %c[r10](%0), %%r10 \n\t"
8947 "mov %c[r11](%0), %%r11 \n\t"
8948 "mov %c[r12](%0), %%r12 \n\t"
8949 "mov %c[r13](%0), %%r13 \n\t"
8950 "mov %c[r14](%0), %%r14 \n\t"
8951 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008952#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008953 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008954
Avi Kivity6aa8b732006-12-10 02:21:36 -08008955 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008956 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008957 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008958 "jmp 2f \n\t"
8959 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8960 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008961 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008962 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008963 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008964 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8965 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8966 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8967 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8968 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8969 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8970 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008971#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008972 "mov %%r8, %c[r8](%0) \n\t"
8973 "mov %%r9, %c[r9](%0) \n\t"
8974 "mov %%r10, %c[r10](%0) \n\t"
8975 "mov %%r11, %c[r11](%0) \n\t"
8976 "mov %%r12, %c[r12](%0) \n\t"
8977 "mov %%r13, %c[r13](%0) \n\t"
8978 "mov %%r14, %c[r14](%0) \n\t"
8979 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008980#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008981 "mov %%cr2, %%" _ASM_AX " \n\t"
8982 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008983
Avi Kivityb188c81f2012-09-16 15:10:58 +03008984 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008985 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008986 ".pushsection .rodata \n\t"
8987 ".global vmx_return \n\t"
8988 "vmx_return: " _ASM_PTR " 2b \n\t"
8989 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008990 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008991 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008992 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03008993 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008994 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8995 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8996 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8997 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8998 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8999 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
9000 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08009001#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08009002 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
9003 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
9004 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
9005 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
9006 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
9007 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
9008 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
9009 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08009010#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02009011 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9012 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009013 : "cc", "memory"
9014#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009015 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009016 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009017#else
9018 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009019#endif
9020 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009021
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009022 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9023 if (debugctlmsr)
9024 update_debugctlmsr(debugctlmsr);
9025
Avi Kivityaa67f602012-08-01 16:48:03 +03009026#ifndef CONFIG_X86_64
9027 /*
9028 * The sysexit path does not restore ds/es, so we must set them to
9029 * a reasonable value ourselves.
9030 *
9031 * We can't defer this to vmx_load_host_state() since that function
9032 * may be executed in interrupt context, which saves and restore segments
9033 * around it, nullifying its effect.
9034 */
9035 loadsegment(ds, __USER_DS);
9036 loadsegment(es, __USER_DS);
9037#endif
9038
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009039 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009040 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009041 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009042 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009043 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009044 vcpu->arch.regs_dirty = 0;
9045
Avi Kivity1155f762007-11-22 11:30:47 +02009046 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9047
Nadav Har'Eld462b812011-05-24 15:26:10 +03009048 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009049
Avi Kivity51aa01d2010-07-20 14:31:20 +03009050 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009051
Gleb Natapove0b890d2013-09-25 12:51:33 +03009052 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009053 * eager fpu is enabled if PKEY is supported and CR4 is switched
9054 * back on host, so it is safe to read guest PKRU from current
9055 * XSAVE.
9056 */
9057 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9058 vmx->guest_pkru = __read_pkru();
9059 if (vmx->guest_pkru != vmx->host_pkru) {
9060 vmx->guest_pkru_valid = true;
9061 __write_pkru(vmx->host_pkru);
9062 } else
9063 vmx->guest_pkru_valid = false;
9064 }
9065
9066 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009067 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9068 * we did not inject a still-pending event to L1 now because of
9069 * nested_run_pending, we need to re-enable this bit.
9070 */
9071 if (vmx->nested.nested_run_pending)
9072 kvm_make_request(KVM_REQ_EVENT, vcpu);
9073
9074 vmx->nested.nested_run_pending = 0;
9075
Avi Kivity51aa01d2010-07-20 14:31:20 +03009076 vmx_complete_atomic_exit(vmx);
9077 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009078 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009079}
9080
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009081static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009082{
9083 struct vcpu_vmx *vmx = to_vmx(vcpu);
9084 int cpu;
9085
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009086 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009087 return;
9088
9089 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009090 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009091 vmx_vcpu_put(vcpu);
9092 vmx_vcpu_load(vcpu, cpu);
9093 vcpu->cpu = cpu;
9094 put_cpu();
9095}
9096
Jim Mattson2f1fe812016-07-08 15:36:06 -07009097/*
9098 * Ensure that the current vmcs of the logical processor is the
9099 * vmcs01 of the vcpu before calling free_nested().
9100 */
9101static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9102{
9103 struct vcpu_vmx *vmx = to_vmx(vcpu);
9104 int r;
9105
9106 r = vcpu_load(vcpu);
9107 BUG_ON(r);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009108 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009109 free_nested(vmx);
9110 vcpu_put(vcpu);
9111}
9112
Avi Kivity6aa8b732006-12-10 02:21:36 -08009113static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9114{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009115 struct vcpu_vmx *vmx = to_vmx(vcpu);
9116
Kai Huang843e4332015-01-28 10:54:28 +08009117 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009118 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009119 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009120 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009121 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009122 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009123 kfree(vmx->guest_msrs);
9124 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009125 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009126}
9127
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009128static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009129{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009130 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009131 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009132 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009133
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009134 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009135 return ERR_PTR(-ENOMEM);
9136
Wanpeng Li991e7a02015-09-16 17:30:05 +08009137 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009138
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009139 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9140 if (err)
9141 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009142
Peter Feiner4e595162016-07-07 14:49:58 -07009143 err = -ENOMEM;
9144
9145 /*
9146 * If PML is turned on, failure on enabling PML just results in failure
9147 * of creating the vcpu, therefore we can simplify PML logic (by
9148 * avoiding dealing with cases, such as enabling PML partially on vcpus
9149 * for the guest, etc.
9150 */
9151 if (enable_pml) {
9152 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9153 if (!vmx->pml_pg)
9154 goto uninit_vcpu;
9155 }
9156
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009157 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009158 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9159 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009160
Peter Feiner4e595162016-07-07 14:49:58 -07009161 if (!vmx->guest_msrs)
9162 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009163
Nadav Har'Eld462b812011-05-24 15:26:10 +03009164 vmx->loaded_vmcs = &vmx->vmcs01;
9165 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009166 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009167 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009168 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009169 if (!vmm_exclusive)
9170 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
9171 loaded_vmcs_init(vmx->loaded_vmcs);
9172 if (!vmm_exclusive)
9173 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009174
Avi Kivity15ad7142007-07-11 18:17:21 +03009175 cpu = get_cpu();
9176 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009177 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009178 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009179 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009180 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009181 if (err)
9182 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009183 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009184 err = alloc_apic_access_page(kvm);
9185 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009186 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009187 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009188
Sheng Yangb927a3c2009-07-21 10:42:48 +08009189 if (enable_ept) {
9190 if (!kvm->arch.ept_identity_map_addr)
9191 kvm->arch.ept_identity_map_addr =
9192 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009193 err = init_rmode_identity_map(kvm);
9194 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009195 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009196 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009197
Wanpeng Li5c614b32015-10-13 09:18:36 -07009198 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009199 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009200 vmx->nested.vpid02 = allocate_vpid();
9201 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009202
Wincy Van705699a2015-02-03 23:58:17 +08009203 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009204 vmx->nested.current_vmptr = -1ull;
9205 vmx->nested.current_vmcs12 = NULL;
9206
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009207 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9208
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009209 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009210
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009211free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009212 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009213 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009214free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009215 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009216free_pml:
9217 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009218uninit_vcpu:
9219 kvm_vcpu_uninit(&vmx->vcpu);
9220free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009221 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009222 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009223 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009224}
9225
Yang, Sheng002c7f72007-07-31 14:23:01 +03009226static void __init vmx_check_processor_compat(void *rtn)
9227{
9228 struct vmcs_config vmcs_conf;
9229
9230 *(int *)rtn = 0;
9231 if (setup_vmcs_config(&vmcs_conf) < 0)
9232 *(int *)rtn = -EIO;
9233 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9234 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9235 smp_processor_id());
9236 *(int *)rtn = -EIO;
9237 }
9238}
9239
Sheng Yang67253af2008-04-25 10:20:22 +08009240static int get_ept_level(void)
9241{
9242 return VMX_EPT_DEFAULT_GAW + 1;
9243}
9244
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009245static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009246{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009247 u8 cache;
9248 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009249
Sheng Yang522c68c2009-04-27 20:35:43 +08009250 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009251 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009252 * 2. EPT with VT-d:
9253 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009254 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009255 * b. VT-d with snooping control feature: snooping control feature of
9256 * VT-d engine can guarantee the cache correctness. Just set it
9257 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009258 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009259 * consistent with host MTRR
9260 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009261 if (is_mmio) {
9262 cache = MTRR_TYPE_UNCACHABLE;
9263 goto exit;
9264 }
9265
9266 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009267 ipat = VMX_EPT_IPAT_BIT;
9268 cache = MTRR_TYPE_WRBACK;
9269 goto exit;
9270 }
9271
9272 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9273 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009274 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009275 cache = MTRR_TYPE_WRBACK;
9276 else
9277 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009278 goto exit;
9279 }
9280
Xiao Guangrongff536042015-06-15 16:55:22 +08009281 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009282
9283exit:
9284 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009285}
9286
Sheng Yang17cc3932010-01-05 19:02:27 +08009287static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009288{
Sheng Yang878403b2010-01-05 19:02:29 +08009289 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9290 return PT_DIRECTORY_LEVEL;
9291 else
9292 /* For shadow and EPT supported 1GB page */
9293 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009294}
9295
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009296static void vmcs_set_secondary_exec_control(u32 new_ctl)
9297{
9298 /*
9299 * These bits in the secondary execution controls field
9300 * are dynamic, the others are mostly based on the hypervisor
9301 * architecture and the guest's CPUID. Do not touch the
9302 * dynamic bits.
9303 */
9304 u32 mask =
9305 SECONDARY_EXEC_SHADOW_VMCS |
9306 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9307 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9308
9309 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9310
9311 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9312 (new_ctl & ~mask) | (cur_ctl & mask));
9313}
9314
David Matlack8322ebb2016-11-29 18:14:09 -08009315/*
9316 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9317 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9318 */
9319static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9320{
9321 struct vcpu_vmx *vmx = to_vmx(vcpu);
9322 struct kvm_cpuid_entry2 *entry;
9323
9324 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9325 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9326
9327#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9328 if (entry && (entry->_reg & (_cpuid_mask))) \
9329 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9330} while (0)
9331
9332 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9333 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9334 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9335 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9336 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9337 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9338 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9339 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9340 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9341 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9342 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9343 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9344 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9345 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9346 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9347
9348 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9349 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9350 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9351 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9352 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
9353 /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */
9354 cr4_fixed1_update(bit(11), ecx, bit(2));
9355
9356#undef cr4_fixed1_update
9357}
9358
Sheng Yang0e851882009-12-18 16:48:46 +08009359static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9360{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009361 struct kvm_cpuid_entry2 *best;
9362 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009363 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009364
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009365 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009366 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9367 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009368 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009369
Paolo Bonzini8b972652015-09-15 17:34:42 +02009370 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009371 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009372 vmx->nested.nested_vmx_secondary_ctls_high |=
9373 SECONDARY_EXEC_RDTSCP;
9374 else
9375 vmx->nested.nested_vmx_secondary_ctls_high &=
9376 ~SECONDARY_EXEC_RDTSCP;
9377 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009378 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009379
Mao, Junjiead756a12012-07-02 01:18:48 +00009380 /* Exposing INVPCID only when PCID is exposed */
9381 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9382 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009383 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9384 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009385 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009386
Mao, Junjiead756a12012-07-02 01:18:48 +00009387 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009388 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009389 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009390
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009391 if (cpu_has_secondary_exec_ctrls())
9392 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009393
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009394 if (nested_vmx_allowed(vcpu))
9395 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9396 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9397 else
9398 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9399 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009400
9401 if (nested_vmx_allowed(vcpu))
9402 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009403}
9404
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009405static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9406{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009407 if (func == 1 && nested)
9408 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009409}
9410
Yang Zhang25d92082013-08-06 12:00:32 +03009411static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9412 struct x86_exception *fault)
9413{
Jan Kiszka533558b2014-01-04 18:47:20 +01009414 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9415 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009416
9417 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009418 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009419 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009420 exit_reason = EXIT_REASON_EPT_VIOLATION;
9421 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009422 vmcs12->guest_physical_address = fault->address;
9423}
9424
Nadav Har'El155a97a2013-08-05 11:07:16 +03009425/* Callbacks for nested_ept_init_mmu_context: */
9426
9427static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9428{
9429 /* return the page table to be shadowed - in our case, EPT12 */
9430 return get_vmcs12(vcpu)->ept_pointer;
9431}
9432
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009433static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009434{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009435 WARN_ON(mmu_is_nested(vcpu));
9436 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009437 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9438 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009439 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9440 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9441 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9442
9443 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009444}
9445
9446static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9447{
9448 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9449}
9450
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009451static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9452 u16 error_code)
9453{
9454 bool inequality, bit;
9455
9456 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9457 inequality =
9458 (error_code & vmcs12->page_fault_error_code_mask) !=
9459 vmcs12->page_fault_error_code_match;
9460 return inequality ^ bit;
9461}
9462
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009463static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9464 struct x86_exception *fault)
9465{
9466 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9467
9468 WARN_ON(!is_guest_mode(vcpu));
9469
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009470 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009471 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9472 vmcs_read32(VM_EXIT_INTR_INFO),
9473 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009474 else
9475 kvm_inject_page_fault(vcpu, fault);
9476}
9477
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009478static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9479 struct vmcs12 *vmcs12);
9480
9481static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009482 struct vmcs12 *vmcs12)
9483{
9484 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009485 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009486
9487 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009488 /*
9489 * Translate L1 physical address to host physical
9490 * address for vmcs02. Keep the page pinned, so this
9491 * physical address remains valid. We keep a reference
9492 * to it so we can release it later.
9493 */
9494 if (vmx->nested.apic_access_page) /* shouldn't happen */
9495 nested_release_page(vmx->nested.apic_access_page);
9496 vmx->nested.apic_access_page =
9497 nested_get_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009498 /*
9499 * If translation failed, no matter: This feature asks
9500 * to exit when accessing the given address, and if it
9501 * can never be accessed, this feature won't do
9502 * anything anyway.
9503 */
9504 if (vmx->nested.apic_access_page) {
9505 hpa = page_to_phys(vmx->nested.apic_access_page);
9506 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9507 } else {
9508 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9509 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9510 }
9511 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9512 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9513 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9514 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9515 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009516 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009517
9518 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009519 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9520 nested_release_page(vmx->nested.virtual_apic_page);
9521 vmx->nested.virtual_apic_page =
9522 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9523
9524 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009525 * If translation failed, VM entry will fail because
9526 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9527 * Failing the vm entry is _not_ what the processor
9528 * does but it's basically the only possibility we
9529 * have. We could still enter the guest if CR8 load
9530 * exits are enabled, CR8 store exits are enabled, and
9531 * virtualize APIC access is disabled; in this case
9532 * the processor would never use the TPR shadow and we
9533 * could simply clear the bit from the execution
9534 * control. But such a configuration is useless, so
9535 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009536 */
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009537 if (vmx->nested.virtual_apic_page) {
9538 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9539 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9540 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009541 }
9542
Wincy Van705699a2015-02-03 23:58:17 +08009543 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +08009544 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9545 kunmap(vmx->nested.pi_desc_page);
9546 nested_release_page(vmx->nested.pi_desc_page);
9547 }
9548 vmx->nested.pi_desc_page =
9549 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
Wincy Van705699a2015-02-03 23:58:17 +08009550 vmx->nested.pi_desc =
9551 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9552 if (!vmx->nested.pi_desc) {
9553 nested_release_page_clean(vmx->nested.pi_desc_page);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009554 return;
Wincy Van705699a2015-02-03 23:58:17 +08009555 }
9556 vmx->nested.pi_desc =
9557 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9558 (unsigned long)(vmcs12->posted_intr_desc_addr &
9559 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009560 vmcs_write64(POSTED_INTR_DESC_ADDR,
9561 page_to_phys(vmx->nested.pi_desc_page) +
9562 (unsigned long)(vmcs12->posted_intr_desc_addr &
9563 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +08009564 }
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009565 if (cpu_has_vmx_msr_bitmap() &&
9566 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
9567 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9568 ;
9569 else
9570 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9571 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009572}
9573
Jan Kiszkaf4124502014-03-07 20:03:13 +01009574static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9575{
9576 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9577 struct vcpu_vmx *vmx = to_vmx(vcpu);
9578
9579 if (vcpu->arch.virtual_tsc_khz == 0)
9580 return;
9581
9582 /* Make sure short timeouts reliably trigger an immediate vmexit.
9583 * hrtimer_start does not guarantee this. */
9584 if (preemption_timeout <= 1) {
9585 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9586 return;
9587 }
9588
9589 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9590 preemption_timeout *= 1000000;
9591 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9592 hrtimer_start(&vmx->nested.preemption_timer,
9593 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9594}
9595
Wincy Van3af18d92015-02-03 23:49:31 +08009596static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9597 struct vmcs12 *vmcs12)
9598{
9599 int maxphyaddr;
9600 u64 addr;
9601
9602 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9603 return 0;
9604
9605 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9606 WARN_ON(1);
9607 return -EINVAL;
9608 }
9609 maxphyaddr = cpuid_maxphyaddr(vcpu);
9610
9611 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9612 ((addr + PAGE_SIZE) >> maxphyaddr))
9613 return -EINVAL;
9614
9615 return 0;
9616}
9617
9618/*
9619 * Merge L0's and L1's MSR bitmap, return false to indicate that
9620 * we do not use the hardware.
9621 */
9622static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9623 struct vmcs12 *vmcs12)
9624{
Wincy Van82f0dd42015-02-03 23:57:18 +08009625 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009626 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009627 unsigned long *msr_bitmap_l1;
9628 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +08009629
Radim Krčmářd048c092016-08-08 20:16:22 +02009630 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +08009631 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9632 return false;
9633
9634 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář05d8d342017-03-07 17:51:49 +01009635 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009636 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009637 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009638
Radim Krčmářd048c092016-08-08 20:16:22 +02009639 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9640
Wincy Vanf2b93282015-02-03 23:56:03 +08009641 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009642 if (nested_cpu_has_apic_reg_virt(vmcs12))
9643 for (msr = 0x800; msr <= 0x8ff; msr++)
9644 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009645 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009646 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009647
9648 nested_vmx_disable_intercept_for_msr(
9649 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009650 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9651 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009652
Wincy Van608406e2015-02-03 23:57:51 +08009653 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009654 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009655 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009656 APIC_BASE_MSR + (APIC_EOI >> 4),
9657 MSR_TYPE_W);
9658 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009659 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009660 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9661 MSR_TYPE_W);
9662 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009663 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009664 kunmap(page);
9665 nested_release_page_clean(page);
9666
9667 return true;
9668}
9669
9670static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9671 struct vmcs12 *vmcs12)
9672{
Wincy Van82f0dd42015-02-03 23:57:18 +08009673 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009674 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009675 !nested_cpu_has_vid(vmcs12) &&
9676 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009677 return 0;
9678
9679 /*
9680 * If virtualize x2apic mode is enabled,
9681 * virtualize apic access must be disabled.
9682 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009683 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9684 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009685 return -EINVAL;
9686
Wincy Van608406e2015-02-03 23:57:51 +08009687 /*
9688 * If virtual interrupt delivery is enabled,
9689 * we must exit on external interrupts.
9690 */
9691 if (nested_cpu_has_vid(vmcs12) &&
9692 !nested_exit_on_intr(vcpu))
9693 return -EINVAL;
9694
Wincy Van705699a2015-02-03 23:58:17 +08009695 /*
9696 * bits 15:8 should be zero in posted_intr_nv,
9697 * the descriptor address has been already checked
9698 * in nested_get_vmcs12_pages.
9699 */
9700 if (nested_cpu_has_posted_intr(vmcs12) &&
9701 (!nested_cpu_has_vid(vmcs12) ||
9702 !nested_exit_intr_ack_set(vcpu) ||
9703 vmcs12->posted_intr_nv & 0xff00))
9704 return -EINVAL;
9705
Wincy Vanf2b93282015-02-03 23:56:03 +08009706 /* tpr shadow is needed by all apicv features. */
9707 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9708 return -EINVAL;
9709
9710 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009711}
9712
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009713static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9714 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009715 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009716{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009717 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009718 u64 count, addr;
9719
9720 if (vmcs12_read_any(vcpu, count_field, &count) ||
9721 vmcs12_read_any(vcpu, addr_field, &addr)) {
9722 WARN_ON(1);
9723 return -EINVAL;
9724 }
9725 if (count == 0)
9726 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009727 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009728 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9729 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009730 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009731 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9732 addr_field, maxphyaddr, count, addr);
9733 return -EINVAL;
9734 }
9735 return 0;
9736}
9737
9738static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9739 struct vmcs12 *vmcs12)
9740{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009741 if (vmcs12->vm_exit_msr_load_count == 0 &&
9742 vmcs12->vm_exit_msr_store_count == 0 &&
9743 vmcs12->vm_entry_msr_load_count == 0)
9744 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009745 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009746 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009747 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009748 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009749 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009750 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009751 return -EINVAL;
9752 return 0;
9753}
9754
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009755static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9756 struct vmx_msr_entry *e)
9757{
9758 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009759 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009760 return -EINVAL;
9761 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9762 e->index == MSR_IA32_UCODE_REV)
9763 return -EINVAL;
9764 if (e->reserved != 0)
9765 return -EINVAL;
9766 return 0;
9767}
9768
9769static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9770 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009771{
9772 if (e->index == MSR_FS_BASE ||
9773 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009774 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9775 nested_vmx_msr_check_common(vcpu, e))
9776 return -EINVAL;
9777 return 0;
9778}
9779
9780static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9781 struct vmx_msr_entry *e)
9782{
9783 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9784 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009785 return -EINVAL;
9786 return 0;
9787}
9788
9789/*
9790 * Load guest's/host's msr at nested entry/exit.
9791 * return 0 for success, entry index for failure.
9792 */
9793static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9794{
9795 u32 i;
9796 struct vmx_msr_entry e;
9797 struct msr_data msr;
9798
9799 msr.host_initiated = false;
9800 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009801 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9802 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009803 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009804 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9805 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009806 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009807 }
9808 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009809 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009810 "%s check failed (%u, 0x%x, 0x%x)\n",
9811 __func__, i, e.index, e.reserved);
9812 goto fail;
9813 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009814 msr.index = e.index;
9815 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009816 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009817 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009818 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9819 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009820 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009821 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009822 }
9823 return 0;
9824fail:
9825 return i + 1;
9826}
9827
9828static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9829{
9830 u32 i;
9831 struct vmx_msr_entry e;
9832
9833 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009834 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009835 if (kvm_vcpu_read_guest(vcpu,
9836 gpa + i * sizeof(e),
9837 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009838 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009839 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9840 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009841 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009842 }
9843 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009844 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009845 "%s check failed (%u, 0x%x, 0x%x)\n",
9846 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009847 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009848 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009849 msr_info.host_initiated = false;
9850 msr_info.index = e.index;
9851 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009852 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009853 "%s cannot read MSR (%u, 0x%x)\n",
9854 __func__, i, e.index);
9855 return -EINVAL;
9856 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009857 if (kvm_vcpu_write_guest(vcpu,
9858 gpa + i * sizeof(e) +
9859 offsetof(struct vmx_msr_entry, value),
9860 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009861 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009862 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009863 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009864 return -EINVAL;
9865 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009866 }
9867 return 0;
9868}
9869
Ladi Prosek1dc35da2016-11-30 16:03:11 +01009870static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
9871{
9872 unsigned long invalid_mask;
9873
9874 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
9875 return (val & invalid_mask) == 0;
9876}
9877
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009878/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +01009879 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
9880 * emulating VM entry into a guest with EPT enabled.
9881 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
9882 * is assigned to entry_failure_code on failure.
9883 */
9884static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -08009885 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +01009886{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +01009887 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +01009888 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +01009889 *entry_failure_code = ENTRY_FAIL_DEFAULT;
9890 return 1;
9891 }
9892
9893 /*
9894 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
9895 * must not be dereferenced.
9896 */
9897 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
9898 !nested_ept) {
9899 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
9900 *entry_failure_code = ENTRY_FAIL_PDPTE;
9901 return 1;
9902 }
9903 }
9904
9905 vcpu->arch.cr3 = cr3;
9906 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
9907 }
9908
9909 kvm_mmu_reset_context(vcpu);
9910 return 0;
9911}
9912
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009913/*
9914 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9915 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009916 * 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 +03009917 * guest in a way that will both be appropriate to L1's requests, and our
9918 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9919 * function also has additional necessary side-effects, like setting various
9920 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +01009921 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
9922 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009923 */
Ladi Prosekee146c12016-11-30 16:03:09 +01009924static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -08009925 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009926{
9927 struct vcpu_vmx *vmx = to_vmx(vcpu);
9928 u32 exec_control;
9929
9930 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9931 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9932 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9933 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9934 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9935 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9936 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9937 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9938 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9939 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9940 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9941 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9942 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9943 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9944 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9945 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9946 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9947 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9948 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9949 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9950 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9951 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9952 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9953 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9954 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9955 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9956 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9957 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9958 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9959 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9960 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9961 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9962 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9963 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9964 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9965 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9966
Jim Mattsoncf8b84f2016-11-30 12:03:42 -08009967 if (from_vmentry &&
9968 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +02009969 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9970 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9971 } else {
9972 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9973 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9974 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -08009975 if (from_vmentry) {
9976 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9977 vmcs12->vm_entry_intr_info_field);
9978 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9979 vmcs12->vm_entry_exception_error_code);
9980 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9981 vmcs12->vm_entry_instruction_len);
9982 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9983 vmcs12->guest_interruptibility_info);
9984 } else {
9985 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9986 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009987 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009988 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009989 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9990 vmcs12->guest_pending_dbg_exceptions);
9991 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9992 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9993
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009994 if (nested_cpu_has_xsaves(vmcs12))
9995 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009996 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9997
Jan Kiszkaf4124502014-03-07 20:03:13 +01009998 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009999
Paolo Bonzini93140062016-07-06 13:23:51 +020010000 /* Preemption timer setting is only taken from vmcs01. */
10001 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10002 exec_control |= vmcs_config.pin_based_exec_ctrl;
10003 if (vmx->hv_deadline_tsc == -1)
10004 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
10005
10006 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080010007 if (nested_cpu_has_posted_intr(vmcs12)) {
10008 /*
10009 * Note that we use L0's vector here and in
10010 * vmx_deliver_nested_posted_interrupt.
10011 */
10012 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10013 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010014 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010015 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010016 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010017 }
Wincy Van705699a2015-02-03 23:58:17 +080010018
Jan Kiszkaf4124502014-03-07 20:03:13 +010010019 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010020
Jan Kiszkaf4124502014-03-07 20:03:13 +010010021 vmx->nested.preemption_timer_expired = false;
10022 if (nested_cpu_has_preemption_timer(vmcs12))
10023 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010024
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010025 /*
10026 * Whether page-faults are trapped is determined by a combination of
10027 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10028 * If enable_ept, L0 doesn't care about page faults and we should
10029 * set all of these to L1's desires. However, if !enable_ept, L0 does
10030 * care about (at least some) page faults, and because it is not easy
10031 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10032 * to exit on each and every L2 page fault. This is done by setting
10033 * MASK=MATCH=0 and (see below) EB.PF=1.
10034 * Note that below we don't need special code to set EB.PF beyond the
10035 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10036 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10037 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10038 *
10039 * A problem with this approach (when !enable_ept) is that L1 may be
10040 * injected with more page faults than it asked for. This could have
10041 * caused problems, but in practice existing hypervisors don't care.
10042 * To fix this, we will need to emulate the PFEC checking (on the L1
10043 * page tables), using walk_addr(), when injecting PFs to L1.
10044 */
10045 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10046 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10047 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10048 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10049
10050 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010010051 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010052
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010053 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010054 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010055 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010056 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010057 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010058 if (nested_cpu_has(vmcs12,
10059 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10060 exec_control |= vmcs12->secondary_vm_exec_control;
10061
Wincy Van608406e2015-02-03 23:57:51 +080010062 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10063 vmcs_write64(EOI_EXIT_BITMAP0,
10064 vmcs12->eoi_exit_bitmap0);
10065 vmcs_write64(EOI_EXIT_BITMAP1,
10066 vmcs12->eoi_exit_bitmap1);
10067 vmcs_write64(EOI_EXIT_BITMAP2,
10068 vmcs12->eoi_exit_bitmap2);
10069 vmcs_write64(EOI_EXIT_BITMAP3,
10070 vmcs12->eoi_exit_bitmap3);
10071 vmcs_write16(GUEST_INTR_STATUS,
10072 vmcs12->guest_intr_status);
10073 }
10074
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010075 /*
10076 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10077 * nested_get_vmcs12_pages will either fix it up or
10078 * remove the VM execution control.
10079 */
10080 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10081 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10082
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010083 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10084 }
10085
10086
10087 /*
10088 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10089 * Some constant fields are set here by vmx_set_constant_host_state().
10090 * Other fields are different per CPU, and will be set later when
10091 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10092 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010093 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010094
10095 /*
Jim Mattson83bafef2016-10-04 10:48:38 -070010096 * Set the MSR load/store lists to match L0's settings.
10097 */
10098 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10099 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10100 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10101 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10102 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10103
10104 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010105 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10106 * entry, but only if the current (host) sp changed from the value
10107 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10108 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10109 * here we just force the write to happen on entry.
10110 */
10111 vmx->host_rsp = 0;
10112
10113 exec_control = vmx_exec_control(vmx); /* L0's desires */
10114 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10115 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10116 exec_control &= ~CPU_BASED_TPR_SHADOW;
10117 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010118
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010119 /*
10120 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10121 * nested_get_vmcs12_pages can't fix it up, the illegal value
10122 * will result in a VM entry failure.
10123 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010124 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010125 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010126 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
10127 }
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
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010160 if (from_vmentry &&
10161 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010162 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010163 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010164 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010165 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010166 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010167
10168 set_cr4_guest_host_mask(vmx);
10169
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010170 if (from_vmentry &&
10171 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010172 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10173
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010174 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10175 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010176 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010177 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010178 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010179 if (kvm_has_tsc_control)
10180 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010181
10182 if (enable_vpid) {
10183 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010184 * There is no direct mapping between vpid02 and vpid12, the
10185 * vpid02 is per-vCPU for L0 and reused while the value of
10186 * vpid12 is changed w/ one invvpid during nested vmentry.
10187 * The vpid12 is allocated by L1 for L2, so it will not
10188 * influence global bitmap(for vpid01 and vpid02 allocation)
10189 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010190 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010191 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10192 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10193 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10194 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10195 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10196 }
10197 } else {
10198 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10199 vmx_flush_tlb(vcpu);
10200 }
10201
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010202 }
10203
Nadav Har'El155a97a2013-08-05 11:07:16 +030010204 if (nested_cpu_has_ept(vmcs12)) {
10205 kvm_mmu_unload(vcpu);
10206 nested_ept_init_mmu_context(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010207 } else if (nested_cpu_has2(vmcs12,
10208 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10209 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010210 }
10211
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010212 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010213 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10214 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010215 * The CR0_READ_SHADOW is what L2 should have expected to read given
10216 * the specifications by L1; It's not enough to take
10217 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10218 * have more bits than L1 expected.
10219 */
10220 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10221 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10222
10223 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10224 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10225
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010226 if (from_vmentry &&
10227 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010228 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10229 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10230 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10231 else
10232 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10233 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10234 vmx_set_efer(vcpu, vcpu->arch.efer);
10235
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010236 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010237 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010238 entry_failure_code))
10239 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010240
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010241 if (!enable_ept)
10242 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10243
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010244 /*
10245 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10246 */
10247 if (enable_ept) {
10248 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10249 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10250 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10251 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10252 }
10253
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010254 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10255 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010256 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010257}
10258
Jim Mattsonca0bde22016-11-30 12:03:46 -080010259static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10260{
10261 struct vcpu_vmx *vmx = to_vmx(vcpu);
10262
10263 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10264 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10265 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10266
10267 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10268 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10269
10270 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10271 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10272
10273 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10274 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10275
10276 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10277 vmx->nested.nested_vmx_procbased_ctls_low,
10278 vmx->nested.nested_vmx_procbased_ctls_high) ||
10279 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10280 vmx->nested.nested_vmx_secondary_ctls_low,
10281 vmx->nested.nested_vmx_secondary_ctls_high) ||
10282 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10283 vmx->nested.nested_vmx_pinbased_ctls_low,
10284 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10285 !vmx_control_verify(vmcs12->vm_exit_controls,
10286 vmx->nested.nested_vmx_exit_ctls_low,
10287 vmx->nested.nested_vmx_exit_ctls_high) ||
10288 !vmx_control_verify(vmcs12->vm_entry_controls,
10289 vmx->nested.nested_vmx_entry_ctls_low,
10290 vmx->nested.nested_vmx_entry_ctls_high))
10291 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10292
10293 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10294 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10295 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10296 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10297
10298 return 0;
10299}
10300
10301static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10302 u32 *exit_qual)
10303{
10304 bool ia32e;
10305
10306 *exit_qual = ENTRY_FAIL_DEFAULT;
10307
10308 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10309 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10310 return 1;
10311
10312 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10313 vmcs12->vmcs_link_pointer != -1ull) {
10314 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10315 return 1;
10316 }
10317
10318 /*
10319 * If the load IA32_EFER VM-entry control is 1, the following checks
10320 * are performed on the field for the IA32_EFER MSR:
10321 * - Bits reserved in the IA32_EFER MSR must be 0.
10322 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10323 * the IA-32e mode guest VM-exit control. It must also be identical
10324 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10325 * CR0.PG) is 1.
10326 */
10327 if (to_vmx(vcpu)->nested.nested_run_pending &&
10328 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10329 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10330 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10331 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10332 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10333 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10334 return 1;
10335 }
10336
10337 /*
10338 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10339 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10340 * the values of the LMA and LME bits in the field must each be that of
10341 * the host address-space size VM-exit control.
10342 */
10343 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10344 ia32e = (vmcs12->vm_exit_controls &
10345 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10346 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10347 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10348 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10349 return 1;
10350 }
10351
10352 return 0;
10353}
10354
Jim Mattson858e25c2016-11-30 12:03:47 -080010355static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10356{
10357 struct vcpu_vmx *vmx = to_vmx(vcpu);
10358 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10359 struct loaded_vmcs *vmcs02;
Jim Mattson858e25c2016-11-30 12:03:47 -080010360 u32 msr_entry_idx;
10361 u32 exit_qual;
10362
10363 vmcs02 = nested_get_current_vmcs02(vmx);
10364 if (!vmcs02)
10365 return -ENOMEM;
10366
10367 enter_guest_mode(vcpu);
10368
10369 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10370 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10371
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010372 vmx_switch_vmcs(vcpu, vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080010373 vmx_segment_cache_clear(vmx);
10374
10375 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10376 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010377 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010378 nested_vmx_entry_failure(vcpu, vmcs12,
10379 EXIT_REASON_INVALID_STATE, exit_qual);
10380 return 1;
10381 }
10382
10383 nested_get_vmcs12_pages(vcpu, vmcs12);
10384
10385 msr_entry_idx = nested_vmx_load_msr(vcpu,
10386 vmcs12->vm_entry_msr_load_addr,
10387 vmcs12->vm_entry_msr_load_count);
10388 if (msr_entry_idx) {
10389 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010390 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010391 nested_vmx_entry_failure(vcpu, vmcs12,
10392 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10393 return 1;
10394 }
10395
10396 vmcs12->launch_state = 1;
10397
10398 /*
10399 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10400 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10401 * returned as far as L1 is concerned. It will only return (and set
10402 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10403 */
10404 return 0;
10405}
10406
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010407/*
10408 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10409 * for running an L2 nested guest.
10410 */
10411static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10412{
10413 struct vmcs12 *vmcs12;
10414 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080010415 u32 exit_qual;
10416 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010417
Kyle Hueyeb277562016-11-29 12:40:39 -080010418 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010419 return 1;
10420
Kyle Hueyeb277562016-11-29 12:40:39 -080010421 if (!nested_vmx_check_vmcs12(vcpu))
10422 goto out;
10423
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010424 vmcs12 = get_vmcs12(vcpu);
10425
Abel Gordon012f83c2013-04-18 14:39:25 +030010426 if (enable_shadow_vmcs)
10427 copy_shadow_to_vmcs12(vmx);
10428
Nadav Har'El7c177932011-05-25 23:12:04 +030010429 /*
10430 * The nested entry process starts with enforcing various prerequisites
10431 * on vmcs12 as required by the Intel SDM, and act appropriately when
10432 * they fail: As the SDM explains, some conditions should cause the
10433 * instruction to fail, while others will cause the instruction to seem
10434 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10435 * To speed up the normal (success) code path, we should avoid checking
10436 * for misconfigurations which will anyway be caught by the processor
10437 * when using the merged vmcs02.
10438 */
10439 if (vmcs12->launch_state == launch) {
10440 nested_vmx_failValid(vcpu,
10441 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10442 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080010443 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030010444 }
10445
Jim Mattsonca0bde22016-11-30 12:03:46 -080010446 ret = check_vmentry_prereqs(vcpu, vmcs12);
10447 if (ret) {
10448 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080010449 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010450 }
10451
Nadav Har'El7c177932011-05-25 23:12:04 +030010452 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080010453 * After this point, the trap flag no longer triggers a singlestep trap
10454 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10455 * This is not 100% correct; for performance reasons, we delegate most
10456 * of the checks on host state to the processor. If those fail,
10457 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020010458 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080010459 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020010460
Jim Mattsonca0bde22016-11-30 12:03:46 -080010461 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10462 if (ret) {
10463 nested_vmx_entry_failure(vcpu, vmcs12,
10464 EXIT_REASON_INVALID_STATE, exit_qual);
10465 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020010466 }
10467
10468 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010469 * We're finally done with prerequisite checking, and can start with
10470 * the nested entry.
10471 */
10472
Jim Mattson858e25c2016-11-30 12:03:47 -080010473 ret = enter_vmx_non_root_mode(vcpu, true);
10474 if (ret)
10475 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030010476
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010477 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010478 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010479
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010480 vmx->nested.nested_run_pending = 1;
10481
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010482 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080010483
10484out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080010485 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010486}
10487
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010488/*
10489 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10490 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10491 * This function returns the new value we should put in vmcs12.guest_cr0.
10492 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10493 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10494 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10495 * didn't trap the bit, because if L1 did, so would L0).
10496 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10497 * been modified by L2, and L1 knows it. So just leave the old value of
10498 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10499 * isn't relevant, because if L0 traps this bit it can set it to anything.
10500 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10501 * changed these bits, and therefore they need to be updated, but L0
10502 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10503 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10504 */
10505static inline unsigned long
10506vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10507{
10508 return
10509 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10510 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10511 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10512 vcpu->arch.cr0_guest_owned_bits));
10513}
10514
10515static inline unsigned long
10516vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10517{
10518 return
10519 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10520 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10521 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10522 vcpu->arch.cr4_guest_owned_bits));
10523}
10524
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010525static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10526 struct vmcs12 *vmcs12)
10527{
10528 u32 idt_vectoring;
10529 unsigned int nr;
10530
Gleb Natapov851eb6672013-09-25 12:51:34 +030010531 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010532 nr = vcpu->arch.exception.nr;
10533 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10534
10535 if (kvm_exception_is_soft(nr)) {
10536 vmcs12->vm_exit_instruction_len =
10537 vcpu->arch.event_exit_inst_len;
10538 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10539 } else
10540 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10541
10542 if (vcpu->arch.exception.has_error_code) {
10543 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10544 vmcs12->idt_vectoring_error_code =
10545 vcpu->arch.exception.error_code;
10546 }
10547
10548 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010549 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010550 vmcs12->idt_vectoring_info_field =
10551 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10552 } else if (vcpu->arch.interrupt.pending) {
10553 nr = vcpu->arch.interrupt.nr;
10554 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10555
10556 if (vcpu->arch.interrupt.soft) {
10557 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10558 vmcs12->vm_entry_instruction_len =
10559 vcpu->arch.event_exit_inst_len;
10560 } else
10561 idt_vectoring |= INTR_TYPE_EXT_INTR;
10562
10563 vmcs12->idt_vectoring_info_field = idt_vectoring;
10564 }
10565}
10566
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010567static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10568{
10569 struct vcpu_vmx *vmx = to_vmx(vcpu);
10570
Wanpeng Liacc9ab62017-02-27 04:24:39 -080010571 if (vcpu->arch.exception.pending ||
10572 vcpu->arch.nmi_injected ||
10573 vcpu->arch.interrupt.pending)
10574 return -EBUSY;
10575
Jan Kiszkaf4124502014-03-07 20:03:13 +010010576 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10577 vmx->nested.preemption_timer_expired) {
10578 if (vmx->nested.nested_run_pending)
10579 return -EBUSY;
10580 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10581 return 0;
10582 }
10583
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010584 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Wanpeng Liacc9ab62017-02-27 04:24:39 -080010585 if (vmx->nested.nested_run_pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010586 return -EBUSY;
10587 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10588 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10589 INTR_INFO_VALID_MASK, 0);
10590 /*
10591 * The NMI-triggered VM exit counts as injection:
10592 * clear this one and block further NMIs.
10593 */
10594 vcpu->arch.nmi_pending = 0;
10595 vmx_set_nmi_mask(vcpu, true);
10596 return 0;
10597 }
10598
10599 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10600 nested_exit_on_intr(vcpu)) {
10601 if (vmx->nested.nested_run_pending)
10602 return -EBUSY;
10603 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010604 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010605 }
10606
David Hildenbrand6342c502017-01-25 11:58:58 +010010607 vmx_complete_nested_posted_interrupt(vcpu);
10608 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010609}
10610
Jan Kiszkaf4124502014-03-07 20:03:13 +010010611static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10612{
10613 ktime_t remaining =
10614 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10615 u64 value;
10616
10617 if (ktime_to_ns(remaining) <= 0)
10618 return 0;
10619
10620 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10621 do_div(value, 1000000);
10622 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10623}
10624
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010625/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010626 * Update the guest state fields of vmcs12 to reflect changes that
10627 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
10628 * VM-entry controls is also updated, since this is really a guest
10629 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010630 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010631static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010632{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010633 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10634 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10635
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010636 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10637 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10638 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10639
10640 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10641 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10642 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10643 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10644 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10645 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10646 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10647 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10648 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10649 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10650 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10651 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10652 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10653 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10654 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10655 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10656 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10657 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10658 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10659 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10660 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10661 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10662 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10663 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10664 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10665 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10666 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10667 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10668 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10669 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10670 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10671 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10672 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10673 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10674 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10675 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10676
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010677 vmcs12->guest_interruptibility_info =
10678 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10679 vmcs12->guest_pending_dbg_exceptions =
10680 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010681 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10682 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10683 else
10684 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010685
Jan Kiszkaf4124502014-03-07 20:03:13 +010010686 if (nested_cpu_has_preemption_timer(vmcs12)) {
10687 if (vmcs12->vm_exit_controls &
10688 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10689 vmcs12->vmx_preemption_timer_value =
10690 vmx_get_preemption_timer_value(vcpu);
10691 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10692 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010693
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010694 /*
10695 * In some cases (usually, nested EPT), L2 is allowed to change its
10696 * own CR3 without exiting. If it has changed it, we must keep it.
10697 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10698 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10699 *
10700 * Additionally, restore L2's PDPTR to vmcs12.
10701 */
10702 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010703 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010704 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10705 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10706 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10707 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10708 }
10709
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010710 if (nested_cpu_has_ept(vmcs12))
10711 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
10712
Wincy Van608406e2015-02-03 23:57:51 +080010713 if (nested_cpu_has_vid(vmcs12))
10714 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10715
Jan Kiszkac18911a2013-03-13 16:06:41 +010010716 vmcs12->vm_entry_controls =
10717 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010718 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010719
Jan Kiszka2996fca2014-06-16 13:59:43 +020010720 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10721 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10722 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10723 }
10724
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010725 /* TODO: These cannot have changed unless we have MSR bitmaps and
10726 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010727 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010728 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010729 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10730 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010731 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10732 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10733 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010734 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010735 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010736 if (nested_cpu_has_xsaves(vmcs12))
10737 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010738}
10739
10740/*
10741 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10742 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10743 * and this function updates it to reflect the changes to the guest state while
10744 * L2 was running (and perhaps made some exits which were handled directly by L0
10745 * without going back to L1), and to reflect the exit reason.
10746 * Note that we do not have to copy here all VMCS fields, just those that
10747 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10748 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10749 * which already writes to vmcs12 directly.
10750 */
10751static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10752 u32 exit_reason, u32 exit_intr_info,
10753 unsigned long exit_qualification)
10754{
10755 /* update guest state fields: */
10756 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010757
10758 /* update exit information fields: */
10759
Jan Kiszka533558b2014-01-04 18:47:20 +010010760 vmcs12->vm_exit_reason = exit_reason;
10761 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010762
Jan Kiszka533558b2014-01-04 18:47:20 +010010763 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010764 if ((vmcs12->vm_exit_intr_info &
10765 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10766 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10767 vmcs12->vm_exit_intr_error_code =
10768 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010769 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010770 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10771 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10772
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010773 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10774 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10775 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010776 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010777
10778 /*
10779 * Transfer the event that L0 or L1 may wanted to inject into
10780 * L2 to IDT_VECTORING_INFO_FIELD.
10781 */
10782 vmcs12_save_pending_event(vcpu, vmcs12);
10783 }
10784
10785 /*
10786 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10787 * preserved above and would only end up incorrectly in L1.
10788 */
10789 vcpu->arch.nmi_injected = false;
10790 kvm_clear_exception_queue(vcpu);
10791 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010792}
10793
10794/*
10795 * A part of what we need to when the nested L2 guest exits and we want to
10796 * run its L1 parent, is to reset L1's guest state to the host state specified
10797 * in vmcs12.
10798 * This function is to be called not only on normal nested exit, but also on
10799 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10800 * Failures During or After Loading Guest State").
10801 * This function should be called when the active VMCS is L1's (vmcs01).
10802 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010803static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10804 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010805{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010806 struct kvm_segment seg;
Jim Mattsonca0bde22016-11-30 12:03:46 -080010807 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010808
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010809 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10810 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010811 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010812 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10813 else
10814 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10815 vmx_set_efer(vcpu, vcpu->arch.efer);
10816
10817 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10818 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010819 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010820 /*
10821 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010822 * actually changed, because vmx_set_cr0 refers to efer set above.
10823 *
10824 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
10825 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010826 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010827 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010828 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010829
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010830 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010831 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10832 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10833
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010834 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010835
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010836 /*
10837 * Only PDPTE load can fail as the value of cr3 was checked on entry and
10838 * couldn't have changed.
10839 */
10840 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
10841 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010842
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010843 if (!enable_ept)
10844 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10845
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010846 if (enable_vpid) {
10847 /*
10848 * Trivially support vpid by letting L2s share their parent
10849 * L1's vpid. TODO: move to a more elaborate solution, giving
10850 * each L2 its own vpid and exposing the vpid feature to L1.
10851 */
10852 vmx_flush_tlb(vcpu);
10853 }
10854
10855
10856 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10857 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10858 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10859 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10860 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010861
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010862 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10863 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10864 vmcs_write64(GUEST_BNDCFGS, 0);
10865
Jan Kiszka44811c02013-08-04 17:17:27 +020010866 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010867 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010868 vcpu->arch.pat = vmcs12->host_ia32_pat;
10869 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010870 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10871 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10872 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010873
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010874 /* Set L1 segment info according to Intel SDM
10875 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10876 seg = (struct kvm_segment) {
10877 .base = 0,
10878 .limit = 0xFFFFFFFF,
10879 .selector = vmcs12->host_cs_selector,
10880 .type = 11,
10881 .present = 1,
10882 .s = 1,
10883 .g = 1
10884 };
10885 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10886 seg.l = 1;
10887 else
10888 seg.db = 1;
10889 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10890 seg = (struct kvm_segment) {
10891 .base = 0,
10892 .limit = 0xFFFFFFFF,
10893 .type = 3,
10894 .present = 1,
10895 .s = 1,
10896 .db = 1,
10897 .g = 1
10898 };
10899 seg.selector = vmcs12->host_ds_selector;
10900 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10901 seg.selector = vmcs12->host_es_selector;
10902 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10903 seg.selector = vmcs12->host_ss_selector;
10904 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10905 seg.selector = vmcs12->host_fs_selector;
10906 seg.base = vmcs12->host_fs_base;
10907 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10908 seg.selector = vmcs12->host_gs_selector;
10909 seg.base = vmcs12->host_gs_base;
10910 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10911 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010912 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010913 .limit = 0x67,
10914 .selector = vmcs12->host_tr_selector,
10915 .type = 11,
10916 .present = 1
10917 };
10918 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10919
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010920 kvm_set_dr(vcpu, 7, 0x400);
10921 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010922
Wincy Van3af18d92015-02-03 23:49:31 +080010923 if (cpu_has_vmx_msr_bitmap())
10924 vmx_set_msr_bitmap(vcpu);
10925
Wincy Vanff651cb2014-12-11 08:52:58 +030010926 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10927 vmcs12->vm_exit_msr_load_count))
10928 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010929}
10930
10931/*
10932 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10933 * and modify vmcs12 to make it see what it would expect to see there if
10934 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10935 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010936static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10937 u32 exit_intr_info,
10938 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010939{
10940 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010941 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattsoncf3215d2016-09-06 09:33:21 -070010942 u32 vm_inst_error = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010943
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010944 /* trying to cancel vmlaunch/vmresume is a bug */
10945 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10946
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010947 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010948 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10949 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010950
Wincy Vanff651cb2014-12-11 08:52:58 +030010951 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10952 vmcs12->vm_exit_msr_store_count))
10953 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10954
Jim Mattsoncf3215d2016-09-06 09:33:21 -070010955 if (unlikely(vmx->fail))
10956 vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR);
10957
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010958 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Wanpeng Lif3380ca2014-08-05 12:42:23 +080010959
Bandan Das77b0f5d2014-04-19 18:17:45 -040010960 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10961 && nested_exit_intr_ack_set(vcpu)) {
10962 int irq = kvm_cpu_get_interrupt(vcpu);
10963 WARN_ON(irq < 0);
10964 vmcs12->vm_exit_intr_info = irq |
10965 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10966 }
10967
Jan Kiszka542060e2014-01-04 18:47:21 +010010968 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10969 vmcs12->exit_qualification,
10970 vmcs12->idt_vectoring_info_field,
10971 vmcs12->vm_exit_intr_info,
10972 vmcs12->vm_exit_intr_error_code,
10973 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010974
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010975 vm_entry_controls_reset_shadow(vmx);
10976 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010977 vmx_segment_cache_clear(vmx);
10978
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010979 /* if no vmcs02 cache requested, remove the one we used */
10980 if (VMCS02_POOL_SIZE == 0)
10981 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10982
10983 load_vmcs12_host_state(vcpu, vmcs12);
10984
Paolo Bonzini93140062016-07-06 13:23:51 +020010985 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070010986 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10987 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010988 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020010989 if (vmx->hv_deadline_tsc == -1)
10990 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10991 PIN_BASED_VMX_PREEMPTION_TIMER);
10992 else
10993 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10994 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070010995 if (kvm_has_tsc_control)
10996 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010997
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010998 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10999 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
11000 vmx_set_virtual_x2apic_mode(vcpu,
11001 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070011002 } else if (!nested_cpu_has_ept(vmcs12) &&
11003 nested_cpu_has2(vmcs12,
11004 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
11005 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020011006 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011007
11008 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11009 vmx->host_rsp = 0;
11010
11011 /* Unpin physical memory we referred to in vmcs02 */
11012 if (vmx->nested.apic_access_page) {
11013 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011014 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011015 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011016 if (vmx->nested.virtual_apic_page) {
11017 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011018 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011019 }
Wincy Van705699a2015-02-03 23:58:17 +080011020 if (vmx->nested.pi_desc_page) {
11021 kunmap(vmx->nested.pi_desc_page);
11022 nested_release_page(vmx->nested.pi_desc_page);
11023 vmx->nested.pi_desc_page = NULL;
11024 vmx->nested.pi_desc = NULL;
11025 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011026
11027 /*
Tang Chen38b99172014-09-24 15:57:54 +080011028 * We are now running in L2, mmu_notifier will force to reload the
11029 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11030 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011031 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011032
11033 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011034 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11035 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11036 * success or failure flag accordingly.
11037 */
11038 if (unlikely(vmx->fail)) {
11039 vmx->fail = 0;
Jim Mattsoncf3215d2016-09-06 09:33:21 -070011040 nested_vmx_failValid(vcpu, vm_inst_error);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011041 } else
11042 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011043 if (enable_shadow_vmcs)
11044 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011045
11046 /* in case we halted in L2 */
11047 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011048}
11049
Nadav Har'El7c177932011-05-25 23:12:04 +030011050/*
Jan Kiszka42124922014-01-04 18:47:19 +010011051 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11052 */
11053static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11054{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011055 if (is_guest_mode(vcpu)) {
11056 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011057 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011058 }
Jan Kiszka42124922014-01-04 18:47:19 +010011059 free_nested(to_vmx(vcpu));
11060}
11061
11062/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011063 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11064 * 23.7 "VM-entry failures during or after loading guest state" (this also
11065 * lists the acceptable exit-reason and exit-qualification parameters).
11066 * It should only be called before L2 actually succeeded to run, and when
11067 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11068 */
11069static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11070 struct vmcs12 *vmcs12,
11071 u32 reason, unsigned long qualification)
11072{
11073 load_vmcs12_host_state(vcpu, vmcs12);
11074 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11075 vmcs12->exit_qualification = qualification;
11076 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011077 if (enable_shadow_vmcs)
11078 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011079}
11080
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011081static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11082 struct x86_instruction_info *info,
11083 enum x86_intercept_stage stage)
11084{
11085 return X86EMUL_CONTINUE;
11086}
11087
Yunhong Jiang64672c92016-06-13 14:19:59 -070011088#ifdef CONFIG_X86_64
11089/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11090static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11091 u64 divisor, u64 *result)
11092{
11093 u64 low = a << shift, high = a >> (64 - shift);
11094
11095 /* To avoid the overflow on divq */
11096 if (high >= divisor)
11097 return 1;
11098
11099 /* Low hold the result, high hold rem which is discarded */
11100 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11101 "rm" (divisor), "0" (low), "1" (high));
11102 *result = low;
11103
11104 return 0;
11105}
11106
11107static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11108{
11109 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011110 u64 tscl = rdtsc();
11111 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11112 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011113
11114 /* Convert to host delta tsc if tsc scaling is enabled */
11115 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11116 u64_shl_div_u64(delta_tsc,
11117 kvm_tsc_scaling_ratio_frac_bits,
11118 vcpu->arch.tsc_scaling_ratio,
11119 &delta_tsc))
11120 return -ERANGE;
11121
11122 /*
11123 * If the delta tsc can't fit in the 32 bit after the multi shift,
11124 * we can't use the preemption timer.
11125 * It's possible that it fits on later vmentries, but checking
11126 * on every vmentry is costly so we just use an hrtimer.
11127 */
11128 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11129 return -ERANGE;
11130
11131 vmx->hv_deadline_tsc = tscl + delta_tsc;
11132 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11133 PIN_BASED_VMX_PREEMPTION_TIMER);
11134 return 0;
11135}
11136
11137static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11138{
11139 struct vcpu_vmx *vmx = to_vmx(vcpu);
11140 vmx->hv_deadline_tsc = -1;
11141 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11142 PIN_BASED_VMX_PREEMPTION_TIMER);
11143}
11144#endif
11145
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011146static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011147{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011148 if (ple_gap)
11149 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011150}
11151
Kai Huang843e4332015-01-28 10:54:28 +080011152static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11153 struct kvm_memory_slot *slot)
11154{
11155 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11156 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11157}
11158
11159static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11160 struct kvm_memory_slot *slot)
11161{
11162 kvm_mmu_slot_set_dirty(kvm, slot);
11163}
11164
11165static void vmx_flush_log_dirty(struct kvm *kvm)
11166{
11167 kvm_flush_pml_buffers(kvm);
11168}
11169
11170static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11171 struct kvm_memory_slot *memslot,
11172 gfn_t offset, unsigned long mask)
11173{
11174 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11175}
11176
Feng Wuefc64402015-09-18 22:29:51 +080011177/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011178 * This routine does the following things for vCPU which is going
11179 * to be blocked if VT-d PI is enabled.
11180 * - Store the vCPU to the wakeup list, so when interrupts happen
11181 * we can find the right vCPU to wake up.
11182 * - Change the Posted-interrupt descriptor as below:
11183 * 'NDST' <-- vcpu->pre_pcpu
11184 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11185 * - If 'ON' is set during this process, which means at least one
11186 * interrupt is posted for this vCPU, we cannot block it, in
11187 * this case, return 1, otherwise, return 0.
11188 *
11189 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011190static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011191{
11192 unsigned long flags;
11193 unsigned int dest;
11194 struct pi_desc old, new;
11195 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11196
11197 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011198 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11199 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011200 return 0;
11201
11202 vcpu->pre_pcpu = vcpu->cpu;
11203 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
11204 vcpu->pre_pcpu), flags);
11205 list_add_tail(&vcpu->blocked_vcpu_list,
11206 &per_cpu(blocked_vcpu_on_cpu,
11207 vcpu->pre_pcpu));
11208 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
11209 vcpu->pre_pcpu), flags);
11210
11211 do {
11212 old.control = new.control = pi_desc->control;
11213
11214 /*
11215 * We should not block the vCPU if
11216 * an interrupt is posted for it.
11217 */
11218 if (pi_test_on(pi_desc) == 1) {
11219 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
11220 vcpu->pre_pcpu), flags);
11221 list_del(&vcpu->blocked_vcpu_list);
11222 spin_unlock_irqrestore(
11223 &per_cpu(blocked_vcpu_on_cpu_lock,
11224 vcpu->pre_pcpu), flags);
11225 vcpu->pre_pcpu = -1;
11226
11227 return 1;
11228 }
11229
11230 WARN((pi_desc->sn == 1),
11231 "Warning: SN field of posted-interrupts "
11232 "is set before blocking\n");
11233
11234 /*
11235 * Since vCPU can be preempted during this process,
11236 * vcpu->cpu could be different with pre_pcpu, we
11237 * need to set pre_pcpu as the destination of wakeup
11238 * notification event, then we can find the right vCPU
11239 * to wakeup in wakeup handler if interrupts happen
11240 * when the vCPU is in blocked state.
11241 */
11242 dest = cpu_physical_id(vcpu->pre_pcpu);
11243
11244 if (x2apic_enabled())
11245 new.ndst = dest;
11246 else
11247 new.ndst = (dest << 8) & 0xFF00;
11248
11249 /* set 'NV' to 'wakeup vector' */
11250 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11251 } while (cmpxchg(&pi_desc->control, old.control,
11252 new.control) != old.control);
11253
11254 return 0;
11255}
11256
Yunhong Jiangbc225122016-06-13 14:19:58 -070011257static int vmx_pre_block(struct kvm_vcpu *vcpu)
11258{
11259 if (pi_pre_block(vcpu))
11260 return 1;
11261
Yunhong Jiang64672c92016-06-13 14:19:59 -070011262 if (kvm_lapic_hv_timer_in_use(vcpu))
11263 kvm_lapic_switch_to_sw_timer(vcpu);
11264
Yunhong Jiangbc225122016-06-13 14:19:58 -070011265 return 0;
11266}
11267
11268static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011269{
11270 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11271 struct pi_desc old, new;
11272 unsigned int dest;
11273 unsigned long flags;
11274
11275 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011276 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11277 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011278 return;
11279
11280 do {
11281 old.control = new.control = pi_desc->control;
11282
11283 dest = cpu_physical_id(vcpu->cpu);
11284
11285 if (x2apic_enabled())
11286 new.ndst = dest;
11287 else
11288 new.ndst = (dest << 8) & 0xFF00;
11289
11290 /* Allow posting non-urgent interrupts */
11291 new.sn = 0;
11292
11293 /* set 'NV' to 'notification vector' */
11294 new.nv = POSTED_INTR_VECTOR;
11295 } while (cmpxchg(&pi_desc->control, old.control,
11296 new.control) != old.control);
11297
11298 if(vcpu->pre_pcpu != -1) {
11299 spin_lock_irqsave(
11300 &per_cpu(blocked_vcpu_on_cpu_lock,
11301 vcpu->pre_pcpu), flags);
11302 list_del(&vcpu->blocked_vcpu_list);
11303 spin_unlock_irqrestore(
11304 &per_cpu(blocked_vcpu_on_cpu_lock,
11305 vcpu->pre_pcpu), flags);
11306 vcpu->pre_pcpu = -1;
11307 }
11308}
11309
Yunhong Jiangbc225122016-06-13 14:19:58 -070011310static void vmx_post_block(struct kvm_vcpu *vcpu)
11311{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011312 if (kvm_x86_ops->set_hv_timer)
11313 kvm_lapic_switch_to_hv_timer(vcpu);
11314
Yunhong Jiangbc225122016-06-13 14:19:58 -070011315 pi_post_block(vcpu);
11316}
11317
Feng Wubf9f6ac2015-09-18 22:29:55 +080011318/*
Feng Wuefc64402015-09-18 22:29:51 +080011319 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11320 *
11321 * @kvm: kvm
11322 * @host_irq: host irq of the interrupt
11323 * @guest_irq: gsi of the interrupt
11324 * @set: set or unset PI
11325 * returns 0 on success, < 0 on failure
11326 */
11327static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11328 uint32_t guest_irq, bool set)
11329{
11330 struct kvm_kernel_irq_routing_entry *e;
11331 struct kvm_irq_routing_table *irq_rt;
11332 struct kvm_lapic_irq irq;
11333 struct kvm_vcpu *vcpu;
11334 struct vcpu_data vcpu_info;
11335 int idx, ret = -EINVAL;
11336
11337 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011338 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11339 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011340 return 0;
11341
11342 idx = srcu_read_lock(&kvm->irq_srcu);
11343 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11344 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
11345
11346 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11347 if (e->type != KVM_IRQ_ROUTING_MSI)
11348 continue;
11349 /*
11350 * VT-d PI cannot support posting multicast/broadcast
11351 * interrupts to a vCPU, we still use interrupt remapping
11352 * for these kind of interrupts.
11353 *
11354 * For lowest-priority interrupts, we only support
11355 * those with single CPU as the destination, e.g. user
11356 * configures the interrupts via /proc/irq or uses
11357 * irqbalance to make the interrupts single-CPU.
11358 *
11359 * We will support full lowest-priority interrupt later.
11360 */
11361
Radim Krčmář371313132016-07-12 22:09:27 +020011362 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011363 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11364 /*
11365 * Make sure the IRTE is in remapped mode if
11366 * we don't handle it in posted mode.
11367 */
11368 ret = irq_set_vcpu_affinity(host_irq, NULL);
11369 if (ret < 0) {
11370 printk(KERN_INFO
11371 "failed to back to remapped mode, irq: %u\n",
11372 host_irq);
11373 goto out;
11374 }
11375
Feng Wuefc64402015-09-18 22:29:51 +080011376 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011377 }
Feng Wuefc64402015-09-18 22:29:51 +080011378
11379 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11380 vcpu_info.vector = irq.vector;
11381
Feng Wub6ce9782016-01-25 16:53:35 +080011382 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011383 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11384
11385 if (set)
11386 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11387 else {
11388 /* suppress notification event before unposting */
11389 pi_set_sn(vcpu_to_pi_desc(vcpu));
11390 ret = irq_set_vcpu_affinity(host_irq, NULL);
11391 pi_clear_sn(vcpu_to_pi_desc(vcpu));
11392 }
11393
11394 if (ret < 0) {
11395 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11396 __func__);
11397 goto out;
11398 }
11399 }
11400
11401 ret = 0;
11402out:
11403 srcu_read_unlock(&kvm->irq_srcu, idx);
11404 return ret;
11405}
11406
Ashok Rajc45dcc72016-06-22 14:59:56 +080011407static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11408{
11409 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11410 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11411 FEATURE_CONTROL_LMCE;
11412 else
11413 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11414 ~FEATURE_CONTROL_LMCE;
11415}
11416
Kees Cook404f6aa2016-08-08 16:29:06 -070011417static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011418 .cpu_has_kvm_support = cpu_has_kvm_support,
11419 .disabled_by_bios = vmx_disabled_by_bios,
11420 .hardware_setup = hardware_setup,
11421 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011422 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011423 .hardware_enable = hardware_enable,
11424 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011425 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020011426 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011427
11428 .vcpu_create = vmx_create_vcpu,
11429 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011430 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011431
Avi Kivity04d2cc72007-09-10 18:10:54 +030011432 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011433 .vcpu_load = vmx_vcpu_load,
11434 .vcpu_put = vmx_vcpu_put,
11435
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011436 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011437 .get_msr = vmx_get_msr,
11438 .set_msr = vmx_set_msr,
11439 .get_segment_base = vmx_get_segment_base,
11440 .get_segment = vmx_get_segment,
11441 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011442 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011443 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011444 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011445 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011446 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011447 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011448 .set_cr3 = vmx_set_cr3,
11449 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011450 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011451 .get_idt = vmx_get_idt,
11452 .set_idt = vmx_set_idt,
11453 .get_gdt = vmx_get_gdt,
11454 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011455 .get_dr6 = vmx_get_dr6,
11456 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011457 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011458 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011459 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011460 .get_rflags = vmx_get_rflags,
11461 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011462
11463 .get_pkru = vmx_get_pkru,
11464
Avi Kivity6aa8b732006-12-10 02:21:36 -080011465 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011466
Avi Kivity6aa8b732006-12-10 02:21:36 -080011467 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011468 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011469 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011470 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11471 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011472 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011473 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011474 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011475 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011476 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011477 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011478 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011479 .get_nmi_mask = vmx_get_nmi_mask,
11480 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011481 .enable_nmi_window = enable_nmi_window,
11482 .enable_irq_window = enable_irq_window,
11483 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011484 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011485 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011486 .get_enable_apicv = vmx_get_enable_apicv,
11487 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011488 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010011489 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011490 .hwapic_irr_update = vmx_hwapic_irr_update,
11491 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011492 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11493 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011494
Izik Eiduscbc94022007-10-25 00:29:55 +020011495 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011496 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011497 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011498
Avi Kivity586f9602010-11-18 13:09:54 +020011499 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011500
Sheng Yang17cc3932010-01-05 19:02:27 +080011501 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011502
11503 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011504
11505 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011506 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011507
11508 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011509
11510 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011511
11512 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011513
11514 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011515
11516 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011517 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011518 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011519 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011520
11521 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011522
11523 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011524
11525 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11526 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11527 .flush_log_dirty = vmx_flush_log_dirty,
11528 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011529
Feng Wubf9f6ac2015-09-18 22:29:55 +080011530 .pre_block = vmx_pre_block,
11531 .post_block = vmx_post_block,
11532
Wei Huang25462f72015-06-19 15:45:05 +020011533 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011534
11535 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011536
11537#ifdef CONFIG_X86_64
11538 .set_hv_timer = vmx_set_hv_timer,
11539 .cancel_hv_timer = vmx_cancel_hv_timer,
11540#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011541
11542 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011543};
11544
11545static int __init vmx_init(void)
11546{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011547 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11548 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011549 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011550 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011551
Dave Young2965faa2015-09-09 15:38:55 -070011552#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011553 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11554 crash_vmclear_local_loaded_vmcss);
11555#endif
11556
He, Qingfdef3ad2007-04-30 09:45:24 +030011557 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011558}
11559
11560static void __exit vmx_exit(void)
11561{
Dave Young2965faa2015-09-09 15:38:55 -070011562#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011563 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011564 synchronize_rcu();
11565#endif
11566
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011567 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011568}
11569
11570module_init(vmx_init)
11571module_exit(vmx_exit)