blob: 0f0b27d96f2741e0d7098966f0c0e36937cb38d9 [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/*
136 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
137 * ple_gap: upper bound on the amount of time between two successive
138 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500139 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800140 * ple_window: upper bound on the amount of time a guest is allowed to execute
141 * in a PAUSE loop. Tests indicate that most spinlocks are held for
142 * less than 2^12 cycles
143 * Time is measured based on a counter that runs at the same rate as the TSC,
144 * refer SDM volume 3b section 21.6.13 & 22.1.3.
145 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200146#define KVM_VMX_DEFAULT_PLE_GAP 128
147#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
148#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
149#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
150#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
151 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
152
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
154module_param(ple_gap, int, S_IRUGO);
155
156static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
157module_param(ple_window, int, S_IRUGO);
158
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200159/* Default doubles per-vcpu window every exit. */
160static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
161module_param(ple_window_grow, int, S_IRUGO);
162
163/* Default resets per-vcpu window every exit to ple_window. */
164static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
165module_param(ple_window_shrink, int, S_IRUGO);
166
167/* Default is to compute the maximum so we can never overflow. */
168static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
169static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
170module_param(ple_window_max, int, S_IRUGO);
171
Avi Kivity83287ea422012-09-16 15:10:57 +0300172extern const ulong vmx_return;
173
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200174#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300175#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300176
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400177struct vmcs {
178 u32 revision_id;
179 u32 abort;
180 char data[0];
181};
182
Nadav Har'Eld462b812011-05-24 15:26:10 +0300183/*
184 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
185 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
186 * loaded on this CPU (so we can clear them if the CPU goes down).
187 */
188struct loaded_vmcs {
189 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700190 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300191 int cpu;
192 int launched;
193 struct list_head loaded_vmcss_on_cpu_link;
194};
195
Avi Kivity26bb0982009-09-07 11:14:12 +0300196struct shared_msr_entry {
197 unsigned index;
198 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200199 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300200};
201
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300202/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300203 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
204 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
205 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
206 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
207 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
208 * More than one of these structures may exist, if L1 runs multiple L2 guests.
209 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
210 * underlying hardware which will be used to run L2.
211 * This structure is packed to ensure that its layout is identical across
212 * machines (necessary for live migration).
213 * If there are changes in this struct, VMCS12_REVISION must be changed.
214 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300215typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300216struct __packed vmcs12 {
217 /* According to the Intel spec, a VMCS region must start with the
218 * following two fields. Then follow implementation-specific data.
219 */
220 u32 revision_id;
221 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300222
Nadav Har'El27d6c862011-05-25 23:06:59 +0300223 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
224 u32 padding[7]; /* room for future expansion */
225
Nadav Har'El22bd0352011-05-25 23:05:57 +0300226 u64 io_bitmap_a;
227 u64 io_bitmap_b;
228 u64 msr_bitmap;
229 u64 vm_exit_msr_store_addr;
230 u64 vm_exit_msr_load_addr;
231 u64 vm_entry_msr_load_addr;
232 u64 tsc_offset;
233 u64 virtual_apic_page_addr;
234 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800235 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300236 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800237 u64 eoi_exit_bitmap0;
238 u64 eoi_exit_bitmap1;
239 u64 eoi_exit_bitmap2;
240 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800241 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300242 u64 guest_physical_address;
243 u64 vmcs_link_pointer;
244 u64 guest_ia32_debugctl;
245 u64 guest_ia32_pat;
246 u64 guest_ia32_efer;
247 u64 guest_ia32_perf_global_ctrl;
248 u64 guest_pdptr0;
249 u64 guest_pdptr1;
250 u64 guest_pdptr2;
251 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100252 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300253 u64 host_ia32_pat;
254 u64 host_ia32_efer;
255 u64 host_ia32_perf_global_ctrl;
256 u64 padding64[8]; /* room for future expansion */
257 /*
258 * To allow migration of L1 (complete with its L2 guests) between
259 * machines of different natural widths (32 or 64 bit), we cannot have
260 * unsigned long fields with no explict size. We use u64 (aliased
261 * natural_width) instead. Luckily, x86 is little-endian.
262 */
263 natural_width cr0_guest_host_mask;
264 natural_width cr4_guest_host_mask;
265 natural_width cr0_read_shadow;
266 natural_width cr4_read_shadow;
267 natural_width cr3_target_value0;
268 natural_width cr3_target_value1;
269 natural_width cr3_target_value2;
270 natural_width cr3_target_value3;
271 natural_width exit_qualification;
272 natural_width guest_linear_address;
273 natural_width guest_cr0;
274 natural_width guest_cr3;
275 natural_width guest_cr4;
276 natural_width guest_es_base;
277 natural_width guest_cs_base;
278 natural_width guest_ss_base;
279 natural_width guest_ds_base;
280 natural_width guest_fs_base;
281 natural_width guest_gs_base;
282 natural_width guest_ldtr_base;
283 natural_width guest_tr_base;
284 natural_width guest_gdtr_base;
285 natural_width guest_idtr_base;
286 natural_width guest_dr7;
287 natural_width guest_rsp;
288 natural_width guest_rip;
289 natural_width guest_rflags;
290 natural_width guest_pending_dbg_exceptions;
291 natural_width guest_sysenter_esp;
292 natural_width guest_sysenter_eip;
293 natural_width host_cr0;
294 natural_width host_cr3;
295 natural_width host_cr4;
296 natural_width host_fs_base;
297 natural_width host_gs_base;
298 natural_width host_tr_base;
299 natural_width host_gdtr_base;
300 natural_width host_idtr_base;
301 natural_width host_ia32_sysenter_esp;
302 natural_width host_ia32_sysenter_eip;
303 natural_width host_rsp;
304 natural_width host_rip;
305 natural_width paddingl[8]; /* room for future expansion */
306 u32 pin_based_vm_exec_control;
307 u32 cpu_based_vm_exec_control;
308 u32 exception_bitmap;
309 u32 page_fault_error_code_mask;
310 u32 page_fault_error_code_match;
311 u32 cr3_target_count;
312 u32 vm_exit_controls;
313 u32 vm_exit_msr_store_count;
314 u32 vm_exit_msr_load_count;
315 u32 vm_entry_controls;
316 u32 vm_entry_msr_load_count;
317 u32 vm_entry_intr_info_field;
318 u32 vm_entry_exception_error_code;
319 u32 vm_entry_instruction_len;
320 u32 tpr_threshold;
321 u32 secondary_vm_exec_control;
322 u32 vm_instruction_error;
323 u32 vm_exit_reason;
324 u32 vm_exit_intr_info;
325 u32 vm_exit_intr_error_code;
326 u32 idt_vectoring_info_field;
327 u32 idt_vectoring_error_code;
328 u32 vm_exit_instruction_len;
329 u32 vmx_instruction_info;
330 u32 guest_es_limit;
331 u32 guest_cs_limit;
332 u32 guest_ss_limit;
333 u32 guest_ds_limit;
334 u32 guest_fs_limit;
335 u32 guest_gs_limit;
336 u32 guest_ldtr_limit;
337 u32 guest_tr_limit;
338 u32 guest_gdtr_limit;
339 u32 guest_idtr_limit;
340 u32 guest_es_ar_bytes;
341 u32 guest_cs_ar_bytes;
342 u32 guest_ss_ar_bytes;
343 u32 guest_ds_ar_bytes;
344 u32 guest_fs_ar_bytes;
345 u32 guest_gs_ar_bytes;
346 u32 guest_ldtr_ar_bytes;
347 u32 guest_tr_ar_bytes;
348 u32 guest_interruptibility_info;
349 u32 guest_activity_state;
350 u32 guest_sysenter_cs;
351 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100352 u32 vmx_preemption_timer_value;
353 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300354 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800355 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300356 u16 guest_es_selector;
357 u16 guest_cs_selector;
358 u16 guest_ss_selector;
359 u16 guest_ds_selector;
360 u16 guest_fs_selector;
361 u16 guest_gs_selector;
362 u16 guest_ldtr_selector;
363 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800364 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300365 u16 host_es_selector;
366 u16 host_cs_selector;
367 u16 host_ss_selector;
368 u16 host_ds_selector;
369 u16 host_fs_selector;
370 u16 host_gs_selector;
371 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300372};
373
374/*
375 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
376 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
377 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
378 */
379#define VMCS12_REVISION 0x11e57ed0
380
381/*
382 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
383 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
384 * current implementation, 4K are reserved to avoid future complications.
385 */
386#define VMCS12_SIZE 0x1000
387
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300388/* Used to remember the last vmcs02 used for some recently used vmcs12s */
389struct vmcs02_list {
390 struct list_head list;
391 gpa_t vmptr;
392 struct loaded_vmcs vmcs02;
393};
394
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300395/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300396 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
397 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
398 */
399struct nested_vmx {
400 /* Has the level1 guest done vmxon? */
401 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400402 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300403
404 /* The guest-physical address of the current VMCS L1 keeps for L2 */
405 gpa_t current_vmptr;
406 /* The host-usable pointer to the above */
407 struct page *current_vmcs12_page;
408 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700409 /*
410 * Cache of the guest's VMCS, existing outside of guest memory.
411 * Loaded from guest memory during VMPTRLD. Flushed to guest
412 * memory during VMXOFF, VMCLEAR, VMPTRLD.
413 */
414 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300415 /*
416 * Indicates if the shadow vmcs must be updated with the
417 * data hold by vmcs12
418 */
419 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300420
421 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
422 struct list_head vmcs02_pool;
423 int vmcs02_num;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200424 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300425 /* L2 must run next, and mustn't decide to exit to L1. */
426 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300427 /*
428 * Guest pages referred to in vmcs02 with host-physical pointers, so
429 * we must keep them pinned while L2 runs.
430 */
431 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800432 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800433 struct page *pi_desc_page;
434 struct pi_desc *pi_desc;
435 bool pi_pending;
436 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100437
Radim Krčmářd048c092016-08-08 20:16:22 +0200438 unsigned long *msr_bitmap;
439
Jan Kiszkaf4124502014-03-07 20:03:13 +0100440 struct hrtimer preemption_timer;
441 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200442
443 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
444 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800445
Wanpeng Li5c614b32015-10-13 09:18:36 -0700446 u16 vpid02;
447 u16 last_vpid;
448
Wincy Vanb9c237b2015-02-03 23:56:30 +0800449 u32 nested_vmx_procbased_ctls_low;
450 u32 nested_vmx_procbased_ctls_high;
451 u32 nested_vmx_true_procbased_ctls_low;
452 u32 nested_vmx_secondary_ctls_low;
453 u32 nested_vmx_secondary_ctls_high;
454 u32 nested_vmx_pinbased_ctls_low;
455 u32 nested_vmx_pinbased_ctls_high;
456 u32 nested_vmx_exit_ctls_low;
457 u32 nested_vmx_exit_ctls_high;
458 u32 nested_vmx_true_exit_ctls_low;
459 u32 nested_vmx_entry_ctls_low;
460 u32 nested_vmx_entry_ctls_high;
461 u32 nested_vmx_true_entry_ctls_low;
462 u32 nested_vmx_misc_low;
463 u32 nested_vmx_misc_high;
464 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700465 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300466};
467
Yang Zhang01e439b2013-04-11 19:25:12 +0800468#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800469#define POSTED_INTR_SN 1
470
Yang Zhang01e439b2013-04-11 19:25:12 +0800471/* Posted-Interrupt Descriptor */
472struct pi_desc {
473 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800474 union {
475 struct {
476 /* bit 256 - Outstanding Notification */
477 u16 on : 1,
478 /* bit 257 - Suppress Notification */
479 sn : 1,
480 /* bit 271:258 - Reserved */
481 rsvd_1 : 14;
482 /* bit 279:272 - Notification Vector */
483 u8 nv;
484 /* bit 287:280 - Reserved */
485 u8 rsvd_2;
486 /* bit 319:288 - Notification Destination */
487 u32 ndst;
488 };
489 u64 control;
490 };
491 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800492} __aligned(64);
493
Yang Zhanga20ed542013-04-11 19:25:15 +0800494static bool pi_test_and_set_on(struct pi_desc *pi_desc)
495{
496 return test_and_set_bit(POSTED_INTR_ON,
497 (unsigned long *)&pi_desc->control);
498}
499
500static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
501{
502 return test_and_clear_bit(POSTED_INTR_ON,
503 (unsigned long *)&pi_desc->control);
504}
505
506static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
507{
508 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
509}
510
Feng Wuebbfc762015-09-18 22:29:46 +0800511static inline void pi_clear_sn(struct pi_desc *pi_desc)
512{
513 return clear_bit(POSTED_INTR_SN,
514 (unsigned long *)&pi_desc->control);
515}
516
517static inline void pi_set_sn(struct pi_desc *pi_desc)
518{
519 return set_bit(POSTED_INTR_SN,
520 (unsigned long *)&pi_desc->control);
521}
522
523static inline int pi_test_on(struct pi_desc *pi_desc)
524{
525 return test_bit(POSTED_INTR_ON,
526 (unsigned long *)&pi_desc->control);
527}
528
529static inline int pi_test_sn(struct pi_desc *pi_desc)
530{
531 return test_bit(POSTED_INTR_SN,
532 (unsigned long *)&pi_desc->control);
533}
534
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400535struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000536 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300537 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300538 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200539 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300540 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200541 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200542 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300543 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400544 int nmsrs;
545 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800546 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400547#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300548 u64 msr_host_kernel_gs_base;
549 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400550#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200551 u32 vm_entry_controls_shadow;
552 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300553 /*
554 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
555 * non-nested (L1) guest, it always points to vmcs01. For a nested
556 * guest (L2), it points to a different VMCS.
557 */
558 struct loaded_vmcs vmcs01;
559 struct loaded_vmcs *loaded_vmcs;
560 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300561 struct msr_autoload {
562 unsigned nr;
563 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
564 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
565 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400566 struct {
567 int loaded;
568 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300569#ifdef CONFIG_X86_64
570 u16 ds_sel, es_sel;
571#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200572 int gs_ldt_reload_needed;
573 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000574 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700575 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400576 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200577 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300578 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300579 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300580 struct kvm_segment segs[8];
581 } rmode;
582 struct {
583 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300584 struct kvm_save_segment {
585 u16 selector;
586 unsigned long base;
587 u32 limit;
588 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300589 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300590 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800591 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300592 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200593
594 /* Support for vnmi-less CPUs */
595 int soft_vnmi_blocked;
596 ktime_t entry_time;
597 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800598 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800599
Yang Zhang01e439b2013-04-11 19:25:12 +0800600 /* Posted interrupt descriptor */
601 struct pi_desc pi_desc;
602
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300603 /* Support for a guest hypervisor (nested VMX) */
604 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200605
606 /* Dynamic PLE window. */
607 int ple_window;
608 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800609
610 /* Support for PML */
611#define PML_ENTITY_NUM 512
612 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800613
Yunhong Jiang64672c92016-06-13 14:19:59 -0700614 /* apic deadline value in host tsc */
615 u64 hv_deadline_tsc;
616
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800617 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800618
619 bool guest_pkru_valid;
620 u32 guest_pkru;
621 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800622
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800623 /*
624 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
625 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
626 * in msr_ia32_feature_control_valid_bits.
627 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800628 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800629 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400630};
631
Avi Kivity2fb92db2011-04-27 19:42:18 +0300632enum segment_cache_field {
633 SEG_FIELD_SEL = 0,
634 SEG_FIELD_BASE = 1,
635 SEG_FIELD_LIMIT = 2,
636 SEG_FIELD_AR = 3,
637
638 SEG_FIELD_NR = 4
639};
640
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400641static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
642{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000643 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400644}
645
Feng Wuefc64402015-09-18 22:29:51 +0800646static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
647{
648 return &(to_vmx(vcpu)->pi_desc);
649}
650
Nadav Har'El22bd0352011-05-25 23:05:57 +0300651#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
652#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
653#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
654 [number##_HIGH] = VMCS12_OFFSET(name)+4
655
Abel Gordon4607c2d2013-04-18 14:35:55 +0300656
Bandan Dasfe2b2012014-04-21 15:20:14 -0400657static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300658 /*
659 * We do NOT shadow fields that are modified when L0
660 * traps and emulates any vmx instruction (e.g. VMPTRLD,
661 * VMXON...) executed by L1.
662 * For example, VM_INSTRUCTION_ERROR is read
663 * by L1 if a vmx instruction fails (part of the error path).
664 * Note the code assumes this logic. If for some reason
665 * we start shadowing these fields then we need to
666 * force a shadow sync when L0 emulates vmx instructions
667 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
668 * by nested_vmx_failValid)
669 */
670 VM_EXIT_REASON,
671 VM_EXIT_INTR_INFO,
672 VM_EXIT_INSTRUCTION_LEN,
673 IDT_VECTORING_INFO_FIELD,
674 IDT_VECTORING_ERROR_CODE,
675 VM_EXIT_INTR_ERROR_CODE,
676 EXIT_QUALIFICATION,
677 GUEST_LINEAR_ADDRESS,
678 GUEST_PHYSICAL_ADDRESS
679};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400680static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300681 ARRAY_SIZE(shadow_read_only_fields);
682
Bandan Dasfe2b2012014-04-21 15:20:14 -0400683static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800684 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300685 GUEST_RIP,
686 GUEST_RSP,
687 GUEST_CR0,
688 GUEST_CR3,
689 GUEST_CR4,
690 GUEST_INTERRUPTIBILITY_INFO,
691 GUEST_RFLAGS,
692 GUEST_CS_SELECTOR,
693 GUEST_CS_AR_BYTES,
694 GUEST_CS_LIMIT,
695 GUEST_CS_BASE,
696 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100697 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300698 CR0_GUEST_HOST_MASK,
699 CR0_READ_SHADOW,
700 CR4_READ_SHADOW,
701 TSC_OFFSET,
702 EXCEPTION_BITMAP,
703 CPU_BASED_VM_EXEC_CONTROL,
704 VM_ENTRY_EXCEPTION_ERROR_CODE,
705 VM_ENTRY_INTR_INFO_FIELD,
706 VM_ENTRY_INSTRUCTION_LEN,
707 VM_ENTRY_EXCEPTION_ERROR_CODE,
708 HOST_FS_BASE,
709 HOST_GS_BASE,
710 HOST_FS_SELECTOR,
711 HOST_GS_SELECTOR
712};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400713static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300714 ARRAY_SIZE(shadow_read_write_fields);
715
Mathias Krause772e0312012-08-30 01:30:19 +0200716static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300717 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800718 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300719 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
720 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
721 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
722 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
723 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
724 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
725 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
726 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800727 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300728 FIELD(HOST_ES_SELECTOR, host_es_selector),
729 FIELD(HOST_CS_SELECTOR, host_cs_selector),
730 FIELD(HOST_SS_SELECTOR, host_ss_selector),
731 FIELD(HOST_DS_SELECTOR, host_ds_selector),
732 FIELD(HOST_FS_SELECTOR, host_fs_selector),
733 FIELD(HOST_GS_SELECTOR, host_gs_selector),
734 FIELD(HOST_TR_SELECTOR, host_tr_selector),
735 FIELD64(IO_BITMAP_A, io_bitmap_a),
736 FIELD64(IO_BITMAP_B, io_bitmap_b),
737 FIELD64(MSR_BITMAP, msr_bitmap),
738 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
739 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
740 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
741 FIELD64(TSC_OFFSET, tsc_offset),
742 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
743 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800744 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300745 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800746 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
747 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
748 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
749 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800750 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300751 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
752 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
753 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
754 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
755 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
756 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
757 FIELD64(GUEST_PDPTR0, guest_pdptr0),
758 FIELD64(GUEST_PDPTR1, guest_pdptr1),
759 FIELD64(GUEST_PDPTR2, guest_pdptr2),
760 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100761 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300762 FIELD64(HOST_IA32_PAT, host_ia32_pat),
763 FIELD64(HOST_IA32_EFER, host_ia32_efer),
764 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
765 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
766 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
767 FIELD(EXCEPTION_BITMAP, exception_bitmap),
768 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
769 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
770 FIELD(CR3_TARGET_COUNT, cr3_target_count),
771 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
772 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
773 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
774 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
775 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
776 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
777 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
778 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
779 FIELD(TPR_THRESHOLD, tpr_threshold),
780 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
781 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
782 FIELD(VM_EXIT_REASON, vm_exit_reason),
783 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
784 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
785 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
786 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
787 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
788 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
789 FIELD(GUEST_ES_LIMIT, guest_es_limit),
790 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
791 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
792 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
793 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
794 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
795 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
796 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
797 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
798 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
799 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
800 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
801 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
802 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
803 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
804 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
805 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
806 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
807 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
808 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
809 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
810 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100811 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300812 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
813 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
814 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
815 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
816 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
817 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
818 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
819 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
820 FIELD(EXIT_QUALIFICATION, exit_qualification),
821 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
822 FIELD(GUEST_CR0, guest_cr0),
823 FIELD(GUEST_CR3, guest_cr3),
824 FIELD(GUEST_CR4, guest_cr4),
825 FIELD(GUEST_ES_BASE, guest_es_base),
826 FIELD(GUEST_CS_BASE, guest_cs_base),
827 FIELD(GUEST_SS_BASE, guest_ss_base),
828 FIELD(GUEST_DS_BASE, guest_ds_base),
829 FIELD(GUEST_FS_BASE, guest_fs_base),
830 FIELD(GUEST_GS_BASE, guest_gs_base),
831 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
832 FIELD(GUEST_TR_BASE, guest_tr_base),
833 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
834 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
835 FIELD(GUEST_DR7, guest_dr7),
836 FIELD(GUEST_RSP, guest_rsp),
837 FIELD(GUEST_RIP, guest_rip),
838 FIELD(GUEST_RFLAGS, guest_rflags),
839 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
840 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
841 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
842 FIELD(HOST_CR0, host_cr0),
843 FIELD(HOST_CR3, host_cr3),
844 FIELD(HOST_CR4, host_cr4),
845 FIELD(HOST_FS_BASE, host_fs_base),
846 FIELD(HOST_GS_BASE, host_gs_base),
847 FIELD(HOST_TR_BASE, host_tr_base),
848 FIELD(HOST_GDTR_BASE, host_gdtr_base),
849 FIELD(HOST_IDTR_BASE, host_idtr_base),
850 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
851 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
852 FIELD(HOST_RSP, host_rsp),
853 FIELD(HOST_RIP, host_rip),
854};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300855
856static inline short vmcs_field_to_offset(unsigned long field)
857{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100858 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
859
860 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
861 vmcs_field_to_offset_table[field] == 0)
862 return -ENOENT;
863
Nadav Har'El22bd0352011-05-25 23:05:57 +0300864 return vmcs_field_to_offset_table[field];
865}
866
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300867static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
868{
David Matlack4f2777b2016-07-13 17:16:37 -0700869 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300870}
871
872static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
873{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200874 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800875 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300876 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800877
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300878 return page;
879}
880
881static void nested_release_page(struct page *page)
882{
883 kvm_release_page_dirty(page);
884}
885
886static void nested_release_page_clean(struct page *page)
887{
888 kvm_release_page_clean(page);
889}
890
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300891static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800892static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800893static void kvm_cpu_vmxon(u64 addr);
894static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800895static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200896static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300897static void vmx_set_segment(struct kvm_vcpu *vcpu,
898 struct kvm_segment *var, int seg);
899static void vmx_get_segment(struct kvm_vcpu *vcpu,
900 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200901static bool guest_state_valid(struct kvm_vcpu *vcpu);
902static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300903static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300904static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800905static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300906
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907static DEFINE_PER_CPU(struct vmcs *, vmxarea);
908static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300909/*
910 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
911 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
912 */
913static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300914static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915
Feng Wubf9f6ac2015-09-18 22:29:55 +0800916/*
917 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
918 * can find which vCPU should be waken up.
919 */
920static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
921static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
922
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200923static unsigned long *vmx_io_bitmap_a;
924static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200925static unsigned long *vmx_msr_bitmap_legacy;
926static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800927static unsigned long *vmx_msr_bitmap_legacy_x2apic;
928static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +0800929static unsigned long *vmx_msr_bitmap_legacy_x2apic_apicv_inactive;
930static unsigned long *vmx_msr_bitmap_longmode_x2apic_apicv_inactive;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300931static unsigned long *vmx_vmread_bitmap;
932static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300933
Avi Kivity110312c2010-12-21 12:54:20 +0200934static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200935static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200936
Sheng Yang2384d2b2008-01-17 15:14:33 +0800937static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
938static DEFINE_SPINLOCK(vmx_vpid_lock);
939
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300940static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 int size;
942 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300943 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300945 u32 pin_based_exec_ctrl;
946 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800947 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300948 u32 vmexit_ctrl;
949 u32 vmentry_ctrl;
950} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951
Hannes Ederefff9e52008-11-28 17:02:06 +0100952static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800953 u32 ept;
954 u32 vpid;
955} vmx_capability;
956
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957#define VMX_SEGMENT_FIELD(seg) \
958 [VCPU_SREG_##seg] = { \
959 .selector = GUEST_##seg##_SELECTOR, \
960 .base = GUEST_##seg##_BASE, \
961 .limit = GUEST_##seg##_LIMIT, \
962 .ar_bytes = GUEST_##seg##_AR_BYTES, \
963 }
964
Mathias Krause772e0312012-08-30 01:30:19 +0200965static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 unsigned selector;
967 unsigned base;
968 unsigned limit;
969 unsigned ar_bytes;
970} kvm_vmx_segment_fields[] = {
971 VMX_SEGMENT_FIELD(CS),
972 VMX_SEGMENT_FIELD(DS),
973 VMX_SEGMENT_FIELD(ES),
974 VMX_SEGMENT_FIELD(FS),
975 VMX_SEGMENT_FIELD(GS),
976 VMX_SEGMENT_FIELD(SS),
977 VMX_SEGMENT_FIELD(TR),
978 VMX_SEGMENT_FIELD(LDTR),
979};
980
Avi Kivity26bb0982009-09-07 11:14:12 +0300981static u64 host_efer;
982
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300983static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
984
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300985/*
Brian Gerst8c065852010-07-17 09:03:26 -0400986 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300987 * away by decrementing the array size.
988 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800990#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300991 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400993 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995
Jan Kiszka5bb16012016-02-09 20:14:21 +0100996static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997{
998 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
999 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001000 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1001}
1002
Jan Kiszka6f054852016-02-09 20:15:18 +01001003static inline bool is_debug(u32 intr_info)
1004{
1005 return is_exception_n(intr_info, DB_VECTOR);
1006}
1007
1008static inline bool is_breakpoint(u32 intr_info)
1009{
1010 return is_exception_n(intr_info, BP_VECTOR);
1011}
1012
Jan Kiszka5bb16012016-02-09 20:14:21 +01001013static inline bool is_page_fault(u32 intr_info)
1014{
1015 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016}
1017
Gui Jianfeng31299942010-03-15 17:29:09 +08001018static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001019{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001020 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001021}
1022
Gui Jianfeng31299942010-03-15 17:29:09 +08001023static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001024{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001025 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001026}
1027
Gui Jianfeng31299942010-03-15 17:29:09 +08001028static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029{
1030 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1031 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1032}
1033
Gui Jianfeng31299942010-03-15 17:29:09 +08001034static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001035{
1036 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1037 INTR_INFO_VALID_MASK)) ==
1038 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1039}
1040
Gui Jianfeng31299942010-03-15 17:29:09 +08001041static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001042{
Sheng Yang04547152009-04-01 15:52:31 +08001043 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001044}
1045
Gui Jianfeng31299942010-03-15 17:29:09 +08001046static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001047{
Sheng Yang04547152009-04-01 15:52:31 +08001048 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001049}
1050
Paolo Bonzini35754c92015-07-29 12:05:37 +02001051static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001052{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001053 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001054}
1055
Gui Jianfeng31299942010-03-15 17:29:09 +08001056static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001057{
Sheng Yang04547152009-04-01 15:52:31 +08001058 return vmcs_config.cpu_based_exec_ctrl &
1059 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001060}
1061
Avi Kivity774ead32007-12-26 13:57:04 +02001062static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001063{
Sheng Yang04547152009-04-01 15:52:31 +08001064 return vmcs_config.cpu_based_2nd_exec_ctrl &
1065 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1066}
1067
Yang Zhang8d146952013-01-25 10:18:50 +08001068static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1069{
1070 return vmcs_config.cpu_based_2nd_exec_ctrl &
1071 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1072}
1073
Yang Zhang83d4c282013-01-25 10:18:49 +08001074static inline bool cpu_has_vmx_apic_register_virt(void)
1075{
1076 return vmcs_config.cpu_based_2nd_exec_ctrl &
1077 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1078}
1079
Yang Zhangc7c9c562013-01-25 10:18:51 +08001080static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1081{
1082 return vmcs_config.cpu_based_2nd_exec_ctrl &
1083 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1084}
1085
Yunhong Jiang64672c92016-06-13 14:19:59 -07001086/*
1087 * Comment's format: document - errata name - stepping - processor name.
1088 * Refer from
1089 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1090 */
1091static u32 vmx_preemption_cpu_tfms[] = {
1092/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
10930x000206E6,
1094/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1095/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1096/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
10970x00020652,
1098/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
10990x00020655,
1100/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1101/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1102/*
1103 * 320767.pdf - AAP86 - B1 -
1104 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1105 */
11060x000106E5,
1107/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11080x000106A0,
1109/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11100x000106A1,
1111/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11120x000106A4,
1113 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1114 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1115 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11160x000106A5,
1117};
1118
1119static inline bool cpu_has_broken_vmx_preemption_timer(void)
1120{
1121 u32 eax = cpuid_eax(0x00000001), i;
1122
1123 /* Clear the reserved bits */
1124 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001125 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001126 if (eax == vmx_preemption_cpu_tfms[i])
1127 return true;
1128
1129 return false;
1130}
1131
1132static inline bool cpu_has_vmx_preemption_timer(void)
1133{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001134 return vmcs_config.pin_based_exec_ctrl &
1135 PIN_BASED_VMX_PREEMPTION_TIMER;
1136}
1137
Yang Zhang01e439b2013-04-11 19:25:12 +08001138static inline bool cpu_has_vmx_posted_intr(void)
1139{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001140 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1141 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001142}
1143
1144static inline bool cpu_has_vmx_apicv(void)
1145{
1146 return cpu_has_vmx_apic_register_virt() &&
1147 cpu_has_vmx_virtual_intr_delivery() &&
1148 cpu_has_vmx_posted_intr();
1149}
1150
Sheng Yang04547152009-04-01 15:52:31 +08001151static inline bool cpu_has_vmx_flexpriority(void)
1152{
1153 return cpu_has_vmx_tpr_shadow() &&
1154 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001155}
1156
Marcelo Tosattie7997942009-06-11 12:07:40 -03001157static inline bool cpu_has_vmx_ept_execute_only(void)
1158{
Gui Jianfeng31299942010-03-15 17:29:09 +08001159 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001160}
1161
Marcelo Tosattie7997942009-06-11 12:07:40 -03001162static inline bool cpu_has_vmx_ept_2m_page(void)
1163{
Gui Jianfeng31299942010-03-15 17:29:09 +08001164 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001165}
1166
Sheng Yang878403b2010-01-05 19:02:29 +08001167static inline bool cpu_has_vmx_ept_1g_page(void)
1168{
Gui Jianfeng31299942010-03-15 17:29:09 +08001169 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001170}
1171
Sheng Yang4bc9b982010-06-02 14:05:24 +08001172static inline bool cpu_has_vmx_ept_4levels(void)
1173{
1174 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1175}
1176
Xudong Hao83c3a332012-05-28 19:33:35 +08001177static inline bool cpu_has_vmx_ept_ad_bits(void)
1178{
1179 return vmx_capability.ept & VMX_EPT_AD_BIT;
1180}
1181
Gui Jianfeng31299942010-03-15 17:29:09 +08001182static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001183{
Gui Jianfeng31299942010-03-15 17:29:09 +08001184 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001185}
1186
Gui Jianfeng31299942010-03-15 17:29:09 +08001187static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001188{
Gui Jianfeng31299942010-03-15 17:29:09 +08001189 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001190}
1191
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001192static inline bool cpu_has_vmx_invvpid_single(void)
1193{
1194 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1195}
1196
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001197static inline bool cpu_has_vmx_invvpid_global(void)
1198{
1199 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1200}
1201
Gui Jianfeng31299942010-03-15 17:29:09 +08001202static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001203{
Sheng Yang04547152009-04-01 15:52:31 +08001204 return vmcs_config.cpu_based_2nd_exec_ctrl &
1205 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001206}
1207
Gui Jianfeng31299942010-03-15 17:29:09 +08001208static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001209{
1210 return vmcs_config.cpu_based_2nd_exec_ctrl &
1211 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1212}
1213
Gui Jianfeng31299942010-03-15 17:29:09 +08001214static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001215{
1216 return vmcs_config.cpu_based_2nd_exec_ctrl &
1217 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1218}
1219
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001220static inline bool cpu_has_vmx_basic_inout(void)
1221{
1222 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1223}
1224
Paolo Bonzini35754c92015-07-29 12:05:37 +02001225static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001226{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001227 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001228}
1229
Gui Jianfeng31299942010-03-15 17:29:09 +08001230static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001231{
Sheng Yang04547152009-04-01 15:52:31 +08001232 return vmcs_config.cpu_based_2nd_exec_ctrl &
1233 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001234}
1235
Gui Jianfeng31299942010-03-15 17:29:09 +08001236static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001237{
1238 return vmcs_config.cpu_based_2nd_exec_ctrl &
1239 SECONDARY_EXEC_RDTSCP;
1240}
1241
Mao, Junjiead756a12012-07-02 01:18:48 +00001242static inline bool cpu_has_vmx_invpcid(void)
1243{
1244 return vmcs_config.cpu_based_2nd_exec_ctrl &
1245 SECONDARY_EXEC_ENABLE_INVPCID;
1246}
1247
Gui Jianfeng31299942010-03-15 17:29:09 +08001248static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001249{
1250 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1251}
1252
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001253static inline bool cpu_has_vmx_wbinvd_exit(void)
1254{
1255 return vmcs_config.cpu_based_2nd_exec_ctrl &
1256 SECONDARY_EXEC_WBINVD_EXITING;
1257}
1258
Abel Gordonabc4fc52013-04-18 14:35:25 +03001259static inline bool cpu_has_vmx_shadow_vmcs(void)
1260{
1261 u64 vmx_msr;
1262 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1263 /* check if the cpu supports writing r/o exit information fields */
1264 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1265 return false;
1266
1267 return vmcs_config.cpu_based_2nd_exec_ctrl &
1268 SECONDARY_EXEC_SHADOW_VMCS;
1269}
1270
Kai Huang843e4332015-01-28 10:54:28 +08001271static inline bool cpu_has_vmx_pml(void)
1272{
1273 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1274}
1275
Haozhong Zhang64903d62015-10-20 15:39:09 +08001276static inline bool cpu_has_vmx_tsc_scaling(void)
1277{
1278 return vmcs_config.cpu_based_2nd_exec_ctrl &
1279 SECONDARY_EXEC_TSC_SCALING;
1280}
1281
Sheng Yang04547152009-04-01 15:52:31 +08001282static inline bool report_flexpriority(void)
1283{
1284 return flexpriority_enabled;
1285}
1286
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001287static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1288{
1289 return vmcs12->cpu_based_vm_exec_control & bit;
1290}
1291
1292static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1293{
1294 return (vmcs12->cpu_based_vm_exec_control &
1295 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1296 (vmcs12->secondary_vm_exec_control & bit);
1297}
1298
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001299static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001300{
1301 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1302}
1303
Jan Kiszkaf4124502014-03-07 20:03:13 +01001304static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1305{
1306 return vmcs12->pin_based_vm_exec_control &
1307 PIN_BASED_VMX_PREEMPTION_TIMER;
1308}
1309
Nadav Har'El155a97a2013-08-05 11:07:16 +03001310static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1311{
1312 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1313}
1314
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001315static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1316{
1317 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1318 vmx_xsaves_supported();
1319}
1320
Wincy Vanf2b93282015-02-03 23:56:03 +08001321static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1322{
1323 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1324}
1325
Wanpeng Li5c614b32015-10-13 09:18:36 -07001326static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1327{
1328 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1329}
1330
Wincy Van82f0dd42015-02-03 23:57:18 +08001331static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1332{
1333 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1334}
1335
Wincy Van608406e2015-02-03 23:57:51 +08001336static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1337{
1338 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1339}
1340
Wincy Van705699a2015-02-03 23:58:17 +08001341static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1342{
1343 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1344}
1345
Jim Mattson3f618a02016-12-12 11:01:37 -08001346static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001347{
1348 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattson3f618a02016-12-12 11:01:37 -08001349 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001350}
1351
Jan Kiszka533558b2014-01-04 18:47:20 +01001352static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1353 u32 exit_intr_info,
1354 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001355static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1356 struct vmcs12 *vmcs12,
1357 u32 reason, unsigned long qualification);
1358
Rusty Russell8b9cf982007-07-30 16:31:43 +10001359static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001360{
1361 int i;
1362
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001363 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001364 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001365 return i;
1366 return -1;
1367}
1368
Sheng Yang2384d2b2008-01-17 15:14:33 +08001369static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1370{
1371 struct {
1372 u64 vpid : 16;
1373 u64 rsvd : 48;
1374 u64 gva;
1375 } operand = { vpid, 0, gva };
1376
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001377 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001378 /* CF==1 or ZF==1 --> rc = -1 */
1379 "; ja 1f ; ud2 ; 1:"
1380 : : "a"(&operand), "c"(ext) : "cc", "memory");
1381}
1382
Sheng Yang14394422008-04-28 12:24:45 +08001383static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1384{
1385 struct {
1386 u64 eptp, gpa;
1387 } operand = {eptp, gpa};
1388
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001389 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001390 /* CF==1 or ZF==1 --> rc = -1 */
1391 "; ja 1f ; ud2 ; 1:\n"
1392 : : "a" (&operand), "c" (ext) : "cc", "memory");
1393}
1394
Avi Kivity26bb0982009-09-07 11:14:12 +03001395static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001396{
1397 int i;
1398
Rusty Russell8b9cf982007-07-30 16:31:43 +10001399 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001400 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001401 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001402 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001403}
1404
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405static void vmcs_clear(struct vmcs *vmcs)
1406{
1407 u64 phys_addr = __pa(vmcs);
1408 u8 error;
1409
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001410 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001411 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 : "cc", "memory");
1413 if (error)
1414 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1415 vmcs, phys_addr);
1416}
1417
Nadav Har'Eld462b812011-05-24 15:26:10 +03001418static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1419{
1420 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001421 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1422 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001423 loaded_vmcs->cpu = -1;
1424 loaded_vmcs->launched = 0;
1425}
1426
Dongxiao Xu7725b892010-05-11 18:29:38 +08001427static void vmcs_load(struct vmcs *vmcs)
1428{
1429 u64 phys_addr = __pa(vmcs);
1430 u8 error;
1431
1432 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001433 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001434 : "cc", "memory");
1435 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001436 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001437 vmcs, phys_addr);
1438}
1439
Dave Young2965faa2015-09-09 15:38:55 -07001440#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001441/*
1442 * This bitmap is used to indicate whether the vmclear
1443 * operation is enabled on all cpus. All disabled by
1444 * default.
1445 */
1446static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1447
1448static inline void crash_enable_local_vmclear(int cpu)
1449{
1450 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1451}
1452
1453static inline void crash_disable_local_vmclear(int cpu)
1454{
1455 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1456}
1457
1458static inline int crash_local_vmclear_enabled(int cpu)
1459{
1460 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1461}
1462
1463static void crash_vmclear_local_loaded_vmcss(void)
1464{
1465 int cpu = raw_smp_processor_id();
1466 struct loaded_vmcs *v;
1467
1468 if (!crash_local_vmclear_enabled(cpu))
1469 return;
1470
1471 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1472 loaded_vmcss_on_cpu_link)
1473 vmcs_clear(v->vmcs);
1474}
1475#else
1476static inline void crash_enable_local_vmclear(int cpu) { }
1477static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001478#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001479
Nadav Har'Eld462b812011-05-24 15:26:10 +03001480static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001482 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001483 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484
Nadav Har'Eld462b812011-05-24 15:26:10 +03001485 if (loaded_vmcs->cpu != cpu)
1486 return; /* vcpu migration can race with cpu offline */
1487 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001488 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001489 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001490 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001491
1492 /*
1493 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1494 * is before setting loaded_vmcs->vcpu to -1 which is done in
1495 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1496 * then adds the vmcs into percpu list before it is deleted.
1497 */
1498 smp_wmb();
1499
Nadav Har'Eld462b812011-05-24 15:26:10 +03001500 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001501 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502}
1503
Nadav Har'Eld462b812011-05-24 15:26:10 +03001504static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001505{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001506 int cpu = loaded_vmcs->cpu;
1507
1508 if (cpu != -1)
1509 smp_call_function_single(cpu,
1510 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001511}
1512
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001513static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001514{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001515 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001516 return;
1517
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001518 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001519 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001520}
1521
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001522static inline void vpid_sync_vcpu_global(void)
1523{
1524 if (cpu_has_vmx_invvpid_global())
1525 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1526}
1527
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001528static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001529{
1530 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001531 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001532 else
1533 vpid_sync_vcpu_global();
1534}
1535
Sheng Yang14394422008-04-28 12:24:45 +08001536static inline void ept_sync_global(void)
1537{
1538 if (cpu_has_vmx_invept_global())
1539 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1540}
1541
1542static inline void ept_sync_context(u64 eptp)
1543{
Avi Kivity089d0342009-03-23 18:26:32 +02001544 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001545 if (cpu_has_vmx_invept_context())
1546 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1547 else
1548 ept_sync_global();
1549 }
1550}
1551
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001552static __always_inline void vmcs_check16(unsigned long field)
1553{
1554 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1555 "16-bit accessor invalid for 64-bit field");
1556 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1557 "16-bit accessor invalid for 64-bit high field");
1558 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1559 "16-bit accessor invalid for 32-bit high field");
1560 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1561 "16-bit accessor invalid for natural width field");
1562}
1563
1564static __always_inline void vmcs_check32(unsigned long field)
1565{
1566 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1567 "32-bit accessor invalid for 16-bit field");
1568 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1569 "32-bit accessor invalid for natural width field");
1570}
1571
1572static __always_inline void vmcs_check64(unsigned long field)
1573{
1574 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1575 "64-bit accessor invalid for 16-bit field");
1576 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1577 "64-bit accessor invalid for 64-bit high field");
1578 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1579 "64-bit accessor invalid for 32-bit field");
1580 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1581 "64-bit accessor invalid for natural width field");
1582}
1583
1584static __always_inline void vmcs_checkl(unsigned long field)
1585{
1586 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1587 "Natural width accessor invalid for 16-bit field");
1588 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1589 "Natural width accessor invalid for 64-bit field");
1590 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1591 "Natural width accessor invalid for 64-bit high field");
1592 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1593 "Natural width accessor invalid for 32-bit field");
1594}
1595
1596static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597{
Avi Kivity5e520e62011-05-15 10:13:12 -04001598 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599
Avi Kivity5e520e62011-05-15 10:13:12 -04001600 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1601 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 return value;
1603}
1604
Avi Kivity96304212011-05-15 10:13:13 -04001605static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001607 vmcs_check16(field);
1608 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609}
1610
Avi Kivity96304212011-05-15 10:13:13 -04001611static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001613 vmcs_check32(field);
1614 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615}
1616
Avi Kivity96304212011-05-15 10:13:13 -04001617static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001619 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001620#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001621 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001623 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624#endif
1625}
1626
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001627static __always_inline unsigned long vmcs_readl(unsigned long field)
1628{
1629 vmcs_checkl(field);
1630 return __vmcs_readl(field);
1631}
1632
Avi Kivitye52de1b2007-01-05 16:36:56 -08001633static noinline void vmwrite_error(unsigned long field, unsigned long value)
1634{
1635 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1636 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1637 dump_stack();
1638}
1639
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001640static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001641{
1642 u8 error;
1643
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001644 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001645 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001646 if (unlikely(error))
1647 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648}
1649
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001650static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001652 vmcs_check16(field);
1653 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654}
1655
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001656static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001657{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001658 vmcs_check32(field);
1659 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660}
1661
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001662static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001664 vmcs_check64(field);
1665 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001666#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001668 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669#endif
1670}
1671
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001672static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001673{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001674 vmcs_checkl(field);
1675 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001676}
1677
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001678static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001679{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001680 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1681 "vmcs_clear_bits does not support 64-bit fields");
1682 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1683}
1684
1685static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1686{
1687 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1688 "vmcs_set_bits does not support 64-bit fields");
1689 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001690}
1691
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001692static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1693{
1694 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1695}
1696
Gleb Natapov2961e8762013-11-25 15:37:13 +02001697static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1698{
1699 vmcs_write32(VM_ENTRY_CONTROLS, val);
1700 vmx->vm_entry_controls_shadow = val;
1701}
1702
1703static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1704{
1705 if (vmx->vm_entry_controls_shadow != val)
1706 vm_entry_controls_init(vmx, val);
1707}
1708
1709static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1710{
1711 return vmx->vm_entry_controls_shadow;
1712}
1713
1714
1715static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1716{
1717 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1718}
1719
1720static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1721{
1722 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1723}
1724
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001725static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1726{
1727 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1728}
1729
Gleb Natapov2961e8762013-11-25 15:37:13 +02001730static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1731{
1732 vmcs_write32(VM_EXIT_CONTROLS, val);
1733 vmx->vm_exit_controls_shadow = val;
1734}
1735
1736static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1737{
1738 if (vmx->vm_exit_controls_shadow != val)
1739 vm_exit_controls_init(vmx, val);
1740}
1741
1742static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1743{
1744 return vmx->vm_exit_controls_shadow;
1745}
1746
1747
1748static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1749{
1750 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1751}
1752
1753static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1754{
1755 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1756}
1757
Avi Kivity2fb92db2011-04-27 19:42:18 +03001758static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1759{
1760 vmx->segment_cache.bitmask = 0;
1761}
1762
1763static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1764 unsigned field)
1765{
1766 bool ret;
1767 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1768
1769 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1770 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1771 vmx->segment_cache.bitmask = 0;
1772 }
1773 ret = vmx->segment_cache.bitmask & mask;
1774 vmx->segment_cache.bitmask |= mask;
1775 return ret;
1776}
1777
1778static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1779{
1780 u16 *p = &vmx->segment_cache.seg[seg].selector;
1781
1782 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1783 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1784 return *p;
1785}
1786
1787static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1788{
1789 ulong *p = &vmx->segment_cache.seg[seg].base;
1790
1791 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1792 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1793 return *p;
1794}
1795
1796static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1797{
1798 u32 *p = &vmx->segment_cache.seg[seg].limit;
1799
1800 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1801 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1802 return *p;
1803}
1804
1805static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1806{
1807 u32 *p = &vmx->segment_cache.seg[seg].ar;
1808
1809 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1810 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1811 return *p;
1812}
1813
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001814static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1815{
1816 u32 eb;
1817
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001818 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01001819 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001820 if ((vcpu->guest_debug &
1821 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1822 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1823 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001824 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001825 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001826 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001827 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001828 if (vcpu->fpu_active)
1829 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001830
1831 /* When we are running a nested L2 guest and L1 specified for it a
1832 * certain exception bitmap, we must trap the same exceptions and pass
1833 * them to L1. When running L2, we will only handle the exceptions
1834 * specified above if L1 did not want them.
1835 */
1836 if (is_guest_mode(vcpu))
1837 eb |= get_vmcs12(vcpu)->exception_bitmap;
1838
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001839 vmcs_write32(EXCEPTION_BITMAP, eb);
1840}
1841
Gleb Natapov2961e8762013-11-25 15:37:13 +02001842static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1843 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001844{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001845 vm_entry_controls_clearbit(vmx, entry);
1846 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001847}
1848
Avi Kivity61d2ef22010-04-28 16:40:38 +03001849static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1850{
1851 unsigned i;
1852 struct msr_autoload *m = &vmx->msr_autoload;
1853
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001854 switch (msr) {
1855 case MSR_EFER:
1856 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001857 clear_atomic_switch_msr_special(vmx,
1858 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001859 VM_EXIT_LOAD_IA32_EFER);
1860 return;
1861 }
1862 break;
1863 case MSR_CORE_PERF_GLOBAL_CTRL:
1864 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001865 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001866 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1867 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1868 return;
1869 }
1870 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001871 }
1872
Avi Kivity61d2ef22010-04-28 16:40:38 +03001873 for (i = 0; i < m->nr; ++i)
1874 if (m->guest[i].index == msr)
1875 break;
1876
1877 if (i == m->nr)
1878 return;
1879 --m->nr;
1880 m->guest[i] = m->guest[m->nr];
1881 m->host[i] = m->host[m->nr];
1882 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1883 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1884}
1885
Gleb Natapov2961e8762013-11-25 15:37:13 +02001886static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1887 unsigned long entry, unsigned long exit,
1888 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1889 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001890{
1891 vmcs_write64(guest_val_vmcs, guest_val);
1892 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001893 vm_entry_controls_setbit(vmx, entry);
1894 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001895}
1896
Avi Kivity61d2ef22010-04-28 16:40:38 +03001897static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1898 u64 guest_val, u64 host_val)
1899{
1900 unsigned i;
1901 struct msr_autoload *m = &vmx->msr_autoload;
1902
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001903 switch (msr) {
1904 case MSR_EFER:
1905 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001906 add_atomic_switch_msr_special(vmx,
1907 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001908 VM_EXIT_LOAD_IA32_EFER,
1909 GUEST_IA32_EFER,
1910 HOST_IA32_EFER,
1911 guest_val, host_val);
1912 return;
1913 }
1914 break;
1915 case MSR_CORE_PERF_GLOBAL_CTRL:
1916 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001917 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001918 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1919 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1920 GUEST_IA32_PERF_GLOBAL_CTRL,
1921 HOST_IA32_PERF_GLOBAL_CTRL,
1922 guest_val, host_val);
1923 return;
1924 }
1925 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001926 case MSR_IA32_PEBS_ENABLE:
1927 /* PEBS needs a quiescent period after being disabled (to write
1928 * a record). Disabling PEBS through VMX MSR swapping doesn't
1929 * provide that period, so a CPU could write host's record into
1930 * guest's memory.
1931 */
1932 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001933 }
1934
Avi Kivity61d2ef22010-04-28 16:40:38 +03001935 for (i = 0; i < m->nr; ++i)
1936 if (m->guest[i].index == msr)
1937 break;
1938
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001939 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001940 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001941 "Can't add msr %x\n", msr);
1942 return;
1943 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001944 ++m->nr;
1945 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1946 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1947 }
1948
1949 m->guest[i].index = msr;
1950 m->guest[i].value = guest_val;
1951 m->host[i].index = msr;
1952 m->host[i].value = host_val;
1953}
1954
Avi Kivity33ed6322007-05-02 16:54:03 +03001955static void reload_tss(void)
1956{
Avi Kivity33ed6322007-05-02 16:54:03 +03001957 /*
1958 * VT restores TR but not its size. Useless.
1959 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001960 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001961 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001962
Avi Kivityd3591922010-07-26 18:32:39 +03001963 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001964 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1965 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001966}
1967
Avi Kivity92c0d902009-10-29 11:00:16 +02001968static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001969{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001970 u64 guest_efer = vmx->vcpu.arch.efer;
1971 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03001972
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001973 if (!enable_ept) {
1974 /*
1975 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1976 * host CPUID is more efficient than testing guest CPUID
1977 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
1978 */
1979 if (boot_cpu_has(X86_FEATURE_SMEP))
1980 guest_efer |= EFER_NX;
1981 else if (!(guest_efer & EFER_NX))
1982 ignore_bits |= EFER_NX;
1983 }
Roel Kluin3a34a882009-08-04 02:08:45 -07001984
Avi Kivity51c6cf62007-08-29 03:48:05 +03001985 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001986 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03001987 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001988 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001989#ifdef CONFIG_X86_64
1990 ignore_bits |= EFER_LMA | EFER_LME;
1991 /* SCE is meaningful only in long mode on Intel */
1992 if (guest_efer & EFER_LMA)
1993 ignore_bits &= ~(u64)EFER_SCE;
1994#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03001995
1996 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001997
1998 /*
1999 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2000 * On CPUs that support "load IA32_EFER", always switch EFER
2001 * atomically, since it's faster than switching it manually.
2002 */
2003 if (cpu_has_load_ia32_efer ||
2004 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002005 if (!(guest_efer & EFER_LMA))
2006 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002007 if (guest_efer != host_efer)
2008 add_atomic_switch_msr(vmx, MSR_EFER,
2009 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002010 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002011 } else {
2012 guest_efer &= ~ignore_bits;
2013 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002014
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002015 vmx->guest_msrs[efer_offset].data = guest_efer;
2016 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2017
2018 return true;
2019 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002020}
2021
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002022static unsigned long segment_base(u16 selector)
2023{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002024 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002025 struct desc_struct *d;
2026 unsigned long table_base;
2027 unsigned long v;
2028
2029 if (!(selector & ~3))
2030 return 0;
2031
Avi Kivityd3591922010-07-26 18:32:39 +03002032 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002033
2034 if (selector & 4) { /* from ldt */
2035 u16 ldt_selector = kvm_read_ldt();
2036
2037 if (!(ldt_selector & ~3))
2038 return 0;
2039
2040 table_base = segment_base(ldt_selector);
2041 }
2042 d = (struct desc_struct *)(table_base + (selector & ~7));
2043 v = get_desc_base(d);
2044#ifdef CONFIG_X86_64
2045 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2046 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2047#endif
2048 return v;
2049}
2050
2051static inline unsigned long kvm_read_tr_base(void)
2052{
2053 u16 tr;
2054 asm("str %0" : "=g"(tr));
2055 return segment_base(tr);
2056}
2057
Avi Kivity04d2cc72007-09-10 18:10:54 +03002058static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002059{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002061 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002062
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002063 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002064 return;
2065
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002066 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002067 /*
2068 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2069 * allow segment selectors with cpl > 0 or ti == 1.
2070 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002071 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002072 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002073 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002074 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002075 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002076 vmx->host_state.fs_reload_needed = 0;
2077 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002078 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002079 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002080 }
Avi Kivity9581d442010-10-19 16:46:55 +02002081 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002082 if (!(vmx->host_state.gs_sel & 7))
2083 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002084 else {
2085 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002086 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002087 }
2088
2089#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002090 savesegment(ds, vmx->host_state.ds_sel);
2091 savesegment(es, vmx->host_state.es_sel);
2092#endif
2093
2094#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002095 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2096 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2097#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002098 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2099 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002100#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002101
2102#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002103 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2104 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002105 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002106#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002107 if (boot_cpu_has(X86_FEATURE_MPX))
2108 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002109 for (i = 0; i < vmx->save_nmsrs; ++i)
2110 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002111 vmx->guest_msrs[i].data,
2112 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002113}
2114
Avi Kivitya9b21b62008-06-24 11:48:49 +03002115static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002116{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002117 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002118 return;
2119
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002120 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002121 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002122#ifdef CONFIG_X86_64
2123 if (is_long_mode(&vmx->vcpu))
2124 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2125#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002126 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002127 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002128#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002129 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002130#else
2131 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002132#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002133 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002134 if (vmx->host_state.fs_reload_needed)
2135 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002136#ifdef CONFIG_X86_64
2137 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2138 loadsegment(ds, vmx->host_state.ds_sel);
2139 loadsegment(es, vmx->host_state.es_sel);
2140 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002141#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002142 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002143#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002144 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002145#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002146 if (vmx->host_state.msr_host_bndcfgs)
2147 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002148 /*
2149 * If the FPU is not active (through the host task or
2150 * the guest vcpu), then restore the cr0.TS bit.
2151 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002152 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002153 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002154 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002155}
2156
Avi Kivitya9b21b62008-06-24 11:48:49 +03002157static void vmx_load_host_state(struct vcpu_vmx *vmx)
2158{
2159 preempt_disable();
2160 __vmx_load_host_state(vmx);
2161 preempt_enable();
2162}
2163
Feng Wu28b835d2015-09-18 22:29:54 +08002164static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2165{
2166 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2167 struct pi_desc old, new;
2168 unsigned int dest;
2169
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002170 /*
2171 * In case of hot-plug or hot-unplug, we may have to undo
2172 * vmx_vcpu_pi_put even if there is no assigned device. And we
2173 * always keep PI.NDST up to date for simplicity: it makes the
2174 * code easier, and CPU migration is not a fast path.
2175 */
2176 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002177 return;
2178
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002179 /*
2180 * First handle the simple case where no cmpxchg is necessary; just
2181 * allow posting non-urgent interrupts.
2182 *
2183 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2184 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
2185 * expects the VCPU to be on the blocked_vcpu_list that matches
2186 * PI.NDST.
2187 */
2188 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
2189 vcpu->cpu == cpu) {
2190 pi_clear_sn(pi_desc);
2191 return;
2192 }
2193
2194 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08002195 do {
2196 old.control = new.control = pi_desc->control;
2197
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002198 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08002199
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02002200 if (x2apic_enabled())
2201 new.ndst = dest;
2202 else
2203 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08002204
Feng Wu28b835d2015-09-18 22:29:54 +08002205 new.sn = 0;
Paolo Bonziniea37f612017-09-28 17:58:41 +02002206 } while (cmpxchg64(&pi_desc->control, old.control,
2207 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08002208}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002209
Peter Feinerc95ba922016-08-17 09:36:47 -07002210static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2211{
2212 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2213 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2214}
2215
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216/*
2217 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2218 * vcpu mutex is already taken.
2219 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002220static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002222 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002223 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002224 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002226 if (!vmm_exclusive)
2227 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002228 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002229 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002231 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002232 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002233 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002234
2235 /*
2236 * Read loaded_vmcs->cpu should be before fetching
2237 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2238 * See the comments in __loaded_vmcs_clear().
2239 */
2240 smp_rmb();
2241
Nadav Har'Eld462b812011-05-24 15:26:10 +03002242 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2243 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002244 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002245 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002246 }
2247
2248 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2249 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2250 vmcs_load(vmx->loaded_vmcs->vmcs);
2251 }
2252
2253 if (!already_loaded) {
2254 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2255 unsigned long sysenter_esp;
2256
2257 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002258
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259 /*
2260 * Linux uses per-cpu TSS and GDT, so set these when switching
2261 * processors.
2262 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002263 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002264 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265
2266 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2267 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002268
Nadav Har'Eld462b812011-05-24 15:26:10 +03002269 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 }
Feng Wu28b835d2015-09-18 22:29:54 +08002271
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002272 /* Setup TSC multiplier */
2273 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002274 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2275 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002276
Feng Wu28b835d2015-09-18 22:29:54 +08002277 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002278 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002279}
2280
2281static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2282{
2283 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2284
2285 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002286 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2287 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002288 return;
2289
2290 /* Set SN when the vCPU is preempted */
2291 if (vcpu->preempted)
2292 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293}
2294
2295static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2296{
Feng Wu28b835d2015-09-18 22:29:54 +08002297 vmx_vcpu_pi_put(vcpu);
2298
Avi Kivitya9b21b62008-06-24 11:48:49 +03002299 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002300 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002301 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2302 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002303 kvm_cpu_vmxoff();
2304 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305}
2306
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002307static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2308{
Avi Kivity81231c62010-01-24 16:26:40 +02002309 ulong cr0;
2310
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002311 if (vcpu->fpu_active)
2312 return;
2313 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002314 cr0 = vmcs_readl(GUEST_CR0);
2315 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2316 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2317 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002318 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002319 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002320 if (is_guest_mode(vcpu))
2321 vcpu->arch.cr0_guest_owned_bits &=
2322 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002323 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002324}
2325
Avi Kivityedcafe32009-12-30 18:07:40 +02002326static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2327
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002328/*
2329 * Return the cr0 value that a nested guest would read. This is a combination
2330 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2331 * its hypervisor (cr0_read_shadow).
2332 */
2333static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2334{
2335 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2336 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2337}
2338static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2339{
2340 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2341 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2342}
2343
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002344static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2345{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002346 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2347 * set this *before* calling this function.
2348 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002349 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002350 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002351 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002352 vcpu->arch.cr0_guest_owned_bits = 0;
2353 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002354 if (is_guest_mode(vcpu)) {
2355 /*
2356 * L1's specified read shadow might not contain the TS bit,
2357 * so now that we turned on shadowing of this bit, we need to
2358 * set this bit of the shadow. Like in nested_vmx_run we need
2359 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2360 * up-to-date here because we just decached cr0.TS (and we'll
2361 * only update vmcs12->guest_cr0 on nested exit).
2362 */
2363 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2364 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2365 (vcpu->arch.cr0 & X86_CR0_TS);
2366 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2367 } else
2368 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002369}
2370
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2372{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002373 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002374
Avi Kivity6de12732011-03-07 12:51:22 +02002375 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2376 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2377 rflags = vmcs_readl(GUEST_RFLAGS);
2378 if (to_vmx(vcpu)->rmode.vm86_active) {
2379 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2380 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2381 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2382 }
2383 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002384 }
Avi Kivity6de12732011-03-07 12:51:22 +02002385 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
2388static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2389{
Avi Kivity6de12732011-03-07 12:51:22 +02002390 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2391 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002392 if (to_vmx(vcpu)->rmode.vm86_active) {
2393 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002394 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002395 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 vmcs_writel(GUEST_RFLAGS, rflags);
2397}
2398
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002399static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2400{
2401 return to_vmx(vcpu)->guest_pkru;
2402}
2403
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002404static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002405{
2406 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2407 int ret = 0;
2408
2409 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002410 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002411 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002412 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002413
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002414 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002415}
2416
2417static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2418{
2419 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2420 u32 interruptibility = interruptibility_old;
2421
2422 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2423
Jan Kiszka48005f62010-02-19 19:38:07 +01002424 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002425 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002426 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002427 interruptibility |= GUEST_INTR_STATE_STI;
2428
2429 if ((interruptibility != interruptibility_old))
2430 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2431}
2432
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2434{
2435 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002437 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002439 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440
Glauber Costa2809f5d2009-05-12 16:21:05 -04002441 /* skipping an emulated instruction also counts */
2442 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443}
2444
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002445/*
2446 * KVM wants to inject page-faults which it got to the guest. This function
2447 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002448 */
Gleb Natapove011c662013-09-25 12:51:35 +03002449static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002450{
2451 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2452
Gleb Natapove011c662013-09-25 12:51:35 +03002453 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002454 return 0;
2455
Wanpeng Lia29fd272017-06-05 05:19:09 -07002456 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
Jan Kiszka533558b2014-01-04 18:47:20 +01002457 vmcs_read32(VM_EXIT_INTR_INFO),
2458 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002459 return 1;
2460}
2461
Avi Kivity298101d2007-11-25 13:41:11 +02002462static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002463 bool has_error_code, u32 error_code,
2464 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002465{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002466 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002467 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002468
Gleb Natapove011c662013-09-25 12:51:35 +03002469 if (!reinject && is_guest_mode(vcpu) &&
2470 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002471 return;
2472
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002473 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002474 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002475 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2476 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002477
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002478 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002479 int inc_eip = 0;
2480 if (kvm_exception_is_soft(nr))
2481 inc_eip = vcpu->arch.event_exit_inst_len;
2482 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002483 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002484 return;
2485 }
2486
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002487 if (kvm_exception_is_soft(nr)) {
2488 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2489 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002490 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2491 } else
2492 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2493
2494 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002495}
2496
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002497static bool vmx_rdtscp_supported(void)
2498{
2499 return cpu_has_vmx_rdtscp();
2500}
2501
Mao, Junjiead756a12012-07-02 01:18:48 +00002502static bool vmx_invpcid_supported(void)
2503{
2504 return cpu_has_vmx_invpcid() && enable_ept;
2505}
2506
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507/*
Eddie Donga75beee2007-05-17 18:55:15 +03002508 * Swap MSR entry in host/guest MSR entry array.
2509 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002510static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002511{
Avi Kivity26bb0982009-09-07 11:14:12 +03002512 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002513
2514 tmp = vmx->guest_msrs[to];
2515 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2516 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002517}
2518
Yang Zhang8d146952013-01-25 10:18:50 +08002519static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2520{
2521 unsigned long *msr_bitmap;
2522
Wincy Van670125b2015-03-04 14:31:56 +08002523 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002524 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002525 else if (cpu_has_secondary_exec_ctrls() &&
2526 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2527 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002528 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2529 if (is_long_mode(vcpu))
2530 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2531 else
2532 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2533 } else {
2534 if (is_long_mode(vcpu))
2535 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv_inactive;
2536 else
2537 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv_inactive;
2538 }
Yang Zhang8d146952013-01-25 10:18:50 +08002539 } else {
2540 if (is_long_mode(vcpu))
2541 msr_bitmap = vmx_msr_bitmap_longmode;
2542 else
2543 msr_bitmap = vmx_msr_bitmap_legacy;
2544 }
2545
2546 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2547}
2548
Eddie Donga75beee2007-05-17 18:55:15 +03002549/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002550 * Set up the vmcs to automatically save and restore system
2551 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2552 * mode, as fiddling with msrs is very expensive.
2553 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002554static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002555{
Avi Kivity26bb0982009-09-07 11:14:12 +03002556 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002557
Eddie Donga75beee2007-05-17 18:55:15 +03002558 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002559#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002560 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002561 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002562 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002563 move_msr_up(vmx, index, save_nmsrs++);
2564 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002565 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002566 move_msr_up(vmx, index, save_nmsrs++);
2567 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002568 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002569 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002570 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002571 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002572 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002573 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002574 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002575 * if efer.sce is enabled.
2576 */
Brian Gerst8c065852010-07-17 09:03:26 -04002577 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002578 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002579 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002580 }
Eddie Donga75beee2007-05-17 18:55:15 +03002581#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002582 index = __find_msr_index(vmx, MSR_EFER);
2583 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002584 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002585
Avi Kivity26bb0982009-09-07 11:14:12 +03002586 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002587
Yang Zhang8d146952013-01-25 10:18:50 +08002588 if (cpu_has_vmx_msr_bitmap())
2589 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002590}
2591
2592/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002594 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2595 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002597static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598{
2599 u64 host_tsc, tsc_offset;
2600
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002601 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002603 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604}
2605
2606/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002607 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002609static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002611 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002612 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002613 * We're here if L1 chose not to trap WRMSR to TSC. According
2614 * to the spec, this should set L1's TSC; The offset that L1
2615 * set for L2 remains unchanged, and still needs to be added
2616 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002617 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002618 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002619 /* recalculate vmcs02.TSC_OFFSET: */
2620 vmcs12 = get_vmcs12(vcpu);
2621 vmcs_write64(TSC_OFFSET, offset +
2622 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2623 vmcs12->tsc_offset : 0));
2624 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002625 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2626 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002627 vmcs_write64(TSC_OFFSET, offset);
2628 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629}
2630
Nadav Har'El801d3422011-05-25 23:02:23 +03002631static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2632{
2633 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2634 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2635}
2636
2637/*
2638 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2639 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2640 * all guests if the "nested" module option is off, and can also be disabled
2641 * for a single guest by disabling its VMX cpuid bit.
2642 */
2643static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2644{
2645 return nested && guest_cpuid_has_vmx(vcpu);
2646}
2647
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002649 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2650 * returned for the various VMX controls MSRs when nested VMX is enabled.
2651 * The same values should also be used to verify that vmcs12 control fields are
2652 * valid during nested entry from L1 to L2.
2653 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2654 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2655 * bit in the high half is on if the corresponding bit in the control field
2656 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002657 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002658static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002659{
2660 /*
2661 * Note that as a general rule, the high half of the MSRs (bits in
2662 * the control fields which may be 1) should be initialized by the
2663 * intersection of the underlying hardware's MSR (i.e., features which
2664 * can be supported) and the list of features we want to expose -
2665 * because they are known to be properly supported in our code.
2666 * Also, usually, the low half of the MSRs (bits which must be 1) can
2667 * be set to 0, meaning that L1 may turn off any of these bits. The
2668 * reason is that if one of these bits is necessary, it will appear
2669 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2670 * fields of vmcs01 and vmcs02, will turn these bits off - and
2671 * nested_vmx_exit_handled() will not pass related exits to L1.
2672 * These rules have exceptions below.
2673 */
2674
2675 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002676 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002677 vmx->nested.nested_vmx_pinbased_ctls_low,
2678 vmx->nested.nested_vmx_pinbased_ctls_high);
2679 vmx->nested.nested_vmx_pinbased_ctls_low |=
2680 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2681 vmx->nested.nested_vmx_pinbased_ctls_high &=
2682 PIN_BASED_EXT_INTR_MASK |
2683 PIN_BASED_NMI_EXITING |
2684 PIN_BASED_VIRTUAL_NMIS;
2685 vmx->nested.nested_vmx_pinbased_ctls_high |=
2686 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002687 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002688 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002689 vmx->nested.nested_vmx_pinbased_ctls_high |=
2690 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002691
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002692 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002693 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002694 vmx->nested.nested_vmx_exit_ctls_low,
2695 vmx->nested.nested_vmx_exit_ctls_high);
2696 vmx->nested.nested_vmx_exit_ctls_low =
2697 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002698
Wincy Vanb9c237b2015-02-03 23:56:30 +08002699 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002700#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002701 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002702#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002703 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002704 vmx->nested.nested_vmx_exit_ctls_high |=
2705 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002706 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002707 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2708
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002709 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002710 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002711
Jan Kiszka2996fca2014-06-16 13:59:43 +02002712 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002713 vmx->nested.nested_vmx_true_exit_ctls_low =
2714 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002715 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2716
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002717 /* entry controls */
2718 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002719 vmx->nested.nested_vmx_entry_ctls_low,
2720 vmx->nested.nested_vmx_entry_ctls_high);
2721 vmx->nested.nested_vmx_entry_ctls_low =
2722 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2723 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002724#ifdef CONFIG_X86_64
2725 VM_ENTRY_IA32E_MODE |
2726#endif
2727 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002728 vmx->nested.nested_vmx_entry_ctls_high |=
2729 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002730 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002731 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002732
Jan Kiszka2996fca2014-06-16 13:59:43 +02002733 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002734 vmx->nested.nested_vmx_true_entry_ctls_low =
2735 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002736 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2737
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002738 /* cpu-based controls */
2739 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002740 vmx->nested.nested_vmx_procbased_ctls_low,
2741 vmx->nested.nested_vmx_procbased_ctls_high);
2742 vmx->nested.nested_vmx_procbased_ctls_low =
2743 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2744 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002745 CPU_BASED_VIRTUAL_INTR_PENDING |
2746 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002747 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2748 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2749 CPU_BASED_CR3_STORE_EXITING |
2750#ifdef CONFIG_X86_64
2751 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2752#endif
2753 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002754 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2755 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2756 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2757 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002758 /*
2759 * We can allow some features even when not supported by the
2760 * hardware. For example, L1 can specify an MSR bitmap - and we
2761 * can use it to avoid exits to L1 - even when L0 runs L2
2762 * without MSR bitmaps.
2763 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002764 vmx->nested.nested_vmx_procbased_ctls_high |=
2765 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002766 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002767
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002768 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002769 vmx->nested.nested_vmx_true_procbased_ctls_low =
2770 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002771 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2772
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002773 /* secondary cpu-based controls */
2774 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002775 vmx->nested.nested_vmx_secondary_ctls_low,
2776 vmx->nested.nested_vmx_secondary_ctls_high);
2777 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2778 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002779 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002780 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002781 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002782 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002783 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002784 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002785 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002786 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002787
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002788 if (enable_ept) {
2789 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002790 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002791 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002792 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002793 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2794 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002795 if (cpu_has_vmx_ept_execute_only())
2796 vmx->nested.nested_vmx_ept_caps |=
2797 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002798 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002799 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2800 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002801 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002802 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002803
Paolo Bonzinief697a72016-03-18 16:58:38 +01002804 /*
2805 * Old versions of KVM use the single-context version without
2806 * checking for support, so declare that it is supported even
2807 * though it is treated as global context. The alternative is
2808 * not failing the single-context invvpid, and it is worse.
2809 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002810 if (enable_vpid)
2811 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Paolo Bonzinief697a72016-03-18 16:58:38 +01002812 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT |
Wanpeng Li089d7b62015-10-13 09:18:37 -07002813 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
2814 else
2815 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002816
Radim Krčmář0790ec12015-03-17 14:02:32 +01002817 if (enable_unrestricted_guest)
2818 vmx->nested.nested_vmx_secondary_ctls_high |=
2819 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2820
Jan Kiszkac18911a2013-03-13 16:06:41 +01002821 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002822 rdmsr(MSR_IA32_VMX_MISC,
2823 vmx->nested.nested_vmx_misc_low,
2824 vmx->nested.nested_vmx_misc_high);
2825 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2826 vmx->nested.nested_vmx_misc_low |=
2827 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002828 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002829 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002830}
2831
2832static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2833{
2834 /*
2835 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2836 */
2837 return ((control & high) | low) == control;
2838}
2839
2840static inline u64 vmx_control_msr(u32 low, u32 high)
2841{
2842 return low | ((u64)high << 32);
2843}
2844
Jan Kiszkacae50132014-01-04 18:47:22 +01002845/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002846static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2847{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002848 struct vcpu_vmx *vmx = to_vmx(vcpu);
2849
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002850 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002851 case MSR_IA32_VMX_BASIC:
2852 /*
2853 * This MSR reports some information about VMX support. We
2854 * should return information about the VMX we emulate for the
2855 * guest, and the VMCS structure we give it - not about the
2856 * VMX support of the underlying hardware.
2857 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002858 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002859 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2860 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002861 if (cpu_has_vmx_basic_inout())
2862 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002863 break;
2864 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2865 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002866 *pdata = vmx_control_msr(
2867 vmx->nested.nested_vmx_pinbased_ctls_low,
2868 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002869 break;
2870 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002871 *pdata = vmx_control_msr(
2872 vmx->nested.nested_vmx_true_procbased_ctls_low,
2873 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002874 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002875 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002876 *pdata = vmx_control_msr(
2877 vmx->nested.nested_vmx_procbased_ctls_low,
2878 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002879 break;
2880 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002881 *pdata = vmx_control_msr(
2882 vmx->nested.nested_vmx_true_exit_ctls_low,
2883 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002884 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002885 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002886 *pdata = vmx_control_msr(
2887 vmx->nested.nested_vmx_exit_ctls_low,
2888 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002889 break;
2890 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002891 *pdata = vmx_control_msr(
2892 vmx->nested.nested_vmx_true_entry_ctls_low,
2893 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002894 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002895 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002896 *pdata = vmx_control_msr(
2897 vmx->nested.nested_vmx_entry_ctls_low,
2898 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002899 break;
2900 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002901 *pdata = vmx_control_msr(
2902 vmx->nested.nested_vmx_misc_low,
2903 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002904 break;
2905 /*
2906 * These MSRs specify bits which the guest must keep fixed (on or off)
2907 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2908 * We picked the standard core2 setting.
2909 */
2910#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2911#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2912 case MSR_IA32_VMX_CR0_FIXED0:
2913 *pdata = VMXON_CR0_ALWAYSON;
2914 break;
2915 case MSR_IA32_VMX_CR0_FIXED1:
2916 *pdata = -1ULL;
2917 break;
2918 case MSR_IA32_VMX_CR4_FIXED0:
2919 *pdata = VMXON_CR4_ALWAYSON;
2920 break;
2921 case MSR_IA32_VMX_CR4_FIXED1:
2922 *pdata = -1ULL;
2923 break;
2924 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002925 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002926 break;
2927 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002928 *pdata = vmx_control_msr(
2929 vmx->nested.nested_vmx_secondary_ctls_low,
2930 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002931 break;
2932 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07002933 *pdata = vmx->nested.nested_vmx_ept_caps |
2934 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002935 break;
2936 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002937 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002938 }
2939
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002940 return 0;
2941}
2942
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002943static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2944 uint64_t val)
2945{
2946 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2947
2948 return !(val & ~valid_bits);
2949}
2950
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002951/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 * Reads an msr value (of 'msr_index') into 'pdata'.
2953 * Returns 0 on success, non-0 otherwise.
2954 * Assumes vcpu_load() was already called.
2955 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002956static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957{
Avi Kivity26bb0982009-09-07 11:14:12 +03002958 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002960 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002961#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002963 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 break;
2965 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002966 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002968 case MSR_KERNEL_GS_BASE:
2969 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002970 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002971 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002972#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002974 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302975 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002976 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977 break;
2978 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002979 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 break;
2981 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002982 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 break;
2984 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002985 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002987 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08002988 if (!kvm_mpx_supported() ||
2989 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002990 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002991 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002992 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002993 case MSR_IA32_MCG_EXT_CTL:
2994 if (!msr_info->host_initiated &&
2995 !(to_vmx(vcpu)->msr_ia32_feature_control &
2996 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01002997 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002998 msr_info->data = vcpu->arch.mcg_ext_ctl;
2999 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003000 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003001 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003002 break;
3003 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3004 if (!nested_vmx_allowed(vcpu))
3005 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003006 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003007 case MSR_IA32_XSS:
3008 if (!vmx_xsaves_supported())
3009 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003010 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003011 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003012 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003013 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003014 return 1;
3015 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003016 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003017 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003018 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003019 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003020 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003022 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003023 }
3024
Avi Kivity6aa8b732006-12-10 02:21:36 -08003025 return 0;
3026}
3027
Jan Kiszkacae50132014-01-04 18:47:22 +01003028static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3029
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030/*
3031 * Writes msr value into into the appropriate "register".
3032 * Returns 0 on success, non-0 otherwise.
3033 * Assumes vcpu_load() was already called.
3034 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003035static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003037 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003038 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003039 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003040 u32 msr_index = msr_info->index;
3041 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003042
Avi Kivity6aa8b732006-12-10 02:21:36 -08003043 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003044 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003045 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003046 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003047#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003049 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 vmcs_writel(GUEST_FS_BASE, data);
3051 break;
3052 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003053 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054 vmcs_writel(GUEST_GS_BASE, data);
3055 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003056 case MSR_KERNEL_GS_BASE:
3057 vmx_load_host_state(vmx);
3058 vmx->msr_guest_kernel_gs_base = data;
3059 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060#endif
3061 case MSR_IA32_SYSENTER_CS:
3062 vmcs_write32(GUEST_SYSENTER_CS, data);
3063 break;
3064 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003065 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 break;
3067 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003068 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003070 case MSR_IA32_BNDCFGS:
Haozhong Zhangcce8d2e2017-07-04 10:27:41 +08003071 if (!kvm_mpx_supported() ||
3072 (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003073 return 1;
Jim Mattson07592d62017-05-23 11:52:54 -07003074 if (is_noncanonical_address(data & PAGE_MASK) ||
3075 (data & MSR_IA32_BNDCFGS_RSVD))
3076 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003077 vmcs_write64(GUEST_BNDCFGS, data);
3078 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303079 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003080 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003082 case MSR_IA32_CR_PAT:
3083 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003084 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3085 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003086 vmcs_write64(GUEST_IA32_PAT, data);
3087 vcpu->arch.pat = data;
3088 break;
3089 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003090 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003091 break;
Will Auldba904632012-11-29 12:42:50 -08003092 case MSR_IA32_TSC_ADJUST:
3093 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003094 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003095 case MSR_IA32_MCG_EXT_CTL:
3096 if ((!msr_info->host_initiated &&
3097 !(to_vmx(vcpu)->msr_ia32_feature_control &
3098 FEATURE_CONTROL_LMCE)) ||
3099 (data & ~MCG_EXT_CTL_LMCE_EN))
3100 return 1;
3101 vcpu->arch.mcg_ext_ctl = data;
3102 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003103 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003104 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003105 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003106 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3107 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003108 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003109 if (msr_info->host_initiated && data == 0)
3110 vmx_leave_nested(vcpu);
3111 break;
3112 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3113 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003114 case MSR_IA32_XSS:
3115 if (!vmx_xsaves_supported())
3116 return 1;
3117 /*
3118 * The only supported bit as of Skylake is bit 8, but
3119 * it is not supported on KVM.
3120 */
3121 if (data != 0)
3122 return 1;
3123 vcpu->arch.ia32_xss = data;
3124 if (vcpu->arch.ia32_xss != host_xss)
3125 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3126 vcpu->arch.ia32_xss, host_xss);
3127 else
3128 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3129 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003130 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003131 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003132 return 1;
3133 /* Check reserved bit, higher 32 bits should be zero */
3134 if ((data >> 32) != 0)
3135 return 1;
3136 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003138 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003139 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003140 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003141 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003142 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3143 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003144 ret = kvm_set_shared_msr(msr->index, msr->data,
3145 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003146 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003147 if (ret)
3148 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003149 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003150 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003152 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153 }
3154
Eddie Dong2cc51562007-05-21 07:28:09 +03003155 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156}
3157
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003158static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003160 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3161 switch (reg) {
3162 case VCPU_REGS_RSP:
3163 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3164 break;
3165 case VCPU_REGS_RIP:
3166 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3167 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003168 case VCPU_EXREG_PDPTR:
3169 if (enable_ept)
3170 ept_save_pdptrs(vcpu);
3171 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003172 default:
3173 break;
3174 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175}
3176
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177static __init int cpu_has_kvm_support(void)
3178{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003179 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180}
3181
3182static __init int vmx_disabled_by_bios(void)
3183{
3184 u64 msr;
3185
3186 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003187 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003188 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003189 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3190 && tboot_enabled())
3191 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003192 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003193 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003194 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003195 && !tboot_enabled()) {
3196 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003197 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003198 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003199 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003200 /* launched w/o TXT and VMX disabled */
3201 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3202 && !tboot_enabled())
3203 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003204 }
3205
3206 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207}
3208
Dongxiao Xu7725b892010-05-11 18:29:38 +08003209static void kvm_cpu_vmxon(u64 addr)
3210{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003211 intel_pt_handle_vmx(1);
3212
Dongxiao Xu7725b892010-05-11 18:29:38 +08003213 asm volatile (ASM_VMX_VMXON_RAX
3214 : : "a"(&addr), "m"(addr)
3215 : "memory", "cc");
3216}
3217
Radim Krčmář13a34e02014-08-28 15:13:03 +02003218static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219{
3220 int cpu = raw_smp_processor_id();
3221 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003222 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003224 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003225 return -EBUSY;
3226
Nadav Har'Eld462b812011-05-24 15:26:10 +03003227 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003228 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3229 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003230
3231 /*
3232 * Now we can enable the vmclear operation in kdump
3233 * since the loaded_vmcss_on_cpu list on this cpu
3234 * has been initialized.
3235 *
3236 * Though the cpu is not in VMX operation now, there
3237 * is no problem to enable the vmclear operation
3238 * for the loaded_vmcss_on_cpu list is empty!
3239 */
3240 crash_enable_local_vmclear(cpu);
3241
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003243
3244 test_bits = FEATURE_CONTROL_LOCKED;
3245 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3246 if (tboot_enabled())
3247 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3248
3249 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003251 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3252 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003253 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003254
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003255 if (vmm_exclusive) {
3256 kvm_cpu_vmxon(phys_addr);
3257 ept_sync_global();
3258 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003259
Christoph Lameter89cbc762014-08-17 12:30:40 -05003260 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003261
Alexander Graf10474ae2009-09-15 11:37:46 +02003262 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263}
3264
Nadav Har'Eld462b812011-05-24 15:26:10 +03003265static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003266{
3267 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003268 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003269
Nadav Har'Eld462b812011-05-24 15:26:10 +03003270 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3271 loaded_vmcss_on_cpu_link)
3272 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003273}
3274
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003275
3276/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3277 * tricks.
3278 */
3279static void kvm_cpu_vmxoff(void)
3280{
3281 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003282
3283 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003284}
3285
Radim Krčmář13a34e02014-08-28 15:13:03 +02003286static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003288 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003289 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003290 kvm_cpu_vmxoff();
3291 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003292 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293}
3294
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003295static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003296 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297{
3298 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003299 u32 ctl = ctl_min | ctl_opt;
3300
3301 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3302
3303 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3304 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3305
3306 /* Ensure minimum (required) set of control bits are supported. */
3307 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003308 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003309
3310 *result = ctl;
3311 return 0;
3312}
3313
Avi Kivity110312c2010-12-21 12:54:20 +02003314static __init bool allow_1_setting(u32 msr, u32 ctl)
3315{
3316 u32 vmx_msr_low, vmx_msr_high;
3317
3318 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3319 return vmx_msr_high & ctl;
3320}
3321
Yang, Sheng002c7f72007-07-31 14:23:01 +03003322static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003323{
3324 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003325 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003326 u32 _pin_based_exec_control = 0;
3327 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003328 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003329 u32 _vmexit_control = 0;
3330 u32 _vmentry_control = 0;
3331
Raghavendra K T10166742012-02-07 23:19:20 +05303332 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003333#ifdef CONFIG_X86_64
3334 CPU_BASED_CR8_LOAD_EXITING |
3335 CPU_BASED_CR8_STORE_EXITING |
3336#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003337 CPU_BASED_CR3_LOAD_EXITING |
3338 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003339 CPU_BASED_USE_IO_BITMAPS |
3340 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003341 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003342 CPU_BASED_MWAIT_EXITING |
3343 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003344 CPU_BASED_INVLPG_EXITING |
3345 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003346
Sheng Yangf78e0e22007-10-29 09:40:42 +08003347 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003348 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003349 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003350 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3351 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003352 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003353#ifdef CONFIG_X86_64
3354 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3355 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3356 ~CPU_BASED_CR8_STORE_EXITING;
3357#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003358 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003359 min2 = 0;
3360 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003361 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003362 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003363 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003364 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003365 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003366 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003367 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003368 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003369 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003370 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003371 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003372 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003373 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003374 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003375 if (adjust_vmx_controls(min2, opt2,
3376 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003377 &_cpu_based_2nd_exec_control) < 0)
3378 return -EIO;
3379 }
3380#ifndef CONFIG_X86_64
3381 if (!(_cpu_based_2nd_exec_control &
3382 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3383 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3384#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003385
3386 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3387 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003388 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003389 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3390 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003391
Sheng Yangd56f5462008-04-25 10:13:16 +08003392 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003393 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3394 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003395 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3396 CPU_BASED_CR3_STORE_EXITING |
3397 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003398 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3399 vmx_capability.ept, vmx_capability.vpid);
3400 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003401
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003402 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003403#ifdef CONFIG_X86_64
3404 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3405#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003406 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003407 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003408 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3409 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003410 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003411
Yang Zhang01e439b2013-04-11 19:25:12 +08003412 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003413 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3414 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003415 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3416 &_pin_based_exec_control) < 0)
3417 return -EIO;
3418
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003419 if (cpu_has_broken_vmx_preemption_timer())
3420 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003421 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003422 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003423 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3424
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003425 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003426 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003427 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3428 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003429 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003431 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003432
3433 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3434 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003435 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003436
3437#ifdef CONFIG_X86_64
3438 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3439 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003440 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003441#endif
3442
3443 /* Require Write-Back (WB) memory type for VMCS accesses. */
3444 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003445 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003446
Yang, Sheng002c7f72007-07-31 14:23:01 +03003447 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003448 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003449 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003450 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003451
Yang, Sheng002c7f72007-07-31 14:23:01 +03003452 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3453 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003454 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003455 vmcs_conf->vmexit_ctrl = _vmexit_control;
3456 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003457
Avi Kivity110312c2010-12-21 12:54:20 +02003458 cpu_has_load_ia32_efer =
3459 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3460 VM_ENTRY_LOAD_IA32_EFER)
3461 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3462 VM_EXIT_LOAD_IA32_EFER);
3463
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003464 cpu_has_load_perf_global_ctrl =
3465 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3466 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3467 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3468 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3469
3470 /*
3471 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003472 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003473 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3474 *
3475 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3476 *
3477 * AAK155 (model 26)
3478 * AAP115 (model 30)
3479 * AAT100 (model 37)
3480 * BC86,AAY89,BD102 (model 44)
3481 * BA97 (model 46)
3482 *
3483 */
3484 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3485 switch (boot_cpu_data.x86_model) {
3486 case 26:
3487 case 30:
3488 case 37:
3489 case 44:
3490 case 46:
3491 cpu_has_load_perf_global_ctrl = false;
3492 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3493 "does not work properly. Using workaround\n");
3494 break;
3495 default:
3496 break;
3497 }
3498 }
3499
Borislav Petkov782511b2016-04-04 22:25:03 +02003500 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003501 rdmsrl(MSR_IA32_XSS, host_xss);
3502
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003503 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003504}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505
3506static struct vmcs *alloc_vmcs_cpu(int cpu)
3507{
3508 int node = cpu_to_node(cpu);
3509 struct page *pages;
3510 struct vmcs *vmcs;
3511
Vlastimil Babka96db8002015-09-08 15:03:50 -07003512 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 if (!pages)
3514 return NULL;
3515 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003516 memset(vmcs, 0, vmcs_config.size);
3517 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 return vmcs;
3519}
3520
3521static struct vmcs *alloc_vmcs(void)
3522{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003523 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524}
3525
3526static void free_vmcs(struct vmcs *vmcs)
3527{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003528 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529}
3530
Nadav Har'Eld462b812011-05-24 15:26:10 +03003531/*
3532 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3533 */
3534static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3535{
3536 if (!loaded_vmcs->vmcs)
3537 return;
3538 loaded_vmcs_clear(loaded_vmcs);
3539 free_vmcs(loaded_vmcs->vmcs);
3540 loaded_vmcs->vmcs = NULL;
Jim Mattson355f4fb2016-10-28 08:29:39 -07003541 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003542}
3543
Sam Ravnborg39959582007-06-01 00:47:13 -07003544static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003545{
3546 int cpu;
3547
Zachary Amsden3230bb42009-09-29 11:38:37 -10003548 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003549 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003550 per_cpu(vmxarea, cpu) = NULL;
3551 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003552}
3553
Bandan Dasfe2b2012014-04-21 15:20:14 -04003554static void init_vmcs_shadow_fields(void)
3555{
3556 int i, j;
3557
3558 /* No checks for read only fields yet */
3559
3560 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3561 switch (shadow_read_write_fields[i]) {
3562 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003563 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003564 continue;
3565 break;
3566 default:
3567 break;
3568 }
3569
3570 if (j < i)
3571 shadow_read_write_fields[j] =
3572 shadow_read_write_fields[i];
3573 j++;
3574 }
3575 max_shadow_read_write_fields = j;
3576
3577 /* shadowed fields guest access without vmexit */
3578 for (i = 0; i < max_shadow_read_write_fields; i++) {
3579 clear_bit(shadow_read_write_fields[i],
3580 vmx_vmwrite_bitmap);
3581 clear_bit(shadow_read_write_fields[i],
3582 vmx_vmread_bitmap);
3583 }
3584 for (i = 0; i < max_shadow_read_only_fields; i++)
3585 clear_bit(shadow_read_only_fields[i],
3586 vmx_vmread_bitmap);
3587}
3588
Avi Kivity6aa8b732006-12-10 02:21:36 -08003589static __init int alloc_kvm_area(void)
3590{
3591 int cpu;
3592
Zachary Amsden3230bb42009-09-29 11:38:37 -10003593 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003594 struct vmcs *vmcs;
3595
3596 vmcs = alloc_vmcs_cpu(cpu);
3597 if (!vmcs) {
3598 free_kvm_area();
3599 return -ENOMEM;
3600 }
3601
3602 per_cpu(vmxarea, cpu) = vmcs;
3603 }
3604 return 0;
3605}
3606
Gleb Natapov14168782013-01-21 15:36:49 +02003607static bool emulation_required(struct kvm_vcpu *vcpu)
3608{
3609 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3610}
3611
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003612static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003613 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003614{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003615 if (!emulate_invalid_guest_state) {
3616 /*
3617 * CS and SS RPL should be equal during guest entry according
3618 * to VMX spec, but in reality it is not always so. Since vcpu
3619 * is in the middle of the transition from real mode to
3620 * protected mode it is safe to assume that RPL 0 is a good
3621 * default value.
3622 */
3623 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003624 save->selector &= ~SEGMENT_RPL_MASK;
3625 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003626 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003627 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003628 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003629}
3630
3631static void enter_pmode(struct kvm_vcpu *vcpu)
3632{
3633 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003634 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635
Gleb Natapovd99e4152012-12-20 16:57:45 +02003636 /*
3637 * Update real mode segment cache. It may be not up-to-date if sement
3638 * register was written while vcpu was in a guest mode.
3639 */
3640 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3641 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3642 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3643 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3644 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3645 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3646
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003647 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003648
Avi Kivity2fb92db2011-04-27 19:42:18 +03003649 vmx_segment_cache_clear(vmx);
3650
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003651 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652
3653 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003654 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3655 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656 vmcs_writel(GUEST_RFLAGS, flags);
3657
Rusty Russell66aee912007-07-17 23:34:16 +10003658 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3659 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660
3661 update_exception_bitmap(vcpu);
3662
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003663 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3664 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3665 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3666 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3667 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3668 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669}
3670
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003671static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003672{
Mathias Krause772e0312012-08-30 01:30:19 +02003673 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003674 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675
Gleb Natapovd99e4152012-12-20 16:57:45 +02003676 var.dpl = 0x3;
3677 if (seg == VCPU_SREG_CS)
3678 var.type = 0x3;
3679
3680 if (!emulate_invalid_guest_state) {
3681 var.selector = var.base >> 4;
3682 var.base = var.base & 0xffff0;
3683 var.limit = 0xffff;
3684 var.g = 0;
3685 var.db = 0;
3686 var.present = 1;
3687 var.s = 1;
3688 var.l = 0;
3689 var.unusable = 0;
3690 var.type = 0x3;
3691 var.avl = 0;
3692 if (save->base & 0xf)
3693 printk_once(KERN_WARNING "kvm: segment base is not "
3694 "paragraph aligned when entering "
3695 "protected mode (seg=%d)", seg);
3696 }
3697
3698 vmcs_write16(sf->selector, var.selector);
Chao Peng7c3bab12017-02-21 03:50:01 -05003699 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003700 vmcs_write32(sf->limit, var.limit);
3701 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003702}
3703
3704static void enter_rmode(struct kvm_vcpu *vcpu)
3705{
3706 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003707 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003708
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003709 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3710 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3711 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3712 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3713 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003714 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3715 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003716
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003717 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718
Gleb Natapov776e58e2011-03-13 12:34:27 +02003719 /*
3720 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003721 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003722 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003723 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003724 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3725 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003726
Avi Kivity2fb92db2011-04-27 19:42:18 +03003727 vmx_segment_cache_clear(vmx);
3728
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003729 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003730 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3732
3733 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003734 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003735
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003736 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003737
3738 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003739 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003740 update_exception_bitmap(vcpu);
3741
Gleb Natapovd99e4152012-12-20 16:57:45 +02003742 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3743 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3744 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3745 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3746 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3747 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003748
Eddie Dong8668a3c2007-10-10 14:26:45 +08003749 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003750}
3751
Amit Shah401d10d2009-02-20 22:53:37 +05303752static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3753{
3754 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003755 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3756
3757 if (!msr)
3758 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303759
Avi Kivity44ea2b12009-09-06 15:55:37 +03003760 /*
3761 * Force kernel_gs_base reloading before EFER changes, as control
3762 * of this msr depends on is_long_mode().
3763 */
3764 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003765 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303766 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003767 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303768 msr->data = efer;
3769 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003770 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303771
3772 msr->data = efer & ~EFER_LME;
3773 }
3774 setup_msrs(vmx);
3775}
3776
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003777#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778
3779static void enter_lmode(struct kvm_vcpu *vcpu)
3780{
3781 u32 guest_tr_ar;
3782
Avi Kivity2fb92db2011-04-27 19:42:18 +03003783 vmx_segment_cache_clear(to_vmx(vcpu));
3784
Avi Kivity6aa8b732006-12-10 02:21:36 -08003785 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003786 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003787 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3788 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003789 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003790 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3791 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003792 }
Avi Kivityda38f432010-07-06 11:30:49 +03003793 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794}
3795
3796static void exit_lmode(struct kvm_vcpu *vcpu)
3797{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003798 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003799 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800}
3801
3802#endif
3803
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003804static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003805{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003806 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003807 if (enable_ept) {
3808 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3809 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003810 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003811 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003812}
3813
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003814static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3815{
3816 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3817}
3818
Avi Kivitye8467fd2009-12-29 18:43:06 +02003819static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3820{
3821 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3822
3823 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3824 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3825}
3826
Avi Kivityaff48ba2010-12-05 18:56:11 +02003827static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3828{
3829 if (enable_ept && is_paging(vcpu))
3830 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3831 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3832}
3833
Anthony Liguori25c4c272007-04-27 09:29:21 +03003834static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003835{
Avi Kivityfc78f512009-12-07 12:16:48 +02003836 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3837
3838 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3839 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003840}
3841
Sheng Yang14394422008-04-28 12:24:45 +08003842static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3843{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003844 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3845
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003846 if (!test_bit(VCPU_EXREG_PDPTR,
3847 (unsigned long *)&vcpu->arch.regs_dirty))
3848 return;
3849
Sheng Yang14394422008-04-28 12:24:45 +08003850 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003851 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3852 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3853 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3854 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003855 }
3856}
3857
Avi Kivity8f5d5492009-05-31 18:41:29 +03003858static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3859{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003860 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3861
Avi Kivity8f5d5492009-05-31 18:41:29 +03003862 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003863 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3864 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3865 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3866 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003867 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003868
3869 __set_bit(VCPU_EXREG_PDPTR,
3870 (unsigned long *)&vcpu->arch.regs_avail);
3871 __set_bit(VCPU_EXREG_PDPTR,
3872 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003873}
3874
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003875static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003876
3877static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3878 unsigned long cr0,
3879 struct kvm_vcpu *vcpu)
3880{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003881 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3882 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003883 if (!(cr0 & X86_CR0_PG)) {
3884 /* From paging/starting to nonpaging */
3885 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003886 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003887 (CPU_BASED_CR3_LOAD_EXITING |
3888 CPU_BASED_CR3_STORE_EXITING));
3889 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003890 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003891 } else if (!is_paging(vcpu)) {
3892 /* From nonpaging to paging */
3893 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003894 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003895 ~(CPU_BASED_CR3_LOAD_EXITING |
3896 CPU_BASED_CR3_STORE_EXITING));
3897 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003898 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003899 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003900
3901 if (!(cr0 & X86_CR0_WP))
3902 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003903}
3904
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3906{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003907 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003908 unsigned long hw_cr0;
3909
Gleb Natapov50378782013-02-04 16:00:28 +02003910 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003911 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003912 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003913 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003914 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003915
Gleb Natapov218e7632013-01-21 15:36:45 +02003916 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3917 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918
Gleb Natapov218e7632013-01-21 15:36:45 +02003919 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3920 enter_rmode(vcpu);
3921 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003923#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003924 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003925 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003926 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003927 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003928 exit_lmode(vcpu);
3929 }
3930#endif
3931
Avi Kivity089d0342009-03-23 18:26:32 +02003932 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003933 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3934
Avi Kivity02daab22009-12-30 12:40:26 +02003935 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003936 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003937
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003939 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003940 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003941
3942 /* depends on vcpu->arch.cr0 to be set to a new value */
3943 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944}
3945
Sheng Yang14394422008-04-28 12:24:45 +08003946static u64 construct_eptp(unsigned long root_hpa)
3947{
3948 u64 eptp;
3949
3950 /* TODO write the value reading from MSR */
3951 eptp = VMX_EPT_DEFAULT_MT |
3952 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003953 if (enable_ept_ad_bits)
3954 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003955 eptp |= (root_hpa & PAGE_MASK);
3956
3957 return eptp;
3958}
3959
Avi Kivity6aa8b732006-12-10 02:21:36 -08003960static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3961{
Sheng Yang14394422008-04-28 12:24:45 +08003962 unsigned long guest_cr3;
3963 u64 eptp;
3964
3965 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003966 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003967 eptp = construct_eptp(cr3);
3968 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003969 if (is_paging(vcpu) || is_guest_mode(vcpu))
3970 guest_cr3 = kvm_read_cr3(vcpu);
3971 else
3972 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02003973 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003974 }
3975
Sheng Yang2384d2b2008-01-17 15:14:33 +08003976 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003977 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978}
3979
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003980static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981{
Ben Serebrin085e68e2015-04-16 11:58:05 -07003982 /*
3983 * Pass through host's Machine Check Enable value to hw_cr4, which
3984 * is in force while we are in guest mode. Do not let guests control
3985 * this bit, even if host CR4.MCE == 0.
3986 */
3987 unsigned long hw_cr4 =
3988 (cr4_read_shadow() & X86_CR4_MCE) |
3989 (cr4 & ~X86_CR4_MCE) |
3990 (to_vmx(vcpu)->rmode.vm86_active ?
3991 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08003992
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003993 if (cr4 & X86_CR4_VMXE) {
3994 /*
3995 * To use VMXON (and later other VMX instructions), a guest
3996 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3997 * So basically the check on whether to allow nested VMX
3998 * is here.
3999 */
4000 if (!nested_vmx_allowed(vcpu))
4001 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004002 }
4003 if (to_vmx(vcpu)->nested.vmxon &&
4004 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004005 return 1;
4006
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004007 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004008 if (enable_ept) {
4009 if (!is_paging(vcpu)) {
4010 hw_cr4 &= ~X86_CR4_PAE;
4011 hw_cr4 |= X86_CR4_PSE;
4012 } else if (!(cr4 & X86_CR4_PAE)) {
4013 hw_cr4 &= ~X86_CR4_PAE;
4014 }
4015 }
Sheng Yang14394422008-04-28 12:24:45 +08004016
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004017 if (!enable_unrestricted_guest && !is_paging(vcpu))
4018 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004019 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4020 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4021 * to be manually disabled when guest switches to non-paging
4022 * mode.
4023 *
4024 * If !enable_unrestricted_guest, the CPU is always running
4025 * with CR0.PG=1 and CR4 needs to be modified.
4026 * If enable_unrestricted_guest, the CPU automatically
4027 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004028 */
Huaitong Handdba2622016-03-22 16:51:15 +08004029 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004030
Sheng Yang14394422008-04-28 12:24:45 +08004031 vmcs_writel(CR4_READ_SHADOW, cr4);
4032 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004033 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004034}
4035
Avi Kivity6aa8b732006-12-10 02:21:36 -08004036static void vmx_get_segment(struct kvm_vcpu *vcpu,
4037 struct kvm_segment *var, int seg)
4038{
Avi Kivitya9179492011-01-03 14:28:52 +02004039 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040 u32 ar;
4041
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004042 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004043 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004044 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004045 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004046 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004047 var->base = vmx_read_guest_seg_base(vmx, seg);
4048 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4049 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004050 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004051 var->base = vmx_read_guest_seg_base(vmx, seg);
4052 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4053 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4054 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004055 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056 var->type = ar & 15;
4057 var->s = (ar >> 4) & 1;
4058 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004059 /*
4060 * Some userspaces do not preserve unusable property. Since usable
4061 * segment has to be present according to VMX spec we can use present
4062 * property to amend userspace bug by making unusable segment always
4063 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4064 * segment as unusable.
4065 */
4066 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 var->avl = (ar >> 12) & 1;
4068 var->l = (ar >> 13) & 1;
4069 var->db = (ar >> 14) & 1;
4070 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071}
4072
Avi Kivitya9179492011-01-03 14:28:52 +02004073static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4074{
Avi Kivitya9179492011-01-03 14:28:52 +02004075 struct kvm_segment s;
4076
4077 if (to_vmx(vcpu)->rmode.vm86_active) {
4078 vmx_get_segment(vcpu, &s, seg);
4079 return s.base;
4080 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004081 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004082}
4083
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004084static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004085{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004086 struct vcpu_vmx *vmx = to_vmx(vcpu);
4087
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004088 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004089 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004090 else {
4091 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004092 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004093 }
Avi Kivity69c73022011-03-07 15:26:44 +02004094}
4095
Avi Kivity653e3102007-05-07 10:55:37 +03004096static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004097{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 u32 ar;
4099
Avi Kivityf0495f92012-06-07 17:06:10 +03004100 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 ar = 1 << 16;
4102 else {
4103 ar = var->type & 15;
4104 ar |= (var->s & 1) << 4;
4105 ar |= (var->dpl & 3) << 5;
4106 ar |= (var->present & 1) << 7;
4107 ar |= (var->avl & 1) << 12;
4108 ar |= (var->l & 1) << 13;
4109 ar |= (var->db & 1) << 14;
4110 ar |= (var->g & 1) << 15;
4111 }
Avi Kivity653e3102007-05-07 10:55:37 +03004112
4113 return ar;
4114}
4115
4116static void vmx_set_segment(struct kvm_vcpu *vcpu,
4117 struct kvm_segment *var, int seg)
4118{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004119 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004120 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004121
Avi Kivity2fb92db2011-04-27 19:42:18 +03004122 vmx_segment_cache_clear(vmx);
4123
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004124 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4125 vmx->rmode.segs[seg] = *var;
4126 if (seg == VCPU_SREG_TR)
4127 vmcs_write16(sf->selector, var->selector);
4128 else if (var->s)
4129 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004130 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004131 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004132
Avi Kivity653e3102007-05-07 10:55:37 +03004133 vmcs_writel(sf->base, var->base);
4134 vmcs_write32(sf->limit, var->limit);
4135 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004136
4137 /*
4138 * Fix the "Accessed" bit in AR field of segment registers for older
4139 * qemu binaries.
4140 * IA32 arch specifies that at the time of processor reset the
4141 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004142 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004143 * state vmexit when "unrestricted guest" mode is turned on.
4144 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4145 * tree. Newer qemu binaries with that qemu fix would not need this
4146 * kvm hack.
4147 */
4148 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004149 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004150
Gleb Natapovf924d662012-12-12 19:10:55 +02004151 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004152
4153out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004154 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155}
4156
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4158{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004159 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160
4161 *db = (ar >> 14) & 1;
4162 *l = (ar >> 13) & 1;
4163}
4164
Gleb Natapov89a27f42010-02-16 10:51:48 +02004165static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004167 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4168 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169}
4170
Gleb Natapov89a27f42010-02-16 10:51:48 +02004171static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004173 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4174 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175}
4176
Gleb Natapov89a27f42010-02-16 10:51:48 +02004177static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004179 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4180 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181}
4182
Gleb Natapov89a27f42010-02-16 10:51:48 +02004183static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004185 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4186 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187}
4188
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004189static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4190{
4191 struct kvm_segment var;
4192 u32 ar;
4193
4194 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004195 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004196 if (seg == VCPU_SREG_CS)
4197 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004198 ar = vmx_segment_access_rights(&var);
4199
4200 if (var.base != (var.selector << 4))
4201 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004202 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004203 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004204 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004205 return false;
4206
4207 return true;
4208}
4209
4210static bool code_segment_valid(struct kvm_vcpu *vcpu)
4211{
4212 struct kvm_segment cs;
4213 unsigned int cs_rpl;
4214
4215 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004216 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004217
Avi Kivity1872a3f2009-01-04 23:26:52 +02004218 if (cs.unusable)
4219 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004220 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004221 return false;
4222 if (!cs.s)
4223 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004224 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004225 if (cs.dpl > cs_rpl)
4226 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004227 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004228 if (cs.dpl != cs_rpl)
4229 return false;
4230 }
4231 if (!cs.present)
4232 return false;
4233
4234 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4235 return true;
4236}
4237
4238static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4239{
4240 struct kvm_segment ss;
4241 unsigned int ss_rpl;
4242
4243 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004244 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004245
Avi Kivity1872a3f2009-01-04 23:26:52 +02004246 if (ss.unusable)
4247 return true;
4248 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004249 return false;
4250 if (!ss.s)
4251 return false;
4252 if (ss.dpl != ss_rpl) /* DPL != RPL */
4253 return false;
4254 if (!ss.present)
4255 return false;
4256
4257 return true;
4258}
4259
4260static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4261{
4262 struct kvm_segment var;
4263 unsigned int rpl;
4264
4265 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004266 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004267
Avi Kivity1872a3f2009-01-04 23:26:52 +02004268 if (var.unusable)
4269 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004270 if (!var.s)
4271 return false;
4272 if (!var.present)
4273 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004274 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004275 if (var.dpl < rpl) /* DPL < RPL */
4276 return false;
4277 }
4278
4279 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4280 * rights flags
4281 */
4282 return true;
4283}
4284
4285static bool tr_valid(struct kvm_vcpu *vcpu)
4286{
4287 struct kvm_segment tr;
4288
4289 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4290
Avi Kivity1872a3f2009-01-04 23:26:52 +02004291 if (tr.unusable)
4292 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004293 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004294 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004295 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004296 return false;
4297 if (!tr.present)
4298 return false;
4299
4300 return true;
4301}
4302
4303static bool ldtr_valid(struct kvm_vcpu *vcpu)
4304{
4305 struct kvm_segment ldtr;
4306
4307 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4308
Avi Kivity1872a3f2009-01-04 23:26:52 +02004309 if (ldtr.unusable)
4310 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004311 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004312 return false;
4313 if (ldtr.type != 2)
4314 return false;
4315 if (!ldtr.present)
4316 return false;
4317
4318 return true;
4319}
4320
4321static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4322{
4323 struct kvm_segment cs, ss;
4324
4325 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4326 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4327
Nadav Amitb32a9912015-03-29 16:33:04 +03004328 return ((cs.selector & SEGMENT_RPL_MASK) ==
4329 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004330}
4331
4332/*
4333 * Check if guest state is valid. Returns true if valid, false if
4334 * not.
4335 * We assume that registers are always usable
4336 */
4337static bool guest_state_valid(struct kvm_vcpu *vcpu)
4338{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004339 if (enable_unrestricted_guest)
4340 return true;
4341
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004342 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004343 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004344 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4345 return false;
4346 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4347 return false;
4348 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4349 return false;
4350 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4351 return false;
4352 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4353 return false;
4354 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4355 return false;
4356 } else {
4357 /* protected mode guest state checks */
4358 if (!cs_ss_rpl_check(vcpu))
4359 return false;
4360 if (!code_segment_valid(vcpu))
4361 return false;
4362 if (!stack_segment_valid(vcpu))
4363 return false;
4364 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4365 return false;
4366 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4367 return false;
4368 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4369 return false;
4370 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4371 return false;
4372 if (!tr_valid(vcpu))
4373 return false;
4374 if (!ldtr_valid(vcpu))
4375 return false;
4376 }
4377 /* TODO:
4378 * - Add checks on RIP
4379 * - Add checks on RFLAGS
4380 */
4381
4382 return true;
4383}
4384
Mike Dayd77c26f2007-10-08 09:02:08 -04004385static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004387 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004388 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004389 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004390
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004391 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004392 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004393 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4394 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004395 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004396 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004397 r = kvm_write_guest_page(kvm, fn++, &data,
4398 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004399 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004400 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004401 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4402 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004403 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004404 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4405 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004406 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004407 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004408 r = kvm_write_guest_page(kvm, fn, &data,
4409 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4410 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004411out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004412 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004413 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414}
4415
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004416static int init_rmode_identity_map(struct kvm *kvm)
4417{
Tang Chenf51770e2014-09-16 18:41:59 +08004418 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004419 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004420 u32 tmp;
4421
Avi Kivity089d0342009-03-23 18:26:32 +02004422 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004423 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004424
4425 /* Protect kvm->arch.ept_identity_pagetable_done. */
4426 mutex_lock(&kvm->slots_lock);
4427
Tang Chenf51770e2014-09-16 18:41:59 +08004428 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004429 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004430
Sheng Yangb927a3c2009-07-21 10:42:48 +08004431 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004432
4433 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004434 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004435 goto out2;
4436
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004437 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004438 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4439 if (r < 0)
4440 goto out;
4441 /* Set up identity-mapping pagetable for EPT in real mode */
4442 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4443 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4444 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4445 r = kvm_write_guest_page(kvm, identity_map_pfn,
4446 &tmp, i * sizeof(tmp), sizeof(tmp));
4447 if (r < 0)
4448 goto out;
4449 }
4450 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004451
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004452out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004453 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004454
4455out2:
4456 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004457 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004458}
4459
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460static void seg_setup(int seg)
4461{
Mathias Krause772e0312012-08-30 01:30:19 +02004462 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004463 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004464
4465 vmcs_write16(sf->selector, 0);
4466 vmcs_writel(sf->base, 0);
4467 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004468 ar = 0x93;
4469 if (seg == VCPU_SREG_CS)
4470 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004471
4472 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473}
4474
Sheng Yangf78e0e22007-10-29 09:40:42 +08004475static int alloc_apic_access_page(struct kvm *kvm)
4476{
Xiao Guangrong44841412012-09-07 14:14:20 +08004477 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004478 int r = 0;
4479
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004480 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004481 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004482 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004483 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4484 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004485 if (r)
4486 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004487
Tang Chen73a6d942014-09-11 13:38:00 +08004488 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004489 if (is_error_page(page)) {
4490 r = -EFAULT;
4491 goto out;
4492 }
4493
Tang Chenc24ae0d2014-09-24 15:57:58 +08004494 /*
4495 * Do not pin the page in memory, so that memory hot-unplug
4496 * is able to migrate it.
4497 */
4498 put_page(page);
4499 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004500out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004501 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004502 return r;
4503}
4504
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004505static int alloc_identity_pagetable(struct kvm *kvm)
4506{
Tang Chena255d472014-09-16 18:41:58 +08004507 /* Called with kvm->slots_lock held. */
4508
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004509 int r = 0;
4510
Tang Chena255d472014-09-16 18:41:58 +08004511 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4512
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004513 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4514 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004515
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004516 return r;
4517}
4518
Wanpeng Li991e7a02015-09-16 17:30:05 +08004519static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004520{
4521 int vpid;
4522
Avi Kivity919818a2009-03-23 18:01:29 +02004523 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004524 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004525 spin_lock(&vmx_vpid_lock);
4526 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004527 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004528 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004529 else
4530 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004531 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004532 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004533}
4534
Wanpeng Li991e7a02015-09-16 17:30:05 +08004535static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004536{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004537 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004538 return;
4539 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004540 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004541 spin_unlock(&vmx_vpid_lock);
4542}
4543
Yang Zhang8d146952013-01-25 10:18:50 +08004544#define MSR_TYPE_R 1
4545#define MSR_TYPE_W 2
4546static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4547 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004548{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004549 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004550
4551 if (!cpu_has_vmx_msr_bitmap())
4552 return;
4553
4554 /*
4555 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4556 * have the write-low and read-high bitmap offsets the wrong way round.
4557 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4558 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004559 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004560 if (type & MSR_TYPE_R)
4561 /* read-low */
4562 __clear_bit(msr, msr_bitmap + 0x000 / f);
4563
4564 if (type & MSR_TYPE_W)
4565 /* write-low */
4566 __clear_bit(msr, msr_bitmap + 0x800 / f);
4567
Sheng Yang25c5f222008-03-28 13:18:56 +08004568 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4569 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004570 if (type & MSR_TYPE_R)
4571 /* read-high */
4572 __clear_bit(msr, msr_bitmap + 0x400 / f);
4573
4574 if (type & MSR_TYPE_W)
4575 /* write-high */
4576 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4577
4578 }
4579}
4580
4581static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4582 u32 msr, int type)
4583{
4584 int f = sizeof(unsigned long);
4585
4586 if (!cpu_has_vmx_msr_bitmap())
4587 return;
4588
4589 /*
4590 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4591 * have the write-low and read-high bitmap offsets the wrong way round.
4592 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4593 */
4594 if (msr <= 0x1fff) {
4595 if (type & MSR_TYPE_R)
4596 /* read-low */
4597 __set_bit(msr, msr_bitmap + 0x000 / f);
4598
4599 if (type & MSR_TYPE_W)
4600 /* write-low */
4601 __set_bit(msr, msr_bitmap + 0x800 / f);
4602
4603 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4604 msr &= 0x1fff;
4605 if (type & MSR_TYPE_R)
4606 /* read-high */
4607 __set_bit(msr, msr_bitmap + 0x400 / f);
4608
4609 if (type & MSR_TYPE_W)
4610 /* write-high */
4611 __set_bit(msr, msr_bitmap + 0xc00 / f);
4612
Sheng Yang25c5f222008-03-28 13:18:56 +08004613 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004614}
4615
Wincy Vanf2b93282015-02-03 23:56:03 +08004616/*
4617 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4618 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4619 */
4620static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4621 unsigned long *msr_bitmap_nested,
4622 u32 msr, int type)
4623{
4624 int f = sizeof(unsigned long);
4625
4626 if (!cpu_has_vmx_msr_bitmap()) {
4627 WARN_ON(1);
4628 return;
4629 }
4630
4631 /*
4632 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4633 * have the write-low and read-high bitmap offsets the wrong way round.
4634 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4635 */
4636 if (msr <= 0x1fff) {
4637 if (type & MSR_TYPE_R &&
4638 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4639 /* read-low */
4640 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4641
4642 if (type & MSR_TYPE_W &&
4643 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4644 /* write-low */
4645 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4646
4647 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4648 msr &= 0x1fff;
4649 if (type & MSR_TYPE_R &&
4650 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4651 /* read-high */
4652 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4653
4654 if (type & MSR_TYPE_W &&
4655 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4656 /* write-high */
4657 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4658
4659 }
4660}
4661
Avi Kivity58972972009-02-24 22:26:47 +02004662static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4663{
4664 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004665 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4666 msr, MSR_TYPE_R | MSR_TYPE_W);
4667 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4668 msr, MSR_TYPE_R | MSR_TYPE_W);
4669}
4670
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004671static void vmx_enable_intercept_msr_read_x2apic(u32 msr, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004672{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004673 if (apicv_active) {
4674 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4675 msr, MSR_TYPE_R);
4676 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4677 msr, MSR_TYPE_R);
4678 } else {
4679 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive,
4680 msr, MSR_TYPE_R);
4681 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive,
4682 msr, MSR_TYPE_R);
4683 }
Yang Zhang8d146952013-01-25 10:18:50 +08004684}
4685
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004686static void vmx_disable_intercept_msr_read_x2apic(u32 msr, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004687{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004688 if (apicv_active) {
4689 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4690 msr, MSR_TYPE_R);
4691 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4692 msr, MSR_TYPE_R);
4693 } else {
4694 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive,
4695 msr, MSR_TYPE_R);
4696 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive,
4697 msr, MSR_TYPE_R);
4698 }
Yang Zhang8d146952013-01-25 10:18:50 +08004699}
4700
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004701static void vmx_disable_intercept_msr_write_x2apic(u32 msr, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004702{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004703 if (apicv_active) {
4704 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4705 msr, MSR_TYPE_W);
4706 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4707 msr, MSR_TYPE_W);
4708 } else {
4709 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive,
4710 msr, MSR_TYPE_W);
4711 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive,
4712 msr, MSR_TYPE_W);
4713 }
Avi Kivity58972972009-02-24 22:26:47 +02004714}
4715
Andrey Smetanind62caab2015-11-10 15:36:33 +03004716static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004717{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004718 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004719}
4720
Wincy Van705699a2015-02-03 23:58:17 +08004721static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
4722{
4723 struct vcpu_vmx *vmx = to_vmx(vcpu);
4724 int max_irr;
4725 void *vapic_page;
4726 u16 status;
4727
4728 if (vmx->nested.pi_desc &&
4729 vmx->nested.pi_pending) {
4730 vmx->nested.pi_pending = false;
4731 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4732 return 0;
4733
4734 max_irr = find_last_bit(
4735 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4736
4737 if (max_irr == 256)
4738 return 0;
4739
4740 vapic_page = kmap(vmx->nested.virtual_apic_page);
4741 if (!vapic_page) {
4742 WARN_ON(1);
4743 return -ENOMEM;
4744 }
4745 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4746 kunmap(vmx->nested.virtual_apic_page);
4747
4748 status = vmcs_read16(GUEST_INTR_STATUS);
4749 if ((u8)max_irr > ((u8)status & 0xff)) {
4750 status &= ~0xff;
4751 status |= (u8)max_irr;
4752 vmcs_write16(GUEST_INTR_STATUS, status);
4753 }
4754 }
4755 return 0;
4756}
4757
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004758static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4759{
4760#ifdef CONFIG_SMP
4761 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004762 /*
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08004763 * The vector of interrupt to be delivered to vcpu had
4764 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08004765 *
Haozhong Zhang3ffbe622017-09-18 09:56:50 +08004766 * Following cases will be reached in this block, and
4767 * we always send a notification event in all cases as
4768 * explained below.
4769 *
4770 * Case 1: vcpu keeps in non-root mode. Sending a
4771 * notification event posts the interrupt to vcpu.
4772 *
4773 * Case 2: vcpu exits to root mode and is still
4774 * runnable. PIR will be synced to vIRR before the
4775 * next vcpu entry. Sending a notification event in
4776 * this case has no effect, as vcpu is not in root
4777 * mode.
4778 *
4779 * Case 3: vcpu exits to root mode and is blocked.
4780 * vcpu_block() has already synced PIR to vIRR and
4781 * never blocks vcpu if vIRR is not cleared. Therefore,
4782 * a blocked vcpu here does not wait for any requested
4783 * interrupts in PIR, and sending a notification event
4784 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08004785 */
Feng Wu28b835d2015-09-18 22:29:54 +08004786
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004787 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4788 POSTED_INTR_VECTOR);
4789 return true;
4790 }
4791#endif
4792 return false;
4793}
4794
Wincy Van705699a2015-02-03 23:58:17 +08004795static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4796 int vector)
4797{
4798 struct vcpu_vmx *vmx = to_vmx(vcpu);
4799
4800 if (is_guest_mode(vcpu) &&
4801 vector == vmx->nested.posted_intr_nv) {
4802 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004803 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004804 /*
4805 * If a posted intr is not recognized by hardware,
4806 * we will accomplish it in the next vmentry.
4807 */
4808 vmx->nested.pi_pending = true;
4809 kvm_make_request(KVM_REQ_EVENT, vcpu);
4810 return 0;
4811 }
4812 return -1;
4813}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004814/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004815 * Send interrupt to vcpu via posted interrupt way.
4816 * 1. If target vcpu is running(non-root mode), send posted interrupt
4817 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4818 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4819 * interrupt from PIR in next vmentry.
4820 */
4821static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4822{
4823 struct vcpu_vmx *vmx = to_vmx(vcpu);
4824 int r;
4825
Wincy Van705699a2015-02-03 23:58:17 +08004826 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4827 if (!r)
4828 return;
4829
Yang Zhanga20ed542013-04-11 19:25:15 +08004830 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4831 return;
4832
4833 r = pi_test_and_set_on(&vmx->pi_desc);
4834 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004835 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08004836 kvm_vcpu_kick(vcpu);
4837}
4838
4839static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4840{
4841 struct vcpu_vmx *vmx = to_vmx(vcpu);
4842
4843 if (!pi_test_and_clear_on(&vmx->pi_desc))
4844 return;
4845
4846 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4847}
4848
Avi Kivity6aa8b732006-12-10 02:21:36 -08004849/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004850 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4851 * will not change in the lifetime of the guest.
4852 * Note that host-state that does change is set elsewhere. E.g., host-state
4853 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4854 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004855static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004856{
4857 u32 low32, high32;
4858 unsigned long tmpl;
4859 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004860 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004861
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004862 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004863 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4864
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004865 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004866 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004867 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4868 vmx->host_state.vmcs_host_cr4 = cr4;
4869
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004870 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004871#ifdef CONFIG_X86_64
4872 /*
4873 * Load null selectors, so we can avoid reloading them in
4874 * __vmx_load_host_state(), in case userspace uses the null selectors
4875 * too (the expected case).
4876 */
4877 vmcs_write16(HOST_DS_SELECTOR, 0);
4878 vmcs_write16(HOST_ES_SELECTOR, 0);
4879#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004880 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4881 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004882#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004883 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4884 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4885
4886 native_store_idt(&dt);
4887 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004888 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004889
Avi Kivity83287ea422012-09-16 15:10:57 +03004890 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004891
4892 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4893 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4894 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4895 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4896
4897 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4898 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4899 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4900 }
4901}
4902
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004903static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4904{
4905 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4906 if (enable_ept)
4907 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004908 if (is_guest_mode(&vmx->vcpu))
4909 vmx->vcpu.arch.cr4_guest_owned_bits &=
4910 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004911 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4912}
4913
Yang Zhang01e439b2013-04-11 19:25:12 +08004914static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4915{
4916 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4917
Andrey Smetanind62caab2015-11-10 15:36:33 +03004918 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004919 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07004920 /* Enable the preemption timer dynamically */
4921 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004922 return pin_based_exec_ctrl;
4923}
4924
Andrey Smetanind62caab2015-11-10 15:36:33 +03004925static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4926{
4927 struct vcpu_vmx *vmx = to_vmx(vcpu);
4928
4929 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004930 if (cpu_has_secondary_exec_ctrls()) {
4931 if (kvm_vcpu_apicv_active(vcpu))
4932 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4933 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4934 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4935 else
4936 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4937 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4938 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4939 }
4940
4941 if (cpu_has_vmx_msr_bitmap())
4942 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004943}
4944
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004945static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4946{
4947 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004948
4949 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4950 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4951
Paolo Bonzini35754c92015-07-29 12:05:37 +02004952 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004953 exec_control &= ~CPU_BASED_TPR_SHADOW;
4954#ifdef CONFIG_X86_64
4955 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4956 CPU_BASED_CR8_LOAD_EXITING;
4957#endif
4958 }
4959 if (!enable_ept)
4960 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4961 CPU_BASED_CR3_LOAD_EXITING |
4962 CPU_BASED_INVLPG_EXITING;
4963 return exec_control;
4964}
4965
4966static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4967{
4968 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004969 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004970 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4971 if (vmx->vpid == 0)
4972 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4973 if (!enable_ept) {
4974 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4975 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004976 /* Enable INVPCID for non-ept guests may cause performance regression. */
4977 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004978 }
4979 if (!enable_unrestricted_guest)
4980 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4981 if (!ple_gap)
4982 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03004983 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004984 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4985 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004986 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004987 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4988 (handle_vmptrld).
4989 We can NOT enable shadow_vmcs here because we don't have yet
4990 a current VMCS12
4991 */
4992 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004993
4994 if (!enable_pml)
4995 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004996
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004997 return exec_control;
4998}
4999
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005000static void ept_set_mmio_spte_mask(void)
5001{
5002 /*
5003 * EPT Misconfigurations can be generated if the value of bits 2:0
5004 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08005005 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005006 * spte.
5007 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08005008 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005009}
5010
Wanpeng Lif53cd632014-12-02 19:14:58 +08005011#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005012/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005013 * Sets up the vmcs for emulated real mode.
5014 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005015static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005016{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005017#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005019#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005021
Avi Kivity6aa8b732006-12-10 02:21:36 -08005022 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005023 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5024 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005025
Abel Gordon4607c2d2013-04-18 14:35:55 +03005026 if (enable_shadow_vmcs) {
5027 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5028 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5029 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005030 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005031 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005032
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5034
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005036 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005037 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005038
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005039 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040
Dan Williamsdfa169b2016-06-02 11:17:24 -07005041 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005042 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5043 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005044 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005045
Andrey Smetanind62caab2015-11-10 15:36:33 +03005046 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005047 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5048 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5049 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5050 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5051
5052 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005053
Li RongQing0bcf2612015-12-03 13:29:34 +08005054 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005055 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005056 }
5057
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005058 if (ple_gap) {
5059 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005060 vmx->ple_window = ple_window;
5061 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005062 }
5063
Xiao Guangrongc3707952011-07-12 03:28:04 +08005064 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5065 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005066 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5067
Avi Kivity9581d442010-10-19 16:46:55 +02005068 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5069 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005070 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005071#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005072 rdmsrl(MSR_FS_BASE, a);
5073 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5074 rdmsrl(MSR_GS_BASE, a);
5075 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5076#else
5077 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5078 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5079#endif
5080
Eddie Dong2cc51562007-05-21 07:28:09 +03005081 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5082 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005083 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005084 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005085 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086
Radim Krčmář74545702015-04-27 15:11:25 +02005087 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5088 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005089
Paolo Bonzini03916db2014-07-24 14:21:57 +02005090 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091 u32 index = vmx_msr_index[i];
5092 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005093 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094
5095 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5096 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005097 if (wrmsr_safe(index, data_low, data_high) < 0)
5098 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005099 vmx->guest_msrs[j].index = i;
5100 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005101 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005102 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005103 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005104
Gleb Natapov2961e8762013-11-25 15:37:13 +02005105
5106 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005107
5108 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005109 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005110
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005111 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005112 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005113
Wanpeng Lif53cd632014-12-02 19:14:58 +08005114 if (vmx_xsaves_supported())
5115 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5116
Peter Feiner4e595162016-07-07 14:49:58 -07005117 if (enable_pml) {
5118 ASSERT(vmx->pml_pg);
5119 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5120 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5121 }
5122
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005123 return 0;
5124}
5125
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005126static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005127{
5128 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005129 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005130 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005131
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005132 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005133
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005134 vmx->soft_vnmi_blocked = 0;
5135
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005136 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005137 kvm_set_cr8(vcpu, 0);
5138
5139 if (!init_event) {
5140 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5141 MSR_IA32_APICBASE_ENABLE;
5142 if (kvm_vcpu_is_reset_bsp(vcpu))
5143 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5144 apic_base_msr.host_initiated = true;
5145 kvm_set_apic_base(vcpu, &apic_base_msr);
5146 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005147
Avi Kivity2fb92db2011-04-27 19:42:18 +03005148 vmx_segment_cache_clear(vmx);
5149
Avi Kivity5706be02008-08-20 15:07:31 +03005150 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005151 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005152 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005153
5154 seg_setup(VCPU_SREG_DS);
5155 seg_setup(VCPU_SREG_ES);
5156 seg_setup(VCPU_SREG_FS);
5157 seg_setup(VCPU_SREG_GS);
5158 seg_setup(VCPU_SREG_SS);
5159
5160 vmcs_write16(GUEST_TR_SELECTOR, 0);
5161 vmcs_writel(GUEST_TR_BASE, 0);
5162 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5163 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5164
5165 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5166 vmcs_writel(GUEST_LDTR_BASE, 0);
5167 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5168 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5169
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005170 if (!init_event) {
5171 vmcs_write32(GUEST_SYSENTER_CS, 0);
5172 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5173 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5174 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5175 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005176
5177 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005178 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005179
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005180 vmcs_writel(GUEST_GDTR_BASE, 0);
5181 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5182
5183 vmcs_writel(GUEST_IDTR_BASE, 0);
5184 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5185
Anthony Liguori443381a2010-12-06 10:53:38 -06005186 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005187 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005188 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005189
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005190 setup_msrs(vmx);
5191
Avi Kivity6aa8b732006-12-10 02:21:36 -08005192 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5193
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005194 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005195 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005196 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005197 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005198 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005199 vmcs_write32(TPR_THRESHOLD, 0);
5200 }
5201
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005202 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005203
Andrey Smetanind62caab2015-11-10 15:36:33 +03005204 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005205 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5206
Sheng Yang2384d2b2008-01-17 15:14:33 +08005207 if (vmx->vpid != 0)
5208 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5209
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005210 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005211 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005212 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005213 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005214 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005215 vmx_fpu_activate(vcpu);
5216 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005217
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005218 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005219}
5220
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005221/*
5222 * In nested virtualization, check if L1 asked to exit on external interrupts.
5223 * For most existing hypervisors, this will always return true.
5224 */
5225static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5226{
5227 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5228 PIN_BASED_EXT_INTR_MASK;
5229}
5230
Bandan Das77b0f5d2014-04-19 18:17:45 -04005231/*
5232 * In nested virtualization, check if L1 has set
5233 * VM_EXIT_ACK_INTR_ON_EXIT
5234 */
5235static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5236{
5237 return get_vmcs12(vcpu)->vm_exit_controls &
5238 VM_EXIT_ACK_INTR_ON_EXIT;
5239}
5240
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005241static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5242{
5243 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5244 PIN_BASED_NMI_EXITING;
5245}
5246
Jan Kiszkac9a79532014-03-07 20:03:15 +01005247static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005248{
5249 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005250
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005251 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5252 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5253 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5254}
5255
Jan Kiszkac9a79532014-03-07 20:03:15 +01005256static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005257{
5258 u32 cpu_based_vm_exec_control;
5259
Jan Kiszkac9a79532014-03-07 20:03:15 +01005260 if (!cpu_has_virtual_nmis() ||
5261 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5262 enable_irq_window(vcpu);
5263 return;
5264 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005265
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005266 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5267 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5268 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5269}
5270
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005271static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005272{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005273 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005274 uint32_t intr;
5275 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005276
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005277 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005278
Avi Kivityfa89a812008-09-01 15:57:51 +03005279 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005280 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005281 int inc_eip = 0;
5282 if (vcpu->arch.interrupt.soft)
5283 inc_eip = vcpu->arch.event_exit_inst_len;
5284 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005285 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005286 return;
5287 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005288 intr = irq | INTR_INFO_VALID_MASK;
5289 if (vcpu->arch.interrupt.soft) {
5290 intr |= INTR_TYPE_SOFT_INTR;
5291 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5292 vmx->vcpu.arch.event_exit_inst_len);
5293 } else
5294 intr |= INTR_TYPE_EXT_INTR;
5295 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005296}
5297
Sheng Yangf08864b2008-05-15 18:23:25 +08005298static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5299{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005300 struct vcpu_vmx *vmx = to_vmx(vcpu);
5301
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005302 if (!is_guest_mode(vcpu)) {
5303 if (!cpu_has_virtual_nmis()) {
5304 /*
5305 * Tracking the NMI-blocked state in software is built upon
5306 * finding the next open IRQ window. This, in turn, depends on
5307 * well-behaving guests: They have to keep IRQs disabled at
5308 * least as long as the NMI handler runs. Otherwise we may
5309 * cause NMI nesting, maybe breaking the guest. But as this is
5310 * highly unlikely, we can live with the residual risk.
5311 */
5312 vmx->soft_vnmi_blocked = 1;
5313 vmx->vnmi_blocked_time = 0;
5314 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005315
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005316 ++vcpu->stat.nmi_injections;
5317 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005318 }
5319
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005320 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005321 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005322 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005323 return;
5324 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005325
Sheng Yangf08864b2008-05-15 18:23:25 +08005326 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5327 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005328}
5329
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005330static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5331{
5332 if (!cpu_has_virtual_nmis())
5333 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005334 if (to_vmx(vcpu)->nmi_known_unmasked)
5335 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005336 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005337}
5338
5339static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5340{
5341 struct vcpu_vmx *vmx = to_vmx(vcpu);
5342
5343 if (!cpu_has_virtual_nmis()) {
5344 if (vmx->soft_vnmi_blocked != masked) {
5345 vmx->soft_vnmi_blocked = masked;
5346 vmx->vnmi_blocked_time = 0;
5347 }
5348 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005349 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005350 if (masked)
5351 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5352 GUEST_INTR_STATE_NMI);
5353 else
5354 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5355 GUEST_INTR_STATE_NMI);
5356 }
5357}
5358
Jan Kiszka2505dc92013-04-14 12:12:47 +02005359static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5360{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005361 if (to_vmx(vcpu)->nested.nested_run_pending)
5362 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005363
Jan Kiszka2505dc92013-04-14 12:12:47 +02005364 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5365 return 0;
5366
5367 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5368 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5369 | GUEST_INTR_STATE_NMI));
5370}
5371
Gleb Natapov78646122009-03-23 12:12:11 +02005372static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5373{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005374 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5375 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005376 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5377 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005378}
5379
Izik Eiduscbc94022007-10-25 00:29:55 +02005380static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5381{
5382 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005383
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005384 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5385 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005386 if (ret)
5387 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005388 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005389 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005390}
5391
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005392static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005393{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005394 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005395 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005396 /*
5397 * Update instruction length as we may reinject the exception
5398 * from user space while in guest debugging mode.
5399 */
5400 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5401 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005402 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005403 return false;
5404 /* fall through */
5405 case DB_VECTOR:
5406 if (vcpu->guest_debug &
5407 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5408 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005409 /* fall through */
5410 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005411 case OF_VECTOR:
5412 case BR_VECTOR:
5413 case UD_VECTOR:
5414 case DF_VECTOR:
5415 case SS_VECTOR:
5416 case GP_VECTOR:
5417 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005418 return true;
5419 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005420 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005421 return false;
5422}
5423
5424static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5425 int vec, u32 err_code)
5426{
5427 /*
5428 * Instruction with address size override prefix opcode 0x67
5429 * Cause the #SS fault with 0 error code in VM86 mode.
5430 */
5431 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5432 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5433 if (vcpu->arch.halt_request) {
5434 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005435 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005436 }
5437 return 1;
5438 }
5439 return 0;
5440 }
5441
5442 /*
5443 * Forward all other exceptions that are valid in real mode.
5444 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5445 * the required debugging infrastructure rework.
5446 */
5447 kvm_queue_exception(vcpu, vec);
5448 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005449}
5450
Andi Kleena0861c02009-06-08 17:37:09 +08005451/*
5452 * Trigger machine check on the host. We assume all the MSRs are already set up
5453 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5454 * We pass a fake environment to the machine check handler because we want
5455 * the guest to be always treated like user space, no matter what context
5456 * it used internally.
5457 */
5458static void kvm_machine_check(void)
5459{
5460#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5461 struct pt_regs regs = {
5462 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5463 .flags = X86_EFLAGS_IF,
5464 };
5465
5466 do_machine_check(&regs, 0);
5467#endif
5468}
5469
Avi Kivity851ba692009-08-24 11:10:17 +03005470static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005471{
5472 /* already handled by vcpu_run */
5473 return 1;
5474}
5475
Avi Kivity851ba692009-08-24 11:10:17 +03005476static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005477{
Avi Kivity1155f762007-11-22 11:30:47 +02005478 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005479 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005480 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005481 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005482 u32 vect_info;
5483 enum emulation_result er;
5484
Avi Kivity1155f762007-11-22 11:30:47 +02005485 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005486 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005487
Andi Kleena0861c02009-06-08 17:37:09 +08005488 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005489 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005490
Jim Mattson3f618a02016-12-12 11:01:37 -08005491 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005492 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005493
5494 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005495 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005496 return 1;
5497 }
5498
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005499 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005500 if (is_guest_mode(vcpu)) {
5501 kvm_queue_exception(vcpu, UD_VECTOR);
5502 return 1;
5503 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005504 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005505 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005506 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005507 return 1;
5508 }
5509
Avi Kivity6aa8b732006-12-10 02:21:36 -08005510 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005511 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005512 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005513
5514 /*
5515 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5516 * MMIO, it is better to report an internal error.
5517 * See the comments in vmx_handle_exit.
5518 */
5519 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5520 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5521 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5522 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005523 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005524 vcpu->run->internal.data[0] = vect_info;
5525 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005526 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005527 return 0;
5528 }
5529
Avi Kivity6aa8b732006-12-10 02:21:36 -08005530 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005531 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005532 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005533 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005534 trace_kvm_page_fault(cr2, error_code);
5535
Gleb Natapov3298b752009-05-11 13:35:46 +03005536 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005537 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005538 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005539 }
5540
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005541 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005542
5543 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5544 return handle_rmode_exception(vcpu, ex_no, error_code);
5545
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005546 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005547 case AC_VECTOR:
5548 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5549 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005550 case DB_VECTOR:
5551 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5552 if (!(vcpu->guest_debug &
5553 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005554 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005555 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005556 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5557 skip_emulated_instruction(vcpu);
5558
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005559 kvm_queue_exception(vcpu, DB_VECTOR);
5560 return 1;
5561 }
5562 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5563 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5564 /* fall through */
5565 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005566 /*
5567 * Update instruction length as we may reinject #BP from
5568 * user space while in guest debugging mode. Reading it for
5569 * #DB as well causes no harm, it is not used in that case.
5570 */
5571 vmx->vcpu.arch.event_exit_inst_len =
5572 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005574 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005575 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5576 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005577 break;
5578 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005579 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5580 kvm_run->ex.exception = ex_no;
5581 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005582 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005583 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005584 return 0;
5585}
5586
Avi Kivity851ba692009-08-24 11:10:17 +03005587static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005588{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005589 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005590 return 1;
5591}
5592
Avi Kivity851ba692009-08-24 11:10:17 +03005593static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005594{
Avi Kivity851ba692009-08-24 11:10:17 +03005595 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005596 return 0;
5597}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005598
Avi Kivity851ba692009-08-24 11:10:17 +03005599static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005600{
He, Qingbfdaab02007-09-12 14:18:28 +08005601 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005602 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005603 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005604
He, Qingbfdaab02007-09-12 14:18:28 +08005605 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005606 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005607 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005608
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005609 ++vcpu->stat.io_exits;
5610
5611 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005612 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005613
5614 port = exit_qualification >> 16;
5615 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005616 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005617
5618 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005619}
5620
Ingo Molnar102d8322007-02-19 14:37:47 +02005621static void
5622vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5623{
5624 /*
5625 * Patch in the VMCALL instruction:
5626 */
5627 hypercall[0] = 0x0f;
5628 hypercall[1] = 0x01;
5629 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005630}
5631
Wincy Vanb9c237b2015-02-03 23:56:30 +08005632static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005633{
5634 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005635 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005636
Wincy Vanb9c237b2015-02-03 23:56:30 +08005637 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005638 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5639 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5640 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5641 return (val & always_on) == always_on;
5642}
5643
Guo Chao0fa06072012-06-28 15:16:19 +08005644/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005645static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5646{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005647 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005648 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5649 unsigned long orig_val = val;
5650
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005651 /*
5652 * We get here when L2 changed cr0 in a way that did not change
5653 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005654 * but did change L0 shadowed bits. So we first calculate the
5655 * effective cr0 value that L1 would like to write into the
5656 * hardware. It consists of the L2-owned bits from the new
5657 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005658 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005659 val = (val & ~vmcs12->cr0_guest_host_mask) |
5660 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5661
Wincy Vanb9c237b2015-02-03 23:56:30 +08005662 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005663 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005664
5665 if (kvm_set_cr0(vcpu, val))
5666 return 1;
5667 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005668 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005669 } else {
5670 if (to_vmx(vcpu)->nested.vmxon &&
5671 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5672 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005673 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005674 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005675}
5676
5677static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5678{
5679 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005680 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5681 unsigned long orig_val = val;
5682
5683 /* analogously to handle_set_cr0 */
5684 val = (val & ~vmcs12->cr4_guest_host_mask) |
5685 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5686 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005687 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005688 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005689 return 0;
5690 } else
5691 return kvm_set_cr4(vcpu, val);
5692}
5693
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005694/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005695static void handle_clts(struct kvm_vcpu *vcpu)
5696{
5697 if (is_guest_mode(vcpu)) {
5698 /*
5699 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5700 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5701 * just pretend it's off (also in arch.cr0 for fpu_activate).
5702 */
5703 vmcs_writel(CR0_READ_SHADOW,
5704 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5705 vcpu->arch.cr0 &= ~X86_CR0_TS;
5706 } else
5707 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5708}
5709
Avi Kivity851ba692009-08-24 11:10:17 +03005710static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005711{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005712 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005713 int cr;
5714 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005715 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005716
He, Qingbfdaab02007-09-12 14:18:28 +08005717 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005718 cr = exit_qualification & 15;
5719 reg = (exit_qualification >> 8) & 15;
5720 switch ((exit_qualification >> 4) & 3) {
5721 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005722 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005723 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005724 switch (cr) {
5725 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005726 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005727 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005728 return 1;
5729 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005730 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005731 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005732 return 1;
5733 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005734 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005735 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005736 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005737 case 8: {
5738 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005739 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005740 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005741 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005742 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005743 return 1;
5744 if (cr8_prev <= cr8)
5745 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005746 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005747 return 0;
5748 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005749 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005750 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005751 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005752 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005753 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005754 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005755 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005756 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005757 case 1: /*mov from cr*/
5758 switch (cr) {
5759 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005760 val = kvm_read_cr3(vcpu);
5761 kvm_register_write(vcpu, reg, val);
5762 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005763 skip_emulated_instruction(vcpu);
5764 return 1;
5765 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005766 val = kvm_get_cr8(vcpu);
5767 kvm_register_write(vcpu, reg, val);
5768 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005769 skip_emulated_instruction(vcpu);
5770 return 1;
5771 }
5772 break;
5773 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005774 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005775 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005776 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005777
5778 skip_emulated_instruction(vcpu);
5779 return 1;
5780 default:
5781 break;
5782 }
Avi Kivity851ba692009-08-24 11:10:17 +03005783 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005784 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005785 (int)(exit_qualification >> 4) & 3, cr);
5786 return 0;
5787}
5788
Avi Kivity851ba692009-08-24 11:10:17 +03005789static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005790{
He, Qingbfdaab02007-09-12 14:18:28 +08005791 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005792 int dr, dr7, reg;
5793
5794 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5795 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5796
5797 /* First, if DR does not exist, trigger UD */
5798 if (!kvm_require_dr(vcpu, dr))
5799 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005800
Jan Kiszkaf2483412010-01-20 18:20:20 +01005801 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005802 if (!kvm_require_cpl(vcpu, 0))
5803 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005804 dr7 = vmcs_readl(GUEST_DR7);
5805 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005806 /*
5807 * As the vm-exit takes precedence over the debug trap, we
5808 * need to emulate the latter, either for the host or the
5809 * guest debugging itself.
5810 */
5811 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005812 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005813 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005814 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005815 vcpu->run->debug.arch.exception = DB_VECTOR;
5816 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005817 return 0;
5818 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005819 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005820 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005821 kvm_queue_exception(vcpu, DB_VECTOR);
5822 return 1;
5823 }
5824 }
5825
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005826 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01005827 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5828 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005829
5830 /*
5831 * No more DR vmexits; force a reload of the debug registers
5832 * and reenter on this instruction. The next vmexit will
5833 * retrieve the full state of the debug registers.
5834 */
5835 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5836 return 1;
5837 }
5838
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005839 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5840 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005841 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005842
5843 if (kvm_get_dr(vcpu, dr, &val))
5844 return 1;
5845 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005846 } else
Nadav Amit57773922014-06-18 17:19:23 +03005847 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005848 return 1;
5849
Avi Kivity6aa8b732006-12-10 02:21:36 -08005850 skip_emulated_instruction(vcpu);
5851 return 1;
5852}
5853
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005854static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5855{
5856 return vcpu->arch.dr6;
5857}
5858
5859static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5860{
5861}
5862
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005863static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5864{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005865 get_debugreg(vcpu->arch.db[0], 0);
5866 get_debugreg(vcpu->arch.db[1], 1);
5867 get_debugreg(vcpu->arch.db[2], 2);
5868 get_debugreg(vcpu->arch.db[3], 3);
5869 get_debugreg(vcpu->arch.dr6, 6);
5870 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5871
5872 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01005873 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005874}
5875
Gleb Natapov020df072010-04-13 10:05:23 +03005876static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5877{
5878 vmcs_writel(GUEST_DR7, val);
5879}
5880
Avi Kivity851ba692009-08-24 11:10:17 +03005881static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005882{
Avi Kivity06465c52007-02-28 20:46:53 +02005883 kvm_emulate_cpuid(vcpu);
5884 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005885}
5886
Avi Kivity851ba692009-08-24 11:10:17 +03005887static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005888{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005889 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005890 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005891
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005892 msr_info.index = ecx;
5893 msr_info.host_initiated = false;
5894 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005895 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005896 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005897 return 1;
5898 }
5899
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005900 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005901
Avi Kivity6aa8b732006-12-10 02:21:36 -08005902 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005903 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5904 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005905 skip_emulated_instruction(vcpu);
5906 return 1;
5907}
5908
Avi Kivity851ba692009-08-24 11:10:17 +03005909static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005910{
Will Auld8fe8ab42012-11-29 12:42:12 -08005911 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005912 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5913 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5914 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005915
Will Auld8fe8ab42012-11-29 12:42:12 -08005916 msr.data = data;
5917 msr.index = ecx;
5918 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005919 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005920 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005921 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005922 return 1;
5923 }
5924
Avi Kivity59200272010-01-25 19:47:02 +02005925 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005926 skip_emulated_instruction(vcpu);
5927 return 1;
5928}
5929
Avi Kivity851ba692009-08-24 11:10:17 +03005930static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005931{
Avi Kivity3842d132010-07-27 12:30:24 +03005932 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005933 return 1;
5934}
5935
Avi Kivity851ba692009-08-24 11:10:17 +03005936static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005937{
Eddie Dong85f455f2007-07-06 12:20:49 +03005938 u32 cpu_based_vm_exec_control;
5939
5940 /* clear pending irq */
5941 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5942 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5943 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005944
Avi Kivity3842d132010-07-27 12:30:24 +03005945 kvm_make_request(KVM_REQ_EVENT, vcpu);
5946
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005947 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005948 return 1;
5949}
5950
Avi Kivity851ba692009-08-24 11:10:17 +03005951static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005952{
Avi Kivityd3bef152007-06-05 15:53:05 +03005953 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005954}
5955
Avi Kivity851ba692009-08-24 11:10:17 +03005956static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005957{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005958 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005959}
5960
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005961static int handle_invd(struct kvm_vcpu *vcpu)
5962{
Andre Przywara51d8b662010-12-21 11:12:02 +01005963 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005964}
5965
Avi Kivity851ba692009-08-24 11:10:17 +03005966static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005967{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005968 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005969
5970 kvm_mmu_invlpg(vcpu, exit_qualification);
5971 skip_emulated_instruction(vcpu);
5972 return 1;
5973}
5974
Avi Kivityfee84b02011-11-10 14:57:25 +02005975static int handle_rdpmc(struct kvm_vcpu *vcpu)
5976{
5977 int err;
5978
5979 err = kvm_rdpmc(vcpu);
5980 kvm_complete_insn_gp(vcpu, err);
5981
5982 return 1;
5983}
5984
Avi Kivity851ba692009-08-24 11:10:17 +03005985static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005986{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005987 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005988 return 1;
5989}
5990
Dexuan Cui2acf9232010-06-10 11:27:12 +08005991static int handle_xsetbv(struct kvm_vcpu *vcpu)
5992{
5993 u64 new_bv = kvm_read_edx_eax(vcpu);
5994 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5995
5996 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5997 skip_emulated_instruction(vcpu);
5998 return 1;
5999}
6000
Wanpeng Lif53cd632014-12-02 19:14:58 +08006001static int handle_xsaves(struct kvm_vcpu *vcpu)
6002{
6003 skip_emulated_instruction(vcpu);
6004 WARN(1, "this should never happen\n");
6005 return 1;
6006}
6007
6008static int handle_xrstors(struct kvm_vcpu *vcpu)
6009{
6010 skip_emulated_instruction(vcpu);
6011 WARN(1, "this should never happen\n");
6012 return 1;
6013}
6014
Avi Kivity851ba692009-08-24 11:10:17 +03006015static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006016{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006017 if (likely(fasteoi)) {
6018 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6019 int access_type, offset;
6020
6021 access_type = exit_qualification & APIC_ACCESS_TYPE;
6022 offset = exit_qualification & APIC_ACCESS_OFFSET;
6023 /*
6024 * Sane guest uses MOV to write EOI, with written value
6025 * not cared. So make a short-circuit here by avoiding
6026 * heavy instruction emulation.
6027 */
6028 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6029 (offset == APIC_EOI)) {
6030 kvm_lapic_set_eoi(vcpu);
6031 skip_emulated_instruction(vcpu);
6032 return 1;
6033 }
6034 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006035 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006036}
6037
Yang Zhangc7c9c562013-01-25 10:18:51 +08006038static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6039{
6040 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6041 int vector = exit_qualification & 0xff;
6042
6043 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6044 kvm_apic_set_eoi_accelerated(vcpu, vector);
6045 return 1;
6046}
6047
Yang Zhang83d4c282013-01-25 10:18:49 +08006048static int handle_apic_write(struct kvm_vcpu *vcpu)
6049{
6050 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6051 u32 offset = exit_qualification & 0xfff;
6052
6053 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6054 kvm_apic_write_nodecode(vcpu, offset);
6055 return 1;
6056}
6057
Avi Kivity851ba692009-08-24 11:10:17 +03006058static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006059{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006061 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006062 bool has_error_code = false;
6063 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006064 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006065 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006066
6067 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006068 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006069 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006070
6071 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6072
6073 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006074 if (reason == TASK_SWITCH_GATE && idt_v) {
6075 switch (type) {
6076 case INTR_TYPE_NMI_INTR:
6077 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006078 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006079 break;
6080 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006081 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006082 kvm_clear_interrupt_queue(vcpu);
6083 break;
6084 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006085 if (vmx->idt_vectoring_info &
6086 VECTORING_INFO_DELIVER_CODE_MASK) {
6087 has_error_code = true;
6088 error_code =
6089 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6090 }
6091 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006092 case INTR_TYPE_SOFT_EXCEPTION:
6093 kvm_clear_exception_queue(vcpu);
6094 break;
6095 default:
6096 break;
6097 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006098 }
Izik Eidus37817f22008-03-24 23:14:53 +02006099 tss_selector = exit_qualification;
6100
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006101 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6102 type != INTR_TYPE_EXT_INTR &&
6103 type != INTR_TYPE_NMI_INTR))
6104 skip_emulated_instruction(vcpu);
6105
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006106 if (kvm_task_switch(vcpu, tss_selector,
6107 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6108 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006109 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6110 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6111 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006112 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006113 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006114
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006115 /*
6116 * TODO: What about debug traps on tss switch?
6117 * Are we supposed to inject them and update dr6?
6118 */
6119
6120 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006121}
6122
Avi Kivity851ba692009-08-24 11:10:17 +03006123static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006124{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006125 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006126 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006127 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006128 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006129
Sheng Yangf9c617f2009-03-25 10:08:52 +08006130 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006131
Sheng Yang14394422008-04-28 12:24:45 +08006132 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006133 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006134 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6135 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6136 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006137 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006138 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6139 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006140 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6141 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006142 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006143 }
6144
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006145 /*
6146 * EPT violation happened while executing iret from NMI,
6147 * "blocked by NMI" bit has to be set before next VM entry.
6148 * There are errata that may cause this bit to not be set:
6149 * AAK134, BY25.
6150 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006151 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6152 cpu_has_virtual_nmis() &&
6153 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006154 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6155
Sheng Yang14394422008-04-28 12:24:45 +08006156 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006157 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006158
Bandan Dasd95c5562016-07-12 18:18:51 -04006159 /* it is a read fault? */
6160 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6161 /* it is a write fault? */
6162 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006163 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006164 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006165 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006166 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006167
Yang Zhang25d92082013-08-06 12:00:32 +03006168 vcpu->arch.exit_qualification = exit_qualification;
6169
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006170 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006171}
6172
Avi Kivity851ba692009-08-24 11:10:17 +03006173static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006174{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006175 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006176 gpa_t gpa;
6177
6178 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006179 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006180 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006181 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006182 return 1;
6183 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006184
Paolo Bonzini450869d2015-11-04 13:41:21 +01006185 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006186 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006187 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6188 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006189
6190 if (unlikely(ret == RET_MMIO_PF_INVALID))
6191 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6192
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006193 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006194 return 1;
6195
6196 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006197 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006198
Avi Kivity851ba692009-08-24 11:10:17 +03006199 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6200 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006201
6202 return 0;
6203}
6204
Avi Kivity851ba692009-08-24 11:10:17 +03006205static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006206{
6207 u32 cpu_based_vm_exec_control;
6208
6209 /* clear pending NMI */
6210 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6211 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6212 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6213 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006214 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006215
6216 return 1;
6217}
6218
Mohammed Gamal80ced182009-09-01 12:48:18 +02006219static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006220{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006221 struct vcpu_vmx *vmx = to_vmx(vcpu);
6222 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006223 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006224 u32 cpu_exec_ctrl;
6225 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006226 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006227
6228 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6229 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006230
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006231 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006232 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006233 return handle_interrupt_window(&vmx->vcpu);
6234
Avi Kivityde87dcd2012-06-12 20:21:38 +03006235 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6236 return 1;
6237
Gleb Natapov991eebf2013-04-11 12:10:51 +03006238 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006239
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006240 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006241 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006242 ret = 0;
6243 goto out;
6244 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006245
Avi Kivityde5f70e2012-06-12 20:22:28 +03006246 if (err != EMULATE_DONE) {
6247 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6248 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6249 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006250 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006251 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006252
Gleb Natapov8d76c492013-05-08 18:38:44 +03006253 if (vcpu->arch.halt_request) {
6254 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006255 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006256 goto out;
6257 }
6258
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006259 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006260 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006261 if (need_resched())
6262 schedule();
6263 }
6264
Mohammed Gamal80ced182009-09-01 12:48:18 +02006265out:
6266 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006267}
6268
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006269static int __grow_ple_window(int val)
6270{
6271 if (ple_window_grow < 1)
6272 return ple_window;
6273
6274 val = min(val, ple_window_actual_max);
6275
6276 if (ple_window_grow < ple_window)
6277 val *= ple_window_grow;
6278 else
6279 val += ple_window_grow;
6280
6281 return val;
6282}
6283
6284static int __shrink_ple_window(int val, int modifier, int minimum)
6285{
6286 if (modifier < 1)
6287 return ple_window;
6288
6289 if (modifier < ple_window)
6290 val /= modifier;
6291 else
6292 val -= modifier;
6293
6294 return max(val, minimum);
6295}
6296
6297static void grow_ple_window(struct kvm_vcpu *vcpu)
6298{
6299 struct vcpu_vmx *vmx = to_vmx(vcpu);
6300 int old = vmx->ple_window;
6301
6302 vmx->ple_window = __grow_ple_window(old);
6303
6304 if (vmx->ple_window != old)
6305 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006306
6307 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006308}
6309
6310static void shrink_ple_window(struct kvm_vcpu *vcpu)
6311{
6312 struct vcpu_vmx *vmx = to_vmx(vcpu);
6313 int old = vmx->ple_window;
6314
6315 vmx->ple_window = __shrink_ple_window(old,
6316 ple_window_shrink, ple_window);
6317
6318 if (vmx->ple_window != old)
6319 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006320
6321 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006322}
6323
6324/*
6325 * ple_window_actual_max is computed to be one grow_ple_window() below
6326 * ple_window_max. (See __grow_ple_window for the reason.)
6327 * This prevents overflows, because ple_window_max is int.
6328 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6329 * this process.
6330 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6331 */
6332static void update_ple_window_actual_max(void)
6333{
6334 ple_window_actual_max =
6335 __shrink_ple_window(max(ple_window_max, ple_window),
6336 ple_window_grow, INT_MIN);
6337}
6338
Feng Wubf9f6ac2015-09-18 22:29:55 +08006339/*
6340 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6341 */
6342static void wakeup_handler(void)
6343{
6344 struct kvm_vcpu *vcpu;
6345 int cpu = smp_processor_id();
6346
6347 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6348 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6349 blocked_vcpu_list) {
6350 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6351
6352 if (pi_test_on(pi_desc) == 1)
6353 kvm_vcpu_kick(vcpu);
6354 }
6355 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6356}
6357
Tiejun Chenf2c76482014-10-28 10:14:47 +08006358static __init int hardware_setup(void)
6359{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006360 int r = -ENOMEM, i, msr;
6361
6362 rdmsrl_safe(MSR_EFER, &host_efer);
6363
6364 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6365 kvm_define_shared_msr(i, vmx_msr_index[i]);
6366
6367 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6368 if (!vmx_io_bitmap_a)
6369 return r;
6370
6371 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6372 if (!vmx_io_bitmap_b)
6373 goto out;
6374
6375 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
6376 if (!vmx_msr_bitmap_legacy)
6377 goto out1;
6378
6379 vmx_msr_bitmap_legacy_x2apic =
6380 (unsigned long *)__get_free_page(GFP_KERNEL);
6381 if (!vmx_msr_bitmap_legacy_x2apic)
6382 goto out2;
6383
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006384 vmx_msr_bitmap_legacy_x2apic_apicv_inactive =
6385 (unsigned long *)__get_free_page(GFP_KERNEL);
6386 if (!vmx_msr_bitmap_legacy_x2apic_apicv_inactive)
6387 goto out3;
6388
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006389 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
6390 if (!vmx_msr_bitmap_longmode)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006391 goto out4;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006392
6393 vmx_msr_bitmap_longmode_x2apic =
6394 (unsigned long *)__get_free_page(GFP_KERNEL);
6395 if (!vmx_msr_bitmap_longmode_x2apic)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006396 goto out5;
6397
6398 vmx_msr_bitmap_longmode_x2apic_apicv_inactive =
6399 (unsigned long *)__get_free_page(GFP_KERNEL);
6400 if (!vmx_msr_bitmap_longmode_x2apic_apicv_inactive)
6401 goto out6;
Wincy Van3af18d92015-02-03 23:49:31 +08006402
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006403 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6404 if (!vmx_vmread_bitmap)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006405 goto out7;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006406
6407 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6408 if (!vmx_vmwrite_bitmap)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006409 goto out8;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006410
6411 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6412 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6413
6414 /*
6415 * Allow direct access to the PC debug port (it is often used for I/O
6416 * delays, but the vmexits simply slow things down).
6417 */
6418 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6419 clear_bit(0x80, vmx_io_bitmap_a);
6420
6421 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6422
6423 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6424 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6425
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006426 if (setup_vmcs_config(&vmcs_config) < 0) {
6427 r = -EIO;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006428 goto out9;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006429 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006430
6431 if (boot_cpu_has(X86_FEATURE_NX))
6432 kvm_enable_efer_bits(EFER_NX);
6433
6434 if (!cpu_has_vmx_vpid())
6435 enable_vpid = 0;
6436 if (!cpu_has_vmx_shadow_vmcs())
6437 enable_shadow_vmcs = 0;
6438 if (enable_shadow_vmcs)
6439 init_vmcs_shadow_fields();
6440
6441 if (!cpu_has_vmx_ept() ||
6442 !cpu_has_vmx_ept_4levels()) {
6443 enable_ept = 0;
6444 enable_unrestricted_guest = 0;
6445 enable_ept_ad_bits = 0;
6446 }
6447
6448 if (!cpu_has_vmx_ept_ad_bits())
6449 enable_ept_ad_bits = 0;
6450
6451 if (!cpu_has_vmx_unrestricted_guest())
6452 enable_unrestricted_guest = 0;
6453
Paolo Bonziniad15a292015-01-30 16:18:49 +01006454 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006455 flexpriority_enabled = 0;
6456
Paolo Bonziniad15a292015-01-30 16:18:49 +01006457 /*
6458 * set_apic_access_page_addr() is used to reload apic access
6459 * page upon invalidation. No need to do anything if not
6460 * using the APIC_ACCESS_ADDR VMCS field.
6461 */
6462 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006463 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006464
6465 if (!cpu_has_vmx_tpr_shadow())
6466 kvm_x86_ops->update_cr8_intercept = NULL;
6467
6468 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6469 kvm_disable_largepages();
6470
6471 if (!cpu_has_vmx_ple())
6472 ple_gap = 0;
6473
6474 if (!cpu_has_vmx_apicv())
6475 enable_apicv = 0;
6476
Haozhong Zhang64903d62015-10-20 15:39:09 +08006477 if (cpu_has_vmx_tsc_scaling()) {
6478 kvm_has_tsc_control = true;
6479 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6480 kvm_tsc_scaling_ratio_frac_bits = 48;
6481 }
6482
Tiejun Chenbaa03522014-12-23 16:21:11 +08006483 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6484 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6485 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6486 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6487 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6488 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006489
6490 memcpy(vmx_msr_bitmap_legacy_x2apic,
6491 vmx_msr_bitmap_legacy, PAGE_SIZE);
6492 memcpy(vmx_msr_bitmap_longmode_x2apic,
6493 vmx_msr_bitmap_longmode, PAGE_SIZE);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006494 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv_inactive,
6495 vmx_msr_bitmap_legacy, PAGE_SIZE);
6496 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv_inactive,
6497 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006498
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006499 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6500
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006501 /*
6502 * enable_apicv && kvm_vcpu_apicv_active()
6503 */
Roman Kagan3ce424e2016-05-18 17:48:20 +03006504 for (msr = 0x800; msr <= 0x8ff; msr++)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006505 vmx_disable_intercept_msr_read_x2apic(msr, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006506
Roman Kagan3ce424e2016-05-18 17:48:20 +03006507 /* TMCCT */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006508 vmx_enable_intercept_msr_read_x2apic(0x839, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006509 /* TPR */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006510 vmx_disable_intercept_msr_write_x2apic(0x808, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006511 /* EOI */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006512 vmx_disable_intercept_msr_write_x2apic(0x80b, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006513 /* SELF-IPI */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006514 vmx_disable_intercept_msr_write_x2apic(0x83f, true);
6515
6516 /*
6517 * (enable_apicv && !kvm_vcpu_apicv_active()) ||
6518 * !enable_apicv
6519 */
6520 /* TPR */
6521 vmx_disable_intercept_msr_read_x2apic(0x808, false);
6522 vmx_disable_intercept_msr_write_x2apic(0x808, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006523
6524 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006525 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006526 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6527 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006528 0ull, VMX_EPT_EXECUTABLE_MASK,
6529 cpu_has_vmx_ept_execute_only() ?
6530 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006531 ept_set_mmio_spte_mask();
6532 kvm_enable_tdp();
6533 } else
6534 kvm_disable_tdp();
6535
6536 update_ple_window_actual_max();
6537
Kai Huang843e4332015-01-28 10:54:28 +08006538 /*
6539 * Only enable PML when hardware supports PML feature, and both EPT
6540 * and EPT A/D bit features are enabled -- PML depends on them to work.
6541 */
6542 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6543 enable_pml = 0;
6544
6545 if (!enable_pml) {
6546 kvm_x86_ops->slot_enable_log_dirty = NULL;
6547 kvm_x86_ops->slot_disable_log_dirty = NULL;
6548 kvm_x86_ops->flush_log_dirty = NULL;
6549 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6550 }
6551
Yunhong Jiang64672c92016-06-13 14:19:59 -07006552 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6553 u64 vmx_msr;
6554
6555 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6556 cpu_preemption_timer_multi =
6557 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6558 } else {
6559 kvm_x86_ops->set_hv_timer = NULL;
6560 kvm_x86_ops->cancel_hv_timer = NULL;
6561 }
6562
Feng Wubf9f6ac2015-09-18 22:29:55 +08006563 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6564
Ashok Rajc45dcc72016-06-22 14:59:56 +08006565 kvm_mce_cap_supported |= MCG_LMCE_P;
6566
Tiejun Chenf2c76482014-10-28 10:14:47 +08006567 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006568
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006569out9:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006570 free_page((unsigned long)vmx_vmwrite_bitmap);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006571out8:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006572 free_page((unsigned long)vmx_vmread_bitmap);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006573out7:
6574 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv_inactive);
Wincy Van3af18d92015-02-03 23:49:31 +08006575out6:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006576 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006577out5:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006578 free_page((unsigned long)vmx_msr_bitmap_longmode);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006579out4:
6580 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv_inactive);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006581out3:
6582 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6583out2:
6584 free_page((unsigned long)vmx_msr_bitmap_legacy);
6585out1:
6586 free_page((unsigned long)vmx_io_bitmap_b);
6587out:
6588 free_page((unsigned long)vmx_io_bitmap_a);
6589
6590 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006591}
6592
6593static __exit void hardware_unsetup(void)
6594{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006595 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006596 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv_inactive);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006597 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006598 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv_inactive);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006599 free_page((unsigned long)vmx_msr_bitmap_legacy);
6600 free_page((unsigned long)vmx_msr_bitmap_longmode);
6601 free_page((unsigned long)vmx_io_bitmap_b);
6602 free_page((unsigned long)vmx_io_bitmap_a);
6603 free_page((unsigned long)vmx_vmwrite_bitmap);
6604 free_page((unsigned long)vmx_vmread_bitmap);
6605
Tiejun Chenf2c76482014-10-28 10:14:47 +08006606 free_kvm_area();
6607}
6608
Avi Kivity6aa8b732006-12-10 02:21:36 -08006609/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006610 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6611 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6612 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006613static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006614{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006615 if (ple_gap)
6616 grow_ple_window(vcpu);
6617
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006618 skip_emulated_instruction(vcpu);
6619 kvm_vcpu_on_spin(vcpu);
6620
6621 return 1;
6622}
6623
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006624static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006625{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006626 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006627 return 1;
6628}
6629
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006630static int handle_mwait(struct kvm_vcpu *vcpu)
6631{
6632 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6633 return handle_nop(vcpu);
6634}
6635
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006636static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6637{
6638 return 1;
6639}
6640
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006641static int handle_monitor(struct kvm_vcpu *vcpu)
6642{
6643 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6644 return handle_nop(vcpu);
6645}
6646
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006647/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006648 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6649 * We could reuse a single VMCS for all the L2 guests, but we also want the
6650 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6651 * allows keeping them loaded on the processor, and in the future will allow
6652 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6653 * every entry if they never change.
6654 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6655 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6656 *
6657 * The following functions allocate and free a vmcs02 in this pool.
6658 */
6659
6660/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6661static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6662{
6663 struct vmcs02_list *item;
6664 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6665 if (item->vmptr == vmx->nested.current_vmptr) {
6666 list_move(&item->list, &vmx->nested.vmcs02_pool);
6667 return &item->vmcs02;
6668 }
6669
6670 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6671 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08006672 item = list_last_entry(&vmx->nested.vmcs02_pool,
6673 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006674 item->vmptr = vmx->nested.current_vmptr;
6675 list_move(&item->list, &vmx->nested.vmcs02_pool);
6676 return &item->vmcs02;
6677 }
6678
6679 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006680 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006681 if (!item)
6682 return NULL;
6683 item->vmcs02.vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07006684 item->vmcs02.shadow_vmcs = NULL;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006685 if (!item->vmcs02.vmcs) {
6686 kfree(item);
6687 return NULL;
6688 }
6689 loaded_vmcs_init(&item->vmcs02);
6690 item->vmptr = vmx->nested.current_vmptr;
6691 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6692 vmx->nested.vmcs02_num++;
6693 return &item->vmcs02;
6694}
6695
6696/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6697static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6698{
6699 struct vmcs02_list *item;
6700 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6701 if (item->vmptr == vmptr) {
6702 free_loaded_vmcs(&item->vmcs02);
6703 list_del(&item->list);
6704 kfree(item);
6705 vmx->nested.vmcs02_num--;
6706 return;
6707 }
6708}
6709
6710/*
6711 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006712 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6713 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006714 */
6715static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6716{
6717 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006718
6719 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006720 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006721 /*
6722 * Something will leak if the above WARN triggers. Better than
6723 * a use-after-free.
6724 */
6725 if (vmx->loaded_vmcs == &item->vmcs02)
6726 continue;
6727
6728 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006729 list_del(&item->list);
6730 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006731 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006732 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006733}
6734
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006735/*
6736 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6737 * set the success or error code of an emulated VMX instruction, as specified
6738 * by Vol 2B, VMX Instruction Reference, "Conventions".
6739 */
6740static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6741{
6742 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6743 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6744 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6745}
6746
6747static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6748{
6749 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6750 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6751 X86_EFLAGS_SF | X86_EFLAGS_OF))
6752 | X86_EFLAGS_CF);
6753}
6754
Abel Gordon145c28d2013-04-18 14:36:55 +03006755static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006756 u32 vm_instruction_error)
6757{
6758 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6759 /*
6760 * failValid writes the error number to the current VMCS, which
6761 * can't be done there isn't a current VMCS.
6762 */
6763 nested_vmx_failInvalid(vcpu);
6764 return;
6765 }
6766 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6767 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6768 X86_EFLAGS_SF | X86_EFLAGS_OF))
6769 | X86_EFLAGS_ZF);
6770 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6771 /*
6772 * We don't need to force a shadow sync because
6773 * VM_INSTRUCTION_ERROR is not shadowed
6774 */
6775}
Abel Gordon145c28d2013-04-18 14:36:55 +03006776
Wincy Vanff651cb2014-12-11 08:52:58 +03006777static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6778{
6779 /* TODO: not to reset guest simply here. */
6780 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006781 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006782}
6783
Jan Kiszkaf4124502014-03-07 20:03:13 +01006784static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6785{
6786 struct vcpu_vmx *vmx =
6787 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6788
6789 vmx->nested.preemption_timer_expired = true;
6790 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6791 kvm_vcpu_kick(&vmx->vcpu);
6792
6793 return HRTIMER_NORESTART;
6794}
6795
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006796/*
Bandan Das19677e32014-05-06 02:19:15 -04006797 * Decode the memory-address operand of a vmx instruction, as recorded on an
6798 * exit caused by such an instruction (run by a guest hypervisor).
6799 * On success, returns 0. When the operand is invalid, returns 1 and throws
6800 * #UD or #GP.
6801 */
6802static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6803 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006804 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006805{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006806 gva_t off;
6807 bool exn;
6808 struct kvm_segment s;
6809
Bandan Das19677e32014-05-06 02:19:15 -04006810 /*
6811 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6812 * Execution", on an exit, vmx_instruction_info holds most of the
6813 * addressing components of the operand. Only the displacement part
6814 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6815 * For how an actual address is calculated from all these components,
6816 * refer to Vol. 1, "Operand Addressing".
6817 */
6818 int scaling = vmx_instruction_info & 3;
6819 int addr_size = (vmx_instruction_info >> 7) & 7;
6820 bool is_reg = vmx_instruction_info & (1u << 10);
6821 int seg_reg = (vmx_instruction_info >> 15) & 7;
6822 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6823 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6824 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6825 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6826
6827 if (is_reg) {
6828 kvm_queue_exception(vcpu, UD_VECTOR);
6829 return 1;
6830 }
6831
6832 /* Addr = segment_base + offset */
6833 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006834 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006835 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006836 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006837 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006838 off += kvm_register_read(vcpu, index_reg)<<scaling;
6839 vmx_get_segment(vcpu, &s, seg_reg);
6840 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006841
6842 if (addr_size == 1) /* 32 bit */
6843 *ret &= 0xffffffff;
6844
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006845 /* Checks for #GP/#SS exceptions. */
6846 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006847 if (is_long_mode(vcpu)) {
6848 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6849 * non-canonical form. This is the only check on the memory
6850 * destination for long mode!
6851 */
6852 exn = is_noncanonical_address(*ret);
6853 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006854 /* Protected mode: apply checks for segment validity in the
6855 * following order:
6856 * - segment type check (#GP(0) may be thrown)
6857 * - usability check (#GP(0)/#SS(0))
6858 * - limit check (#GP(0)/#SS(0))
6859 */
6860 if (wr)
6861 /* #GP(0) if the destination operand is located in a
6862 * read-only data segment or any code segment.
6863 */
6864 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6865 else
6866 /* #GP(0) if the source operand is located in an
6867 * execute-only code segment
6868 */
6869 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006870 if (exn) {
6871 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6872 return 1;
6873 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006874 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6875 */
6876 exn = (s.unusable != 0);
6877 /* Protected mode: #GP(0)/#SS(0) if the memory
6878 * operand is outside the segment limit.
6879 */
6880 exn = exn || (off + sizeof(u64) > s.limit);
6881 }
6882 if (exn) {
6883 kvm_queue_exception_e(vcpu,
6884 seg_reg == VCPU_SREG_SS ?
6885 SS_VECTOR : GP_VECTOR,
6886 0);
6887 return 1;
6888 }
6889
Bandan Das19677e32014-05-06 02:19:15 -04006890 return 0;
6891}
6892
6893/*
Bandan Das3573e222014-05-06 02:19:16 -04006894 * This function performs the various checks including
6895 * - if it's 4KB aligned
6896 * - No bits beyond the physical address width are set
6897 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006898 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006899 */
Bandan Das4291b582014-05-06 02:19:18 -04006900static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6901 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006902{
6903 gva_t gva;
6904 gpa_t vmptr;
6905 struct x86_exception e;
6906 struct page *page;
6907 struct vcpu_vmx *vmx = to_vmx(vcpu);
6908 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6909
6910 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006911 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006912 return 1;
6913
6914 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6915 sizeof(vmptr), &e)) {
6916 kvm_inject_page_fault(vcpu, &e);
6917 return 1;
6918 }
6919
6920 switch (exit_reason) {
6921 case EXIT_REASON_VMON:
6922 /*
6923 * SDM 3: 24.11.5
6924 * The first 4 bytes of VMXON region contain the supported
6925 * VMCS revision identifier
6926 *
6927 * Note - IA32_VMX_BASIC[48] will never be 1
6928 * for the nested case;
6929 * which replaces physical address width with 32
6930 *
6931 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006932 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006933 nested_vmx_failInvalid(vcpu);
6934 skip_emulated_instruction(vcpu);
6935 return 1;
6936 }
6937
6938 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006939 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04006940 nested_vmx_failInvalid(vcpu);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006941 skip_emulated_instruction(vcpu);
6942 return 1;
6943 }
6944 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04006945 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006946 nested_release_page_clean(page);
6947 nested_vmx_failInvalid(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04006948 skip_emulated_instruction(vcpu);
6949 return 1;
6950 }
6951 kunmap(page);
Paolo Bonzini75465e72017-01-24 11:56:21 +01006952 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04006953 vmx->nested.vmxon_ptr = vmptr;
6954 break;
Bandan Das4291b582014-05-06 02:19:18 -04006955 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006956 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006957 nested_vmx_failValid(vcpu,
6958 VMXERR_VMCLEAR_INVALID_ADDRESS);
6959 skip_emulated_instruction(vcpu);
6960 return 1;
6961 }
Bandan Das3573e222014-05-06 02:19:16 -04006962
Bandan Das4291b582014-05-06 02:19:18 -04006963 if (vmptr == vmx->nested.vmxon_ptr) {
6964 nested_vmx_failValid(vcpu,
6965 VMXERR_VMCLEAR_VMXON_POINTER);
6966 skip_emulated_instruction(vcpu);
6967 return 1;
6968 }
6969 break;
6970 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006971 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006972 nested_vmx_failValid(vcpu,
6973 VMXERR_VMPTRLD_INVALID_ADDRESS);
6974 skip_emulated_instruction(vcpu);
6975 return 1;
6976 }
6977
6978 if (vmptr == vmx->nested.vmxon_ptr) {
6979 nested_vmx_failValid(vcpu,
6980 VMXERR_VMCLEAR_VMXON_POINTER);
6981 skip_emulated_instruction(vcpu);
6982 return 1;
6983 }
6984 break;
Bandan Das3573e222014-05-06 02:19:16 -04006985 default:
6986 return 1; /* shouldn't happen */
6987 }
6988
Bandan Das4291b582014-05-06 02:19:18 -04006989 if (vmpointer)
6990 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006991 return 0;
6992}
6993
6994/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006995 * Emulate the VMXON instruction.
6996 * Currently, we just remember that VMX is active, and do not save or even
6997 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6998 * do not currently need to store anything in that guest-allocated memory
6999 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7000 * argument is different from the VMXON pointer (which the spec says they do).
7001 */
7002static int handle_vmon(struct kvm_vcpu *vcpu)
7003{
7004 struct kvm_segment cs;
7005 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03007006 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007007 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7008 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007009
7010 /* The Intel VMX Instruction Reference lists a bunch of bits that
7011 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7012 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7013 * Otherwise, we should fail with #UD. We test these now:
7014 */
7015 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7016 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7017 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7018 kvm_queue_exception(vcpu, UD_VECTOR);
7019 return 1;
7020 }
7021
7022 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7023 if (is_long_mode(vcpu) && !cs.l) {
7024 kvm_queue_exception(vcpu, UD_VECTOR);
7025 return 1;
7026 }
7027
7028 if (vmx_get_cpl(vcpu)) {
7029 kvm_inject_gp(vcpu, 0);
7030 return 1;
7031 }
Bandan Das3573e222014-05-06 02:19:16 -04007032
Bandan Das4291b582014-05-06 02:19:18 -04007033 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007034 return 1;
7035
Abel Gordon145c28d2013-04-18 14:36:55 +03007036 if (vmx->nested.vmxon) {
7037 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7038 skip_emulated_instruction(vcpu);
7039 return 1;
7040 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007041
Haozhong Zhang3b840802016-06-22 14:59:54 +08007042 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007043 != VMXON_NEEDED_FEATURES) {
7044 kvm_inject_gp(vcpu, 0);
7045 return 1;
7046 }
7047
Radim Krčmářd048c092016-08-08 20:16:22 +02007048 if (cpu_has_vmx_msr_bitmap()) {
7049 vmx->nested.msr_bitmap =
7050 (unsigned long *)__get_free_page(GFP_KERNEL);
7051 if (!vmx->nested.msr_bitmap)
7052 goto out_msr_bitmap;
7053 }
7054
David Matlack4f2777b2016-07-13 17:16:37 -07007055 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7056 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007057 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007058
Abel Gordon8de48832013-04-18 14:37:25 +03007059 if (enable_shadow_vmcs) {
7060 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007061 if (!shadow_vmcs)
7062 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007063 /* mark vmcs as shadow */
7064 shadow_vmcs->revision_id |= (1u << 31);
7065 /* init shadow vmcs */
7066 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007067 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007068 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007069
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007070 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7071 vmx->nested.vmcs02_num = 0;
7072
Jan Kiszkaf4124502014-03-07 20:03:13 +01007073 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007074 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007075 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7076
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007077 vmx->nested.vmxon = true;
7078
7079 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007080 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007081 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007082
7083out_shadow_vmcs:
7084 kfree(vmx->nested.cached_vmcs12);
7085
7086out_cached_vmcs12:
7087 free_page((unsigned long)vmx->nested.msr_bitmap);
7088
7089out_msr_bitmap:
7090 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007091}
7092
7093/*
7094 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7095 * for running VMX instructions (except VMXON, whose prerequisites are
7096 * slightly different). It also specifies what exception to inject otherwise.
7097 */
7098static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7099{
7100 struct kvm_segment cs;
7101 struct vcpu_vmx *vmx = to_vmx(vcpu);
7102
7103 if (!vmx->nested.vmxon) {
7104 kvm_queue_exception(vcpu, UD_VECTOR);
7105 return 0;
7106 }
7107
7108 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7109 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7110 (is_long_mode(vcpu) && !cs.l)) {
7111 kvm_queue_exception(vcpu, UD_VECTOR);
7112 return 0;
7113 }
7114
7115 if (vmx_get_cpl(vcpu)) {
7116 kvm_inject_gp(vcpu, 0);
7117 return 0;
7118 }
7119
7120 return 1;
7121}
7122
Abel Gordone7953d72013-04-18 14:37:55 +03007123static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7124{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007125 if (vmx->nested.current_vmptr == -1ull)
7126 return;
7127
7128 /* current_vmptr and current_vmcs12 are always set/reset together */
7129 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7130 return;
7131
Abel Gordon012f83c2013-04-18 14:39:25 +03007132 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007133 /* copy to memory all shadowed fields in case
7134 they were modified */
7135 copy_shadow_to_vmcs12(vmx);
7136 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007137 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7138 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007139 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007140 }
Wincy Van705699a2015-02-03 23:58:17 +08007141 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007142
7143 /* Flush VMCS12 to guest memory */
7144 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7145 VMCS12_SIZE);
7146
Abel Gordone7953d72013-04-18 14:37:55 +03007147 kunmap(vmx->nested.current_vmcs12_page);
7148 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007149 vmx->nested.current_vmptr = -1ull;
7150 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007151}
7152
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007153/*
7154 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7155 * just stops using VMX.
7156 */
7157static void free_nested(struct vcpu_vmx *vmx)
7158{
7159 if (!vmx->nested.vmxon)
7160 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007161
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007162 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007163 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007164 nested_release_vmcs12(vmx);
Radim Krčmářd048c092016-08-08 20:16:22 +02007165 if (vmx->nested.msr_bitmap) {
7166 free_page((unsigned long)vmx->nested.msr_bitmap);
7167 vmx->nested.msr_bitmap = NULL;
7168 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007169 if (enable_shadow_vmcs) {
7170 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7171 free_vmcs(vmx->vmcs01.shadow_vmcs);
7172 vmx->vmcs01.shadow_vmcs = NULL;
7173 }
David Matlack4f2777b2016-07-13 17:16:37 -07007174 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007175 /* Unpin physical memory we referred to in current vmcs02 */
7176 if (vmx->nested.apic_access_page) {
7177 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007178 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007179 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007180 if (vmx->nested.virtual_apic_page) {
7181 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007182 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007183 }
Wincy Van705699a2015-02-03 23:58:17 +08007184 if (vmx->nested.pi_desc_page) {
7185 kunmap(vmx->nested.pi_desc_page);
7186 nested_release_page(vmx->nested.pi_desc_page);
7187 vmx->nested.pi_desc_page = NULL;
7188 vmx->nested.pi_desc = NULL;
7189 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007190
7191 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007192}
7193
7194/* Emulate the VMXOFF instruction */
7195static int handle_vmoff(struct kvm_vcpu *vcpu)
7196{
7197 if (!nested_vmx_check_permission(vcpu))
7198 return 1;
7199 free_nested(to_vmx(vcpu));
7200 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007201 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007202 return 1;
7203}
7204
Nadav Har'El27d6c862011-05-25 23:06:59 +03007205/* Emulate the VMCLEAR instruction */
7206static int handle_vmclear(struct kvm_vcpu *vcpu)
7207{
7208 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007209 gpa_t vmptr;
7210 struct vmcs12 *vmcs12;
7211 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007212
7213 if (!nested_vmx_check_permission(vcpu))
7214 return 1;
7215
Bandan Das4291b582014-05-06 02:19:18 -04007216 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007217 return 1;
7218
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007219 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007220 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007221
7222 page = nested_get_page(vcpu, vmptr);
7223 if (page == NULL) {
7224 /*
7225 * For accurate processor emulation, VMCLEAR beyond available
7226 * physical memory should do nothing at all. However, it is
7227 * possible that a nested vmx bug, not a guest hypervisor bug,
7228 * resulted in this case, so let's shut down before doing any
7229 * more damage:
7230 */
7231 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7232 return 1;
7233 }
7234 vmcs12 = kmap(page);
7235 vmcs12->launch_state = 0;
7236 kunmap(page);
7237 nested_release_page(page);
7238
7239 nested_free_vmcs02(vmx, vmptr);
7240
7241 skip_emulated_instruction(vcpu);
7242 nested_vmx_succeed(vcpu);
7243 return 1;
7244}
7245
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007246static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7247
7248/* Emulate the VMLAUNCH instruction */
7249static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7250{
7251 return nested_vmx_run(vcpu, true);
7252}
7253
7254/* Emulate the VMRESUME instruction */
7255static int handle_vmresume(struct kvm_vcpu *vcpu)
7256{
7257
7258 return nested_vmx_run(vcpu, false);
7259}
7260
Nadav Har'El49f705c2011-05-25 23:08:30 +03007261enum vmcs_field_type {
7262 VMCS_FIELD_TYPE_U16 = 0,
7263 VMCS_FIELD_TYPE_U64 = 1,
7264 VMCS_FIELD_TYPE_U32 = 2,
7265 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7266};
7267
7268static inline int vmcs_field_type(unsigned long field)
7269{
7270 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7271 return VMCS_FIELD_TYPE_U32;
7272 return (field >> 13) & 0x3 ;
7273}
7274
7275static inline int vmcs_field_readonly(unsigned long field)
7276{
7277 return (((field >> 10) & 0x3) == 1);
7278}
7279
7280/*
7281 * Read a vmcs12 field. Since these can have varying lengths and we return
7282 * one type, we chose the biggest type (u64) and zero-extend the return value
7283 * to that size. Note that the caller, handle_vmread, might need to use only
7284 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7285 * 64-bit fields are to be returned).
7286 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007287static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7288 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007289{
7290 short offset = vmcs_field_to_offset(field);
7291 char *p;
7292
7293 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007294 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007295
7296 p = ((char *)(get_vmcs12(vcpu))) + offset;
7297
7298 switch (vmcs_field_type(field)) {
7299 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7300 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007301 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007302 case VMCS_FIELD_TYPE_U16:
7303 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007304 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007305 case VMCS_FIELD_TYPE_U32:
7306 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007307 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007308 case VMCS_FIELD_TYPE_U64:
7309 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007310 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007311 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007312 WARN_ON(1);
7313 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007314 }
7315}
7316
Abel Gordon20b97fe2013-04-18 14:36:25 +03007317
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007318static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7319 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007320 short offset = vmcs_field_to_offset(field);
7321 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7322 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007323 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007324
7325 switch (vmcs_field_type(field)) {
7326 case VMCS_FIELD_TYPE_U16:
7327 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007328 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007329 case VMCS_FIELD_TYPE_U32:
7330 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007331 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007332 case VMCS_FIELD_TYPE_U64:
7333 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007334 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007335 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7336 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007337 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007338 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007339 WARN_ON(1);
7340 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007341 }
7342
7343}
7344
Abel Gordon16f5b902013-04-18 14:38:25 +03007345static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7346{
7347 int i;
7348 unsigned long field;
7349 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007350 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007351 const unsigned long *fields = shadow_read_write_fields;
7352 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007353
Jan Kiszka282da872014-10-08 18:05:39 +02007354 preempt_disable();
7355
Abel Gordon16f5b902013-04-18 14:38:25 +03007356 vmcs_load(shadow_vmcs);
7357
7358 for (i = 0; i < num_fields; i++) {
7359 field = fields[i];
7360 switch (vmcs_field_type(field)) {
7361 case VMCS_FIELD_TYPE_U16:
7362 field_value = vmcs_read16(field);
7363 break;
7364 case VMCS_FIELD_TYPE_U32:
7365 field_value = vmcs_read32(field);
7366 break;
7367 case VMCS_FIELD_TYPE_U64:
7368 field_value = vmcs_read64(field);
7369 break;
7370 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7371 field_value = vmcs_readl(field);
7372 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007373 default:
7374 WARN_ON(1);
7375 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007376 }
7377 vmcs12_write_any(&vmx->vcpu, field, field_value);
7378 }
7379
7380 vmcs_clear(shadow_vmcs);
7381 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007382
7383 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007384}
7385
Abel Gordonc3114422013-04-18 14:38:55 +03007386static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7387{
Mathias Krausec2bae892013-06-26 20:36:21 +02007388 const unsigned long *fields[] = {
7389 shadow_read_write_fields,
7390 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007391 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007392 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007393 max_shadow_read_write_fields,
7394 max_shadow_read_only_fields
7395 };
7396 int i, q;
7397 unsigned long field;
7398 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007399 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007400
7401 vmcs_load(shadow_vmcs);
7402
Mathias Krausec2bae892013-06-26 20:36:21 +02007403 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007404 for (i = 0; i < max_fields[q]; i++) {
7405 field = fields[q][i];
7406 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7407
7408 switch (vmcs_field_type(field)) {
7409 case VMCS_FIELD_TYPE_U16:
7410 vmcs_write16(field, (u16)field_value);
7411 break;
7412 case VMCS_FIELD_TYPE_U32:
7413 vmcs_write32(field, (u32)field_value);
7414 break;
7415 case VMCS_FIELD_TYPE_U64:
7416 vmcs_write64(field, (u64)field_value);
7417 break;
7418 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7419 vmcs_writel(field, (long)field_value);
7420 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007421 default:
7422 WARN_ON(1);
7423 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007424 }
7425 }
7426 }
7427
7428 vmcs_clear(shadow_vmcs);
7429 vmcs_load(vmx->loaded_vmcs->vmcs);
7430}
7431
Nadav Har'El49f705c2011-05-25 23:08:30 +03007432/*
7433 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7434 * used before) all generate the same failure when it is missing.
7435 */
7436static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7437{
7438 struct vcpu_vmx *vmx = to_vmx(vcpu);
7439 if (vmx->nested.current_vmptr == -1ull) {
7440 nested_vmx_failInvalid(vcpu);
7441 skip_emulated_instruction(vcpu);
7442 return 0;
7443 }
7444 return 1;
7445}
7446
7447static int handle_vmread(struct kvm_vcpu *vcpu)
7448{
7449 unsigned long field;
7450 u64 field_value;
7451 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7452 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7453 gva_t gva = 0;
7454
7455 if (!nested_vmx_check_permission(vcpu) ||
7456 !nested_vmx_check_vmcs12(vcpu))
7457 return 1;
7458
7459 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007460 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007461 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007462 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007463 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7464 skip_emulated_instruction(vcpu);
7465 return 1;
7466 }
7467 /*
7468 * Now copy part of this value to register or memory, as requested.
7469 * Note that the number of bits actually copied is 32 or 64 depending
7470 * on the guest's mode (32 or 64 bit), not on the given field's length.
7471 */
7472 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007473 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007474 field_value);
7475 } else {
7476 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007477 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007478 return 1;
7479 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7480 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7481 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7482 }
7483
7484 nested_vmx_succeed(vcpu);
7485 skip_emulated_instruction(vcpu);
7486 return 1;
7487}
7488
7489
7490static int handle_vmwrite(struct kvm_vcpu *vcpu)
7491{
7492 unsigned long field;
7493 gva_t gva;
7494 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7495 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007496 /* The value to write might be 32 or 64 bits, depending on L1's long
7497 * mode, and eventually we need to write that into a field of several
7498 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007499 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007500 * bits into the vmcs12 field.
7501 */
7502 u64 field_value = 0;
7503 struct x86_exception e;
7504
7505 if (!nested_vmx_check_permission(vcpu) ||
7506 !nested_vmx_check_vmcs12(vcpu))
7507 return 1;
7508
7509 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007510 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007511 (((vmx_instruction_info) >> 3) & 0xf));
7512 else {
7513 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007514 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007515 return 1;
7516 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007517 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007518 kvm_inject_page_fault(vcpu, &e);
7519 return 1;
7520 }
7521 }
7522
7523
Nadav Amit27e6fb52014-06-18 17:19:26 +03007524 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007525 if (vmcs_field_readonly(field)) {
7526 nested_vmx_failValid(vcpu,
7527 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7528 skip_emulated_instruction(vcpu);
7529 return 1;
7530 }
7531
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007532 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007533 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7534 skip_emulated_instruction(vcpu);
7535 return 1;
7536 }
7537
7538 nested_vmx_succeed(vcpu);
7539 skip_emulated_instruction(vcpu);
7540 return 1;
7541}
7542
Nadav Har'El63846662011-05-25 23:07:29 +03007543/* Emulate the VMPTRLD instruction */
7544static int handle_vmptrld(struct kvm_vcpu *vcpu)
7545{
7546 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007547 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007548
7549 if (!nested_vmx_check_permission(vcpu))
7550 return 1;
7551
Bandan Das4291b582014-05-06 02:19:18 -04007552 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007553 return 1;
7554
Nadav Har'El63846662011-05-25 23:07:29 +03007555 if (vmx->nested.current_vmptr != vmptr) {
7556 struct vmcs12 *new_vmcs12;
7557 struct page *page;
7558 page = nested_get_page(vcpu, vmptr);
7559 if (page == NULL) {
7560 nested_vmx_failInvalid(vcpu);
7561 skip_emulated_instruction(vcpu);
7562 return 1;
7563 }
7564 new_vmcs12 = kmap(page);
7565 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7566 kunmap(page);
7567 nested_release_page_clean(page);
7568 nested_vmx_failValid(vcpu,
7569 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7570 skip_emulated_instruction(vcpu);
7571 return 1;
7572 }
Nadav Har'El63846662011-05-25 23:07:29 +03007573
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007574 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007575 vmx->nested.current_vmptr = vmptr;
7576 vmx->nested.current_vmcs12 = new_vmcs12;
7577 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007578 /*
7579 * Load VMCS12 from guest memory since it is not already
7580 * cached.
7581 */
7582 memcpy(vmx->nested.cached_vmcs12,
7583 vmx->nested.current_vmcs12, VMCS12_SIZE);
7584
Abel Gordon012f83c2013-04-18 14:39:25 +03007585 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007586 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7587 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007588 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007589 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007590 vmx->nested.sync_shadow_vmcs = true;
7591 }
Nadav Har'El63846662011-05-25 23:07:29 +03007592 }
7593
7594 nested_vmx_succeed(vcpu);
7595 skip_emulated_instruction(vcpu);
7596 return 1;
7597}
7598
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007599/* Emulate the VMPTRST instruction */
7600static int handle_vmptrst(struct kvm_vcpu *vcpu)
7601{
7602 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7603 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7604 gva_t vmcs_gva;
7605 struct x86_exception e;
7606
7607 if (!nested_vmx_check_permission(vcpu))
7608 return 1;
7609
7610 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007611 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007612 return 1;
7613 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7614 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7615 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7616 sizeof(u64), &e)) {
7617 kvm_inject_page_fault(vcpu, &e);
7618 return 1;
7619 }
7620 nested_vmx_succeed(vcpu);
7621 skip_emulated_instruction(vcpu);
7622 return 1;
7623}
7624
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007625/* Emulate the INVEPT instruction */
7626static int handle_invept(struct kvm_vcpu *vcpu)
7627{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007628 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007629 u32 vmx_instruction_info, types;
7630 unsigned long type;
7631 gva_t gva;
7632 struct x86_exception e;
7633 struct {
7634 u64 eptp, gpa;
7635 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007636
Wincy Vanb9c237b2015-02-03 23:56:30 +08007637 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7638 SECONDARY_EXEC_ENABLE_EPT) ||
7639 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007640 kvm_queue_exception(vcpu, UD_VECTOR);
7641 return 1;
7642 }
7643
7644 if (!nested_vmx_check_permission(vcpu))
7645 return 1;
7646
7647 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7648 kvm_queue_exception(vcpu, UD_VECTOR);
7649 return 1;
7650 }
7651
7652 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007653 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007654
Wincy Vanb9c237b2015-02-03 23:56:30 +08007655 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007656
Jim Mattson85c856b2016-10-26 08:38:38 -07007657 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007658 nested_vmx_failValid(vcpu,
7659 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007660 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007661 return 1;
7662 }
7663
7664 /* According to the Intel VMX instruction reference, the memory
7665 * operand is read even if it isn't needed (e.g., for type==global)
7666 */
7667 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007668 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007669 return 1;
7670 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7671 sizeof(operand), &e)) {
7672 kvm_inject_page_fault(vcpu, &e);
7673 return 1;
7674 }
7675
7676 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007677 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007678 /*
7679 * TODO: track mappings and invalidate
7680 * single context requests appropriately
7681 */
7682 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007683 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007684 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007685 nested_vmx_succeed(vcpu);
7686 break;
7687 default:
7688 BUG_ON(1);
7689 break;
7690 }
7691
7692 skip_emulated_instruction(vcpu);
7693 return 1;
7694}
7695
Petr Matouseka642fc32014-09-23 20:22:30 +02007696static int handle_invvpid(struct kvm_vcpu *vcpu)
7697{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007698 struct vcpu_vmx *vmx = to_vmx(vcpu);
7699 u32 vmx_instruction_info;
7700 unsigned long type, types;
7701 gva_t gva;
7702 struct x86_exception e;
7703 int vpid;
7704
7705 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7706 SECONDARY_EXEC_ENABLE_VPID) ||
7707 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7708 kvm_queue_exception(vcpu, UD_VECTOR);
7709 return 1;
7710 }
7711
7712 if (!nested_vmx_check_permission(vcpu))
7713 return 1;
7714
7715 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7716 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7717
7718 types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7;
7719
Jim Mattson85c856b2016-10-26 08:38:38 -07007720 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007721 nested_vmx_failValid(vcpu,
7722 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007723 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007724 return 1;
7725 }
7726
7727 /* according to the intel vmx instruction reference, the memory
7728 * operand is read even if it isn't needed (e.g., for type==global)
7729 */
7730 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7731 vmx_instruction_info, false, &gva))
7732 return 1;
7733 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7734 sizeof(u32), &e)) {
7735 kvm_inject_page_fault(vcpu, &e);
7736 return 1;
7737 }
7738
7739 switch (type) {
Paolo Bonzinief697a72016-03-18 16:58:38 +01007740 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
7741 /*
7742 * Old versions of KVM use the single-context version so we
7743 * have to support it; just treat it the same as all-context.
7744 */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007745 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li5c614b32015-10-13 09:18:36 -07007746 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007747 nested_vmx_succeed(vcpu);
7748 break;
7749 default:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007750 /* Trap individual address invalidation invvpid calls */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007751 BUG_ON(1);
7752 break;
7753 }
7754
7755 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007756 return 1;
7757}
7758
Kai Huang843e4332015-01-28 10:54:28 +08007759static int handle_pml_full(struct kvm_vcpu *vcpu)
7760{
7761 unsigned long exit_qualification;
7762
7763 trace_kvm_pml_full(vcpu->vcpu_id);
7764
7765 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7766
7767 /*
7768 * PML buffer FULL happened while executing iret from NMI,
7769 * "blocked by NMI" bit has to be set before next VM entry.
7770 */
7771 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7772 cpu_has_virtual_nmis() &&
7773 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7774 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7775 GUEST_INTR_STATE_NMI);
7776
7777 /*
7778 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7779 * here.., and there's no userspace involvement needed for PML.
7780 */
7781 return 1;
7782}
7783
Yunhong Jiang64672c92016-06-13 14:19:59 -07007784static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7785{
7786 kvm_lapic_expired_hv_timer(vcpu);
7787 return 1;
7788}
7789
Nadav Har'El0140cae2011-05-25 23:06:28 +03007790/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007791 * The exit handlers return 1 if the exit was handled fully and guest execution
7792 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7793 * to be done to userspace and return 0.
7794 */
Mathias Krause772e0312012-08-30 01:30:19 +02007795static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007796 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7797 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007798 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007799 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007800 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007801 [EXIT_REASON_CR_ACCESS] = handle_cr,
7802 [EXIT_REASON_DR_ACCESS] = handle_dr,
7803 [EXIT_REASON_CPUID] = handle_cpuid,
7804 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7805 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7806 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7807 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007808 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007809 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007810 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007811 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007812 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007813 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007814 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007815 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007816 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007817 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007818 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007819 [EXIT_REASON_VMOFF] = handle_vmoff,
7820 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007821 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7822 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007823 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007824 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007825 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007826 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007827 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007828 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007829 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7830 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007831 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007832 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007833 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007834 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007835 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007836 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007837 [EXIT_REASON_XSAVES] = handle_xsaves,
7838 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007839 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007840 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007841};
7842
7843static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007844 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007845
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007846static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7847 struct vmcs12 *vmcs12)
7848{
7849 unsigned long exit_qualification;
7850 gpa_t bitmap, last_bitmap;
7851 unsigned int port;
7852 int size;
7853 u8 b;
7854
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007855 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007856 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007857
7858 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7859
7860 port = exit_qualification >> 16;
7861 size = (exit_qualification & 7) + 1;
7862
7863 last_bitmap = (gpa_t)-1;
7864 b = -1;
7865
7866 while (size > 0) {
7867 if (port < 0x8000)
7868 bitmap = vmcs12->io_bitmap_a;
7869 else if (port < 0x10000)
7870 bitmap = vmcs12->io_bitmap_b;
7871 else
Joe Perches1d804d02015-03-30 16:46:09 -07007872 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007873 bitmap += (port & 0x7fff) / 8;
7874
7875 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007876 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007877 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007878 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007879 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007880
7881 port++;
7882 size--;
7883 last_bitmap = bitmap;
7884 }
7885
Joe Perches1d804d02015-03-30 16:46:09 -07007886 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007887}
7888
Nadav Har'El644d7112011-05-25 23:12:35 +03007889/*
7890 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7891 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7892 * disinterest in the current event (read or write a specific MSR) by using an
7893 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7894 */
7895static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7896 struct vmcs12 *vmcs12, u32 exit_reason)
7897{
7898 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7899 gpa_t bitmap;
7900
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007901 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007902 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007903
7904 /*
7905 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7906 * for the four combinations of read/write and low/high MSR numbers.
7907 * First we need to figure out which of the four to use:
7908 */
7909 bitmap = vmcs12->msr_bitmap;
7910 if (exit_reason == EXIT_REASON_MSR_WRITE)
7911 bitmap += 2048;
7912 if (msr_index >= 0xc0000000) {
7913 msr_index -= 0xc0000000;
7914 bitmap += 1024;
7915 }
7916
7917 /* Then read the msr_index'th bit from this bitmap: */
7918 if (msr_index < 1024*8) {
7919 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007920 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007921 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007922 return 1 & (b >> (msr_index & 7));
7923 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007924 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007925}
7926
7927/*
7928 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7929 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7930 * intercept (via guest_host_mask etc.) the current event.
7931 */
7932static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7933 struct vmcs12 *vmcs12)
7934{
7935 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7936 int cr = exit_qualification & 15;
7937 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007938 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007939
7940 switch ((exit_qualification >> 4) & 3) {
7941 case 0: /* mov to cr */
7942 switch (cr) {
7943 case 0:
7944 if (vmcs12->cr0_guest_host_mask &
7945 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007946 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007947 break;
7948 case 3:
7949 if ((vmcs12->cr3_target_count >= 1 &&
7950 vmcs12->cr3_target_value0 == val) ||
7951 (vmcs12->cr3_target_count >= 2 &&
7952 vmcs12->cr3_target_value1 == val) ||
7953 (vmcs12->cr3_target_count >= 3 &&
7954 vmcs12->cr3_target_value2 == val) ||
7955 (vmcs12->cr3_target_count >= 4 &&
7956 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007957 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007958 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007959 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007960 break;
7961 case 4:
7962 if (vmcs12->cr4_guest_host_mask &
7963 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007964 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007965 break;
7966 case 8:
7967 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007968 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007969 break;
7970 }
7971 break;
7972 case 2: /* clts */
7973 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7974 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007975 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007976 break;
7977 case 1: /* mov from cr */
7978 switch (cr) {
7979 case 3:
7980 if (vmcs12->cpu_based_vm_exec_control &
7981 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007982 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007983 break;
7984 case 8:
7985 if (vmcs12->cpu_based_vm_exec_control &
7986 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007987 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007988 break;
7989 }
7990 break;
7991 case 3: /* lmsw */
7992 /*
7993 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7994 * cr0. Other attempted changes are ignored, with no exit.
7995 */
7996 if (vmcs12->cr0_guest_host_mask & 0xe &
7997 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007998 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007999 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
8000 !(vmcs12->cr0_read_shadow & 0x1) &&
8001 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07008002 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008003 break;
8004 }
Joe Perches1d804d02015-03-30 16:46:09 -07008005 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008006}
8007
8008/*
8009 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8010 * should handle it ourselves in L0 (and then continue L2). Only call this
8011 * when in is_guest_mode (L2).
8012 */
8013static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8014{
Nadav Har'El644d7112011-05-25 23:12:35 +03008015 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8016 struct vcpu_vmx *vmx = to_vmx(vcpu);
8017 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008018 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008019
Jan Kiszka542060e2014-01-04 18:47:21 +01008020 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8021 vmcs_readl(EXIT_QUALIFICATION),
8022 vmx->idt_vectoring_info,
8023 intr_info,
8024 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8025 KVM_ISA_VMX);
8026
Nadav Har'El644d7112011-05-25 23:12:35 +03008027 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008028 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008029
8030 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008031 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8032 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008033 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008034 }
8035
8036 switch (exit_reason) {
8037 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattson3f618a02016-12-12 11:01:37 -08008038 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008039 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008040 else if (is_page_fault(intr_info))
8041 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008042 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008043 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008044 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008045 else if (is_debug(intr_info) &&
8046 vcpu->guest_debug &
8047 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8048 return false;
8049 else if (is_breakpoint(intr_info) &&
8050 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8051 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008052 return vmcs12->exception_bitmap &
8053 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8054 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008055 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008056 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008057 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008058 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008059 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008060 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008061 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008062 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008063 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008064 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008065 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008066 case EXIT_REASON_HLT:
8067 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8068 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008069 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008070 case EXIT_REASON_INVLPG:
8071 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8072 case EXIT_REASON_RDPMC:
8073 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008074 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008075 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8076 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8077 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8078 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8079 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8080 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008081 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008082 /*
8083 * VMX instructions trap unconditionally. This allows L1 to
8084 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8085 */
Joe Perches1d804d02015-03-30 16:46:09 -07008086 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008087 case EXIT_REASON_CR_ACCESS:
8088 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8089 case EXIT_REASON_DR_ACCESS:
8090 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8091 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008092 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008093 case EXIT_REASON_MSR_READ:
8094 case EXIT_REASON_MSR_WRITE:
8095 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8096 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008097 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008098 case EXIT_REASON_MWAIT_INSTRUCTION:
8099 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008100 case EXIT_REASON_MONITOR_TRAP_FLAG:
8101 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008102 case EXIT_REASON_MONITOR_INSTRUCTION:
8103 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8104 case EXIT_REASON_PAUSE_INSTRUCTION:
8105 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8106 nested_cpu_has2(vmcs12,
8107 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8108 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008109 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008110 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008111 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008112 case EXIT_REASON_APIC_ACCESS:
8113 return nested_cpu_has2(vmcs12,
8114 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008115 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008116 case EXIT_REASON_EOI_INDUCED:
8117 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008118 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008119 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008120 /*
8121 * L0 always deals with the EPT violation. If nested EPT is
8122 * used, and the nested mmu code discovers that the address is
8123 * missing in the guest EPT table (EPT12), the EPT violation
8124 * will be injected with nested_ept_inject_page_fault()
8125 */
Joe Perches1d804d02015-03-30 16:46:09 -07008126 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008127 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008128 /*
8129 * L2 never uses directly L1's EPT, but rather L0's own EPT
8130 * table (shadow on EPT) or a merged EPT table that L0 built
8131 * (EPT on EPT). So any problems with the structure of the
8132 * table is L0's fault.
8133 */
Joe Perches1d804d02015-03-30 16:46:09 -07008134 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008135 case EXIT_REASON_WBINVD:
8136 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8137 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008138 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008139 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8140 /*
8141 * This should never happen, since it is not possible to
8142 * set XSS to a non-zero value---neither in L1 nor in L2.
8143 * If if it were, XSS would have to be checked against
8144 * the XSS exit bitmap in vmcs12.
8145 */
8146 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008147 case EXIT_REASON_PREEMPTION_TIMER:
8148 return false;
Ladi Prosekd0ee3632017-03-31 10:19:26 +02008149 case EXIT_REASON_PML_FULL:
8150 /* We don't expose PML support to L1. */
8151 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008152 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008153 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008154 }
8155}
8156
Avi Kivity586f9602010-11-18 13:09:54 +02008157static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8158{
8159 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8160 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8161}
8162
Kai Huanga3eaa862015-11-04 13:46:05 +08008163static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008164{
Kai Huanga3eaa862015-11-04 13:46:05 +08008165 if (vmx->pml_pg) {
8166 __free_page(vmx->pml_pg);
8167 vmx->pml_pg = NULL;
8168 }
Kai Huang843e4332015-01-28 10:54:28 +08008169}
8170
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008171static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008172{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008173 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008174 u64 *pml_buf;
8175 u16 pml_idx;
8176
8177 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8178
8179 /* Do nothing if PML buffer is empty */
8180 if (pml_idx == (PML_ENTITY_NUM - 1))
8181 return;
8182
8183 /* PML index always points to next available PML buffer entity */
8184 if (pml_idx >= PML_ENTITY_NUM)
8185 pml_idx = 0;
8186 else
8187 pml_idx++;
8188
8189 pml_buf = page_address(vmx->pml_pg);
8190 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8191 u64 gpa;
8192
8193 gpa = pml_buf[pml_idx];
8194 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008195 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008196 }
8197
8198 /* reset PML index */
8199 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8200}
8201
8202/*
8203 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8204 * Called before reporting dirty_bitmap to userspace.
8205 */
8206static void kvm_flush_pml_buffers(struct kvm *kvm)
8207{
8208 int i;
8209 struct kvm_vcpu *vcpu;
8210 /*
8211 * We only need to kick vcpu out of guest mode here, as PML buffer
8212 * is flushed at beginning of all VMEXITs, and it's obvious that only
8213 * vcpus running in guest are possible to have unflushed GPAs in PML
8214 * buffer.
8215 */
8216 kvm_for_each_vcpu(i, vcpu, kvm)
8217 kvm_vcpu_kick(vcpu);
8218}
8219
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008220static void vmx_dump_sel(char *name, uint32_t sel)
8221{
8222 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng7c3bab12017-02-21 03:50:01 -05008223 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008224 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8225 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8226 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8227}
8228
8229static void vmx_dump_dtsel(char *name, uint32_t limit)
8230{
8231 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8232 name, vmcs_read32(limit),
8233 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8234}
8235
8236static void dump_vmcs(void)
8237{
8238 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8239 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8240 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8241 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8242 u32 secondary_exec_control = 0;
8243 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008244 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008245 int i, n;
8246
8247 if (cpu_has_secondary_exec_ctrls())
8248 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8249
8250 pr_err("*** Guest State ***\n");
8251 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8252 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8253 vmcs_readl(CR0_GUEST_HOST_MASK));
8254 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8255 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8256 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8257 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8258 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8259 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008260 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8261 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8262 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8263 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008264 }
8265 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8266 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8267 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8268 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8269 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8270 vmcs_readl(GUEST_SYSENTER_ESP),
8271 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8272 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8273 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8274 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8275 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8276 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8277 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8278 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8279 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8280 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8281 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8282 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8283 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008284 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8285 efer, vmcs_read64(GUEST_IA32_PAT));
8286 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8287 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008288 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8289 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008290 pr_err("PerfGlobCtl = 0x%016llx\n",
8291 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008292 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008293 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008294 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8295 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8296 vmcs_read32(GUEST_ACTIVITY_STATE));
8297 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8298 pr_err("InterruptStatus = %04x\n",
8299 vmcs_read16(GUEST_INTR_STATUS));
8300
8301 pr_err("*** Host State ***\n");
8302 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8303 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8304 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8305 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8306 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8307 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8308 vmcs_read16(HOST_TR_SELECTOR));
8309 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8310 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8311 vmcs_readl(HOST_TR_BASE));
8312 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8313 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8314 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8315 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8316 vmcs_readl(HOST_CR4));
8317 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8318 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8319 vmcs_read32(HOST_IA32_SYSENTER_CS),
8320 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8321 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008322 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8323 vmcs_read64(HOST_IA32_EFER),
8324 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008325 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008326 pr_err("PerfGlobCtl = 0x%016llx\n",
8327 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008328
8329 pr_err("*** Control State ***\n");
8330 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8331 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8332 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8333 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8334 vmcs_read32(EXCEPTION_BITMAP),
8335 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8336 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8337 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8338 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8339 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8340 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8341 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8342 vmcs_read32(VM_EXIT_INTR_INFO),
8343 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8344 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8345 pr_err(" reason=%08x qualification=%016lx\n",
8346 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8347 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8348 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8349 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008350 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008351 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008352 pr_err("TSC Multiplier = 0x%016llx\n",
8353 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008354 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8355 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8356 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8357 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8358 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008359 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008360 n = vmcs_read32(CR3_TARGET_COUNT);
8361 for (i = 0; i + 1 < n; i += 4)
8362 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8363 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8364 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8365 if (i < n)
8366 pr_err("CR3 target%u=%016lx\n",
8367 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8368 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8369 pr_err("PLE Gap=%08x Window=%08x\n",
8370 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8371 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8372 pr_err("Virtual processor ID = 0x%04x\n",
8373 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8374}
8375
Avi Kivity6aa8b732006-12-10 02:21:36 -08008376/*
8377 * The guest has exited. See if we can fix it or if we need userspace
8378 * assistance.
8379 */
Avi Kivity851ba692009-08-24 11:10:17 +03008380static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008381{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008382 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008383 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008384 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008385
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008386 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8387
Kai Huang843e4332015-01-28 10:54:28 +08008388 /*
8389 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8390 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8391 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8392 * mode as if vcpus is in root mode, the PML buffer must has been
8393 * flushed already.
8394 */
8395 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008396 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008397
Mohammed Gamal80ced182009-09-01 12:48:18 +02008398 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008399 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008400 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008401
Nadav Har'El644d7112011-05-25 23:12:35 +03008402 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008403 nested_vmx_vmexit(vcpu, exit_reason,
8404 vmcs_read32(VM_EXIT_INTR_INFO),
8405 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008406 return 1;
8407 }
8408
Mohammed Gamal51207022010-05-31 22:40:54 +03008409 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008410 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008411 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8412 vcpu->run->fail_entry.hardware_entry_failure_reason
8413 = exit_reason;
8414 return 0;
8415 }
8416
Avi Kivity29bd8a72007-09-10 17:27:03 +03008417 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008418 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8419 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008420 = vmcs_read32(VM_INSTRUCTION_ERROR);
8421 return 0;
8422 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008423
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008424 /*
8425 * Note:
8426 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8427 * delivery event since it indicates guest is accessing MMIO.
8428 * The vm-exit can be triggered again after return to guest that
8429 * will cause infinite loop.
8430 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008431 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008432 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008433 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008434 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008435 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8436 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8437 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8438 vcpu->run->internal.ndata = 2;
8439 vcpu->run->internal.data[0] = vectoring_info;
8440 vcpu->run->internal.data[1] = exit_reason;
8441 return 0;
8442 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008443
Nadav Har'El644d7112011-05-25 23:12:35 +03008444 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8445 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008446 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008447 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008448 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008449 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008450 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008451 /*
8452 * This CPU don't support us in finding the end of an
8453 * NMI-blocked window if the guest runs with IRQs
8454 * disabled. So we pull the trigger after 1 s of
8455 * futile waiting, but inform the user about this.
8456 */
8457 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8458 "state on VCPU %d after 1 s timeout\n",
8459 __func__, vcpu->vcpu_id);
8460 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008461 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008462 }
8463
Avi Kivity6aa8b732006-12-10 02:21:36 -08008464 if (exit_reason < kvm_vmx_max_exit_handlers
8465 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008466 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008467 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008468 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8469 kvm_queue_exception(vcpu, UD_VECTOR);
8470 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008471 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008472}
8473
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008474static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008475{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008476 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8477
8478 if (is_guest_mode(vcpu) &&
8479 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8480 return;
8481
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008482 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008483 vmcs_write32(TPR_THRESHOLD, 0);
8484 return;
8485 }
8486
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008487 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008488}
8489
Yang Zhang8d146952013-01-25 10:18:50 +08008490static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8491{
8492 u32 sec_exec_control;
8493
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008494 /* Postpone execution until vmcs01 is the current VMCS. */
8495 if (is_guest_mode(vcpu)) {
8496 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8497 return;
8498 }
8499
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008500 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008501 return;
8502
Paolo Bonzini35754c92015-07-29 12:05:37 +02008503 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008504 return;
8505
8506 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8507
8508 if (set) {
8509 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8510 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8511 } else {
8512 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8513 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8514 }
8515 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8516
8517 vmx_set_msr_bitmap(vcpu);
8518}
8519
Tang Chen38b99172014-09-24 15:57:54 +08008520static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8521{
8522 struct vcpu_vmx *vmx = to_vmx(vcpu);
8523
8524 /*
8525 * Currently we do not handle the nested case where L2 has an
8526 * APIC access page of its own; that page is still pinned.
8527 * Hence, we skip the case where the VCPU is in guest mode _and_
8528 * L1 prepared an APIC access page for L2.
8529 *
8530 * For the case where L1 and L2 share the same APIC access page
8531 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8532 * in the vmcs12), this function will only update either the vmcs01
8533 * or the vmcs02. If the former, the vmcs02 will be updated by
8534 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8535 * the next L2->L1 exit.
8536 */
8537 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008538 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Tang Chen38b99172014-09-24 15:57:54 +08008539 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
8540 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8541}
8542
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008543static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008544{
8545 u16 status;
8546 u8 old;
8547
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008548 if (max_isr == -1)
8549 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008550
8551 status = vmcs_read16(GUEST_INTR_STATUS);
8552 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008553 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008554 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008555 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008556 vmcs_write16(GUEST_INTR_STATUS, status);
8557 }
8558}
8559
8560static void vmx_set_rvi(int vector)
8561{
8562 u16 status;
8563 u8 old;
8564
Wei Wang4114c272014-11-05 10:53:43 +08008565 if (vector == -1)
8566 vector = 0;
8567
Yang Zhangc7c9c562013-01-25 10:18:51 +08008568 status = vmcs_read16(GUEST_INTR_STATUS);
8569 old = (u8)status & 0xff;
8570 if ((u8)vector != old) {
8571 status &= ~0xff;
8572 status |= (u8)vector;
8573 vmcs_write16(GUEST_INTR_STATUS, status);
8574 }
8575}
8576
8577static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8578{
Wanpeng Li963fee12014-07-17 19:03:00 +08008579 if (!is_guest_mode(vcpu)) {
8580 vmx_set_rvi(max_irr);
8581 return;
8582 }
8583
Wei Wang4114c272014-11-05 10:53:43 +08008584 if (max_irr == -1)
8585 return;
8586
Wanpeng Li963fee12014-07-17 19:03:00 +08008587 /*
Wei Wang4114c272014-11-05 10:53:43 +08008588 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8589 * handles it.
8590 */
8591 if (nested_exit_on_intr(vcpu))
8592 return;
8593
8594 /*
8595 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008596 * is run without virtual interrupt delivery.
8597 */
8598 if (!kvm_event_needs_reinjection(vcpu) &&
8599 vmx_interrupt_allowed(vcpu)) {
8600 kvm_queue_interrupt(vcpu, max_irr, false);
8601 vmx_inject_irq(vcpu);
8602 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008603}
8604
Andrey Smetanin63086302015-11-10 15:36:32 +03008605static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008606{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008607 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008608 return;
8609
Yang Zhangc7c9c562013-01-25 10:18:51 +08008610 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8611 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8612 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8613 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8614}
8615
Avi Kivity51aa01d2010-07-20 14:31:20 +03008616static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008617{
Avi Kivity00eba012011-03-07 17:24:54 +02008618 u32 exit_intr_info;
8619
8620 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8621 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8622 return;
8623
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008624 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008625 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008626
8627 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008628 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008629 kvm_machine_check();
8630
Gleb Natapov20f65982009-05-11 13:35:55 +03008631 /* We need to handle NMIs before interrupts are enabled */
Jim Mattson3f618a02016-12-12 11:01:37 -08008632 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008633 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008634 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008635 kvm_after_handle_nmi(&vmx->vcpu);
8636 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008637}
Gleb Natapov20f65982009-05-11 13:35:55 +03008638
Yang Zhanga547c6d2013-04-11 19:25:10 +08008639static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8640{
8641 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008642 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008643
8644 /*
8645 * If external interrupt exists, IF bit is set in rflags/eflags on the
8646 * interrupt stack frame, and interrupt will be enabled on a return
8647 * from interrupt handler.
8648 */
8649 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8650 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8651 unsigned int vector;
8652 unsigned long entry;
8653 gate_desc *desc;
8654 struct vcpu_vmx *vmx = to_vmx(vcpu);
8655#ifdef CONFIG_X86_64
8656 unsigned long tmp;
8657#endif
8658
8659 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8660 desc = (gate_desc *)vmx->host_idt_base + vector;
8661 entry = gate_offset(*desc);
8662 asm volatile(
8663#ifdef CONFIG_X86_64
8664 "mov %%" _ASM_SP ", %[sp]\n\t"
8665 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8666 "push $%c[ss]\n\t"
8667 "push %[sp]\n\t"
8668#endif
8669 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008670 __ASM_SIZE(push) " $%c[cs]\n\t"
8671 "call *%[entry]\n\t"
8672 :
8673#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008674 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008675#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008676 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008677 :
8678 [entry]"r"(entry),
8679 [ss]"i"(__KERNEL_DS),
8680 [cs]"i"(__KERNEL_CS)
8681 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008682 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008683}
8684
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008685static bool vmx_has_high_real_mode_segbase(void)
8686{
8687 return enable_unrestricted_guest || emulate_invalid_guest_state;
8688}
8689
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008690static bool vmx_mpx_supported(void)
8691{
8692 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8693 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8694}
8695
Wanpeng Li55412b22014-12-02 19:21:30 +08008696static bool vmx_xsaves_supported(void)
8697{
8698 return vmcs_config.cpu_based_2nd_exec_ctrl &
8699 SECONDARY_EXEC_XSAVES;
8700}
8701
Avi Kivity51aa01d2010-07-20 14:31:20 +03008702static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8703{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008704 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008705 bool unblock_nmi;
8706 u8 vector;
8707 bool idtv_info_valid;
8708
8709 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008710
Avi Kivitycf393f72008-07-01 16:20:21 +03008711 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008712 if (vmx->nmi_known_unmasked)
8713 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008714 /*
8715 * Can't use vmx->exit_intr_info since we're not sure what
8716 * the exit reason is.
8717 */
8718 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008719 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8720 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8721 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008722 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008723 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8724 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008725 * SDM 3: 23.2.2 (September 2008)
8726 * Bit 12 is undefined in any of the following cases:
8727 * If the VM exit sets the valid bit in the IDT-vectoring
8728 * information field.
8729 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008730 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008731 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8732 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008733 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8734 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008735 else
8736 vmx->nmi_known_unmasked =
8737 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8738 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008739 } else if (unlikely(vmx->soft_vnmi_blocked))
8740 vmx->vnmi_blocked_time +=
8741 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008742}
8743
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008744static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008745 u32 idt_vectoring_info,
8746 int instr_len_field,
8747 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008748{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008749 u8 vector;
8750 int type;
8751 bool idtv_info_valid;
8752
8753 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008754
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008755 vcpu->arch.nmi_injected = false;
8756 kvm_clear_exception_queue(vcpu);
8757 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008758
8759 if (!idtv_info_valid)
8760 return;
8761
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008762 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008763
Avi Kivity668f6122008-07-02 09:28:55 +03008764 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8765 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008766
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008767 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008768 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008769 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008770 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008771 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008772 * Clear bit "block by NMI" before VM entry if a NMI
8773 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008774 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008775 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008776 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008777 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008778 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008779 /* fall through */
8780 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008781 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008782 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008783 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008784 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008785 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008786 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008787 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008788 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008789 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008790 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008791 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008792 break;
8793 default:
8794 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008795 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008796}
8797
Avi Kivity83422e12010-07-20 14:43:23 +03008798static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8799{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008800 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008801 VM_EXIT_INSTRUCTION_LEN,
8802 IDT_VECTORING_ERROR_CODE);
8803}
8804
Avi Kivityb463a6f2010-07-20 15:06:17 +03008805static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8806{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008807 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008808 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8809 VM_ENTRY_INSTRUCTION_LEN,
8810 VM_ENTRY_EXCEPTION_ERROR_CODE);
8811
8812 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8813}
8814
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008815static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8816{
8817 int i, nr_msrs;
8818 struct perf_guest_switch_msr *msrs;
8819
8820 msrs = perf_guest_get_msrs(&nr_msrs);
8821
8822 if (!msrs)
8823 return;
8824
8825 for (i = 0; i < nr_msrs; i++)
8826 if (msrs[i].host == msrs[i].guest)
8827 clear_atomic_switch_msr(vmx, msrs[i].msr);
8828 else
8829 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8830 msrs[i].host);
8831}
8832
Yunhong Jiang64672c92016-06-13 14:19:59 -07008833void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8834{
8835 struct vcpu_vmx *vmx = to_vmx(vcpu);
8836 u64 tscl;
8837 u32 delta_tsc;
8838
8839 if (vmx->hv_deadline_tsc == -1)
8840 return;
8841
8842 tscl = rdtsc();
8843 if (vmx->hv_deadline_tsc > tscl)
8844 /* sure to be 32 bit only because checked on set_hv_timer */
8845 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8846 cpu_preemption_timer_multi);
8847 else
8848 delta_tsc = 0;
8849
8850 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8851}
8852
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008853static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008854{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008855 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008856 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008857
8858 /* Record the guest's net vcpu time for enforced NMI injections. */
8859 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8860 vmx->entry_time = ktime_get();
8861
8862 /* Don't enter VMX if guest state is invalid, let the exit handler
8863 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008864 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008865 return;
8866
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008867 if (vmx->ple_window_dirty) {
8868 vmx->ple_window_dirty = false;
8869 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8870 }
8871
Abel Gordon012f83c2013-04-18 14:39:25 +03008872 if (vmx->nested.sync_shadow_vmcs) {
8873 copy_vmcs12_to_shadow(vmx);
8874 vmx->nested.sync_shadow_vmcs = false;
8875 }
8876
Avi Kivity104f2262010-11-18 13:12:52 +02008877 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8878 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8879 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8880 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8881
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008882 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008883 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8884 vmcs_writel(HOST_CR4, cr4);
8885 vmx->host_state.vmcs_host_cr4 = cr4;
8886 }
8887
Avi Kivity104f2262010-11-18 13:12:52 +02008888 /* When single-stepping over STI and MOV SS, we must clear the
8889 * corresponding interruptibility bits in the guest state. Otherwise
8890 * vmentry fails as it then expects bit 14 (BS) in pending debug
8891 * exceptions being set, but that's not correct for the guest debugging
8892 * case. */
8893 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8894 vmx_set_interrupt_shadow(vcpu, 0);
8895
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008896 if (vmx->guest_pkru_valid)
8897 __write_pkru(vmx->guest_pkru);
8898
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008899 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008900 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008901
Yunhong Jiang64672c92016-06-13 14:19:59 -07008902 vmx_arm_hv_timer(vcpu);
8903
Nadav Har'Eld462b812011-05-24 15:26:10 +03008904 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008905 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008906 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008907 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8908 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8909 "push %%" _ASM_CX " \n\t"
8910 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008911 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008912 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008913 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008914 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008915 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008916 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8917 "mov %%cr2, %%" _ASM_DX " \n\t"
8918 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008919 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008920 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008921 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008922 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008923 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008924 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008925 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8926 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8927 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8928 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8929 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8930 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008931#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008932 "mov %c[r8](%0), %%r8 \n\t"
8933 "mov %c[r9](%0), %%r9 \n\t"
8934 "mov %c[r10](%0), %%r10 \n\t"
8935 "mov %c[r11](%0), %%r11 \n\t"
8936 "mov %c[r12](%0), %%r12 \n\t"
8937 "mov %c[r13](%0), %%r13 \n\t"
8938 "mov %c[r14](%0), %%r14 \n\t"
8939 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008940#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008941 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008942
Avi Kivity6aa8b732006-12-10 02:21:36 -08008943 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008944 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008945 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008946 "jmp 2f \n\t"
8947 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8948 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008949 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008950 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008951 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008952 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8953 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8954 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8955 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8956 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8957 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8958 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008959#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008960 "mov %%r8, %c[r8](%0) \n\t"
8961 "mov %%r9, %c[r9](%0) \n\t"
8962 "mov %%r10, %c[r10](%0) \n\t"
8963 "mov %%r11, %c[r11](%0) \n\t"
8964 "mov %%r12, %c[r12](%0) \n\t"
8965 "mov %%r13, %c[r13](%0) \n\t"
8966 "mov %%r14, %c[r14](%0) \n\t"
8967 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008968#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008969 "mov %%cr2, %%" _ASM_AX " \n\t"
8970 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008971
Avi Kivityb188c81f2012-09-16 15:10:58 +03008972 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008973 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008974 ".pushsection .rodata \n\t"
8975 ".global vmx_return \n\t"
8976 "vmx_return: " _ASM_PTR " 2b \n\t"
8977 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008978 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008979 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008980 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03008981 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008982 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8983 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8984 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8985 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8986 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8987 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8988 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008989#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008990 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8991 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8992 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8993 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8994 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8995 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8996 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8997 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008998#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008999 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
9000 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02009001 : "cc", "memory"
9002#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009003 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009004 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009005#else
9006 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009007#endif
9008 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009009
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009010 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9011 if (debugctlmsr)
9012 update_debugctlmsr(debugctlmsr);
9013
Avi Kivityaa67f602012-08-01 16:48:03 +03009014#ifndef CONFIG_X86_64
9015 /*
9016 * The sysexit path does not restore ds/es, so we must set them to
9017 * a reasonable value ourselves.
9018 *
9019 * We can't defer this to vmx_load_host_state() since that function
9020 * may be executed in interrupt context, which saves and restore segments
9021 * around it, nullifying its effect.
9022 */
9023 loadsegment(ds, __USER_DS);
9024 loadsegment(es, __USER_DS);
9025#endif
9026
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009027 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009028 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009029 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009030 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009031 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009032 vcpu->arch.regs_dirty = 0;
9033
Avi Kivity1155f762007-11-22 11:30:47 +02009034 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9035
Nadav Har'Eld462b812011-05-24 15:26:10 +03009036 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009037
Avi Kivity51aa01d2010-07-20 14:31:20 +03009038 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009039
Gleb Natapove0b890d2013-09-25 12:51:33 +03009040 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009041 * eager fpu is enabled if PKEY is supported and CR4 is switched
9042 * back on host, so it is safe to read guest PKRU from current
9043 * XSAVE.
9044 */
9045 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9046 vmx->guest_pkru = __read_pkru();
9047 if (vmx->guest_pkru != vmx->host_pkru) {
9048 vmx->guest_pkru_valid = true;
9049 __write_pkru(vmx->host_pkru);
9050 } else
9051 vmx->guest_pkru_valid = false;
9052 }
9053
9054 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009055 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9056 * we did not inject a still-pending event to L1 now because of
9057 * nested_run_pending, we need to re-enable this bit.
9058 */
9059 if (vmx->nested.nested_run_pending)
9060 kvm_make_request(KVM_REQ_EVENT, vcpu);
9061
9062 vmx->nested.nested_run_pending = 0;
9063
Avi Kivity51aa01d2010-07-20 14:31:20 +03009064 vmx_complete_atomic_exit(vmx);
9065 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009066 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009067}
9068
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009069static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9070{
9071 struct vcpu_vmx *vmx = to_vmx(vcpu);
9072 int cpu;
9073
9074 if (vmx->loaded_vmcs == &vmx->vmcs01)
9075 return;
9076
9077 cpu = get_cpu();
9078 vmx->loaded_vmcs = &vmx->vmcs01;
9079 vmx_vcpu_put(vcpu);
9080 vmx_vcpu_load(vcpu, cpu);
9081 vcpu->cpu = cpu;
9082 put_cpu();
9083}
9084
Jim Mattson2f1fe812016-07-08 15:36:06 -07009085/*
9086 * Ensure that the current vmcs of the logical processor is the
9087 * vmcs01 of the vcpu before calling free_nested().
9088 */
9089static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9090{
9091 struct vcpu_vmx *vmx = to_vmx(vcpu);
9092 int r;
9093
9094 r = vcpu_load(vcpu);
9095 BUG_ON(r);
9096 vmx_load_vmcs01(vcpu);
9097 free_nested(vmx);
9098 vcpu_put(vcpu);
9099}
9100
Avi Kivity6aa8b732006-12-10 02:21:36 -08009101static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9102{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009103 struct vcpu_vmx *vmx = to_vmx(vcpu);
9104
Kai Huang843e4332015-01-28 10:54:28 +08009105 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009106 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009107 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009108 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009109 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009110 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009111 kfree(vmx->guest_msrs);
9112 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009113 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009114}
9115
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009116static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009117{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009118 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009119 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009120 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009121
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009122 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009123 return ERR_PTR(-ENOMEM);
9124
Wanpeng Li991e7a02015-09-16 17:30:05 +08009125 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009126
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009127 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9128 if (err)
9129 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009130
Peter Feiner4e595162016-07-07 14:49:58 -07009131 err = -ENOMEM;
9132
9133 /*
9134 * If PML is turned on, failure on enabling PML just results in failure
9135 * of creating the vcpu, therefore we can simplify PML logic (by
9136 * avoiding dealing with cases, such as enabling PML partially on vcpus
9137 * for the guest, etc.
9138 */
9139 if (enable_pml) {
9140 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9141 if (!vmx->pml_pg)
9142 goto uninit_vcpu;
9143 }
9144
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009145 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009146 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9147 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009148
Peter Feiner4e595162016-07-07 14:49:58 -07009149 if (!vmx->guest_msrs)
9150 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009151
Nadav Har'Eld462b812011-05-24 15:26:10 +03009152 vmx->loaded_vmcs = &vmx->vmcs01;
9153 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009154 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009155 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009156 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009157 if (!vmm_exclusive)
9158 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
9159 loaded_vmcs_init(vmx->loaded_vmcs);
9160 if (!vmm_exclusive)
9161 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009162
Avi Kivity15ad7142007-07-11 18:17:21 +03009163 cpu = get_cpu();
9164 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009165 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009166 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009167 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009168 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009169 if (err)
9170 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009171 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009172 err = alloc_apic_access_page(kvm);
9173 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009174 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009175 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009176
Sheng Yangb927a3c2009-07-21 10:42:48 +08009177 if (enable_ept) {
9178 if (!kvm->arch.ept_identity_map_addr)
9179 kvm->arch.ept_identity_map_addr =
9180 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009181 err = init_rmode_identity_map(kvm);
9182 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009183 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009184 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009185
Wanpeng Li5c614b32015-10-13 09:18:36 -07009186 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009187 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009188 vmx->nested.vpid02 = allocate_vpid();
9189 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009190
Wincy Van705699a2015-02-03 23:58:17 +08009191 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009192 vmx->nested.current_vmptr = -1ull;
9193 vmx->nested.current_vmcs12 = NULL;
9194
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009195 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9196
Paolo Bonzini58d2fb12017-06-06 12:57:06 +02009197 /*
9198 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
9199 * or POSTED_INTR_WAKEUP_VECTOR.
9200 */
9201 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
9202 vmx->pi_desc.sn = 1;
9203
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009204 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009205
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009206free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009207 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009208 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009209free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009210 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009211free_pml:
9212 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009213uninit_vcpu:
9214 kvm_vcpu_uninit(&vmx->vcpu);
9215free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009216 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009217 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009218 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009219}
9220
Yang, Sheng002c7f72007-07-31 14:23:01 +03009221static void __init vmx_check_processor_compat(void *rtn)
9222{
9223 struct vmcs_config vmcs_conf;
9224
9225 *(int *)rtn = 0;
9226 if (setup_vmcs_config(&vmcs_conf) < 0)
9227 *(int *)rtn = -EIO;
9228 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9229 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9230 smp_processor_id());
9231 *(int *)rtn = -EIO;
9232 }
9233}
9234
Sheng Yang67253af2008-04-25 10:20:22 +08009235static int get_ept_level(void)
9236{
9237 return VMX_EPT_DEFAULT_GAW + 1;
9238}
9239
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009240static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009241{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009242 u8 cache;
9243 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009244
Sheng Yang522c68c2009-04-27 20:35:43 +08009245 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009246 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009247 * 2. EPT with VT-d:
9248 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009249 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009250 * b. VT-d with snooping control feature: snooping control feature of
9251 * VT-d engine can guarantee the cache correctness. Just set it
9252 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009253 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009254 * consistent with host MTRR
9255 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009256 if (is_mmio) {
9257 cache = MTRR_TYPE_UNCACHABLE;
9258 goto exit;
9259 }
9260
9261 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009262 ipat = VMX_EPT_IPAT_BIT;
9263 cache = MTRR_TYPE_WRBACK;
9264 goto exit;
9265 }
9266
9267 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9268 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009269 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009270 cache = MTRR_TYPE_WRBACK;
9271 else
9272 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009273 goto exit;
9274 }
9275
Xiao Guangrongff536042015-06-15 16:55:22 +08009276 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009277
9278exit:
9279 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009280}
9281
Sheng Yang17cc3932010-01-05 19:02:27 +08009282static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009283{
Sheng Yang878403b2010-01-05 19:02:29 +08009284 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9285 return PT_DIRECTORY_LEVEL;
9286 else
9287 /* For shadow and EPT supported 1GB page */
9288 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009289}
9290
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009291static void vmcs_set_secondary_exec_control(u32 new_ctl)
9292{
9293 /*
9294 * These bits in the secondary execution controls field
9295 * are dynamic, the others are mostly based on the hypervisor
9296 * architecture and the guest's CPUID. Do not touch the
9297 * dynamic bits.
9298 */
9299 u32 mask =
9300 SECONDARY_EXEC_SHADOW_VMCS |
9301 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9302 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9303
9304 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9305
9306 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9307 (new_ctl & ~mask) | (cur_ctl & mask));
9308}
9309
Sheng Yang0e851882009-12-18 16:48:46 +08009310static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9311{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009312 struct kvm_cpuid_entry2 *best;
9313 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009314 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009315
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009316 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009317 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9318 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009319 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009320
Paolo Bonzini8b972652015-09-15 17:34:42 +02009321 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009322 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009323 vmx->nested.nested_vmx_secondary_ctls_high |=
9324 SECONDARY_EXEC_RDTSCP;
9325 else
9326 vmx->nested.nested_vmx_secondary_ctls_high &=
9327 ~SECONDARY_EXEC_RDTSCP;
9328 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009329 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009330
Mao, Junjiead756a12012-07-02 01:18:48 +00009331 /* Exposing INVPCID only when PCID is exposed */
9332 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9333 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009334 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9335 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009336 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009337
Mao, Junjiead756a12012-07-02 01:18:48 +00009338 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009339 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009340 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009341
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009342 if (cpu_has_secondary_exec_ctrls())
9343 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009344
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009345 if (nested_vmx_allowed(vcpu))
9346 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9347 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9348 else
9349 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9350 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009351}
9352
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009353static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9354{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009355 if (func == 1 && nested)
9356 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009357}
9358
Yang Zhang25d92082013-08-06 12:00:32 +03009359static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9360 struct x86_exception *fault)
9361{
Jan Kiszka533558b2014-01-04 18:47:20 +01009362 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9363 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009364
9365 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009366 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009367 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009368 exit_reason = EXIT_REASON_EPT_VIOLATION;
9369 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009370 vmcs12->guest_physical_address = fault->address;
9371}
9372
Nadav Har'El155a97a2013-08-05 11:07:16 +03009373/* Callbacks for nested_ept_init_mmu_context: */
9374
9375static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9376{
9377 /* return the page table to be shadowed - in our case, EPT12 */
9378 return get_vmcs12(vcpu)->ept_pointer;
9379}
9380
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009381static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009382{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009383 WARN_ON(mmu_is_nested(vcpu));
9384 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009385 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9386 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009387 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9388 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9389 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9390
9391 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009392}
9393
9394static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9395{
9396 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9397}
9398
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009399static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9400 u16 error_code)
9401{
9402 bool inequality, bit;
9403
9404 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9405 inequality =
9406 (error_code & vmcs12->page_fault_error_code_mask) !=
9407 vmcs12->page_fault_error_code_match;
9408 return inequality ^ bit;
9409}
9410
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009411static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9412 struct x86_exception *fault)
9413{
9414 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9415
9416 WARN_ON(!is_guest_mode(vcpu));
9417
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009418 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009419 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9420 vmcs_read32(VM_EXIT_INTR_INFO),
9421 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009422 else
9423 kvm_inject_page_fault(vcpu, fault);
9424}
9425
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009426static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9427 struct vmcs12 *vmcs12)
9428{
9429 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009430 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009431
9432 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009433 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9434 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009435 return false;
9436
9437 /*
9438 * Translate L1 physical address to host physical
9439 * address for vmcs02. Keep the page pinned, so this
9440 * physical address remains valid. We keep a reference
9441 * to it so we can release it later.
9442 */
9443 if (vmx->nested.apic_access_page) /* shouldn't happen */
9444 nested_release_page(vmx->nested.apic_access_page);
9445 vmx->nested.apic_access_page =
9446 nested_get_page(vcpu, vmcs12->apic_access_addr);
9447 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009448
9449 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009450 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9451 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009452 return false;
9453
9454 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9455 nested_release_page(vmx->nested.virtual_apic_page);
9456 vmx->nested.virtual_apic_page =
9457 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9458
9459 /*
9460 * Failing the vm entry is _not_ what the processor does
9461 * but it's basically the only possibility we have.
9462 * We could still enter the guest if CR8 load exits are
9463 * enabled, CR8 store exits are enabled, and virtualize APIC
9464 * access is disabled; in this case the processor would never
9465 * use the TPR shadow and we could simply clear the bit from
9466 * the execution control. But such a configuration is useless,
9467 * so let's keep the code simple.
9468 */
9469 if (!vmx->nested.virtual_apic_page)
9470 return false;
9471 }
9472
Wincy Van705699a2015-02-03 23:58:17 +08009473 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009474 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9475 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009476 return false;
9477
9478 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9479 kunmap(vmx->nested.pi_desc_page);
9480 nested_release_page(vmx->nested.pi_desc_page);
9481 }
9482 vmx->nested.pi_desc_page =
9483 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9484 if (!vmx->nested.pi_desc_page)
9485 return false;
9486
9487 vmx->nested.pi_desc =
9488 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9489 if (!vmx->nested.pi_desc) {
9490 nested_release_page_clean(vmx->nested.pi_desc_page);
9491 return false;
9492 }
9493 vmx->nested.pi_desc =
9494 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9495 (unsigned long)(vmcs12->posted_intr_desc_addr &
9496 (PAGE_SIZE - 1)));
9497 }
9498
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009499 return true;
9500}
9501
Jan Kiszkaf4124502014-03-07 20:03:13 +01009502static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9503{
9504 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9505 struct vcpu_vmx *vmx = to_vmx(vcpu);
9506
9507 if (vcpu->arch.virtual_tsc_khz == 0)
9508 return;
9509
9510 /* Make sure short timeouts reliably trigger an immediate vmexit.
9511 * hrtimer_start does not guarantee this. */
9512 if (preemption_timeout <= 1) {
9513 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9514 return;
9515 }
9516
9517 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9518 preemption_timeout *= 1000000;
9519 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9520 hrtimer_start(&vmx->nested.preemption_timer,
9521 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9522}
9523
Wincy Van3af18d92015-02-03 23:49:31 +08009524static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9525 struct vmcs12 *vmcs12)
9526{
9527 int maxphyaddr;
9528 u64 addr;
9529
9530 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9531 return 0;
9532
9533 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9534 WARN_ON(1);
9535 return -EINVAL;
9536 }
9537 maxphyaddr = cpuid_maxphyaddr(vcpu);
9538
9539 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9540 ((addr + PAGE_SIZE) >> maxphyaddr))
9541 return -EINVAL;
9542
9543 return 0;
9544}
9545
9546/*
9547 * Merge L0's and L1's MSR bitmap, return false to indicate that
9548 * we do not use the hardware.
9549 */
9550static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9551 struct vmcs12 *vmcs12)
9552{
Wincy Van82f0dd42015-02-03 23:57:18 +08009553 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009554 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009555 unsigned long *msr_bitmap_l1;
9556 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +08009557
Radim Krčmářd048c092016-08-08 20:16:22 +02009558 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +08009559 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9560 return false;
9561
9562 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
9563 if (!page) {
9564 WARN_ON(1);
9565 return false;
9566 }
Radim Krčmářd048c092016-08-08 20:16:22 +02009567 msr_bitmap_l1 = (unsigned long *)kmap(page);
9568 if (!msr_bitmap_l1) {
Wincy Vanf2b93282015-02-03 23:56:03 +08009569 nested_release_page_clean(page);
9570 WARN_ON(1);
9571 return false;
9572 }
9573
Radim Krčmářd048c092016-08-08 20:16:22 +02009574 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9575
Wincy Vanf2b93282015-02-03 23:56:03 +08009576 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009577 if (nested_cpu_has_apic_reg_virt(vmcs12))
9578 for (msr = 0x800; msr <= 0x8ff; msr++)
9579 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009580 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009581 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009582
9583 nested_vmx_disable_intercept_for_msr(
9584 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009585 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9586 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009587
Wincy Van608406e2015-02-03 23:57:51 +08009588 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009589 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009590 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009591 APIC_BASE_MSR + (APIC_EOI >> 4),
9592 MSR_TYPE_W);
9593 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009594 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009595 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9596 MSR_TYPE_W);
9597 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009598 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009599 kunmap(page);
9600 nested_release_page_clean(page);
9601
9602 return true;
9603}
9604
9605static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9606 struct vmcs12 *vmcs12)
9607{
Wincy Van82f0dd42015-02-03 23:57:18 +08009608 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009609 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009610 !nested_cpu_has_vid(vmcs12) &&
9611 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009612 return 0;
9613
9614 /*
9615 * If virtualize x2apic mode is enabled,
9616 * virtualize apic access must be disabled.
9617 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009618 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9619 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009620 return -EINVAL;
9621
Wincy Van608406e2015-02-03 23:57:51 +08009622 /*
9623 * If virtual interrupt delivery is enabled,
9624 * we must exit on external interrupts.
9625 */
9626 if (nested_cpu_has_vid(vmcs12) &&
9627 !nested_exit_on_intr(vcpu))
9628 return -EINVAL;
9629
Wincy Van705699a2015-02-03 23:58:17 +08009630 /*
9631 * bits 15:8 should be zero in posted_intr_nv,
9632 * the descriptor address has been already checked
9633 * in nested_get_vmcs12_pages.
9634 */
9635 if (nested_cpu_has_posted_intr(vmcs12) &&
9636 (!nested_cpu_has_vid(vmcs12) ||
9637 !nested_exit_intr_ack_set(vcpu) ||
9638 vmcs12->posted_intr_nv & 0xff00))
9639 return -EINVAL;
9640
Wincy Vanf2b93282015-02-03 23:56:03 +08009641 /* tpr shadow is needed by all apicv features. */
9642 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9643 return -EINVAL;
9644
9645 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009646}
9647
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009648static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9649 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009650 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009651{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009652 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009653 u64 count, addr;
9654
9655 if (vmcs12_read_any(vcpu, count_field, &count) ||
9656 vmcs12_read_any(vcpu, addr_field, &addr)) {
9657 WARN_ON(1);
9658 return -EINVAL;
9659 }
9660 if (count == 0)
9661 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009662 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009663 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9664 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009665 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009666 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9667 addr_field, maxphyaddr, count, addr);
9668 return -EINVAL;
9669 }
9670 return 0;
9671}
9672
9673static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9674 struct vmcs12 *vmcs12)
9675{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009676 if (vmcs12->vm_exit_msr_load_count == 0 &&
9677 vmcs12->vm_exit_msr_store_count == 0 &&
9678 vmcs12->vm_entry_msr_load_count == 0)
9679 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009680 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009681 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009682 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009683 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009684 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009685 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009686 return -EINVAL;
9687 return 0;
9688}
9689
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009690static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9691 struct vmx_msr_entry *e)
9692{
9693 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009694 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009695 return -EINVAL;
9696 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9697 e->index == MSR_IA32_UCODE_REV)
9698 return -EINVAL;
9699 if (e->reserved != 0)
9700 return -EINVAL;
9701 return 0;
9702}
9703
9704static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9705 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009706{
9707 if (e->index == MSR_FS_BASE ||
9708 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009709 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9710 nested_vmx_msr_check_common(vcpu, e))
9711 return -EINVAL;
9712 return 0;
9713}
9714
9715static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9716 struct vmx_msr_entry *e)
9717{
9718 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9719 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009720 return -EINVAL;
9721 return 0;
9722}
9723
9724/*
9725 * Load guest's/host's msr at nested entry/exit.
9726 * return 0 for success, entry index for failure.
9727 */
9728static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9729{
9730 u32 i;
9731 struct vmx_msr_entry e;
9732 struct msr_data msr;
9733
9734 msr.host_initiated = false;
9735 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009736 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9737 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009738 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009739 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9740 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009741 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009742 }
9743 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009744 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009745 "%s check failed (%u, 0x%x, 0x%x)\n",
9746 __func__, i, e.index, e.reserved);
9747 goto fail;
9748 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009749 msr.index = e.index;
9750 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009751 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009752 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009753 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9754 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009755 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009756 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009757 }
9758 return 0;
9759fail:
9760 return i + 1;
9761}
9762
9763static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9764{
9765 u32 i;
9766 struct vmx_msr_entry e;
9767
9768 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009769 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009770 if (kvm_vcpu_read_guest(vcpu,
9771 gpa + i * sizeof(e),
9772 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009773 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009774 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9775 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009776 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009777 }
9778 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009779 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009780 "%s check failed (%u, 0x%x, 0x%x)\n",
9781 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009782 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009783 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009784 msr_info.host_initiated = false;
9785 msr_info.index = e.index;
9786 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009787 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009788 "%s cannot read MSR (%u, 0x%x)\n",
9789 __func__, i, e.index);
9790 return -EINVAL;
9791 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009792 if (kvm_vcpu_write_guest(vcpu,
9793 gpa + i * sizeof(e) +
9794 offsetof(struct vmx_msr_entry, value),
9795 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009796 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009797 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009798 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009799 return -EINVAL;
9800 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009801 }
9802 return 0;
9803}
9804
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009805/*
9806 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9807 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009808 * 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 +03009809 * guest in a way that will both be appropriate to L1's requests, and our
9810 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9811 * function also has additional necessary side-effects, like setting various
9812 * vcpu->arch fields.
9813 */
9814static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9815{
9816 struct vcpu_vmx *vmx = to_vmx(vcpu);
9817 u32 exec_control;
9818
9819 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9820 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9821 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9822 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9823 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9824 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9825 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9826 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9827 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9828 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9829 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9830 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9831 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9832 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9833 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9834 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9835 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9836 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9837 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9838 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9839 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9840 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9841 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9842 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9843 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9844 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9845 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9846 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9847 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9848 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9849 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9850 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9851 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9852 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9853 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9854 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9855
Jan Kiszka2996fca2014-06-16 13:59:43 +02009856 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9857 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9858 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9859 } else {
9860 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9861 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9862 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009863 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9864 vmcs12->vm_entry_intr_info_field);
9865 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9866 vmcs12->vm_entry_exception_error_code);
9867 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9868 vmcs12->vm_entry_instruction_len);
9869 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9870 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009871 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009872 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009873 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9874 vmcs12->guest_pending_dbg_exceptions);
9875 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9876 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9877
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009878 if (nested_cpu_has_xsaves(vmcs12))
9879 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009880 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9881
Jan Kiszkaf4124502014-03-07 20:03:13 +01009882 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009883
Paolo Bonzini93140062016-07-06 13:23:51 +02009884 /* Preemption timer setting is only taken from vmcs01. */
9885 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9886 exec_control |= vmcs_config.pin_based_exec_ctrl;
9887 if (vmx->hv_deadline_tsc == -1)
9888 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9889
9890 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +08009891 if (nested_cpu_has_posted_intr(vmcs12)) {
9892 /*
9893 * Note that we use L0's vector here and in
9894 * vmx_deliver_nested_posted_interrupt.
9895 */
9896 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9897 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +08009898 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +08009899 vmcs_write64(POSTED_INTR_DESC_ADDR,
9900 page_to_phys(vmx->nested.pi_desc_page) +
9901 (unsigned long)(vmcs12->posted_intr_desc_addr &
9902 (PAGE_SIZE - 1)));
9903 } else
9904 exec_control &= ~PIN_BASED_POSTED_INTR;
9905
Jan Kiszkaf4124502014-03-07 20:03:13 +01009906 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009907
Jan Kiszkaf4124502014-03-07 20:03:13 +01009908 vmx->nested.preemption_timer_expired = false;
9909 if (nested_cpu_has_preemption_timer(vmcs12))
9910 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01009911
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009912 /*
9913 * Whether page-faults are trapped is determined by a combination of
9914 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
9915 * If enable_ept, L0 doesn't care about page faults and we should
9916 * set all of these to L1's desires. However, if !enable_ept, L0 does
9917 * care about (at least some) page faults, and because it is not easy
9918 * (if at all possible?) to merge L0 and L1's desires, we simply ask
9919 * to exit on each and every L2 page fault. This is done by setting
9920 * MASK=MATCH=0 and (see below) EB.PF=1.
9921 * Note that below we don't need special code to set EB.PF beyond the
9922 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
9923 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
9924 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
9925 *
9926 * A problem with this approach (when !enable_ept) is that L1 may be
9927 * injected with more page faults than it asked for. This could have
9928 * caused problems, but in practice existing hypervisors don't care.
9929 * To fix this, we will need to emulate the PFEC checking (on the L1
9930 * page tables), using walk_addr(), when injecting PFs to L1.
9931 */
9932 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
9933 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
9934 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
9935 enable_ept ? vmcs12->page_fault_error_code_match : 0);
9936
9937 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +01009938 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +08009939
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009940 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009941 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009942 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009943 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -07009944 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009945 if (nested_cpu_has(vmcs12,
9946 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
9947 exec_control |= vmcs12->secondary_vm_exec_control;
9948
9949 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
9950 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009951 * If translation failed, no matter: This feature asks
9952 * to exit when accessing the given address, and if it
9953 * can never be accessed, this feature won't do
9954 * anything anyway.
9955 */
9956 if (!vmx->nested.apic_access_page)
9957 exec_control &=
9958 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9959 else
9960 vmcs_write64(APIC_ACCESS_ADDR,
9961 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +08009962 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +02009963 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +01009964 exec_control |=
9965 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +08009966 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009967 }
9968
Wincy Van608406e2015-02-03 23:57:51 +08009969 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
9970 vmcs_write64(EOI_EXIT_BITMAP0,
9971 vmcs12->eoi_exit_bitmap0);
9972 vmcs_write64(EOI_EXIT_BITMAP1,
9973 vmcs12->eoi_exit_bitmap1);
9974 vmcs_write64(EOI_EXIT_BITMAP2,
9975 vmcs12->eoi_exit_bitmap2);
9976 vmcs_write64(EOI_EXIT_BITMAP3,
9977 vmcs12->eoi_exit_bitmap3);
9978 vmcs_write16(GUEST_INTR_STATUS,
9979 vmcs12->guest_intr_status);
9980 }
9981
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009982 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
9983 }
9984
9985
9986 /*
9987 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
9988 * Some constant fields are set here by vmx_set_constant_host_state().
9989 * Other fields are different per CPU, and will be set later when
9990 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
9991 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08009992 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009993
9994 /*
9995 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
9996 * entry, but only if the current (host) sp changed from the value
9997 * we wrote last (vmx->host_rsp). This cache is no longer relevant
9998 * if we switch vmcs, and rather than hold a separate cache per vmcs,
9999 * here we just force the write to happen on entry.
10000 */
10001 vmx->host_rsp = 0;
10002
10003 exec_control = vmx_exec_control(vmx); /* L0's desires */
10004 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10005 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10006 exec_control &= ~CPU_BASED_TPR_SHADOW;
10007 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010008
10009 if (exec_control & CPU_BASED_TPR_SHADOW) {
10010 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
10011 page_to_phys(vmx->nested.virtual_apic_page));
10012 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson86ef97b2017-09-12 13:02:54 -070010013 } else {
10014#ifdef CONFIG_X86_64
10015 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
10016 CPU_BASED_CR8_STORE_EXITING;
10017#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010018 }
10019
Wincy Van3af18d92015-02-03 23:49:31 +080010020 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +020010021 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
10022 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
10023 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
10024 else
Wincy Van3af18d92015-02-03 23:49:31 +080010025 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
10026
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010027 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010028 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010029 * Rather, exit every time.
10030 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010031 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10032 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10033
10034 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10035
10036 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10037 * bitwise-or of what L1 wants to trap for L2, and what we want to
10038 * trap. Note that CR0.TS also needs updating - we do this later.
10039 */
10040 update_exception_bitmap(vcpu);
10041 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10042 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10043
Nadav Har'El8049d652013-08-05 11:07:06 +030010044 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10045 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10046 * bits are further modified by vmx_set_efer() below.
10047 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010010048 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010049
10050 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10051 * emulated by vmx_set_efer(), below.
10052 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010053 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010054 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10055 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010056 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10057
Jan Kiszka44811c02013-08-04 17:17:27 +020010058 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010059 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010060 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10061 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010062 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10063
10064
10065 set_cr4_guest_host_mask(vmx);
10066
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010067 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10068 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10069
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010070 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10071 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010072 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010073 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010074 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010075 if (kvm_has_tsc_control)
10076 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010077
10078 if (enable_vpid) {
10079 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010080 * There is no direct mapping between vpid02 and vpid12, the
10081 * vpid02 is per-vCPU for L0 and reused while the value of
10082 * vpid12 is changed w/ one invvpid during nested vmentry.
10083 * The vpid12 is allocated by L1 for L2, so it will not
10084 * influence global bitmap(for vpid01 and vpid02 allocation)
10085 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010086 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010087 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10088 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10089 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10090 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10091 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10092 }
10093 } else {
10094 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10095 vmx_flush_tlb(vcpu);
10096 }
10097
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010098 }
10099
Ladi Prosek560a9792017-04-04 14:18:53 +020010100 if (enable_pml) {
10101 /*
10102 * Conceptually we want to copy the PML address and index from
10103 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
10104 * since we always flush the log on each vmexit, this happens
10105 * to be equivalent to simply resetting the fields in vmcs02.
10106 */
10107 ASSERT(vmx->pml_pg);
10108 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
10109 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
10110 }
10111
Nadav Har'El155a97a2013-08-05 11:07:16 +030010112 if (nested_cpu_has_ept(vmcs12)) {
10113 kvm_mmu_unload(vcpu);
10114 nested_ept_init_mmu_context(vcpu);
10115 }
10116
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010117 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10118 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010119 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010120 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10121 else
10122 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10123 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10124 vmx_set_efer(vcpu, vcpu->arch.efer);
10125
10126 /*
10127 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10128 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10129 * The CR0_READ_SHADOW is what L2 should have expected to read given
10130 * the specifications by L1; It's not enough to take
10131 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10132 * have more bits than L1 expected.
10133 */
10134 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10135 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10136
10137 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10138 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10139
10140 /* shadow page tables on either EPT or shadow page tables */
10141 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10142 kvm_mmu_reset_context(vcpu);
10143
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010144 if (!enable_ept)
10145 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10146
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010147 /*
10148 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10149 */
10150 if (enable_ept) {
10151 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10152 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10153 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10154 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10155 }
10156
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010157 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10158 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10159}
10160
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010161/*
10162 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10163 * for running an L2 nested guest.
10164 */
10165static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10166{
10167 struct vmcs12 *vmcs12;
10168 struct vcpu_vmx *vmx = to_vmx(vcpu);
10169 int cpu;
10170 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +020010171 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010172 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010173
10174 if (!nested_vmx_check_permission(vcpu) ||
10175 !nested_vmx_check_vmcs12(vcpu))
10176 return 1;
10177
10178 skip_emulated_instruction(vcpu);
10179 vmcs12 = get_vmcs12(vcpu);
10180
Abel Gordon012f83c2013-04-18 14:39:25 +030010181 if (enable_shadow_vmcs)
10182 copy_shadow_to_vmcs12(vmx);
10183
Nadav Har'El7c177932011-05-25 23:12:04 +030010184 /*
10185 * The nested entry process starts with enforcing various prerequisites
10186 * on vmcs12 as required by the Intel SDM, and act appropriately when
10187 * they fail: As the SDM explains, some conditions should cause the
10188 * instruction to fail, while others will cause the instruction to seem
10189 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10190 * To speed up the normal (success) code path, we should avoid checking
10191 * for misconfigurations which will anyway be caught by the processor
10192 * when using the merged vmcs02.
10193 */
10194 if (vmcs12->launch_state == launch) {
10195 nested_vmx_failValid(vcpu,
10196 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10197 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10198 return 1;
10199 }
10200
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010201 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10202 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010203 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10204 return 1;
10205 }
10206
Wincy Van3af18d92015-02-03 23:49:31 +080010207 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010208 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10209 return 1;
10210 }
10211
Wincy Van3af18d92015-02-03 23:49:31 +080010212 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010213 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10214 return 1;
10215 }
10216
Wincy Vanf2b93282015-02-03 23:56:03 +080010217 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10218 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10219 return 1;
10220 }
10221
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010222 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10223 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10224 return 1;
10225 }
10226
Nadav Har'El7c177932011-05-25 23:12:04 +030010227 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010228 vmx->nested.nested_vmx_true_procbased_ctls_low,
10229 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010230 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010231 vmx->nested.nested_vmx_secondary_ctls_low,
10232 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010233 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010234 vmx->nested.nested_vmx_pinbased_ctls_low,
10235 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010236 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010237 vmx->nested.nested_vmx_true_exit_ctls_low,
10238 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010239 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010240 vmx->nested.nested_vmx_true_entry_ctls_low,
10241 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010242 {
10243 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10244 return 1;
10245 }
10246
10247 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10248 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10249 nested_vmx_failValid(vcpu,
10250 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10251 return 1;
10252 }
10253
Wincy Vanb9c237b2015-02-03 23:56:30 +080010254 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010255 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10256 nested_vmx_entry_failure(vcpu, vmcs12,
10257 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10258 return 1;
10259 }
10260 if (vmcs12->vmcs_link_pointer != -1ull) {
10261 nested_vmx_entry_failure(vcpu, vmcs12,
10262 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10263 return 1;
10264 }
10265
10266 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010267 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010268 * are performed on the field for the IA32_EFER MSR:
10269 * - Bits reserved in the IA32_EFER MSR must be 0.
10270 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10271 * the IA-32e mode guest VM-exit control. It must also be identical
10272 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10273 * CR0.PG) is 1.
10274 */
10275 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10276 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10277 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10278 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10279 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10280 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10281 nested_vmx_entry_failure(vcpu, vmcs12,
10282 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10283 return 1;
10284 }
10285 }
10286
10287 /*
10288 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10289 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10290 * the values of the LMA and LME bits in the field must each be that of
10291 * the host address-space size VM-exit control.
10292 */
10293 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10294 ia32e = (vmcs12->vm_exit_controls &
10295 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10296 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10297 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10298 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10299 nested_vmx_entry_failure(vcpu, vmcs12,
10300 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10301 return 1;
10302 }
10303 }
10304
10305 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010306 * We're finally done with prerequisite checking, and can start with
10307 * the nested entry.
10308 */
10309
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010310 vmcs02 = nested_get_current_vmcs02(vmx);
10311 if (!vmcs02)
10312 return -ENOMEM;
10313
10314 enter_guest_mode(vcpu);
10315
Jan Kiszka2996fca2014-06-16 13:59:43 +020010316 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10317 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10318
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010319 cpu = get_cpu();
10320 vmx->loaded_vmcs = vmcs02;
10321 vmx_vcpu_put(vcpu);
10322 vmx_vcpu_load(vcpu, cpu);
10323 vcpu->cpu = cpu;
10324 put_cpu();
10325
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010326 vmx_segment_cache_clear(vmx);
10327
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010328 prepare_vmcs02(vcpu, vmcs12);
10329
Wincy Vanff651cb2014-12-11 08:52:58 +030010330 msr_entry_idx = nested_vmx_load_msr(vcpu,
10331 vmcs12->vm_entry_msr_load_addr,
10332 vmcs12->vm_entry_msr_load_count);
10333 if (msr_entry_idx) {
10334 leave_guest_mode(vcpu);
10335 vmx_load_vmcs01(vcpu);
10336 nested_vmx_entry_failure(vcpu, vmcs12,
10337 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10338 return 1;
10339 }
10340
10341 vmcs12->launch_state = 1;
10342
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010343 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010344 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010345
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010346 vmx->nested.nested_run_pending = 1;
10347
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010348 /*
10349 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10350 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10351 * returned as far as L1 is concerned. It will only return (and set
10352 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10353 */
10354 return 1;
10355}
10356
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010357/*
10358 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10359 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10360 * This function returns the new value we should put in vmcs12.guest_cr0.
10361 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10362 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10363 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10364 * didn't trap the bit, because if L1 did, so would L0).
10365 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10366 * been modified by L2, and L1 knows it. So just leave the old value of
10367 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10368 * isn't relevant, because if L0 traps this bit it can set it to anything.
10369 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10370 * changed these bits, and therefore they need to be updated, but L0
10371 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10372 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10373 */
10374static inline unsigned long
10375vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10376{
10377 return
10378 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10379 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10380 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10381 vcpu->arch.cr0_guest_owned_bits));
10382}
10383
10384static inline unsigned long
10385vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10386{
10387 return
10388 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10389 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10390 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10391 vcpu->arch.cr4_guest_owned_bits));
10392}
10393
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010394static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10395 struct vmcs12 *vmcs12)
10396{
10397 u32 idt_vectoring;
10398 unsigned int nr;
10399
Gleb Natapov851eb6672013-09-25 12:51:34 +030010400 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010401 nr = vcpu->arch.exception.nr;
10402 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10403
10404 if (kvm_exception_is_soft(nr)) {
10405 vmcs12->vm_exit_instruction_len =
10406 vcpu->arch.event_exit_inst_len;
10407 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10408 } else
10409 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10410
10411 if (vcpu->arch.exception.has_error_code) {
10412 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10413 vmcs12->idt_vectoring_error_code =
10414 vcpu->arch.exception.error_code;
10415 }
10416
10417 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010418 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010419 vmcs12->idt_vectoring_info_field =
10420 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10421 } else if (vcpu->arch.interrupt.pending) {
10422 nr = vcpu->arch.interrupt.nr;
10423 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10424
10425 if (vcpu->arch.interrupt.soft) {
10426 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10427 vmcs12->vm_entry_instruction_len =
10428 vcpu->arch.event_exit_inst_len;
10429 } else
10430 idt_vectoring |= INTR_TYPE_EXT_INTR;
10431
10432 vmcs12->idt_vectoring_info_field = idt_vectoring;
10433 }
10434}
10435
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010436static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10437{
10438 struct vcpu_vmx *vmx = to_vmx(vcpu);
10439
Jan Kiszkaf4124502014-03-07 20:03:13 +010010440 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10441 vmx->nested.preemption_timer_expired) {
10442 if (vmx->nested.nested_run_pending)
10443 return -EBUSY;
10444 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10445 return 0;
10446 }
10447
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010448 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010449 if (vmx->nested.nested_run_pending ||
10450 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010451 return -EBUSY;
10452 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10453 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10454 INTR_INFO_VALID_MASK, 0);
10455 /*
10456 * The NMI-triggered VM exit counts as injection:
10457 * clear this one and block further NMIs.
10458 */
10459 vcpu->arch.nmi_pending = 0;
10460 vmx_set_nmi_mask(vcpu, true);
10461 return 0;
10462 }
10463
10464 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10465 nested_exit_on_intr(vcpu)) {
10466 if (vmx->nested.nested_run_pending)
10467 return -EBUSY;
10468 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010469 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010470 }
10471
Wincy Van705699a2015-02-03 23:58:17 +080010472 return vmx_complete_nested_posted_interrupt(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010473}
10474
Jan Kiszkaf4124502014-03-07 20:03:13 +010010475static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10476{
10477 ktime_t remaining =
10478 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10479 u64 value;
10480
10481 if (ktime_to_ns(remaining) <= 0)
10482 return 0;
10483
10484 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10485 do_div(value, 1000000);
10486 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10487}
10488
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010489/*
10490 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10491 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10492 * and this function updates it to reflect the changes to the guest state while
10493 * L2 was running (and perhaps made some exits which were handled directly by L0
10494 * without going back to L1), and to reflect the exit reason.
10495 * Note that we do not have to copy here all VMCS fields, just those that
10496 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10497 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10498 * which already writes to vmcs12 directly.
10499 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010500static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10501 u32 exit_reason, u32 exit_intr_info,
10502 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010503{
10504 /* update guest state fields: */
10505 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10506 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10507
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010508 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10509 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10510 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10511
10512 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10513 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10514 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10515 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10516 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10517 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10518 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10519 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10520 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10521 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10522 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10523 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10524 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10525 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10526 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10527 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10528 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10529 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10530 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10531 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10532 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10533 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10534 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10535 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10536 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10537 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10538 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10539 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10540 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10541 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10542 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10543 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10544 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10545 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10546 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10547 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10548
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010549 vmcs12->guest_interruptibility_info =
10550 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10551 vmcs12->guest_pending_dbg_exceptions =
10552 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010553 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10554 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10555 else
10556 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010557
Jan Kiszkaf4124502014-03-07 20:03:13 +010010558 if (nested_cpu_has_preemption_timer(vmcs12)) {
10559 if (vmcs12->vm_exit_controls &
10560 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10561 vmcs12->vmx_preemption_timer_value =
10562 vmx_get_preemption_timer_value(vcpu);
10563 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10564 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010565
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010566 /*
10567 * In some cases (usually, nested EPT), L2 is allowed to change its
10568 * own CR3 without exiting. If it has changed it, we must keep it.
10569 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10570 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10571 *
10572 * Additionally, restore L2's PDPTR to vmcs12.
10573 */
10574 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010575 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010576 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10577 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10578 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10579 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10580 }
10581
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010582 if (nested_cpu_has_ept(vmcs12))
10583 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
10584
Wincy Van608406e2015-02-03 23:57:51 +080010585 if (nested_cpu_has_vid(vmcs12))
10586 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10587
Jan Kiszkac18911a2013-03-13 16:06:41 +010010588 vmcs12->vm_entry_controls =
10589 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010590 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010591
Jan Kiszka2996fca2014-06-16 13:59:43 +020010592 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10593 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10594 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10595 }
10596
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010597 /* TODO: These cannot have changed unless we have MSR bitmaps and
10598 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010599 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010600 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010601 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10602 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010603 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10604 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10605 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010606 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010607 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010608 if (nested_cpu_has_xsaves(vmcs12))
10609 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010610
10611 /* update exit information fields: */
10612
Jan Kiszka533558b2014-01-04 18:47:20 +010010613 vmcs12->vm_exit_reason = exit_reason;
10614 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010615
Jan Kiszka533558b2014-01-04 18:47:20 +010010616 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010617 if ((vmcs12->vm_exit_intr_info &
10618 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10619 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10620 vmcs12->vm_exit_intr_error_code =
10621 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010622 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010623 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10624 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10625
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010626 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10627 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10628 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010629 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010630
10631 /*
10632 * Transfer the event that L0 or L1 may wanted to inject into
10633 * L2 to IDT_VECTORING_INFO_FIELD.
10634 */
10635 vmcs12_save_pending_event(vcpu, vmcs12);
10636 }
10637
10638 /*
10639 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10640 * preserved above and would only end up incorrectly in L1.
10641 */
10642 vcpu->arch.nmi_injected = false;
10643 kvm_clear_exception_queue(vcpu);
10644 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010645}
10646
10647/*
10648 * A part of what we need to when the nested L2 guest exits and we want to
10649 * run its L1 parent, is to reset L1's guest state to the host state specified
10650 * in vmcs12.
10651 * This function is to be called not only on normal nested exit, but also on
10652 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10653 * Failures During or After Loading Guest State").
10654 * This function should be called when the active VMCS is L1's (vmcs01).
10655 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010656static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10657 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010658{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010659 struct kvm_segment seg;
10660
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010661 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10662 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010663 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010664 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10665 else
10666 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10667 vmx_set_efer(vcpu, vcpu->arch.efer);
10668
10669 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10670 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010671 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010672 /*
10673 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10674 * actually changed, because it depends on the current state of
10675 * fpu_active (which may have changed).
10676 * Note that vmx_set_cr0 refers to efer set above.
10677 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010678 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010679 /*
10680 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10681 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10682 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10683 */
10684 update_exception_bitmap(vcpu);
10685 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10686 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10687
10688 /*
10689 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10690 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10691 */
10692 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang08e16742017-10-10 15:01:22 +080010693 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010694
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010695 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010696
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010697 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10698 kvm_mmu_reset_context(vcpu);
10699
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010700 if (!enable_ept)
10701 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10702
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010703 if (enable_vpid) {
10704 /*
10705 * Trivially support vpid by letting L2s share their parent
10706 * L1's vpid. TODO: move to a more elaborate solution, giving
10707 * each L2 its own vpid and exposing the vpid feature to L1.
10708 */
10709 vmx_flush_tlb(vcpu);
10710 }
10711
10712
10713 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10714 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10715 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10716 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10717 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek1be0c0e2017-10-11 16:54:42 +020010718 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
10719 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010720
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010721 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10722 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10723 vmcs_write64(GUEST_BNDCFGS, 0);
10724
Jan Kiszka44811c02013-08-04 17:17:27 +020010725 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010726 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010727 vcpu->arch.pat = vmcs12->host_ia32_pat;
10728 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010729 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10730 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10731 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010732
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010733 /* Set L1 segment info according to Intel SDM
10734 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10735 seg = (struct kvm_segment) {
10736 .base = 0,
10737 .limit = 0xFFFFFFFF,
10738 .selector = vmcs12->host_cs_selector,
10739 .type = 11,
10740 .present = 1,
10741 .s = 1,
10742 .g = 1
10743 };
10744 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10745 seg.l = 1;
10746 else
10747 seg.db = 1;
10748 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10749 seg = (struct kvm_segment) {
10750 .base = 0,
10751 .limit = 0xFFFFFFFF,
10752 .type = 3,
10753 .present = 1,
10754 .s = 1,
10755 .db = 1,
10756 .g = 1
10757 };
10758 seg.selector = vmcs12->host_ds_selector;
10759 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10760 seg.selector = vmcs12->host_es_selector;
10761 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10762 seg.selector = vmcs12->host_ss_selector;
10763 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10764 seg.selector = vmcs12->host_fs_selector;
10765 seg.base = vmcs12->host_fs_base;
10766 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10767 seg.selector = vmcs12->host_gs_selector;
10768 seg.base = vmcs12->host_gs_base;
10769 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10770 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010771 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010772 .limit = 0x67,
10773 .selector = vmcs12->host_tr_selector,
10774 .type = 11,
10775 .present = 1
10776 };
10777 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10778
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010779 kvm_set_dr(vcpu, 7, 0x400);
10780 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010781
Wincy Van3af18d92015-02-03 23:49:31 +080010782 if (cpu_has_vmx_msr_bitmap())
10783 vmx_set_msr_bitmap(vcpu);
10784
Wincy Vanff651cb2014-12-11 08:52:58 +030010785 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10786 vmcs12->vm_exit_msr_load_count))
10787 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010788}
10789
10790/*
10791 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10792 * and modify vmcs12 to make it see what it would expect to see there if
10793 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10794 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010795static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10796 u32 exit_intr_info,
10797 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010798{
10799 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010800 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10801
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010802 /* trying to cancel vmlaunch/vmresume is a bug */
10803 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10804
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010805 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010806 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10807 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010808
Wincy Vanff651cb2014-12-11 08:52:58 +030010809 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10810 vmcs12->vm_exit_msr_store_count))
10811 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10812
Wanpeng Lif3380ca2014-08-05 12:42:23 +080010813 vmx_load_vmcs01(vcpu);
10814
Bandan Das77b0f5d2014-04-19 18:17:45 -040010815 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10816 && nested_exit_intr_ack_set(vcpu)) {
10817 int irq = kvm_cpu_get_interrupt(vcpu);
10818 WARN_ON(irq < 0);
10819 vmcs12->vm_exit_intr_info = irq |
10820 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10821 }
10822
Jan Kiszka542060e2014-01-04 18:47:21 +010010823 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10824 vmcs12->exit_qualification,
10825 vmcs12->idt_vectoring_info_field,
10826 vmcs12->vm_exit_intr_info,
10827 vmcs12->vm_exit_intr_error_code,
10828 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010829
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010830 vm_entry_controls_reset_shadow(vmx);
10831 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010832 vmx_segment_cache_clear(vmx);
10833
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010834 /* if no vmcs02 cache requested, remove the one we used */
10835 if (VMCS02_POOL_SIZE == 0)
10836 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10837
10838 load_vmcs12_host_state(vcpu, vmcs12);
10839
Paolo Bonzini93140062016-07-06 13:23:51 +020010840 /* Update any VMCS fields that might have changed while L2 ran */
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010841 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020010842 if (vmx->hv_deadline_tsc == -1)
10843 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10844 PIN_BASED_VMX_PREEMPTION_TIMER);
10845 else
10846 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10847 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070010848 if (kvm_has_tsc_control)
10849 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010850
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010851 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10852 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10853 vmx_set_virtual_x2apic_mode(vcpu,
10854 vcpu->arch.apic_base & X2APIC_ENABLE);
10855 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010856
10857 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10858 vmx->host_rsp = 0;
10859
10860 /* Unpin physical memory we referred to in vmcs02 */
10861 if (vmx->nested.apic_access_page) {
10862 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010863 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010864 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010865 if (vmx->nested.virtual_apic_page) {
10866 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010867 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010868 }
Wincy Van705699a2015-02-03 23:58:17 +080010869 if (vmx->nested.pi_desc_page) {
10870 kunmap(vmx->nested.pi_desc_page);
10871 nested_release_page(vmx->nested.pi_desc_page);
10872 vmx->nested.pi_desc_page = NULL;
10873 vmx->nested.pi_desc = NULL;
10874 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010875
10876 /*
Tang Chen38b99172014-09-24 15:57:54 +080010877 * We are now running in L2, mmu_notifier will force to reload the
10878 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10879 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080010880 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080010881
10882 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010883 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10884 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10885 * success or failure flag accordingly.
10886 */
10887 if (unlikely(vmx->fail)) {
10888 vmx->fail = 0;
10889 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
10890 } else
10891 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010892 if (enable_shadow_vmcs)
10893 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010894
10895 /* in case we halted in L2 */
10896 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010897}
10898
Nadav Har'El7c177932011-05-25 23:12:04 +030010899/*
Jan Kiszka42124922014-01-04 18:47:19 +010010900 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10901 */
10902static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10903{
10904 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +010010905 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +010010906 free_nested(to_vmx(vcpu));
10907}
10908
10909/*
Nadav Har'El7c177932011-05-25 23:12:04 +030010910 * L1's failure to enter L2 is a subset of a normal exit, as explained in
10911 * 23.7 "VM-entry failures during or after loading guest state" (this also
10912 * lists the acceptable exit-reason and exit-qualification parameters).
10913 * It should only be called before L2 actually succeeded to run, and when
10914 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
10915 */
10916static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
10917 struct vmcs12 *vmcs12,
10918 u32 reason, unsigned long qualification)
10919{
10920 load_vmcs12_host_state(vcpu, vmcs12);
10921 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
10922 vmcs12->exit_qualification = qualification;
10923 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010924 if (enable_shadow_vmcs)
10925 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030010926}
10927
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010928static int vmx_check_intercept(struct kvm_vcpu *vcpu,
10929 struct x86_instruction_info *info,
10930 enum x86_intercept_stage stage)
10931{
10932 return X86EMUL_CONTINUE;
10933}
10934
Yunhong Jiang64672c92016-06-13 14:19:59 -070010935#ifdef CONFIG_X86_64
10936/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
10937static inline int u64_shl_div_u64(u64 a, unsigned int shift,
10938 u64 divisor, u64 *result)
10939{
10940 u64 low = a << shift, high = a >> (64 - shift);
10941
10942 /* To avoid the overflow on divq */
10943 if (high >= divisor)
10944 return 1;
10945
10946 /* Low hold the result, high hold rem which is discarded */
10947 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
10948 "rm" (divisor), "0" (low), "1" (high));
10949 *result = low;
10950
10951 return 0;
10952}
10953
10954static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
10955{
10956 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020010957 u64 tscl = rdtsc();
10958 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
10959 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010960
10961 /* Convert to host delta tsc if tsc scaling is enabled */
10962 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
10963 u64_shl_div_u64(delta_tsc,
10964 kvm_tsc_scaling_ratio_frac_bits,
10965 vcpu->arch.tsc_scaling_ratio,
10966 &delta_tsc))
10967 return -ERANGE;
10968
10969 /*
10970 * If the delta tsc can't fit in the 32 bit after the multi shift,
10971 * we can't use the preemption timer.
10972 * It's possible that it fits on later vmentries, but checking
10973 * on every vmentry is costly so we just use an hrtimer.
10974 */
10975 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
10976 return -ERANGE;
10977
10978 vmx->hv_deadline_tsc = tscl + delta_tsc;
10979 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10980 PIN_BASED_VMX_PREEMPTION_TIMER);
10981 return 0;
10982}
10983
10984static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
10985{
10986 struct vcpu_vmx *vmx = to_vmx(vcpu);
10987 vmx->hv_deadline_tsc = -1;
10988 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10989 PIN_BASED_VMX_PREEMPTION_TIMER);
10990}
10991#endif
10992
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010993static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010994{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020010995 if (ple_gap)
10996 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010997}
10998
Kai Huang843e4332015-01-28 10:54:28 +080010999static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11000 struct kvm_memory_slot *slot)
11001{
11002 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11003 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11004}
11005
11006static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11007 struct kvm_memory_slot *slot)
11008{
11009 kvm_mmu_slot_set_dirty(kvm, slot);
11010}
11011
11012static void vmx_flush_log_dirty(struct kvm *kvm)
11013{
11014 kvm_flush_pml_buffers(kvm);
11015}
11016
11017static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11018 struct kvm_memory_slot *memslot,
11019 gfn_t offset, unsigned long mask)
11020{
11021 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11022}
11023
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011024static void __pi_post_block(struct kvm_vcpu *vcpu)
11025{
11026 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11027 struct pi_desc old, new;
11028 unsigned int dest;
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011029
11030 do {
11031 old.control = new.control = pi_desc->control;
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011032 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
11033 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011034
11035 dest = cpu_physical_id(vcpu->cpu);
11036
11037 if (x2apic_enabled())
11038 new.ndst = dest;
11039 else
11040 new.ndst = (dest << 8) & 0xFF00;
11041
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011042 /* set 'NV' to 'notification vector' */
11043 new.nv = POSTED_INTR_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011044 } while (cmpxchg64(&pi_desc->control, old.control,
11045 new.control) != old.control);
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011046
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011047 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
11048 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011049 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011050 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011051 vcpu->pre_pcpu = -1;
11052 }
11053}
11054
Feng Wuefc64402015-09-18 22:29:51 +080011055/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011056 * This routine does the following things for vCPU which is going
11057 * to be blocked if VT-d PI is enabled.
11058 * - Store the vCPU to the wakeup list, so when interrupts happen
11059 * we can find the right vCPU to wake up.
11060 * - Change the Posted-interrupt descriptor as below:
11061 * 'NDST' <-- vcpu->pre_pcpu
11062 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11063 * - If 'ON' is set during this process, which means at least one
11064 * interrupt is posted for this vCPU, we cannot block it, in
11065 * this case, return 1, otherwise, return 0.
11066 *
11067 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011068static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011069{
Feng Wubf9f6ac2015-09-18 22:29:55 +080011070 unsigned int dest;
11071 struct pi_desc old, new;
11072 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11073
11074 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011075 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11076 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011077 return 0;
11078
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011079 WARN_ON(irqs_disabled());
11080 local_irq_disable();
11081 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
11082 vcpu->pre_pcpu = vcpu->cpu;
11083 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11084 list_add_tail(&vcpu->blocked_vcpu_list,
11085 &per_cpu(blocked_vcpu_on_cpu,
11086 vcpu->pre_pcpu));
11087 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
11088 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080011089
11090 do {
11091 old.control = new.control = pi_desc->control;
11092
Feng Wubf9f6ac2015-09-18 22:29:55 +080011093 WARN((pi_desc->sn == 1),
11094 "Warning: SN field of posted-interrupts "
11095 "is set before blocking\n");
11096
11097 /*
11098 * Since vCPU can be preempted during this process,
11099 * vcpu->cpu could be different with pre_pcpu, we
11100 * need to set pre_pcpu as the destination of wakeup
11101 * notification event, then we can find the right vCPU
11102 * to wakeup in wakeup handler if interrupts happen
11103 * when the vCPU is in blocked state.
11104 */
11105 dest = cpu_physical_id(vcpu->pre_pcpu);
11106
11107 if (x2apic_enabled())
11108 new.ndst = dest;
11109 else
11110 new.ndst = (dest << 8) & 0xFF00;
11111
11112 /* set 'NV' to 'wakeup vector' */
11113 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonziniea37f612017-09-28 17:58:41 +020011114 } while (cmpxchg64(&pi_desc->control, old.control,
11115 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011116
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011117 /* We should not block the vCPU if an interrupt is posted for it. */
11118 if (pi_test_on(pi_desc) == 1)
11119 __pi_post_block(vcpu);
11120
11121 local_irq_enable();
11122 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080011123}
11124
Yunhong Jiangbc225122016-06-13 14:19:58 -070011125static int vmx_pre_block(struct kvm_vcpu *vcpu)
11126{
11127 if (pi_pre_block(vcpu))
11128 return 1;
11129
Yunhong Jiang64672c92016-06-13 14:19:59 -070011130 if (kvm_lapic_hv_timer_in_use(vcpu))
11131 kvm_lapic_switch_to_sw_timer(vcpu);
11132
Yunhong Jiangbc225122016-06-13 14:19:58 -070011133 return 0;
11134}
11135
11136static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011137{
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011138 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011139 return;
11140
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011141 WARN_ON(irqs_disabled());
11142 local_irq_disable();
Paolo Bonzini01c58b02017-06-06 12:57:04 +020011143 __pi_post_block(vcpu);
Paolo Bonziniff5eb8f2017-06-06 12:57:05 +020011144 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080011145}
11146
Yunhong Jiangbc225122016-06-13 14:19:58 -070011147static void vmx_post_block(struct kvm_vcpu *vcpu)
11148{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011149 if (kvm_x86_ops->set_hv_timer)
11150 kvm_lapic_switch_to_hv_timer(vcpu);
11151
Yunhong Jiangbc225122016-06-13 14:19:58 -070011152 pi_post_block(vcpu);
11153}
11154
Feng Wubf9f6ac2015-09-18 22:29:55 +080011155/*
Feng Wuefc64402015-09-18 22:29:51 +080011156 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11157 *
11158 * @kvm: kvm
11159 * @host_irq: host irq of the interrupt
11160 * @guest_irq: gsi of the interrupt
11161 * @set: set or unset PI
11162 * returns 0 on success, < 0 on failure
11163 */
11164static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11165 uint32_t guest_irq, bool set)
11166{
11167 struct kvm_kernel_irq_routing_entry *e;
11168 struct kvm_irq_routing_table *irq_rt;
11169 struct kvm_lapic_irq irq;
11170 struct kvm_vcpu *vcpu;
11171 struct vcpu_data vcpu_info;
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011172 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080011173
11174 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011175 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11176 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011177 return 0;
11178
11179 idx = srcu_read_lock(&kvm->irq_srcu);
11180 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3d4213f2017-09-07 19:02:30 +010011181 if (guest_irq >= irq_rt->nr_rt_entries ||
11182 hlist_empty(&irq_rt->map[guest_irq])) {
11183 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
11184 guest_irq, irq_rt->nr_rt_entries);
11185 goto out;
11186 }
Feng Wuefc64402015-09-18 22:29:51 +080011187
11188 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11189 if (e->type != KVM_IRQ_ROUTING_MSI)
11190 continue;
11191 /*
11192 * VT-d PI cannot support posting multicast/broadcast
11193 * interrupts to a vCPU, we still use interrupt remapping
11194 * for these kind of interrupts.
11195 *
11196 * For lowest-priority interrupts, we only support
11197 * those with single CPU as the destination, e.g. user
11198 * configures the interrupts via /proc/irq or uses
11199 * irqbalance to make the interrupts single-CPU.
11200 *
11201 * We will support full lowest-priority interrupt later.
11202 */
11203
Radim Krčmář371313132016-07-12 22:09:27 +020011204 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011205 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11206 /*
11207 * Make sure the IRTE is in remapped mode if
11208 * we don't handle it in posted mode.
11209 */
11210 ret = irq_set_vcpu_affinity(host_irq, NULL);
11211 if (ret < 0) {
11212 printk(KERN_INFO
11213 "failed to back to remapped mode, irq: %u\n",
11214 host_irq);
11215 goto out;
11216 }
11217
Feng Wuefc64402015-09-18 22:29:51 +080011218 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011219 }
Feng Wuefc64402015-09-18 22:29:51 +080011220
11221 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11222 vcpu_info.vector = irq.vector;
11223
Feng Wub6ce9782016-01-25 16:53:35 +080011224 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011225 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11226
11227 if (set)
11228 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhang0c4e39c2017-09-18 09:56:49 +080011229 else
Feng Wuefc64402015-09-18 22:29:51 +080011230 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080011231
11232 if (ret < 0) {
11233 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11234 __func__);
11235 goto out;
11236 }
11237 }
11238
11239 ret = 0;
11240out:
11241 srcu_read_unlock(&kvm->irq_srcu, idx);
11242 return ret;
11243}
11244
Ashok Rajc45dcc72016-06-22 14:59:56 +080011245static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11246{
11247 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11248 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11249 FEATURE_CONTROL_LMCE;
11250 else
11251 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11252 ~FEATURE_CONTROL_LMCE;
11253}
11254
Kees Cook404f6aa2016-08-08 16:29:06 -070011255static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011256 .cpu_has_kvm_support = cpu_has_kvm_support,
11257 .disabled_by_bios = vmx_disabled_by_bios,
11258 .hardware_setup = hardware_setup,
11259 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011260 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011261 .hardware_enable = hardware_enable,
11262 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011263 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020011264 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011265
11266 .vcpu_create = vmx_create_vcpu,
11267 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011268 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011269
Avi Kivity04d2cc72007-09-10 18:10:54 +030011270 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011271 .vcpu_load = vmx_vcpu_load,
11272 .vcpu_put = vmx_vcpu_put,
11273
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011274 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011275 .get_msr = vmx_get_msr,
11276 .set_msr = vmx_set_msr,
11277 .get_segment_base = vmx_get_segment_base,
11278 .get_segment = vmx_get_segment,
11279 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011280 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011281 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011282 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011283 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011284 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011285 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011286 .set_cr3 = vmx_set_cr3,
11287 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011288 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011289 .get_idt = vmx_get_idt,
11290 .set_idt = vmx_set_idt,
11291 .get_gdt = vmx_get_gdt,
11292 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011293 .get_dr6 = vmx_get_dr6,
11294 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011295 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011296 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011297 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011298 .get_rflags = vmx_get_rflags,
11299 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011300
11301 .get_pkru = vmx_get_pkru,
11302
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011303 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011304 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011305
11306 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011307
Avi Kivity6aa8b732006-12-10 02:21:36 -080011308 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011309 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011310 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011311 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11312 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011313 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011314 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011315 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011316 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011317 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011318 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011319 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011320 .get_nmi_mask = vmx_get_nmi_mask,
11321 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011322 .enable_nmi_window = enable_nmi_window,
11323 .enable_irq_window = enable_irq_window,
11324 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011325 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011326 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011327 .get_enable_apicv = vmx_get_enable_apicv,
11328 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011329 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11330 .hwapic_irr_update = vmx_hwapic_irr_update,
11331 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011332 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11333 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011334
Izik Eiduscbc94022007-10-25 00:29:55 +020011335 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011336 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011337 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011338
Avi Kivity586f9602010-11-18 13:09:54 +020011339 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011340
Sheng Yang17cc3932010-01-05 19:02:27 +080011341 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011342
11343 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011344
11345 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011346 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011347
11348 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011349
11350 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011351
11352 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011353
11354 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011355
11356 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011357 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011358 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011359 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011360
11361 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011362
11363 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011364
11365 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11366 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11367 .flush_log_dirty = vmx_flush_log_dirty,
11368 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011369
Feng Wubf9f6ac2015-09-18 22:29:55 +080011370 .pre_block = vmx_pre_block,
11371 .post_block = vmx_post_block,
11372
Wei Huang25462f72015-06-19 15:45:05 +020011373 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011374
11375 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011376
11377#ifdef CONFIG_X86_64
11378 .set_hv_timer = vmx_set_hv_timer,
11379 .cancel_hv_timer = vmx_cancel_hv_timer,
11380#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011381
11382 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011383};
11384
11385static int __init vmx_init(void)
11386{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011387 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11388 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011389 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011390 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011391
Dave Young2965faa2015-09-09 15:38:55 -070011392#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011393 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11394 crash_vmclear_local_loaded_vmcss);
11395#endif
11396
He, Qingfdef3ad2007-04-30 09:45:24 +030011397 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011398}
11399
11400static void __exit vmx_exit(void)
11401{
Dave Young2965faa2015-09-09 15:38:55 -070011402#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011403 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011404 synchronize_rcu();
11405#endif
11406
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011407 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011408}
11409
11410module_init(vmx_init)
11411module_exit(vmx_exit)