blob: 684e72d0c41bfe7fd47c83a082ba2d0d1f0f48b1 [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 Kiszkaf41245002014-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 Kiszkaf41245002014-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 Kiszkaf41245002014-03-07 20:03:13 +0100437
Radim Krčmářd048c092016-08-08 20:16:22 +0200438 unsigned long *msr_bitmap;
439
Jan Kiszkaf41245002014-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 Kivity313dbd492008-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;
Wanpeng Lic63e4562016-09-23 19:17:16 +0800927static unsigned long *vmx_msr_bitmap_legacy_x2apic_apicv;
928static unsigned long *vmx_msr_bitmap_longmode_x2apic_apicv;
Yang Zhang8d146952013-01-25 10:18:50 +0800929static unsigned long *vmx_msr_bitmap_legacy_x2apic;
930static unsigned long *vmx_msr_bitmap_longmode_x2apic;
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 Kiszkaf41245002014-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
Nadav Har'El644d7112011-05-25 23:12:35 +03001346static inline bool is_exception(u32 intr_info)
1347{
1348 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1349 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1350}
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
2170 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002171 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2172 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002173 return;
2174
2175 do {
2176 old.control = new.control = pi_desc->control;
2177
2178 /*
2179 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
2180 * are two possible cases:
2181 * 1. After running 'pre_block', context switch
2182 * happened. For this case, 'sn' was set in
2183 * vmx_vcpu_put(), so we need to clear it here.
2184 * 2. After running 'pre_block', we were blocked,
2185 * and woken up by some other guy. For this case,
2186 * we don't need to do anything, 'pi_post_block'
2187 * will do everything for us. However, we cannot
2188 * check whether it is case #1 or case #2 here
2189 * (maybe, not needed), so we also clear sn here,
2190 * I think it is not a big deal.
2191 */
2192 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
2193 if (vcpu->cpu != cpu) {
2194 dest = cpu_physical_id(cpu);
2195
2196 if (x2apic_enabled())
2197 new.ndst = dest;
2198 else
2199 new.ndst = (dest << 8) & 0xFF00;
2200 }
2201
2202 /* set 'NV' to 'notification vector' */
2203 new.nv = POSTED_INTR_VECTOR;
2204 }
2205
2206 /* Allow posting non-urgent interrupts */
2207 new.sn = 0;
2208 } while (cmpxchg(&pi_desc->control, old.control,
2209 new.control) != old.control);
2210}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002211
Peter Feinerc95ba922016-08-17 09:36:47 -07002212static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2213{
2214 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2215 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2216}
2217
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218/*
2219 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2220 * vcpu mutex is already taken.
2221 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002222static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002224 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002225 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002226 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002228 if (!vmm_exclusive)
2229 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002230 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002231 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002233 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002234 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002235 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002236
2237 /*
2238 * Read loaded_vmcs->cpu should be before fetching
2239 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2240 * See the comments in __loaded_vmcs_clear().
2241 */
2242 smp_rmb();
2243
Nadav Har'Eld462b812011-05-24 15:26:10 +03002244 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2245 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002246 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002247 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002248 }
2249
2250 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2251 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2252 vmcs_load(vmx->loaded_vmcs->vmcs);
2253 }
2254
2255 if (!already_loaded) {
2256 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2257 unsigned long sysenter_esp;
2258
2259 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002260
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 /*
2262 * Linux uses per-cpu TSS and GDT, so set these when switching
2263 * processors.
2264 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002265 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002266 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267
2268 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2269 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002270
Nadav Har'Eld462b812011-05-24 15:26:10 +03002271 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 }
Feng Wu28b835d2015-09-18 22:29:54 +08002273
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002274 /* Setup TSC multiplier */
2275 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002276 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2277 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002278
Feng Wu28b835d2015-09-18 22:29:54 +08002279 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002280 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002281}
2282
2283static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2284{
2285 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2286
2287 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002288 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2289 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002290 return;
2291
2292 /* Set SN when the vCPU is preempted */
2293 if (vcpu->preempted)
2294 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295}
2296
2297static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2298{
Feng Wu28b835d2015-09-18 22:29:54 +08002299 vmx_vcpu_pi_put(vcpu);
2300
Avi Kivitya9b21b62008-06-24 11:48:49 +03002301 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002302 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002303 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2304 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002305 kvm_cpu_vmxoff();
2306 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307}
2308
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002309static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2310{
Avi Kivity81231c62010-01-24 16:26:40 +02002311 ulong cr0;
2312
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002313 if (vcpu->fpu_active)
2314 return;
2315 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002316 cr0 = vmcs_readl(GUEST_CR0);
2317 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2318 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2319 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002320 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002321 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002322 if (is_guest_mode(vcpu))
2323 vcpu->arch.cr0_guest_owned_bits &=
2324 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002325 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002326}
2327
Avi Kivityedcafe32009-12-30 18:07:40 +02002328static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2329
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002330/*
2331 * Return the cr0 value that a nested guest would read. This is a combination
2332 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2333 * its hypervisor (cr0_read_shadow).
2334 */
2335static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2336{
2337 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2338 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2339}
2340static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2341{
2342 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2343 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2344}
2345
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002346static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2347{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002348 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2349 * set this *before* calling this function.
2350 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002351 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002352 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002353 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002354 vcpu->arch.cr0_guest_owned_bits = 0;
2355 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002356 if (is_guest_mode(vcpu)) {
2357 /*
2358 * L1's specified read shadow might not contain the TS bit,
2359 * so now that we turned on shadowing of this bit, we need to
2360 * set this bit of the shadow. Like in nested_vmx_run we need
2361 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2362 * up-to-date here because we just decached cr0.TS (and we'll
2363 * only update vmcs12->guest_cr0 on nested exit).
2364 */
2365 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2366 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2367 (vcpu->arch.cr0 & X86_CR0_TS);
2368 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2369 } else
2370 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002371}
2372
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2374{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002375 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002376
Avi Kivity6de12732011-03-07 12:51:22 +02002377 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2378 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2379 rflags = vmcs_readl(GUEST_RFLAGS);
2380 if (to_vmx(vcpu)->rmode.vm86_active) {
2381 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2382 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2383 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2384 }
2385 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002386 }
Avi Kivity6de12732011-03-07 12:51:22 +02002387 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388}
2389
2390static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2391{
Avi Kivity6de12732011-03-07 12:51:22 +02002392 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2393 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002394 if (to_vmx(vcpu)->rmode.vm86_active) {
2395 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002396 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002397 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 vmcs_writel(GUEST_RFLAGS, rflags);
2399}
2400
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002401static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2402{
2403 return to_vmx(vcpu)->guest_pkru;
2404}
2405
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002406static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002407{
2408 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2409 int ret = 0;
2410
2411 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002412 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002413 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002414 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002415
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002416 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002417}
2418
2419static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2420{
2421 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2422 u32 interruptibility = interruptibility_old;
2423
2424 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2425
Jan Kiszka48005f62010-02-19 19:38:07 +01002426 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002427 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002428 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002429 interruptibility |= GUEST_INTR_STATE_STI;
2430
2431 if ((interruptibility != interruptibility_old))
2432 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2433}
2434
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2436{
2437 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002439 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002441 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442
Glauber Costa2809f5d2009-05-12 16:21:05 -04002443 /* skipping an emulated instruction also counts */
2444 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445}
2446
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002447/*
2448 * KVM wants to inject page-faults which it got to the guest. This function
2449 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002450 */
Gleb Natapove011c662013-09-25 12:51:35 +03002451static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002452{
2453 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2454
Gleb Natapove011c662013-09-25 12:51:35 +03002455 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002456 return 0;
2457
Jan Kiszka533558b2014-01-04 18:47:20 +01002458 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2459 vmcs_read32(VM_EXIT_INTR_INFO),
2460 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002461 return 1;
2462}
2463
Avi Kivity298101d2007-11-25 13:41:11 +02002464static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002465 bool has_error_code, u32 error_code,
2466 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002467{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002468 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002469 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002470
Gleb Natapove011c662013-09-25 12:51:35 +03002471 if (!reinject && is_guest_mode(vcpu) &&
2472 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002473 return;
2474
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002475 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002476 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002477 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2478 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002479
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002480 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002481 int inc_eip = 0;
2482 if (kvm_exception_is_soft(nr))
2483 inc_eip = vcpu->arch.event_exit_inst_len;
2484 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002485 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002486 return;
2487 }
2488
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002489 if (kvm_exception_is_soft(nr)) {
2490 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2491 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002492 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2493 } else
2494 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2495
2496 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002497}
2498
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002499static bool vmx_rdtscp_supported(void)
2500{
2501 return cpu_has_vmx_rdtscp();
2502}
2503
Mao, Junjiead756a12012-07-02 01:18:48 +00002504static bool vmx_invpcid_supported(void)
2505{
2506 return cpu_has_vmx_invpcid() && enable_ept;
2507}
2508
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509/*
Eddie Donga75beee2007-05-17 18:55:15 +03002510 * Swap MSR entry in host/guest MSR entry array.
2511 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002512static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002513{
Avi Kivity26bb0982009-09-07 11:14:12 +03002514 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002515
2516 tmp = vmx->guest_msrs[to];
2517 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2518 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002519}
2520
Yang Zhang8d146952013-01-25 10:18:50 +08002521static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2522{
2523 unsigned long *msr_bitmap;
2524
Wincy Van670125b2015-03-04 14:31:56 +08002525 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002526 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002527 else if (cpu_has_secondary_exec_ctrls() &&
2528 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2529 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002530 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2531 if (is_long_mode(vcpu))
Wanpeng Lic63e4562016-09-23 19:17:16 +08002532 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2533 else
2534 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2535 } else {
2536 if (is_long_mode(vcpu))
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002537 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2538 else
2539 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002540 }
Yang Zhang8d146952013-01-25 10:18:50 +08002541 } else {
2542 if (is_long_mode(vcpu))
2543 msr_bitmap = vmx_msr_bitmap_longmode;
2544 else
2545 msr_bitmap = vmx_msr_bitmap_legacy;
2546 }
2547
2548 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2549}
2550
Eddie Donga75beee2007-05-17 18:55:15 +03002551/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002552 * Set up the vmcs to automatically save and restore system
2553 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2554 * mode, as fiddling with msrs is very expensive.
2555 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002556static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002557{
Avi Kivity26bb0982009-09-07 11:14:12 +03002558 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002559
Eddie Donga75beee2007-05-17 18:55:15 +03002560 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002561#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002562 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002563 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002564 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002565 move_msr_up(vmx, index, save_nmsrs++);
2566 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002567 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002568 move_msr_up(vmx, index, save_nmsrs++);
2569 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002570 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002571 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002572 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002573 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002574 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002575 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002576 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002577 * if efer.sce is enabled.
2578 */
Brian Gerst8c065852010-07-17 09:03:26 -04002579 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002580 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002581 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002582 }
Eddie Donga75beee2007-05-17 18:55:15 +03002583#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002584 index = __find_msr_index(vmx, MSR_EFER);
2585 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002586 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002587
Avi Kivity26bb0982009-09-07 11:14:12 +03002588 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002589
Yang Zhang8d146952013-01-25 10:18:50 +08002590 if (cpu_has_vmx_msr_bitmap())
2591 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002592}
2593
2594/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002596 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2597 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002599static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600{
2601 u64 host_tsc, tsc_offset;
2602
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002603 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002605 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606}
2607
2608/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002609 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002611static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002613 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002614 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002615 * We're here if L1 chose not to trap WRMSR to TSC. According
2616 * to the spec, this should set L1's TSC; The offset that L1
2617 * set for L2 remains unchanged, and still needs to be added
2618 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002619 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002620 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002621 /* recalculate vmcs02.TSC_OFFSET: */
2622 vmcs12 = get_vmcs12(vcpu);
2623 vmcs_write64(TSC_OFFSET, offset +
2624 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2625 vmcs12->tsc_offset : 0));
2626 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002627 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2628 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002629 vmcs_write64(TSC_OFFSET, offset);
2630 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631}
2632
Nadav Har'El801d3422011-05-25 23:02:23 +03002633static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2634{
2635 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2636 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2637}
2638
2639/*
2640 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2641 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2642 * all guests if the "nested" module option is off, and can also be disabled
2643 * for a single guest by disabling its VMX cpuid bit.
2644 */
2645static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2646{
2647 return nested && guest_cpuid_has_vmx(vcpu);
2648}
2649
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002651 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2652 * returned for the various VMX controls MSRs when nested VMX is enabled.
2653 * The same values should also be used to verify that vmcs12 control fields are
2654 * valid during nested entry from L1 to L2.
2655 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2656 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2657 * bit in the high half is on if the corresponding bit in the control field
2658 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002659 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002660static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002661{
2662 /*
2663 * Note that as a general rule, the high half of the MSRs (bits in
2664 * the control fields which may be 1) should be initialized by the
2665 * intersection of the underlying hardware's MSR (i.e., features which
2666 * can be supported) and the list of features we want to expose -
2667 * because they are known to be properly supported in our code.
2668 * Also, usually, the low half of the MSRs (bits which must be 1) can
2669 * be set to 0, meaning that L1 may turn off any of these bits. The
2670 * reason is that if one of these bits is necessary, it will appear
2671 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2672 * fields of vmcs01 and vmcs02, will turn these bits off - and
2673 * nested_vmx_exit_handled() will not pass related exits to L1.
2674 * These rules have exceptions below.
2675 */
2676
2677 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002678 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002679 vmx->nested.nested_vmx_pinbased_ctls_low,
2680 vmx->nested.nested_vmx_pinbased_ctls_high);
2681 vmx->nested.nested_vmx_pinbased_ctls_low |=
2682 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2683 vmx->nested.nested_vmx_pinbased_ctls_high &=
2684 PIN_BASED_EXT_INTR_MASK |
2685 PIN_BASED_NMI_EXITING |
2686 PIN_BASED_VIRTUAL_NMIS;
2687 vmx->nested.nested_vmx_pinbased_ctls_high |=
2688 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002689 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002690 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002691 vmx->nested.nested_vmx_pinbased_ctls_high |=
2692 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002693
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002694 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002695 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002696 vmx->nested.nested_vmx_exit_ctls_low,
2697 vmx->nested.nested_vmx_exit_ctls_high);
2698 vmx->nested.nested_vmx_exit_ctls_low =
2699 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002700
Wincy Vanb9c237b2015-02-03 23:56:30 +08002701 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002702#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002703 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002704#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002705 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002706 vmx->nested.nested_vmx_exit_ctls_high |=
2707 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002708 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002709 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2710
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002711 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002712 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002713
Jan Kiszka2996fca2014-06-16 13:59:43 +02002714 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002715 vmx->nested.nested_vmx_true_exit_ctls_low =
2716 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002717 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2718
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002719 /* entry controls */
2720 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002721 vmx->nested.nested_vmx_entry_ctls_low,
2722 vmx->nested.nested_vmx_entry_ctls_high);
2723 vmx->nested.nested_vmx_entry_ctls_low =
2724 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2725 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002726#ifdef CONFIG_X86_64
2727 VM_ENTRY_IA32E_MODE |
2728#endif
2729 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002730 vmx->nested.nested_vmx_entry_ctls_high |=
2731 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002732 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002733 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002734
Jan Kiszka2996fca2014-06-16 13:59:43 +02002735 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002736 vmx->nested.nested_vmx_true_entry_ctls_low =
2737 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002738 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2739
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002740 /* cpu-based controls */
2741 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002742 vmx->nested.nested_vmx_procbased_ctls_low,
2743 vmx->nested.nested_vmx_procbased_ctls_high);
2744 vmx->nested.nested_vmx_procbased_ctls_low =
2745 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2746 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002747 CPU_BASED_VIRTUAL_INTR_PENDING |
2748 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002749 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2750 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2751 CPU_BASED_CR3_STORE_EXITING |
2752#ifdef CONFIG_X86_64
2753 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2754#endif
2755 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002756 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2757 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2758 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2759 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002760 /*
2761 * We can allow some features even when not supported by the
2762 * hardware. For example, L1 can specify an MSR bitmap - and we
2763 * can use it to avoid exits to L1 - even when L0 runs L2
2764 * without MSR bitmaps.
2765 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002766 vmx->nested.nested_vmx_procbased_ctls_high |=
2767 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002768 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002769
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002770 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002771 vmx->nested.nested_vmx_true_procbased_ctls_low =
2772 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002773 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2774
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002775 /* secondary cpu-based controls */
2776 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002777 vmx->nested.nested_vmx_secondary_ctls_low,
2778 vmx->nested.nested_vmx_secondary_ctls_high);
2779 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2780 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002781 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002782 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002783 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002784 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002785 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002786 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002787 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002788 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002789
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002790 if (enable_ept) {
2791 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002792 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002793 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002794 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002795 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2796 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002797 if (cpu_has_vmx_ept_execute_only())
2798 vmx->nested.nested_vmx_ept_caps |=
2799 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002800 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002801 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2802 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002803 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002804 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002805
Paolo Bonzinief697a72016-03-18 16:58:38 +01002806 /*
2807 * Old versions of KVM use the single-context version without
2808 * checking for support, so declare that it is supported even
2809 * though it is treated as global context. The alternative is
2810 * not failing the single-context invvpid, and it is worse.
2811 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002812 if (enable_vpid)
2813 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Paolo Bonzinief697a72016-03-18 16:58:38 +01002814 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT |
Wanpeng Li089d7b62015-10-13 09:18:37 -07002815 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
2816 else
2817 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002818
Radim Krčmář0790ec12015-03-17 14:02:32 +01002819 if (enable_unrestricted_guest)
2820 vmx->nested.nested_vmx_secondary_ctls_high |=
2821 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2822
Jan Kiszkac18911a2013-03-13 16:06:41 +01002823 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002824 rdmsr(MSR_IA32_VMX_MISC,
2825 vmx->nested.nested_vmx_misc_low,
2826 vmx->nested.nested_vmx_misc_high);
2827 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2828 vmx->nested.nested_vmx_misc_low |=
2829 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002830 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002831 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002832}
2833
2834static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2835{
2836 /*
2837 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2838 */
2839 return ((control & high) | low) == control;
2840}
2841
2842static inline u64 vmx_control_msr(u32 low, u32 high)
2843{
2844 return low | ((u64)high << 32);
2845}
2846
Jan Kiszkacae50132014-01-04 18:47:22 +01002847/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002848static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2849{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002850 struct vcpu_vmx *vmx = to_vmx(vcpu);
2851
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002852 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002853 case MSR_IA32_VMX_BASIC:
2854 /*
2855 * This MSR reports some information about VMX support. We
2856 * should return information about the VMX we emulate for the
2857 * guest, and the VMCS structure we give it - not about the
2858 * VMX support of the underlying hardware.
2859 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002860 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002861 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2862 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002863 if (cpu_has_vmx_basic_inout())
2864 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002865 break;
2866 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2867 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002868 *pdata = vmx_control_msr(
2869 vmx->nested.nested_vmx_pinbased_ctls_low,
2870 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002871 break;
2872 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002873 *pdata = vmx_control_msr(
2874 vmx->nested.nested_vmx_true_procbased_ctls_low,
2875 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002876 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002877 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002878 *pdata = vmx_control_msr(
2879 vmx->nested.nested_vmx_procbased_ctls_low,
2880 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002881 break;
2882 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002883 *pdata = vmx_control_msr(
2884 vmx->nested.nested_vmx_true_exit_ctls_low,
2885 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002886 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002887 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002888 *pdata = vmx_control_msr(
2889 vmx->nested.nested_vmx_exit_ctls_low,
2890 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002891 break;
2892 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002893 *pdata = vmx_control_msr(
2894 vmx->nested.nested_vmx_true_entry_ctls_low,
2895 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002896 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002897 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002898 *pdata = vmx_control_msr(
2899 vmx->nested.nested_vmx_entry_ctls_low,
2900 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002901 break;
2902 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002903 *pdata = vmx_control_msr(
2904 vmx->nested.nested_vmx_misc_low,
2905 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002906 break;
2907 /*
2908 * These MSRs specify bits which the guest must keep fixed (on or off)
2909 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2910 * We picked the standard core2 setting.
2911 */
2912#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2913#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2914 case MSR_IA32_VMX_CR0_FIXED0:
2915 *pdata = VMXON_CR0_ALWAYSON;
2916 break;
2917 case MSR_IA32_VMX_CR0_FIXED1:
2918 *pdata = -1ULL;
2919 break;
2920 case MSR_IA32_VMX_CR4_FIXED0:
2921 *pdata = VMXON_CR4_ALWAYSON;
2922 break;
2923 case MSR_IA32_VMX_CR4_FIXED1:
2924 *pdata = -1ULL;
2925 break;
2926 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002927 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002928 break;
2929 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002930 *pdata = vmx_control_msr(
2931 vmx->nested.nested_vmx_secondary_ctls_low,
2932 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002933 break;
2934 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07002935 *pdata = vmx->nested.nested_vmx_ept_caps |
2936 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002937 break;
2938 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002939 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002940 }
2941
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002942 return 0;
2943}
2944
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002945static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2946 uint64_t val)
2947{
2948 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2949
2950 return !(val & ~valid_bits);
2951}
2952
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002953/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 * Reads an msr value (of 'msr_index') into 'pdata'.
2955 * Returns 0 on success, non-0 otherwise.
2956 * Assumes vcpu_load() was already called.
2957 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002958static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959{
Avi Kivity26bb0982009-09-07 11:14:12 +03002960 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002962 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002963#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002965 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966 break;
2967 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002968 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002970 case MSR_KERNEL_GS_BASE:
2971 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002972 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002973 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002974#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002976 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302977 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002978 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979 break;
2980 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002981 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982 break;
2983 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002984 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985 break;
2986 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002987 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002989 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002990 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002991 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002992 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002993 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002994 case MSR_IA32_MCG_EXT_CTL:
2995 if (!msr_info->host_initiated &&
2996 !(to_vmx(vcpu)->msr_ia32_feature_control &
2997 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01002998 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002999 msr_info->data = vcpu->arch.mcg_ext_ctl;
3000 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003001 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003002 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003003 break;
3004 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3005 if (!nested_vmx_allowed(vcpu))
3006 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003007 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003008 case MSR_IA32_XSS:
3009 if (!vmx_xsaves_supported())
3010 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003011 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003012 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003013 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003014 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003015 return 1;
3016 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003018 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003019 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003020 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003021 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003023 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024 }
3025
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 return 0;
3027}
3028
Jan Kiszkacae50132014-01-04 18:47:22 +01003029static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3030
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031/*
3032 * Writes msr value into into the appropriate "register".
3033 * Returns 0 on success, non-0 otherwise.
3034 * Assumes vcpu_load() was already called.
3035 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003036static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003038 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003039 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003040 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003041 u32 msr_index = msr_info->index;
3042 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003043
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003045 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003046 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003047 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003048#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003050 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051 vmcs_writel(GUEST_FS_BASE, data);
3052 break;
3053 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003054 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 vmcs_writel(GUEST_GS_BASE, data);
3056 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003057 case MSR_KERNEL_GS_BASE:
3058 vmx_load_host_state(vmx);
3059 vmx->msr_guest_kernel_gs_base = data;
3060 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061#endif
3062 case MSR_IA32_SYSENTER_CS:
3063 vmcs_write32(GUEST_SYSENTER_CS, data);
3064 break;
3065 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003066 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003067 break;
3068 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003069 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003071 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003072 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003073 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003074 vmcs_write64(GUEST_BNDCFGS, data);
3075 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303076 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003077 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003078 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003079 case MSR_IA32_CR_PAT:
3080 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003081 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3082 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003083 vmcs_write64(GUEST_IA32_PAT, data);
3084 vcpu->arch.pat = data;
3085 break;
3086 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003087 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003088 break;
Will Auldba904632012-11-29 12:42:50 -08003089 case MSR_IA32_TSC_ADJUST:
3090 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003091 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003092 case MSR_IA32_MCG_EXT_CTL:
3093 if ((!msr_info->host_initiated &&
3094 !(to_vmx(vcpu)->msr_ia32_feature_control &
3095 FEATURE_CONTROL_LMCE)) ||
3096 (data & ~MCG_EXT_CTL_LMCE_EN))
3097 return 1;
3098 vcpu->arch.mcg_ext_ctl = data;
3099 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003100 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003101 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003102 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003103 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3104 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003105 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003106 if (msr_info->host_initiated && data == 0)
3107 vmx_leave_nested(vcpu);
3108 break;
3109 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3110 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003111 case MSR_IA32_XSS:
3112 if (!vmx_xsaves_supported())
3113 return 1;
3114 /*
3115 * The only supported bit as of Skylake is bit 8, but
3116 * it is not supported on KVM.
3117 */
3118 if (data != 0)
3119 return 1;
3120 vcpu->arch.ia32_xss = data;
3121 if (vcpu->arch.ia32_xss != host_xss)
3122 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3123 vcpu->arch.ia32_xss, host_xss);
3124 else
3125 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3126 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003127 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003128 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003129 return 1;
3130 /* Check reserved bit, higher 32 bits should be zero */
3131 if ((data >> 32) != 0)
3132 return 1;
3133 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003135 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003136 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003137 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003138 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003139 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3140 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003141 ret = kvm_set_shared_msr(msr->index, msr->data,
3142 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003143 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003144 if (ret)
3145 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003146 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003147 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003149 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003150 }
3151
Eddie Dong2cc51562007-05-21 07:28:09 +03003152 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153}
3154
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003155static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003157 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3158 switch (reg) {
3159 case VCPU_REGS_RSP:
3160 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3161 break;
3162 case VCPU_REGS_RIP:
3163 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3164 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003165 case VCPU_EXREG_PDPTR:
3166 if (enable_ept)
3167 ept_save_pdptrs(vcpu);
3168 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003169 default:
3170 break;
3171 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172}
3173
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174static __init int cpu_has_kvm_support(void)
3175{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003176 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177}
3178
3179static __init int vmx_disabled_by_bios(void)
3180{
3181 u64 msr;
3182
3183 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003184 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003185 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003186 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3187 && tboot_enabled())
3188 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003189 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003190 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003191 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003192 && !tboot_enabled()) {
3193 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003194 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003195 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003196 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003197 /* launched w/o TXT and VMX disabled */
3198 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3199 && !tboot_enabled())
3200 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003201 }
3202
3203 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204}
3205
Dongxiao Xu7725b892010-05-11 18:29:38 +08003206static void kvm_cpu_vmxon(u64 addr)
3207{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003208 intel_pt_handle_vmx(1);
3209
Dongxiao Xu7725b892010-05-11 18:29:38 +08003210 asm volatile (ASM_VMX_VMXON_RAX
3211 : : "a"(&addr), "m"(addr)
3212 : "memory", "cc");
3213}
3214
Radim Krčmář13a34e02014-08-28 15:13:03 +02003215static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216{
3217 int cpu = raw_smp_processor_id();
3218 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003219 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003221 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003222 return -EBUSY;
3223
Nadav Har'Eld462b812011-05-24 15:26:10 +03003224 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003225 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3226 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003227
3228 /*
3229 * Now we can enable the vmclear operation in kdump
3230 * since the loaded_vmcss_on_cpu list on this cpu
3231 * has been initialized.
3232 *
3233 * Though the cpu is not in VMX operation now, there
3234 * is no problem to enable the vmclear operation
3235 * for the loaded_vmcss_on_cpu list is empty!
3236 */
3237 crash_enable_local_vmclear(cpu);
3238
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003240
3241 test_bits = FEATURE_CONTROL_LOCKED;
3242 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3243 if (tboot_enabled())
3244 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3245
3246 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003248 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3249 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003250 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003251
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003252 if (vmm_exclusive) {
3253 kvm_cpu_vmxon(phys_addr);
3254 ept_sync_global();
3255 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003256
Christoph Lameter89cbc762014-08-17 12:30:40 -05003257 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003258
Alexander Graf10474ae2009-09-15 11:37:46 +02003259 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260}
3261
Nadav Har'Eld462b812011-05-24 15:26:10 +03003262static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003263{
3264 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003265 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003266
Nadav Har'Eld462b812011-05-24 15:26:10 +03003267 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3268 loaded_vmcss_on_cpu_link)
3269 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003270}
3271
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003272
3273/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3274 * tricks.
3275 */
3276static void kvm_cpu_vmxoff(void)
3277{
3278 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003279
3280 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003281}
3282
Radim Krčmář13a34e02014-08-28 15:13:03 +02003283static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003285 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003286 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003287 kvm_cpu_vmxoff();
3288 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003289 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003290}
3291
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003292static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003293 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294{
3295 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003296 u32 ctl = ctl_min | ctl_opt;
3297
3298 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3299
3300 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3301 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3302
3303 /* Ensure minimum (required) set of control bits are supported. */
3304 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003305 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003306
3307 *result = ctl;
3308 return 0;
3309}
3310
Avi Kivity110312c2010-12-21 12:54:20 +02003311static __init bool allow_1_setting(u32 msr, u32 ctl)
3312{
3313 u32 vmx_msr_low, vmx_msr_high;
3314
3315 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3316 return vmx_msr_high & ctl;
3317}
3318
Yang, Sheng002c7f72007-07-31 14:23:01 +03003319static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003320{
3321 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003322 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003323 u32 _pin_based_exec_control = 0;
3324 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003325 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003326 u32 _vmexit_control = 0;
3327 u32 _vmentry_control = 0;
3328
Raghavendra K T10166742012-02-07 23:19:20 +05303329 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003330#ifdef CONFIG_X86_64
3331 CPU_BASED_CR8_LOAD_EXITING |
3332 CPU_BASED_CR8_STORE_EXITING |
3333#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003334 CPU_BASED_CR3_LOAD_EXITING |
3335 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003336 CPU_BASED_USE_IO_BITMAPS |
3337 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003338 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003339 CPU_BASED_MWAIT_EXITING |
3340 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003341 CPU_BASED_INVLPG_EXITING |
3342 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003343
Sheng Yangf78e0e22007-10-29 09:40:42 +08003344 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003345 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003346 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003347 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3348 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003349 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003350#ifdef CONFIG_X86_64
3351 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3352 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3353 ~CPU_BASED_CR8_STORE_EXITING;
3354#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003355 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003356 min2 = 0;
3357 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003358 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003359 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003360 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003361 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003362 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003363 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003364 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003365 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003366 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003367 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003368 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003369 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003370 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003371 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003372 if (adjust_vmx_controls(min2, opt2,
3373 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003374 &_cpu_based_2nd_exec_control) < 0)
3375 return -EIO;
3376 }
3377#ifndef CONFIG_X86_64
3378 if (!(_cpu_based_2nd_exec_control &
3379 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3380 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3381#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003382
3383 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3384 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003385 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003386 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3387 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003388
Sheng Yangd56f5462008-04-25 10:13:16 +08003389 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003390 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3391 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003392 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3393 CPU_BASED_CR3_STORE_EXITING |
3394 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003395 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3396 vmx_capability.ept, vmx_capability.vpid);
3397 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003398
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003399 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003400#ifdef CONFIG_X86_64
3401 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3402#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003403 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003404 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003405 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3406 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003407 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003408
Yang Zhang01e439b2013-04-11 19:25:12 +08003409 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003410 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3411 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003412 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3413 &_pin_based_exec_control) < 0)
3414 return -EIO;
3415
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003416 if (cpu_has_broken_vmx_preemption_timer())
3417 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003418 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003419 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003420 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3421
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003422 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003423 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003424 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3425 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003426 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003428 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003429
3430 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3431 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003432 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003433
3434#ifdef CONFIG_X86_64
3435 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3436 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003437 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003438#endif
3439
3440 /* Require Write-Back (WB) memory type for VMCS accesses. */
3441 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003442 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003443
Yang, Sheng002c7f72007-07-31 14:23:01 +03003444 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003445 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003446 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003447 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003448
Yang, Sheng002c7f72007-07-31 14:23:01 +03003449 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3450 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003451 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003452 vmcs_conf->vmexit_ctrl = _vmexit_control;
3453 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003454
Avi Kivity110312c2010-12-21 12:54:20 +02003455 cpu_has_load_ia32_efer =
3456 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3457 VM_ENTRY_LOAD_IA32_EFER)
3458 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3459 VM_EXIT_LOAD_IA32_EFER);
3460
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003461 cpu_has_load_perf_global_ctrl =
3462 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3463 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3464 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3465 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3466
3467 /*
3468 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003469 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003470 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3471 *
3472 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3473 *
3474 * AAK155 (model 26)
3475 * AAP115 (model 30)
3476 * AAT100 (model 37)
3477 * BC86,AAY89,BD102 (model 44)
3478 * BA97 (model 46)
3479 *
3480 */
3481 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3482 switch (boot_cpu_data.x86_model) {
3483 case 26:
3484 case 30:
3485 case 37:
3486 case 44:
3487 case 46:
3488 cpu_has_load_perf_global_ctrl = false;
3489 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3490 "does not work properly. Using workaround\n");
3491 break;
3492 default:
3493 break;
3494 }
3495 }
3496
Borislav Petkov782511b2016-04-04 22:25:03 +02003497 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003498 rdmsrl(MSR_IA32_XSS, host_xss);
3499
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003500 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003501}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502
3503static struct vmcs *alloc_vmcs_cpu(int cpu)
3504{
3505 int node = cpu_to_node(cpu);
3506 struct page *pages;
3507 struct vmcs *vmcs;
3508
Vlastimil Babka96db8002015-09-08 15:03:50 -07003509 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510 if (!pages)
3511 return NULL;
3512 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003513 memset(vmcs, 0, vmcs_config.size);
3514 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515 return vmcs;
3516}
3517
3518static struct vmcs *alloc_vmcs(void)
3519{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003520 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521}
3522
3523static void free_vmcs(struct vmcs *vmcs)
3524{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003525 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526}
3527
Nadav Har'Eld462b812011-05-24 15:26:10 +03003528/*
3529 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3530 */
3531static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3532{
3533 if (!loaded_vmcs->vmcs)
3534 return;
3535 loaded_vmcs_clear(loaded_vmcs);
3536 free_vmcs(loaded_vmcs->vmcs);
3537 loaded_vmcs->vmcs = NULL;
Jim Mattson355f4fb2016-10-28 08:29:39 -07003538 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003539}
3540
Sam Ravnborg39959582007-06-01 00:47:13 -07003541static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003542{
3543 int cpu;
3544
Zachary Amsden3230bb42009-09-29 11:38:37 -10003545 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003546 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003547 per_cpu(vmxarea, cpu) = NULL;
3548 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003549}
3550
Bandan Dasfe2b2012014-04-21 15:20:14 -04003551static void init_vmcs_shadow_fields(void)
3552{
3553 int i, j;
3554
3555 /* No checks for read only fields yet */
3556
3557 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3558 switch (shadow_read_write_fields[i]) {
3559 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003560 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003561 continue;
3562 break;
3563 default:
3564 break;
3565 }
3566
3567 if (j < i)
3568 shadow_read_write_fields[j] =
3569 shadow_read_write_fields[i];
3570 j++;
3571 }
3572 max_shadow_read_write_fields = j;
3573
3574 /* shadowed fields guest access without vmexit */
3575 for (i = 0; i < max_shadow_read_write_fields; i++) {
3576 clear_bit(shadow_read_write_fields[i],
3577 vmx_vmwrite_bitmap);
3578 clear_bit(shadow_read_write_fields[i],
3579 vmx_vmread_bitmap);
3580 }
3581 for (i = 0; i < max_shadow_read_only_fields; i++)
3582 clear_bit(shadow_read_only_fields[i],
3583 vmx_vmread_bitmap);
3584}
3585
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586static __init int alloc_kvm_area(void)
3587{
3588 int cpu;
3589
Zachary Amsden3230bb42009-09-29 11:38:37 -10003590 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003591 struct vmcs *vmcs;
3592
3593 vmcs = alloc_vmcs_cpu(cpu);
3594 if (!vmcs) {
3595 free_kvm_area();
3596 return -ENOMEM;
3597 }
3598
3599 per_cpu(vmxarea, cpu) = vmcs;
3600 }
3601 return 0;
3602}
3603
Gleb Natapov14168782013-01-21 15:36:49 +02003604static bool emulation_required(struct kvm_vcpu *vcpu)
3605{
3606 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3607}
3608
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003609static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003610 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003611{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003612 if (!emulate_invalid_guest_state) {
3613 /*
3614 * CS and SS RPL should be equal during guest entry according
3615 * to VMX spec, but in reality it is not always so. Since vcpu
3616 * is in the middle of the transition from real mode to
3617 * protected mode it is safe to assume that RPL 0 is a good
3618 * default value.
3619 */
3620 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003621 save->selector &= ~SEGMENT_RPL_MASK;
3622 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003623 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003624 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003625 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003626}
3627
3628static void enter_pmode(struct kvm_vcpu *vcpu)
3629{
3630 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003631 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003632
Gleb Natapovd99e4152012-12-20 16:57:45 +02003633 /*
3634 * Update real mode segment cache. It may be not up-to-date if sement
3635 * register was written while vcpu was in a guest mode.
3636 */
3637 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3638 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3639 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3640 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3641 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3642 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3643
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003644 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645
Avi Kivity2fb92db2011-04-27 19:42:18 +03003646 vmx_segment_cache_clear(vmx);
3647
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003648 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003649
3650 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003651 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3652 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003653 vmcs_writel(GUEST_RFLAGS, flags);
3654
Rusty Russell66aee912007-07-17 23:34:16 +10003655 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3656 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657
3658 update_exception_bitmap(vcpu);
3659
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003660 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3661 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3662 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3663 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3664 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3665 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003666}
3667
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003668static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669{
Mathias Krause772e0312012-08-30 01:30:19 +02003670 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003671 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003672
Gleb Natapovd99e4152012-12-20 16:57:45 +02003673 var.dpl = 0x3;
3674 if (seg == VCPU_SREG_CS)
3675 var.type = 0x3;
3676
3677 if (!emulate_invalid_guest_state) {
3678 var.selector = var.base >> 4;
3679 var.base = var.base & 0xffff0;
3680 var.limit = 0xffff;
3681 var.g = 0;
3682 var.db = 0;
3683 var.present = 1;
3684 var.s = 1;
3685 var.l = 0;
3686 var.unusable = 0;
3687 var.type = 0x3;
3688 var.avl = 0;
3689 if (save->base & 0xf)
3690 printk_once(KERN_WARNING "kvm: segment base is not "
3691 "paragraph aligned when entering "
3692 "protected mode (seg=%d)", seg);
3693 }
3694
3695 vmcs_write16(sf->selector, var.selector);
3696 vmcs_write32(sf->base, var.base);
3697 vmcs_write32(sf->limit, var.limit);
3698 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003699}
3700
3701static void enter_rmode(struct kvm_vcpu *vcpu)
3702{
3703 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003704 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003705
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003706 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3707 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3708 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3709 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3710 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003711 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3712 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003713
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003714 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003715
Gleb Natapov776e58e2011-03-13 12:34:27 +02003716 /*
3717 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003718 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003719 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003720 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003721 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3722 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003723
Avi Kivity2fb92db2011-04-27 19:42:18 +03003724 vmx_segment_cache_clear(vmx);
3725
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003726 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3729
3730 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003731 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003732
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003733 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734
3735 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003736 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003737 update_exception_bitmap(vcpu);
3738
Gleb Natapovd99e4152012-12-20 16:57:45 +02003739 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3740 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3741 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3742 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3743 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3744 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003745
Eddie Dong8668a3c2007-10-10 14:26:45 +08003746 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003747}
3748
Amit Shah401d10d2009-02-20 22:53:37 +05303749static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3750{
3751 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003752 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3753
3754 if (!msr)
3755 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303756
Avi Kivity44ea2b12009-09-06 15:55:37 +03003757 /*
3758 * Force kernel_gs_base reloading before EFER changes, as control
3759 * of this msr depends on is_long_mode().
3760 */
3761 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003762 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303763 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003764 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303765 msr->data = efer;
3766 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003767 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303768
3769 msr->data = efer & ~EFER_LME;
3770 }
3771 setup_msrs(vmx);
3772}
3773
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003774#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775
3776static void enter_lmode(struct kvm_vcpu *vcpu)
3777{
3778 u32 guest_tr_ar;
3779
Avi Kivity2fb92db2011-04-27 19:42:18 +03003780 vmx_segment_cache_clear(to_vmx(vcpu));
3781
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003783 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003784 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3785 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003787 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3788 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003789 }
Avi Kivityda38f432010-07-06 11:30:49 +03003790 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791}
3792
3793static void exit_lmode(struct kvm_vcpu *vcpu)
3794{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003795 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003796 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797}
3798
3799#endif
3800
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003801static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003802{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003803 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003804 if (enable_ept) {
3805 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3806 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003807 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003808 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003809}
3810
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003811static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3812{
3813 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3814}
3815
Avi Kivitye8467fd2009-12-29 18:43:06 +02003816static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3817{
3818 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3819
3820 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3821 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3822}
3823
Avi Kivityaff48ba2010-12-05 18:56:11 +02003824static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3825{
3826 if (enable_ept && is_paging(vcpu))
3827 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3828 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3829}
3830
Anthony Liguori25c4c272007-04-27 09:29:21 +03003831static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003832{
Avi Kivityfc78f512009-12-07 12:16:48 +02003833 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3834
3835 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3836 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003837}
3838
Sheng Yang14394422008-04-28 12:24:45 +08003839static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3840{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003841 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3842
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003843 if (!test_bit(VCPU_EXREG_PDPTR,
3844 (unsigned long *)&vcpu->arch.regs_dirty))
3845 return;
3846
Sheng Yang14394422008-04-28 12:24:45 +08003847 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003848 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3849 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3850 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3851 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003852 }
3853}
3854
Avi Kivity8f5d5492009-05-31 18:41:29 +03003855static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3856{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003857 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3858
Avi Kivity8f5d5492009-05-31 18:41:29 +03003859 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003860 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3861 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3862 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3863 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003864 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003865
3866 __set_bit(VCPU_EXREG_PDPTR,
3867 (unsigned long *)&vcpu->arch.regs_avail);
3868 __set_bit(VCPU_EXREG_PDPTR,
3869 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003870}
3871
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003872static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003873
3874static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3875 unsigned long cr0,
3876 struct kvm_vcpu *vcpu)
3877{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003878 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3879 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003880 if (!(cr0 & X86_CR0_PG)) {
3881 /* From paging/starting to nonpaging */
3882 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003883 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003884 (CPU_BASED_CR3_LOAD_EXITING |
3885 CPU_BASED_CR3_STORE_EXITING));
3886 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003887 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003888 } else if (!is_paging(vcpu)) {
3889 /* From nonpaging to paging */
3890 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003891 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003892 ~(CPU_BASED_CR3_LOAD_EXITING |
3893 CPU_BASED_CR3_STORE_EXITING));
3894 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003895 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003896 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003897
3898 if (!(cr0 & X86_CR0_WP))
3899 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003900}
3901
Avi Kivity6aa8b732006-12-10 02:21:36 -08003902static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3903{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003904 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003905 unsigned long hw_cr0;
3906
Gleb Natapov50378782013-02-04 16:00:28 +02003907 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003908 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003909 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003910 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003911 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003912
Gleb Natapov218e7632013-01-21 15:36:45 +02003913 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3914 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915
Gleb Natapov218e7632013-01-21 15:36:45 +02003916 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3917 enter_rmode(vcpu);
3918 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003920#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003921 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003922 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003924 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925 exit_lmode(vcpu);
3926 }
3927#endif
3928
Avi Kivity089d0342009-03-23 18:26:32 +02003929 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003930 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3931
Avi Kivity02daab22009-12-30 12:40:26 +02003932 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003933 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003934
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003936 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003937 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003938
3939 /* depends on vcpu->arch.cr0 to be set to a new value */
3940 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003941}
3942
Sheng Yang14394422008-04-28 12:24:45 +08003943static u64 construct_eptp(unsigned long root_hpa)
3944{
3945 u64 eptp;
3946
3947 /* TODO write the value reading from MSR */
3948 eptp = VMX_EPT_DEFAULT_MT |
3949 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003950 if (enable_ept_ad_bits)
3951 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003952 eptp |= (root_hpa & PAGE_MASK);
3953
3954 return eptp;
3955}
3956
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3958{
Sheng Yang14394422008-04-28 12:24:45 +08003959 unsigned long guest_cr3;
3960 u64 eptp;
3961
3962 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003963 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003964 eptp = construct_eptp(cr3);
3965 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003966 if (is_paging(vcpu) || is_guest_mode(vcpu))
3967 guest_cr3 = kvm_read_cr3(vcpu);
3968 else
3969 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003970 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003971 }
3972
Sheng Yang2384d2b2008-01-17 15:14:33 +08003973 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003974 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975}
3976
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003977static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978{
Ben Serebrin085e68e2015-04-16 11:58:05 -07003979 /*
3980 * Pass through host's Machine Check Enable value to hw_cr4, which
3981 * is in force while we are in guest mode. Do not let guests control
3982 * this bit, even if host CR4.MCE == 0.
3983 */
3984 unsigned long hw_cr4 =
3985 (cr4_read_shadow() & X86_CR4_MCE) |
3986 (cr4 & ~X86_CR4_MCE) |
3987 (to_vmx(vcpu)->rmode.vm86_active ?
3988 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08003989
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003990 if (cr4 & X86_CR4_VMXE) {
3991 /*
3992 * To use VMXON (and later other VMX instructions), a guest
3993 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3994 * So basically the check on whether to allow nested VMX
3995 * is here.
3996 */
3997 if (!nested_vmx_allowed(vcpu))
3998 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003999 }
4000 if (to_vmx(vcpu)->nested.vmxon &&
4001 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004002 return 1;
4003
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004004 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004005 if (enable_ept) {
4006 if (!is_paging(vcpu)) {
4007 hw_cr4 &= ~X86_CR4_PAE;
4008 hw_cr4 |= X86_CR4_PSE;
4009 } else if (!(cr4 & X86_CR4_PAE)) {
4010 hw_cr4 &= ~X86_CR4_PAE;
4011 }
4012 }
Sheng Yang14394422008-04-28 12:24:45 +08004013
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004014 if (!enable_unrestricted_guest && !is_paging(vcpu))
4015 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004016 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4017 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4018 * to be manually disabled when guest switches to non-paging
4019 * mode.
4020 *
4021 * If !enable_unrestricted_guest, the CPU is always running
4022 * with CR0.PG=1 and CR4 needs to be modified.
4023 * If enable_unrestricted_guest, the CPU automatically
4024 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004025 */
Huaitong Handdba2622016-03-22 16:51:15 +08004026 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004027
Sheng Yang14394422008-04-28 12:24:45 +08004028 vmcs_writel(CR4_READ_SHADOW, cr4);
4029 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004030 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004031}
4032
Avi Kivity6aa8b732006-12-10 02:21:36 -08004033static void vmx_get_segment(struct kvm_vcpu *vcpu,
4034 struct kvm_segment *var, int seg)
4035{
Avi Kivitya9179492011-01-03 14:28:52 +02004036 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004037 u32 ar;
4038
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004039 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004040 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004041 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004042 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004043 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004044 var->base = vmx_read_guest_seg_base(vmx, seg);
4045 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4046 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004047 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004048 var->base = vmx_read_guest_seg_base(vmx, seg);
4049 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4050 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4051 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004052 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053 var->type = ar & 15;
4054 var->s = (ar >> 4) & 1;
4055 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004056 /*
4057 * Some userspaces do not preserve unusable property. Since usable
4058 * segment has to be present according to VMX spec we can use present
4059 * property to amend userspace bug by making unusable segment always
4060 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4061 * segment as unusable.
4062 */
4063 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 var->avl = (ar >> 12) & 1;
4065 var->l = (ar >> 13) & 1;
4066 var->db = (ar >> 14) & 1;
4067 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068}
4069
Avi Kivitya9179492011-01-03 14:28:52 +02004070static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4071{
Avi Kivitya9179492011-01-03 14:28:52 +02004072 struct kvm_segment s;
4073
4074 if (to_vmx(vcpu)->rmode.vm86_active) {
4075 vmx_get_segment(vcpu, &s, seg);
4076 return s.base;
4077 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004078 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004079}
4080
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004081static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004082{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004083 struct vcpu_vmx *vmx = to_vmx(vcpu);
4084
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004085 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004086 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004087 else {
4088 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004089 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004090 }
Avi Kivity69c73022011-03-07 15:26:44 +02004091}
4092
Avi Kivity653e3102007-05-07 10:55:37 +03004093static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004094{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 u32 ar;
4096
Avi Kivityf0495f92012-06-07 17:06:10 +03004097 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 ar = 1 << 16;
4099 else {
4100 ar = var->type & 15;
4101 ar |= (var->s & 1) << 4;
4102 ar |= (var->dpl & 3) << 5;
4103 ar |= (var->present & 1) << 7;
4104 ar |= (var->avl & 1) << 12;
4105 ar |= (var->l & 1) << 13;
4106 ar |= (var->db & 1) << 14;
4107 ar |= (var->g & 1) << 15;
4108 }
Avi Kivity653e3102007-05-07 10:55:37 +03004109
4110 return ar;
4111}
4112
4113static void vmx_set_segment(struct kvm_vcpu *vcpu,
4114 struct kvm_segment *var, int seg)
4115{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004116 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004117 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004118
Avi Kivity2fb92db2011-04-27 19:42:18 +03004119 vmx_segment_cache_clear(vmx);
4120
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004121 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4122 vmx->rmode.segs[seg] = *var;
4123 if (seg == VCPU_SREG_TR)
4124 vmcs_write16(sf->selector, var->selector);
4125 else if (var->s)
4126 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004127 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004128 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004129
Avi Kivity653e3102007-05-07 10:55:37 +03004130 vmcs_writel(sf->base, var->base);
4131 vmcs_write32(sf->limit, var->limit);
4132 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004133
4134 /*
4135 * Fix the "Accessed" bit in AR field of segment registers for older
4136 * qemu binaries.
4137 * IA32 arch specifies that at the time of processor reset the
4138 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004139 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004140 * state vmexit when "unrestricted guest" mode is turned on.
4141 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4142 * tree. Newer qemu binaries with that qemu fix would not need this
4143 * kvm hack.
4144 */
4145 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004146 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004147
Gleb Natapovf924d662012-12-12 19:10:55 +02004148 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004149
4150out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004151 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152}
4153
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4155{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004156 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157
4158 *db = (ar >> 14) & 1;
4159 *l = (ar >> 13) & 1;
4160}
4161
Gleb Natapov89a27f42010-02-16 10:51:48 +02004162static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004164 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4165 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166}
4167
Gleb Natapov89a27f42010-02-16 10:51:48 +02004168static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004170 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4171 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172}
4173
Gleb Natapov89a27f42010-02-16 10:51:48 +02004174static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004176 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4177 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178}
4179
Gleb Natapov89a27f42010-02-16 10:51:48 +02004180static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004182 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4183 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184}
4185
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004186static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4187{
4188 struct kvm_segment var;
4189 u32 ar;
4190
4191 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004192 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004193 if (seg == VCPU_SREG_CS)
4194 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004195 ar = vmx_segment_access_rights(&var);
4196
4197 if (var.base != (var.selector << 4))
4198 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004199 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004200 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004201 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004202 return false;
4203
4204 return true;
4205}
4206
4207static bool code_segment_valid(struct kvm_vcpu *vcpu)
4208{
4209 struct kvm_segment cs;
4210 unsigned int cs_rpl;
4211
4212 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004213 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004214
Avi Kivity1872a3f2009-01-04 23:26:52 +02004215 if (cs.unusable)
4216 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004217 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004218 return false;
4219 if (!cs.s)
4220 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004221 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004222 if (cs.dpl > cs_rpl)
4223 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004224 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004225 if (cs.dpl != cs_rpl)
4226 return false;
4227 }
4228 if (!cs.present)
4229 return false;
4230
4231 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4232 return true;
4233}
4234
4235static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4236{
4237 struct kvm_segment ss;
4238 unsigned int ss_rpl;
4239
4240 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004241 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004242
Avi Kivity1872a3f2009-01-04 23:26:52 +02004243 if (ss.unusable)
4244 return true;
4245 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004246 return false;
4247 if (!ss.s)
4248 return false;
4249 if (ss.dpl != ss_rpl) /* DPL != RPL */
4250 return false;
4251 if (!ss.present)
4252 return false;
4253
4254 return true;
4255}
4256
4257static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4258{
4259 struct kvm_segment var;
4260 unsigned int rpl;
4261
4262 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004263 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004264
Avi Kivity1872a3f2009-01-04 23:26:52 +02004265 if (var.unusable)
4266 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004267 if (!var.s)
4268 return false;
4269 if (!var.present)
4270 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004271 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004272 if (var.dpl < rpl) /* DPL < RPL */
4273 return false;
4274 }
4275
4276 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4277 * rights flags
4278 */
4279 return true;
4280}
4281
4282static bool tr_valid(struct kvm_vcpu *vcpu)
4283{
4284 struct kvm_segment tr;
4285
4286 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4287
Avi Kivity1872a3f2009-01-04 23:26:52 +02004288 if (tr.unusable)
4289 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004290 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004291 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004292 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004293 return false;
4294 if (!tr.present)
4295 return false;
4296
4297 return true;
4298}
4299
4300static bool ldtr_valid(struct kvm_vcpu *vcpu)
4301{
4302 struct kvm_segment ldtr;
4303
4304 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4305
Avi Kivity1872a3f2009-01-04 23:26:52 +02004306 if (ldtr.unusable)
4307 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004308 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004309 return false;
4310 if (ldtr.type != 2)
4311 return false;
4312 if (!ldtr.present)
4313 return false;
4314
4315 return true;
4316}
4317
4318static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4319{
4320 struct kvm_segment cs, ss;
4321
4322 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4323 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4324
Nadav Amitb32a9912015-03-29 16:33:04 +03004325 return ((cs.selector & SEGMENT_RPL_MASK) ==
4326 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004327}
4328
4329/*
4330 * Check if guest state is valid. Returns true if valid, false if
4331 * not.
4332 * We assume that registers are always usable
4333 */
4334static bool guest_state_valid(struct kvm_vcpu *vcpu)
4335{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004336 if (enable_unrestricted_guest)
4337 return true;
4338
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004339 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004340 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004341 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4342 return false;
4343 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4344 return false;
4345 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4346 return false;
4347 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4348 return false;
4349 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4350 return false;
4351 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4352 return false;
4353 } else {
4354 /* protected mode guest state checks */
4355 if (!cs_ss_rpl_check(vcpu))
4356 return false;
4357 if (!code_segment_valid(vcpu))
4358 return false;
4359 if (!stack_segment_valid(vcpu))
4360 return false;
4361 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4362 return false;
4363 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4364 return false;
4365 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4366 return false;
4367 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4368 return false;
4369 if (!tr_valid(vcpu))
4370 return false;
4371 if (!ldtr_valid(vcpu))
4372 return false;
4373 }
4374 /* TODO:
4375 * - Add checks on RIP
4376 * - Add checks on RFLAGS
4377 */
4378
4379 return true;
4380}
4381
Mike Dayd77c26f2007-10-08 09:02:08 -04004382static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004383{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004384 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004385 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004386 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004388 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004389 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004390 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4391 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004392 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004393 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004394 r = kvm_write_guest_page(kvm, fn++, &data,
4395 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004396 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004397 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004398 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4399 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 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004405 r = kvm_write_guest_page(kvm, fn, &data,
4406 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4407 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004408out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004409 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004410 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004411}
4412
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004413static int init_rmode_identity_map(struct kvm *kvm)
4414{
Tang Chenf51770e2014-09-16 18:41:59 +08004415 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004416 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004417 u32 tmp;
4418
Avi Kivity089d0342009-03-23 18:26:32 +02004419 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004420 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004421
4422 /* Protect kvm->arch.ept_identity_pagetable_done. */
4423 mutex_lock(&kvm->slots_lock);
4424
Tang Chenf51770e2014-09-16 18:41:59 +08004425 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004426 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004427
Sheng Yangb927a3c2009-07-21 10:42:48 +08004428 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004429
4430 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004431 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004432 goto out2;
4433
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004434 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004435 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4436 if (r < 0)
4437 goto out;
4438 /* Set up identity-mapping pagetable for EPT in real mode */
4439 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4440 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4441 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4442 r = kvm_write_guest_page(kvm, identity_map_pfn,
4443 &tmp, i * sizeof(tmp), sizeof(tmp));
4444 if (r < 0)
4445 goto out;
4446 }
4447 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004448
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004449out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004450 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004451
4452out2:
4453 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004454 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004455}
4456
Avi Kivity6aa8b732006-12-10 02:21:36 -08004457static void seg_setup(int seg)
4458{
Mathias Krause772e0312012-08-30 01:30:19 +02004459 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004460 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004461
4462 vmcs_write16(sf->selector, 0);
4463 vmcs_writel(sf->base, 0);
4464 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004465 ar = 0x93;
4466 if (seg == VCPU_SREG_CS)
4467 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004468
4469 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470}
4471
Sheng Yangf78e0e22007-10-29 09:40:42 +08004472static int alloc_apic_access_page(struct kvm *kvm)
4473{
Xiao Guangrong44841412012-09-07 14:14:20 +08004474 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004475 int r = 0;
4476
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004477 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004478 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004479 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004480 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4481 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004482 if (r)
4483 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004484
Tang Chen73a6d942014-09-11 13:38:00 +08004485 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004486 if (is_error_page(page)) {
4487 r = -EFAULT;
4488 goto out;
4489 }
4490
Tang Chenc24ae0d2014-09-24 15:57:58 +08004491 /*
4492 * Do not pin the page in memory, so that memory hot-unplug
4493 * is able to migrate it.
4494 */
4495 put_page(page);
4496 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004497out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004498 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004499 return r;
4500}
4501
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004502static int alloc_identity_pagetable(struct kvm *kvm)
4503{
Tang Chena255d472014-09-16 18:41:58 +08004504 /* Called with kvm->slots_lock held. */
4505
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004506 int r = 0;
4507
Tang Chena255d472014-09-16 18:41:58 +08004508 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4509
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004510 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4511 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004512
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004513 return r;
4514}
4515
Wanpeng Li991e7a02015-09-16 17:30:05 +08004516static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004517{
4518 int vpid;
4519
Avi Kivity919818a2009-03-23 18:01:29 +02004520 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004521 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004522 spin_lock(&vmx_vpid_lock);
4523 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004524 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004525 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004526 else
4527 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004528 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004529 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004530}
4531
Wanpeng Li991e7a02015-09-16 17:30:05 +08004532static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004533{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004534 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004535 return;
4536 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004537 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004538 spin_unlock(&vmx_vpid_lock);
4539}
4540
Yang Zhang8d146952013-01-25 10:18:50 +08004541#define MSR_TYPE_R 1
4542#define MSR_TYPE_W 2
4543static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4544 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004545{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004546 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004547
4548 if (!cpu_has_vmx_msr_bitmap())
4549 return;
4550
4551 /*
4552 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4553 * have the write-low and read-high bitmap offsets the wrong way round.
4554 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4555 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004556 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004557 if (type & MSR_TYPE_R)
4558 /* read-low */
4559 __clear_bit(msr, msr_bitmap + 0x000 / f);
4560
4561 if (type & MSR_TYPE_W)
4562 /* write-low */
4563 __clear_bit(msr, msr_bitmap + 0x800 / f);
4564
Sheng Yang25c5f222008-03-28 13:18:56 +08004565 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4566 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004567 if (type & MSR_TYPE_R)
4568 /* read-high */
4569 __clear_bit(msr, msr_bitmap + 0x400 / f);
4570
4571 if (type & MSR_TYPE_W)
4572 /* write-high */
4573 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4574
4575 }
4576}
4577
Wincy Vanf2b93282015-02-03 23:56:03 +08004578/*
4579 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4580 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4581 */
4582static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4583 unsigned long *msr_bitmap_nested,
4584 u32 msr, int type)
4585{
4586 int f = sizeof(unsigned long);
4587
4588 if (!cpu_has_vmx_msr_bitmap()) {
4589 WARN_ON(1);
4590 return;
4591 }
4592
4593 /*
4594 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4595 * have the write-low and read-high bitmap offsets the wrong way round.
4596 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4597 */
4598 if (msr <= 0x1fff) {
4599 if (type & MSR_TYPE_R &&
4600 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4601 /* read-low */
4602 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4603
4604 if (type & MSR_TYPE_W &&
4605 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4606 /* write-low */
4607 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4608
4609 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4610 msr &= 0x1fff;
4611 if (type & MSR_TYPE_R &&
4612 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4613 /* read-high */
4614 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4615
4616 if (type & MSR_TYPE_W &&
4617 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4618 /* write-high */
4619 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4620
4621 }
4622}
4623
Avi Kivity58972972009-02-24 22:26:47 +02004624static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4625{
4626 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004627 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4628 msr, MSR_TYPE_R | MSR_TYPE_W);
4629 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4630 msr, MSR_TYPE_R | MSR_TYPE_W);
4631}
4632
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004633static void vmx_disable_intercept_msr_read_x2apic(u32 msr, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004634{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004635 if (apicv_active) {
Wanpeng Lic63e4562016-09-23 19:17:16 +08004636 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
4637 msr, MSR_TYPE_R);
4638 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
4639 msr, MSR_TYPE_R);
4640 } else {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004641 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4642 msr, MSR_TYPE_R);
4643 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4644 msr, MSR_TYPE_R);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004645 }
Yang Zhang8d146952013-01-25 10:18:50 +08004646}
4647
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004648static void vmx_disable_intercept_msr_write_x2apic(u32 msr, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004649{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004650 if (apicv_active) {
Wanpeng Lic63e4562016-09-23 19:17:16 +08004651 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
4652 msr, MSR_TYPE_W);
4653 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
4654 msr, MSR_TYPE_W);
4655 } else {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004656 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4657 msr, MSR_TYPE_W);
4658 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4659 msr, MSR_TYPE_W);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004660 }
Avi Kivity58972972009-02-24 22:26:47 +02004661}
4662
Andrey Smetanind62caab2015-11-10 15:36:33 +03004663static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004664{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004665 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004666}
4667
Wincy Van705699a2015-02-03 23:58:17 +08004668static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
4669{
4670 struct vcpu_vmx *vmx = to_vmx(vcpu);
4671 int max_irr;
4672 void *vapic_page;
4673 u16 status;
4674
4675 if (vmx->nested.pi_desc &&
4676 vmx->nested.pi_pending) {
4677 vmx->nested.pi_pending = false;
4678 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4679 return 0;
4680
4681 max_irr = find_last_bit(
4682 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4683
4684 if (max_irr == 256)
4685 return 0;
4686
4687 vapic_page = kmap(vmx->nested.virtual_apic_page);
4688 if (!vapic_page) {
4689 WARN_ON(1);
4690 return -ENOMEM;
4691 }
4692 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4693 kunmap(vmx->nested.virtual_apic_page);
4694
4695 status = vmcs_read16(GUEST_INTR_STATUS);
4696 if ((u8)max_irr > ((u8)status & 0xff)) {
4697 status &= ~0xff;
4698 status |= (u8)max_irr;
4699 vmcs_write16(GUEST_INTR_STATUS, status);
4700 }
4701 }
4702 return 0;
4703}
4704
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004705static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4706{
4707#ifdef CONFIG_SMP
4708 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004709 struct vcpu_vmx *vmx = to_vmx(vcpu);
4710
4711 /*
4712 * Currently, we don't support urgent interrupt,
4713 * all interrupts are recognized as non-urgent
4714 * interrupt, so we cannot post interrupts when
4715 * 'SN' is set.
4716 *
4717 * If the vcpu is in guest mode, it means it is
4718 * running instead of being scheduled out and
4719 * waiting in the run queue, and that's the only
4720 * case when 'SN' is set currently, warning if
4721 * 'SN' is set.
4722 */
4723 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
4724
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004725 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4726 POSTED_INTR_VECTOR);
4727 return true;
4728 }
4729#endif
4730 return false;
4731}
4732
Wincy Van705699a2015-02-03 23:58:17 +08004733static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4734 int vector)
4735{
4736 struct vcpu_vmx *vmx = to_vmx(vcpu);
4737
4738 if (is_guest_mode(vcpu) &&
4739 vector == vmx->nested.posted_intr_nv) {
4740 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004741 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004742 /*
4743 * If a posted intr is not recognized by hardware,
4744 * we will accomplish it in the next vmentry.
4745 */
4746 vmx->nested.pi_pending = true;
4747 kvm_make_request(KVM_REQ_EVENT, vcpu);
4748 return 0;
4749 }
4750 return -1;
4751}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004753 * Send interrupt to vcpu via posted interrupt way.
4754 * 1. If target vcpu is running(non-root mode), send posted interrupt
4755 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4756 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4757 * interrupt from PIR in next vmentry.
4758 */
4759static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4760{
4761 struct vcpu_vmx *vmx = to_vmx(vcpu);
4762 int r;
4763
Wincy Van705699a2015-02-03 23:58:17 +08004764 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4765 if (!r)
4766 return;
4767
Yang Zhanga20ed542013-04-11 19:25:15 +08004768 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4769 return;
4770
4771 r = pi_test_and_set_on(&vmx->pi_desc);
4772 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004773 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08004774 kvm_vcpu_kick(vcpu);
4775}
4776
4777static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4778{
4779 struct vcpu_vmx *vmx = to_vmx(vcpu);
4780
4781 if (!pi_test_and_clear_on(&vmx->pi_desc))
4782 return;
4783
4784 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4785}
4786
Avi Kivity6aa8b732006-12-10 02:21:36 -08004787/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004788 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4789 * will not change in the lifetime of the guest.
4790 * Note that host-state that does change is set elsewhere. E.g., host-state
4791 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4792 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004793static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004794{
4795 u32 low32, high32;
4796 unsigned long tmpl;
4797 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004798 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004799
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004800 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004801 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4802
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004803 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004804 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004805 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4806 vmx->host_state.vmcs_host_cr4 = cr4;
4807
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004808 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004809#ifdef CONFIG_X86_64
4810 /*
4811 * Load null selectors, so we can avoid reloading them in
4812 * __vmx_load_host_state(), in case userspace uses the null selectors
4813 * too (the expected case).
4814 */
4815 vmcs_write16(HOST_DS_SELECTOR, 0);
4816 vmcs_write16(HOST_ES_SELECTOR, 0);
4817#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004818 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4819 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004820#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004821 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4822 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4823
4824 native_store_idt(&dt);
4825 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004826 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004827
Avi Kivity83287ea422012-09-16 15:10:57 +03004828 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004829
4830 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4831 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4832 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4833 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4834
4835 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4836 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4837 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4838 }
4839}
4840
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004841static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4842{
4843 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4844 if (enable_ept)
4845 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004846 if (is_guest_mode(&vmx->vcpu))
4847 vmx->vcpu.arch.cr4_guest_owned_bits &=
4848 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004849 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4850}
4851
Yang Zhang01e439b2013-04-11 19:25:12 +08004852static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4853{
4854 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4855
Andrey Smetanind62caab2015-11-10 15:36:33 +03004856 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004857 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07004858 /* Enable the preemption timer dynamically */
4859 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004860 return pin_based_exec_ctrl;
4861}
4862
Andrey Smetanind62caab2015-11-10 15:36:33 +03004863static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4864{
4865 struct vcpu_vmx *vmx = to_vmx(vcpu);
4866
4867 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004868 if (cpu_has_secondary_exec_ctrls()) {
4869 if (kvm_vcpu_apicv_active(vcpu))
4870 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4871 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4872 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4873 else
4874 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4875 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4876 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4877 }
4878
4879 if (cpu_has_vmx_msr_bitmap())
4880 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004881}
4882
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004883static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4884{
4885 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004886
4887 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4888 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4889
Paolo Bonzini35754c92015-07-29 12:05:37 +02004890 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004891 exec_control &= ~CPU_BASED_TPR_SHADOW;
4892#ifdef CONFIG_X86_64
4893 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4894 CPU_BASED_CR8_LOAD_EXITING;
4895#endif
4896 }
4897 if (!enable_ept)
4898 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4899 CPU_BASED_CR3_LOAD_EXITING |
4900 CPU_BASED_INVLPG_EXITING;
4901 return exec_control;
4902}
4903
4904static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4905{
4906 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004907 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004908 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4909 if (vmx->vpid == 0)
4910 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4911 if (!enable_ept) {
4912 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4913 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004914 /* Enable INVPCID for non-ept guests may cause performance regression. */
4915 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004916 }
4917 if (!enable_unrestricted_guest)
4918 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4919 if (!ple_gap)
4920 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03004921 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004922 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4923 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004924 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004925 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4926 (handle_vmptrld).
4927 We can NOT enable shadow_vmcs here because we don't have yet
4928 a current VMCS12
4929 */
4930 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004931
4932 if (!enable_pml)
4933 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004934
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004935 return exec_control;
4936}
4937
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004938static void ept_set_mmio_spte_mask(void)
4939{
4940 /*
4941 * EPT Misconfigurations can be generated if the value of bits 2:0
4942 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004943 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004944 * spte.
4945 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004946 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004947}
4948
Wanpeng Lif53cd632014-12-02 19:14:58 +08004949#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004950/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951 * Sets up the vmcs for emulated real mode.
4952 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004953static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004955#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004956 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004957#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004959
Avi Kivity6aa8b732006-12-10 02:21:36 -08004960 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004961 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4962 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963
Abel Gordon4607c2d2013-04-18 14:35:55 +03004964 if (enable_shadow_vmcs) {
4965 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4966 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4967 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004968 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004969 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004970
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4972
Avi Kivity6aa8b732006-12-10 02:21:36 -08004973 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004974 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07004975 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004976
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004977 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978
Dan Williamsdfa169b2016-06-02 11:17:24 -07004979 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004980 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4981 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07004982 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004983
Andrey Smetanind62caab2015-11-10 15:36:33 +03004984 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004985 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4986 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4987 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4988 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4989
4990 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004991
Li RongQing0bcf2612015-12-03 13:29:34 +08004992 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08004993 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004994 }
4995
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004996 if (ple_gap) {
4997 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004998 vmx->ple_window = ple_window;
4999 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005000 }
5001
Xiao Guangrongc3707952011-07-12 03:28:04 +08005002 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5003 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005004 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5005
Avi Kivity9581d442010-10-19 16:46:55 +02005006 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5007 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005008 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005009#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005010 rdmsrl(MSR_FS_BASE, a);
5011 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5012 rdmsrl(MSR_GS_BASE, a);
5013 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5014#else
5015 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5016 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5017#endif
5018
Eddie Dong2cc51562007-05-21 07:28:09 +03005019 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5020 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005021 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005022 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005023 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005024
Radim Krčmář74545702015-04-27 15:11:25 +02005025 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5026 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005027
Paolo Bonzini03916db2014-07-24 14:21:57 +02005028 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029 u32 index = vmx_msr_index[i];
5030 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005031 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032
5033 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5034 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005035 if (wrmsr_safe(index, data_low, data_high) < 0)
5036 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005037 vmx->guest_msrs[j].index = i;
5038 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005039 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005040 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042
Gleb Natapov2961e8762013-11-25 15:37:13 +02005043
5044 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045
5046 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005047 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005048
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005049 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005050 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005051
Wanpeng Lif53cd632014-12-02 19:14:58 +08005052 if (vmx_xsaves_supported())
5053 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5054
Peter Feiner4e595162016-07-07 14:49:58 -07005055 if (enable_pml) {
5056 ASSERT(vmx->pml_pg);
5057 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5058 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5059 }
5060
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005061 return 0;
5062}
5063
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005064static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005065{
5066 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005067 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005068 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005069
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005070 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005071
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005072 vmx->soft_vnmi_blocked = 0;
5073
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005074 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005075 kvm_set_cr8(vcpu, 0);
5076
5077 if (!init_event) {
5078 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5079 MSR_IA32_APICBASE_ENABLE;
5080 if (kvm_vcpu_is_reset_bsp(vcpu))
5081 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5082 apic_base_msr.host_initiated = true;
5083 kvm_set_apic_base(vcpu, &apic_base_msr);
5084 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005085
Avi Kivity2fb92db2011-04-27 19:42:18 +03005086 vmx_segment_cache_clear(vmx);
5087
Avi Kivity5706be02008-08-20 15:07:31 +03005088 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005089 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005090 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005091
5092 seg_setup(VCPU_SREG_DS);
5093 seg_setup(VCPU_SREG_ES);
5094 seg_setup(VCPU_SREG_FS);
5095 seg_setup(VCPU_SREG_GS);
5096 seg_setup(VCPU_SREG_SS);
5097
5098 vmcs_write16(GUEST_TR_SELECTOR, 0);
5099 vmcs_writel(GUEST_TR_BASE, 0);
5100 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5101 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5102
5103 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5104 vmcs_writel(GUEST_LDTR_BASE, 0);
5105 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5106 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5107
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005108 if (!init_event) {
5109 vmcs_write32(GUEST_SYSENTER_CS, 0);
5110 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5111 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5112 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5113 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005114
5115 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005116 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005117
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005118 vmcs_writel(GUEST_GDTR_BASE, 0);
5119 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5120
5121 vmcs_writel(GUEST_IDTR_BASE, 0);
5122 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5123
Anthony Liguori443381a2010-12-06 10:53:38 -06005124 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005125 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005126 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005127
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005128 setup_msrs(vmx);
5129
Avi Kivity6aa8b732006-12-10 02:21:36 -08005130 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5131
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005132 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005133 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005134 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005135 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005136 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005137 vmcs_write32(TPR_THRESHOLD, 0);
5138 }
5139
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005140 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005141
Andrey Smetanind62caab2015-11-10 15:36:33 +03005142 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005143 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5144
Sheng Yang2384d2b2008-01-17 15:14:33 +08005145 if (vmx->vpid != 0)
5146 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5147
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005148 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005149 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005150 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005151 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005152 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005153 vmx_fpu_activate(vcpu);
5154 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005155
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005156 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005157}
5158
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005159/*
5160 * In nested virtualization, check if L1 asked to exit on external interrupts.
5161 * For most existing hypervisors, this will always return true.
5162 */
5163static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5164{
5165 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5166 PIN_BASED_EXT_INTR_MASK;
5167}
5168
Bandan Das77b0f5d2014-04-19 18:17:45 -04005169/*
5170 * In nested virtualization, check if L1 has set
5171 * VM_EXIT_ACK_INTR_ON_EXIT
5172 */
5173static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5174{
5175 return get_vmcs12(vcpu)->vm_exit_controls &
5176 VM_EXIT_ACK_INTR_ON_EXIT;
5177}
5178
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005179static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5180{
5181 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5182 PIN_BASED_NMI_EXITING;
5183}
5184
Jan Kiszkac9a79532014-03-07 20:03:15 +01005185static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005186{
5187 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005188
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005189 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5190 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5191 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5192}
5193
Jan Kiszkac9a79532014-03-07 20:03:15 +01005194static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005195{
5196 u32 cpu_based_vm_exec_control;
5197
Jan Kiszkac9a79532014-03-07 20:03:15 +01005198 if (!cpu_has_virtual_nmis() ||
5199 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5200 enable_irq_window(vcpu);
5201 return;
5202 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005203
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005204 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5205 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5206 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5207}
5208
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005209static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005210{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005211 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005212 uint32_t intr;
5213 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005214
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005215 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005216
Avi Kivityfa89a812008-09-01 15:57:51 +03005217 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005218 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005219 int inc_eip = 0;
5220 if (vcpu->arch.interrupt.soft)
5221 inc_eip = vcpu->arch.event_exit_inst_len;
5222 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005223 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005224 return;
5225 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005226 intr = irq | INTR_INFO_VALID_MASK;
5227 if (vcpu->arch.interrupt.soft) {
5228 intr |= INTR_TYPE_SOFT_INTR;
5229 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5230 vmx->vcpu.arch.event_exit_inst_len);
5231 } else
5232 intr |= INTR_TYPE_EXT_INTR;
5233 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005234}
5235
Sheng Yangf08864b2008-05-15 18:23:25 +08005236static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5237{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005238 struct vcpu_vmx *vmx = to_vmx(vcpu);
5239
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005240 if (!is_guest_mode(vcpu)) {
5241 if (!cpu_has_virtual_nmis()) {
5242 /*
5243 * Tracking the NMI-blocked state in software is built upon
5244 * finding the next open IRQ window. This, in turn, depends on
5245 * well-behaving guests: They have to keep IRQs disabled at
5246 * least as long as the NMI handler runs. Otherwise we may
5247 * cause NMI nesting, maybe breaking the guest. But as this is
5248 * highly unlikely, we can live with the residual risk.
5249 */
5250 vmx->soft_vnmi_blocked = 1;
5251 vmx->vnmi_blocked_time = 0;
5252 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005253
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005254 ++vcpu->stat.nmi_injections;
5255 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005256 }
5257
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005258 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005259 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005260 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005261 return;
5262 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005263
Sheng Yangf08864b2008-05-15 18:23:25 +08005264 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5265 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005266}
5267
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005268static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5269{
5270 if (!cpu_has_virtual_nmis())
5271 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005272 if (to_vmx(vcpu)->nmi_known_unmasked)
5273 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005274 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005275}
5276
5277static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5278{
5279 struct vcpu_vmx *vmx = to_vmx(vcpu);
5280
5281 if (!cpu_has_virtual_nmis()) {
5282 if (vmx->soft_vnmi_blocked != masked) {
5283 vmx->soft_vnmi_blocked = masked;
5284 vmx->vnmi_blocked_time = 0;
5285 }
5286 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005287 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005288 if (masked)
5289 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5290 GUEST_INTR_STATE_NMI);
5291 else
5292 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5293 GUEST_INTR_STATE_NMI);
5294 }
5295}
5296
Jan Kiszka2505dc92013-04-14 12:12:47 +02005297static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5298{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005299 if (to_vmx(vcpu)->nested.nested_run_pending)
5300 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005301
Jan Kiszka2505dc92013-04-14 12:12:47 +02005302 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5303 return 0;
5304
5305 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5306 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5307 | GUEST_INTR_STATE_NMI));
5308}
5309
Gleb Natapov78646122009-03-23 12:12:11 +02005310static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5311{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005312 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5313 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005314 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5315 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005316}
5317
Izik Eiduscbc94022007-10-25 00:29:55 +02005318static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5319{
5320 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005321
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005322 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5323 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005324 if (ret)
5325 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005326 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005327 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005328}
5329
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005330static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005331{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005332 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005333 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005334 /*
5335 * Update instruction length as we may reinject the exception
5336 * from user space while in guest debugging mode.
5337 */
5338 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5339 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005340 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005341 return false;
5342 /* fall through */
5343 case DB_VECTOR:
5344 if (vcpu->guest_debug &
5345 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5346 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005347 /* fall through */
5348 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005349 case OF_VECTOR:
5350 case BR_VECTOR:
5351 case UD_VECTOR:
5352 case DF_VECTOR:
5353 case SS_VECTOR:
5354 case GP_VECTOR:
5355 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005356 return true;
5357 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005358 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005359 return false;
5360}
5361
5362static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5363 int vec, u32 err_code)
5364{
5365 /*
5366 * Instruction with address size override prefix opcode 0x67
5367 * Cause the #SS fault with 0 error code in VM86 mode.
5368 */
5369 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5370 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5371 if (vcpu->arch.halt_request) {
5372 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005373 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005374 }
5375 return 1;
5376 }
5377 return 0;
5378 }
5379
5380 /*
5381 * Forward all other exceptions that are valid in real mode.
5382 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5383 * the required debugging infrastructure rework.
5384 */
5385 kvm_queue_exception(vcpu, vec);
5386 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005387}
5388
Andi Kleena0861c02009-06-08 17:37:09 +08005389/*
5390 * Trigger machine check on the host. We assume all the MSRs are already set up
5391 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5392 * We pass a fake environment to the machine check handler because we want
5393 * the guest to be always treated like user space, no matter what context
5394 * it used internally.
5395 */
5396static void kvm_machine_check(void)
5397{
5398#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5399 struct pt_regs regs = {
5400 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5401 .flags = X86_EFLAGS_IF,
5402 };
5403
5404 do_machine_check(&regs, 0);
5405#endif
5406}
5407
Avi Kivity851ba692009-08-24 11:10:17 +03005408static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005409{
5410 /* already handled by vcpu_run */
5411 return 1;
5412}
5413
Avi Kivity851ba692009-08-24 11:10:17 +03005414static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005415{
Avi Kivity1155f762007-11-22 11:30:47 +02005416 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005417 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005418 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005419 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005420 u32 vect_info;
5421 enum emulation_result er;
5422
Avi Kivity1155f762007-11-22 11:30:47 +02005423 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005424 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005425
Andi Kleena0861c02009-06-08 17:37:09 +08005426 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005427 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005428
Jan Kiszkae4a41882008-09-26 09:30:46 +02005429 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02005430 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005431
5432 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005433 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005434 return 1;
5435 }
5436
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005437 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005438 if (is_guest_mode(vcpu)) {
5439 kvm_queue_exception(vcpu, UD_VECTOR);
5440 return 1;
5441 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005442 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005443 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005444 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005445 return 1;
5446 }
5447
Avi Kivity6aa8b732006-12-10 02:21:36 -08005448 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005449 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005451
5452 /*
5453 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5454 * MMIO, it is better to report an internal error.
5455 * See the comments in vmx_handle_exit.
5456 */
5457 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5458 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5459 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5460 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005461 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005462 vcpu->run->internal.data[0] = vect_info;
5463 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005464 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005465 return 0;
5466 }
5467
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005469 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005470 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005471 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005472 trace_kvm_page_fault(cr2, error_code);
5473
Gleb Natapov3298b752009-05-11 13:35:46 +03005474 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005475 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005476 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005477 }
5478
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005479 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005480
5481 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5482 return handle_rmode_exception(vcpu, ex_no, error_code);
5483
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005484 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005485 case AC_VECTOR:
5486 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5487 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005488 case DB_VECTOR:
5489 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5490 if (!(vcpu->guest_debug &
5491 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005492 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005493 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005494 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5495 skip_emulated_instruction(vcpu);
5496
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005497 kvm_queue_exception(vcpu, DB_VECTOR);
5498 return 1;
5499 }
5500 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5501 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5502 /* fall through */
5503 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005504 /*
5505 * Update instruction length as we may reinject #BP from
5506 * user space while in guest debugging mode. Reading it for
5507 * #DB as well causes no harm, it is not used in that case.
5508 */
5509 vmx->vcpu.arch.event_exit_inst_len =
5510 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005511 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005512 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005513 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5514 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005515 break;
5516 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005517 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5518 kvm_run->ex.exception = ex_no;
5519 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005520 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005521 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005522 return 0;
5523}
5524
Avi Kivity851ba692009-08-24 11:10:17 +03005525static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005526{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005527 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005528 return 1;
5529}
5530
Avi Kivity851ba692009-08-24 11:10:17 +03005531static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005532{
Avi Kivity851ba692009-08-24 11:10:17 +03005533 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005534 return 0;
5535}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005536
Avi Kivity851ba692009-08-24 11:10:17 +03005537static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005538{
He, Qingbfdaab02007-09-12 14:18:28 +08005539 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005540 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005541 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005542
He, Qingbfdaab02007-09-12 14:18:28 +08005543 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005544 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005545 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005546
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005547 ++vcpu->stat.io_exits;
5548
5549 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005550 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005551
5552 port = exit_qualification >> 16;
5553 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005554 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005555
5556 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005557}
5558
Ingo Molnar102d8322007-02-19 14:37:47 +02005559static void
5560vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5561{
5562 /*
5563 * Patch in the VMCALL instruction:
5564 */
5565 hypercall[0] = 0x0f;
5566 hypercall[1] = 0x01;
5567 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005568}
5569
Wincy Vanb9c237b2015-02-03 23:56:30 +08005570static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005571{
5572 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005573 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005574
Wincy Vanb9c237b2015-02-03 23:56:30 +08005575 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005576 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5577 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5578 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5579 return (val & always_on) == always_on;
5580}
5581
Guo Chao0fa06072012-06-28 15:16:19 +08005582/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005583static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5584{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005585 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005586 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5587 unsigned long orig_val = val;
5588
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005589 /*
5590 * We get here when L2 changed cr0 in a way that did not change
5591 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005592 * but did change L0 shadowed bits. So we first calculate the
5593 * effective cr0 value that L1 would like to write into the
5594 * hardware. It consists of the L2-owned bits from the new
5595 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005596 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005597 val = (val & ~vmcs12->cr0_guest_host_mask) |
5598 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5599
Wincy Vanb9c237b2015-02-03 23:56:30 +08005600 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005601 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005602
5603 if (kvm_set_cr0(vcpu, val))
5604 return 1;
5605 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005606 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005607 } else {
5608 if (to_vmx(vcpu)->nested.vmxon &&
5609 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5610 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005611 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005612 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005613}
5614
5615static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5616{
5617 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005618 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5619 unsigned long orig_val = val;
5620
5621 /* analogously to handle_set_cr0 */
5622 val = (val & ~vmcs12->cr4_guest_host_mask) |
5623 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5624 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005625 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005626 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005627 return 0;
5628 } else
5629 return kvm_set_cr4(vcpu, val);
5630}
5631
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005632/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005633static void handle_clts(struct kvm_vcpu *vcpu)
5634{
5635 if (is_guest_mode(vcpu)) {
5636 /*
5637 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5638 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5639 * just pretend it's off (also in arch.cr0 for fpu_activate).
5640 */
5641 vmcs_writel(CR0_READ_SHADOW,
5642 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5643 vcpu->arch.cr0 &= ~X86_CR0_TS;
5644 } else
5645 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5646}
5647
Avi Kivity851ba692009-08-24 11:10:17 +03005648static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005649{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005650 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005651 int cr;
5652 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005653 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005654
He, Qingbfdaab02007-09-12 14:18:28 +08005655 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005656 cr = exit_qualification & 15;
5657 reg = (exit_qualification >> 8) & 15;
5658 switch ((exit_qualification >> 4) & 3) {
5659 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005660 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005661 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005662 switch (cr) {
5663 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005664 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005665 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005666 return 1;
5667 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005668 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005669 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005670 return 1;
5671 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005672 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005673 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005674 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005675 case 8: {
5676 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005677 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005678 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005679 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005680 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005681 return 1;
5682 if (cr8_prev <= cr8)
5683 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005684 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005685 return 0;
5686 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005687 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005688 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005689 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005690 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005691 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005692 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005693 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005694 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005695 case 1: /*mov from cr*/
5696 switch (cr) {
5697 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005698 val = kvm_read_cr3(vcpu);
5699 kvm_register_write(vcpu, reg, val);
5700 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005701 skip_emulated_instruction(vcpu);
5702 return 1;
5703 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005704 val = kvm_get_cr8(vcpu);
5705 kvm_register_write(vcpu, reg, val);
5706 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005707 skip_emulated_instruction(vcpu);
5708 return 1;
5709 }
5710 break;
5711 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005712 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005713 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005714 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005715
5716 skip_emulated_instruction(vcpu);
5717 return 1;
5718 default:
5719 break;
5720 }
Avi Kivity851ba692009-08-24 11:10:17 +03005721 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005722 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005723 (int)(exit_qualification >> 4) & 3, cr);
5724 return 0;
5725}
5726
Avi Kivity851ba692009-08-24 11:10:17 +03005727static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005728{
He, Qingbfdaab02007-09-12 14:18:28 +08005729 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005730 int dr, dr7, reg;
5731
5732 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5733 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5734
5735 /* First, if DR does not exist, trigger UD */
5736 if (!kvm_require_dr(vcpu, dr))
5737 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005738
Jan Kiszkaf2483412010-01-20 18:20:20 +01005739 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005740 if (!kvm_require_cpl(vcpu, 0))
5741 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005742 dr7 = vmcs_readl(GUEST_DR7);
5743 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005744 /*
5745 * As the vm-exit takes precedence over the debug trap, we
5746 * need to emulate the latter, either for the host or the
5747 * guest debugging itself.
5748 */
5749 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005750 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005751 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005752 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005753 vcpu->run->debug.arch.exception = DB_VECTOR;
5754 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005755 return 0;
5756 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005757 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005758 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005759 kvm_queue_exception(vcpu, DB_VECTOR);
5760 return 1;
5761 }
5762 }
5763
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005764 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01005765 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5766 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005767
5768 /*
5769 * No more DR vmexits; force a reload of the debug registers
5770 * and reenter on this instruction. The next vmexit will
5771 * retrieve the full state of the debug registers.
5772 */
5773 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5774 return 1;
5775 }
5776
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005777 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5778 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005779 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005780
5781 if (kvm_get_dr(vcpu, dr, &val))
5782 return 1;
5783 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005784 } else
Nadav Amit57773922014-06-18 17:19:23 +03005785 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005786 return 1;
5787
Avi Kivity6aa8b732006-12-10 02:21:36 -08005788 skip_emulated_instruction(vcpu);
5789 return 1;
5790}
5791
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005792static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5793{
5794 return vcpu->arch.dr6;
5795}
5796
5797static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5798{
5799}
5800
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005801static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5802{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005803 get_debugreg(vcpu->arch.db[0], 0);
5804 get_debugreg(vcpu->arch.db[1], 1);
5805 get_debugreg(vcpu->arch.db[2], 2);
5806 get_debugreg(vcpu->arch.db[3], 3);
5807 get_debugreg(vcpu->arch.dr6, 6);
5808 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5809
5810 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01005811 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005812}
5813
Gleb Natapov020df072010-04-13 10:05:23 +03005814static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5815{
5816 vmcs_writel(GUEST_DR7, val);
5817}
5818
Avi Kivity851ba692009-08-24 11:10:17 +03005819static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005820{
Avi Kivity06465c52007-02-28 20:46:53 +02005821 kvm_emulate_cpuid(vcpu);
5822 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005823}
5824
Avi Kivity851ba692009-08-24 11:10:17 +03005825static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005826{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005827 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005828 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005829
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005830 msr_info.index = ecx;
5831 msr_info.host_initiated = false;
5832 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005833 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005834 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005835 return 1;
5836 }
5837
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005838 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005839
Avi Kivity6aa8b732006-12-10 02:21:36 -08005840 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005841 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5842 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005843 skip_emulated_instruction(vcpu);
5844 return 1;
5845}
5846
Avi Kivity851ba692009-08-24 11:10:17 +03005847static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005848{
Will Auld8fe8ab42012-11-29 12:42:12 -08005849 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005850 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5851 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5852 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005853
Will Auld8fe8ab42012-11-29 12:42:12 -08005854 msr.data = data;
5855 msr.index = ecx;
5856 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005857 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005858 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005859 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005860 return 1;
5861 }
5862
Avi Kivity59200272010-01-25 19:47:02 +02005863 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005864 skip_emulated_instruction(vcpu);
5865 return 1;
5866}
5867
Avi Kivity851ba692009-08-24 11:10:17 +03005868static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005869{
Avi Kivity3842d132010-07-27 12:30:24 +03005870 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005871 return 1;
5872}
5873
Avi Kivity851ba692009-08-24 11:10:17 +03005874static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005875{
Eddie Dong85f455f2007-07-06 12:20:49 +03005876 u32 cpu_based_vm_exec_control;
5877
5878 /* clear pending irq */
5879 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5880 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5881 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005882
Avi Kivity3842d132010-07-27 12:30:24 +03005883 kvm_make_request(KVM_REQ_EVENT, vcpu);
5884
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005885 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005886 return 1;
5887}
5888
Avi Kivity851ba692009-08-24 11:10:17 +03005889static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005890{
Avi Kivityd3bef152007-06-05 15:53:05 +03005891 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005892}
5893
Avi Kivity851ba692009-08-24 11:10:17 +03005894static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005895{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005896 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005897}
5898
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005899static int handle_invd(struct kvm_vcpu *vcpu)
5900{
Andre Przywara51d8b662010-12-21 11:12:02 +01005901 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005902}
5903
Avi Kivity851ba692009-08-24 11:10:17 +03005904static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005905{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005906 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005907
5908 kvm_mmu_invlpg(vcpu, exit_qualification);
5909 skip_emulated_instruction(vcpu);
5910 return 1;
5911}
5912
Avi Kivityfee84b02011-11-10 14:57:25 +02005913static int handle_rdpmc(struct kvm_vcpu *vcpu)
5914{
5915 int err;
5916
5917 err = kvm_rdpmc(vcpu);
5918 kvm_complete_insn_gp(vcpu, err);
5919
5920 return 1;
5921}
5922
Avi Kivity851ba692009-08-24 11:10:17 +03005923static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005924{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005925 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005926 return 1;
5927}
5928
Dexuan Cui2acf9232010-06-10 11:27:12 +08005929static int handle_xsetbv(struct kvm_vcpu *vcpu)
5930{
5931 u64 new_bv = kvm_read_edx_eax(vcpu);
5932 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5933
5934 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5935 skip_emulated_instruction(vcpu);
5936 return 1;
5937}
5938
Wanpeng Lif53cd632014-12-02 19:14:58 +08005939static int handle_xsaves(struct kvm_vcpu *vcpu)
5940{
5941 skip_emulated_instruction(vcpu);
5942 WARN(1, "this should never happen\n");
5943 return 1;
5944}
5945
5946static int handle_xrstors(struct kvm_vcpu *vcpu)
5947{
5948 skip_emulated_instruction(vcpu);
5949 WARN(1, "this should never happen\n");
5950 return 1;
5951}
5952
Avi Kivity851ba692009-08-24 11:10:17 +03005953static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005954{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005955 if (likely(fasteoi)) {
5956 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5957 int access_type, offset;
5958
5959 access_type = exit_qualification & APIC_ACCESS_TYPE;
5960 offset = exit_qualification & APIC_ACCESS_OFFSET;
5961 /*
5962 * Sane guest uses MOV to write EOI, with written value
5963 * not cared. So make a short-circuit here by avoiding
5964 * heavy instruction emulation.
5965 */
5966 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5967 (offset == APIC_EOI)) {
5968 kvm_lapic_set_eoi(vcpu);
5969 skip_emulated_instruction(vcpu);
5970 return 1;
5971 }
5972 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005973 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005974}
5975
Yang Zhangc7c9c562013-01-25 10:18:51 +08005976static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5977{
5978 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5979 int vector = exit_qualification & 0xff;
5980
5981 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5982 kvm_apic_set_eoi_accelerated(vcpu, vector);
5983 return 1;
5984}
5985
Yang Zhang83d4c282013-01-25 10:18:49 +08005986static int handle_apic_write(struct kvm_vcpu *vcpu)
5987{
5988 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5989 u32 offset = exit_qualification & 0xfff;
5990
5991 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5992 kvm_apic_write_nodecode(vcpu, offset);
5993 return 1;
5994}
5995
Avi Kivity851ba692009-08-24 11:10:17 +03005996static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005997{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005998 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005999 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006000 bool has_error_code = false;
6001 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006002 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006003 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006004
6005 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006006 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006007 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006008
6009 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6010
6011 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006012 if (reason == TASK_SWITCH_GATE && idt_v) {
6013 switch (type) {
6014 case INTR_TYPE_NMI_INTR:
6015 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006016 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006017 break;
6018 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006019 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006020 kvm_clear_interrupt_queue(vcpu);
6021 break;
6022 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006023 if (vmx->idt_vectoring_info &
6024 VECTORING_INFO_DELIVER_CODE_MASK) {
6025 has_error_code = true;
6026 error_code =
6027 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6028 }
6029 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006030 case INTR_TYPE_SOFT_EXCEPTION:
6031 kvm_clear_exception_queue(vcpu);
6032 break;
6033 default:
6034 break;
6035 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006036 }
Izik Eidus37817f22008-03-24 23:14:53 +02006037 tss_selector = exit_qualification;
6038
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006039 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6040 type != INTR_TYPE_EXT_INTR &&
6041 type != INTR_TYPE_NMI_INTR))
6042 skip_emulated_instruction(vcpu);
6043
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006044 if (kvm_task_switch(vcpu, tss_selector,
6045 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6046 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006047 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6048 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6049 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006050 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006051 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006052
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006053 /*
6054 * TODO: What about debug traps on tss switch?
6055 * Are we supposed to inject them and update dr6?
6056 */
6057
6058 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006059}
6060
Avi Kivity851ba692009-08-24 11:10:17 +03006061static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006062{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006063 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006064 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006065 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006066 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006067
Sheng Yangf9c617f2009-03-25 10:08:52 +08006068 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006069
Sheng Yang14394422008-04-28 12:24:45 +08006070 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006071 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006072 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6073 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6074 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006075 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006076 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6077 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006078 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6079 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006080 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006081 }
6082
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006083 /*
6084 * EPT violation happened while executing iret from NMI,
6085 * "blocked by NMI" bit has to be set before next VM entry.
6086 * There are errata that may cause this bit to not be set:
6087 * AAK134, BY25.
6088 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006089 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6090 cpu_has_virtual_nmis() &&
6091 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006092 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6093
Sheng Yang14394422008-04-28 12:24:45 +08006094 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006095 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006096
Bandan Dasd95c5562016-07-12 18:18:51 -04006097 /* it is a read fault? */
6098 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6099 /* it is a write fault? */
6100 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006101 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006102 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006103 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006104 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006105
Yang Zhang25d92082013-08-06 12:00:32 +03006106 vcpu->arch.exit_qualification = exit_qualification;
6107
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006108 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006109}
6110
Avi Kivity851ba692009-08-24 11:10:17 +03006111static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006112{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006113 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006114 gpa_t gpa;
6115
6116 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006117 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006118 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006119 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006120 return 1;
6121 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006122
Paolo Bonzini450869d2015-11-04 13:41:21 +01006123 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006124 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006125 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6126 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006127
6128 if (unlikely(ret == RET_MMIO_PF_INVALID))
6129 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6130
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006131 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006132 return 1;
6133
6134 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006135 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006136
Avi Kivity851ba692009-08-24 11:10:17 +03006137 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6138 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006139
6140 return 0;
6141}
6142
Avi Kivity851ba692009-08-24 11:10:17 +03006143static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006144{
6145 u32 cpu_based_vm_exec_control;
6146
6147 /* clear pending NMI */
6148 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6149 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6150 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6151 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006152 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006153
6154 return 1;
6155}
6156
Mohammed Gamal80ced182009-09-01 12:48:18 +02006157static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006158{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006159 struct vcpu_vmx *vmx = to_vmx(vcpu);
6160 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006161 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006162 u32 cpu_exec_ctrl;
6163 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006164 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006165
6166 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6167 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006168
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006169 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006170 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006171 return handle_interrupt_window(&vmx->vcpu);
6172
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006173 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6174 return 1;
6175
Gleb Natapov991eebf2013-04-11 12:10:51 +03006176 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006177
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006178 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006179 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006180 ret = 0;
6181 goto out;
6182 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006183
Avi Kivityde5f70e2012-06-12 20:22:28 +03006184 if (err != EMULATE_DONE) {
6185 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6186 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6187 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006188 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006189 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006190
Gleb Natapov8d76c492013-05-08 18:38:44 +03006191 if (vcpu->arch.halt_request) {
6192 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006193 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006194 goto out;
6195 }
6196
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006197 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006198 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006199 if (need_resched())
6200 schedule();
6201 }
6202
Mohammed Gamal80ced182009-09-01 12:48:18 +02006203out:
6204 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006205}
6206
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006207static int __grow_ple_window(int val)
6208{
6209 if (ple_window_grow < 1)
6210 return ple_window;
6211
6212 val = min(val, ple_window_actual_max);
6213
6214 if (ple_window_grow < ple_window)
6215 val *= ple_window_grow;
6216 else
6217 val += ple_window_grow;
6218
6219 return val;
6220}
6221
6222static int __shrink_ple_window(int val, int modifier, int minimum)
6223{
6224 if (modifier < 1)
6225 return ple_window;
6226
6227 if (modifier < ple_window)
6228 val /= modifier;
6229 else
6230 val -= modifier;
6231
6232 return max(val, minimum);
6233}
6234
6235static void grow_ple_window(struct kvm_vcpu *vcpu)
6236{
6237 struct vcpu_vmx *vmx = to_vmx(vcpu);
6238 int old = vmx->ple_window;
6239
6240 vmx->ple_window = __grow_ple_window(old);
6241
6242 if (vmx->ple_window != old)
6243 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006244
6245 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006246}
6247
6248static void shrink_ple_window(struct kvm_vcpu *vcpu)
6249{
6250 struct vcpu_vmx *vmx = to_vmx(vcpu);
6251 int old = vmx->ple_window;
6252
6253 vmx->ple_window = __shrink_ple_window(old,
6254 ple_window_shrink, ple_window);
6255
6256 if (vmx->ple_window != old)
6257 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006258
6259 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006260}
6261
6262/*
6263 * ple_window_actual_max is computed to be one grow_ple_window() below
6264 * ple_window_max. (See __grow_ple_window for the reason.)
6265 * This prevents overflows, because ple_window_max is int.
6266 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6267 * this process.
6268 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6269 */
6270static void update_ple_window_actual_max(void)
6271{
6272 ple_window_actual_max =
6273 __shrink_ple_window(max(ple_window_max, ple_window),
6274 ple_window_grow, INT_MIN);
6275}
6276
Feng Wubf9f6ac2015-09-18 22:29:55 +08006277/*
6278 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6279 */
6280static void wakeup_handler(void)
6281{
6282 struct kvm_vcpu *vcpu;
6283 int cpu = smp_processor_id();
6284
6285 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6286 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6287 blocked_vcpu_list) {
6288 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6289
6290 if (pi_test_on(pi_desc) == 1)
6291 kvm_vcpu_kick(vcpu);
6292 }
6293 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6294}
6295
Tiejun Chenf2c76482014-10-28 10:14:47 +08006296static __init int hardware_setup(void)
6297{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006298 int r = -ENOMEM, i, msr;
6299
6300 rdmsrl_safe(MSR_EFER, &host_efer);
6301
6302 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6303 kvm_define_shared_msr(i, vmx_msr_index[i]);
6304
6305 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6306 if (!vmx_io_bitmap_a)
6307 return r;
6308
6309 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6310 if (!vmx_io_bitmap_b)
6311 goto out;
6312
6313 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
6314 if (!vmx_msr_bitmap_legacy)
6315 goto out1;
6316
Wanpeng Lic63e4562016-09-23 19:17:16 +08006317 vmx_msr_bitmap_legacy_x2apic_apicv =
6318 (unsigned long *)__get_free_page(GFP_KERNEL);
6319 if (!vmx_msr_bitmap_legacy_x2apic_apicv)
6320 goto out2;
6321
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006322 vmx_msr_bitmap_legacy_x2apic =
6323 (unsigned long *)__get_free_page(GFP_KERNEL);
6324 if (!vmx_msr_bitmap_legacy_x2apic)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006325 goto out3;
6326
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006327 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
6328 if (!vmx_msr_bitmap_longmode)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006329 goto out4;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006330
Wanpeng Lic63e4562016-09-23 19:17:16 +08006331 vmx_msr_bitmap_longmode_x2apic_apicv =
6332 (unsigned long *)__get_free_page(GFP_KERNEL);
6333 if (!vmx_msr_bitmap_longmode_x2apic_apicv)
6334 goto out5;
6335
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006336 vmx_msr_bitmap_longmode_x2apic =
6337 (unsigned long *)__get_free_page(GFP_KERNEL);
6338 if (!vmx_msr_bitmap_longmode_x2apic)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006339 goto out6;
Wincy Van3af18d92015-02-03 23:49:31 +08006340
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006341 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6342 if (!vmx_vmread_bitmap)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006343 goto out7;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006344
6345 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6346 if (!vmx_vmwrite_bitmap)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006347 goto out8;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006348
6349 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6350 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6351
6352 /*
6353 * Allow direct access to the PC debug port (it is often used for I/O
6354 * delays, but the vmexits simply slow things down).
6355 */
6356 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6357 clear_bit(0x80, vmx_io_bitmap_a);
6358
6359 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6360
6361 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6362 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6363
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006364 if (setup_vmcs_config(&vmcs_config) < 0) {
6365 r = -EIO;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006366 goto out9;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006367 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006368
6369 if (boot_cpu_has(X86_FEATURE_NX))
6370 kvm_enable_efer_bits(EFER_NX);
6371
6372 if (!cpu_has_vmx_vpid())
6373 enable_vpid = 0;
6374 if (!cpu_has_vmx_shadow_vmcs())
6375 enable_shadow_vmcs = 0;
6376 if (enable_shadow_vmcs)
6377 init_vmcs_shadow_fields();
6378
6379 if (!cpu_has_vmx_ept() ||
6380 !cpu_has_vmx_ept_4levels()) {
6381 enable_ept = 0;
6382 enable_unrestricted_guest = 0;
6383 enable_ept_ad_bits = 0;
6384 }
6385
6386 if (!cpu_has_vmx_ept_ad_bits())
6387 enable_ept_ad_bits = 0;
6388
6389 if (!cpu_has_vmx_unrestricted_guest())
6390 enable_unrestricted_guest = 0;
6391
Paolo Bonziniad15a292015-01-30 16:18:49 +01006392 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006393 flexpriority_enabled = 0;
6394
Paolo Bonziniad15a292015-01-30 16:18:49 +01006395 /*
6396 * set_apic_access_page_addr() is used to reload apic access
6397 * page upon invalidation. No need to do anything if not
6398 * using the APIC_ACCESS_ADDR VMCS field.
6399 */
6400 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006401 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006402
6403 if (!cpu_has_vmx_tpr_shadow())
6404 kvm_x86_ops->update_cr8_intercept = NULL;
6405
6406 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6407 kvm_disable_largepages();
6408
6409 if (!cpu_has_vmx_ple())
6410 ple_gap = 0;
6411
6412 if (!cpu_has_vmx_apicv())
6413 enable_apicv = 0;
6414
Haozhong Zhang64903d62015-10-20 15:39:09 +08006415 if (cpu_has_vmx_tsc_scaling()) {
6416 kvm_has_tsc_control = true;
6417 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6418 kvm_tsc_scaling_ratio_frac_bits = 48;
6419 }
6420
Tiejun Chenbaa03522014-12-23 16:21:11 +08006421 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6422 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6423 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6424 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6425 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6426 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6427 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6428
Wanpeng Lic63e4562016-09-23 19:17:16 +08006429 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6430 vmx_msr_bitmap_legacy, PAGE_SIZE);
6431 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6432 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006433 memcpy(vmx_msr_bitmap_legacy_x2apic,
6434 vmx_msr_bitmap_legacy, PAGE_SIZE);
6435 memcpy(vmx_msr_bitmap_longmode_x2apic,
6436 vmx_msr_bitmap_longmode, PAGE_SIZE);
6437
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006438 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6439
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006440 /*
6441 * enable_apicv && kvm_vcpu_apicv_active()
6442 */
Radim Krčmář40d83382016-09-29 22:41:31 +02006443 for (msr = 0x800; msr <= 0x8ff; msr++) {
6444 if (msr == 0x839 /* TMCCT */)
6445 continue;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006446 vmx_disable_intercept_msr_read_x2apic(msr, true);
Radim Krčmář40d83382016-09-29 22:41:31 +02006447 }
Tiejun Chenbaa03522014-12-23 16:21:11 +08006448
Roman Kagan3ce424e2016-05-18 17:48:20 +03006449 /* TPR */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006450 vmx_disable_intercept_msr_write_x2apic(0x808, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006451 /* EOI */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006452 vmx_disable_intercept_msr_write_x2apic(0x80b, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006453 /* SELF-IPI */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006454 vmx_disable_intercept_msr_write_x2apic(0x83f, true);
6455
6456 /*
6457 * (enable_apicv && !kvm_vcpu_apicv_active()) ||
6458 * !enable_apicv
6459 */
6460 /* TPR */
6461 vmx_disable_intercept_msr_read_x2apic(0x808, false);
6462 vmx_disable_intercept_msr_write_x2apic(0x808, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006463
6464 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006465 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006466 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6467 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006468 0ull, VMX_EPT_EXECUTABLE_MASK,
6469 cpu_has_vmx_ept_execute_only() ?
6470 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006471 ept_set_mmio_spte_mask();
6472 kvm_enable_tdp();
6473 } else
6474 kvm_disable_tdp();
6475
6476 update_ple_window_actual_max();
6477
Kai Huang843e4332015-01-28 10:54:28 +08006478 /*
6479 * Only enable PML when hardware supports PML feature, and both EPT
6480 * and EPT A/D bit features are enabled -- PML depends on them to work.
6481 */
6482 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6483 enable_pml = 0;
6484
6485 if (!enable_pml) {
6486 kvm_x86_ops->slot_enable_log_dirty = NULL;
6487 kvm_x86_ops->slot_disable_log_dirty = NULL;
6488 kvm_x86_ops->flush_log_dirty = NULL;
6489 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6490 }
6491
Yunhong Jiang64672c92016-06-13 14:19:59 -07006492 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6493 u64 vmx_msr;
6494
6495 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6496 cpu_preemption_timer_multi =
6497 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6498 } else {
6499 kvm_x86_ops->set_hv_timer = NULL;
6500 kvm_x86_ops->cancel_hv_timer = NULL;
6501 }
6502
Feng Wubf9f6ac2015-09-18 22:29:55 +08006503 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6504
Ashok Rajc45dcc72016-06-22 14:59:56 +08006505 kvm_mce_cap_supported |= MCG_LMCE_P;
6506
Tiejun Chenf2c76482014-10-28 10:14:47 +08006507 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006508
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006509out9:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006510 free_page((unsigned long)vmx_vmwrite_bitmap);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006511out8:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006512 free_page((unsigned long)vmx_vmread_bitmap);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006513out7:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006514 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Wanpeng Lic63e4562016-09-23 19:17:16 +08006515out6:
6516 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006517out5:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006518 free_page((unsigned long)vmx_msr_bitmap_longmode);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006519out4:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006520 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Wanpeng Lic63e4562016-09-23 19:17:16 +08006521out3:
6522 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006523out2:
6524 free_page((unsigned long)vmx_msr_bitmap_legacy);
6525out1:
6526 free_page((unsigned long)vmx_io_bitmap_b);
6527out:
6528 free_page((unsigned long)vmx_io_bitmap_a);
6529
6530 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006531}
6532
6533static __exit void hardware_unsetup(void)
6534{
Wanpeng Lic63e4562016-09-23 19:17:16 +08006535 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006536 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Wanpeng Lic63e4562016-09-23 19:17:16 +08006537 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006538 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6539 free_page((unsigned long)vmx_msr_bitmap_legacy);
6540 free_page((unsigned long)vmx_msr_bitmap_longmode);
6541 free_page((unsigned long)vmx_io_bitmap_b);
6542 free_page((unsigned long)vmx_io_bitmap_a);
6543 free_page((unsigned long)vmx_vmwrite_bitmap);
6544 free_page((unsigned long)vmx_vmread_bitmap);
6545
Tiejun Chenf2c76482014-10-28 10:14:47 +08006546 free_kvm_area();
6547}
6548
Avi Kivity6aa8b732006-12-10 02:21:36 -08006549/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006550 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6551 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6552 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006553static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006554{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006555 if (ple_gap)
6556 grow_ple_window(vcpu);
6557
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006558 skip_emulated_instruction(vcpu);
6559 kvm_vcpu_on_spin(vcpu);
6560
6561 return 1;
6562}
6563
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006564static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006565{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006566 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006567 return 1;
6568}
6569
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006570static int handle_mwait(struct kvm_vcpu *vcpu)
6571{
6572 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6573 return handle_nop(vcpu);
6574}
6575
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006576static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6577{
6578 return 1;
6579}
6580
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006581static int handle_monitor(struct kvm_vcpu *vcpu)
6582{
6583 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6584 return handle_nop(vcpu);
6585}
6586
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006587/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006588 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6589 * We could reuse a single VMCS for all the L2 guests, but we also want the
6590 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6591 * allows keeping them loaded on the processor, and in the future will allow
6592 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6593 * every entry if they never change.
6594 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6595 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6596 *
6597 * The following functions allocate and free a vmcs02 in this pool.
6598 */
6599
6600/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6601static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6602{
6603 struct vmcs02_list *item;
6604 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6605 if (item->vmptr == vmx->nested.current_vmptr) {
6606 list_move(&item->list, &vmx->nested.vmcs02_pool);
6607 return &item->vmcs02;
6608 }
6609
6610 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6611 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08006612 item = list_last_entry(&vmx->nested.vmcs02_pool,
6613 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006614 item->vmptr = vmx->nested.current_vmptr;
6615 list_move(&item->list, &vmx->nested.vmcs02_pool);
6616 return &item->vmcs02;
6617 }
6618
6619 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006620 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006621 if (!item)
6622 return NULL;
6623 item->vmcs02.vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07006624 item->vmcs02.shadow_vmcs = NULL;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006625 if (!item->vmcs02.vmcs) {
6626 kfree(item);
6627 return NULL;
6628 }
6629 loaded_vmcs_init(&item->vmcs02);
6630 item->vmptr = vmx->nested.current_vmptr;
6631 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6632 vmx->nested.vmcs02_num++;
6633 return &item->vmcs02;
6634}
6635
6636/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6637static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6638{
6639 struct vmcs02_list *item;
6640 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6641 if (item->vmptr == vmptr) {
6642 free_loaded_vmcs(&item->vmcs02);
6643 list_del(&item->list);
6644 kfree(item);
6645 vmx->nested.vmcs02_num--;
6646 return;
6647 }
6648}
6649
6650/*
6651 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006652 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6653 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006654 */
6655static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6656{
6657 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006658
6659 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006660 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006661 /*
6662 * Something will leak if the above WARN triggers. Better than
6663 * a use-after-free.
6664 */
6665 if (vmx->loaded_vmcs == &item->vmcs02)
6666 continue;
6667
6668 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006669 list_del(&item->list);
6670 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006671 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006672 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006673}
6674
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006675/*
6676 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6677 * set the success or error code of an emulated VMX instruction, as specified
6678 * by Vol 2B, VMX Instruction Reference, "Conventions".
6679 */
6680static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6681{
6682 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6683 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6684 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6685}
6686
6687static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6688{
6689 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6690 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6691 X86_EFLAGS_SF | X86_EFLAGS_OF))
6692 | X86_EFLAGS_CF);
6693}
6694
Abel Gordon145c28d2013-04-18 14:36:55 +03006695static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006696 u32 vm_instruction_error)
6697{
6698 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6699 /*
6700 * failValid writes the error number to the current VMCS, which
6701 * can't be done there isn't a current VMCS.
6702 */
6703 nested_vmx_failInvalid(vcpu);
6704 return;
6705 }
6706 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6707 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6708 X86_EFLAGS_SF | X86_EFLAGS_OF))
6709 | X86_EFLAGS_ZF);
6710 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6711 /*
6712 * We don't need to force a shadow sync because
6713 * VM_INSTRUCTION_ERROR is not shadowed
6714 */
6715}
Abel Gordon145c28d2013-04-18 14:36:55 +03006716
Wincy Vanff651cb2014-12-11 08:52:58 +03006717static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6718{
6719 /* TODO: not to reset guest simply here. */
6720 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006721 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006722}
6723
Jan Kiszkaf41245002014-03-07 20:03:13 +01006724static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6725{
6726 struct vcpu_vmx *vmx =
6727 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6728
6729 vmx->nested.preemption_timer_expired = true;
6730 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6731 kvm_vcpu_kick(&vmx->vcpu);
6732
6733 return HRTIMER_NORESTART;
6734}
6735
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006736/*
Bandan Das19677e32014-05-06 02:19:15 -04006737 * Decode the memory-address operand of a vmx instruction, as recorded on an
6738 * exit caused by such an instruction (run by a guest hypervisor).
6739 * On success, returns 0. When the operand is invalid, returns 1 and throws
6740 * #UD or #GP.
6741 */
6742static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6743 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006744 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006745{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006746 gva_t off;
6747 bool exn;
6748 struct kvm_segment s;
6749
Bandan Das19677e32014-05-06 02:19:15 -04006750 /*
6751 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6752 * Execution", on an exit, vmx_instruction_info holds most of the
6753 * addressing components of the operand. Only the displacement part
6754 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6755 * For how an actual address is calculated from all these components,
6756 * refer to Vol. 1, "Operand Addressing".
6757 */
6758 int scaling = vmx_instruction_info & 3;
6759 int addr_size = (vmx_instruction_info >> 7) & 7;
6760 bool is_reg = vmx_instruction_info & (1u << 10);
6761 int seg_reg = (vmx_instruction_info >> 15) & 7;
6762 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6763 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6764 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6765 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6766
6767 if (is_reg) {
6768 kvm_queue_exception(vcpu, UD_VECTOR);
6769 return 1;
6770 }
6771
6772 /* Addr = segment_base + offset */
6773 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006774 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006775 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006776 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006777 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006778 off += kvm_register_read(vcpu, index_reg)<<scaling;
6779 vmx_get_segment(vcpu, &s, seg_reg);
6780 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006781
6782 if (addr_size == 1) /* 32 bit */
6783 *ret &= 0xffffffff;
6784
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006785 /* Checks for #GP/#SS exceptions. */
6786 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006787 if (is_long_mode(vcpu)) {
6788 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6789 * non-canonical form. This is the only check on the memory
6790 * destination for long mode!
6791 */
6792 exn = is_noncanonical_address(*ret);
6793 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006794 /* Protected mode: apply checks for segment validity in the
6795 * following order:
6796 * - segment type check (#GP(0) may be thrown)
6797 * - usability check (#GP(0)/#SS(0))
6798 * - limit check (#GP(0)/#SS(0))
6799 */
6800 if (wr)
6801 /* #GP(0) if the destination operand is located in a
6802 * read-only data segment or any code segment.
6803 */
6804 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6805 else
6806 /* #GP(0) if the source operand is located in an
6807 * execute-only code segment
6808 */
6809 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006810 if (exn) {
6811 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6812 return 1;
6813 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006814 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6815 */
6816 exn = (s.unusable != 0);
6817 /* Protected mode: #GP(0)/#SS(0) if the memory
6818 * operand is outside the segment limit.
6819 */
6820 exn = exn || (off + sizeof(u64) > s.limit);
6821 }
6822 if (exn) {
6823 kvm_queue_exception_e(vcpu,
6824 seg_reg == VCPU_SREG_SS ?
6825 SS_VECTOR : GP_VECTOR,
6826 0);
6827 return 1;
6828 }
6829
Bandan Das19677e32014-05-06 02:19:15 -04006830 return 0;
6831}
6832
6833/*
Bandan Das3573e222014-05-06 02:19:16 -04006834 * This function performs the various checks including
6835 * - if it's 4KB aligned
6836 * - No bits beyond the physical address width are set
6837 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006838 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006839 */
Bandan Das4291b582014-05-06 02:19:18 -04006840static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6841 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006842{
6843 gva_t gva;
6844 gpa_t vmptr;
6845 struct x86_exception e;
6846 struct page *page;
6847 struct vcpu_vmx *vmx = to_vmx(vcpu);
6848 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6849
6850 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006851 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006852 return 1;
6853
6854 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6855 sizeof(vmptr), &e)) {
6856 kvm_inject_page_fault(vcpu, &e);
6857 return 1;
6858 }
6859
6860 switch (exit_reason) {
6861 case EXIT_REASON_VMON:
6862 /*
6863 * SDM 3: 24.11.5
6864 * The first 4 bytes of VMXON region contain the supported
6865 * VMCS revision identifier
6866 *
6867 * Note - IA32_VMX_BASIC[48] will never be 1
6868 * for the nested case;
6869 * which replaces physical address width with 32
6870 *
6871 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006872 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006873 nested_vmx_failInvalid(vcpu);
6874 skip_emulated_instruction(vcpu);
6875 return 1;
6876 }
6877
6878 page = nested_get_page(vcpu, vmptr);
6879 if (page == NULL ||
6880 *(u32 *)kmap(page) != VMCS12_REVISION) {
6881 nested_vmx_failInvalid(vcpu);
6882 kunmap(page);
6883 skip_emulated_instruction(vcpu);
6884 return 1;
6885 }
6886 kunmap(page);
6887 vmx->nested.vmxon_ptr = vmptr;
6888 break;
Bandan Das4291b582014-05-06 02:19:18 -04006889 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006890 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006891 nested_vmx_failValid(vcpu,
6892 VMXERR_VMCLEAR_INVALID_ADDRESS);
6893 skip_emulated_instruction(vcpu);
6894 return 1;
6895 }
Bandan Das3573e222014-05-06 02:19:16 -04006896
Bandan Das4291b582014-05-06 02:19:18 -04006897 if (vmptr == vmx->nested.vmxon_ptr) {
6898 nested_vmx_failValid(vcpu,
6899 VMXERR_VMCLEAR_VMXON_POINTER);
6900 skip_emulated_instruction(vcpu);
6901 return 1;
6902 }
6903 break;
6904 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006905 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006906 nested_vmx_failValid(vcpu,
6907 VMXERR_VMPTRLD_INVALID_ADDRESS);
6908 skip_emulated_instruction(vcpu);
6909 return 1;
6910 }
6911
6912 if (vmptr == vmx->nested.vmxon_ptr) {
6913 nested_vmx_failValid(vcpu,
6914 VMXERR_VMCLEAR_VMXON_POINTER);
6915 skip_emulated_instruction(vcpu);
6916 return 1;
6917 }
6918 break;
Bandan Das3573e222014-05-06 02:19:16 -04006919 default:
6920 return 1; /* shouldn't happen */
6921 }
6922
Bandan Das4291b582014-05-06 02:19:18 -04006923 if (vmpointer)
6924 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006925 return 0;
6926}
6927
6928/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006929 * Emulate the VMXON instruction.
6930 * Currently, we just remember that VMX is active, and do not save or even
6931 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6932 * do not currently need to store anything in that guest-allocated memory
6933 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6934 * argument is different from the VMXON pointer (which the spec says they do).
6935 */
6936static int handle_vmon(struct kvm_vcpu *vcpu)
6937{
6938 struct kvm_segment cs;
6939 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03006940 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006941 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6942 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006943
6944 /* The Intel VMX Instruction Reference lists a bunch of bits that
6945 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6946 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6947 * Otherwise, we should fail with #UD. We test these now:
6948 */
6949 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6950 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6951 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6952 kvm_queue_exception(vcpu, UD_VECTOR);
6953 return 1;
6954 }
6955
6956 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6957 if (is_long_mode(vcpu) && !cs.l) {
6958 kvm_queue_exception(vcpu, UD_VECTOR);
6959 return 1;
6960 }
6961
6962 if (vmx_get_cpl(vcpu)) {
6963 kvm_inject_gp(vcpu, 0);
6964 return 1;
6965 }
Bandan Das3573e222014-05-06 02:19:16 -04006966
Bandan Das4291b582014-05-06 02:19:18 -04006967 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04006968 return 1;
6969
Abel Gordon145c28d2013-04-18 14:36:55 +03006970 if (vmx->nested.vmxon) {
6971 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6972 skip_emulated_instruction(vcpu);
6973 return 1;
6974 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006975
Haozhong Zhang3b840802016-06-22 14:59:54 +08006976 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006977 != VMXON_NEEDED_FEATURES) {
6978 kvm_inject_gp(vcpu, 0);
6979 return 1;
6980 }
6981
Radim Krčmářd048c092016-08-08 20:16:22 +02006982 if (cpu_has_vmx_msr_bitmap()) {
6983 vmx->nested.msr_bitmap =
6984 (unsigned long *)__get_free_page(GFP_KERNEL);
6985 if (!vmx->nested.msr_bitmap)
6986 goto out_msr_bitmap;
6987 }
6988
David Matlack4f2777b2016-07-13 17:16:37 -07006989 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
6990 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02006991 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07006992
Abel Gordon8de48832013-04-18 14:37:25 +03006993 if (enable_shadow_vmcs) {
6994 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02006995 if (!shadow_vmcs)
6996 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03006997 /* mark vmcs as shadow */
6998 shadow_vmcs->revision_id |= (1u << 31);
6999 /* init shadow vmcs */
7000 vmcs_clear(shadow_vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07007001 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007002 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007003
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007004 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7005 vmx->nested.vmcs02_num = 0;
7006
Jan Kiszkaf41245002014-03-07 20:03:13 +01007007 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007008 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf41245002014-03-07 20:03:13 +01007009 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7010
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007011 vmx->nested.vmxon = true;
7012
7013 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007014 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007015 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007016
7017out_shadow_vmcs:
7018 kfree(vmx->nested.cached_vmcs12);
7019
7020out_cached_vmcs12:
7021 free_page((unsigned long)vmx->nested.msr_bitmap);
7022
7023out_msr_bitmap:
7024 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007025}
7026
7027/*
7028 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7029 * for running VMX instructions (except VMXON, whose prerequisites are
7030 * slightly different). It also specifies what exception to inject otherwise.
7031 */
7032static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7033{
7034 struct kvm_segment cs;
7035 struct vcpu_vmx *vmx = to_vmx(vcpu);
7036
7037 if (!vmx->nested.vmxon) {
7038 kvm_queue_exception(vcpu, UD_VECTOR);
7039 return 0;
7040 }
7041
7042 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7043 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7044 (is_long_mode(vcpu) && !cs.l)) {
7045 kvm_queue_exception(vcpu, UD_VECTOR);
7046 return 0;
7047 }
7048
7049 if (vmx_get_cpl(vcpu)) {
7050 kvm_inject_gp(vcpu, 0);
7051 return 0;
7052 }
7053
7054 return 1;
7055}
7056
Abel Gordone7953d72013-04-18 14:37:55 +03007057static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7058{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007059 if (vmx->nested.current_vmptr == -1ull)
7060 return;
7061
7062 /* current_vmptr and current_vmcs12 are always set/reset together */
7063 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7064 return;
7065
Abel Gordon012f83c2013-04-18 14:39:25 +03007066 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007067 /* copy to memory all shadowed fields in case
7068 they were modified */
7069 copy_shadow_to_vmcs12(vmx);
7070 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007071 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7072 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007073 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007074 }
Wincy Van705699a2015-02-03 23:58:17 +08007075 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007076
7077 /* Flush VMCS12 to guest memory */
7078 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7079 VMCS12_SIZE);
7080
Abel Gordone7953d72013-04-18 14:37:55 +03007081 kunmap(vmx->nested.current_vmcs12_page);
7082 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007083 vmx->nested.current_vmptr = -1ull;
7084 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007085}
7086
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007087/*
7088 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7089 * just stops using VMX.
7090 */
7091static void free_nested(struct vcpu_vmx *vmx)
7092{
7093 if (!vmx->nested.vmxon)
7094 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007095
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007096 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007097 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007098 nested_release_vmcs12(vmx);
Radim Krčmářd048c092016-08-08 20:16:22 +02007099 if (vmx->nested.msr_bitmap) {
7100 free_page((unsigned long)vmx->nested.msr_bitmap);
7101 vmx->nested.msr_bitmap = NULL;
7102 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007103 if (enable_shadow_vmcs) {
7104 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7105 free_vmcs(vmx->vmcs01.shadow_vmcs);
7106 vmx->vmcs01.shadow_vmcs = NULL;
7107 }
David Matlack4f2777b2016-07-13 17:16:37 -07007108 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007109 /* Unpin physical memory we referred to in current vmcs02 */
7110 if (vmx->nested.apic_access_page) {
7111 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007112 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007113 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007114 if (vmx->nested.virtual_apic_page) {
7115 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007116 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007117 }
Wincy Van705699a2015-02-03 23:58:17 +08007118 if (vmx->nested.pi_desc_page) {
7119 kunmap(vmx->nested.pi_desc_page);
7120 nested_release_page(vmx->nested.pi_desc_page);
7121 vmx->nested.pi_desc_page = NULL;
7122 vmx->nested.pi_desc = NULL;
7123 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007124
7125 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007126}
7127
7128/* Emulate the VMXOFF instruction */
7129static int handle_vmoff(struct kvm_vcpu *vcpu)
7130{
7131 if (!nested_vmx_check_permission(vcpu))
7132 return 1;
7133 free_nested(to_vmx(vcpu));
7134 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007135 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007136 return 1;
7137}
7138
Nadav Har'El27d6c862011-05-25 23:06:59 +03007139/* Emulate the VMCLEAR instruction */
7140static int handle_vmclear(struct kvm_vcpu *vcpu)
7141{
7142 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007143 gpa_t vmptr;
7144 struct vmcs12 *vmcs12;
7145 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007146
7147 if (!nested_vmx_check_permission(vcpu))
7148 return 1;
7149
Bandan Das4291b582014-05-06 02:19:18 -04007150 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007151 return 1;
7152
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007153 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007154 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007155
7156 page = nested_get_page(vcpu, vmptr);
7157 if (page == NULL) {
7158 /*
7159 * For accurate processor emulation, VMCLEAR beyond available
7160 * physical memory should do nothing at all. However, it is
7161 * possible that a nested vmx bug, not a guest hypervisor bug,
7162 * resulted in this case, so let's shut down before doing any
7163 * more damage:
7164 */
7165 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7166 return 1;
7167 }
7168 vmcs12 = kmap(page);
7169 vmcs12->launch_state = 0;
7170 kunmap(page);
7171 nested_release_page(page);
7172
7173 nested_free_vmcs02(vmx, vmptr);
7174
7175 skip_emulated_instruction(vcpu);
7176 nested_vmx_succeed(vcpu);
7177 return 1;
7178}
7179
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007180static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7181
7182/* Emulate the VMLAUNCH instruction */
7183static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7184{
7185 return nested_vmx_run(vcpu, true);
7186}
7187
7188/* Emulate the VMRESUME instruction */
7189static int handle_vmresume(struct kvm_vcpu *vcpu)
7190{
7191
7192 return nested_vmx_run(vcpu, false);
7193}
7194
Nadav Har'El49f705c2011-05-25 23:08:30 +03007195enum vmcs_field_type {
7196 VMCS_FIELD_TYPE_U16 = 0,
7197 VMCS_FIELD_TYPE_U64 = 1,
7198 VMCS_FIELD_TYPE_U32 = 2,
7199 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7200};
7201
7202static inline int vmcs_field_type(unsigned long field)
7203{
7204 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7205 return VMCS_FIELD_TYPE_U32;
7206 return (field >> 13) & 0x3 ;
7207}
7208
7209static inline int vmcs_field_readonly(unsigned long field)
7210{
7211 return (((field >> 10) & 0x3) == 1);
7212}
7213
7214/*
7215 * Read a vmcs12 field. Since these can have varying lengths and we return
7216 * one type, we chose the biggest type (u64) and zero-extend the return value
7217 * to that size. Note that the caller, handle_vmread, might need to use only
7218 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7219 * 64-bit fields are to be returned).
7220 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007221static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7222 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007223{
7224 short offset = vmcs_field_to_offset(field);
7225 char *p;
7226
7227 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007228 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007229
7230 p = ((char *)(get_vmcs12(vcpu))) + offset;
7231
7232 switch (vmcs_field_type(field)) {
7233 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7234 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007235 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007236 case VMCS_FIELD_TYPE_U16:
7237 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007238 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007239 case VMCS_FIELD_TYPE_U32:
7240 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007241 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007242 case VMCS_FIELD_TYPE_U64:
7243 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007244 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007245 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007246 WARN_ON(1);
7247 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007248 }
7249}
7250
Abel Gordon20b97fe2013-04-18 14:36:25 +03007251
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007252static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7253 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007254 short offset = vmcs_field_to_offset(field);
7255 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7256 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007257 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007258
7259 switch (vmcs_field_type(field)) {
7260 case VMCS_FIELD_TYPE_U16:
7261 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007262 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007263 case VMCS_FIELD_TYPE_U32:
7264 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007265 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007266 case VMCS_FIELD_TYPE_U64:
7267 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007268 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007269 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7270 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007271 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007272 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007273 WARN_ON(1);
7274 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007275 }
7276
7277}
7278
Abel Gordon16f5b902013-04-18 14:38:25 +03007279static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7280{
7281 int i;
7282 unsigned long field;
7283 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007284 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007285 const unsigned long *fields = shadow_read_write_fields;
7286 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007287
Jan Kiszka282da872014-10-08 18:05:39 +02007288 preempt_disable();
7289
Abel Gordon16f5b902013-04-18 14:38:25 +03007290 vmcs_load(shadow_vmcs);
7291
7292 for (i = 0; i < num_fields; i++) {
7293 field = fields[i];
7294 switch (vmcs_field_type(field)) {
7295 case VMCS_FIELD_TYPE_U16:
7296 field_value = vmcs_read16(field);
7297 break;
7298 case VMCS_FIELD_TYPE_U32:
7299 field_value = vmcs_read32(field);
7300 break;
7301 case VMCS_FIELD_TYPE_U64:
7302 field_value = vmcs_read64(field);
7303 break;
7304 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7305 field_value = vmcs_readl(field);
7306 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007307 default:
7308 WARN_ON(1);
7309 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007310 }
7311 vmcs12_write_any(&vmx->vcpu, field, field_value);
7312 }
7313
7314 vmcs_clear(shadow_vmcs);
7315 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007316
7317 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007318}
7319
Abel Gordonc3114422013-04-18 14:38:55 +03007320static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7321{
Mathias Krausec2bae892013-06-26 20:36:21 +02007322 const unsigned long *fields[] = {
7323 shadow_read_write_fields,
7324 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007325 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007326 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007327 max_shadow_read_write_fields,
7328 max_shadow_read_only_fields
7329 };
7330 int i, q;
7331 unsigned long field;
7332 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007333 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007334
7335 vmcs_load(shadow_vmcs);
7336
Mathias Krausec2bae892013-06-26 20:36:21 +02007337 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007338 for (i = 0; i < max_fields[q]; i++) {
7339 field = fields[q][i];
7340 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7341
7342 switch (vmcs_field_type(field)) {
7343 case VMCS_FIELD_TYPE_U16:
7344 vmcs_write16(field, (u16)field_value);
7345 break;
7346 case VMCS_FIELD_TYPE_U32:
7347 vmcs_write32(field, (u32)field_value);
7348 break;
7349 case VMCS_FIELD_TYPE_U64:
7350 vmcs_write64(field, (u64)field_value);
7351 break;
7352 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7353 vmcs_writel(field, (long)field_value);
7354 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007355 default:
7356 WARN_ON(1);
7357 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007358 }
7359 }
7360 }
7361
7362 vmcs_clear(shadow_vmcs);
7363 vmcs_load(vmx->loaded_vmcs->vmcs);
7364}
7365
Nadav Har'El49f705c2011-05-25 23:08:30 +03007366/*
7367 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7368 * used before) all generate the same failure when it is missing.
7369 */
7370static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7371{
7372 struct vcpu_vmx *vmx = to_vmx(vcpu);
7373 if (vmx->nested.current_vmptr == -1ull) {
7374 nested_vmx_failInvalid(vcpu);
7375 skip_emulated_instruction(vcpu);
7376 return 0;
7377 }
7378 return 1;
7379}
7380
7381static int handle_vmread(struct kvm_vcpu *vcpu)
7382{
7383 unsigned long field;
7384 u64 field_value;
7385 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7386 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7387 gva_t gva = 0;
7388
7389 if (!nested_vmx_check_permission(vcpu) ||
7390 !nested_vmx_check_vmcs12(vcpu))
7391 return 1;
7392
7393 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007394 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007395 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007396 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007397 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7398 skip_emulated_instruction(vcpu);
7399 return 1;
7400 }
7401 /*
7402 * Now copy part of this value to register or memory, as requested.
7403 * Note that the number of bits actually copied is 32 or 64 depending
7404 * on the guest's mode (32 or 64 bit), not on the given field's length.
7405 */
7406 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007407 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007408 field_value);
7409 } else {
7410 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007411 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007412 return 1;
7413 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7414 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7415 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7416 }
7417
7418 nested_vmx_succeed(vcpu);
7419 skip_emulated_instruction(vcpu);
7420 return 1;
7421}
7422
7423
7424static int handle_vmwrite(struct kvm_vcpu *vcpu)
7425{
7426 unsigned long field;
7427 gva_t gva;
7428 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7429 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007430 /* The value to write might be 32 or 64 bits, depending on L1's long
7431 * mode, and eventually we need to write that into a field of several
7432 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007433 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007434 * bits into the vmcs12 field.
7435 */
7436 u64 field_value = 0;
7437 struct x86_exception e;
7438
7439 if (!nested_vmx_check_permission(vcpu) ||
7440 !nested_vmx_check_vmcs12(vcpu))
7441 return 1;
7442
7443 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007444 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007445 (((vmx_instruction_info) >> 3) & 0xf));
7446 else {
7447 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007448 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007449 return 1;
7450 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007451 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007452 kvm_inject_page_fault(vcpu, &e);
7453 return 1;
7454 }
7455 }
7456
7457
Nadav Amit27e6fb52014-06-18 17:19:26 +03007458 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007459 if (vmcs_field_readonly(field)) {
7460 nested_vmx_failValid(vcpu,
7461 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7462 skip_emulated_instruction(vcpu);
7463 return 1;
7464 }
7465
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007466 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007467 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7468 skip_emulated_instruction(vcpu);
7469 return 1;
7470 }
7471
7472 nested_vmx_succeed(vcpu);
7473 skip_emulated_instruction(vcpu);
7474 return 1;
7475}
7476
Nadav Har'El63846662011-05-25 23:07:29 +03007477/* Emulate the VMPTRLD instruction */
7478static int handle_vmptrld(struct kvm_vcpu *vcpu)
7479{
7480 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007481 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007482
7483 if (!nested_vmx_check_permission(vcpu))
7484 return 1;
7485
Bandan Das4291b582014-05-06 02:19:18 -04007486 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007487 return 1;
7488
Nadav Har'El63846662011-05-25 23:07:29 +03007489 if (vmx->nested.current_vmptr != vmptr) {
7490 struct vmcs12 *new_vmcs12;
7491 struct page *page;
7492 page = nested_get_page(vcpu, vmptr);
7493 if (page == NULL) {
7494 nested_vmx_failInvalid(vcpu);
7495 skip_emulated_instruction(vcpu);
7496 return 1;
7497 }
7498 new_vmcs12 = kmap(page);
7499 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7500 kunmap(page);
7501 nested_release_page_clean(page);
7502 nested_vmx_failValid(vcpu,
7503 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7504 skip_emulated_instruction(vcpu);
7505 return 1;
7506 }
Nadav Har'El63846662011-05-25 23:07:29 +03007507
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007508 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007509 vmx->nested.current_vmptr = vmptr;
7510 vmx->nested.current_vmcs12 = new_vmcs12;
7511 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007512 /*
7513 * Load VMCS12 from guest memory since it is not already
7514 * cached.
7515 */
7516 memcpy(vmx->nested.cached_vmcs12,
7517 vmx->nested.current_vmcs12, VMCS12_SIZE);
7518
Abel Gordon012f83c2013-04-18 14:39:25 +03007519 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007520 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7521 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007522 vmcs_write64(VMCS_LINK_POINTER,
Jim Mattson355f4fb2016-10-28 08:29:39 -07007523 __pa(vmx->vmcs01.shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007524 vmx->nested.sync_shadow_vmcs = true;
7525 }
Nadav Har'El63846662011-05-25 23:07:29 +03007526 }
7527
7528 nested_vmx_succeed(vcpu);
7529 skip_emulated_instruction(vcpu);
7530 return 1;
7531}
7532
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007533/* Emulate the VMPTRST instruction */
7534static int handle_vmptrst(struct kvm_vcpu *vcpu)
7535{
7536 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7537 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7538 gva_t vmcs_gva;
7539 struct x86_exception e;
7540
7541 if (!nested_vmx_check_permission(vcpu))
7542 return 1;
7543
7544 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007545 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007546 return 1;
7547 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7548 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7549 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7550 sizeof(u64), &e)) {
7551 kvm_inject_page_fault(vcpu, &e);
7552 return 1;
7553 }
7554 nested_vmx_succeed(vcpu);
7555 skip_emulated_instruction(vcpu);
7556 return 1;
7557}
7558
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007559/* Emulate the INVEPT instruction */
7560static int handle_invept(struct kvm_vcpu *vcpu)
7561{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007562 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007563 u32 vmx_instruction_info, types;
7564 unsigned long type;
7565 gva_t gva;
7566 struct x86_exception e;
7567 struct {
7568 u64 eptp, gpa;
7569 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007570
Wincy Vanb9c237b2015-02-03 23:56:30 +08007571 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7572 SECONDARY_EXEC_ENABLE_EPT) ||
7573 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007574 kvm_queue_exception(vcpu, UD_VECTOR);
7575 return 1;
7576 }
7577
7578 if (!nested_vmx_check_permission(vcpu))
7579 return 1;
7580
7581 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7582 kvm_queue_exception(vcpu, UD_VECTOR);
7583 return 1;
7584 }
7585
7586 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007587 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007588
Wincy Vanb9c237b2015-02-03 23:56:30 +08007589 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007590
Jim Mattson85c856b2016-10-26 08:38:38 -07007591 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007592 nested_vmx_failValid(vcpu,
7593 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007594 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007595 return 1;
7596 }
7597
7598 /* According to the Intel VMX instruction reference, the memory
7599 * operand is read even if it isn't needed (e.g., for type==global)
7600 */
7601 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007602 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007603 return 1;
7604 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7605 sizeof(operand), &e)) {
7606 kvm_inject_page_fault(vcpu, &e);
7607 return 1;
7608 }
7609
7610 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007611 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007612 /*
7613 * TODO: track mappings and invalidate
7614 * single context requests appropriately
7615 */
7616 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007617 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007618 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007619 nested_vmx_succeed(vcpu);
7620 break;
7621 default:
7622 BUG_ON(1);
7623 break;
7624 }
7625
7626 skip_emulated_instruction(vcpu);
7627 return 1;
7628}
7629
Petr Matouseka642fc32014-09-23 20:22:30 +02007630static int handle_invvpid(struct kvm_vcpu *vcpu)
7631{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007632 struct vcpu_vmx *vmx = to_vmx(vcpu);
7633 u32 vmx_instruction_info;
7634 unsigned long type, types;
7635 gva_t gva;
7636 struct x86_exception e;
7637 int vpid;
7638
7639 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7640 SECONDARY_EXEC_ENABLE_VPID) ||
7641 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7642 kvm_queue_exception(vcpu, UD_VECTOR);
7643 return 1;
7644 }
7645
7646 if (!nested_vmx_check_permission(vcpu))
7647 return 1;
7648
7649 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7650 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7651
7652 types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7;
7653
Jim Mattson85c856b2016-10-26 08:38:38 -07007654 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007655 nested_vmx_failValid(vcpu,
7656 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007657 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007658 return 1;
7659 }
7660
7661 /* according to the intel vmx instruction reference, the memory
7662 * operand is read even if it isn't needed (e.g., for type==global)
7663 */
7664 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7665 vmx_instruction_info, false, &gva))
7666 return 1;
7667 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7668 sizeof(u32), &e)) {
7669 kvm_inject_page_fault(vcpu, &e);
7670 return 1;
7671 }
7672
7673 switch (type) {
Paolo Bonzinief697a72016-03-18 16:58:38 +01007674 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
7675 /*
7676 * Old versions of KVM use the single-context version so we
7677 * have to support it; just treat it the same as all-context.
7678 */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007679 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li5c614b32015-10-13 09:18:36 -07007680 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007681 nested_vmx_succeed(vcpu);
7682 break;
7683 default:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007684 /* Trap individual address invalidation invvpid calls */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007685 BUG_ON(1);
7686 break;
7687 }
7688
7689 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007690 return 1;
7691}
7692
Kai Huang843e4332015-01-28 10:54:28 +08007693static int handle_pml_full(struct kvm_vcpu *vcpu)
7694{
7695 unsigned long exit_qualification;
7696
7697 trace_kvm_pml_full(vcpu->vcpu_id);
7698
7699 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7700
7701 /*
7702 * PML buffer FULL happened while executing iret from NMI,
7703 * "blocked by NMI" bit has to be set before next VM entry.
7704 */
7705 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7706 cpu_has_virtual_nmis() &&
7707 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7708 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7709 GUEST_INTR_STATE_NMI);
7710
7711 /*
7712 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7713 * here.., and there's no userspace involvement needed for PML.
7714 */
7715 return 1;
7716}
7717
Yunhong Jiang64672c92016-06-13 14:19:59 -07007718static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7719{
7720 kvm_lapic_expired_hv_timer(vcpu);
7721 return 1;
7722}
7723
Nadav Har'El0140cae2011-05-25 23:06:28 +03007724/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007725 * The exit handlers return 1 if the exit was handled fully and guest execution
7726 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7727 * to be done to userspace and return 0.
7728 */
Mathias Krause772e0312012-08-30 01:30:19 +02007729static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007730 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7731 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007732 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007733 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007734 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007735 [EXIT_REASON_CR_ACCESS] = handle_cr,
7736 [EXIT_REASON_DR_ACCESS] = handle_dr,
7737 [EXIT_REASON_CPUID] = handle_cpuid,
7738 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7739 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7740 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7741 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007742 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007743 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007744 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007745 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007746 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007747 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007748 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007749 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007750 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007751 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007752 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007753 [EXIT_REASON_VMOFF] = handle_vmoff,
7754 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007755 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7756 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007757 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007758 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007759 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007760 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007761 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007762 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007763 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7764 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007765 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007766 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007767 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007768 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007769 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007770 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007771 [EXIT_REASON_XSAVES] = handle_xsaves,
7772 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007773 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007774 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007775};
7776
7777static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007778 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007779
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007780static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7781 struct vmcs12 *vmcs12)
7782{
7783 unsigned long exit_qualification;
7784 gpa_t bitmap, last_bitmap;
7785 unsigned int port;
7786 int size;
7787 u8 b;
7788
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007789 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007790 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007791
7792 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7793
7794 port = exit_qualification >> 16;
7795 size = (exit_qualification & 7) + 1;
7796
7797 last_bitmap = (gpa_t)-1;
7798 b = -1;
7799
7800 while (size > 0) {
7801 if (port < 0x8000)
7802 bitmap = vmcs12->io_bitmap_a;
7803 else if (port < 0x10000)
7804 bitmap = vmcs12->io_bitmap_b;
7805 else
Joe Perches1d804d02015-03-30 16:46:09 -07007806 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007807 bitmap += (port & 0x7fff) / 8;
7808
7809 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007810 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007811 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007812 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007813 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007814
7815 port++;
7816 size--;
7817 last_bitmap = bitmap;
7818 }
7819
Joe Perches1d804d02015-03-30 16:46:09 -07007820 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007821}
7822
Nadav Har'El644d7112011-05-25 23:12:35 +03007823/*
7824 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7825 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7826 * disinterest in the current event (read or write a specific MSR) by using an
7827 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7828 */
7829static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7830 struct vmcs12 *vmcs12, u32 exit_reason)
7831{
7832 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7833 gpa_t bitmap;
7834
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007835 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007836 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007837
7838 /*
7839 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7840 * for the four combinations of read/write and low/high MSR numbers.
7841 * First we need to figure out which of the four to use:
7842 */
7843 bitmap = vmcs12->msr_bitmap;
7844 if (exit_reason == EXIT_REASON_MSR_WRITE)
7845 bitmap += 2048;
7846 if (msr_index >= 0xc0000000) {
7847 msr_index -= 0xc0000000;
7848 bitmap += 1024;
7849 }
7850
7851 /* Then read the msr_index'th bit from this bitmap: */
7852 if (msr_index < 1024*8) {
7853 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007854 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007855 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007856 return 1 & (b >> (msr_index & 7));
7857 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007858 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007859}
7860
7861/*
7862 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7863 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7864 * intercept (via guest_host_mask etc.) the current event.
7865 */
7866static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7867 struct vmcs12 *vmcs12)
7868{
7869 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7870 int cr = exit_qualification & 15;
7871 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007872 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007873
7874 switch ((exit_qualification >> 4) & 3) {
7875 case 0: /* mov to cr */
7876 switch (cr) {
7877 case 0:
7878 if (vmcs12->cr0_guest_host_mask &
7879 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007880 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007881 break;
7882 case 3:
7883 if ((vmcs12->cr3_target_count >= 1 &&
7884 vmcs12->cr3_target_value0 == val) ||
7885 (vmcs12->cr3_target_count >= 2 &&
7886 vmcs12->cr3_target_value1 == val) ||
7887 (vmcs12->cr3_target_count >= 3 &&
7888 vmcs12->cr3_target_value2 == val) ||
7889 (vmcs12->cr3_target_count >= 4 &&
7890 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007891 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007892 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007893 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007894 break;
7895 case 4:
7896 if (vmcs12->cr4_guest_host_mask &
7897 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007898 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007899 break;
7900 case 8:
7901 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007902 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007903 break;
7904 }
7905 break;
7906 case 2: /* clts */
7907 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7908 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007909 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007910 break;
7911 case 1: /* mov from cr */
7912 switch (cr) {
7913 case 3:
7914 if (vmcs12->cpu_based_vm_exec_control &
7915 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007916 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007917 break;
7918 case 8:
7919 if (vmcs12->cpu_based_vm_exec_control &
7920 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007921 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007922 break;
7923 }
7924 break;
7925 case 3: /* lmsw */
7926 /*
7927 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7928 * cr0. Other attempted changes are ignored, with no exit.
7929 */
7930 if (vmcs12->cr0_guest_host_mask & 0xe &
7931 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007932 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007933 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7934 !(vmcs12->cr0_read_shadow & 0x1) &&
7935 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07007936 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007937 break;
7938 }
Joe Perches1d804d02015-03-30 16:46:09 -07007939 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007940}
7941
7942/*
7943 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
7944 * should handle it ourselves in L0 (and then continue L2). Only call this
7945 * when in is_guest_mode (L2).
7946 */
7947static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
7948{
Nadav Har'El644d7112011-05-25 23:12:35 +03007949 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7950 struct vcpu_vmx *vmx = to_vmx(vcpu);
7951 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01007952 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03007953
Jan Kiszka542060e2014-01-04 18:47:21 +01007954 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
7955 vmcs_readl(EXIT_QUALIFICATION),
7956 vmx->idt_vectoring_info,
7957 intr_info,
7958 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
7959 KVM_ISA_VMX);
7960
Nadav Har'El644d7112011-05-25 23:12:35 +03007961 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07007962 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007963
7964 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007965 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
7966 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07007967 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007968 }
7969
7970 switch (exit_reason) {
7971 case EXIT_REASON_EXCEPTION_NMI:
7972 if (!is_exception(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07007973 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007974 else if (is_page_fault(intr_info))
7975 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01007976 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01007977 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007978 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01007979 else if (is_debug(intr_info) &&
7980 vcpu->guest_debug &
7981 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
7982 return false;
7983 else if (is_breakpoint(intr_info) &&
7984 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
7985 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007986 return vmcs12->exception_bitmap &
7987 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
7988 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07007989 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007990 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07007991 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007992 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007993 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007994 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007995 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007996 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07007997 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007998 case EXIT_REASON_CPUID:
Marcelo Tosattibc613492014-09-18 18:24:57 -03007999 if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
Joe Perches1d804d02015-03-30 16:46:09 -07008000 return false;
8001 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008002 case EXIT_REASON_HLT:
8003 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8004 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008005 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008006 case EXIT_REASON_INVLPG:
8007 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8008 case EXIT_REASON_RDPMC:
8009 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008010 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008011 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8012 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8013 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8014 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8015 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8016 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008017 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008018 /*
8019 * VMX instructions trap unconditionally. This allows L1 to
8020 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8021 */
Joe Perches1d804d02015-03-30 16:46:09 -07008022 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008023 case EXIT_REASON_CR_ACCESS:
8024 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8025 case EXIT_REASON_DR_ACCESS:
8026 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8027 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008028 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008029 case EXIT_REASON_MSR_READ:
8030 case EXIT_REASON_MSR_WRITE:
8031 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8032 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008033 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008034 case EXIT_REASON_MWAIT_INSTRUCTION:
8035 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008036 case EXIT_REASON_MONITOR_TRAP_FLAG:
8037 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008038 case EXIT_REASON_MONITOR_INSTRUCTION:
8039 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8040 case EXIT_REASON_PAUSE_INSTRUCTION:
8041 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8042 nested_cpu_has2(vmcs12,
8043 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8044 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008045 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008046 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008047 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008048 case EXIT_REASON_APIC_ACCESS:
8049 return nested_cpu_has2(vmcs12,
8050 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008051 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008052 case EXIT_REASON_EOI_INDUCED:
8053 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008054 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008055 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008056 /*
8057 * L0 always deals with the EPT violation. If nested EPT is
8058 * used, and the nested mmu code discovers that the address is
8059 * missing in the guest EPT table (EPT12), the EPT violation
8060 * will be injected with nested_ept_inject_page_fault()
8061 */
Joe Perches1d804d02015-03-30 16:46:09 -07008062 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008063 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008064 /*
8065 * L2 never uses directly L1's EPT, but rather L0's own EPT
8066 * table (shadow on EPT) or a merged EPT table that L0 built
8067 * (EPT on EPT). So any problems with the structure of the
8068 * table is L0's fault.
8069 */
Joe Perches1d804d02015-03-30 16:46:09 -07008070 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008071 case EXIT_REASON_WBINVD:
8072 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8073 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008074 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008075 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8076 /*
8077 * This should never happen, since it is not possible to
8078 * set XSS to a non-zero value---neither in L1 nor in L2.
8079 * If if it were, XSS would have to be checked against
8080 * the XSS exit bitmap in vmcs12.
8081 */
8082 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008083 case EXIT_REASON_PREEMPTION_TIMER:
8084 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008085 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008086 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008087 }
8088}
8089
Avi Kivity586f9602010-11-18 13:09:54 +02008090static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8091{
8092 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8093 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8094}
8095
Kai Huanga3eaa862015-11-04 13:46:05 +08008096static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008097{
Kai Huanga3eaa862015-11-04 13:46:05 +08008098 if (vmx->pml_pg) {
8099 __free_page(vmx->pml_pg);
8100 vmx->pml_pg = NULL;
8101 }
Kai Huang843e4332015-01-28 10:54:28 +08008102}
8103
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008104static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008105{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008106 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008107 u64 *pml_buf;
8108 u16 pml_idx;
8109
8110 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8111
8112 /* Do nothing if PML buffer is empty */
8113 if (pml_idx == (PML_ENTITY_NUM - 1))
8114 return;
8115
8116 /* PML index always points to next available PML buffer entity */
8117 if (pml_idx >= PML_ENTITY_NUM)
8118 pml_idx = 0;
8119 else
8120 pml_idx++;
8121
8122 pml_buf = page_address(vmx->pml_pg);
8123 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8124 u64 gpa;
8125
8126 gpa = pml_buf[pml_idx];
8127 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008128 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008129 }
8130
8131 /* reset PML index */
8132 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8133}
8134
8135/*
8136 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8137 * Called before reporting dirty_bitmap to userspace.
8138 */
8139static void kvm_flush_pml_buffers(struct kvm *kvm)
8140{
8141 int i;
8142 struct kvm_vcpu *vcpu;
8143 /*
8144 * We only need to kick vcpu out of guest mode here, as PML buffer
8145 * is flushed at beginning of all VMEXITs, and it's obvious that only
8146 * vcpus running in guest are possible to have unflushed GPAs in PML
8147 * buffer.
8148 */
8149 kvm_for_each_vcpu(i, vcpu, kvm)
8150 kvm_vcpu_kick(vcpu);
8151}
8152
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008153static void vmx_dump_sel(char *name, uint32_t sel)
8154{
8155 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8156 name, vmcs_read32(sel),
8157 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8158 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8159 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8160}
8161
8162static void vmx_dump_dtsel(char *name, uint32_t limit)
8163{
8164 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8165 name, vmcs_read32(limit),
8166 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8167}
8168
8169static void dump_vmcs(void)
8170{
8171 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8172 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8173 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8174 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8175 u32 secondary_exec_control = 0;
8176 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008177 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008178 int i, n;
8179
8180 if (cpu_has_secondary_exec_ctrls())
8181 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8182
8183 pr_err("*** Guest State ***\n");
8184 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8185 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8186 vmcs_readl(CR0_GUEST_HOST_MASK));
8187 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8188 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8189 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8190 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8191 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8192 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008193 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8194 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8195 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8196 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008197 }
8198 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8199 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8200 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8201 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8202 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8203 vmcs_readl(GUEST_SYSENTER_ESP),
8204 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8205 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8206 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8207 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8208 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8209 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8210 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8211 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8212 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8213 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8214 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8215 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8216 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008217 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8218 efer, vmcs_read64(GUEST_IA32_PAT));
8219 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8220 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008221 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8222 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008223 pr_err("PerfGlobCtl = 0x%016llx\n",
8224 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008225 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008226 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008227 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8228 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8229 vmcs_read32(GUEST_ACTIVITY_STATE));
8230 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8231 pr_err("InterruptStatus = %04x\n",
8232 vmcs_read16(GUEST_INTR_STATUS));
8233
8234 pr_err("*** Host State ***\n");
8235 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8236 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8237 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8238 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8239 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8240 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8241 vmcs_read16(HOST_TR_SELECTOR));
8242 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8243 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8244 vmcs_readl(HOST_TR_BASE));
8245 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8246 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8247 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8248 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8249 vmcs_readl(HOST_CR4));
8250 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8251 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8252 vmcs_read32(HOST_IA32_SYSENTER_CS),
8253 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8254 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008255 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8256 vmcs_read64(HOST_IA32_EFER),
8257 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008258 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008259 pr_err("PerfGlobCtl = 0x%016llx\n",
8260 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008261
8262 pr_err("*** Control State ***\n");
8263 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8264 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8265 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8266 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8267 vmcs_read32(EXCEPTION_BITMAP),
8268 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8269 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8270 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8271 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8272 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8273 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8274 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8275 vmcs_read32(VM_EXIT_INTR_INFO),
8276 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8277 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8278 pr_err(" reason=%08x qualification=%016lx\n",
8279 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8280 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8281 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8282 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008283 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008284 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008285 pr_err("TSC Multiplier = 0x%016llx\n",
8286 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008287 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8288 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8289 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8290 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8291 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008292 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008293 n = vmcs_read32(CR3_TARGET_COUNT);
8294 for (i = 0; i + 1 < n; i += 4)
8295 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8296 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8297 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8298 if (i < n)
8299 pr_err("CR3 target%u=%016lx\n",
8300 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8301 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8302 pr_err("PLE Gap=%08x Window=%08x\n",
8303 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8304 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8305 pr_err("Virtual processor ID = 0x%04x\n",
8306 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8307}
8308
Avi Kivity6aa8b732006-12-10 02:21:36 -08008309/*
8310 * The guest has exited. See if we can fix it or if we need userspace
8311 * assistance.
8312 */
Avi Kivity851ba692009-08-24 11:10:17 +03008313static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008314{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008315 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008316 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008317 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008318
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008319 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8320
Kai Huang843e4332015-01-28 10:54:28 +08008321 /*
8322 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8323 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8324 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8325 * mode as if vcpus is in root mode, the PML buffer must has been
8326 * flushed already.
8327 */
8328 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008329 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008330
Mohammed Gamal80ced182009-09-01 12:48:18 +02008331 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008332 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008333 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008334
Nadav Har'El644d7112011-05-25 23:12:35 +03008335 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008336 nested_vmx_vmexit(vcpu, exit_reason,
8337 vmcs_read32(VM_EXIT_INTR_INFO),
8338 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008339 return 1;
8340 }
8341
Mohammed Gamal51207022010-05-31 22:40:54 +03008342 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008343 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008344 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8345 vcpu->run->fail_entry.hardware_entry_failure_reason
8346 = exit_reason;
8347 return 0;
8348 }
8349
Avi Kivity29bd8a72007-09-10 17:27:03 +03008350 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008351 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8352 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008353 = vmcs_read32(VM_INSTRUCTION_ERROR);
8354 return 0;
8355 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008356
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008357 /*
8358 * Note:
8359 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8360 * delivery event since it indicates guest is accessing MMIO.
8361 * The vm-exit can be triggered again after return to guest that
8362 * will cause infinite loop.
8363 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008364 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008365 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008366 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008367 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008368 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8369 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8370 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8371 vcpu->run->internal.ndata = 2;
8372 vcpu->run->internal.data[0] = vectoring_info;
8373 vcpu->run->internal.data[1] = exit_reason;
8374 return 0;
8375 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008376
Nadav Har'El644d7112011-05-25 23:12:35 +03008377 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8378 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008379 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008380 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008381 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008382 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008383 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008384 /*
8385 * This CPU don't support us in finding the end of an
8386 * NMI-blocked window if the guest runs with IRQs
8387 * disabled. So we pull the trigger after 1 s of
8388 * futile waiting, but inform the user about this.
8389 */
8390 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8391 "state on VCPU %d after 1 s timeout\n",
8392 __func__, vcpu->vcpu_id);
8393 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008394 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008395 }
8396
Avi Kivity6aa8b732006-12-10 02:21:36 -08008397 if (exit_reason < kvm_vmx_max_exit_handlers
8398 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008399 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008400 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008401 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8402 kvm_queue_exception(vcpu, UD_VECTOR);
8403 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008404 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008405}
8406
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008407static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008408{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008409 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8410
8411 if (is_guest_mode(vcpu) &&
8412 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8413 return;
8414
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008415 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008416 vmcs_write32(TPR_THRESHOLD, 0);
8417 return;
8418 }
8419
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008420 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008421}
8422
Yang Zhang8d146952013-01-25 10:18:50 +08008423static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8424{
8425 u32 sec_exec_control;
8426
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008427 /* Postpone execution until vmcs01 is the current VMCS. */
8428 if (is_guest_mode(vcpu)) {
8429 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8430 return;
8431 }
8432
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008433 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008434 return;
8435
Paolo Bonzini35754c92015-07-29 12:05:37 +02008436 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008437 return;
8438
8439 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8440
8441 if (set) {
8442 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8443 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8444 } else {
8445 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8446 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8447 }
8448 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8449
8450 vmx_set_msr_bitmap(vcpu);
8451}
8452
Tang Chen38b99172014-09-24 15:57:54 +08008453static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8454{
8455 struct vcpu_vmx *vmx = to_vmx(vcpu);
8456
8457 /*
8458 * Currently we do not handle the nested case where L2 has an
8459 * APIC access page of its own; that page is still pinned.
8460 * Hence, we skip the case where the VCPU is in guest mode _and_
8461 * L1 prepared an APIC access page for L2.
8462 *
8463 * For the case where L1 and L2 share the same APIC access page
8464 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8465 * in the vmcs12), this function will only update either the vmcs01
8466 * or the vmcs02. If the former, the vmcs02 will be updated by
8467 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8468 * the next L2->L1 exit.
8469 */
8470 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008471 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Tang Chen38b99172014-09-24 15:57:54 +08008472 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
8473 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8474}
8475
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008476static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008477{
8478 u16 status;
8479 u8 old;
8480
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008481 if (max_isr == -1)
8482 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008483
8484 status = vmcs_read16(GUEST_INTR_STATUS);
8485 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008486 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008487 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008488 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008489 vmcs_write16(GUEST_INTR_STATUS, status);
8490 }
8491}
8492
8493static void vmx_set_rvi(int vector)
8494{
8495 u16 status;
8496 u8 old;
8497
Wei Wang4114c272014-11-05 10:53:43 +08008498 if (vector == -1)
8499 vector = 0;
8500
Yang Zhangc7c9c562013-01-25 10:18:51 +08008501 status = vmcs_read16(GUEST_INTR_STATUS);
8502 old = (u8)status & 0xff;
8503 if ((u8)vector != old) {
8504 status &= ~0xff;
8505 status |= (u8)vector;
8506 vmcs_write16(GUEST_INTR_STATUS, status);
8507 }
8508}
8509
8510static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8511{
Wanpeng Li963fee12014-07-17 19:03:00 +08008512 if (!is_guest_mode(vcpu)) {
8513 vmx_set_rvi(max_irr);
8514 return;
8515 }
8516
Wei Wang4114c272014-11-05 10:53:43 +08008517 if (max_irr == -1)
8518 return;
8519
Wanpeng Li963fee12014-07-17 19:03:00 +08008520 /*
Wei Wang4114c272014-11-05 10:53:43 +08008521 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8522 * handles it.
8523 */
8524 if (nested_exit_on_intr(vcpu))
8525 return;
8526
8527 /*
8528 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008529 * is run without virtual interrupt delivery.
8530 */
8531 if (!kvm_event_needs_reinjection(vcpu) &&
8532 vmx_interrupt_allowed(vcpu)) {
8533 kvm_queue_interrupt(vcpu, max_irr, false);
8534 vmx_inject_irq(vcpu);
8535 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008536}
8537
Andrey Smetanin63086302015-11-10 15:36:32 +03008538static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008539{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008540 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008541 return;
8542
Yang Zhangc7c9c562013-01-25 10:18:51 +08008543 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8544 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8545 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8546 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8547}
8548
Avi Kivity51aa01d2010-07-20 14:31:20 +03008549static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008550{
Avi Kivity00eba012011-03-07 17:24:54 +02008551 u32 exit_intr_info;
8552
8553 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8554 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8555 return;
8556
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008557 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008558 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008559
8560 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008561 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008562 kvm_machine_check();
8563
Gleb Natapov20f65982009-05-11 13:35:55 +03008564 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008565 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008566 (exit_intr_info & INTR_INFO_VALID_MASK)) {
8567 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008568 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008569 kvm_after_handle_nmi(&vmx->vcpu);
8570 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008571}
Gleb Natapov20f65982009-05-11 13:35:55 +03008572
Yang Zhanga547c6d2013-04-11 19:25:10 +08008573static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8574{
8575 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008576 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008577
8578 /*
8579 * If external interrupt exists, IF bit is set in rflags/eflags on the
8580 * interrupt stack frame, and interrupt will be enabled on a return
8581 * from interrupt handler.
8582 */
8583 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8584 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8585 unsigned int vector;
8586 unsigned long entry;
8587 gate_desc *desc;
8588 struct vcpu_vmx *vmx = to_vmx(vcpu);
8589#ifdef CONFIG_X86_64
8590 unsigned long tmp;
8591#endif
8592
8593 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8594 desc = (gate_desc *)vmx->host_idt_base + vector;
8595 entry = gate_offset(*desc);
8596 asm volatile(
8597#ifdef CONFIG_X86_64
8598 "mov %%" _ASM_SP ", %[sp]\n\t"
8599 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8600 "push $%c[ss]\n\t"
8601 "push %[sp]\n\t"
8602#endif
8603 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008604 __ASM_SIZE(push) " $%c[cs]\n\t"
8605 "call *%[entry]\n\t"
8606 :
8607#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008608 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008609#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008610 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008611 :
8612 [entry]"r"(entry),
8613 [ss]"i"(__KERNEL_DS),
8614 [cs]"i"(__KERNEL_CS)
8615 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008616 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008617}
8618
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008619static bool vmx_has_high_real_mode_segbase(void)
8620{
8621 return enable_unrestricted_guest || emulate_invalid_guest_state;
8622}
8623
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008624static bool vmx_mpx_supported(void)
8625{
8626 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8627 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8628}
8629
Wanpeng Li55412b22014-12-02 19:21:30 +08008630static bool vmx_xsaves_supported(void)
8631{
8632 return vmcs_config.cpu_based_2nd_exec_ctrl &
8633 SECONDARY_EXEC_XSAVES;
8634}
8635
Avi Kivity51aa01d2010-07-20 14:31:20 +03008636static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8637{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008638 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008639 bool unblock_nmi;
8640 u8 vector;
8641 bool idtv_info_valid;
8642
8643 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008644
Avi Kivitycf393f72008-07-01 16:20:21 +03008645 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008646 if (vmx->nmi_known_unmasked)
8647 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008648 /*
8649 * Can't use vmx->exit_intr_info since we're not sure what
8650 * the exit reason is.
8651 */
8652 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008653 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8654 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8655 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008656 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008657 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8658 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008659 * SDM 3: 23.2.2 (September 2008)
8660 * Bit 12 is undefined in any of the following cases:
8661 * If the VM exit sets the valid bit in the IDT-vectoring
8662 * information field.
8663 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008664 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008665 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8666 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008667 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8668 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008669 else
8670 vmx->nmi_known_unmasked =
8671 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8672 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008673 } else if (unlikely(vmx->soft_vnmi_blocked))
8674 vmx->vnmi_blocked_time +=
8675 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008676}
8677
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008678static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008679 u32 idt_vectoring_info,
8680 int instr_len_field,
8681 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008682{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008683 u8 vector;
8684 int type;
8685 bool idtv_info_valid;
8686
8687 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008688
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008689 vcpu->arch.nmi_injected = false;
8690 kvm_clear_exception_queue(vcpu);
8691 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008692
8693 if (!idtv_info_valid)
8694 return;
8695
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008696 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008697
Avi Kivity668f6122008-07-02 09:28:55 +03008698 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8699 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008700
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008701 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008702 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008703 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008704 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008705 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008706 * Clear bit "block by NMI" before VM entry if a NMI
8707 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008708 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008709 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008710 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008711 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008712 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008713 /* fall through */
8714 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008715 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008716 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008717 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008718 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008719 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008720 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008721 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008722 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008723 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008724 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008725 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008726 break;
8727 default:
8728 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008729 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008730}
8731
Avi Kivity83422e12010-07-20 14:43:23 +03008732static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8733{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008734 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008735 VM_EXIT_INSTRUCTION_LEN,
8736 IDT_VECTORING_ERROR_CODE);
8737}
8738
Avi Kivityb463a6f2010-07-20 15:06:17 +03008739static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8740{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008741 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008742 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8743 VM_ENTRY_INSTRUCTION_LEN,
8744 VM_ENTRY_EXCEPTION_ERROR_CODE);
8745
8746 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8747}
8748
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008749static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8750{
8751 int i, nr_msrs;
8752 struct perf_guest_switch_msr *msrs;
8753
8754 msrs = perf_guest_get_msrs(&nr_msrs);
8755
8756 if (!msrs)
8757 return;
8758
8759 for (i = 0; i < nr_msrs; i++)
8760 if (msrs[i].host == msrs[i].guest)
8761 clear_atomic_switch_msr(vmx, msrs[i].msr);
8762 else
8763 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8764 msrs[i].host);
8765}
8766
Yunhong Jiang64672c92016-06-13 14:19:59 -07008767void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8768{
8769 struct vcpu_vmx *vmx = to_vmx(vcpu);
8770 u64 tscl;
8771 u32 delta_tsc;
8772
8773 if (vmx->hv_deadline_tsc == -1)
8774 return;
8775
8776 tscl = rdtsc();
8777 if (vmx->hv_deadline_tsc > tscl)
8778 /* sure to be 32 bit only because checked on set_hv_timer */
8779 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8780 cpu_preemption_timer_multi);
8781 else
8782 delta_tsc = 0;
8783
8784 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8785}
8786
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008787static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008788{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008789 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008790 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008791
8792 /* Record the guest's net vcpu time for enforced NMI injections. */
8793 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8794 vmx->entry_time = ktime_get();
8795
8796 /* Don't enter VMX if guest state is invalid, let the exit handler
8797 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008798 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008799 return;
8800
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008801 if (vmx->ple_window_dirty) {
8802 vmx->ple_window_dirty = false;
8803 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8804 }
8805
Abel Gordon012f83c2013-04-18 14:39:25 +03008806 if (vmx->nested.sync_shadow_vmcs) {
8807 copy_vmcs12_to_shadow(vmx);
8808 vmx->nested.sync_shadow_vmcs = false;
8809 }
8810
Avi Kivity104f2262010-11-18 13:12:52 +02008811 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8812 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8813 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8814 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8815
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008816 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008817 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8818 vmcs_writel(HOST_CR4, cr4);
8819 vmx->host_state.vmcs_host_cr4 = cr4;
8820 }
8821
Avi Kivity104f2262010-11-18 13:12:52 +02008822 /* When single-stepping over STI and MOV SS, we must clear the
8823 * corresponding interruptibility bits in the guest state. Otherwise
8824 * vmentry fails as it then expects bit 14 (BS) in pending debug
8825 * exceptions being set, but that's not correct for the guest debugging
8826 * case. */
8827 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8828 vmx_set_interrupt_shadow(vcpu, 0);
8829
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008830 if (vmx->guest_pkru_valid)
8831 __write_pkru(vmx->guest_pkru);
8832
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008833 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008834 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008835
Yunhong Jiang64672c92016-06-13 14:19:59 -07008836 vmx_arm_hv_timer(vcpu);
8837
Nadav Har'Eld462b812011-05-24 15:26:10 +03008838 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008839 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008840 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008841 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8842 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8843 "push %%" _ASM_CX " \n\t"
8844 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03008845 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008846 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008847 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03008848 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008849 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008850 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8851 "mov %%cr2, %%" _ASM_DX " \n\t"
8852 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008853 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008854 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008855 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008856 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008857 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008858 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008859 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8860 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8861 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8862 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8863 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8864 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008865#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008866 "mov %c[r8](%0), %%r8 \n\t"
8867 "mov %c[r9](%0), %%r9 \n\t"
8868 "mov %c[r10](%0), %%r10 \n\t"
8869 "mov %c[r11](%0), %%r11 \n\t"
8870 "mov %c[r12](%0), %%r12 \n\t"
8871 "mov %c[r13](%0), %%r13 \n\t"
8872 "mov %c[r14](%0), %%r14 \n\t"
8873 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008874#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008875 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008876
Avi Kivity6aa8b732006-12-10 02:21:36 -08008877 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008878 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008879 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008880 "jmp 2f \n\t"
8881 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8882 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008883 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008884 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008885 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008886 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8887 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8888 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8889 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8890 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8891 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8892 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008893#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008894 "mov %%r8, %c[r8](%0) \n\t"
8895 "mov %%r9, %c[r9](%0) \n\t"
8896 "mov %%r10, %c[r10](%0) \n\t"
8897 "mov %%r11, %c[r11](%0) \n\t"
8898 "mov %%r12, %c[r12](%0) \n\t"
8899 "mov %%r13, %c[r13](%0) \n\t"
8900 "mov %%r14, %c[r14](%0) \n\t"
8901 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008902#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008903 "mov %%cr2, %%" _ASM_AX " \n\t"
8904 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008905
Avi Kivityb188c81f2012-09-16 15:10:58 +03008906 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008907 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008908 ".pushsection .rodata \n\t"
8909 ".global vmx_return \n\t"
8910 "vmx_return: " _ASM_PTR " 2b \n\t"
8911 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008912 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008913 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008914 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03008915 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008916 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8917 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8918 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8919 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8920 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8921 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8922 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008923#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008924 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8925 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8926 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8927 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8928 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8929 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8930 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8931 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008932#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008933 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8934 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02008935 : "cc", "memory"
8936#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03008937 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008938 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008939#else
8940 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008941#endif
8942 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08008943
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008944 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
8945 if (debugctlmsr)
8946 update_debugctlmsr(debugctlmsr);
8947
Avi Kivityaa67f602012-08-01 16:48:03 +03008948#ifndef CONFIG_X86_64
8949 /*
8950 * The sysexit path does not restore ds/es, so we must set them to
8951 * a reasonable value ourselves.
8952 *
8953 * We can't defer this to vmx_load_host_state() since that function
8954 * may be executed in interrupt context, which saves and restore segments
8955 * around it, nullifying its effect.
8956 */
8957 loadsegment(ds, __USER_DS);
8958 loadsegment(es, __USER_DS);
8959#endif
8960
Avi Kivity6de4f3a2009-05-31 22:58:47 +03008961 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02008962 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008963 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03008964 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008965 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008966 vcpu->arch.regs_dirty = 0;
8967
Avi Kivity1155f762007-11-22 11:30:47 +02008968 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
8969
Nadav Har'Eld462b812011-05-24 15:26:10 +03008970 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02008971
Avi Kivity51aa01d2010-07-20 14:31:20 +03008972 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008973
Gleb Natapove0b890d2013-09-25 12:51:33 +03008974 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008975 * eager fpu is enabled if PKEY is supported and CR4 is switched
8976 * back on host, so it is safe to read guest PKRU from current
8977 * XSAVE.
8978 */
8979 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
8980 vmx->guest_pkru = __read_pkru();
8981 if (vmx->guest_pkru != vmx->host_pkru) {
8982 vmx->guest_pkru_valid = true;
8983 __write_pkru(vmx->host_pkru);
8984 } else
8985 vmx->guest_pkru_valid = false;
8986 }
8987
8988 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03008989 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
8990 * we did not inject a still-pending event to L1 now because of
8991 * nested_run_pending, we need to re-enable this bit.
8992 */
8993 if (vmx->nested.nested_run_pending)
8994 kvm_make_request(KVM_REQ_EVENT, vcpu);
8995
8996 vmx->nested.nested_run_pending = 0;
8997
Avi Kivity51aa01d2010-07-20 14:31:20 +03008998 vmx_complete_atomic_exit(vmx);
8999 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009000 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009001}
9002
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009003static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9004{
9005 struct vcpu_vmx *vmx = to_vmx(vcpu);
9006 int cpu;
9007
9008 if (vmx->loaded_vmcs == &vmx->vmcs01)
9009 return;
9010
9011 cpu = get_cpu();
9012 vmx->loaded_vmcs = &vmx->vmcs01;
9013 vmx_vcpu_put(vcpu);
9014 vmx_vcpu_load(vcpu, cpu);
9015 vcpu->cpu = cpu;
9016 put_cpu();
9017}
9018
Jim Mattson2f1fe812016-07-08 15:36:06 -07009019/*
9020 * Ensure that the current vmcs of the logical processor is the
9021 * vmcs01 of the vcpu before calling free_nested().
9022 */
9023static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9024{
9025 struct vcpu_vmx *vmx = to_vmx(vcpu);
9026 int r;
9027
9028 r = vcpu_load(vcpu);
9029 BUG_ON(r);
9030 vmx_load_vmcs01(vcpu);
9031 free_nested(vmx);
9032 vcpu_put(vcpu);
9033}
9034
Avi Kivity6aa8b732006-12-10 02:21:36 -08009035static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9036{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009037 struct vcpu_vmx *vmx = to_vmx(vcpu);
9038
Kai Huang843e4332015-01-28 10:54:28 +08009039 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009040 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009041 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009042 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009043 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009044 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009045 kfree(vmx->guest_msrs);
9046 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009047 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009048}
9049
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009050static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009051{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009052 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009053 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009054 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009055
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009056 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009057 return ERR_PTR(-ENOMEM);
9058
Wanpeng Li991e7a02015-09-16 17:30:05 +08009059 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009060
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009061 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9062 if (err)
9063 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009064
Peter Feiner4e595162016-07-07 14:49:58 -07009065 err = -ENOMEM;
9066
9067 /*
9068 * If PML is turned on, failure on enabling PML just results in failure
9069 * of creating the vcpu, therefore we can simplify PML logic (by
9070 * avoiding dealing with cases, such as enabling PML partially on vcpus
9071 * for the guest, etc.
9072 */
9073 if (enable_pml) {
9074 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9075 if (!vmx->pml_pg)
9076 goto uninit_vcpu;
9077 }
9078
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009079 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009080 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9081 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009082
Peter Feiner4e595162016-07-07 14:49:58 -07009083 if (!vmx->guest_msrs)
9084 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009085
Nadav Har'Eld462b812011-05-24 15:26:10 +03009086 vmx->loaded_vmcs = &vmx->vmcs01;
9087 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009088 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009089 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009090 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009091 if (!vmm_exclusive)
9092 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
9093 loaded_vmcs_init(vmx->loaded_vmcs);
9094 if (!vmm_exclusive)
9095 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009096
Avi Kivity15ad7142007-07-11 18:17:21 +03009097 cpu = get_cpu();
9098 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009099 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009100 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009101 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009102 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009103 if (err)
9104 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009105 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009106 err = alloc_apic_access_page(kvm);
9107 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009108 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009109 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009110
Sheng Yangb927a3c2009-07-21 10:42:48 +08009111 if (enable_ept) {
9112 if (!kvm->arch.ept_identity_map_addr)
9113 kvm->arch.ept_identity_map_addr =
9114 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009115 err = init_rmode_identity_map(kvm);
9116 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009117 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009118 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009119
Wanpeng Li5c614b32015-10-13 09:18:36 -07009120 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009121 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009122 vmx->nested.vpid02 = allocate_vpid();
9123 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009124
Wincy Van705699a2015-02-03 23:58:17 +08009125 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009126 vmx->nested.current_vmptr = -1ull;
9127 vmx->nested.current_vmcs12 = NULL;
9128
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009129 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9130
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009131 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009132
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009133free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009134 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009135 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009136free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009137 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009138free_pml:
9139 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009140uninit_vcpu:
9141 kvm_vcpu_uninit(&vmx->vcpu);
9142free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009143 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009144 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009145 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009146}
9147
Yang, Sheng002c7f72007-07-31 14:23:01 +03009148static void __init vmx_check_processor_compat(void *rtn)
9149{
9150 struct vmcs_config vmcs_conf;
9151
9152 *(int *)rtn = 0;
9153 if (setup_vmcs_config(&vmcs_conf) < 0)
9154 *(int *)rtn = -EIO;
9155 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9156 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9157 smp_processor_id());
9158 *(int *)rtn = -EIO;
9159 }
9160}
9161
Sheng Yang67253af2008-04-25 10:20:22 +08009162static int get_ept_level(void)
9163{
9164 return VMX_EPT_DEFAULT_GAW + 1;
9165}
9166
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009167static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009168{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009169 u8 cache;
9170 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009171
Sheng Yang522c68c2009-04-27 20:35:43 +08009172 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009173 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009174 * 2. EPT with VT-d:
9175 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009176 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009177 * b. VT-d with snooping control feature: snooping control feature of
9178 * VT-d engine can guarantee the cache correctness. Just set it
9179 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009180 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009181 * consistent with host MTRR
9182 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009183 if (is_mmio) {
9184 cache = MTRR_TYPE_UNCACHABLE;
9185 goto exit;
9186 }
9187
9188 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009189 ipat = VMX_EPT_IPAT_BIT;
9190 cache = MTRR_TYPE_WRBACK;
9191 goto exit;
9192 }
9193
9194 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9195 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009196 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009197 cache = MTRR_TYPE_WRBACK;
9198 else
9199 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009200 goto exit;
9201 }
9202
Xiao Guangrongff536042015-06-15 16:55:22 +08009203 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009204
9205exit:
9206 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009207}
9208
Sheng Yang17cc3932010-01-05 19:02:27 +08009209static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009210{
Sheng Yang878403b2010-01-05 19:02:29 +08009211 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9212 return PT_DIRECTORY_LEVEL;
9213 else
9214 /* For shadow and EPT supported 1GB page */
9215 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009216}
9217
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009218static void vmcs_set_secondary_exec_control(u32 new_ctl)
9219{
9220 /*
9221 * These bits in the secondary execution controls field
9222 * are dynamic, the others are mostly based on the hypervisor
9223 * architecture and the guest's CPUID. Do not touch the
9224 * dynamic bits.
9225 */
9226 u32 mask =
9227 SECONDARY_EXEC_SHADOW_VMCS |
9228 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9229 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9230
9231 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9232
9233 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9234 (new_ctl & ~mask) | (cur_ctl & mask));
9235}
9236
Sheng Yang0e851882009-12-18 16:48:46 +08009237static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9238{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009239 struct kvm_cpuid_entry2 *best;
9240 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009241 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009242
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009243 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009244 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9245 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009246 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009247
Paolo Bonzini8b972652015-09-15 17:34:42 +02009248 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009249 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009250 vmx->nested.nested_vmx_secondary_ctls_high |=
9251 SECONDARY_EXEC_RDTSCP;
9252 else
9253 vmx->nested.nested_vmx_secondary_ctls_high &=
9254 ~SECONDARY_EXEC_RDTSCP;
9255 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009256 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009257
Mao, Junjiead756a12012-07-02 01:18:48 +00009258 /* Exposing INVPCID only when PCID is exposed */
9259 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9260 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009261 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9262 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009263 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009264
Mao, Junjiead756a12012-07-02 01:18:48 +00009265 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009266 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009267 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009268
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009269 if (cpu_has_secondary_exec_ctrls())
9270 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009271
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009272 if (nested_vmx_allowed(vcpu))
9273 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9274 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9275 else
9276 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9277 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009278}
9279
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009280static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9281{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009282 if (func == 1 && nested)
9283 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009284}
9285
Yang Zhang25d92082013-08-06 12:00:32 +03009286static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9287 struct x86_exception *fault)
9288{
Jan Kiszka533558b2014-01-04 18:47:20 +01009289 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9290 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009291
9292 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009293 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009294 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009295 exit_reason = EXIT_REASON_EPT_VIOLATION;
9296 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009297 vmcs12->guest_physical_address = fault->address;
9298}
9299
Nadav Har'El155a97a2013-08-05 11:07:16 +03009300/* Callbacks for nested_ept_init_mmu_context: */
9301
9302static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9303{
9304 /* return the page table to be shadowed - in our case, EPT12 */
9305 return get_vmcs12(vcpu)->ept_pointer;
9306}
9307
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009308static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009309{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009310 WARN_ON(mmu_is_nested(vcpu));
9311 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009312 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9313 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009314 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9315 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9316 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9317
9318 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009319}
9320
9321static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9322{
9323 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9324}
9325
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009326static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9327 u16 error_code)
9328{
9329 bool inequality, bit;
9330
9331 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9332 inequality =
9333 (error_code & vmcs12->page_fault_error_code_mask) !=
9334 vmcs12->page_fault_error_code_match;
9335 return inequality ^ bit;
9336}
9337
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009338static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9339 struct x86_exception *fault)
9340{
9341 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9342
9343 WARN_ON(!is_guest_mode(vcpu));
9344
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009345 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009346 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9347 vmcs_read32(VM_EXIT_INTR_INFO),
9348 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009349 else
9350 kvm_inject_page_fault(vcpu, fault);
9351}
9352
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009353static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9354 struct vmcs12 *vmcs12)
9355{
9356 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009357 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009358
9359 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009360 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9361 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009362 return false;
9363
9364 /*
9365 * Translate L1 physical address to host physical
9366 * address for vmcs02. Keep the page pinned, so this
9367 * physical address remains valid. We keep a reference
9368 * to it so we can release it later.
9369 */
9370 if (vmx->nested.apic_access_page) /* shouldn't happen */
9371 nested_release_page(vmx->nested.apic_access_page);
9372 vmx->nested.apic_access_page =
9373 nested_get_page(vcpu, vmcs12->apic_access_addr);
9374 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009375
9376 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009377 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9378 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009379 return false;
9380
9381 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9382 nested_release_page(vmx->nested.virtual_apic_page);
9383 vmx->nested.virtual_apic_page =
9384 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9385
9386 /*
9387 * Failing the vm entry is _not_ what the processor does
9388 * but it's basically the only possibility we have.
9389 * We could still enter the guest if CR8 load exits are
9390 * enabled, CR8 store exits are enabled, and virtualize APIC
9391 * access is disabled; in this case the processor would never
9392 * use the TPR shadow and we could simply clear the bit from
9393 * the execution control. But such a configuration is useless,
9394 * so let's keep the code simple.
9395 */
9396 if (!vmx->nested.virtual_apic_page)
9397 return false;
9398 }
9399
Wincy Van705699a2015-02-03 23:58:17 +08009400 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009401 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9402 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009403 return false;
9404
9405 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9406 kunmap(vmx->nested.pi_desc_page);
9407 nested_release_page(vmx->nested.pi_desc_page);
9408 }
9409 vmx->nested.pi_desc_page =
9410 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9411 if (!vmx->nested.pi_desc_page)
9412 return false;
9413
9414 vmx->nested.pi_desc =
9415 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9416 if (!vmx->nested.pi_desc) {
9417 nested_release_page_clean(vmx->nested.pi_desc_page);
9418 return false;
9419 }
9420 vmx->nested.pi_desc =
9421 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9422 (unsigned long)(vmcs12->posted_intr_desc_addr &
9423 (PAGE_SIZE - 1)));
9424 }
9425
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009426 return true;
9427}
9428
Jan Kiszkaf41245002014-03-07 20:03:13 +01009429static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9430{
9431 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9432 struct vcpu_vmx *vmx = to_vmx(vcpu);
9433
9434 if (vcpu->arch.virtual_tsc_khz == 0)
9435 return;
9436
9437 /* Make sure short timeouts reliably trigger an immediate vmexit.
9438 * hrtimer_start does not guarantee this. */
9439 if (preemption_timeout <= 1) {
9440 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9441 return;
9442 }
9443
9444 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9445 preemption_timeout *= 1000000;
9446 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9447 hrtimer_start(&vmx->nested.preemption_timer,
9448 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9449}
9450
Wincy Van3af18d92015-02-03 23:49:31 +08009451static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9452 struct vmcs12 *vmcs12)
9453{
9454 int maxphyaddr;
9455 u64 addr;
9456
9457 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9458 return 0;
9459
9460 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9461 WARN_ON(1);
9462 return -EINVAL;
9463 }
9464 maxphyaddr = cpuid_maxphyaddr(vcpu);
9465
9466 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9467 ((addr + PAGE_SIZE) >> maxphyaddr))
9468 return -EINVAL;
9469
9470 return 0;
9471}
9472
9473/*
9474 * Merge L0's and L1's MSR bitmap, return false to indicate that
9475 * we do not use the hardware.
9476 */
9477static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9478 struct vmcs12 *vmcs12)
9479{
Wincy Van82f0dd42015-02-03 23:57:18 +08009480 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009481 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009482 unsigned long *msr_bitmap_l1;
9483 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +08009484
Radim Krčmářd048c092016-08-08 20:16:22 +02009485 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +08009486 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9487 return false;
9488
9489 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
9490 if (!page) {
9491 WARN_ON(1);
9492 return false;
9493 }
Radim Krčmářd048c092016-08-08 20:16:22 +02009494 msr_bitmap_l1 = (unsigned long *)kmap(page);
9495 if (!msr_bitmap_l1) {
Wincy Vanf2b93282015-02-03 23:56:03 +08009496 nested_release_page_clean(page);
9497 WARN_ON(1);
9498 return false;
9499 }
9500
Radim Krčmářd048c092016-08-08 20:16:22 +02009501 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9502
Wincy Vanf2b93282015-02-03 23:56:03 +08009503 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009504 if (nested_cpu_has_apic_reg_virt(vmcs12))
9505 for (msr = 0x800; msr <= 0x8ff; msr++)
9506 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009507 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009508 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009509
9510 nested_vmx_disable_intercept_for_msr(
9511 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009512 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9513 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009514
Wincy Van608406e2015-02-03 23:57:51 +08009515 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009516 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009517 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009518 APIC_BASE_MSR + (APIC_EOI >> 4),
9519 MSR_TYPE_W);
9520 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009521 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009522 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9523 MSR_TYPE_W);
9524 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009525 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009526 kunmap(page);
9527 nested_release_page_clean(page);
9528
9529 return true;
9530}
9531
9532static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9533 struct vmcs12 *vmcs12)
9534{
Wincy Van82f0dd42015-02-03 23:57:18 +08009535 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009536 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009537 !nested_cpu_has_vid(vmcs12) &&
9538 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009539 return 0;
9540
9541 /*
9542 * If virtualize x2apic mode is enabled,
9543 * virtualize apic access must be disabled.
9544 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009545 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9546 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009547 return -EINVAL;
9548
Wincy Van608406e2015-02-03 23:57:51 +08009549 /*
9550 * If virtual interrupt delivery is enabled,
9551 * we must exit on external interrupts.
9552 */
9553 if (nested_cpu_has_vid(vmcs12) &&
9554 !nested_exit_on_intr(vcpu))
9555 return -EINVAL;
9556
Wincy Van705699a2015-02-03 23:58:17 +08009557 /*
9558 * bits 15:8 should be zero in posted_intr_nv,
9559 * the descriptor address has been already checked
9560 * in nested_get_vmcs12_pages.
9561 */
9562 if (nested_cpu_has_posted_intr(vmcs12) &&
9563 (!nested_cpu_has_vid(vmcs12) ||
9564 !nested_exit_intr_ack_set(vcpu) ||
9565 vmcs12->posted_intr_nv & 0xff00))
9566 return -EINVAL;
9567
Wincy Vanf2b93282015-02-03 23:56:03 +08009568 /* tpr shadow is needed by all apicv features. */
9569 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9570 return -EINVAL;
9571
9572 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009573}
9574
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009575static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9576 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009577 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009578{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009579 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009580 u64 count, addr;
9581
9582 if (vmcs12_read_any(vcpu, count_field, &count) ||
9583 vmcs12_read_any(vcpu, addr_field, &addr)) {
9584 WARN_ON(1);
9585 return -EINVAL;
9586 }
9587 if (count == 0)
9588 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009589 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009590 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9591 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009592 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009593 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9594 addr_field, maxphyaddr, count, addr);
9595 return -EINVAL;
9596 }
9597 return 0;
9598}
9599
9600static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9601 struct vmcs12 *vmcs12)
9602{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009603 if (vmcs12->vm_exit_msr_load_count == 0 &&
9604 vmcs12->vm_exit_msr_store_count == 0 &&
9605 vmcs12->vm_entry_msr_load_count == 0)
9606 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009607 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009608 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009609 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009610 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009611 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009612 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009613 return -EINVAL;
9614 return 0;
9615}
9616
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009617static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9618 struct vmx_msr_entry *e)
9619{
9620 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009621 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009622 return -EINVAL;
9623 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9624 e->index == MSR_IA32_UCODE_REV)
9625 return -EINVAL;
9626 if (e->reserved != 0)
9627 return -EINVAL;
9628 return 0;
9629}
9630
9631static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9632 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009633{
9634 if (e->index == MSR_FS_BASE ||
9635 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009636 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9637 nested_vmx_msr_check_common(vcpu, e))
9638 return -EINVAL;
9639 return 0;
9640}
9641
9642static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9643 struct vmx_msr_entry *e)
9644{
9645 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9646 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009647 return -EINVAL;
9648 return 0;
9649}
9650
9651/*
9652 * Load guest's/host's msr at nested entry/exit.
9653 * return 0 for success, entry index for failure.
9654 */
9655static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9656{
9657 u32 i;
9658 struct vmx_msr_entry e;
9659 struct msr_data msr;
9660
9661 msr.host_initiated = false;
9662 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009663 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9664 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009665 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009666 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9667 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009668 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009669 }
9670 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009671 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009672 "%s check failed (%u, 0x%x, 0x%x)\n",
9673 __func__, i, e.index, e.reserved);
9674 goto fail;
9675 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009676 msr.index = e.index;
9677 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009678 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009679 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009680 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9681 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009682 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009683 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009684 }
9685 return 0;
9686fail:
9687 return i + 1;
9688}
9689
9690static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9691{
9692 u32 i;
9693 struct vmx_msr_entry e;
9694
9695 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009696 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009697 if (kvm_vcpu_read_guest(vcpu,
9698 gpa + i * sizeof(e),
9699 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009700 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009701 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9702 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009703 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009704 }
9705 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009706 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009707 "%s check failed (%u, 0x%x, 0x%x)\n",
9708 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009709 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009710 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009711 msr_info.host_initiated = false;
9712 msr_info.index = e.index;
9713 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009714 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009715 "%s cannot read MSR (%u, 0x%x)\n",
9716 __func__, i, e.index);
9717 return -EINVAL;
9718 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009719 if (kvm_vcpu_write_guest(vcpu,
9720 gpa + i * sizeof(e) +
9721 offsetof(struct vmx_msr_entry, value),
9722 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009723 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009724 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009725 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009726 return -EINVAL;
9727 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009728 }
9729 return 0;
9730}
9731
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009732/*
9733 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9734 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009735 * 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 +03009736 * guest in a way that will both be appropriate to L1's requests, and our
9737 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9738 * function also has additional necessary side-effects, like setting various
9739 * vcpu->arch fields.
9740 */
9741static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9742{
9743 struct vcpu_vmx *vmx = to_vmx(vcpu);
9744 u32 exec_control;
9745
9746 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9747 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9748 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9749 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9750 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9751 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9752 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9753 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9754 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9755 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9756 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9757 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9758 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9759 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9760 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9761 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9762 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9763 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9764 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9765 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9766 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9767 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9768 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9769 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9770 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9771 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9772 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9773 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9774 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9775 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9776 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9777 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9778 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9779 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9780 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9781 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9782
Jan Kiszka2996fca2014-06-16 13:59:43 +02009783 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9784 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9785 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9786 } else {
9787 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9788 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9789 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009790 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9791 vmcs12->vm_entry_intr_info_field);
9792 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9793 vmcs12->vm_entry_exception_error_code);
9794 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9795 vmcs12->vm_entry_instruction_len);
9796 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9797 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009798 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009799 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009800 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9801 vmcs12->guest_pending_dbg_exceptions);
9802 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9803 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9804
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009805 if (nested_cpu_has_xsaves(vmcs12))
9806 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009807 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9808
Jan Kiszkaf41245002014-03-07 20:03:13 +01009809 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009810
Paolo Bonzini9314006db2016-07-06 13:23:51 +02009811 /* Preemption timer setting is only taken from vmcs01. */
9812 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9813 exec_control |= vmcs_config.pin_based_exec_ctrl;
9814 if (vmx->hv_deadline_tsc == -1)
9815 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9816
9817 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +08009818 if (nested_cpu_has_posted_intr(vmcs12)) {
9819 /*
9820 * Note that we use L0's vector here and in
9821 * vmx_deliver_nested_posted_interrupt.
9822 */
9823 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9824 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +08009825 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +08009826 vmcs_write64(POSTED_INTR_DESC_ADDR,
9827 page_to_phys(vmx->nested.pi_desc_page) +
9828 (unsigned long)(vmcs12->posted_intr_desc_addr &
9829 (PAGE_SIZE - 1)));
9830 } else
9831 exec_control &= ~PIN_BASED_POSTED_INTR;
9832
Jan Kiszkaf41245002014-03-07 20:03:13 +01009833 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009834
Jan Kiszkaf41245002014-03-07 20:03:13 +01009835 vmx->nested.preemption_timer_expired = false;
9836 if (nested_cpu_has_preemption_timer(vmcs12))
9837 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01009838
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009839 /*
9840 * Whether page-faults are trapped is determined by a combination of
9841 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
9842 * If enable_ept, L0 doesn't care about page faults and we should
9843 * set all of these to L1's desires. However, if !enable_ept, L0 does
9844 * care about (at least some) page faults, and because it is not easy
9845 * (if at all possible?) to merge L0 and L1's desires, we simply ask
9846 * to exit on each and every L2 page fault. This is done by setting
9847 * MASK=MATCH=0 and (see below) EB.PF=1.
9848 * Note that below we don't need special code to set EB.PF beyond the
9849 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
9850 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
9851 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
9852 *
9853 * A problem with this approach (when !enable_ept) is that L1 may be
9854 * injected with more page faults than it asked for. This could have
9855 * caused problems, but in practice existing hypervisors don't care.
9856 * To fix this, we will need to emulate the PFEC checking (on the L1
9857 * page tables), using walk_addr(), when injecting PFs to L1.
9858 */
9859 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
9860 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
9861 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
9862 enable_ept ? vmcs12->page_fault_error_code_match : 0);
9863
9864 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +01009865 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +08009866
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009867 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009868 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009869 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009870 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -07009871 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009872 if (nested_cpu_has(vmcs12,
9873 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
9874 exec_control |= vmcs12->secondary_vm_exec_control;
9875
9876 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
9877 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009878 * If translation failed, no matter: This feature asks
9879 * to exit when accessing the given address, and if it
9880 * can never be accessed, this feature won't do
9881 * anything anyway.
9882 */
9883 if (!vmx->nested.apic_access_page)
9884 exec_control &=
9885 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9886 else
9887 vmcs_write64(APIC_ACCESS_ADDR,
9888 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +08009889 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +02009890 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +01009891 exec_control |=
9892 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +08009893 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009894 }
9895
Wincy Van608406e2015-02-03 23:57:51 +08009896 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
9897 vmcs_write64(EOI_EXIT_BITMAP0,
9898 vmcs12->eoi_exit_bitmap0);
9899 vmcs_write64(EOI_EXIT_BITMAP1,
9900 vmcs12->eoi_exit_bitmap1);
9901 vmcs_write64(EOI_EXIT_BITMAP2,
9902 vmcs12->eoi_exit_bitmap2);
9903 vmcs_write64(EOI_EXIT_BITMAP3,
9904 vmcs12->eoi_exit_bitmap3);
9905 vmcs_write16(GUEST_INTR_STATUS,
9906 vmcs12->guest_intr_status);
9907 }
9908
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009909 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
9910 }
9911
9912
9913 /*
9914 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
9915 * Some constant fields are set here by vmx_set_constant_host_state().
9916 * Other fields are different per CPU, and will be set later when
9917 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
9918 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08009919 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009920
9921 /*
Jim Mattson83bafef2016-10-04 10:48:38 -07009922 * Set the MSR load/store lists to match L0's settings.
9923 */
9924 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
9925 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
9926 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
9927 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
9928 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
9929
9930 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009931 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
9932 * entry, but only if the current (host) sp changed from the value
9933 * we wrote last (vmx->host_rsp). This cache is no longer relevant
9934 * if we switch vmcs, and rather than hold a separate cache per vmcs,
9935 * here we just force the write to happen on entry.
9936 */
9937 vmx->host_rsp = 0;
9938
9939 exec_control = vmx_exec_control(vmx); /* L0's desires */
9940 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
9941 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
9942 exec_control &= ~CPU_BASED_TPR_SHADOW;
9943 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009944
9945 if (exec_control & CPU_BASED_TPR_SHADOW) {
9946 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
9947 page_to_phys(vmx->nested.virtual_apic_page));
9948 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
9949 }
9950
Wincy Van3af18d92015-02-03 23:49:31 +08009951 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +02009952 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
9953 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9954 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
9955 else
Wincy Van3af18d92015-02-03 23:49:31 +08009956 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
9957
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009958 /*
Wincy Van3af18d92015-02-03 23:49:31 +08009959 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009960 * Rather, exit every time.
9961 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009962 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
9963 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
9964
9965 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
9966
9967 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
9968 * bitwise-or of what L1 wants to trap for L2, and what we want to
9969 * trap. Note that CR0.TS also needs updating - we do this later.
9970 */
9971 update_exception_bitmap(vcpu);
9972 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
9973 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
9974
Nadav Har'El8049d652013-08-05 11:07:06 +03009975 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
9976 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
9977 * bits are further modified by vmx_set_efer() below.
9978 */
Jan Kiszkaf41245002014-03-07 20:03:13 +01009979 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03009980
9981 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
9982 * emulated by vmx_set_efer(), below.
9983 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02009984 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03009985 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
9986 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009987 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
9988
Jan Kiszka44811c02013-08-04 17:17:27 +02009989 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009990 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02009991 vcpu->arch.pat = vmcs12->guest_ia32_pat;
9992 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009993 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
9994
9995
9996 set_cr4_guest_host_mask(vmx);
9997
Paolo Bonzini36be0b92014-02-24 12:30:04 +01009998 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
9999 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10000
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010001 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10002 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010003 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010004 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010005 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010006 if (kvm_has_tsc_control)
10007 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010008
10009 if (enable_vpid) {
10010 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010011 * There is no direct mapping between vpid02 and vpid12, the
10012 * vpid02 is per-vCPU for L0 and reused while the value of
10013 * vpid12 is changed w/ one invvpid during nested vmentry.
10014 * The vpid12 is allocated by L1 for L2, so it will not
10015 * influence global bitmap(for vpid01 and vpid02 allocation)
10016 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010017 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010018 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10019 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10020 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10021 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10022 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10023 }
10024 } else {
10025 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10026 vmx_flush_tlb(vcpu);
10027 }
10028
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010029 }
10030
Nadav Har'El155a97a2013-08-05 11:07:16 +030010031 if (nested_cpu_has_ept(vmcs12)) {
10032 kvm_mmu_unload(vcpu);
10033 nested_ept_init_mmu_context(vcpu);
10034 }
10035
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010036 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10037 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010038 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010039 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10040 else
10041 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10042 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10043 vmx_set_efer(vcpu, vcpu->arch.efer);
10044
10045 /*
10046 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10047 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10048 * The CR0_READ_SHADOW is what L2 should have expected to read given
10049 * the specifications by L1; It's not enough to take
10050 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10051 * have more bits than L1 expected.
10052 */
10053 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10054 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10055
10056 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10057 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10058
10059 /* shadow page tables on either EPT or shadow page tables */
10060 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10061 kvm_mmu_reset_context(vcpu);
10062
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010063 if (!enable_ept)
10064 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10065
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010066 /*
10067 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10068 */
10069 if (enable_ept) {
10070 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10071 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10072 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10073 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10074 }
10075
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010076 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10077 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10078}
10079
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010080/*
10081 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10082 * for running an L2 nested guest.
10083 */
10084static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10085{
10086 struct vmcs12 *vmcs12;
10087 struct vcpu_vmx *vmx = to_vmx(vcpu);
10088 int cpu;
10089 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +020010090 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010091 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010092
10093 if (!nested_vmx_check_permission(vcpu) ||
10094 !nested_vmx_check_vmcs12(vcpu))
10095 return 1;
10096
10097 skip_emulated_instruction(vcpu);
10098 vmcs12 = get_vmcs12(vcpu);
10099
Abel Gordon012f83c2013-04-18 14:39:25 +030010100 if (enable_shadow_vmcs)
10101 copy_shadow_to_vmcs12(vmx);
10102
Nadav Har'El7c177932011-05-25 23:12:04 +030010103 /*
10104 * The nested entry process starts with enforcing various prerequisites
10105 * on vmcs12 as required by the Intel SDM, and act appropriately when
10106 * they fail: As the SDM explains, some conditions should cause the
10107 * instruction to fail, while others will cause the instruction to seem
10108 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10109 * To speed up the normal (success) code path, we should avoid checking
10110 * for misconfigurations which will anyway be caught by the processor
10111 * when using the merged vmcs02.
10112 */
10113 if (vmcs12->launch_state == launch) {
10114 nested_vmx_failValid(vcpu,
10115 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10116 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10117 return 1;
10118 }
10119
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010120 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10121 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010122 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10123 return 1;
10124 }
10125
Wincy Van3af18d92015-02-03 23:49:31 +080010126 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010127 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10128 return 1;
10129 }
10130
Wincy Van3af18d92015-02-03 23:49:31 +080010131 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010132 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10133 return 1;
10134 }
10135
Wincy Vanf2b93282015-02-03 23:56:03 +080010136 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10137 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10138 return 1;
10139 }
10140
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010141 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10142 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10143 return 1;
10144 }
10145
Nadav Har'El7c177932011-05-25 23:12:04 +030010146 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010147 vmx->nested.nested_vmx_true_procbased_ctls_low,
10148 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010149 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010150 vmx->nested.nested_vmx_secondary_ctls_low,
10151 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010152 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010153 vmx->nested.nested_vmx_pinbased_ctls_low,
10154 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010155 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010156 vmx->nested.nested_vmx_true_exit_ctls_low,
10157 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010158 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010159 vmx->nested.nested_vmx_true_entry_ctls_low,
10160 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010161 {
10162 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10163 return 1;
10164 }
10165
10166 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10167 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10168 nested_vmx_failValid(vcpu,
10169 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10170 return 1;
10171 }
10172
Wincy Vanb9c237b2015-02-03 23:56:30 +080010173 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010174 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10175 nested_vmx_entry_failure(vcpu, vmcs12,
10176 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10177 return 1;
10178 }
10179 if (vmcs12->vmcs_link_pointer != -1ull) {
10180 nested_vmx_entry_failure(vcpu, vmcs12,
10181 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10182 return 1;
10183 }
10184
10185 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010186 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010187 * are performed on the field for the IA32_EFER MSR:
10188 * - Bits reserved in the IA32_EFER MSR must be 0.
10189 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10190 * the IA-32e mode guest VM-exit control. It must also be identical
10191 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10192 * CR0.PG) is 1.
10193 */
10194 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10195 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10196 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10197 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10198 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10199 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10200 nested_vmx_entry_failure(vcpu, vmcs12,
10201 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10202 return 1;
10203 }
10204 }
10205
10206 /*
10207 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10208 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10209 * the values of the LMA and LME bits in the field must each be that of
10210 * the host address-space size VM-exit control.
10211 */
10212 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10213 ia32e = (vmcs12->vm_exit_controls &
10214 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10215 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10216 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10217 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10218 nested_vmx_entry_failure(vcpu, vmcs12,
10219 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10220 return 1;
10221 }
10222 }
10223
10224 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010225 * We're finally done with prerequisite checking, and can start with
10226 * the nested entry.
10227 */
10228
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010229 vmcs02 = nested_get_current_vmcs02(vmx);
10230 if (!vmcs02)
10231 return -ENOMEM;
10232
10233 enter_guest_mode(vcpu);
10234
Jan Kiszka2996fca2014-06-16 13:59:43 +020010235 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10236 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10237
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010238 cpu = get_cpu();
10239 vmx->loaded_vmcs = vmcs02;
10240 vmx_vcpu_put(vcpu);
10241 vmx_vcpu_load(vcpu, cpu);
10242 vcpu->cpu = cpu;
10243 put_cpu();
10244
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010245 vmx_segment_cache_clear(vmx);
10246
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010247 prepare_vmcs02(vcpu, vmcs12);
10248
Wincy Vanff651cb2014-12-11 08:52:58 +030010249 msr_entry_idx = nested_vmx_load_msr(vcpu,
10250 vmcs12->vm_entry_msr_load_addr,
10251 vmcs12->vm_entry_msr_load_count);
10252 if (msr_entry_idx) {
10253 leave_guest_mode(vcpu);
10254 vmx_load_vmcs01(vcpu);
10255 nested_vmx_entry_failure(vcpu, vmcs12,
10256 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10257 return 1;
10258 }
10259
10260 vmcs12->launch_state = 1;
10261
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010262 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010263 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010264
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010265 vmx->nested.nested_run_pending = 1;
10266
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010267 /*
10268 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10269 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10270 * returned as far as L1 is concerned. It will only return (and set
10271 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10272 */
10273 return 1;
10274}
10275
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010276/*
10277 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10278 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10279 * This function returns the new value we should put in vmcs12.guest_cr0.
10280 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10281 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10282 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10283 * didn't trap the bit, because if L1 did, so would L0).
10284 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10285 * been modified by L2, and L1 knows it. So just leave the old value of
10286 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10287 * isn't relevant, because if L0 traps this bit it can set it to anything.
10288 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10289 * changed these bits, and therefore they need to be updated, but L0
10290 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10291 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10292 */
10293static inline unsigned long
10294vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10295{
10296 return
10297 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10298 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10299 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10300 vcpu->arch.cr0_guest_owned_bits));
10301}
10302
10303static inline unsigned long
10304vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10305{
10306 return
10307 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10308 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10309 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10310 vcpu->arch.cr4_guest_owned_bits));
10311}
10312
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010313static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10314 struct vmcs12 *vmcs12)
10315{
10316 u32 idt_vectoring;
10317 unsigned int nr;
10318
Gleb Natapov851eb6672013-09-25 12:51:34 +030010319 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010320 nr = vcpu->arch.exception.nr;
10321 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10322
10323 if (kvm_exception_is_soft(nr)) {
10324 vmcs12->vm_exit_instruction_len =
10325 vcpu->arch.event_exit_inst_len;
10326 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10327 } else
10328 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10329
10330 if (vcpu->arch.exception.has_error_code) {
10331 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10332 vmcs12->idt_vectoring_error_code =
10333 vcpu->arch.exception.error_code;
10334 }
10335
10336 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010337 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010338 vmcs12->idt_vectoring_info_field =
10339 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10340 } else if (vcpu->arch.interrupt.pending) {
10341 nr = vcpu->arch.interrupt.nr;
10342 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10343
10344 if (vcpu->arch.interrupt.soft) {
10345 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10346 vmcs12->vm_entry_instruction_len =
10347 vcpu->arch.event_exit_inst_len;
10348 } else
10349 idt_vectoring |= INTR_TYPE_EXT_INTR;
10350
10351 vmcs12->idt_vectoring_info_field = idt_vectoring;
10352 }
10353}
10354
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010355static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10356{
10357 struct vcpu_vmx *vmx = to_vmx(vcpu);
10358
Jan Kiszkaf41245002014-03-07 20:03:13 +010010359 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10360 vmx->nested.preemption_timer_expired) {
10361 if (vmx->nested.nested_run_pending)
10362 return -EBUSY;
10363 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10364 return 0;
10365 }
10366
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010367 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010368 if (vmx->nested.nested_run_pending ||
10369 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010370 return -EBUSY;
10371 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10372 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10373 INTR_INFO_VALID_MASK, 0);
10374 /*
10375 * The NMI-triggered VM exit counts as injection:
10376 * clear this one and block further NMIs.
10377 */
10378 vcpu->arch.nmi_pending = 0;
10379 vmx_set_nmi_mask(vcpu, true);
10380 return 0;
10381 }
10382
10383 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10384 nested_exit_on_intr(vcpu)) {
10385 if (vmx->nested.nested_run_pending)
10386 return -EBUSY;
10387 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010388 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010389 }
10390
Wincy Van705699a2015-02-03 23:58:17 +080010391 return vmx_complete_nested_posted_interrupt(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010392}
10393
Jan Kiszkaf41245002014-03-07 20:03:13 +010010394static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10395{
10396 ktime_t remaining =
10397 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10398 u64 value;
10399
10400 if (ktime_to_ns(remaining) <= 0)
10401 return 0;
10402
10403 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10404 do_div(value, 1000000);
10405 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10406}
10407
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010408/*
10409 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10410 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10411 * and this function updates it to reflect the changes to the guest state while
10412 * L2 was running (and perhaps made some exits which were handled directly by L0
10413 * without going back to L1), and to reflect the exit reason.
10414 * Note that we do not have to copy here all VMCS fields, just those that
10415 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10416 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10417 * which already writes to vmcs12 directly.
10418 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010419static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10420 u32 exit_reason, u32 exit_intr_info,
10421 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010422{
10423 /* update guest state fields: */
10424 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10425 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10426
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010427 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10428 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10429 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10430
10431 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10432 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10433 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10434 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10435 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10436 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10437 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10438 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10439 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10440 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10441 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10442 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10443 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10444 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10445 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10446 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10447 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10448 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10449 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10450 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10451 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10452 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10453 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10454 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10455 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10456 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10457 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10458 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10459 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10460 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10461 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10462 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10463 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10464 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10465 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10466 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10467
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010468 vmcs12->guest_interruptibility_info =
10469 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10470 vmcs12->guest_pending_dbg_exceptions =
10471 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010472 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10473 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10474 else
10475 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010476
Jan Kiszkaf41245002014-03-07 20:03:13 +010010477 if (nested_cpu_has_preemption_timer(vmcs12)) {
10478 if (vmcs12->vm_exit_controls &
10479 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10480 vmcs12->vmx_preemption_timer_value =
10481 vmx_get_preemption_timer_value(vcpu);
10482 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10483 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010484
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010485 /*
10486 * In some cases (usually, nested EPT), L2 is allowed to change its
10487 * own CR3 without exiting. If it has changed it, we must keep it.
10488 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10489 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10490 *
10491 * Additionally, restore L2's PDPTR to vmcs12.
10492 */
10493 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010494 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010495 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10496 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10497 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10498 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10499 }
10500
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010501 if (nested_cpu_has_ept(vmcs12))
10502 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
10503
Wincy Van608406e2015-02-03 23:57:51 +080010504 if (nested_cpu_has_vid(vmcs12))
10505 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10506
Jan Kiszkac18911a2013-03-13 16:06:41 +010010507 vmcs12->vm_entry_controls =
10508 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010509 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010510
Jan Kiszka2996fca2014-06-16 13:59:43 +020010511 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10512 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10513 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10514 }
10515
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010516 /* TODO: These cannot have changed unless we have MSR bitmaps and
10517 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010518 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010519 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010520 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10521 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010522 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10523 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10524 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010525 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010526 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010527 if (nested_cpu_has_xsaves(vmcs12))
10528 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010529
10530 /* update exit information fields: */
10531
Jan Kiszka533558b2014-01-04 18:47:20 +010010532 vmcs12->vm_exit_reason = exit_reason;
10533 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010534
Jan Kiszka533558b2014-01-04 18:47:20 +010010535 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010536 if ((vmcs12->vm_exit_intr_info &
10537 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10538 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10539 vmcs12->vm_exit_intr_error_code =
10540 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010541 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010542 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10543 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10544
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010545 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10546 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10547 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010548 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010549
10550 /*
10551 * Transfer the event that L0 or L1 may wanted to inject into
10552 * L2 to IDT_VECTORING_INFO_FIELD.
10553 */
10554 vmcs12_save_pending_event(vcpu, vmcs12);
10555 }
10556
10557 /*
10558 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10559 * preserved above and would only end up incorrectly in L1.
10560 */
10561 vcpu->arch.nmi_injected = false;
10562 kvm_clear_exception_queue(vcpu);
10563 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010564}
10565
10566/*
10567 * A part of what we need to when the nested L2 guest exits and we want to
10568 * run its L1 parent, is to reset L1's guest state to the host state specified
10569 * in vmcs12.
10570 * This function is to be called not only on normal nested exit, but also on
10571 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10572 * Failures During or After Loading Guest State").
10573 * This function should be called when the active VMCS is L1's (vmcs01).
10574 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010575static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10576 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010577{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010578 struct kvm_segment seg;
10579
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010580 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10581 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010582 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010583 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10584 else
10585 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10586 vmx_set_efer(vcpu, vcpu->arch.efer);
10587
10588 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10589 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010590 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010591 /*
10592 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10593 * actually changed, because it depends on the current state of
10594 * fpu_active (which may have changed).
10595 * Note that vmx_set_cr0 refers to efer set above.
10596 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010597 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010598 /*
10599 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10600 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10601 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10602 */
10603 update_exception_bitmap(vcpu);
10604 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10605 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10606
10607 /*
10608 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10609 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10610 */
10611 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10612 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10613
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010614 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010615
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010616 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10617 kvm_mmu_reset_context(vcpu);
10618
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010619 if (!enable_ept)
10620 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10621
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010622 if (enable_vpid) {
10623 /*
10624 * Trivially support vpid by letting L2s share their parent
10625 * L1's vpid. TODO: move to a more elaborate solution, giving
10626 * each L2 its own vpid and exposing the vpid feature to L1.
10627 */
10628 vmx_flush_tlb(vcpu);
10629 }
10630
10631
10632 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10633 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10634 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10635 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10636 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010637
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010638 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10639 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10640 vmcs_write64(GUEST_BNDCFGS, 0);
10641
Jan Kiszka44811c02013-08-04 17:17:27 +020010642 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010643 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010644 vcpu->arch.pat = vmcs12->host_ia32_pat;
10645 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010646 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10647 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10648 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010649
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010650 /* Set L1 segment info according to Intel SDM
10651 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10652 seg = (struct kvm_segment) {
10653 .base = 0,
10654 .limit = 0xFFFFFFFF,
10655 .selector = vmcs12->host_cs_selector,
10656 .type = 11,
10657 .present = 1,
10658 .s = 1,
10659 .g = 1
10660 };
10661 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10662 seg.l = 1;
10663 else
10664 seg.db = 1;
10665 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10666 seg = (struct kvm_segment) {
10667 .base = 0,
10668 .limit = 0xFFFFFFFF,
10669 .type = 3,
10670 .present = 1,
10671 .s = 1,
10672 .db = 1,
10673 .g = 1
10674 };
10675 seg.selector = vmcs12->host_ds_selector;
10676 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10677 seg.selector = vmcs12->host_es_selector;
10678 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10679 seg.selector = vmcs12->host_ss_selector;
10680 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10681 seg.selector = vmcs12->host_fs_selector;
10682 seg.base = vmcs12->host_fs_base;
10683 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10684 seg.selector = vmcs12->host_gs_selector;
10685 seg.base = vmcs12->host_gs_base;
10686 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10687 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010688 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010689 .limit = 0x67,
10690 .selector = vmcs12->host_tr_selector,
10691 .type = 11,
10692 .present = 1
10693 };
10694 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10695
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010696 kvm_set_dr(vcpu, 7, 0x400);
10697 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010698
Wincy Van3af18d92015-02-03 23:49:31 +080010699 if (cpu_has_vmx_msr_bitmap())
10700 vmx_set_msr_bitmap(vcpu);
10701
Wincy Vanff651cb2014-12-11 08:52:58 +030010702 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10703 vmcs12->vm_exit_msr_load_count))
10704 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010705}
10706
10707/*
10708 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10709 * and modify vmcs12 to make it see what it would expect to see there if
10710 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10711 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010712static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10713 u32 exit_intr_info,
10714 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010715{
10716 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010717 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattsoncf3215d2016-09-06 09:33:21 -070010718 u32 vm_inst_error = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010719
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010720 /* trying to cancel vmlaunch/vmresume is a bug */
10721 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10722
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010723 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010724 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10725 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010726
Wincy Vanff651cb2014-12-11 08:52:58 +030010727 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10728 vmcs12->vm_exit_msr_store_count))
10729 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10730
Jim Mattsoncf3215d2016-09-06 09:33:21 -070010731 if (unlikely(vmx->fail))
10732 vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR);
10733
Wanpeng Lif3380ca2014-08-05 12:42:23 +080010734 vmx_load_vmcs01(vcpu);
10735
Bandan Das77b0f5d2014-04-19 18:17:45 -040010736 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10737 && nested_exit_intr_ack_set(vcpu)) {
10738 int irq = kvm_cpu_get_interrupt(vcpu);
10739 WARN_ON(irq < 0);
10740 vmcs12->vm_exit_intr_info = irq |
10741 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10742 }
10743
Jan Kiszka542060e2014-01-04 18:47:21 +010010744 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10745 vmcs12->exit_qualification,
10746 vmcs12->idt_vectoring_info_field,
10747 vmcs12->vm_exit_intr_info,
10748 vmcs12->vm_exit_intr_error_code,
10749 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010750
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010751 vm_entry_controls_reset_shadow(vmx);
10752 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010753 vmx_segment_cache_clear(vmx);
10754
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010755 /* if no vmcs02 cache requested, remove the one we used */
10756 if (VMCS02_POOL_SIZE == 0)
10757 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10758
10759 load_vmcs12_host_state(vcpu, vmcs12);
10760
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010761 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070010762 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10763 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010764 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010765 if (vmx->hv_deadline_tsc == -1)
10766 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10767 PIN_BASED_VMX_PREEMPTION_TIMER);
10768 else
10769 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10770 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070010771 if (kvm_has_tsc_control)
10772 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010773
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010774 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10775 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10776 vmx_set_virtual_x2apic_mode(vcpu,
10777 vcpu->arch.apic_base & X2APIC_ENABLE);
10778 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010779
10780 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10781 vmx->host_rsp = 0;
10782
10783 /* Unpin physical memory we referred to in vmcs02 */
10784 if (vmx->nested.apic_access_page) {
10785 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010786 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010787 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010788 if (vmx->nested.virtual_apic_page) {
10789 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010790 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010791 }
Wincy Van705699a2015-02-03 23:58:17 +080010792 if (vmx->nested.pi_desc_page) {
10793 kunmap(vmx->nested.pi_desc_page);
10794 nested_release_page(vmx->nested.pi_desc_page);
10795 vmx->nested.pi_desc_page = NULL;
10796 vmx->nested.pi_desc = NULL;
10797 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010798
10799 /*
Tang Chen38b99172014-09-24 15:57:54 +080010800 * We are now running in L2, mmu_notifier will force to reload the
10801 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10802 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080010803 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080010804
10805 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010806 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10807 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10808 * success or failure flag accordingly.
10809 */
10810 if (unlikely(vmx->fail)) {
10811 vmx->fail = 0;
Jim Mattsoncf3215d2016-09-06 09:33:21 -070010812 nested_vmx_failValid(vcpu, vm_inst_error);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010813 } else
10814 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010815 if (enable_shadow_vmcs)
10816 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010817
10818 /* in case we halted in L2 */
10819 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010820}
10821
Nadav Har'El7c177932011-05-25 23:12:04 +030010822/*
Jan Kiszka42124922014-01-04 18:47:19 +010010823 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10824 */
10825static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10826{
10827 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +010010828 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +010010829 free_nested(to_vmx(vcpu));
10830}
10831
10832/*
Nadav Har'El7c177932011-05-25 23:12:04 +030010833 * L1's failure to enter L2 is a subset of a normal exit, as explained in
10834 * 23.7 "VM-entry failures during or after loading guest state" (this also
10835 * lists the acceptable exit-reason and exit-qualification parameters).
10836 * It should only be called before L2 actually succeeded to run, and when
10837 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
10838 */
10839static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
10840 struct vmcs12 *vmcs12,
10841 u32 reason, unsigned long qualification)
10842{
10843 load_vmcs12_host_state(vcpu, vmcs12);
10844 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
10845 vmcs12->exit_qualification = qualification;
10846 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010847 if (enable_shadow_vmcs)
10848 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030010849}
10850
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010851static int vmx_check_intercept(struct kvm_vcpu *vcpu,
10852 struct x86_instruction_info *info,
10853 enum x86_intercept_stage stage)
10854{
10855 return X86EMUL_CONTINUE;
10856}
10857
Yunhong Jiang64672c92016-06-13 14:19:59 -070010858#ifdef CONFIG_X86_64
10859/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
10860static inline int u64_shl_div_u64(u64 a, unsigned int shift,
10861 u64 divisor, u64 *result)
10862{
10863 u64 low = a << shift, high = a >> (64 - shift);
10864
10865 /* To avoid the overflow on divq */
10866 if (high >= divisor)
10867 return 1;
10868
10869 /* Low hold the result, high hold rem which is discarded */
10870 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
10871 "rm" (divisor), "0" (low), "1" (high));
10872 *result = low;
10873
10874 return 0;
10875}
10876
10877static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
10878{
10879 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020010880 u64 tscl = rdtsc();
10881 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
10882 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010883
10884 /* Convert to host delta tsc if tsc scaling is enabled */
10885 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
10886 u64_shl_div_u64(delta_tsc,
10887 kvm_tsc_scaling_ratio_frac_bits,
10888 vcpu->arch.tsc_scaling_ratio,
10889 &delta_tsc))
10890 return -ERANGE;
10891
10892 /*
10893 * If the delta tsc can't fit in the 32 bit after the multi shift,
10894 * we can't use the preemption timer.
10895 * It's possible that it fits on later vmentries, but checking
10896 * on every vmentry is costly so we just use an hrtimer.
10897 */
10898 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
10899 return -ERANGE;
10900
10901 vmx->hv_deadline_tsc = tscl + delta_tsc;
10902 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10903 PIN_BASED_VMX_PREEMPTION_TIMER);
10904 return 0;
10905}
10906
10907static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
10908{
10909 struct vcpu_vmx *vmx = to_vmx(vcpu);
10910 vmx->hv_deadline_tsc = -1;
10911 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10912 PIN_BASED_VMX_PREEMPTION_TIMER);
10913}
10914#endif
10915
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010916static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010917{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020010918 if (ple_gap)
10919 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010920}
10921
Kai Huang843e4332015-01-28 10:54:28 +080010922static void vmx_slot_enable_log_dirty(struct kvm *kvm,
10923 struct kvm_memory_slot *slot)
10924{
10925 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
10926 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
10927}
10928
10929static void vmx_slot_disable_log_dirty(struct kvm *kvm,
10930 struct kvm_memory_slot *slot)
10931{
10932 kvm_mmu_slot_set_dirty(kvm, slot);
10933}
10934
10935static void vmx_flush_log_dirty(struct kvm *kvm)
10936{
10937 kvm_flush_pml_buffers(kvm);
10938}
10939
10940static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
10941 struct kvm_memory_slot *memslot,
10942 gfn_t offset, unsigned long mask)
10943{
10944 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
10945}
10946
Feng Wuefc64402015-09-18 22:29:51 +080010947/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080010948 * This routine does the following things for vCPU which is going
10949 * to be blocked if VT-d PI is enabled.
10950 * - Store the vCPU to the wakeup list, so when interrupts happen
10951 * we can find the right vCPU to wake up.
10952 * - Change the Posted-interrupt descriptor as below:
10953 * 'NDST' <-- vcpu->pre_pcpu
10954 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
10955 * - If 'ON' is set during this process, which means at least one
10956 * interrupt is posted for this vCPU, we cannot block it, in
10957 * this case, return 1, otherwise, return 0.
10958 *
10959 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070010960static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080010961{
10962 unsigned long flags;
10963 unsigned int dest;
10964 struct pi_desc old, new;
10965 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
10966
10967 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080010968 !irq_remapping_cap(IRQ_POSTING_CAP) ||
10969 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080010970 return 0;
10971
10972 vcpu->pre_pcpu = vcpu->cpu;
10973 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10974 vcpu->pre_pcpu), flags);
10975 list_add_tail(&vcpu->blocked_vcpu_list,
10976 &per_cpu(blocked_vcpu_on_cpu,
10977 vcpu->pre_pcpu));
10978 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
10979 vcpu->pre_pcpu), flags);
10980
10981 do {
10982 old.control = new.control = pi_desc->control;
10983
10984 /*
10985 * We should not block the vCPU if
10986 * an interrupt is posted for it.
10987 */
10988 if (pi_test_on(pi_desc) == 1) {
10989 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10990 vcpu->pre_pcpu), flags);
10991 list_del(&vcpu->blocked_vcpu_list);
10992 spin_unlock_irqrestore(
10993 &per_cpu(blocked_vcpu_on_cpu_lock,
10994 vcpu->pre_pcpu), flags);
10995 vcpu->pre_pcpu = -1;
10996
10997 return 1;
10998 }
10999
11000 WARN((pi_desc->sn == 1),
11001 "Warning: SN field of posted-interrupts "
11002 "is set before blocking\n");
11003
11004 /*
11005 * Since vCPU can be preempted during this process,
11006 * vcpu->cpu could be different with pre_pcpu, we
11007 * need to set pre_pcpu as the destination of wakeup
11008 * notification event, then we can find the right vCPU
11009 * to wakeup in wakeup handler if interrupts happen
11010 * when the vCPU is in blocked state.
11011 */
11012 dest = cpu_physical_id(vcpu->pre_pcpu);
11013
11014 if (x2apic_enabled())
11015 new.ndst = dest;
11016 else
11017 new.ndst = (dest << 8) & 0xFF00;
11018
11019 /* set 'NV' to 'wakeup vector' */
11020 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11021 } while (cmpxchg(&pi_desc->control, old.control,
11022 new.control) != old.control);
11023
11024 return 0;
11025}
11026
Yunhong Jiangbc225122016-06-13 14:19:58 -070011027static int vmx_pre_block(struct kvm_vcpu *vcpu)
11028{
11029 if (pi_pre_block(vcpu))
11030 return 1;
11031
Yunhong Jiang64672c92016-06-13 14:19:59 -070011032 if (kvm_lapic_hv_timer_in_use(vcpu))
11033 kvm_lapic_switch_to_sw_timer(vcpu);
11034
Yunhong Jiangbc225122016-06-13 14:19:58 -070011035 return 0;
11036}
11037
11038static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011039{
11040 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11041 struct pi_desc old, new;
11042 unsigned int dest;
11043 unsigned long flags;
11044
11045 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011046 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11047 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011048 return;
11049
11050 do {
11051 old.control = new.control = pi_desc->control;
11052
11053 dest = cpu_physical_id(vcpu->cpu);
11054
11055 if (x2apic_enabled())
11056 new.ndst = dest;
11057 else
11058 new.ndst = (dest << 8) & 0xFF00;
11059
11060 /* Allow posting non-urgent interrupts */
11061 new.sn = 0;
11062
11063 /* set 'NV' to 'notification vector' */
11064 new.nv = POSTED_INTR_VECTOR;
11065 } while (cmpxchg(&pi_desc->control, old.control,
11066 new.control) != old.control);
11067
11068 if(vcpu->pre_pcpu != -1) {
11069 spin_lock_irqsave(
11070 &per_cpu(blocked_vcpu_on_cpu_lock,
11071 vcpu->pre_pcpu), flags);
11072 list_del(&vcpu->blocked_vcpu_list);
11073 spin_unlock_irqrestore(
11074 &per_cpu(blocked_vcpu_on_cpu_lock,
11075 vcpu->pre_pcpu), flags);
11076 vcpu->pre_pcpu = -1;
11077 }
11078}
11079
Yunhong Jiangbc225122016-06-13 14:19:58 -070011080static void vmx_post_block(struct kvm_vcpu *vcpu)
11081{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011082 if (kvm_x86_ops->set_hv_timer)
11083 kvm_lapic_switch_to_hv_timer(vcpu);
11084
Yunhong Jiangbc225122016-06-13 14:19:58 -070011085 pi_post_block(vcpu);
11086}
11087
Feng Wubf9f6ac2015-09-18 22:29:55 +080011088/*
Feng Wuefc64402015-09-18 22:29:51 +080011089 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11090 *
11091 * @kvm: kvm
11092 * @host_irq: host irq of the interrupt
11093 * @guest_irq: gsi of the interrupt
11094 * @set: set or unset PI
11095 * returns 0 on success, < 0 on failure
11096 */
11097static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11098 uint32_t guest_irq, bool set)
11099{
11100 struct kvm_kernel_irq_routing_entry *e;
11101 struct kvm_irq_routing_table *irq_rt;
11102 struct kvm_lapic_irq irq;
11103 struct kvm_vcpu *vcpu;
11104 struct vcpu_data vcpu_info;
11105 int idx, ret = -EINVAL;
11106
11107 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011108 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11109 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011110 return 0;
11111
11112 idx = srcu_read_lock(&kvm->irq_srcu);
11113 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11114 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
11115
11116 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11117 if (e->type != KVM_IRQ_ROUTING_MSI)
11118 continue;
11119 /*
11120 * VT-d PI cannot support posting multicast/broadcast
11121 * interrupts to a vCPU, we still use interrupt remapping
11122 * for these kind of interrupts.
11123 *
11124 * For lowest-priority interrupts, we only support
11125 * those with single CPU as the destination, e.g. user
11126 * configures the interrupts via /proc/irq or uses
11127 * irqbalance to make the interrupts single-CPU.
11128 *
11129 * We will support full lowest-priority interrupt later.
11130 */
11131
Radim Krčmář371313132016-07-12 22:09:27 +020011132 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011133 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11134 /*
11135 * Make sure the IRTE is in remapped mode if
11136 * we don't handle it in posted mode.
11137 */
11138 ret = irq_set_vcpu_affinity(host_irq, NULL);
11139 if (ret < 0) {
11140 printk(KERN_INFO
11141 "failed to back to remapped mode, irq: %u\n",
11142 host_irq);
11143 goto out;
11144 }
11145
Feng Wuefc64402015-09-18 22:29:51 +080011146 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011147 }
Feng Wuefc64402015-09-18 22:29:51 +080011148
11149 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11150 vcpu_info.vector = irq.vector;
11151
Feng Wub6ce9782016-01-25 16:53:35 +080011152 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011153 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11154
11155 if (set)
11156 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11157 else {
11158 /* suppress notification event before unposting */
11159 pi_set_sn(vcpu_to_pi_desc(vcpu));
11160 ret = irq_set_vcpu_affinity(host_irq, NULL);
11161 pi_clear_sn(vcpu_to_pi_desc(vcpu));
11162 }
11163
11164 if (ret < 0) {
11165 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11166 __func__);
11167 goto out;
11168 }
11169 }
11170
11171 ret = 0;
11172out:
11173 srcu_read_unlock(&kvm->irq_srcu, idx);
11174 return ret;
11175}
11176
Ashok Rajc45dcc72016-06-22 14:59:56 +080011177static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11178{
11179 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11180 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11181 FEATURE_CONTROL_LMCE;
11182 else
11183 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11184 ~FEATURE_CONTROL_LMCE;
11185}
11186
Kees Cook404f6aa2016-08-08 16:29:06 -070011187static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011188 .cpu_has_kvm_support = cpu_has_kvm_support,
11189 .disabled_by_bios = vmx_disabled_by_bios,
11190 .hardware_setup = hardware_setup,
11191 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011192 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011193 .hardware_enable = hardware_enable,
11194 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011195 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020011196 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011197
11198 .vcpu_create = vmx_create_vcpu,
11199 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011200 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011201
Avi Kivity04d2cc72007-09-10 18:10:54 +030011202 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011203 .vcpu_load = vmx_vcpu_load,
11204 .vcpu_put = vmx_vcpu_put,
11205
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011206 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011207 .get_msr = vmx_get_msr,
11208 .set_msr = vmx_set_msr,
11209 .get_segment_base = vmx_get_segment_base,
11210 .get_segment = vmx_get_segment,
11211 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011212 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011213 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011214 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011215 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011216 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011217 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011218 .set_cr3 = vmx_set_cr3,
11219 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011220 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011221 .get_idt = vmx_get_idt,
11222 .set_idt = vmx_set_idt,
11223 .get_gdt = vmx_get_gdt,
11224 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011225 .get_dr6 = vmx_get_dr6,
11226 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011227 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011228 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011229 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011230 .get_rflags = vmx_get_rflags,
11231 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011232
11233 .get_pkru = vmx_get_pkru,
11234
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011235 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011236 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011237
11238 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011239
Avi Kivity6aa8b732006-12-10 02:21:36 -080011240 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011241 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011242 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011243 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11244 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011245 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011246 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011247 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011248 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011249 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011250 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011251 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011252 .get_nmi_mask = vmx_get_nmi_mask,
11253 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011254 .enable_nmi_window = enable_nmi_window,
11255 .enable_irq_window = enable_irq_window,
11256 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011257 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011258 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011259 .get_enable_apicv = vmx_get_enable_apicv,
11260 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011261 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11262 .hwapic_irr_update = vmx_hwapic_irr_update,
11263 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011264 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11265 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011266
Izik Eiduscbc94022007-10-25 00:29:55 +020011267 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011268 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011269 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011270
Avi Kivity586f9602010-11-18 13:09:54 +020011271 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011272
Sheng Yang17cc3932010-01-05 19:02:27 +080011273 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011274
11275 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011276
11277 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011278 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011279
11280 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011281
11282 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011283
11284 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011285
11286 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011287
11288 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011289 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011290 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011291 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011292
11293 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011294
11295 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011296
11297 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11298 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11299 .flush_log_dirty = vmx_flush_log_dirty,
11300 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011301
Feng Wubf9f6ac2015-09-18 22:29:55 +080011302 .pre_block = vmx_pre_block,
11303 .post_block = vmx_post_block,
11304
Wei Huang25462f72015-06-19 15:45:05 +020011305 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011306
11307 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011308
11309#ifdef CONFIG_X86_64
11310 .set_hv_timer = vmx_set_hv_timer,
11311 .cancel_hv_timer = vmx_cancel_hv_timer,
11312#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011313
11314 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011315};
11316
11317static int __init vmx_init(void)
11318{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011319 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11320 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011321 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011322 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011323
Dave Young2965faa2015-09-09 15:38:55 -070011324#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011325 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11326 crash_vmclear_local_loaded_vmcss);
11327#endif
11328
He, Qingfdef3ad2007-04-30 09:45:24 +030011329 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011330}
11331
11332static void __exit vmx_exit(void)
11333{
Dave Young2965faa2015-09-09 15:38:55 -070011334#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011335 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011336 synchronize_rcu();
11337#endif
11338
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011339 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011340}
11341
11342module_init(vmx_init)
11343module_exit(vmx_exit)