blob: ae111a07acc485af75664fccd127ea6f6abe7697 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030036#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030037#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040038
Feng Wu28b835d2015-09-18 22:29:54 +080039#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080040#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080041#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020042#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020043#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080044#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020045#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020046#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010047#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080048#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010049#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080050#include <asm/irq_remapping.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Marcelo Tosatti229456f2009-06-17 09:22:14 -030052#include "trace.h"
Wei Huang25462f7f2015-06-19 15:45:05 +020053#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030054
Avi Kivity4ecac3f2008-05-13 13:23:38 +030055#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040056#define __ex_clear(x, reg) \
57 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030058
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Josh Triplette9bda3b2012-03-20 23:33:51 -070062static const struct x86_cpu_id vmx_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_VMX),
64 {}
65};
66MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
67
Rusty Russell476bc002012-01-13 09:32:18 +103068static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020069module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080070
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020072module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020073
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080076
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070078module_param_named(unrestricted_guest,
79 enable_unrestricted_guest, bool, S_IRUGO);
80
Xudong Hao83c3a332012-05-28 19:33:35 +080081static bool __read_mostly enable_ept_ad_bits = 1;
82module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
83
Avi Kivitya27685c2012-06-12 20:30:18 +030084static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020085module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080088module_param(vmm_exclusive, bool, S_IRUGO);
89
Rusty Russell476bc002012-01-13 09:32:18 +103090static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030091module_param(fasteoi, bool, S_IRUGO);
92
Yang Zhang5a717852013-04-11 19:25:16 +080093static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080094module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080095
Abel Gordonabc4fc52013-04-18 14:35:25 +030096static bool __read_mostly enable_shadow_vmcs = 1;
97module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030098/*
99 * If nested=1, nested virtualization is supported, i.e., guests may use
100 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
101 * use VMX instructions.
102 */
Rusty Russell476bc002012-01-13 09:32:18 +1030103static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300104module_param(nested, bool, S_IRUGO);
105
Wanpeng Li20300092014-12-02 19:14:59 +0800106static u64 __read_mostly host_xss;
107
Kai Huang843e4332015-01-28 10:54:28 +0800108static bool __read_mostly enable_pml = 1;
109module_param_named(pml, enable_pml, bool, S_IRUGO);
110
Haozhong Zhang64903d62015-10-20 15:39:09 +0800111#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
112
Yunhong Jiang64672c92016-06-13 14:19:59 -0700113/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
114static int __read_mostly cpu_preemption_timer_multi;
115static bool __read_mostly enable_preemption_timer = 1;
116#ifdef CONFIG_X86_64
117module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
118#endif
119
Gleb Natapov50378782013-02-04 16:00:28 +0200120#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
121#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200122#define KVM_VM_CR0_ALWAYS_ON \
123 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200124#define KVM_CR4_GUEST_OWNED_BITS \
125 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700126 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200127
Avi Kivitycdc0e242009-12-06 17:21:14 +0200128#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
129#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
130
Avi Kivity78ac8b42010-04-08 18:19:35 +0300131#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
132
Jan Kiszkaf4124502014-03-07 20:03:13 +0100133#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
134
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800135/*
136 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
137 * ple_gap: upper bound on the amount of time between two successive
138 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500139 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800140 * ple_window: upper bound on the amount of time a guest is allowed to execute
141 * in a PAUSE loop. Tests indicate that most spinlocks are held for
142 * less than 2^12 cycles
143 * Time is measured based on a counter that runs at the same rate as the TSC,
144 * refer SDM volume 3b section 21.6.13 & 22.1.3.
145 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200146#define KVM_VMX_DEFAULT_PLE_GAP 128
147#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
148#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
149#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
150#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
151 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
152
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
154module_param(ple_gap, int, S_IRUGO);
155
156static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
157module_param(ple_window, int, S_IRUGO);
158
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200159/* Default doubles per-vcpu window every exit. */
160static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
161module_param(ple_window_grow, int, S_IRUGO);
162
163/* Default resets per-vcpu window every exit to ple_window. */
164static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
165module_param(ple_window_shrink, int, S_IRUGO);
166
167/* Default is to compute the maximum so we can never overflow. */
168static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
169static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
170module_param(ple_window_max, int, S_IRUGO);
171
Avi Kivity83287ea422012-09-16 15:10:57 +0300172extern const ulong vmx_return;
173
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200174#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300175#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300176
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400177struct vmcs {
178 u32 revision_id;
179 u32 abort;
180 char data[0];
181};
182
Nadav Har'Eld462b812011-05-24 15:26:10 +0300183/*
184 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
185 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
186 * loaded on this CPU (so we can clear them if the CPU goes down).
187 */
188struct loaded_vmcs {
189 struct vmcs *vmcs;
190 int cpu;
191 int launched;
192 struct list_head loaded_vmcss_on_cpu_link;
193};
194
Avi Kivity26bb0982009-09-07 11:14:12 +0300195struct shared_msr_entry {
196 unsigned index;
197 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200198 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300199};
200
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300201/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300202 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
203 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
204 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
205 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
206 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
207 * More than one of these structures may exist, if L1 runs multiple L2 guests.
208 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
209 * underlying hardware which will be used to run L2.
210 * This structure is packed to ensure that its layout is identical across
211 * machines (necessary for live migration).
212 * If there are changes in this struct, VMCS12_REVISION must be changed.
213 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300214typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300215struct __packed vmcs12 {
216 /* According to the Intel spec, a VMCS region must start with the
217 * following two fields. Then follow implementation-specific data.
218 */
219 u32 revision_id;
220 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300221
Nadav Har'El27d6c862011-05-25 23:06:59 +0300222 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
223 u32 padding[7]; /* room for future expansion */
224
Nadav Har'El22bd0352011-05-25 23:05:57 +0300225 u64 io_bitmap_a;
226 u64 io_bitmap_b;
227 u64 msr_bitmap;
228 u64 vm_exit_msr_store_addr;
229 u64 vm_exit_msr_load_addr;
230 u64 vm_entry_msr_load_addr;
231 u64 tsc_offset;
232 u64 virtual_apic_page_addr;
233 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800234 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300235 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800236 u64 eoi_exit_bitmap0;
237 u64 eoi_exit_bitmap1;
238 u64 eoi_exit_bitmap2;
239 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800240 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300241 u64 guest_physical_address;
242 u64 vmcs_link_pointer;
243 u64 guest_ia32_debugctl;
244 u64 guest_ia32_pat;
245 u64 guest_ia32_efer;
246 u64 guest_ia32_perf_global_ctrl;
247 u64 guest_pdptr0;
248 u64 guest_pdptr1;
249 u64 guest_pdptr2;
250 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100251 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300252 u64 host_ia32_pat;
253 u64 host_ia32_efer;
254 u64 host_ia32_perf_global_ctrl;
255 u64 padding64[8]; /* room for future expansion */
256 /*
257 * To allow migration of L1 (complete with its L2 guests) between
258 * machines of different natural widths (32 or 64 bit), we cannot have
259 * unsigned long fields with no explict size. We use u64 (aliased
260 * natural_width) instead. Luckily, x86 is little-endian.
261 */
262 natural_width cr0_guest_host_mask;
263 natural_width cr4_guest_host_mask;
264 natural_width cr0_read_shadow;
265 natural_width cr4_read_shadow;
266 natural_width cr3_target_value0;
267 natural_width cr3_target_value1;
268 natural_width cr3_target_value2;
269 natural_width cr3_target_value3;
270 natural_width exit_qualification;
271 natural_width guest_linear_address;
272 natural_width guest_cr0;
273 natural_width guest_cr3;
274 natural_width guest_cr4;
275 natural_width guest_es_base;
276 natural_width guest_cs_base;
277 natural_width guest_ss_base;
278 natural_width guest_ds_base;
279 natural_width guest_fs_base;
280 natural_width guest_gs_base;
281 natural_width guest_ldtr_base;
282 natural_width guest_tr_base;
283 natural_width guest_gdtr_base;
284 natural_width guest_idtr_base;
285 natural_width guest_dr7;
286 natural_width guest_rsp;
287 natural_width guest_rip;
288 natural_width guest_rflags;
289 natural_width guest_pending_dbg_exceptions;
290 natural_width guest_sysenter_esp;
291 natural_width guest_sysenter_eip;
292 natural_width host_cr0;
293 natural_width host_cr3;
294 natural_width host_cr4;
295 natural_width host_fs_base;
296 natural_width host_gs_base;
297 natural_width host_tr_base;
298 natural_width host_gdtr_base;
299 natural_width host_idtr_base;
300 natural_width host_ia32_sysenter_esp;
301 natural_width host_ia32_sysenter_eip;
302 natural_width host_rsp;
303 natural_width host_rip;
304 natural_width paddingl[8]; /* room for future expansion */
305 u32 pin_based_vm_exec_control;
306 u32 cpu_based_vm_exec_control;
307 u32 exception_bitmap;
308 u32 page_fault_error_code_mask;
309 u32 page_fault_error_code_match;
310 u32 cr3_target_count;
311 u32 vm_exit_controls;
312 u32 vm_exit_msr_store_count;
313 u32 vm_exit_msr_load_count;
314 u32 vm_entry_controls;
315 u32 vm_entry_msr_load_count;
316 u32 vm_entry_intr_info_field;
317 u32 vm_entry_exception_error_code;
318 u32 vm_entry_instruction_len;
319 u32 tpr_threshold;
320 u32 secondary_vm_exec_control;
321 u32 vm_instruction_error;
322 u32 vm_exit_reason;
323 u32 vm_exit_intr_info;
324 u32 vm_exit_intr_error_code;
325 u32 idt_vectoring_info_field;
326 u32 idt_vectoring_error_code;
327 u32 vm_exit_instruction_len;
328 u32 vmx_instruction_info;
329 u32 guest_es_limit;
330 u32 guest_cs_limit;
331 u32 guest_ss_limit;
332 u32 guest_ds_limit;
333 u32 guest_fs_limit;
334 u32 guest_gs_limit;
335 u32 guest_ldtr_limit;
336 u32 guest_tr_limit;
337 u32 guest_gdtr_limit;
338 u32 guest_idtr_limit;
339 u32 guest_es_ar_bytes;
340 u32 guest_cs_ar_bytes;
341 u32 guest_ss_ar_bytes;
342 u32 guest_ds_ar_bytes;
343 u32 guest_fs_ar_bytes;
344 u32 guest_gs_ar_bytes;
345 u32 guest_ldtr_ar_bytes;
346 u32 guest_tr_ar_bytes;
347 u32 guest_interruptibility_info;
348 u32 guest_activity_state;
349 u32 guest_sysenter_cs;
350 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100351 u32 vmx_preemption_timer_value;
352 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300353 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800354 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300355 u16 guest_es_selector;
356 u16 guest_cs_selector;
357 u16 guest_ss_selector;
358 u16 guest_ds_selector;
359 u16 guest_fs_selector;
360 u16 guest_gs_selector;
361 u16 guest_ldtr_selector;
362 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800363 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300364 u16 host_es_selector;
365 u16 host_cs_selector;
366 u16 host_ss_selector;
367 u16 host_ds_selector;
368 u16 host_fs_selector;
369 u16 host_gs_selector;
370 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300371};
372
373/*
374 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
375 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
376 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
377 */
378#define VMCS12_REVISION 0x11e57ed0
379
380/*
381 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
382 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
383 * current implementation, 4K are reserved to avoid future complications.
384 */
385#define VMCS12_SIZE 0x1000
386
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300387/* Used to remember the last vmcs02 used for some recently used vmcs12s */
388struct vmcs02_list {
389 struct list_head list;
390 gpa_t vmptr;
391 struct loaded_vmcs vmcs02;
392};
393
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300394/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300395 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
396 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
397 */
398struct nested_vmx {
399 /* Has the level1 guest done vmxon? */
400 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400401 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300402
403 /* The guest-physical address of the current VMCS L1 keeps for L2 */
404 gpa_t current_vmptr;
405 /* The host-usable pointer to the above */
406 struct page *current_vmcs12_page;
407 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700408 /*
409 * Cache of the guest's VMCS, existing outside of guest memory.
410 * Loaded from guest memory during VMPTRLD. Flushed to guest
411 * memory during VMXOFF, VMCLEAR, VMPTRLD.
412 */
413 struct vmcs12 *cached_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300414 struct vmcs *current_shadow_vmcs;
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;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300424 u64 vmcs01_tsc_offset;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200425 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300426 /* L2 must run next, and mustn't decide to exit to L1. */
427 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300428 /*
429 * Guest pages referred to in vmcs02 with host-physical pointers, so
430 * we must keep them pinned while L2 runs.
431 */
432 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800433 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800434 struct page *pi_desc_page;
435 struct pi_desc *pi_desc;
436 bool pi_pending;
437 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100438
Radim Krčmářd048c092016-08-08 20:16:22 +0200439 unsigned long *msr_bitmap;
440
Jan Kiszkaf4124502014-03-07 20:03:13 +0100441 struct hrtimer preemption_timer;
442 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200443
444 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
445 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800446
Wanpeng Li5c614b32015-10-13 09:18:36 -0700447 u16 vpid02;
448 u16 last_vpid;
449
Wincy Vanb9c237b2015-02-03 23:56:30 +0800450 u32 nested_vmx_procbased_ctls_low;
451 u32 nested_vmx_procbased_ctls_high;
452 u32 nested_vmx_true_procbased_ctls_low;
453 u32 nested_vmx_secondary_ctls_low;
454 u32 nested_vmx_secondary_ctls_high;
455 u32 nested_vmx_pinbased_ctls_low;
456 u32 nested_vmx_pinbased_ctls_high;
457 u32 nested_vmx_exit_ctls_low;
458 u32 nested_vmx_exit_ctls_high;
459 u32 nested_vmx_true_exit_ctls_low;
460 u32 nested_vmx_entry_ctls_low;
461 u32 nested_vmx_entry_ctls_high;
462 u32 nested_vmx_true_entry_ctls_low;
463 u32 nested_vmx_misc_low;
464 u32 nested_vmx_misc_high;
465 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700466 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300467};
468
Yang Zhang01e439b2013-04-11 19:25:12 +0800469#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800470#define POSTED_INTR_SN 1
471
Yang Zhang01e439b2013-04-11 19:25:12 +0800472/* Posted-Interrupt Descriptor */
473struct pi_desc {
474 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800475 union {
476 struct {
477 /* bit 256 - Outstanding Notification */
478 u16 on : 1,
479 /* bit 257 - Suppress Notification */
480 sn : 1,
481 /* bit 271:258 - Reserved */
482 rsvd_1 : 14;
483 /* bit 279:272 - Notification Vector */
484 u8 nv;
485 /* bit 287:280 - Reserved */
486 u8 rsvd_2;
487 /* bit 319:288 - Notification Destination */
488 u32 ndst;
489 };
490 u64 control;
491 };
492 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800493} __aligned(64);
494
Yang Zhanga20ed542013-04-11 19:25:15 +0800495static bool pi_test_and_set_on(struct pi_desc *pi_desc)
496{
497 return test_and_set_bit(POSTED_INTR_ON,
498 (unsigned long *)&pi_desc->control);
499}
500
501static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
502{
503 return test_and_clear_bit(POSTED_INTR_ON,
504 (unsigned long *)&pi_desc->control);
505}
506
507static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
508{
509 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
510}
511
Feng Wuebbfc762015-09-18 22:29:46 +0800512static inline void pi_clear_sn(struct pi_desc *pi_desc)
513{
514 return clear_bit(POSTED_INTR_SN,
515 (unsigned long *)&pi_desc->control);
516}
517
518static inline void pi_set_sn(struct pi_desc *pi_desc)
519{
520 return set_bit(POSTED_INTR_SN,
521 (unsigned long *)&pi_desc->control);
522}
523
524static inline int pi_test_on(struct pi_desc *pi_desc)
525{
526 return test_bit(POSTED_INTR_ON,
527 (unsigned long *)&pi_desc->control);
528}
529
530static inline int pi_test_sn(struct pi_desc *pi_desc)
531{
532 return test_bit(POSTED_INTR_SN,
533 (unsigned long *)&pi_desc->control);
534}
535
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400536struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000537 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300538 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300539 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200540 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300541 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200542 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200543 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300544 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400545 int nmsrs;
546 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800547 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400548#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300549 u64 msr_host_kernel_gs_base;
550 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400551#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200552 u32 vm_entry_controls_shadow;
553 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300554 /*
555 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
556 * non-nested (L1) guest, it always points to vmcs01. For a nested
557 * guest (L2), it points to a different VMCS.
558 */
559 struct loaded_vmcs vmcs01;
560 struct loaded_vmcs *loaded_vmcs;
561 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300562 struct msr_autoload {
563 unsigned nr;
564 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
565 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
566 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400567 struct {
568 int loaded;
569 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300570#ifdef CONFIG_X86_64
571 u16 ds_sel, es_sel;
572#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200573 int gs_ldt_reload_needed;
574 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000575 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700576 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400577 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200578 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300579 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300580 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300581 struct kvm_segment segs[8];
582 } rmode;
583 struct {
584 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300585 struct kvm_save_segment {
586 u16 selector;
587 unsigned long base;
588 u32 limit;
589 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300590 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300591 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800592 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300593 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200594
595 /* Support for vnmi-less CPUs */
596 int soft_vnmi_blocked;
597 ktime_t entry_time;
598 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800599 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800600
Yang Zhang01e439b2013-04-11 19:25:12 +0800601 /* Posted interrupt descriptor */
602 struct pi_desc pi_desc;
603
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300604 /* Support for a guest hypervisor (nested VMX) */
605 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200606
607 /* Dynamic PLE window. */
608 int ple_window;
609 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800610
611 /* Support for PML */
612#define PML_ENTITY_NUM 512
613 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800614
Yunhong Jiang64672c92016-06-13 14:19:59 -0700615 /* apic deadline value in host tsc */
616 u64 hv_deadline_tsc;
617
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800618 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800619
620 bool guest_pkru_valid;
621 u32 guest_pkru;
622 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800623
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800624 /*
625 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
626 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
627 * in msr_ia32_feature_control_valid_bits.
628 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800629 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800630 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400631};
632
Avi Kivity2fb92db2011-04-27 19:42:18 +0300633enum segment_cache_field {
634 SEG_FIELD_SEL = 0,
635 SEG_FIELD_BASE = 1,
636 SEG_FIELD_LIMIT = 2,
637 SEG_FIELD_AR = 3,
638
639 SEG_FIELD_NR = 4
640};
641
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400642static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
643{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000644 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400645}
646
Feng Wuefc64402015-09-18 22:29:51 +0800647static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
648{
649 return &(to_vmx(vcpu)->pi_desc);
650}
651
Nadav Har'El22bd0352011-05-25 23:05:57 +0300652#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
653#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
654#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
655 [number##_HIGH] = VMCS12_OFFSET(name)+4
656
Abel Gordon4607c2d2013-04-18 14:35:55 +0300657
Bandan Dasfe2b2012014-04-21 15:20:14 -0400658static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300659 /*
660 * We do NOT shadow fields that are modified when L0
661 * traps and emulates any vmx instruction (e.g. VMPTRLD,
662 * VMXON...) executed by L1.
663 * For example, VM_INSTRUCTION_ERROR is read
664 * by L1 if a vmx instruction fails (part of the error path).
665 * Note the code assumes this logic. If for some reason
666 * we start shadowing these fields then we need to
667 * force a shadow sync when L0 emulates vmx instructions
668 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
669 * by nested_vmx_failValid)
670 */
671 VM_EXIT_REASON,
672 VM_EXIT_INTR_INFO,
673 VM_EXIT_INSTRUCTION_LEN,
674 IDT_VECTORING_INFO_FIELD,
675 IDT_VECTORING_ERROR_CODE,
676 VM_EXIT_INTR_ERROR_CODE,
677 EXIT_QUALIFICATION,
678 GUEST_LINEAR_ADDRESS,
679 GUEST_PHYSICAL_ADDRESS
680};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400681static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300682 ARRAY_SIZE(shadow_read_only_fields);
683
Bandan Dasfe2b2012014-04-21 15:20:14 -0400684static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800685 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300686 GUEST_RIP,
687 GUEST_RSP,
688 GUEST_CR0,
689 GUEST_CR3,
690 GUEST_CR4,
691 GUEST_INTERRUPTIBILITY_INFO,
692 GUEST_RFLAGS,
693 GUEST_CS_SELECTOR,
694 GUEST_CS_AR_BYTES,
695 GUEST_CS_LIMIT,
696 GUEST_CS_BASE,
697 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100698 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300699 CR0_GUEST_HOST_MASK,
700 CR0_READ_SHADOW,
701 CR4_READ_SHADOW,
702 TSC_OFFSET,
703 EXCEPTION_BITMAP,
704 CPU_BASED_VM_EXEC_CONTROL,
705 VM_ENTRY_EXCEPTION_ERROR_CODE,
706 VM_ENTRY_INTR_INFO_FIELD,
707 VM_ENTRY_INSTRUCTION_LEN,
708 VM_ENTRY_EXCEPTION_ERROR_CODE,
709 HOST_FS_BASE,
710 HOST_GS_BASE,
711 HOST_FS_SELECTOR,
712 HOST_GS_SELECTOR
713};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400714static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300715 ARRAY_SIZE(shadow_read_write_fields);
716
Mathias Krause772e0312012-08-30 01:30:19 +0200717static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300718 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800719 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300720 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
721 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
722 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
723 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
724 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
725 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
726 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
727 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800728 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300729 FIELD(HOST_ES_SELECTOR, host_es_selector),
730 FIELD(HOST_CS_SELECTOR, host_cs_selector),
731 FIELD(HOST_SS_SELECTOR, host_ss_selector),
732 FIELD(HOST_DS_SELECTOR, host_ds_selector),
733 FIELD(HOST_FS_SELECTOR, host_fs_selector),
734 FIELD(HOST_GS_SELECTOR, host_gs_selector),
735 FIELD(HOST_TR_SELECTOR, host_tr_selector),
736 FIELD64(IO_BITMAP_A, io_bitmap_a),
737 FIELD64(IO_BITMAP_B, io_bitmap_b),
738 FIELD64(MSR_BITMAP, msr_bitmap),
739 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
740 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
741 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
742 FIELD64(TSC_OFFSET, tsc_offset),
743 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
744 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800745 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300746 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800747 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
748 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
749 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
750 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800751 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300752 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
753 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
754 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
755 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
756 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
757 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
758 FIELD64(GUEST_PDPTR0, guest_pdptr0),
759 FIELD64(GUEST_PDPTR1, guest_pdptr1),
760 FIELD64(GUEST_PDPTR2, guest_pdptr2),
761 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100762 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300763 FIELD64(HOST_IA32_PAT, host_ia32_pat),
764 FIELD64(HOST_IA32_EFER, host_ia32_efer),
765 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
766 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
767 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
768 FIELD(EXCEPTION_BITMAP, exception_bitmap),
769 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
770 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
771 FIELD(CR3_TARGET_COUNT, cr3_target_count),
772 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
773 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
774 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
775 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
776 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
777 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
778 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
779 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
780 FIELD(TPR_THRESHOLD, tpr_threshold),
781 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
782 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
783 FIELD(VM_EXIT_REASON, vm_exit_reason),
784 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
785 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
786 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
787 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
788 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
789 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
790 FIELD(GUEST_ES_LIMIT, guest_es_limit),
791 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
792 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
793 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
794 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
795 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
796 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
797 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
798 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
799 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
800 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
801 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
802 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
803 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
804 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
805 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
806 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
807 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
808 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
809 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
810 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
811 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100812 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300813 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
814 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
815 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
816 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
817 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
818 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
819 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
820 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
821 FIELD(EXIT_QUALIFICATION, exit_qualification),
822 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
823 FIELD(GUEST_CR0, guest_cr0),
824 FIELD(GUEST_CR3, guest_cr3),
825 FIELD(GUEST_CR4, guest_cr4),
826 FIELD(GUEST_ES_BASE, guest_es_base),
827 FIELD(GUEST_CS_BASE, guest_cs_base),
828 FIELD(GUEST_SS_BASE, guest_ss_base),
829 FIELD(GUEST_DS_BASE, guest_ds_base),
830 FIELD(GUEST_FS_BASE, guest_fs_base),
831 FIELD(GUEST_GS_BASE, guest_gs_base),
832 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
833 FIELD(GUEST_TR_BASE, guest_tr_base),
834 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
835 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
836 FIELD(GUEST_DR7, guest_dr7),
837 FIELD(GUEST_RSP, guest_rsp),
838 FIELD(GUEST_RIP, guest_rip),
839 FIELD(GUEST_RFLAGS, guest_rflags),
840 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
841 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
842 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
843 FIELD(HOST_CR0, host_cr0),
844 FIELD(HOST_CR3, host_cr3),
845 FIELD(HOST_CR4, host_cr4),
846 FIELD(HOST_FS_BASE, host_fs_base),
847 FIELD(HOST_GS_BASE, host_gs_base),
848 FIELD(HOST_TR_BASE, host_tr_base),
849 FIELD(HOST_GDTR_BASE, host_gdtr_base),
850 FIELD(HOST_IDTR_BASE, host_idtr_base),
851 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
852 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
853 FIELD(HOST_RSP, host_rsp),
854 FIELD(HOST_RIP, host_rip),
855};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300856
857static inline short vmcs_field_to_offset(unsigned long field)
858{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100859 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
860
861 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
862 vmcs_field_to_offset_table[field] == 0)
863 return -ENOENT;
864
Nadav Har'El22bd0352011-05-25 23:05:57 +0300865 return vmcs_field_to_offset_table[field];
866}
867
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300868static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
869{
David Matlack4f2777b2016-07-13 17:16:37 -0700870 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300871}
872
873static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
874{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200875 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800876 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300877 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800878
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300879 return page;
880}
881
882static void nested_release_page(struct page *page)
883{
884 kvm_release_page_dirty(page);
885}
886
887static void nested_release_page_clean(struct page *page)
888{
889 kvm_release_page_clean(page);
890}
891
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300892static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800893static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800894static void kvm_cpu_vmxon(u64 addr);
895static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800896static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200897static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300898static void vmx_set_segment(struct kvm_vcpu *vcpu,
899 struct kvm_segment *var, int seg);
900static void vmx_get_segment(struct kvm_vcpu *vcpu,
901 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200902static bool guest_state_valid(struct kvm_vcpu *vcpu);
903static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300904static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300905static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800906static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300907
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908static DEFINE_PER_CPU(struct vmcs *, vmxarea);
909static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300910/*
911 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
912 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
913 */
914static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300915static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916
Feng Wubf9f6ac2015-09-18 22:29:55 +0800917/*
918 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
919 * can find which vCPU should be waken up.
920 */
921static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
922static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
923
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200924static unsigned long *vmx_io_bitmap_a;
925static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200926static unsigned long *vmx_msr_bitmap_legacy;
927static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800928static unsigned long *vmx_msr_bitmap_legacy_x2apic;
929static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300930static unsigned long *vmx_vmread_bitmap;
931static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300932
Avi Kivity110312c2010-12-21 12:54:20 +0200933static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200934static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200935
Sheng Yang2384d2b2008-01-17 15:14:33 +0800936static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
937static DEFINE_SPINLOCK(vmx_vpid_lock);
938
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300939static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940 int size;
941 int order;
942 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300943 u32 pin_based_exec_ctrl;
944 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800945 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300946 u32 vmexit_ctrl;
947 u32 vmentry_ctrl;
948} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949
Hannes Ederefff9e52008-11-28 17:02:06 +0100950static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800951 u32 ept;
952 u32 vpid;
953} vmx_capability;
954
Avi Kivity6aa8b732006-12-10 02:21:36 -0800955#define VMX_SEGMENT_FIELD(seg) \
956 [VCPU_SREG_##seg] = { \
957 .selector = GUEST_##seg##_SELECTOR, \
958 .base = GUEST_##seg##_BASE, \
959 .limit = GUEST_##seg##_LIMIT, \
960 .ar_bytes = GUEST_##seg##_AR_BYTES, \
961 }
962
Mathias Krause772e0312012-08-30 01:30:19 +0200963static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964 unsigned selector;
965 unsigned base;
966 unsigned limit;
967 unsigned ar_bytes;
968} kvm_vmx_segment_fields[] = {
969 VMX_SEGMENT_FIELD(CS),
970 VMX_SEGMENT_FIELD(DS),
971 VMX_SEGMENT_FIELD(ES),
972 VMX_SEGMENT_FIELD(FS),
973 VMX_SEGMENT_FIELD(GS),
974 VMX_SEGMENT_FIELD(SS),
975 VMX_SEGMENT_FIELD(TR),
976 VMX_SEGMENT_FIELD(LDTR),
977};
978
Avi Kivity26bb0982009-09-07 11:14:12 +0300979static u64 host_efer;
980
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300981static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
982
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300983/*
Brian Gerst8c065852010-07-17 09:03:26 -0400984 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300985 * away by decrementing the array size.
986 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800988#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300989 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400991 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993
Jan Kiszka5bb16012016-02-09 20:14:21 +0100994static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995{
996 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
997 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +0100998 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
999}
1000
Jan Kiszka6f054852016-02-09 20:15:18 +01001001static inline bool is_debug(u32 intr_info)
1002{
1003 return is_exception_n(intr_info, DB_VECTOR);
1004}
1005
1006static inline bool is_breakpoint(u32 intr_info)
1007{
1008 return is_exception_n(intr_info, BP_VECTOR);
1009}
1010
Jan Kiszka5bb16012016-02-09 20:14:21 +01001011static inline bool is_page_fault(u32 intr_info)
1012{
1013 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014}
1015
Gui Jianfeng31299942010-03-15 17:29:09 +08001016static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001017{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001018 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001019}
1020
Gui Jianfeng31299942010-03-15 17:29:09 +08001021static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001022{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001023 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001024}
1025
Gui Jianfeng31299942010-03-15 17:29:09 +08001026static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027{
1028 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1029 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1030}
1031
Gui Jianfeng31299942010-03-15 17:29:09 +08001032static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001033{
1034 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1035 INTR_INFO_VALID_MASK)) ==
1036 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1037}
1038
Gui Jianfeng31299942010-03-15 17:29:09 +08001039static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001040{
Sheng Yang04547152009-04-01 15:52:31 +08001041 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001042}
1043
Gui Jianfeng31299942010-03-15 17:29:09 +08001044static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001045{
Sheng Yang04547152009-04-01 15:52:31 +08001046 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001047}
1048
Paolo Bonzini35754c92015-07-29 12:05:37 +02001049static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001050{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001051 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001052}
1053
Gui Jianfeng31299942010-03-15 17:29:09 +08001054static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001055{
Sheng Yang04547152009-04-01 15:52:31 +08001056 return vmcs_config.cpu_based_exec_ctrl &
1057 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001058}
1059
Avi Kivity774ead32007-12-26 13:57:04 +02001060static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001061{
Sheng Yang04547152009-04-01 15:52:31 +08001062 return vmcs_config.cpu_based_2nd_exec_ctrl &
1063 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1064}
1065
Yang Zhang8d146952013-01-25 10:18:50 +08001066static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1067{
1068 return vmcs_config.cpu_based_2nd_exec_ctrl &
1069 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1070}
1071
Yang Zhang83d4c282013-01-25 10:18:49 +08001072static inline bool cpu_has_vmx_apic_register_virt(void)
1073{
1074 return vmcs_config.cpu_based_2nd_exec_ctrl &
1075 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1076}
1077
Yang Zhangc7c9c562013-01-25 10:18:51 +08001078static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1079{
1080 return vmcs_config.cpu_based_2nd_exec_ctrl &
1081 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1082}
1083
Yunhong Jiang64672c92016-06-13 14:19:59 -07001084/*
1085 * Comment's format: document - errata name - stepping - processor name.
1086 * Refer from
1087 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1088 */
1089static u32 vmx_preemption_cpu_tfms[] = {
1090/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
10910x000206E6,
1092/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1093/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1094/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
10950x00020652,
1096/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
10970x00020655,
1098/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1099/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1100/*
1101 * 320767.pdf - AAP86 - B1 -
1102 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1103 */
11040x000106E5,
1105/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11060x000106A0,
1107/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11080x000106A1,
1109/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11100x000106A4,
1111 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1112 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1113 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11140x000106A5,
1115};
1116
1117static inline bool cpu_has_broken_vmx_preemption_timer(void)
1118{
1119 u32 eax = cpuid_eax(0x00000001), i;
1120
1121 /* Clear the reserved bits */
1122 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001123 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001124 if (eax == vmx_preemption_cpu_tfms[i])
1125 return true;
1126
1127 return false;
1128}
1129
1130static inline bool cpu_has_vmx_preemption_timer(void)
1131{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001132 return vmcs_config.pin_based_exec_ctrl &
1133 PIN_BASED_VMX_PREEMPTION_TIMER;
1134}
1135
Yang Zhang01e439b2013-04-11 19:25:12 +08001136static inline bool cpu_has_vmx_posted_intr(void)
1137{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001138 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1139 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001140}
1141
1142static inline bool cpu_has_vmx_apicv(void)
1143{
1144 return cpu_has_vmx_apic_register_virt() &&
1145 cpu_has_vmx_virtual_intr_delivery() &&
1146 cpu_has_vmx_posted_intr();
1147}
1148
Sheng Yang04547152009-04-01 15:52:31 +08001149static inline bool cpu_has_vmx_flexpriority(void)
1150{
1151 return cpu_has_vmx_tpr_shadow() &&
1152 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001153}
1154
Marcelo Tosattie7997942009-06-11 12:07:40 -03001155static inline bool cpu_has_vmx_ept_execute_only(void)
1156{
Gui Jianfeng31299942010-03-15 17:29:09 +08001157 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001158}
1159
Marcelo Tosattie7997942009-06-11 12:07:40 -03001160static inline bool cpu_has_vmx_ept_2m_page(void)
1161{
Gui Jianfeng31299942010-03-15 17:29:09 +08001162 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001163}
1164
Sheng Yang878403b2010-01-05 19:02:29 +08001165static inline bool cpu_has_vmx_ept_1g_page(void)
1166{
Gui Jianfeng31299942010-03-15 17:29:09 +08001167 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001168}
1169
Sheng Yang4bc9b982010-06-02 14:05:24 +08001170static inline bool cpu_has_vmx_ept_4levels(void)
1171{
1172 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1173}
1174
Xudong Hao83c3a332012-05-28 19:33:35 +08001175static inline bool cpu_has_vmx_ept_ad_bits(void)
1176{
1177 return vmx_capability.ept & VMX_EPT_AD_BIT;
1178}
1179
Gui Jianfeng31299942010-03-15 17:29:09 +08001180static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001181{
Gui Jianfeng31299942010-03-15 17:29:09 +08001182 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001183}
1184
Gui Jianfeng31299942010-03-15 17:29:09 +08001185static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001186{
Gui Jianfeng31299942010-03-15 17:29:09 +08001187 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001188}
1189
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001190static inline bool cpu_has_vmx_invvpid_single(void)
1191{
1192 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1193}
1194
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001195static inline bool cpu_has_vmx_invvpid_global(void)
1196{
1197 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1198}
1199
Gui Jianfeng31299942010-03-15 17:29:09 +08001200static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001201{
Sheng Yang04547152009-04-01 15:52:31 +08001202 return vmcs_config.cpu_based_2nd_exec_ctrl &
1203 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001204}
1205
Gui Jianfeng31299942010-03-15 17:29:09 +08001206static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001207{
1208 return vmcs_config.cpu_based_2nd_exec_ctrl &
1209 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1210}
1211
Gui Jianfeng31299942010-03-15 17:29:09 +08001212static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001213{
1214 return vmcs_config.cpu_based_2nd_exec_ctrl &
1215 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1216}
1217
Paolo Bonzini35754c92015-07-29 12:05:37 +02001218static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001219{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001220 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001221}
1222
Gui Jianfeng31299942010-03-15 17:29:09 +08001223static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001224{
Sheng Yang04547152009-04-01 15:52:31 +08001225 return vmcs_config.cpu_based_2nd_exec_ctrl &
1226 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001227}
1228
Gui Jianfeng31299942010-03-15 17:29:09 +08001229static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001230{
1231 return vmcs_config.cpu_based_2nd_exec_ctrl &
1232 SECONDARY_EXEC_RDTSCP;
1233}
1234
Mao, Junjiead756a12012-07-02 01:18:48 +00001235static inline bool cpu_has_vmx_invpcid(void)
1236{
1237 return vmcs_config.cpu_based_2nd_exec_ctrl &
1238 SECONDARY_EXEC_ENABLE_INVPCID;
1239}
1240
Gui Jianfeng31299942010-03-15 17:29:09 +08001241static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001242{
1243 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1244}
1245
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001246static inline bool cpu_has_vmx_wbinvd_exit(void)
1247{
1248 return vmcs_config.cpu_based_2nd_exec_ctrl &
1249 SECONDARY_EXEC_WBINVD_EXITING;
1250}
1251
Abel Gordonabc4fc52013-04-18 14:35:25 +03001252static inline bool cpu_has_vmx_shadow_vmcs(void)
1253{
1254 u64 vmx_msr;
1255 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1256 /* check if the cpu supports writing r/o exit information fields */
1257 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1258 return false;
1259
1260 return vmcs_config.cpu_based_2nd_exec_ctrl &
1261 SECONDARY_EXEC_SHADOW_VMCS;
1262}
1263
Kai Huang843e4332015-01-28 10:54:28 +08001264static inline bool cpu_has_vmx_pml(void)
1265{
1266 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1267}
1268
Haozhong Zhang64903d62015-10-20 15:39:09 +08001269static inline bool cpu_has_vmx_tsc_scaling(void)
1270{
1271 return vmcs_config.cpu_based_2nd_exec_ctrl &
1272 SECONDARY_EXEC_TSC_SCALING;
1273}
1274
Sheng Yang04547152009-04-01 15:52:31 +08001275static inline bool report_flexpriority(void)
1276{
1277 return flexpriority_enabled;
1278}
1279
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001280static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1281{
1282 return vmcs12->cpu_based_vm_exec_control & bit;
1283}
1284
1285static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1286{
1287 return (vmcs12->cpu_based_vm_exec_control &
1288 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1289 (vmcs12->secondary_vm_exec_control & bit);
1290}
1291
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001292static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001293{
1294 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1295}
1296
Jan Kiszkaf4124502014-03-07 20:03:13 +01001297static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1298{
1299 return vmcs12->pin_based_vm_exec_control &
1300 PIN_BASED_VMX_PREEMPTION_TIMER;
1301}
1302
Nadav Har'El155a97a2013-08-05 11:07:16 +03001303static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1304{
1305 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1306}
1307
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001308static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1309{
1310 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1311 vmx_xsaves_supported();
1312}
1313
Wincy Vanf2b93282015-02-03 23:56:03 +08001314static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1315{
1316 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1317}
1318
Wanpeng Li5c614b32015-10-13 09:18:36 -07001319static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1320{
1321 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1322}
1323
Wincy Van82f0dd42015-02-03 23:57:18 +08001324static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1325{
1326 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1327}
1328
Wincy Van608406e2015-02-03 23:57:51 +08001329static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1330{
1331 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1332}
1333
Wincy Van705699a2015-02-03 23:58:17 +08001334static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1335{
1336 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1337}
1338
Nadav Har'El644d7112011-05-25 23:12:35 +03001339static inline bool is_exception(u32 intr_info)
1340{
1341 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1342 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1343}
1344
Jan Kiszka533558b2014-01-04 18:47:20 +01001345static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1346 u32 exit_intr_info,
1347 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001348static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1349 struct vmcs12 *vmcs12,
1350 u32 reason, unsigned long qualification);
1351
Rusty Russell8b9cf982007-07-30 16:31:43 +10001352static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001353{
1354 int i;
1355
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001356 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001357 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001358 return i;
1359 return -1;
1360}
1361
Sheng Yang2384d2b2008-01-17 15:14:33 +08001362static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1363{
1364 struct {
1365 u64 vpid : 16;
1366 u64 rsvd : 48;
1367 u64 gva;
1368 } operand = { vpid, 0, gva };
1369
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001370 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001371 /* CF==1 or ZF==1 --> rc = -1 */
1372 "; ja 1f ; ud2 ; 1:"
1373 : : "a"(&operand), "c"(ext) : "cc", "memory");
1374}
1375
Sheng Yang14394422008-04-28 12:24:45 +08001376static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1377{
1378 struct {
1379 u64 eptp, gpa;
1380 } operand = {eptp, gpa};
1381
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001382 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001383 /* CF==1 or ZF==1 --> rc = -1 */
1384 "; ja 1f ; ud2 ; 1:\n"
1385 : : "a" (&operand), "c" (ext) : "cc", "memory");
1386}
1387
Avi Kivity26bb0982009-09-07 11:14:12 +03001388static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001389{
1390 int i;
1391
Rusty Russell8b9cf982007-07-30 16:31:43 +10001392 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001393 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001394 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001395 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001396}
1397
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398static void vmcs_clear(struct vmcs *vmcs)
1399{
1400 u64 phys_addr = __pa(vmcs);
1401 u8 error;
1402
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001403 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001404 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 : "cc", "memory");
1406 if (error)
1407 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1408 vmcs, phys_addr);
1409}
1410
Nadav Har'Eld462b812011-05-24 15:26:10 +03001411static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1412{
1413 vmcs_clear(loaded_vmcs->vmcs);
1414 loaded_vmcs->cpu = -1;
1415 loaded_vmcs->launched = 0;
1416}
1417
Dongxiao Xu7725b892010-05-11 18:29:38 +08001418static void vmcs_load(struct vmcs *vmcs)
1419{
1420 u64 phys_addr = __pa(vmcs);
1421 u8 error;
1422
1423 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001424 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001425 : "cc", "memory");
1426 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001427 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001428 vmcs, phys_addr);
1429}
1430
Dave Young2965faa2015-09-09 15:38:55 -07001431#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001432/*
1433 * This bitmap is used to indicate whether the vmclear
1434 * operation is enabled on all cpus. All disabled by
1435 * default.
1436 */
1437static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1438
1439static inline void crash_enable_local_vmclear(int cpu)
1440{
1441 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1442}
1443
1444static inline void crash_disable_local_vmclear(int cpu)
1445{
1446 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1447}
1448
1449static inline int crash_local_vmclear_enabled(int cpu)
1450{
1451 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1452}
1453
1454static void crash_vmclear_local_loaded_vmcss(void)
1455{
1456 int cpu = raw_smp_processor_id();
1457 struct loaded_vmcs *v;
1458
1459 if (!crash_local_vmclear_enabled(cpu))
1460 return;
1461
1462 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1463 loaded_vmcss_on_cpu_link)
1464 vmcs_clear(v->vmcs);
1465}
1466#else
1467static inline void crash_enable_local_vmclear(int cpu) { }
1468static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001469#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001470
Nadav Har'Eld462b812011-05-24 15:26:10 +03001471static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001473 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001474 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475
Nadav Har'Eld462b812011-05-24 15:26:10 +03001476 if (loaded_vmcs->cpu != cpu)
1477 return; /* vcpu migration can race with cpu offline */
1478 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001480 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001481 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001482
1483 /*
1484 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1485 * is before setting loaded_vmcs->vcpu to -1 which is done in
1486 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1487 * then adds the vmcs into percpu list before it is deleted.
1488 */
1489 smp_wmb();
1490
Nadav Har'Eld462b812011-05-24 15:26:10 +03001491 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001492 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493}
1494
Nadav Har'Eld462b812011-05-24 15:26:10 +03001495static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001496{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001497 int cpu = loaded_vmcs->cpu;
1498
1499 if (cpu != -1)
1500 smp_call_function_single(cpu,
1501 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001502}
1503
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001504static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001505{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001506 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001507 return;
1508
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001509 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001510 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001511}
1512
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001513static inline void vpid_sync_vcpu_global(void)
1514{
1515 if (cpu_has_vmx_invvpid_global())
1516 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1517}
1518
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001519static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001520{
1521 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001522 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001523 else
1524 vpid_sync_vcpu_global();
1525}
1526
Sheng Yang14394422008-04-28 12:24:45 +08001527static inline void ept_sync_global(void)
1528{
1529 if (cpu_has_vmx_invept_global())
1530 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1531}
1532
1533static inline void ept_sync_context(u64 eptp)
1534{
Avi Kivity089d0342009-03-23 18:26:32 +02001535 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001536 if (cpu_has_vmx_invept_context())
1537 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1538 else
1539 ept_sync_global();
1540 }
1541}
1542
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001543static __always_inline void vmcs_check16(unsigned long field)
1544{
1545 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1546 "16-bit accessor invalid for 64-bit field");
1547 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1548 "16-bit accessor invalid for 64-bit high field");
1549 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1550 "16-bit accessor invalid for 32-bit high field");
1551 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1552 "16-bit accessor invalid for natural width field");
1553}
1554
1555static __always_inline void vmcs_check32(unsigned long field)
1556{
1557 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1558 "32-bit accessor invalid for 16-bit field");
1559 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1560 "32-bit accessor invalid for natural width field");
1561}
1562
1563static __always_inline void vmcs_check64(unsigned long field)
1564{
1565 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1566 "64-bit accessor invalid for 16-bit field");
1567 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1568 "64-bit accessor invalid for 64-bit high field");
1569 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1570 "64-bit accessor invalid for 32-bit field");
1571 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1572 "64-bit accessor invalid for natural width field");
1573}
1574
1575static __always_inline void vmcs_checkl(unsigned long field)
1576{
1577 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1578 "Natural width accessor invalid for 16-bit field");
1579 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1580 "Natural width accessor invalid for 64-bit field");
1581 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1582 "Natural width accessor invalid for 64-bit high field");
1583 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1584 "Natural width accessor invalid for 32-bit field");
1585}
1586
1587static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588{
Avi Kivity5e520e62011-05-15 10:13:12 -04001589 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001590
Avi Kivity5e520e62011-05-15 10:13:12 -04001591 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1592 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 return value;
1594}
1595
Avi Kivity96304212011-05-15 10:13:13 -04001596static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001598 vmcs_check16(field);
1599 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600}
1601
Avi Kivity96304212011-05-15 10:13:13 -04001602static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001604 vmcs_check32(field);
1605 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606}
1607
Avi Kivity96304212011-05-15 10:13:13 -04001608static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001609{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001610 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001611#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001612 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001614 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615#endif
1616}
1617
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001618static __always_inline unsigned long vmcs_readl(unsigned long field)
1619{
1620 vmcs_checkl(field);
1621 return __vmcs_readl(field);
1622}
1623
Avi Kivitye52de1b2007-01-05 16:36:56 -08001624static noinline void vmwrite_error(unsigned long field, unsigned long value)
1625{
1626 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1627 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1628 dump_stack();
1629}
1630
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001631static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632{
1633 u8 error;
1634
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001635 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001636 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001637 if (unlikely(error))
1638 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639}
1640
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001641static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001643 vmcs_check16(field);
1644 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001645}
1646
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001647static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001649 vmcs_check32(field);
1650 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001651}
1652
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001653static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001654{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001655 vmcs_check64(field);
1656 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001657#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001659 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001660#endif
1661}
1662
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001663static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001664{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001665 vmcs_checkl(field);
1666 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001667}
1668
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001669static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001670{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001671 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1672 "vmcs_clear_bits does not support 64-bit fields");
1673 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1674}
1675
1676static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1677{
1678 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1679 "vmcs_set_bits does not support 64-bit fields");
1680 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001681}
1682
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001683static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1684{
1685 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1686}
1687
Gleb Natapov2961e8762013-11-25 15:37:13 +02001688static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1689{
1690 vmcs_write32(VM_ENTRY_CONTROLS, val);
1691 vmx->vm_entry_controls_shadow = val;
1692}
1693
1694static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1695{
1696 if (vmx->vm_entry_controls_shadow != val)
1697 vm_entry_controls_init(vmx, val);
1698}
1699
1700static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1701{
1702 return vmx->vm_entry_controls_shadow;
1703}
1704
1705
1706static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1707{
1708 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1709}
1710
1711static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1712{
1713 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1714}
1715
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001716static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1717{
1718 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1719}
1720
Gleb Natapov2961e8762013-11-25 15:37:13 +02001721static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1722{
1723 vmcs_write32(VM_EXIT_CONTROLS, val);
1724 vmx->vm_exit_controls_shadow = val;
1725}
1726
1727static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1728{
1729 if (vmx->vm_exit_controls_shadow != val)
1730 vm_exit_controls_init(vmx, val);
1731}
1732
1733static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1734{
1735 return vmx->vm_exit_controls_shadow;
1736}
1737
1738
1739static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1740{
1741 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1742}
1743
1744static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1745{
1746 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1747}
1748
Avi Kivity2fb92db2011-04-27 19:42:18 +03001749static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1750{
1751 vmx->segment_cache.bitmask = 0;
1752}
1753
1754static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1755 unsigned field)
1756{
1757 bool ret;
1758 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1759
1760 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1761 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1762 vmx->segment_cache.bitmask = 0;
1763 }
1764 ret = vmx->segment_cache.bitmask & mask;
1765 vmx->segment_cache.bitmask |= mask;
1766 return ret;
1767}
1768
1769static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1770{
1771 u16 *p = &vmx->segment_cache.seg[seg].selector;
1772
1773 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1774 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1775 return *p;
1776}
1777
1778static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1779{
1780 ulong *p = &vmx->segment_cache.seg[seg].base;
1781
1782 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1783 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1784 return *p;
1785}
1786
1787static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1788{
1789 u32 *p = &vmx->segment_cache.seg[seg].limit;
1790
1791 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1792 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1793 return *p;
1794}
1795
1796static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1797{
1798 u32 *p = &vmx->segment_cache.seg[seg].ar;
1799
1800 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1801 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1802 return *p;
1803}
1804
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001805static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1806{
1807 u32 eb;
1808
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001809 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01001810 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001811 if ((vcpu->guest_debug &
1812 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1813 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1814 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001815 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001816 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001817 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001818 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001819 if (vcpu->fpu_active)
1820 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001821
1822 /* When we are running a nested L2 guest and L1 specified for it a
1823 * certain exception bitmap, we must trap the same exceptions and pass
1824 * them to L1. When running L2, we will only handle the exceptions
1825 * specified above if L1 did not want them.
1826 */
1827 if (is_guest_mode(vcpu))
1828 eb |= get_vmcs12(vcpu)->exception_bitmap;
1829
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001830 vmcs_write32(EXCEPTION_BITMAP, eb);
1831}
1832
Gleb Natapov2961e8762013-11-25 15:37:13 +02001833static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1834 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001835{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001836 vm_entry_controls_clearbit(vmx, entry);
1837 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001838}
1839
Avi Kivity61d2ef22010-04-28 16:40:38 +03001840static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1841{
1842 unsigned i;
1843 struct msr_autoload *m = &vmx->msr_autoload;
1844
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001845 switch (msr) {
1846 case MSR_EFER:
1847 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001848 clear_atomic_switch_msr_special(vmx,
1849 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001850 VM_EXIT_LOAD_IA32_EFER);
1851 return;
1852 }
1853 break;
1854 case MSR_CORE_PERF_GLOBAL_CTRL:
1855 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001856 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001857 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1858 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1859 return;
1860 }
1861 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001862 }
1863
Avi Kivity61d2ef22010-04-28 16:40:38 +03001864 for (i = 0; i < m->nr; ++i)
1865 if (m->guest[i].index == msr)
1866 break;
1867
1868 if (i == m->nr)
1869 return;
1870 --m->nr;
1871 m->guest[i] = m->guest[m->nr];
1872 m->host[i] = m->host[m->nr];
1873 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1874 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1875}
1876
Gleb Natapov2961e8762013-11-25 15:37:13 +02001877static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1878 unsigned long entry, unsigned long exit,
1879 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1880 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001881{
1882 vmcs_write64(guest_val_vmcs, guest_val);
1883 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001884 vm_entry_controls_setbit(vmx, entry);
1885 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001886}
1887
Avi Kivity61d2ef22010-04-28 16:40:38 +03001888static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1889 u64 guest_val, u64 host_val)
1890{
1891 unsigned i;
1892 struct msr_autoload *m = &vmx->msr_autoload;
1893
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001894 switch (msr) {
1895 case MSR_EFER:
1896 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001897 add_atomic_switch_msr_special(vmx,
1898 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001899 VM_EXIT_LOAD_IA32_EFER,
1900 GUEST_IA32_EFER,
1901 HOST_IA32_EFER,
1902 guest_val, host_val);
1903 return;
1904 }
1905 break;
1906 case MSR_CORE_PERF_GLOBAL_CTRL:
1907 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001908 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001909 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1910 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1911 GUEST_IA32_PERF_GLOBAL_CTRL,
1912 HOST_IA32_PERF_GLOBAL_CTRL,
1913 guest_val, host_val);
1914 return;
1915 }
1916 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001917 case MSR_IA32_PEBS_ENABLE:
1918 /* PEBS needs a quiescent period after being disabled (to write
1919 * a record). Disabling PEBS through VMX MSR swapping doesn't
1920 * provide that period, so a CPU could write host's record into
1921 * guest's memory.
1922 */
1923 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001924 }
1925
Avi Kivity61d2ef22010-04-28 16:40:38 +03001926 for (i = 0; i < m->nr; ++i)
1927 if (m->guest[i].index == msr)
1928 break;
1929
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001930 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001931 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001932 "Can't add msr %x\n", msr);
1933 return;
1934 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001935 ++m->nr;
1936 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1937 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1938 }
1939
1940 m->guest[i].index = msr;
1941 m->guest[i].value = guest_val;
1942 m->host[i].index = msr;
1943 m->host[i].value = host_val;
1944}
1945
Avi Kivity33ed6322007-05-02 16:54:03 +03001946static void reload_tss(void)
1947{
Avi Kivity33ed6322007-05-02 16:54:03 +03001948 /*
1949 * VT restores TR but not its size. Useless.
1950 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001951 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001952 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001953
Avi Kivityd3591922010-07-26 18:32:39 +03001954 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001955 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1956 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001957}
1958
Avi Kivity92c0d902009-10-29 11:00:16 +02001959static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001960{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001961 u64 guest_efer = vmx->vcpu.arch.efer;
1962 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03001963
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001964 if (!enable_ept) {
1965 /*
1966 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1967 * host CPUID is more efficient than testing guest CPUID
1968 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
1969 */
1970 if (boot_cpu_has(X86_FEATURE_SMEP))
1971 guest_efer |= EFER_NX;
1972 else if (!(guest_efer & EFER_NX))
1973 ignore_bits |= EFER_NX;
1974 }
Roel Kluin3a34a882009-08-04 02:08:45 -07001975
Avi Kivity51c6cf62007-08-29 03:48:05 +03001976 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001977 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03001978 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001979 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001980#ifdef CONFIG_X86_64
1981 ignore_bits |= EFER_LMA | EFER_LME;
1982 /* SCE is meaningful only in long mode on Intel */
1983 if (guest_efer & EFER_LMA)
1984 ignore_bits &= ~(u64)EFER_SCE;
1985#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03001986
1987 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001988
1989 /*
1990 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1991 * On CPUs that support "load IA32_EFER", always switch EFER
1992 * atomically, since it's faster than switching it manually.
1993 */
1994 if (cpu_has_load_ia32_efer ||
1995 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001996 if (!(guest_efer & EFER_LMA))
1997 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001998 if (guest_efer != host_efer)
1999 add_atomic_switch_msr(vmx, MSR_EFER,
2000 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002001 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002002 } else {
2003 guest_efer &= ~ignore_bits;
2004 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002005
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002006 vmx->guest_msrs[efer_offset].data = guest_efer;
2007 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2008
2009 return true;
2010 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002011}
2012
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002013static unsigned long segment_base(u16 selector)
2014{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002015 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002016 struct desc_struct *d;
2017 unsigned long table_base;
2018 unsigned long v;
2019
2020 if (!(selector & ~3))
2021 return 0;
2022
Avi Kivityd3591922010-07-26 18:32:39 +03002023 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002024
2025 if (selector & 4) { /* from ldt */
2026 u16 ldt_selector = kvm_read_ldt();
2027
2028 if (!(ldt_selector & ~3))
2029 return 0;
2030
2031 table_base = segment_base(ldt_selector);
2032 }
2033 d = (struct desc_struct *)(table_base + (selector & ~7));
2034 v = get_desc_base(d);
2035#ifdef CONFIG_X86_64
2036 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2037 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2038#endif
2039 return v;
2040}
2041
2042static inline unsigned long kvm_read_tr_base(void)
2043{
2044 u16 tr;
2045 asm("str %0" : "=g"(tr));
2046 return segment_base(tr);
2047}
2048
Avi Kivity04d2cc72007-09-10 18:10:54 +03002049static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002050{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002051 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002052 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002053
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002054 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002055 return;
2056
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002057 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002058 /*
2059 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2060 * allow segment selectors with cpl > 0 or ti == 1.
2061 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002062 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002063 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002064 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002065 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002066 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002067 vmx->host_state.fs_reload_needed = 0;
2068 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002069 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002070 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002071 }
Avi Kivity9581d442010-10-19 16:46:55 +02002072 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002073 if (!(vmx->host_state.gs_sel & 7))
2074 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002075 else {
2076 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002077 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002078 }
2079
2080#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002081 savesegment(ds, vmx->host_state.ds_sel);
2082 savesegment(es, vmx->host_state.es_sel);
2083#endif
2084
2085#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002086 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2087 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2088#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002089 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2090 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002091#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002092
2093#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002094 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2095 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002096 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002097#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002098 if (boot_cpu_has(X86_FEATURE_MPX))
2099 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002100 for (i = 0; i < vmx->save_nmsrs; ++i)
2101 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002102 vmx->guest_msrs[i].data,
2103 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002104}
2105
Avi Kivitya9b21b62008-06-24 11:48:49 +03002106static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002107{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002108 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002109 return;
2110
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002111 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002112 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002113#ifdef CONFIG_X86_64
2114 if (is_long_mode(&vmx->vcpu))
2115 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2116#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002117 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002118 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002119#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002120 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002121#else
2122 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002123#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002124 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002125 if (vmx->host_state.fs_reload_needed)
2126 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002127#ifdef CONFIG_X86_64
2128 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2129 loadsegment(ds, vmx->host_state.ds_sel);
2130 loadsegment(es, vmx->host_state.es_sel);
2131 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002132#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002133 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002134#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002135 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002136#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002137 if (vmx->host_state.msr_host_bndcfgs)
2138 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002139 /*
2140 * If the FPU is not active (through the host task or
2141 * the guest vcpu), then restore the cr0.TS bit.
2142 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002143 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002144 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002145 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002146}
2147
Avi Kivitya9b21b62008-06-24 11:48:49 +03002148static void vmx_load_host_state(struct vcpu_vmx *vmx)
2149{
2150 preempt_disable();
2151 __vmx_load_host_state(vmx);
2152 preempt_enable();
2153}
2154
Feng Wu28b835d2015-09-18 22:29:54 +08002155static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2156{
2157 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2158 struct pi_desc old, new;
2159 unsigned int dest;
2160
2161 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002162 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2163 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002164 return;
2165
2166 do {
2167 old.control = new.control = pi_desc->control;
2168
2169 /*
2170 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
2171 * are two possible cases:
2172 * 1. After running 'pre_block', context switch
2173 * happened. For this case, 'sn' was set in
2174 * vmx_vcpu_put(), so we need to clear it here.
2175 * 2. After running 'pre_block', we were blocked,
2176 * and woken up by some other guy. For this case,
2177 * we don't need to do anything, 'pi_post_block'
2178 * will do everything for us. However, we cannot
2179 * check whether it is case #1 or case #2 here
2180 * (maybe, not needed), so we also clear sn here,
2181 * I think it is not a big deal.
2182 */
2183 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
2184 if (vcpu->cpu != cpu) {
2185 dest = cpu_physical_id(cpu);
2186
2187 if (x2apic_enabled())
2188 new.ndst = dest;
2189 else
2190 new.ndst = (dest << 8) & 0xFF00;
2191 }
2192
2193 /* set 'NV' to 'notification vector' */
2194 new.nv = POSTED_INTR_VECTOR;
2195 }
2196
2197 /* Allow posting non-urgent interrupts */
2198 new.sn = 0;
2199 } while (cmpxchg(&pi_desc->control, old.control,
2200 new.control) != old.control);
2201}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002202
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203/*
2204 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2205 * vcpu mutex is already taken.
2206 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002207static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002209 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002210 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002211 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002212
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002213 if (!vmm_exclusive)
2214 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002215 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002216 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002217
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002218 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002219 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002220 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002221
2222 /*
2223 * Read loaded_vmcs->cpu should be before fetching
2224 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2225 * See the comments in __loaded_vmcs_clear().
2226 */
2227 smp_rmb();
2228
Nadav Har'Eld462b812011-05-24 15:26:10 +03002229 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2230 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002231 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002232 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002233 }
2234
2235 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2236 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2237 vmcs_load(vmx->loaded_vmcs->vmcs);
2238 }
2239
2240 if (!already_loaded) {
2241 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2242 unsigned long sysenter_esp;
2243
2244 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002245
Avi Kivity6aa8b732006-12-10 02:21:36 -08002246 /*
2247 * Linux uses per-cpu TSS and GDT, so set these when switching
2248 * processors.
2249 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002250 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002251 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252
2253 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2254 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002255
Nadav Har'Eld462b812011-05-24 15:26:10 +03002256 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 }
Feng Wu28b835d2015-09-18 22:29:54 +08002258
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002259 /* Setup TSC multiplier */
2260 if (kvm_has_tsc_control &&
2261 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) {
2262 vmx->current_tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2263 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2264 }
2265
Feng Wu28b835d2015-09-18 22:29:54 +08002266 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002267 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002268}
2269
2270static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2271{
2272 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2273
2274 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002275 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2276 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002277 return;
2278
2279 /* Set SN when the vCPU is preempted */
2280 if (vcpu->preempted)
2281 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282}
2283
2284static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2285{
Feng Wu28b835d2015-09-18 22:29:54 +08002286 vmx_vcpu_pi_put(vcpu);
2287
Avi Kivitya9b21b62008-06-24 11:48:49 +03002288 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002289 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002290 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2291 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002292 kvm_cpu_vmxoff();
2293 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294}
2295
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002296static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2297{
Avi Kivity81231c62010-01-24 16:26:40 +02002298 ulong cr0;
2299
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002300 if (vcpu->fpu_active)
2301 return;
2302 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002303 cr0 = vmcs_readl(GUEST_CR0);
2304 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2305 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2306 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002307 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002308 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002309 if (is_guest_mode(vcpu))
2310 vcpu->arch.cr0_guest_owned_bits &=
2311 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002312 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002313}
2314
Avi Kivityedcafe32009-12-30 18:07:40 +02002315static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2316
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002317/*
2318 * Return the cr0 value that a nested guest would read. This is a combination
2319 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2320 * its hypervisor (cr0_read_shadow).
2321 */
2322static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2323{
2324 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2325 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2326}
2327static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2328{
2329 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2330 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2331}
2332
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002333static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2334{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002335 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2336 * set this *before* calling this function.
2337 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002338 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002339 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002340 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002341 vcpu->arch.cr0_guest_owned_bits = 0;
2342 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002343 if (is_guest_mode(vcpu)) {
2344 /*
2345 * L1's specified read shadow might not contain the TS bit,
2346 * so now that we turned on shadowing of this bit, we need to
2347 * set this bit of the shadow. Like in nested_vmx_run we need
2348 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2349 * up-to-date here because we just decached cr0.TS (and we'll
2350 * only update vmcs12->guest_cr0 on nested exit).
2351 */
2352 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2353 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2354 (vcpu->arch.cr0 & X86_CR0_TS);
2355 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2356 } else
2357 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002358}
2359
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2361{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002362 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002363
Avi Kivity6de12732011-03-07 12:51:22 +02002364 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2365 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2366 rflags = vmcs_readl(GUEST_RFLAGS);
2367 if (to_vmx(vcpu)->rmode.vm86_active) {
2368 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2369 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2370 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2371 }
2372 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002373 }
Avi Kivity6de12732011-03-07 12:51:22 +02002374 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375}
2376
2377static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2378{
Avi Kivity6de12732011-03-07 12:51:22 +02002379 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2380 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002381 if (to_vmx(vcpu)->rmode.vm86_active) {
2382 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002383 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002384 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 vmcs_writel(GUEST_RFLAGS, rflags);
2386}
2387
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002388static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2389{
2390 return to_vmx(vcpu)->guest_pkru;
2391}
2392
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002393static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002394{
2395 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2396 int ret = 0;
2397
2398 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002399 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002400 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002401 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002402
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002403 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002404}
2405
2406static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2407{
2408 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2409 u32 interruptibility = interruptibility_old;
2410
2411 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2412
Jan Kiszka48005f62010-02-19 19:38:07 +01002413 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002414 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002415 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002416 interruptibility |= GUEST_INTR_STATE_STI;
2417
2418 if ((interruptibility != interruptibility_old))
2419 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2420}
2421
Avi Kivity6aa8b732006-12-10 02:21:36 -08002422static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2423{
2424 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002426 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002428 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429
Glauber Costa2809f5d2009-05-12 16:21:05 -04002430 /* skipping an emulated instruction also counts */
2431 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432}
2433
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002434/*
2435 * KVM wants to inject page-faults which it got to the guest. This function
2436 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002437 */
Gleb Natapove011c662013-09-25 12:51:35 +03002438static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002439{
2440 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2441
Gleb Natapove011c662013-09-25 12:51:35 +03002442 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002443 return 0;
2444
Jan Kiszka533558b2014-01-04 18:47:20 +01002445 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2446 vmcs_read32(VM_EXIT_INTR_INFO),
2447 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002448 return 1;
2449}
2450
Avi Kivity298101d2007-11-25 13:41:11 +02002451static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002452 bool has_error_code, u32 error_code,
2453 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002454{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002455 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002456 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002457
Gleb Natapove011c662013-09-25 12:51:35 +03002458 if (!reinject && is_guest_mode(vcpu) &&
2459 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002460 return;
2461
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002462 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002463 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002464 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2465 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002466
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002467 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002468 int inc_eip = 0;
2469 if (kvm_exception_is_soft(nr))
2470 inc_eip = vcpu->arch.event_exit_inst_len;
2471 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002472 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002473 return;
2474 }
2475
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002476 if (kvm_exception_is_soft(nr)) {
2477 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2478 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002479 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2480 } else
2481 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2482
2483 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002484}
2485
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002486static bool vmx_rdtscp_supported(void)
2487{
2488 return cpu_has_vmx_rdtscp();
2489}
2490
Mao, Junjiead756a12012-07-02 01:18:48 +00002491static bool vmx_invpcid_supported(void)
2492{
2493 return cpu_has_vmx_invpcid() && enable_ept;
2494}
2495
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496/*
Eddie Donga75beee2007-05-17 18:55:15 +03002497 * Swap MSR entry in host/guest MSR entry array.
2498 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002499static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002500{
Avi Kivity26bb0982009-09-07 11:14:12 +03002501 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002502
2503 tmp = vmx->guest_msrs[to];
2504 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2505 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002506}
2507
Yang Zhang8d146952013-01-25 10:18:50 +08002508static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2509{
2510 unsigned long *msr_bitmap;
2511
Wincy Van670125b2015-03-04 14:31:56 +08002512 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002513 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002514 else if (cpu_has_secondary_exec_ctrls() &&
2515 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2516 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Yang Zhang8d146952013-01-25 10:18:50 +08002517 if (is_long_mode(vcpu))
2518 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2519 else
2520 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2521 } else {
2522 if (is_long_mode(vcpu))
2523 msr_bitmap = vmx_msr_bitmap_longmode;
2524 else
2525 msr_bitmap = vmx_msr_bitmap_legacy;
2526 }
2527
2528 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2529}
2530
Eddie Donga75beee2007-05-17 18:55:15 +03002531/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002532 * Set up the vmcs to automatically save and restore system
2533 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2534 * mode, as fiddling with msrs is very expensive.
2535 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002536static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002537{
Avi Kivity26bb0982009-09-07 11:14:12 +03002538 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002539
Eddie Donga75beee2007-05-17 18:55:15 +03002540 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002541#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002542 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002543 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002544 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002545 move_msr_up(vmx, index, save_nmsrs++);
2546 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002547 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002548 move_msr_up(vmx, index, save_nmsrs++);
2549 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002550 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002551 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002552 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002553 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002554 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002555 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002556 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002557 * if efer.sce is enabled.
2558 */
Brian Gerst8c065852010-07-17 09:03:26 -04002559 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002560 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002561 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002562 }
Eddie Donga75beee2007-05-17 18:55:15 +03002563#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002564 index = __find_msr_index(vmx, MSR_EFER);
2565 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002566 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002567
Avi Kivity26bb0982009-09-07 11:14:12 +03002568 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002569
Yang Zhang8d146952013-01-25 10:18:50 +08002570 if (cpu_has_vmx_msr_bitmap())
2571 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002572}
2573
2574/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002576 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2577 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002579static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580{
2581 u64 host_tsc, tsc_offset;
2582
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002583 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002585 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586}
2587
2588/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002589 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2590 * counter, even if a nested guest (L2) is currently running.
2591 */
Paolo Bonzini48d89b92014-08-26 13:27:46 +02002592static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002593{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002594 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002595
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002596 tsc_offset = is_guest_mode(vcpu) ?
2597 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2598 vmcs_read64(TSC_OFFSET);
2599 return host_tsc + tsc_offset;
2600}
2601
Will Auldba904632012-11-29 12:42:50 -08002602static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2603{
2604 return vmcs_read64(TSC_OFFSET);
2605}
2606
Joerg Roedel4051b182011-03-25 09:44:49 +01002607/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002608 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002610static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002611{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002612 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002613 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002614 * We're here if L1 chose not to trap WRMSR to TSC. According
2615 * to the spec, this should set L1's TSC; The offset that L1
2616 * set for L2 remains unchanged, and still needs to be added
2617 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002618 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002619 struct vmcs12 *vmcs12;
2620 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2621 /* 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
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002633static void vmx_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002634{
2635 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002636
Zachary Amsdene48672f2010-08-19 22:07:23 -10002637 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002638 if (is_guest_mode(vcpu)) {
2639 /* Even when running L2, the adjustment needs to apply to L1 */
2640 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002641 } else
2642 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2643 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002644}
2645
Nadav Har'El801d3422011-05-25 23:02:23 +03002646static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2647{
2648 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2649 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2650}
2651
2652/*
2653 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2654 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2655 * all guests if the "nested" module option is off, and can also be disabled
2656 * for a single guest by disabling its VMX cpuid bit.
2657 */
2658static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2659{
2660 return nested && guest_cpuid_has_vmx(vcpu);
2661}
2662
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002664 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2665 * returned for the various VMX controls MSRs when nested VMX is enabled.
2666 * The same values should also be used to verify that vmcs12 control fields are
2667 * valid during nested entry from L1 to L2.
2668 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2669 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2670 * bit in the high half is on if the corresponding bit in the control field
2671 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002672 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002673static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002674{
2675 /*
2676 * Note that as a general rule, the high half of the MSRs (bits in
2677 * the control fields which may be 1) should be initialized by the
2678 * intersection of the underlying hardware's MSR (i.e., features which
2679 * can be supported) and the list of features we want to expose -
2680 * because they are known to be properly supported in our code.
2681 * Also, usually, the low half of the MSRs (bits which must be 1) can
2682 * be set to 0, meaning that L1 may turn off any of these bits. The
2683 * reason is that if one of these bits is necessary, it will appear
2684 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2685 * fields of vmcs01 and vmcs02, will turn these bits off - and
2686 * nested_vmx_exit_handled() will not pass related exits to L1.
2687 * These rules have exceptions below.
2688 */
2689
2690 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002691 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002692 vmx->nested.nested_vmx_pinbased_ctls_low,
2693 vmx->nested.nested_vmx_pinbased_ctls_high);
2694 vmx->nested.nested_vmx_pinbased_ctls_low |=
2695 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2696 vmx->nested.nested_vmx_pinbased_ctls_high &=
2697 PIN_BASED_EXT_INTR_MASK |
2698 PIN_BASED_NMI_EXITING |
2699 PIN_BASED_VIRTUAL_NMIS;
2700 vmx->nested.nested_vmx_pinbased_ctls_high |=
2701 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002702 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002703 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002704 vmx->nested.nested_vmx_pinbased_ctls_high |=
2705 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002706
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002707 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002708 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002709 vmx->nested.nested_vmx_exit_ctls_low,
2710 vmx->nested.nested_vmx_exit_ctls_high);
2711 vmx->nested.nested_vmx_exit_ctls_low =
2712 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002713
Wincy Vanb9c237b2015-02-03 23:56:30 +08002714 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002715#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002716 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002717#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002718 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002719 vmx->nested.nested_vmx_exit_ctls_high |=
2720 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002721 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002722 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2723
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002724 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002725 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002726
Jan Kiszka2996fca2014-06-16 13:59:43 +02002727 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002728 vmx->nested.nested_vmx_true_exit_ctls_low =
2729 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002730 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2731
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002732 /* entry controls */
2733 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002734 vmx->nested.nested_vmx_entry_ctls_low,
2735 vmx->nested.nested_vmx_entry_ctls_high);
2736 vmx->nested.nested_vmx_entry_ctls_low =
2737 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2738 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002739#ifdef CONFIG_X86_64
2740 VM_ENTRY_IA32E_MODE |
2741#endif
2742 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002743 vmx->nested.nested_vmx_entry_ctls_high |=
2744 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002745 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002746 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002747
Jan Kiszka2996fca2014-06-16 13:59:43 +02002748 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002749 vmx->nested.nested_vmx_true_entry_ctls_low =
2750 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002751 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2752
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002753 /* cpu-based controls */
2754 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002755 vmx->nested.nested_vmx_procbased_ctls_low,
2756 vmx->nested.nested_vmx_procbased_ctls_high);
2757 vmx->nested.nested_vmx_procbased_ctls_low =
2758 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2759 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002760 CPU_BASED_VIRTUAL_INTR_PENDING |
2761 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002762 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2763 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2764 CPU_BASED_CR3_STORE_EXITING |
2765#ifdef CONFIG_X86_64
2766 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2767#endif
2768 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002769 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2770 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2771 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2772 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002773 /*
2774 * We can allow some features even when not supported by the
2775 * hardware. For example, L1 can specify an MSR bitmap - and we
2776 * can use it to avoid exits to L1 - even when L0 runs L2
2777 * without MSR bitmaps.
2778 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002779 vmx->nested.nested_vmx_procbased_ctls_high |=
2780 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002781 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002782
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002783 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002784 vmx->nested.nested_vmx_true_procbased_ctls_low =
2785 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002786 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2787
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002788 /* secondary cpu-based controls */
2789 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002790 vmx->nested.nested_vmx_secondary_ctls_low,
2791 vmx->nested.nested_vmx_secondary_ctls_high);
2792 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2793 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002794 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002795 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002796 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002797 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002798 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002799 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002800 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002801 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002802
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002803 if (enable_ept) {
2804 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002805 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002806 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002807 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002808 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2809 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002810 if (cpu_has_vmx_ept_execute_only())
2811 vmx->nested.nested_vmx_ept_caps |=
2812 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002813 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002814 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2815 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002816 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002817 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002818
Paolo Bonzinief697a72016-03-18 16:58:38 +01002819 /*
2820 * Old versions of KVM use the single-context version without
2821 * checking for support, so declare that it is supported even
2822 * though it is treated as global context. The alternative is
2823 * not failing the single-context invvpid, and it is worse.
2824 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002825 if (enable_vpid)
2826 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Paolo Bonzinief697a72016-03-18 16:58:38 +01002827 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT |
Wanpeng Li089d7b62015-10-13 09:18:37 -07002828 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
2829 else
2830 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002831
Radim Krčmář0790ec12015-03-17 14:02:32 +01002832 if (enable_unrestricted_guest)
2833 vmx->nested.nested_vmx_secondary_ctls_high |=
2834 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2835
Jan Kiszkac18911a2013-03-13 16:06:41 +01002836 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002837 rdmsr(MSR_IA32_VMX_MISC,
2838 vmx->nested.nested_vmx_misc_low,
2839 vmx->nested.nested_vmx_misc_high);
2840 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2841 vmx->nested.nested_vmx_misc_low |=
2842 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002843 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002844 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002845}
2846
2847static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2848{
2849 /*
2850 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2851 */
2852 return ((control & high) | low) == control;
2853}
2854
2855static inline u64 vmx_control_msr(u32 low, u32 high)
2856{
2857 return low | ((u64)high << 32);
2858}
2859
Jan Kiszkacae50132014-01-04 18:47:22 +01002860/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002861static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2862{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002863 struct vcpu_vmx *vmx = to_vmx(vcpu);
2864
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002865 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002866 case MSR_IA32_VMX_BASIC:
2867 /*
2868 * This MSR reports some information about VMX support. We
2869 * should return information about the VMX we emulate for the
2870 * guest, and the VMCS structure we give it - not about the
2871 * VMX support of the underlying hardware.
2872 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002873 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002874 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2875 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2876 break;
2877 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2878 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002879 *pdata = vmx_control_msr(
2880 vmx->nested.nested_vmx_pinbased_ctls_low,
2881 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002882 break;
2883 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002884 *pdata = vmx_control_msr(
2885 vmx->nested.nested_vmx_true_procbased_ctls_low,
2886 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002887 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002888 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002889 *pdata = vmx_control_msr(
2890 vmx->nested.nested_vmx_procbased_ctls_low,
2891 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002892 break;
2893 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002894 *pdata = vmx_control_msr(
2895 vmx->nested.nested_vmx_true_exit_ctls_low,
2896 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002897 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002898 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002899 *pdata = vmx_control_msr(
2900 vmx->nested.nested_vmx_exit_ctls_low,
2901 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002902 break;
2903 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002904 *pdata = vmx_control_msr(
2905 vmx->nested.nested_vmx_true_entry_ctls_low,
2906 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002907 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002908 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002909 *pdata = vmx_control_msr(
2910 vmx->nested.nested_vmx_entry_ctls_low,
2911 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002912 break;
2913 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002914 *pdata = vmx_control_msr(
2915 vmx->nested.nested_vmx_misc_low,
2916 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002917 break;
2918 /*
2919 * These MSRs specify bits which the guest must keep fixed (on or off)
2920 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2921 * We picked the standard core2 setting.
2922 */
2923#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2924#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2925 case MSR_IA32_VMX_CR0_FIXED0:
2926 *pdata = VMXON_CR0_ALWAYSON;
2927 break;
2928 case MSR_IA32_VMX_CR0_FIXED1:
2929 *pdata = -1ULL;
2930 break;
2931 case MSR_IA32_VMX_CR4_FIXED0:
2932 *pdata = VMXON_CR4_ALWAYSON;
2933 break;
2934 case MSR_IA32_VMX_CR4_FIXED1:
2935 *pdata = -1ULL;
2936 break;
2937 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002938 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002939 break;
2940 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002941 *pdata = vmx_control_msr(
2942 vmx->nested.nested_vmx_secondary_ctls_low,
2943 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002944 break;
2945 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07002946 *pdata = vmx->nested.nested_vmx_ept_caps |
2947 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002948 break;
2949 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002950 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002951 }
2952
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002953 return 0;
2954}
2955
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002956static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2957 uint64_t val)
2958{
2959 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2960
2961 return !(val & ~valid_bits);
2962}
2963
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002964/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 * Reads an msr value (of 'msr_index') into 'pdata'.
2966 * Returns 0 on success, non-0 otherwise.
2967 * Assumes vcpu_load() was already called.
2968 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002969static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970{
Avi Kivity26bb0982009-09-07 11:14:12 +03002971 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002973 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002974#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002976 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977 break;
2978 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002979 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002981 case MSR_KERNEL_GS_BASE:
2982 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002983 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002984 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002985#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002987 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302988 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002989 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002990 break;
2991 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002992 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993 break;
2994 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002995 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996 break;
2997 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002998 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003000 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003001 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003002 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003003 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003004 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003005 case MSR_IA32_MCG_EXT_CTL:
3006 if (!msr_info->host_initiated &&
3007 !(to_vmx(vcpu)->msr_ia32_feature_control &
3008 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003009 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003010 msr_info->data = vcpu->arch.mcg_ext_ctl;
3011 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003012 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003013 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003014 break;
3015 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3016 if (!nested_vmx_allowed(vcpu))
3017 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003018 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003019 case MSR_IA32_XSS:
3020 if (!vmx_xsaves_supported())
3021 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003022 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003023 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003024 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003025 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003026 return 1;
3027 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003029 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003030 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003031 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003032 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003034 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 }
3036
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037 return 0;
3038}
3039
Jan Kiszkacae50132014-01-04 18:47:22 +01003040static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3041
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042/*
3043 * Writes msr value into into the appropriate "register".
3044 * Returns 0 on success, non-0 otherwise.
3045 * Assumes vcpu_load() was already called.
3046 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003047static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003049 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003050 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003051 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003052 u32 msr_index = msr_info->index;
3053 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003054
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003056 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003057 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003058 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003059#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003061 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062 vmcs_writel(GUEST_FS_BASE, data);
3063 break;
3064 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003065 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 vmcs_writel(GUEST_GS_BASE, data);
3067 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003068 case MSR_KERNEL_GS_BASE:
3069 vmx_load_host_state(vmx);
3070 vmx->msr_guest_kernel_gs_base = data;
3071 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072#endif
3073 case MSR_IA32_SYSENTER_CS:
3074 vmcs_write32(GUEST_SYSENTER_CS, data);
3075 break;
3076 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003077 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003078 break;
3079 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003080 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003082 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003083 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003084 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003085 vmcs_write64(GUEST_BNDCFGS, data);
3086 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303087 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003088 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003090 case MSR_IA32_CR_PAT:
3091 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003092 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3093 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003094 vmcs_write64(GUEST_IA32_PAT, data);
3095 vcpu->arch.pat = data;
3096 break;
3097 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003098 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003099 break;
Will Auldba904632012-11-29 12:42:50 -08003100 case MSR_IA32_TSC_ADJUST:
3101 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003102 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003103 case MSR_IA32_MCG_EXT_CTL:
3104 if ((!msr_info->host_initiated &&
3105 !(to_vmx(vcpu)->msr_ia32_feature_control &
3106 FEATURE_CONTROL_LMCE)) ||
3107 (data & ~MCG_EXT_CTL_LMCE_EN))
3108 return 1;
3109 vcpu->arch.mcg_ext_ctl = data;
3110 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003111 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003112 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003113 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003114 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3115 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003116 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003117 if (msr_info->host_initiated && data == 0)
3118 vmx_leave_nested(vcpu);
3119 break;
3120 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3121 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003122 case MSR_IA32_XSS:
3123 if (!vmx_xsaves_supported())
3124 return 1;
3125 /*
3126 * The only supported bit as of Skylake is bit 8, but
3127 * it is not supported on KVM.
3128 */
3129 if (data != 0)
3130 return 1;
3131 vcpu->arch.ia32_xss = data;
3132 if (vcpu->arch.ia32_xss != host_xss)
3133 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3134 vcpu->arch.ia32_xss, host_xss);
3135 else
3136 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3137 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003138 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003139 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003140 return 1;
3141 /* Check reserved bit, higher 32 bits should be zero */
3142 if ((data >> 32) != 0)
3143 return 1;
3144 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003146 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003147 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003148 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003149 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003150 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3151 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003152 ret = kvm_set_shared_msr(msr->index, msr->data,
3153 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003154 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003155 if (ret)
3156 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003157 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003158 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003160 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 }
3162
Eddie Dong2cc51562007-05-21 07:28:09 +03003163 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164}
3165
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003166static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003168 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3169 switch (reg) {
3170 case VCPU_REGS_RSP:
3171 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3172 break;
3173 case VCPU_REGS_RIP:
3174 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3175 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003176 case VCPU_EXREG_PDPTR:
3177 if (enable_ept)
3178 ept_save_pdptrs(vcpu);
3179 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003180 default:
3181 break;
3182 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183}
3184
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185static __init int cpu_has_kvm_support(void)
3186{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003187 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188}
3189
3190static __init int vmx_disabled_by_bios(void)
3191{
3192 u64 msr;
3193
3194 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003195 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003196 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003197 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3198 && tboot_enabled())
3199 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003200 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003201 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003202 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003203 && !tboot_enabled()) {
3204 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003205 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003206 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003207 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003208 /* launched w/o TXT and VMX disabled */
3209 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3210 && !tboot_enabled())
3211 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003212 }
3213
3214 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215}
3216
Dongxiao Xu7725b892010-05-11 18:29:38 +08003217static void kvm_cpu_vmxon(u64 addr)
3218{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003219 intel_pt_handle_vmx(1);
3220
Dongxiao Xu7725b892010-05-11 18:29:38 +08003221 asm volatile (ASM_VMX_VMXON_RAX
3222 : : "a"(&addr), "m"(addr)
3223 : "memory", "cc");
3224}
3225
Radim Krčmář13a34e02014-08-28 15:13:03 +02003226static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227{
3228 int cpu = raw_smp_processor_id();
3229 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003230 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003232 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003233 return -EBUSY;
3234
Nadav Har'Eld462b812011-05-24 15:26:10 +03003235 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003236 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3237 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003238
3239 /*
3240 * Now we can enable the vmclear operation in kdump
3241 * since the loaded_vmcss_on_cpu list on this cpu
3242 * has been initialized.
3243 *
3244 * Though the cpu is not in VMX operation now, there
3245 * is no problem to enable the vmclear operation
3246 * for the loaded_vmcss_on_cpu list is empty!
3247 */
3248 crash_enable_local_vmclear(cpu);
3249
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003251
3252 test_bits = FEATURE_CONTROL_LOCKED;
3253 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3254 if (tboot_enabled())
3255 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3256
3257 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003259 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3260 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003261 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003262
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003263 if (vmm_exclusive) {
3264 kvm_cpu_vmxon(phys_addr);
3265 ept_sync_global();
3266 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003267
Christoph Lameter89cbc762014-08-17 12:30:40 -05003268 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003269
Alexander Graf10474ae2009-09-15 11:37:46 +02003270 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271}
3272
Nadav Har'Eld462b812011-05-24 15:26:10 +03003273static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003274{
3275 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003276 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003277
Nadav Har'Eld462b812011-05-24 15:26:10 +03003278 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3279 loaded_vmcss_on_cpu_link)
3280 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003281}
3282
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003283
3284/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3285 * tricks.
3286 */
3287static void kvm_cpu_vmxoff(void)
3288{
3289 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003290
3291 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003292}
3293
Radim Krčmář13a34e02014-08-28 15:13:03 +02003294static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003295{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003296 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003297 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003298 kvm_cpu_vmxoff();
3299 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003300 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003301}
3302
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003303static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003304 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305{
3306 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003307 u32 ctl = ctl_min | ctl_opt;
3308
3309 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3310
3311 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3312 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3313
3314 /* Ensure minimum (required) set of control bits are supported. */
3315 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003316 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003317
3318 *result = ctl;
3319 return 0;
3320}
3321
Avi Kivity110312c2010-12-21 12:54:20 +02003322static __init bool allow_1_setting(u32 msr, u32 ctl)
3323{
3324 u32 vmx_msr_low, vmx_msr_high;
3325
3326 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3327 return vmx_msr_high & ctl;
3328}
3329
Yang, Sheng002c7f72007-07-31 14:23:01 +03003330static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003331{
3332 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003333 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003334 u32 _pin_based_exec_control = 0;
3335 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003336 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003337 u32 _vmexit_control = 0;
3338 u32 _vmentry_control = 0;
3339
Raghavendra K T10166742012-02-07 23:19:20 +05303340 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003341#ifdef CONFIG_X86_64
3342 CPU_BASED_CR8_LOAD_EXITING |
3343 CPU_BASED_CR8_STORE_EXITING |
3344#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003345 CPU_BASED_CR3_LOAD_EXITING |
3346 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003347 CPU_BASED_USE_IO_BITMAPS |
3348 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003349 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003350 CPU_BASED_MWAIT_EXITING |
3351 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003352 CPU_BASED_INVLPG_EXITING |
3353 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003354
Sheng Yangf78e0e22007-10-29 09:40:42 +08003355 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003356 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003357 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003358 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3359 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003360 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003361#ifdef CONFIG_X86_64
3362 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3363 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3364 ~CPU_BASED_CR8_STORE_EXITING;
3365#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003366 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003367 min2 = 0;
3368 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003369 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003370 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003371 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003372 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003373 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003374 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003375 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003376 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003377 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003378 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003379 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003380 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003381 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003382 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003383 if (adjust_vmx_controls(min2, opt2,
3384 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003385 &_cpu_based_2nd_exec_control) < 0)
3386 return -EIO;
3387 }
3388#ifndef CONFIG_X86_64
3389 if (!(_cpu_based_2nd_exec_control &
3390 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3391 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3392#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003393
3394 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3395 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003396 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003397 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3398 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003399
Sheng Yangd56f5462008-04-25 10:13:16 +08003400 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003401 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3402 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003403 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3404 CPU_BASED_CR3_STORE_EXITING |
3405 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003406 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3407 vmx_capability.ept, vmx_capability.vpid);
3408 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003409
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003410 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003411#ifdef CONFIG_X86_64
3412 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3413#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003414 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003415 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003416 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3417 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003418 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003419
Yang Zhang01e439b2013-04-11 19:25:12 +08003420 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003421 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3422 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003423 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3424 &_pin_based_exec_control) < 0)
3425 return -EIO;
3426
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003427 if (cpu_has_broken_vmx_preemption_timer())
3428 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003429 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003430 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003431 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3432
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003433 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003434 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003435 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3436 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003437 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003439 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003440
3441 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3442 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003443 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003444
3445#ifdef CONFIG_X86_64
3446 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3447 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003448 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003449#endif
3450
3451 /* Require Write-Back (WB) memory type for VMCS accesses. */
3452 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003453 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003454
Yang, Sheng002c7f72007-07-31 14:23:01 +03003455 vmcs_conf->size = vmx_msr_high & 0x1fff;
3456 vmcs_conf->order = get_order(vmcs_config.size);
3457 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003458
Yang, Sheng002c7f72007-07-31 14:23:01 +03003459 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3460 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003461 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003462 vmcs_conf->vmexit_ctrl = _vmexit_control;
3463 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003464
Avi Kivity110312c2010-12-21 12:54:20 +02003465 cpu_has_load_ia32_efer =
3466 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3467 VM_ENTRY_LOAD_IA32_EFER)
3468 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3469 VM_EXIT_LOAD_IA32_EFER);
3470
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003471 cpu_has_load_perf_global_ctrl =
3472 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3473 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3474 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3475 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3476
3477 /*
3478 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003479 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003480 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3481 *
3482 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3483 *
3484 * AAK155 (model 26)
3485 * AAP115 (model 30)
3486 * AAT100 (model 37)
3487 * BC86,AAY89,BD102 (model 44)
3488 * BA97 (model 46)
3489 *
3490 */
3491 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3492 switch (boot_cpu_data.x86_model) {
3493 case 26:
3494 case 30:
3495 case 37:
3496 case 44:
3497 case 46:
3498 cpu_has_load_perf_global_ctrl = false;
3499 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3500 "does not work properly. Using workaround\n");
3501 break;
3502 default:
3503 break;
3504 }
3505 }
3506
Borislav Petkov782511b2016-04-04 22:25:03 +02003507 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003508 rdmsrl(MSR_IA32_XSS, host_xss);
3509
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003510 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003511}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003512
3513static struct vmcs *alloc_vmcs_cpu(int cpu)
3514{
3515 int node = cpu_to_node(cpu);
3516 struct page *pages;
3517 struct vmcs *vmcs;
3518
Vlastimil Babka96db8002015-09-08 15:03:50 -07003519 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520 if (!pages)
3521 return NULL;
3522 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003523 memset(vmcs, 0, vmcs_config.size);
3524 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525 return vmcs;
3526}
3527
3528static struct vmcs *alloc_vmcs(void)
3529{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003530 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531}
3532
3533static void free_vmcs(struct vmcs *vmcs)
3534{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003535 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536}
3537
Nadav Har'Eld462b812011-05-24 15:26:10 +03003538/*
3539 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3540 */
3541static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3542{
3543 if (!loaded_vmcs->vmcs)
3544 return;
3545 loaded_vmcs_clear(loaded_vmcs);
3546 free_vmcs(loaded_vmcs->vmcs);
3547 loaded_vmcs->vmcs = NULL;
3548}
3549
Sam Ravnborg39959582007-06-01 00:47:13 -07003550static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003551{
3552 int cpu;
3553
Zachary Amsden3230bb42009-09-29 11:38:37 -10003554 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003555 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003556 per_cpu(vmxarea, cpu) = NULL;
3557 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003558}
3559
Bandan Dasfe2b2012014-04-21 15:20:14 -04003560static void init_vmcs_shadow_fields(void)
3561{
3562 int i, j;
3563
3564 /* No checks for read only fields yet */
3565
3566 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3567 switch (shadow_read_write_fields[i]) {
3568 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003569 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003570 continue;
3571 break;
3572 default:
3573 break;
3574 }
3575
3576 if (j < i)
3577 shadow_read_write_fields[j] =
3578 shadow_read_write_fields[i];
3579 j++;
3580 }
3581 max_shadow_read_write_fields = j;
3582
3583 /* shadowed fields guest access without vmexit */
3584 for (i = 0; i < max_shadow_read_write_fields; i++) {
3585 clear_bit(shadow_read_write_fields[i],
3586 vmx_vmwrite_bitmap);
3587 clear_bit(shadow_read_write_fields[i],
3588 vmx_vmread_bitmap);
3589 }
3590 for (i = 0; i < max_shadow_read_only_fields; i++)
3591 clear_bit(shadow_read_only_fields[i],
3592 vmx_vmread_bitmap);
3593}
3594
Avi Kivity6aa8b732006-12-10 02:21:36 -08003595static __init int alloc_kvm_area(void)
3596{
3597 int cpu;
3598
Zachary Amsden3230bb42009-09-29 11:38:37 -10003599 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600 struct vmcs *vmcs;
3601
3602 vmcs = alloc_vmcs_cpu(cpu);
3603 if (!vmcs) {
3604 free_kvm_area();
3605 return -ENOMEM;
3606 }
3607
3608 per_cpu(vmxarea, cpu) = vmcs;
3609 }
3610 return 0;
3611}
3612
Gleb Natapov14168782013-01-21 15:36:49 +02003613static bool emulation_required(struct kvm_vcpu *vcpu)
3614{
3615 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3616}
3617
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003618static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003619 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003621 if (!emulate_invalid_guest_state) {
3622 /*
3623 * CS and SS RPL should be equal during guest entry according
3624 * to VMX spec, but in reality it is not always so. Since vcpu
3625 * is in the middle of the transition from real mode to
3626 * protected mode it is safe to assume that RPL 0 is a good
3627 * default value.
3628 */
3629 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003630 save->selector &= ~SEGMENT_RPL_MASK;
3631 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003632 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003634 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003635}
3636
3637static void enter_pmode(struct kvm_vcpu *vcpu)
3638{
3639 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003640 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003641
Gleb Natapovd99e4152012-12-20 16:57:45 +02003642 /*
3643 * Update real mode segment cache. It may be not up-to-date if sement
3644 * register was written while vcpu was in a guest mode.
3645 */
3646 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3647 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3648 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3649 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3650 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3651 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3652
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003653 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003654
Avi Kivity2fb92db2011-04-27 19:42:18 +03003655 vmx_segment_cache_clear(vmx);
3656
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003657 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658
3659 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003660 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3661 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003662 vmcs_writel(GUEST_RFLAGS, flags);
3663
Rusty Russell66aee912007-07-17 23:34:16 +10003664 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3665 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003666
3667 update_exception_bitmap(vcpu);
3668
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003669 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3670 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3671 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3672 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3673 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3674 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675}
3676
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003677static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003678{
Mathias Krause772e0312012-08-30 01:30:19 +02003679 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003680 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681
Gleb Natapovd99e4152012-12-20 16:57:45 +02003682 var.dpl = 0x3;
3683 if (seg == VCPU_SREG_CS)
3684 var.type = 0x3;
3685
3686 if (!emulate_invalid_guest_state) {
3687 var.selector = var.base >> 4;
3688 var.base = var.base & 0xffff0;
3689 var.limit = 0xffff;
3690 var.g = 0;
3691 var.db = 0;
3692 var.present = 1;
3693 var.s = 1;
3694 var.l = 0;
3695 var.unusable = 0;
3696 var.type = 0x3;
3697 var.avl = 0;
3698 if (save->base & 0xf)
3699 printk_once(KERN_WARNING "kvm: segment base is not "
3700 "paragraph aligned when entering "
3701 "protected mode (seg=%d)", seg);
3702 }
3703
3704 vmcs_write16(sf->selector, var.selector);
3705 vmcs_write32(sf->base, var.base);
3706 vmcs_write32(sf->limit, var.limit);
3707 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003708}
3709
3710static void enter_rmode(struct kvm_vcpu *vcpu)
3711{
3712 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003713 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003715 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3716 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3717 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3718 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3719 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003720 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3721 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003722
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003723 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003724
Gleb Natapov776e58e2011-03-13 12:34:27 +02003725 /*
3726 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003727 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003728 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003729 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003730 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3731 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003732
Avi Kivity2fb92db2011-04-27 19:42:18 +03003733 vmx_segment_cache_clear(vmx);
3734
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003735 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003737 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3738
3739 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003740 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003741
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003742 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003743
3744 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003745 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746 update_exception_bitmap(vcpu);
3747
Gleb Natapovd99e4152012-12-20 16:57:45 +02003748 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3749 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3750 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3751 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3752 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3753 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003754
Eddie Dong8668a3c2007-10-10 14:26:45 +08003755 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003756}
3757
Amit Shah401d10d2009-02-20 22:53:37 +05303758static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3759{
3760 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003761 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3762
3763 if (!msr)
3764 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303765
Avi Kivity44ea2b12009-09-06 15:55:37 +03003766 /*
3767 * Force kernel_gs_base reloading before EFER changes, as control
3768 * of this msr depends on is_long_mode().
3769 */
3770 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003771 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303772 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003773 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303774 msr->data = efer;
3775 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003776 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303777
3778 msr->data = efer & ~EFER_LME;
3779 }
3780 setup_msrs(vmx);
3781}
3782
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003783#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784
3785static void enter_lmode(struct kvm_vcpu *vcpu)
3786{
3787 u32 guest_tr_ar;
3788
Avi Kivity2fb92db2011-04-27 19:42:18 +03003789 vmx_segment_cache_clear(to_vmx(vcpu));
3790
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003792 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003793 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3794 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003796 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3797 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798 }
Avi Kivityda38f432010-07-06 11:30:49 +03003799 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800}
3801
3802static void exit_lmode(struct kvm_vcpu *vcpu)
3803{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003804 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003805 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806}
3807
3808#endif
3809
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003810static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003811{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003812 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003813 if (enable_ept) {
3814 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3815 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003816 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003817 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003818}
3819
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003820static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3821{
3822 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3823}
3824
Avi Kivitye8467fd2009-12-29 18:43:06 +02003825static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3826{
3827 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3828
3829 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3830 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3831}
3832
Avi Kivityaff48ba2010-12-05 18:56:11 +02003833static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3834{
3835 if (enable_ept && is_paging(vcpu))
3836 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3837 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3838}
3839
Anthony Liguori25c4c272007-04-27 09:29:21 +03003840static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003841{
Avi Kivityfc78f512009-12-07 12:16:48 +02003842 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3843
3844 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3845 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003846}
3847
Sheng Yang14394422008-04-28 12:24:45 +08003848static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3849{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003850 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3851
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003852 if (!test_bit(VCPU_EXREG_PDPTR,
3853 (unsigned long *)&vcpu->arch.regs_dirty))
3854 return;
3855
Sheng Yang14394422008-04-28 12:24:45 +08003856 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003857 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3858 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3859 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3860 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003861 }
3862}
3863
Avi Kivity8f5d5492009-05-31 18:41:29 +03003864static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3865{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003866 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3867
Avi Kivity8f5d5492009-05-31 18:41:29 +03003868 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003869 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3870 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3871 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3872 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003873 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003874
3875 __set_bit(VCPU_EXREG_PDPTR,
3876 (unsigned long *)&vcpu->arch.regs_avail);
3877 __set_bit(VCPU_EXREG_PDPTR,
3878 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003879}
3880
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003881static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003882
3883static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3884 unsigned long cr0,
3885 struct kvm_vcpu *vcpu)
3886{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003887 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3888 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003889 if (!(cr0 & X86_CR0_PG)) {
3890 /* From paging/starting to nonpaging */
3891 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003892 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003893 (CPU_BASED_CR3_LOAD_EXITING |
3894 CPU_BASED_CR3_STORE_EXITING));
3895 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003896 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003897 } else if (!is_paging(vcpu)) {
3898 /* From nonpaging to paging */
3899 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003900 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003901 ~(CPU_BASED_CR3_LOAD_EXITING |
3902 CPU_BASED_CR3_STORE_EXITING));
3903 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003904 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003905 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003906
3907 if (!(cr0 & X86_CR0_WP))
3908 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003909}
3910
Avi Kivity6aa8b732006-12-10 02:21:36 -08003911static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3912{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003913 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003914 unsigned long hw_cr0;
3915
Gleb Natapov50378782013-02-04 16:00:28 +02003916 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003917 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003918 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003919 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003920 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003921
Gleb Natapov218e7632013-01-21 15:36:45 +02003922 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3923 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924
Gleb Natapov218e7632013-01-21 15:36:45 +02003925 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3926 enter_rmode(vcpu);
3927 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003928
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003929#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003930 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003931 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003933 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934 exit_lmode(vcpu);
3935 }
3936#endif
3937
Avi Kivity089d0342009-03-23 18:26:32 +02003938 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003939 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3940
Avi Kivity02daab22009-12-30 12:40:26 +02003941 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003942 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003943
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003945 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003946 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003947
3948 /* depends on vcpu->arch.cr0 to be set to a new value */
3949 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003950}
3951
Sheng Yang14394422008-04-28 12:24:45 +08003952static u64 construct_eptp(unsigned long root_hpa)
3953{
3954 u64 eptp;
3955
3956 /* TODO write the value reading from MSR */
3957 eptp = VMX_EPT_DEFAULT_MT |
3958 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003959 if (enable_ept_ad_bits)
3960 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003961 eptp |= (root_hpa & PAGE_MASK);
3962
3963 return eptp;
3964}
3965
Avi Kivity6aa8b732006-12-10 02:21:36 -08003966static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3967{
Sheng Yang14394422008-04-28 12:24:45 +08003968 unsigned long guest_cr3;
3969 u64 eptp;
3970
3971 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003972 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003973 eptp = construct_eptp(cr3);
3974 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003975 if (is_paging(vcpu) || is_guest_mode(vcpu))
3976 guest_cr3 = kvm_read_cr3(vcpu);
3977 else
3978 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02003979 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003980 }
3981
Sheng Yang2384d2b2008-01-17 15:14:33 +08003982 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003983 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984}
3985
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003986static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003987{
Ben Serebrin085e68e2015-04-16 11:58:05 -07003988 /*
3989 * Pass through host's Machine Check Enable value to hw_cr4, which
3990 * is in force while we are in guest mode. Do not let guests control
3991 * this bit, even if host CR4.MCE == 0.
3992 */
3993 unsigned long hw_cr4 =
3994 (cr4_read_shadow() & X86_CR4_MCE) |
3995 (cr4 & ~X86_CR4_MCE) |
3996 (to_vmx(vcpu)->rmode.vm86_active ?
3997 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08003998
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003999 if (cr4 & X86_CR4_VMXE) {
4000 /*
4001 * To use VMXON (and later other VMX instructions), a guest
4002 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4003 * So basically the check on whether to allow nested VMX
4004 * is here.
4005 */
4006 if (!nested_vmx_allowed(vcpu))
4007 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004008 }
4009 if (to_vmx(vcpu)->nested.vmxon &&
4010 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004011 return 1;
4012
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004013 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004014 if (enable_ept) {
4015 if (!is_paging(vcpu)) {
4016 hw_cr4 &= ~X86_CR4_PAE;
4017 hw_cr4 |= X86_CR4_PSE;
4018 } else if (!(cr4 & X86_CR4_PAE)) {
4019 hw_cr4 &= ~X86_CR4_PAE;
4020 }
4021 }
Sheng Yang14394422008-04-28 12:24:45 +08004022
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004023 if (!enable_unrestricted_guest && !is_paging(vcpu))
4024 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004025 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4026 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4027 * to be manually disabled when guest switches to non-paging
4028 * mode.
4029 *
4030 * If !enable_unrestricted_guest, the CPU is always running
4031 * with CR0.PG=1 and CR4 needs to be modified.
4032 * If enable_unrestricted_guest, the CPU automatically
4033 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004034 */
Huaitong Handdba2622016-03-22 16:51:15 +08004035 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004036
Sheng Yang14394422008-04-28 12:24:45 +08004037 vmcs_writel(CR4_READ_SHADOW, cr4);
4038 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004039 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040}
4041
Avi Kivity6aa8b732006-12-10 02:21:36 -08004042static void vmx_get_segment(struct kvm_vcpu *vcpu,
4043 struct kvm_segment *var, int seg)
4044{
Avi Kivitya9179492011-01-03 14:28:52 +02004045 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004046 u32 ar;
4047
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004048 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004049 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004050 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004051 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004052 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004053 var->base = vmx_read_guest_seg_base(vmx, seg);
4054 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4055 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004056 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004057 var->base = vmx_read_guest_seg_base(vmx, seg);
4058 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4059 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4060 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004061 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062 var->type = ar & 15;
4063 var->s = (ar >> 4) & 1;
4064 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004065 /*
4066 * Some userspaces do not preserve unusable property. Since usable
4067 * segment has to be present according to VMX spec we can use present
4068 * property to amend userspace bug by making unusable segment always
4069 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4070 * segment as unusable.
4071 */
4072 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 var->avl = (ar >> 12) & 1;
4074 var->l = (ar >> 13) & 1;
4075 var->db = (ar >> 14) & 1;
4076 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077}
4078
Avi Kivitya9179492011-01-03 14:28:52 +02004079static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4080{
Avi Kivitya9179492011-01-03 14:28:52 +02004081 struct kvm_segment s;
4082
4083 if (to_vmx(vcpu)->rmode.vm86_active) {
4084 vmx_get_segment(vcpu, &s, seg);
4085 return s.base;
4086 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004087 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004088}
4089
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004090static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004091{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004092 struct vcpu_vmx *vmx = to_vmx(vcpu);
4093
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004094 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004095 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004096 else {
4097 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004098 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004099 }
Avi Kivity69c73022011-03-07 15:26:44 +02004100}
4101
Avi Kivity653e3102007-05-07 10:55:37 +03004102static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004104 u32 ar;
4105
Avi Kivityf0495f92012-06-07 17:06:10 +03004106 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107 ar = 1 << 16;
4108 else {
4109 ar = var->type & 15;
4110 ar |= (var->s & 1) << 4;
4111 ar |= (var->dpl & 3) << 5;
4112 ar |= (var->present & 1) << 7;
4113 ar |= (var->avl & 1) << 12;
4114 ar |= (var->l & 1) << 13;
4115 ar |= (var->db & 1) << 14;
4116 ar |= (var->g & 1) << 15;
4117 }
Avi Kivity653e3102007-05-07 10:55:37 +03004118
4119 return ar;
4120}
4121
4122static void vmx_set_segment(struct kvm_vcpu *vcpu,
4123 struct kvm_segment *var, int seg)
4124{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004125 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004126 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004127
Avi Kivity2fb92db2011-04-27 19:42:18 +03004128 vmx_segment_cache_clear(vmx);
4129
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004130 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4131 vmx->rmode.segs[seg] = *var;
4132 if (seg == VCPU_SREG_TR)
4133 vmcs_write16(sf->selector, var->selector);
4134 else if (var->s)
4135 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004136 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004137 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004138
Avi Kivity653e3102007-05-07 10:55:37 +03004139 vmcs_writel(sf->base, var->base);
4140 vmcs_write32(sf->limit, var->limit);
4141 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004142
4143 /*
4144 * Fix the "Accessed" bit in AR field of segment registers for older
4145 * qemu binaries.
4146 * IA32 arch specifies that at the time of processor reset the
4147 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004148 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004149 * state vmexit when "unrestricted guest" mode is turned on.
4150 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4151 * tree. Newer qemu binaries with that qemu fix would not need this
4152 * kvm hack.
4153 */
4154 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004155 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004156
Gleb Natapovf924d662012-12-12 19:10:55 +02004157 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004158
4159out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004160 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161}
4162
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4164{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004165 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166
4167 *db = (ar >> 14) & 1;
4168 *l = (ar >> 13) & 1;
4169}
4170
Gleb Natapov89a27f42010-02-16 10:51:48 +02004171static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004173 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4174 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175}
4176
Gleb Natapov89a27f42010-02-16 10:51:48 +02004177static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004179 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4180 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181}
4182
Gleb Natapov89a27f42010-02-16 10:51:48 +02004183static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004185 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4186 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187}
4188
Gleb Natapov89a27f42010-02-16 10:51:48 +02004189static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004191 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4192 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193}
4194
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004195static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4196{
4197 struct kvm_segment var;
4198 u32 ar;
4199
4200 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004201 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004202 if (seg == VCPU_SREG_CS)
4203 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004204 ar = vmx_segment_access_rights(&var);
4205
4206 if (var.base != (var.selector << 4))
4207 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004208 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004209 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004210 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004211 return false;
4212
4213 return true;
4214}
4215
4216static bool code_segment_valid(struct kvm_vcpu *vcpu)
4217{
4218 struct kvm_segment cs;
4219 unsigned int cs_rpl;
4220
4221 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004222 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004223
Avi Kivity1872a3f2009-01-04 23:26:52 +02004224 if (cs.unusable)
4225 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004226 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004227 return false;
4228 if (!cs.s)
4229 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004230 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004231 if (cs.dpl > cs_rpl)
4232 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004233 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004234 if (cs.dpl != cs_rpl)
4235 return false;
4236 }
4237 if (!cs.present)
4238 return false;
4239
4240 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4241 return true;
4242}
4243
4244static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4245{
4246 struct kvm_segment ss;
4247 unsigned int ss_rpl;
4248
4249 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004250 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004251
Avi Kivity1872a3f2009-01-04 23:26:52 +02004252 if (ss.unusable)
4253 return true;
4254 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004255 return false;
4256 if (!ss.s)
4257 return false;
4258 if (ss.dpl != ss_rpl) /* DPL != RPL */
4259 return false;
4260 if (!ss.present)
4261 return false;
4262
4263 return true;
4264}
4265
4266static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4267{
4268 struct kvm_segment var;
4269 unsigned int rpl;
4270
4271 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004272 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004273
Avi Kivity1872a3f2009-01-04 23:26:52 +02004274 if (var.unusable)
4275 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004276 if (!var.s)
4277 return false;
4278 if (!var.present)
4279 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004280 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004281 if (var.dpl < rpl) /* DPL < RPL */
4282 return false;
4283 }
4284
4285 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4286 * rights flags
4287 */
4288 return true;
4289}
4290
4291static bool tr_valid(struct kvm_vcpu *vcpu)
4292{
4293 struct kvm_segment tr;
4294
4295 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4296
Avi Kivity1872a3f2009-01-04 23:26:52 +02004297 if (tr.unusable)
4298 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004299 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004300 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004301 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004302 return false;
4303 if (!tr.present)
4304 return false;
4305
4306 return true;
4307}
4308
4309static bool ldtr_valid(struct kvm_vcpu *vcpu)
4310{
4311 struct kvm_segment ldtr;
4312
4313 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4314
Avi Kivity1872a3f2009-01-04 23:26:52 +02004315 if (ldtr.unusable)
4316 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004317 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004318 return false;
4319 if (ldtr.type != 2)
4320 return false;
4321 if (!ldtr.present)
4322 return false;
4323
4324 return true;
4325}
4326
4327static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4328{
4329 struct kvm_segment cs, ss;
4330
4331 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4332 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4333
Nadav Amitb32a9912015-03-29 16:33:04 +03004334 return ((cs.selector & SEGMENT_RPL_MASK) ==
4335 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004336}
4337
4338/*
4339 * Check if guest state is valid. Returns true if valid, false if
4340 * not.
4341 * We assume that registers are always usable
4342 */
4343static bool guest_state_valid(struct kvm_vcpu *vcpu)
4344{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004345 if (enable_unrestricted_guest)
4346 return true;
4347
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004348 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004349 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004350 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4351 return false;
4352 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4353 return false;
4354 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4355 return false;
4356 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4357 return false;
4358 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4359 return false;
4360 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4361 return false;
4362 } else {
4363 /* protected mode guest state checks */
4364 if (!cs_ss_rpl_check(vcpu))
4365 return false;
4366 if (!code_segment_valid(vcpu))
4367 return false;
4368 if (!stack_segment_valid(vcpu))
4369 return false;
4370 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4371 return false;
4372 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4373 return false;
4374 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4375 return false;
4376 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4377 return false;
4378 if (!tr_valid(vcpu))
4379 return false;
4380 if (!ldtr_valid(vcpu))
4381 return false;
4382 }
4383 /* TODO:
4384 * - Add checks on RIP
4385 * - Add checks on RFLAGS
4386 */
4387
4388 return true;
4389}
4390
Mike Dayd77c26f2007-10-08 09:02:08 -04004391static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004393 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004394 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004395 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004397 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004398 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004399 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4400 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004401 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004402 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004403 r = kvm_write_guest_page(kvm, fn++, &data,
4404 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004405 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004406 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004407 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4408 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004409 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004410 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4411 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004412 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004413 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004414 r = kvm_write_guest_page(kvm, fn, &data,
4415 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4416 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004417out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004418 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004419 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004420}
4421
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004422static int init_rmode_identity_map(struct kvm *kvm)
4423{
Tang Chenf51770e2014-09-16 18:41:59 +08004424 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004425 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004426 u32 tmp;
4427
Avi Kivity089d0342009-03-23 18:26:32 +02004428 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004429 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004430
4431 /* Protect kvm->arch.ept_identity_pagetable_done. */
4432 mutex_lock(&kvm->slots_lock);
4433
Tang Chenf51770e2014-09-16 18:41:59 +08004434 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004435 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004436
Sheng Yangb927a3c2009-07-21 10:42:48 +08004437 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004438
4439 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004440 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004441 goto out2;
4442
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004443 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004444 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4445 if (r < 0)
4446 goto out;
4447 /* Set up identity-mapping pagetable for EPT in real mode */
4448 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4449 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4450 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4451 r = kvm_write_guest_page(kvm, identity_map_pfn,
4452 &tmp, i * sizeof(tmp), sizeof(tmp));
4453 if (r < 0)
4454 goto out;
4455 }
4456 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004457
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004458out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004459 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004460
4461out2:
4462 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004463 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004464}
4465
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466static void seg_setup(int seg)
4467{
Mathias Krause772e0312012-08-30 01:30:19 +02004468 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004469 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470
4471 vmcs_write16(sf->selector, 0);
4472 vmcs_writel(sf->base, 0);
4473 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004474 ar = 0x93;
4475 if (seg == VCPU_SREG_CS)
4476 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004477
4478 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004479}
4480
Sheng Yangf78e0e22007-10-29 09:40:42 +08004481static int alloc_apic_access_page(struct kvm *kvm)
4482{
Xiao Guangrong44841412012-09-07 14:14:20 +08004483 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004484 int r = 0;
4485
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004486 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004487 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004488 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004489 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4490 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004491 if (r)
4492 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004493
Tang Chen73a6d942014-09-11 13:38:00 +08004494 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004495 if (is_error_page(page)) {
4496 r = -EFAULT;
4497 goto out;
4498 }
4499
Tang Chenc24ae0d2014-09-24 15:57:58 +08004500 /*
4501 * Do not pin the page in memory, so that memory hot-unplug
4502 * is able to migrate it.
4503 */
4504 put_page(page);
4505 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004506out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004507 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004508 return r;
4509}
4510
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004511static int alloc_identity_pagetable(struct kvm *kvm)
4512{
Tang Chena255d472014-09-16 18:41:58 +08004513 /* Called with kvm->slots_lock held. */
4514
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004515 int r = 0;
4516
Tang Chena255d472014-09-16 18:41:58 +08004517 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4518
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004519 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4520 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004521
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004522 return r;
4523}
4524
Wanpeng Li991e7a02015-09-16 17:30:05 +08004525static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004526{
4527 int vpid;
4528
Avi Kivity919818a2009-03-23 18:01:29 +02004529 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004530 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004531 spin_lock(&vmx_vpid_lock);
4532 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004533 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004534 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004535 else
4536 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004537 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004538 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004539}
4540
Wanpeng Li991e7a02015-09-16 17:30:05 +08004541static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004542{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004543 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004544 return;
4545 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004546 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004547 spin_unlock(&vmx_vpid_lock);
4548}
4549
Yang Zhang8d146952013-01-25 10:18:50 +08004550#define MSR_TYPE_R 1
4551#define MSR_TYPE_W 2
4552static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4553 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004554{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004555 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004556
4557 if (!cpu_has_vmx_msr_bitmap())
4558 return;
4559
4560 /*
4561 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4562 * have the write-low and read-high bitmap offsets the wrong way round.
4563 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4564 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004565 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004566 if (type & MSR_TYPE_R)
4567 /* read-low */
4568 __clear_bit(msr, msr_bitmap + 0x000 / f);
4569
4570 if (type & MSR_TYPE_W)
4571 /* write-low */
4572 __clear_bit(msr, msr_bitmap + 0x800 / f);
4573
Sheng Yang25c5f222008-03-28 13:18:56 +08004574 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4575 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004576 if (type & MSR_TYPE_R)
4577 /* read-high */
4578 __clear_bit(msr, msr_bitmap + 0x400 / f);
4579
4580 if (type & MSR_TYPE_W)
4581 /* write-high */
4582 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4583
4584 }
4585}
4586
4587static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4588 u32 msr, int type)
4589{
4590 int f = sizeof(unsigned long);
4591
4592 if (!cpu_has_vmx_msr_bitmap())
4593 return;
4594
4595 /*
4596 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4597 * have the write-low and read-high bitmap offsets the wrong way round.
4598 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4599 */
4600 if (msr <= 0x1fff) {
4601 if (type & MSR_TYPE_R)
4602 /* read-low */
4603 __set_bit(msr, msr_bitmap + 0x000 / f);
4604
4605 if (type & MSR_TYPE_W)
4606 /* write-low */
4607 __set_bit(msr, msr_bitmap + 0x800 / f);
4608
4609 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4610 msr &= 0x1fff;
4611 if (type & MSR_TYPE_R)
4612 /* read-high */
4613 __set_bit(msr, msr_bitmap + 0x400 / f);
4614
4615 if (type & MSR_TYPE_W)
4616 /* write-high */
4617 __set_bit(msr, msr_bitmap + 0xc00 / f);
4618
Sheng Yang25c5f222008-03-28 13:18:56 +08004619 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004620}
4621
Wincy Vanf2b93282015-02-03 23:56:03 +08004622/*
4623 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4624 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4625 */
4626static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4627 unsigned long *msr_bitmap_nested,
4628 u32 msr, int type)
4629{
4630 int f = sizeof(unsigned long);
4631
4632 if (!cpu_has_vmx_msr_bitmap()) {
4633 WARN_ON(1);
4634 return;
4635 }
4636
4637 /*
4638 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4639 * have the write-low and read-high bitmap offsets the wrong way round.
4640 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4641 */
4642 if (msr <= 0x1fff) {
4643 if (type & MSR_TYPE_R &&
4644 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4645 /* read-low */
4646 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4647
4648 if (type & MSR_TYPE_W &&
4649 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4650 /* write-low */
4651 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4652
4653 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4654 msr &= 0x1fff;
4655 if (type & MSR_TYPE_R &&
4656 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4657 /* read-high */
4658 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4659
4660 if (type & MSR_TYPE_W &&
4661 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4662 /* write-high */
4663 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4664
4665 }
4666}
4667
Avi Kivity58972972009-02-24 22:26:47 +02004668static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4669{
4670 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004671 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4672 msr, MSR_TYPE_R | MSR_TYPE_W);
4673 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4674 msr, MSR_TYPE_R | MSR_TYPE_W);
4675}
4676
4677static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4678{
4679 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4680 msr, MSR_TYPE_R);
4681 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4682 msr, MSR_TYPE_R);
4683}
4684
4685static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4686{
4687 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4688 msr, MSR_TYPE_R);
4689 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4690 msr, MSR_TYPE_R);
4691}
4692
4693static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4694{
4695 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4696 msr, MSR_TYPE_W);
4697 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4698 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004699}
4700
Andrey Smetanind62caab2015-11-10 15:36:33 +03004701static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004702{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004703 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004704}
4705
Wincy Van705699a2015-02-03 23:58:17 +08004706static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
4707{
4708 struct vcpu_vmx *vmx = to_vmx(vcpu);
4709 int max_irr;
4710 void *vapic_page;
4711 u16 status;
4712
4713 if (vmx->nested.pi_desc &&
4714 vmx->nested.pi_pending) {
4715 vmx->nested.pi_pending = false;
4716 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4717 return 0;
4718
4719 max_irr = find_last_bit(
4720 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4721
4722 if (max_irr == 256)
4723 return 0;
4724
4725 vapic_page = kmap(vmx->nested.virtual_apic_page);
4726 if (!vapic_page) {
4727 WARN_ON(1);
4728 return -ENOMEM;
4729 }
4730 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4731 kunmap(vmx->nested.virtual_apic_page);
4732
4733 status = vmcs_read16(GUEST_INTR_STATUS);
4734 if ((u8)max_irr > ((u8)status & 0xff)) {
4735 status &= ~0xff;
4736 status |= (u8)max_irr;
4737 vmcs_write16(GUEST_INTR_STATUS, status);
4738 }
4739 }
4740 return 0;
4741}
4742
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004743static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4744{
4745#ifdef CONFIG_SMP
4746 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004747 struct vcpu_vmx *vmx = to_vmx(vcpu);
4748
4749 /*
4750 * Currently, we don't support urgent interrupt,
4751 * all interrupts are recognized as non-urgent
4752 * interrupt, so we cannot post interrupts when
4753 * 'SN' is set.
4754 *
4755 * If the vcpu is in guest mode, it means it is
4756 * running instead of being scheduled out and
4757 * waiting in the run queue, and that's the only
4758 * case when 'SN' is set currently, warning if
4759 * 'SN' is set.
4760 */
4761 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
4762
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004763 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4764 POSTED_INTR_VECTOR);
4765 return true;
4766 }
4767#endif
4768 return false;
4769}
4770
Wincy Van705699a2015-02-03 23:58:17 +08004771static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4772 int vector)
4773{
4774 struct vcpu_vmx *vmx = to_vmx(vcpu);
4775
4776 if (is_guest_mode(vcpu) &&
4777 vector == vmx->nested.posted_intr_nv) {
4778 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004779 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004780 /*
4781 * If a posted intr is not recognized by hardware,
4782 * we will accomplish it in the next vmentry.
4783 */
4784 vmx->nested.pi_pending = true;
4785 kvm_make_request(KVM_REQ_EVENT, vcpu);
4786 return 0;
4787 }
4788 return -1;
4789}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004791 * Send interrupt to vcpu via posted interrupt way.
4792 * 1. If target vcpu is running(non-root mode), send posted interrupt
4793 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4794 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4795 * interrupt from PIR in next vmentry.
4796 */
4797static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4798{
4799 struct vcpu_vmx *vmx = to_vmx(vcpu);
4800 int r;
4801
Wincy Van705699a2015-02-03 23:58:17 +08004802 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4803 if (!r)
4804 return;
4805
Yang Zhanga20ed542013-04-11 19:25:15 +08004806 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4807 return;
4808
4809 r = pi_test_and_set_on(&vmx->pi_desc);
4810 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004811 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08004812 kvm_vcpu_kick(vcpu);
4813}
4814
4815static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4816{
4817 struct vcpu_vmx *vmx = to_vmx(vcpu);
4818
4819 if (!pi_test_and_clear_on(&vmx->pi_desc))
4820 return;
4821
4822 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4823}
4824
Avi Kivity6aa8b732006-12-10 02:21:36 -08004825/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004826 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4827 * will not change in the lifetime of the guest.
4828 * Note that host-state that does change is set elsewhere. E.g., host-state
4829 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4830 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004831static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004832{
4833 u32 low32, high32;
4834 unsigned long tmpl;
4835 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004836 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004837
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004838 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004839 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4840
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004841 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004842 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004843 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4844 vmx->host_state.vmcs_host_cr4 = cr4;
4845
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004846 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004847#ifdef CONFIG_X86_64
4848 /*
4849 * Load null selectors, so we can avoid reloading them in
4850 * __vmx_load_host_state(), in case userspace uses the null selectors
4851 * too (the expected case).
4852 */
4853 vmcs_write16(HOST_DS_SELECTOR, 0);
4854 vmcs_write16(HOST_ES_SELECTOR, 0);
4855#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004856 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4857 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004858#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004859 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4860 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4861
4862 native_store_idt(&dt);
4863 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004864 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004865
Avi Kivity83287ea422012-09-16 15:10:57 +03004866 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004867
4868 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4869 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4870 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4871 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4872
4873 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4874 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4875 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4876 }
4877}
4878
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004879static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4880{
4881 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4882 if (enable_ept)
4883 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004884 if (is_guest_mode(&vmx->vcpu))
4885 vmx->vcpu.arch.cr4_guest_owned_bits &=
4886 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004887 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4888}
4889
Yang Zhang01e439b2013-04-11 19:25:12 +08004890static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4891{
4892 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4893
Andrey Smetanind62caab2015-11-10 15:36:33 +03004894 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004895 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07004896 /* Enable the preemption timer dynamically */
4897 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004898 return pin_based_exec_ctrl;
4899}
4900
Andrey Smetanind62caab2015-11-10 15:36:33 +03004901static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4902{
4903 struct vcpu_vmx *vmx = to_vmx(vcpu);
4904
4905 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004906 if (cpu_has_secondary_exec_ctrls()) {
4907 if (kvm_vcpu_apicv_active(vcpu))
4908 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4909 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4910 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4911 else
4912 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4913 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4914 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4915 }
4916
4917 if (cpu_has_vmx_msr_bitmap())
4918 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004919}
4920
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004921static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4922{
4923 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004924
4925 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4926 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4927
Paolo Bonzini35754c92015-07-29 12:05:37 +02004928 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004929 exec_control &= ~CPU_BASED_TPR_SHADOW;
4930#ifdef CONFIG_X86_64
4931 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4932 CPU_BASED_CR8_LOAD_EXITING;
4933#endif
4934 }
4935 if (!enable_ept)
4936 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4937 CPU_BASED_CR3_LOAD_EXITING |
4938 CPU_BASED_INVLPG_EXITING;
4939 return exec_control;
4940}
4941
4942static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4943{
4944 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004945 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004946 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4947 if (vmx->vpid == 0)
4948 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4949 if (!enable_ept) {
4950 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4951 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004952 /* Enable INVPCID for non-ept guests may cause performance regression. */
4953 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004954 }
4955 if (!enable_unrestricted_guest)
4956 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4957 if (!ple_gap)
4958 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03004959 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004960 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4961 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004962 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004963 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4964 (handle_vmptrld).
4965 We can NOT enable shadow_vmcs here because we don't have yet
4966 a current VMCS12
4967 */
4968 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004969
4970 if (!enable_pml)
4971 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004972
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004973 return exec_control;
4974}
4975
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004976static void ept_set_mmio_spte_mask(void)
4977{
4978 /*
4979 * EPT Misconfigurations can be generated if the value of bits 2:0
4980 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004981 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004982 * spte.
4983 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004984 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004985}
4986
Wanpeng Lif53cd632014-12-02 19:14:58 +08004987#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004988/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004989 * Sets up the vmcs for emulated real mode.
4990 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004991static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004992{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004993#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004994 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004995#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004997
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004999 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5000 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001
Abel Gordon4607c2d2013-04-18 14:35:55 +03005002 if (enable_shadow_vmcs) {
5003 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5004 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5005 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005006 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005007 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005008
Avi Kivity6aa8b732006-12-10 02:21:36 -08005009 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5010
Avi Kivity6aa8b732006-12-10 02:21:36 -08005011 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005012 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005013 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005014
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005015 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005016
Dan Williamsdfa169b2016-06-02 11:17:24 -07005017 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005018 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5019 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005020 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005021
Andrey Smetanind62caab2015-11-10 15:36:33 +03005022 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005023 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5024 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5025 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5026 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5027
5028 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005029
Li RongQing0bcf2612015-12-03 13:29:34 +08005030 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005031 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005032 }
5033
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005034 if (ple_gap) {
5035 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005036 vmx->ple_window = ple_window;
5037 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005038 }
5039
Xiao Guangrongc3707952011-07-12 03:28:04 +08005040 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5041 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5043
Avi Kivity9581d442010-10-19 16:46:55 +02005044 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5045 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005046 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005047#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048 rdmsrl(MSR_FS_BASE, a);
5049 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5050 rdmsrl(MSR_GS_BASE, a);
5051 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5052#else
5053 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5054 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5055#endif
5056
Eddie Dong2cc51562007-05-21 07:28:09 +03005057 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5058 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005059 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005060 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005061 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005062
Radim Krčmář74545702015-04-27 15:11:25 +02005063 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5064 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005065
Paolo Bonzini03916db2014-07-24 14:21:57 +02005066 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005067 u32 index = vmx_msr_index[i];
5068 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005069 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005070
5071 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5072 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005073 if (wrmsr_safe(index, data_low, data_high) < 0)
5074 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005075 vmx->guest_msrs[j].index = i;
5076 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005077 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005078 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005080
Gleb Natapov2961e8762013-11-25 15:37:13 +02005081
5082 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005083
5084 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005085 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005086
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005087 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005088 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005089
Wanpeng Lif53cd632014-12-02 19:14:58 +08005090 if (vmx_xsaves_supported())
5091 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5092
Peter Feiner4e595162016-07-07 14:49:58 -07005093 if (enable_pml) {
5094 ASSERT(vmx->pml_pg);
5095 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5096 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5097 }
5098
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005099 return 0;
5100}
5101
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005102static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005103{
5104 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005105 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005106 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005107
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005108 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005109
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005110 vmx->soft_vnmi_blocked = 0;
5111
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005112 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005113 kvm_set_cr8(vcpu, 0);
5114
5115 if (!init_event) {
5116 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5117 MSR_IA32_APICBASE_ENABLE;
5118 if (kvm_vcpu_is_reset_bsp(vcpu))
5119 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5120 apic_base_msr.host_initiated = true;
5121 kvm_set_apic_base(vcpu, &apic_base_msr);
5122 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005123
Avi Kivity2fb92db2011-04-27 19:42:18 +03005124 vmx_segment_cache_clear(vmx);
5125
Avi Kivity5706be02008-08-20 15:07:31 +03005126 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005127 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005128 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005129
5130 seg_setup(VCPU_SREG_DS);
5131 seg_setup(VCPU_SREG_ES);
5132 seg_setup(VCPU_SREG_FS);
5133 seg_setup(VCPU_SREG_GS);
5134 seg_setup(VCPU_SREG_SS);
5135
5136 vmcs_write16(GUEST_TR_SELECTOR, 0);
5137 vmcs_writel(GUEST_TR_BASE, 0);
5138 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5139 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5140
5141 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5142 vmcs_writel(GUEST_LDTR_BASE, 0);
5143 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5144 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5145
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005146 if (!init_event) {
5147 vmcs_write32(GUEST_SYSENTER_CS, 0);
5148 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5149 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5150 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5151 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005152
5153 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005154 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005155
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005156 vmcs_writel(GUEST_GDTR_BASE, 0);
5157 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5158
5159 vmcs_writel(GUEST_IDTR_BASE, 0);
5160 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5161
Anthony Liguori443381a2010-12-06 10:53:38 -06005162 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005163 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005164 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005165
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005166 setup_msrs(vmx);
5167
Avi Kivity6aa8b732006-12-10 02:21:36 -08005168 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5169
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005170 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005171 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005172 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005173 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005174 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005175 vmcs_write32(TPR_THRESHOLD, 0);
5176 }
5177
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005178 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005179
Andrey Smetanind62caab2015-11-10 15:36:33 +03005180 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005181 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5182
Sheng Yang2384d2b2008-01-17 15:14:33 +08005183 if (vmx->vpid != 0)
5184 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5185
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005186 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005187 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005188 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005189 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005190 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005191 vmx_fpu_activate(vcpu);
5192 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005193
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005194 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005195}
5196
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005197/*
5198 * In nested virtualization, check if L1 asked to exit on external interrupts.
5199 * For most existing hypervisors, this will always return true.
5200 */
5201static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5202{
5203 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5204 PIN_BASED_EXT_INTR_MASK;
5205}
5206
Bandan Das77b0f5d2014-04-19 18:17:45 -04005207/*
5208 * In nested virtualization, check if L1 has set
5209 * VM_EXIT_ACK_INTR_ON_EXIT
5210 */
5211static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5212{
5213 return get_vmcs12(vcpu)->vm_exit_controls &
5214 VM_EXIT_ACK_INTR_ON_EXIT;
5215}
5216
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005217static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5218{
5219 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5220 PIN_BASED_NMI_EXITING;
5221}
5222
Jan Kiszkac9a79532014-03-07 20:03:15 +01005223static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005224{
5225 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005226
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005227 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5228 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5229 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5230}
5231
Jan Kiszkac9a79532014-03-07 20:03:15 +01005232static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005233{
5234 u32 cpu_based_vm_exec_control;
5235
Jan Kiszkac9a79532014-03-07 20:03:15 +01005236 if (!cpu_has_virtual_nmis() ||
5237 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5238 enable_irq_window(vcpu);
5239 return;
5240 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005241
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005242 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5243 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5244 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5245}
5246
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005247static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005248{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005249 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005250 uint32_t intr;
5251 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005252
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005253 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005254
Avi Kivityfa89a812008-09-01 15:57:51 +03005255 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005256 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005257 int inc_eip = 0;
5258 if (vcpu->arch.interrupt.soft)
5259 inc_eip = vcpu->arch.event_exit_inst_len;
5260 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005261 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005262 return;
5263 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005264 intr = irq | INTR_INFO_VALID_MASK;
5265 if (vcpu->arch.interrupt.soft) {
5266 intr |= INTR_TYPE_SOFT_INTR;
5267 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5268 vmx->vcpu.arch.event_exit_inst_len);
5269 } else
5270 intr |= INTR_TYPE_EXT_INTR;
5271 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005272}
5273
Sheng Yangf08864b2008-05-15 18:23:25 +08005274static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5275{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005276 struct vcpu_vmx *vmx = to_vmx(vcpu);
5277
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005278 if (is_guest_mode(vcpu))
5279 return;
5280
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005281 if (!cpu_has_virtual_nmis()) {
5282 /*
5283 * Tracking the NMI-blocked state in software is built upon
5284 * finding the next open IRQ window. This, in turn, depends on
5285 * well-behaving guests: They have to keep IRQs disabled at
5286 * least as long as the NMI handler runs. Otherwise we may
5287 * cause NMI nesting, maybe breaking the guest. But as this is
5288 * highly unlikely, we can live with the residual risk.
5289 */
5290 vmx->soft_vnmi_blocked = 1;
5291 vmx->vnmi_blocked_time = 0;
5292 }
5293
Jan Kiszka487b3912008-09-26 09:30:56 +02005294 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02005295 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005296 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005297 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005298 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005299 return;
5300 }
Sheng Yangf08864b2008-05-15 18:23:25 +08005301 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5302 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005303}
5304
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005305static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5306{
5307 if (!cpu_has_virtual_nmis())
5308 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005309 if (to_vmx(vcpu)->nmi_known_unmasked)
5310 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005311 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005312}
5313
5314static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5315{
5316 struct vcpu_vmx *vmx = to_vmx(vcpu);
5317
5318 if (!cpu_has_virtual_nmis()) {
5319 if (vmx->soft_vnmi_blocked != masked) {
5320 vmx->soft_vnmi_blocked = masked;
5321 vmx->vnmi_blocked_time = 0;
5322 }
5323 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005324 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005325 if (masked)
5326 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5327 GUEST_INTR_STATE_NMI);
5328 else
5329 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5330 GUEST_INTR_STATE_NMI);
5331 }
5332}
5333
Jan Kiszka2505dc92013-04-14 12:12:47 +02005334static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5335{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005336 if (to_vmx(vcpu)->nested.nested_run_pending)
5337 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005338
Jan Kiszka2505dc92013-04-14 12:12:47 +02005339 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5340 return 0;
5341
5342 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5343 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5344 | GUEST_INTR_STATE_NMI));
5345}
5346
Gleb Natapov78646122009-03-23 12:12:11 +02005347static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5348{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005349 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5350 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005351 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5352 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005353}
5354
Izik Eiduscbc94022007-10-25 00:29:55 +02005355static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5356{
5357 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005358
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005359 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5360 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005361 if (ret)
5362 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005363 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005364 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005365}
5366
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005367static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005368{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005369 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005370 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005371 /*
5372 * Update instruction length as we may reinject the exception
5373 * from user space while in guest debugging mode.
5374 */
5375 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5376 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005377 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005378 return false;
5379 /* fall through */
5380 case DB_VECTOR:
5381 if (vcpu->guest_debug &
5382 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5383 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005384 /* fall through */
5385 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005386 case OF_VECTOR:
5387 case BR_VECTOR:
5388 case UD_VECTOR:
5389 case DF_VECTOR:
5390 case SS_VECTOR:
5391 case GP_VECTOR:
5392 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005393 return true;
5394 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005395 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005396 return false;
5397}
5398
5399static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5400 int vec, u32 err_code)
5401{
5402 /*
5403 * Instruction with address size override prefix opcode 0x67
5404 * Cause the #SS fault with 0 error code in VM86 mode.
5405 */
5406 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5407 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5408 if (vcpu->arch.halt_request) {
5409 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005410 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005411 }
5412 return 1;
5413 }
5414 return 0;
5415 }
5416
5417 /*
5418 * Forward all other exceptions that are valid in real mode.
5419 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5420 * the required debugging infrastructure rework.
5421 */
5422 kvm_queue_exception(vcpu, vec);
5423 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005424}
5425
Andi Kleena0861c02009-06-08 17:37:09 +08005426/*
5427 * Trigger machine check on the host. We assume all the MSRs are already set up
5428 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5429 * We pass a fake environment to the machine check handler because we want
5430 * the guest to be always treated like user space, no matter what context
5431 * it used internally.
5432 */
5433static void kvm_machine_check(void)
5434{
5435#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5436 struct pt_regs regs = {
5437 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5438 .flags = X86_EFLAGS_IF,
5439 };
5440
5441 do_machine_check(&regs, 0);
5442#endif
5443}
5444
Avi Kivity851ba692009-08-24 11:10:17 +03005445static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005446{
5447 /* already handled by vcpu_run */
5448 return 1;
5449}
5450
Avi Kivity851ba692009-08-24 11:10:17 +03005451static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452{
Avi Kivity1155f762007-11-22 11:30:47 +02005453 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005454 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005455 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005456 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457 u32 vect_info;
5458 enum emulation_result er;
5459
Avi Kivity1155f762007-11-22 11:30:47 +02005460 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005461 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005462
Andi Kleena0861c02009-06-08 17:37:09 +08005463 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005464 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005465
Jan Kiszkae4a41882008-09-26 09:30:46 +02005466 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02005467 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005468
5469 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005470 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005471 return 1;
5472 }
5473
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005474 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005475 if (is_guest_mode(vcpu)) {
5476 kvm_queue_exception(vcpu, UD_VECTOR);
5477 return 1;
5478 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005479 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005480 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005481 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005482 return 1;
5483 }
5484
Avi Kivity6aa8b732006-12-10 02:21:36 -08005485 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005486 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005487 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005488
5489 /*
5490 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5491 * MMIO, it is better to report an internal error.
5492 * See the comments in vmx_handle_exit.
5493 */
5494 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5495 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5496 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5497 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005498 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005499 vcpu->run->internal.data[0] = vect_info;
5500 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005501 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005502 return 0;
5503 }
5504
Avi Kivity6aa8b732006-12-10 02:21:36 -08005505 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005506 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005507 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005508 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005509 trace_kvm_page_fault(cr2, error_code);
5510
Gleb Natapov3298b752009-05-11 13:35:46 +03005511 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005512 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005513 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005514 }
5515
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005516 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005517
5518 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5519 return handle_rmode_exception(vcpu, ex_no, error_code);
5520
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005521 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005522 case AC_VECTOR:
5523 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5524 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005525 case DB_VECTOR:
5526 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5527 if (!(vcpu->guest_debug &
5528 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005529 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005530 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005531 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5532 skip_emulated_instruction(vcpu);
5533
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005534 kvm_queue_exception(vcpu, DB_VECTOR);
5535 return 1;
5536 }
5537 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5538 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5539 /* fall through */
5540 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005541 /*
5542 * Update instruction length as we may reinject #BP from
5543 * user space while in guest debugging mode. Reading it for
5544 * #DB as well causes no harm, it is not used in that case.
5545 */
5546 vmx->vcpu.arch.event_exit_inst_len =
5547 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005548 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005549 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005550 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5551 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005552 break;
5553 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005554 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5555 kvm_run->ex.exception = ex_no;
5556 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005557 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005559 return 0;
5560}
5561
Avi Kivity851ba692009-08-24 11:10:17 +03005562static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005563{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005564 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005565 return 1;
5566}
5567
Avi Kivity851ba692009-08-24 11:10:17 +03005568static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005569{
Avi Kivity851ba692009-08-24 11:10:17 +03005570 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005571 return 0;
5572}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573
Avi Kivity851ba692009-08-24 11:10:17 +03005574static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005575{
He, Qingbfdaab02007-09-12 14:18:28 +08005576 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005577 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005578 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005579
He, Qingbfdaab02007-09-12 14:18:28 +08005580 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005581 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005582 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005583
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005584 ++vcpu->stat.io_exits;
5585
5586 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005587 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005588
5589 port = exit_qualification >> 16;
5590 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005591 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005592
5593 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005594}
5595
Ingo Molnar102d8322007-02-19 14:37:47 +02005596static void
5597vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5598{
5599 /*
5600 * Patch in the VMCALL instruction:
5601 */
5602 hypercall[0] = 0x0f;
5603 hypercall[1] = 0x01;
5604 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005605}
5606
Wincy Vanb9c237b2015-02-03 23:56:30 +08005607static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005608{
5609 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005610 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005611
Wincy Vanb9c237b2015-02-03 23:56:30 +08005612 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005613 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5614 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5615 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5616 return (val & always_on) == always_on;
5617}
5618
Guo Chao0fa06072012-06-28 15:16:19 +08005619/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005620static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5621{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005622 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005623 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5624 unsigned long orig_val = val;
5625
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005626 /*
5627 * We get here when L2 changed cr0 in a way that did not change
5628 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005629 * but did change L0 shadowed bits. So we first calculate the
5630 * effective cr0 value that L1 would like to write into the
5631 * hardware. It consists of the L2-owned bits from the new
5632 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005633 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005634 val = (val & ~vmcs12->cr0_guest_host_mask) |
5635 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5636
Wincy Vanb9c237b2015-02-03 23:56:30 +08005637 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005638 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005639
5640 if (kvm_set_cr0(vcpu, val))
5641 return 1;
5642 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005643 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005644 } else {
5645 if (to_vmx(vcpu)->nested.vmxon &&
5646 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5647 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005648 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005649 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005650}
5651
5652static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5653{
5654 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005655 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5656 unsigned long orig_val = val;
5657
5658 /* analogously to handle_set_cr0 */
5659 val = (val & ~vmcs12->cr4_guest_host_mask) |
5660 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5661 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005662 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005663 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005664 return 0;
5665 } else
5666 return kvm_set_cr4(vcpu, val);
5667}
5668
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005669/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005670static void handle_clts(struct kvm_vcpu *vcpu)
5671{
5672 if (is_guest_mode(vcpu)) {
5673 /*
5674 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5675 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5676 * just pretend it's off (also in arch.cr0 for fpu_activate).
5677 */
5678 vmcs_writel(CR0_READ_SHADOW,
5679 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5680 vcpu->arch.cr0 &= ~X86_CR0_TS;
5681 } else
5682 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5683}
5684
Avi Kivity851ba692009-08-24 11:10:17 +03005685static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005686{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005687 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005688 int cr;
5689 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005690 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005691
He, Qingbfdaab02007-09-12 14:18:28 +08005692 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005693 cr = exit_qualification & 15;
5694 reg = (exit_qualification >> 8) & 15;
5695 switch ((exit_qualification >> 4) & 3) {
5696 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005697 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005698 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005699 switch (cr) {
5700 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005701 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005702 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005703 return 1;
5704 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005705 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005706 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005707 return 1;
5708 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005709 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005710 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005711 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005712 case 8: {
5713 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005714 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005715 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005716 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005717 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005718 return 1;
5719 if (cr8_prev <= cr8)
5720 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005721 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005722 return 0;
5723 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005724 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005725 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005726 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005727 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005728 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005729 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005730 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005731 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005732 case 1: /*mov from cr*/
5733 switch (cr) {
5734 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005735 val = kvm_read_cr3(vcpu);
5736 kvm_register_write(vcpu, reg, val);
5737 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005738 skip_emulated_instruction(vcpu);
5739 return 1;
5740 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005741 val = kvm_get_cr8(vcpu);
5742 kvm_register_write(vcpu, reg, val);
5743 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005744 skip_emulated_instruction(vcpu);
5745 return 1;
5746 }
5747 break;
5748 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005749 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005750 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005751 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005752
5753 skip_emulated_instruction(vcpu);
5754 return 1;
5755 default:
5756 break;
5757 }
Avi Kivity851ba692009-08-24 11:10:17 +03005758 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005759 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005760 (int)(exit_qualification >> 4) & 3, cr);
5761 return 0;
5762}
5763
Avi Kivity851ba692009-08-24 11:10:17 +03005764static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005765{
He, Qingbfdaab02007-09-12 14:18:28 +08005766 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005767 int dr, dr7, reg;
5768
5769 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5770 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5771
5772 /* First, if DR does not exist, trigger UD */
5773 if (!kvm_require_dr(vcpu, dr))
5774 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005775
Jan Kiszkaf2483412010-01-20 18:20:20 +01005776 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005777 if (!kvm_require_cpl(vcpu, 0))
5778 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005779 dr7 = vmcs_readl(GUEST_DR7);
5780 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005781 /*
5782 * As the vm-exit takes precedence over the debug trap, we
5783 * need to emulate the latter, either for the host or the
5784 * guest debugging itself.
5785 */
5786 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005787 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005788 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005789 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005790 vcpu->run->debug.arch.exception = DB_VECTOR;
5791 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005792 return 0;
5793 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005794 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005795 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005796 kvm_queue_exception(vcpu, DB_VECTOR);
5797 return 1;
5798 }
5799 }
5800
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005801 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01005802 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5803 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005804
5805 /*
5806 * No more DR vmexits; force a reload of the debug registers
5807 * and reenter on this instruction. The next vmexit will
5808 * retrieve the full state of the debug registers.
5809 */
5810 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5811 return 1;
5812 }
5813
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005814 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5815 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005816 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005817
5818 if (kvm_get_dr(vcpu, dr, &val))
5819 return 1;
5820 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005821 } else
Nadav Amit57773922014-06-18 17:19:23 +03005822 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005823 return 1;
5824
Avi Kivity6aa8b732006-12-10 02:21:36 -08005825 skip_emulated_instruction(vcpu);
5826 return 1;
5827}
5828
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005829static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5830{
5831 return vcpu->arch.dr6;
5832}
5833
5834static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5835{
5836}
5837
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005838static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5839{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005840 get_debugreg(vcpu->arch.db[0], 0);
5841 get_debugreg(vcpu->arch.db[1], 1);
5842 get_debugreg(vcpu->arch.db[2], 2);
5843 get_debugreg(vcpu->arch.db[3], 3);
5844 get_debugreg(vcpu->arch.dr6, 6);
5845 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5846
5847 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01005848 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005849}
5850
Gleb Natapov020df072010-04-13 10:05:23 +03005851static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5852{
5853 vmcs_writel(GUEST_DR7, val);
5854}
5855
Avi Kivity851ba692009-08-24 11:10:17 +03005856static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005857{
Avi Kivity06465c52007-02-28 20:46:53 +02005858 kvm_emulate_cpuid(vcpu);
5859 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005860}
5861
Avi Kivity851ba692009-08-24 11:10:17 +03005862static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005863{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005864 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005865 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005866
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005867 msr_info.index = ecx;
5868 msr_info.host_initiated = false;
5869 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005870 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005871 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005872 return 1;
5873 }
5874
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005875 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005876
Avi Kivity6aa8b732006-12-10 02:21:36 -08005877 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005878 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5879 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005880 skip_emulated_instruction(vcpu);
5881 return 1;
5882}
5883
Avi Kivity851ba692009-08-24 11:10:17 +03005884static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005885{
Will Auld8fe8ab42012-11-29 12:42:12 -08005886 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005887 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5888 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5889 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005890
Will Auld8fe8ab42012-11-29 12:42:12 -08005891 msr.data = data;
5892 msr.index = ecx;
5893 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005894 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005895 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005896 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005897 return 1;
5898 }
5899
Avi Kivity59200272010-01-25 19:47:02 +02005900 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005901 skip_emulated_instruction(vcpu);
5902 return 1;
5903}
5904
Avi Kivity851ba692009-08-24 11:10:17 +03005905static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005906{
Avi Kivity3842d132010-07-27 12:30:24 +03005907 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005908 return 1;
5909}
5910
Avi Kivity851ba692009-08-24 11:10:17 +03005911static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005912{
Eddie Dong85f455f2007-07-06 12:20:49 +03005913 u32 cpu_based_vm_exec_control;
5914
5915 /* clear pending irq */
5916 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5917 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5918 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005919
Avi Kivity3842d132010-07-27 12:30:24 +03005920 kvm_make_request(KVM_REQ_EVENT, vcpu);
5921
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005922 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005923 return 1;
5924}
5925
Avi Kivity851ba692009-08-24 11:10:17 +03005926static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005927{
Avi Kivityd3bef152007-06-05 15:53:05 +03005928 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005929}
5930
Avi Kivity851ba692009-08-24 11:10:17 +03005931static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005932{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005933 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005934}
5935
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005936static int handle_invd(struct kvm_vcpu *vcpu)
5937{
Andre Przywara51d8b662010-12-21 11:12:02 +01005938 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005939}
5940
Avi Kivity851ba692009-08-24 11:10:17 +03005941static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005942{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005943 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005944
5945 kvm_mmu_invlpg(vcpu, exit_qualification);
5946 skip_emulated_instruction(vcpu);
5947 return 1;
5948}
5949
Avi Kivityfee84b02011-11-10 14:57:25 +02005950static int handle_rdpmc(struct kvm_vcpu *vcpu)
5951{
5952 int err;
5953
5954 err = kvm_rdpmc(vcpu);
5955 kvm_complete_insn_gp(vcpu, err);
5956
5957 return 1;
5958}
5959
Avi Kivity851ba692009-08-24 11:10:17 +03005960static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005961{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005962 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005963 return 1;
5964}
5965
Dexuan Cui2acf9232010-06-10 11:27:12 +08005966static int handle_xsetbv(struct kvm_vcpu *vcpu)
5967{
5968 u64 new_bv = kvm_read_edx_eax(vcpu);
5969 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5970
5971 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5972 skip_emulated_instruction(vcpu);
5973 return 1;
5974}
5975
Wanpeng Lif53cd632014-12-02 19:14:58 +08005976static int handle_xsaves(struct kvm_vcpu *vcpu)
5977{
5978 skip_emulated_instruction(vcpu);
5979 WARN(1, "this should never happen\n");
5980 return 1;
5981}
5982
5983static int handle_xrstors(struct kvm_vcpu *vcpu)
5984{
5985 skip_emulated_instruction(vcpu);
5986 WARN(1, "this should never happen\n");
5987 return 1;
5988}
5989
Avi Kivity851ba692009-08-24 11:10:17 +03005990static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005991{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005992 if (likely(fasteoi)) {
5993 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5994 int access_type, offset;
5995
5996 access_type = exit_qualification & APIC_ACCESS_TYPE;
5997 offset = exit_qualification & APIC_ACCESS_OFFSET;
5998 /*
5999 * Sane guest uses MOV to write EOI, with written value
6000 * not cared. So make a short-circuit here by avoiding
6001 * heavy instruction emulation.
6002 */
6003 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6004 (offset == APIC_EOI)) {
6005 kvm_lapic_set_eoi(vcpu);
6006 skip_emulated_instruction(vcpu);
6007 return 1;
6008 }
6009 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006010 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006011}
6012
Yang Zhangc7c9c562013-01-25 10:18:51 +08006013static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6014{
6015 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6016 int vector = exit_qualification & 0xff;
6017
6018 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6019 kvm_apic_set_eoi_accelerated(vcpu, vector);
6020 return 1;
6021}
6022
Yang Zhang83d4c282013-01-25 10:18:49 +08006023static int handle_apic_write(struct kvm_vcpu *vcpu)
6024{
6025 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6026 u32 offset = exit_qualification & 0xfff;
6027
6028 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6029 kvm_apic_write_nodecode(vcpu, offset);
6030 return 1;
6031}
6032
Avi Kivity851ba692009-08-24 11:10:17 +03006033static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006034{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006035 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006036 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006037 bool has_error_code = false;
6038 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006039 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006040 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006041
6042 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006043 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006044 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006045
6046 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6047
6048 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006049 if (reason == TASK_SWITCH_GATE && idt_v) {
6050 switch (type) {
6051 case INTR_TYPE_NMI_INTR:
6052 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006053 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006054 break;
6055 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006056 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006057 kvm_clear_interrupt_queue(vcpu);
6058 break;
6059 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006060 if (vmx->idt_vectoring_info &
6061 VECTORING_INFO_DELIVER_CODE_MASK) {
6062 has_error_code = true;
6063 error_code =
6064 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6065 }
6066 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006067 case INTR_TYPE_SOFT_EXCEPTION:
6068 kvm_clear_exception_queue(vcpu);
6069 break;
6070 default:
6071 break;
6072 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006073 }
Izik Eidus37817f22008-03-24 23:14:53 +02006074 tss_selector = exit_qualification;
6075
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006076 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6077 type != INTR_TYPE_EXT_INTR &&
6078 type != INTR_TYPE_NMI_INTR))
6079 skip_emulated_instruction(vcpu);
6080
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006081 if (kvm_task_switch(vcpu, tss_selector,
6082 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6083 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006084 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6085 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6086 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006087 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006088 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006089
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006090 /*
6091 * TODO: What about debug traps on tss switch?
6092 * Are we supposed to inject them and update dr6?
6093 */
6094
6095 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006096}
6097
Avi Kivity851ba692009-08-24 11:10:17 +03006098static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006099{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006100 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006101 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006102 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006103 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006104
Sheng Yangf9c617f2009-03-25 10:08:52 +08006105 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006106
Sheng Yang14394422008-04-28 12:24:45 +08006107 gla_validity = (exit_qualification >> 7) & 0x3;
6108 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
6109 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6110 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6111 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006112 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006113 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6114 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006115 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6116 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006117 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006118 }
6119
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006120 /*
6121 * EPT violation happened while executing iret from NMI,
6122 * "blocked by NMI" bit has to be set before next VM entry.
6123 * There are errata that may cause this bit to not be set:
6124 * AAK134, BY25.
6125 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006126 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6127 cpu_has_virtual_nmis() &&
6128 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006129 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6130
Sheng Yang14394422008-04-28 12:24:45 +08006131 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006132 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006133
Bandan Dasd95c5562016-07-12 18:18:51 -04006134 /* it is a read fault? */
6135 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6136 /* it is a write fault? */
6137 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006138 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006139 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006140 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006141 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006142
Yang Zhang25d92082013-08-06 12:00:32 +03006143 vcpu->arch.exit_qualification = exit_qualification;
6144
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006145 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006146}
6147
Avi Kivity851ba692009-08-24 11:10:17 +03006148static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006149{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006150 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006151 gpa_t gpa;
6152
6153 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006154 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006155 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006156 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006157 return 1;
6158 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006159
Paolo Bonzini450869d2015-11-04 13:41:21 +01006160 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006161 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006162 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6163 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006164
6165 if (unlikely(ret == RET_MMIO_PF_INVALID))
6166 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6167
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006168 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006169 return 1;
6170
6171 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006172 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006173
Avi Kivity851ba692009-08-24 11:10:17 +03006174 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6175 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006176
6177 return 0;
6178}
6179
Avi Kivity851ba692009-08-24 11:10:17 +03006180static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006181{
6182 u32 cpu_based_vm_exec_control;
6183
6184 /* clear pending NMI */
6185 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6186 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6187 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6188 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006189 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006190
6191 return 1;
6192}
6193
Mohammed Gamal80ced182009-09-01 12:48:18 +02006194static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006195{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006196 struct vcpu_vmx *vmx = to_vmx(vcpu);
6197 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006198 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006199 u32 cpu_exec_ctrl;
6200 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006201 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006202
6203 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6204 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006205
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006206 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006207 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006208 return handle_interrupt_window(&vmx->vcpu);
6209
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006210 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6211 return 1;
6212
Gleb Natapov991eebf2013-04-11 12:10:51 +03006213 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006214
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006215 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006216 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006217 ret = 0;
6218 goto out;
6219 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006220
Avi Kivityde5f70e2012-06-12 20:22:28 +03006221 if (err != EMULATE_DONE) {
6222 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6223 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6224 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006225 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006226 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006227
Gleb Natapov8d76c492013-05-08 18:38:44 +03006228 if (vcpu->arch.halt_request) {
6229 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006230 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006231 goto out;
6232 }
6233
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006234 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006235 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006236 if (need_resched())
6237 schedule();
6238 }
6239
Mohammed Gamal80ced182009-09-01 12:48:18 +02006240out:
6241 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006242}
6243
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006244static int __grow_ple_window(int val)
6245{
6246 if (ple_window_grow < 1)
6247 return ple_window;
6248
6249 val = min(val, ple_window_actual_max);
6250
6251 if (ple_window_grow < ple_window)
6252 val *= ple_window_grow;
6253 else
6254 val += ple_window_grow;
6255
6256 return val;
6257}
6258
6259static int __shrink_ple_window(int val, int modifier, int minimum)
6260{
6261 if (modifier < 1)
6262 return ple_window;
6263
6264 if (modifier < ple_window)
6265 val /= modifier;
6266 else
6267 val -= modifier;
6268
6269 return max(val, minimum);
6270}
6271
6272static void grow_ple_window(struct kvm_vcpu *vcpu)
6273{
6274 struct vcpu_vmx *vmx = to_vmx(vcpu);
6275 int old = vmx->ple_window;
6276
6277 vmx->ple_window = __grow_ple_window(old);
6278
6279 if (vmx->ple_window != old)
6280 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006281
6282 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006283}
6284
6285static void shrink_ple_window(struct kvm_vcpu *vcpu)
6286{
6287 struct vcpu_vmx *vmx = to_vmx(vcpu);
6288 int old = vmx->ple_window;
6289
6290 vmx->ple_window = __shrink_ple_window(old,
6291 ple_window_shrink, ple_window);
6292
6293 if (vmx->ple_window != old)
6294 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006295
6296 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006297}
6298
6299/*
6300 * ple_window_actual_max is computed to be one grow_ple_window() below
6301 * ple_window_max. (See __grow_ple_window for the reason.)
6302 * This prevents overflows, because ple_window_max is int.
6303 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6304 * this process.
6305 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6306 */
6307static void update_ple_window_actual_max(void)
6308{
6309 ple_window_actual_max =
6310 __shrink_ple_window(max(ple_window_max, ple_window),
6311 ple_window_grow, INT_MIN);
6312}
6313
Feng Wubf9f6ac2015-09-18 22:29:55 +08006314/*
6315 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6316 */
6317static void wakeup_handler(void)
6318{
6319 struct kvm_vcpu *vcpu;
6320 int cpu = smp_processor_id();
6321
6322 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6323 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6324 blocked_vcpu_list) {
6325 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6326
6327 if (pi_test_on(pi_desc) == 1)
6328 kvm_vcpu_kick(vcpu);
6329 }
6330 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6331}
6332
Tiejun Chenf2c76482014-10-28 10:14:47 +08006333static __init int hardware_setup(void)
6334{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006335 int r = -ENOMEM, i, msr;
6336
6337 rdmsrl_safe(MSR_EFER, &host_efer);
6338
6339 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6340 kvm_define_shared_msr(i, vmx_msr_index[i]);
6341
6342 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6343 if (!vmx_io_bitmap_a)
6344 return r;
6345
6346 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6347 if (!vmx_io_bitmap_b)
6348 goto out;
6349
6350 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
6351 if (!vmx_msr_bitmap_legacy)
6352 goto out1;
6353
6354 vmx_msr_bitmap_legacy_x2apic =
6355 (unsigned long *)__get_free_page(GFP_KERNEL);
6356 if (!vmx_msr_bitmap_legacy_x2apic)
6357 goto out2;
6358
6359 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
6360 if (!vmx_msr_bitmap_longmode)
6361 goto out3;
6362
6363 vmx_msr_bitmap_longmode_x2apic =
6364 (unsigned long *)__get_free_page(GFP_KERNEL);
6365 if (!vmx_msr_bitmap_longmode_x2apic)
6366 goto out4;
Wincy Van3af18d92015-02-03 23:49:31 +08006367
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006368 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6369 if (!vmx_vmread_bitmap)
Wincy Van3af18d92015-02-03 23:49:31 +08006370 goto out6;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006371
6372 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6373 if (!vmx_vmwrite_bitmap)
Wincy Van3af18d92015-02-03 23:49:31 +08006374 goto out7;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006375
6376 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6377 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6378
6379 /*
6380 * Allow direct access to the PC debug port (it is often used for I/O
6381 * delays, but the vmexits simply slow things down).
6382 */
6383 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6384 clear_bit(0x80, vmx_io_bitmap_a);
6385
6386 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6387
6388 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6389 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6390
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006391 if (setup_vmcs_config(&vmcs_config) < 0) {
6392 r = -EIO;
Wincy Van3af18d92015-02-03 23:49:31 +08006393 goto out8;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006394 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006395
6396 if (boot_cpu_has(X86_FEATURE_NX))
6397 kvm_enable_efer_bits(EFER_NX);
6398
6399 if (!cpu_has_vmx_vpid())
6400 enable_vpid = 0;
6401 if (!cpu_has_vmx_shadow_vmcs())
6402 enable_shadow_vmcs = 0;
6403 if (enable_shadow_vmcs)
6404 init_vmcs_shadow_fields();
6405
6406 if (!cpu_has_vmx_ept() ||
6407 !cpu_has_vmx_ept_4levels()) {
6408 enable_ept = 0;
6409 enable_unrestricted_guest = 0;
6410 enable_ept_ad_bits = 0;
6411 }
6412
6413 if (!cpu_has_vmx_ept_ad_bits())
6414 enable_ept_ad_bits = 0;
6415
6416 if (!cpu_has_vmx_unrestricted_guest())
6417 enable_unrestricted_guest = 0;
6418
Paolo Bonziniad15a292015-01-30 16:18:49 +01006419 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006420 flexpriority_enabled = 0;
6421
Paolo Bonziniad15a292015-01-30 16:18:49 +01006422 /*
6423 * set_apic_access_page_addr() is used to reload apic access
6424 * page upon invalidation. No need to do anything if not
6425 * using the APIC_ACCESS_ADDR VMCS field.
6426 */
6427 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006428 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006429
6430 if (!cpu_has_vmx_tpr_shadow())
6431 kvm_x86_ops->update_cr8_intercept = NULL;
6432
6433 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6434 kvm_disable_largepages();
6435
6436 if (!cpu_has_vmx_ple())
6437 ple_gap = 0;
6438
6439 if (!cpu_has_vmx_apicv())
6440 enable_apicv = 0;
6441
Haozhong Zhang64903d62015-10-20 15:39:09 +08006442 if (cpu_has_vmx_tsc_scaling()) {
6443 kvm_has_tsc_control = true;
6444 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6445 kvm_tsc_scaling_ratio_frac_bits = 48;
6446 }
6447
Tiejun Chenbaa03522014-12-23 16:21:11 +08006448 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6449 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6450 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6451 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6452 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6453 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6454 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6455
6456 memcpy(vmx_msr_bitmap_legacy_x2apic,
6457 vmx_msr_bitmap_legacy, PAGE_SIZE);
6458 memcpy(vmx_msr_bitmap_longmode_x2apic,
6459 vmx_msr_bitmap_longmode, PAGE_SIZE);
6460
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006461 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6462
Roman Kagan3ce424e2016-05-18 17:48:20 +03006463 for (msr = 0x800; msr <= 0x8ff; msr++)
6464 vmx_disable_intercept_msr_read_x2apic(msr);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006465
Roman Kagan3ce424e2016-05-18 17:48:20 +03006466 /* TMCCT */
6467 vmx_enable_intercept_msr_read_x2apic(0x839);
6468 /* TPR */
6469 vmx_disable_intercept_msr_write_x2apic(0x808);
6470 /* EOI */
6471 vmx_disable_intercept_msr_write_x2apic(0x80b);
6472 /* SELF-IPI */
6473 vmx_disable_intercept_msr_write_x2apic(0x83f);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006474
6475 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006476 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006477 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6478 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006479 0ull, VMX_EPT_EXECUTABLE_MASK,
6480 cpu_has_vmx_ept_execute_only() ?
6481 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006482 ept_set_mmio_spte_mask();
6483 kvm_enable_tdp();
6484 } else
6485 kvm_disable_tdp();
6486
6487 update_ple_window_actual_max();
6488
Kai Huang843e4332015-01-28 10:54:28 +08006489 /*
6490 * Only enable PML when hardware supports PML feature, and both EPT
6491 * and EPT A/D bit features are enabled -- PML depends on them to work.
6492 */
6493 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6494 enable_pml = 0;
6495
6496 if (!enable_pml) {
6497 kvm_x86_ops->slot_enable_log_dirty = NULL;
6498 kvm_x86_ops->slot_disable_log_dirty = NULL;
6499 kvm_x86_ops->flush_log_dirty = NULL;
6500 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6501 }
6502
Yunhong Jiang64672c92016-06-13 14:19:59 -07006503 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6504 u64 vmx_msr;
6505
6506 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6507 cpu_preemption_timer_multi =
6508 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6509 } else {
6510 kvm_x86_ops->set_hv_timer = NULL;
6511 kvm_x86_ops->cancel_hv_timer = NULL;
6512 }
6513
Feng Wubf9f6ac2015-09-18 22:29:55 +08006514 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6515
Ashok Rajc45dcc72016-06-22 14:59:56 +08006516 kvm_mce_cap_supported |= MCG_LMCE_P;
6517
Tiejun Chenf2c76482014-10-28 10:14:47 +08006518 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006519
Wincy Van3af18d92015-02-03 23:49:31 +08006520out8:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006521 free_page((unsigned long)vmx_vmwrite_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006522out7:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006523 free_page((unsigned long)vmx_vmread_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006524out6:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006525 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6526out4:
6527 free_page((unsigned long)vmx_msr_bitmap_longmode);
6528out3:
6529 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6530out2:
6531 free_page((unsigned long)vmx_msr_bitmap_legacy);
6532out1:
6533 free_page((unsigned long)vmx_io_bitmap_b);
6534out:
6535 free_page((unsigned long)vmx_io_bitmap_a);
6536
6537 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006538}
6539
6540static __exit void hardware_unsetup(void)
6541{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006542 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6543 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6544 free_page((unsigned long)vmx_msr_bitmap_legacy);
6545 free_page((unsigned long)vmx_msr_bitmap_longmode);
6546 free_page((unsigned long)vmx_io_bitmap_b);
6547 free_page((unsigned long)vmx_io_bitmap_a);
6548 free_page((unsigned long)vmx_vmwrite_bitmap);
6549 free_page((unsigned long)vmx_vmread_bitmap);
6550
Tiejun Chenf2c76482014-10-28 10:14:47 +08006551 free_kvm_area();
6552}
6553
Avi Kivity6aa8b732006-12-10 02:21:36 -08006554/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006555 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6556 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6557 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006558static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006559{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006560 if (ple_gap)
6561 grow_ple_window(vcpu);
6562
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006563 skip_emulated_instruction(vcpu);
6564 kvm_vcpu_on_spin(vcpu);
6565
6566 return 1;
6567}
6568
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006569static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006570{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006571 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006572 return 1;
6573}
6574
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006575static int handle_mwait(struct kvm_vcpu *vcpu)
6576{
6577 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6578 return handle_nop(vcpu);
6579}
6580
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006581static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6582{
6583 return 1;
6584}
6585
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006586static int handle_monitor(struct kvm_vcpu *vcpu)
6587{
6588 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6589 return handle_nop(vcpu);
6590}
6591
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006592/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006593 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6594 * We could reuse a single VMCS for all the L2 guests, but we also want the
6595 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6596 * allows keeping them loaded on the processor, and in the future will allow
6597 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6598 * every entry if they never change.
6599 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6600 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6601 *
6602 * The following functions allocate and free a vmcs02 in this pool.
6603 */
6604
6605/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6606static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6607{
6608 struct vmcs02_list *item;
6609 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6610 if (item->vmptr == vmx->nested.current_vmptr) {
6611 list_move(&item->list, &vmx->nested.vmcs02_pool);
6612 return &item->vmcs02;
6613 }
6614
6615 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6616 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08006617 item = list_last_entry(&vmx->nested.vmcs02_pool,
6618 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006619 item->vmptr = vmx->nested.current_vmptr;
6620 list_move(&item->list, &vmx->nested.vmcs02_pool);
6621 return &item->vmcs02;
6622 }
6623
6624 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006625 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006626 if (!item)
6627 return NULL;
6628 item->vmcs02.vmcs = alloc_vmcs();
6629 if (!item->vmcs02.vmcs) {
6630 kfree(item);
6631 return NULL;
6632 }
6633 loaded_vmcs_init(&item->vmcs02);
6634 item->vmptr = vmx->nested.current_vmptr;
6635 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6636 vmx->nested.vmcs02_num++;
6637 return &item->vmcs02;
6638}
6639
6640/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6641static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6642{
6643 struct vmcs02_list *item;
6644 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6645 if (item->vmptr == vmptr) {
6646 free_loaded_vmcs(&item->vmcs02);
6647 list_del(&item->list);
6648 kfree(item);
6649 vmx->nested.vmcs02_num--;
6650 return;
6651 }
6652}
6653
6654/*
6655 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006656 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6657 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006658 */
6659static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6660{
6661 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006662
6663 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006664 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006665 /*
6666 * Something will leak if the above WARN triggers. Better than
6667 * a use-after-free.
6668 */
6669 if (vmx->loaded_vmcs == &item->vmcs02)
6670 continue;
6671
6672 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006673 list_del(&item->list);
6674 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006675 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006676 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006677}
6678
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006679/*
6680 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6681 * set the success or error code of an emulated VMX instruction, as specified
6682 * by Vol 2B, VMX Instruction Reference, "Conventions".
6683 */
6684static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6685{
6686 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6687 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6688 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6689}
6690
6691static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6692{
6693 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6694 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6695 X86_EFLAGS_SF | X86_EFLAGS_OF))
6696 | X86_EFLAGS_CF);
6697}
6698
Abel Gordon145c28d2013-04-18 14:36:55 +03006699static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006700 u32 vm_instruction_error)
6701{
6702 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6703 /*
6704 * failValid writes the error number to the current VMCS, which
6705 * can't be done there isn't a current VMCS.
6706 */
6707 nested_vmx_failInvalid(vcpu);
6708 return;
6709 }
6710 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6711 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6712 X86_EFLAGS_SF | X86_EFLAGS_OF))
6713 | X86_EFLAGS_ZF);
6714 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6715 /*
6716 * We don't need to force a shadow sync because
6717 * VM_INSTRUCTION_ERROR is not shadowed
6718 */
6719}
Abel Gordon145c28d2013-04-18 14:36:55 +03006720
Wincy Vanff651cb2014-12-11 08:52:58 +03006721static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6722{
6723 /* TODO: not to reset guest simply here. */
6724 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6725 pr_warn("kvm: nested vmx abort, indicator %d\n", indicator);
6726}
6727
Jan Kiszkaf4124502014-03-07 20:03:13 +01006728static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6729{
6730 struct vcpu_vmx *vmx =
6731 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6732
6733 vmx->nested.preemption_timer_expired = true;
6734 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6735 kvm_vcpu_kick(&vmx->vcpu);
6736
6737 return HRTIMER_NORESTART;
6738}
6739
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006740/*
Bandan Das19677e32014-05-06 02:19:15 -04006741 * Decode the memory-address operand of a vmx instruction, as recorded on an
6742 * exit caused by such an instruction (run by a guest hypervisor).
6743 * On success, returns 0. When the operand is invalid, returns 1 and throws
6744 * #UD or #GP.
6745 */
6746static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6747 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006748 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006749{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006750 gva_t off;
6751 bool exn;
6752 struct kvm_segment s;
6753
Bandan Das19677e32014-05-06 02:19:15 -04006754 /*
6755 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6756 * Execution", on an exit, vmx_instruction_info holds most of the
6757 * addressing components of the operand. Only the displacement part
6758 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6759 * For how an actual address is calculated from all these components,
6760 * refer to Vol. 1, "Operand Addressing".
6761 */
6762 int scaling = vmx_instruction_info & 3;
6763 int addr_size = (vmx_instruction_info >> 7) & 7;
6764 bool is_reg = vmx_instruction_info & (1u << 10);
6765 int seg_reg = (vmx_instruction_info >> 15) & 7;
6766 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6767 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6768 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6769 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6770
6771 if (is_reg) {
6772 kvm_queue_exception(vcpu, UD_VECTOR);
6773 return 1;
6774 }
6775
6776 /* Addr = segment_base + offset */
6777 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006778 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006779 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006780 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006781 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006782 off += kvm_register_read(vcpu, index_reg)<<scaling;
6783 vmx_get_segment(vcpu, &s, seg_reg);
6784 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006785
6786 if (addr_size == 1) /* 32 bit */
6787 *ret &= 0xffffffff;
6788
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006789 /* Checks for #GP/#SS exceptions. */
6790 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006791 if (is_long_mode(vcpu)) {
6792 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6793 * non-canonical form. This is the only check on the memory
6794 * destination for long mode!
6795 */
6796 exn = is_noncanonical_address(*ret);
6797 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006798 /* Protected mode: apply checks for segment validity in the
6799 * following order:
6800 * - segment type check (#GP(0) may be thrown)
6801 * - usability check (#GP(0)/#SS(0))
6802 * - limit check (#GP(0)/#SS(0))
6803 */
6804 if (wr)
6805 /* #GP(0) if the destination operand is located in a
6806 * read-only data segment or any code segment.
6807 */
6808 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6809 else
6810 /* #GP(0) if the source operand is located in an
6811 * execute-only code segment
6812 */
6813 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006814 if (exn) {
6815 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6816 return 1;
6817 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006818 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6819 */
6820 exn = (s.unusable != 0);
6821 /* Protected mode: #GP(0)/#SS(0) if the memory
6822 * operand is outside the segment limit.
6823 */
6824 exn = exn || (off + sizeof(u64) > s.limit);
6825 }
6826 if (exn) {
6827 kvm_queue_exception_e(vcpu,
6828 seg_reg == VCPU_SREG_SS ?
6829 SS_VECTOR : GP_VECTOR,
6830 0);
6831 return 1;
6832 }
6833
Bandan Das19677e32014-05-06 02:19:15 -04006834 return 0;
6835}
6836
6837/*
Bandan Das3573e222014-05-06 02:19:16 -04006838 * This function performs the various checks including
6839 * - if it's 4KB aligned
6840 * - No bits beyond the physical address width are set
6841 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006842 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006843 */
Bandan Das4291b582014-05-06 02:19:18 -04006844static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6845 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006846{
6847 gva_t gva;
6848 gpa_t vmptr;
6849 struct x86_exception e;
6850 struct page *page;
6851 struct vcpu_vmx *vmx = to_vmx(vcpu);
6852 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6853
6854 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006855 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006856 return 1;
6857
6858 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6859 sizeof(vmptr), &e)) {
6860 kvm_inject_page_fault(vcpu, &e);
6861 return 1;
6862 }
6863
6864 switch (exit_reason) {
6865 case EXIT_REASON_VMON:
6866 /*
6867 * SDM 3: 24.11.5
6868 * The first 4 bytes of VMXON region contain the supported
6869 * VMCS revision identifier
6870 *
6871 * Note - IA32_VMX_BASIC[48] will never be 1
6872 * for the nested case;
6873 * which replaces physical address width with 32
6874 *
6875 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006876 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006877 nested_vmx_failInvalid(vcpu);
6878 skip_emulated_instruction(vcpu);
6879 return 1;
6880 }
6881
6882 page = nested_get_page(vcpu, vmptr);
6883 if (page == NULL ||
6884 *(u32 *)kmap(page) != VMCS12_REVISION) {
6885 nested_vmx_failInvalid(vcpu);
6886 kunmap(page);
6887 skip_emulated_instruction(vcpu);
6888 return 1;
6889 }
6890 kunmap(page);
6891 vmx->nested.vmxon_ptr = vmptr;
6892 break;
Bandan Das4291b582014-05-06 02:19:18 -04006893 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006894 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006895 nested_vmx_failValid(vcpu,
6896 VMXERR_VMCLEAR_INVALID_ADDRESS);
6897 skip_emulated_instruction(vcpu);
6898 return 1;
6899 }
Bandan Das3573e222014-05-06 02:19:16 -04006900
Bandan Das4291b582014-05-06 02:19:18 -04006901 if (vmptr == vmx->nested.vmxon_ptr) {
6902 nested_vmx_failValid(vcpu,
6903 VMXERR_VMCLEAR_VMXON_POINTER);
6904 skip_emulated_instruction(vcpu);
6905 return 1;
6906 }
6907 break;
6908 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006909 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006910 nested_vmx_failValid(vcpu,
6911 VMXERR_VMPTRLD_INVALID_ADDRESS);
6912 skip_emulated_instruction(vcpu);
6913 return 1;
6914 }
6915
6916 if (vmptr == vmx->nested.vmxon_ptr) {
6917 nested_vmx_failValid(vcpu,
6918 VMXERR_VMCLEAR_VMXON_POINTER);
6919 skip_emulated_instruction(vcpu);
6920 return 1;
6921 }
6922 break;
Bandan Das3573e222014-05-06 02:19:16 -04006923 default:
6924 return 1; /* shouldn't happen */
6925 }
6926
Bandan Das4291b582014-05-06 02:19:18 -04006927 if (vmpointer)
6928 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006929 return 0;
6930}
6931
6932/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006933 * Emulate the VMXON instruction.
6934 * Currently, we just remember that VMX is active, and do not save or even
6935 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6936 * do not currently need to store anything in that guest-allocated memory
6937 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6938 * argument is different from the VMXON pointer (which the spec says they do).
6939 */
6940static int handle_vmon(struct kvm_vcpu *vcpu)
6941{
6942 struct kvm_segment cs;
6943 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03006944 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006945 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6946 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006947
6948 /* The Intel VMX Instruction Reference lists a bunch of bits that
6949 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6950 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6951 * Otherwise, we should fail with #UD. We test these now:
6952 */
6953 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6954 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6955 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6956 kvm_queue_exception(vcpu, UD_VECTOR);
6957 return 1;
6958 }
6959
6960 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6961 if (is_long_mode(vcpu) && !cs.l) {
6962 kvm_queue_exception(vcpu, UD_VECTOR);
6963 return 1;
6964 }
6965
6966 if (vmx_get_cpl(vcpu)) {
6967 kvm_inject_gp(vcpu, 0);
6968 return 1;
6969 }
Bandan Das3573e222014-05-06 02:19:16 -04006970
Bandan Das4291b582014-05-06 02:19:18 -04006971 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04006972 return 1;
6973
Abel Gordon145c28d2013-04-18 14:36:55 +03006974 if (vmx->nested.vmxon) {
6975 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6976 skip_emulated_instruction(vcpu);
6977 return 1;
6978 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006979
Haozhong Zhang3b840802016-06-22 14:59:54 +08006980 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006981 != VMXON_NEEDED_FEATURES) {
6982 kvm_inject_gp(vcpu, 0);
6983 return 1;
6984 }
6985
Radim Krčmářd048c092016-08-08 20:16:22 +02006986 if (cpu_has_vmx_msr_bitmap()) {
6987 vmx->nested.msr_bitmap =
6988 (unsigned long *)__get_free_page(GFP_KERNEL);
6989 if (!vmx->nested.msr_bitmap)
6990 goto out_msr_bitmap;
6991 }
6992
David Matlack4f2777b2016-07-13 17:16:37 -07006993 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
6994 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02006995 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07006996
Abel Gordon8de48832013-04-18 14:37:25 +03006997 if (enable_shadow_vmcs) {
6998 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02006999 if (!shadow_vmcs)
7000 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007001 /* mark vmcs as shadow */
7002 shadow_vmcs->revision_id |= (1u << 31);
7003 /* init shadow vmcs */
7004 vmcs_clear(shadow_vmcs);
7005 vmx->nested.current_shadow_vmcs = shadow_vmcs;
7006 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007007
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007008 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7009 vmx->nested.vmcs02_num = 0;
7010
Jan Kiszkaf4124502014-03-07 20:03:13 +01007011 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7012 HRTIMER_MODE_REL);
7013 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7014
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007015 vmx->nested.vmxon = true;
7016
7017 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007018 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007019 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007020
7021out_shadow_vmcs:
7022 kfree(vmx->nested.cached_vmcs12);
7023
7024out_cached_vmcs12:
7025 free_page((unsigned long)vmx->nested.msr_bitmap);
7026
7027out_msr_bitmap:
7028 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007029}
7030
7031/*
7032 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7033 * for running VMX instructions (except VMXON, whose prerequisites are
7034 * slightly different). It also specifies what exception to inject otherwise.
7035 */
7036static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7037{
7038 struct kvm_segment cs;
7039 struct vcpu_vmx *vmx = to_vmx(vcpu);
7040
7041 if (!vmx->nested.vmxon) {
7042 kvm_queue_exception(vcpu, UD_VECTOR);
7043 return 0;
7044 }
7045
7046 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7047 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7048 (is_long_mode(vcpu) && !cs.l)) {
7049 kvm_queue_exception(vcpu, UD_VECTOR);
7050 return 0;
7051 }
7052
7053 if (vmx_get_cpl(vcpu)) {
7054 kvm_inject_gp(vcpu, 0);
7055 return 0;
7056 }
7057
7058 return 1;
7059}
7060
Abel Gordone7953d72013-04-18 14:37:55 +03007061static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7062{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007063 if (vmx->nested.current_vmptr == -1ull)
7064 return;
7065
7066 /* current_vmptr and current_vmcs12 are always set/reset together */
7067 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7068 return;
7069
Abel Gordon012f83c2013-04-18 14:39:25 +03007070 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007071 /* copy to memory all shadowed fields in case
7072 they were modified */
7073 copy_shadow_to_vmcs12(vmx);
7074 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007075 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7076 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007077 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007078 }
Wincy Van705699a2015-02-03 23:58:17 +08007079 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007080
7081 /* Flush VMCS12 to guest memory */
7082 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7083 VMCS12_SIZE);
7084
Abel Gordone7953d72013-04-18 14:37:55 +03007085 kunmap(vmx->nested.current_vmcs12_page);
7086 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007087 vmx->nested.current_vmptr = -1ull;
7088 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007089}
7090
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007091/*
7092 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7093 * just stops using VMX.
7094 */
7095static void free_nested(struct vcpu_vmx *vmx)
7096{
7097 if (!vmx->nested.vmxon)
7098 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007099
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007100 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007101 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007102 nested_release_vmcs12(vmx);
Radim Krčmářd048c092016-08-08 20:16:22 +02007103 if (vmx->nested.msr_bitmap) {
7104 free_page((unsigned long)vmx->nested.msr_bitmap);
7105 vmx->nested.msr_bitmap = NULL;
7106 }
Abel Gordone7953d72013-04-18 14:37:55 +03007107 if (enable_shadow_vmcs)
7108 free_vmcs(vmx->nested.current_shadow_vmcs);
David Matlack4f2777b2016-07-13 17:16:37 -07007109 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007110 /* Unpin physical memory we referred to in current vmcs02 */
7111 if (vmx->nested.apic_access_page) {
7112 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007113 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007114 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007115 if (vmx->nested.virtual_apic_page) {
7116 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007117 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007118 }
Wincy Van705699a2015-02-03 23:58:17 +08007119 if (vmx->nested.pi_desc_page) {
7120 kunmap(vmx->nested.pi_desc_page);
7121 nested_release_page(vmx->nested.pi_desc_page);
7122 vmx->nested.pi_desc_page = NULL;
7123 vmx->nested.pi_desc = NULL;
7124 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007125
7126 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007127}
7128
7129/* Emulate the VMXOFF instruction */
7130static int handle_vmoff(struct kvm_vcpu *vcpu)
7131{
7132 if (!nested_vmx_check_permission(vcpu))
7133 return 1;
7134 free_nested(to_vmx(vcpu));
7135 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007136 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007137 return 1;
7138}
7139
Nadav Har'El27d6c862011-05-25 23:06:59 +03007140/* Emulate the VMCLEAR instruction */
7141static int handle_vmclear(struct kvm_vcpu *vcpu)
7142{
7143 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007144 gpa_t vmptr;
7145 struct vmcs12 *vmcs12;
7146 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007147
7148 if (!nested_vmx_check_permission(vcpu))
7149 return 1;
7150
Bandan Das4291b582014-05-06 02:19:18 -04007151 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007152 return 1;
7153
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007154 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007155 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007156
7157 page = nested_get_page(vcpu, vmptr);
7158 if (page == NULL) {
7159 /*
7160 * For accurate processor emulation, VMCLEAR beyond available
7161 * physical memory should do nothing at all. However, it is
7162 * possible that a nested vmx bug, not a guest hypervisor bug,
7163 * resulted in this case, so let's shut down before doing any
7164 * more damage:
7165 */
7166 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7167 return 1;
7168 }
7169 vmcs12 = kmap(page);
7170 vmcs12->launch_state = 0;
7171 kunmap(page);
7172 nested_release_page(page);
7173
7174 nested_free_vmcs02(vmx, vmptr);
7175
7176 skip_emulated_instruction(vcpu);
7177 nested_vmx_succeed(vcpu);
7178 return 1;
7179}
7180
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007181static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7182
7183/* Emulate the VMLAUNCH instruction */
7184static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7185{
7186 return nested_vmx_run(vcpu, true);
7187}
7188
7189/* Emulate the VMRESUME instruction */
7190static int handle_vmresume(struct kvm_vcpu *vcpu)
7191{
7192
7193 return nested_vmx_run(vcpu, false);
7194}
7195
Nadav Har'El49f705c2011-05-25 23:08:30 +03007196enum vmcs_field_type {
7197 VMCS_FIELD_TYPE_U16 = 0,
7198 VMCS_FIELD_TYPE_U64 = 1,
7199 VMCS_FIELD_TYPE_U32 = 2,
7200 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7201};
7202
7203static inline int vmcs_field_type(unsigned long field)
7204{
7205 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7206 return VMCS_FIELD_TYPE_U32;
7207 return (field >> 13) & 0x3 ;
7208}
7209
7210static inline int vmcs_field_readonly(unsigned long field)
7211{
7212 return (((field >> 10) & 0x3) == 1);
7213}
7214
7215/*
7216 * Read a vmcs12 field. Since these can have varying lengths and we return
7217 * one type, we chose the biggest type (u64) and zero-extend the return value
7218 * to that size. Note that the caller, handle_vmread, might need to use only
7219 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7220 * 64-bit fields are to be returned).
7221 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007222static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7223 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007224{
7225 short offset = vmcs_field_to_offset(field);
7226 char *p;
7227
7228 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007229 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007230
7231 p = ((char *)(get_vmcs12(vcpu))) + offset;
7232
7233 switch (vmcs_field_type(field)) {
7234 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7235 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007236 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007237 case VMCS_FIELD_TYPE_U16:
7238 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007239 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007240 case VMCS_FIELD_TYPE_U32:
7241 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007242 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007243 case VMCS_FIELD_TYPE_U64:
7244 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007245 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007246 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007247 WARN_ON(1);
7248 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007249 }
7250}
7251
Abel Gordon20b97fe2013-04-18 14:36:25 +03007252
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007253static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7254 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007255 short offset = vmcs_field_to_offset(field);
7256 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7257 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007258 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007259
7260 switch (vmcs_field_type(field)) {
7261 case VMCS_FIELD_TYPE_U16:
7262 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007263 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007264 case VMCS_FIELD_TYPE_U32:
7265 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007266 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007267 case VMCS_FIELD_TYPE_U64:
7268 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007269 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007270 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7271 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007272 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007273 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007274 WARN_ON(1);
7275 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007276 }
7277
7278}
7279
Abel Gordon16f5b902013-04-18 14:38:25 +03007280static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7281{
7282 int i;
7283 unsigned long field;
7284 u64 field_value;
7285 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007286 const unsigned long *fields = shadow_read_write_fields;
7287 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007288
Jan Kiszka282da872014-10-08 18:05:39 +02007289 preempt_disable();
7290
Abel Gordon16f5b902013-04-18 14:38:25 +03007291 vmcs_load(shadow_vmcs);
7292
7293 for (i = 0; i < num_fields; i++) {
7294 field = fields[i];
7295 switch (vmcs_field_type(field)) {
7296 case VMCS_FIELD_TYPE_U16:
7297 field_value = vmcs_read16(field);
7298 break;
7299 case VMCS_FIELD_TYPE_U32:
7300 field_value = vmcs_read32(field);
7301 break;
7302 case VMCS_FIELD_TYPE_U64:
7303 field_value = vmcs_read64(field);
7304 break;
7305 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7306 field_value = vmcs_readl(field);
7307 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007308 default:
7309 WARN_ON(1);
7310 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007311 }
7312 vmcs12_write_any(&vmx->vcpu, field, field_value);
7313 }
7314
7315 vmcs_clear(shadow_vmcs);
7316 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007317
7318 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007319}
7320
Abel Gordonc3114422013-04-18 14:38:55 +03007321static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7322{
Mathias Krausec2bae892013-06-26 20:36:21 +02007323 const unsigned long *fields[] = {
7324 shadow_read_write_fields,
7325 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007326 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007327 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007328 max_shadow_read_write_fields,
7329 max_shadow_read_only_fields
7330 };
7331 int i, q;
7332 unsigned long field;
7333 u64 field_value = 0;
7334 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
7335
7336 vmcs_load(shadow_vmcs);
7337
Mathias Krausec2bae892013-06-26 20:36:21 +02007338 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007339 for (i = 0; i < max_fields[q]; i++) {
7340 field = fields[q][i];
7341 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7342
7343 switch (vmcs_field_type(field)) {
7344 case VMCS_FIELD_TYPE_U16:
7345 vmcs_write16(field, (u16)field_value);
7346 break;
7347 case VMCS_FIELD_TYPE_U32:
7348 vmcs_write32(field, (u32)field_value);
7349 break;
7350 case VMCS_FIELD_TYPE_U64:
7351 vmcs_write64(field, (u64)field_value);
7352 break;
7353 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7354 vmcs_writel(field, (long)field_value);
7355 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007356 default:
7357 WARN_ON(1);
7358 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007359 }
7360 }
7361 }
7362
7363 vmcs_clear(shadow_vmcs);
7364 vmcs_load(vmx->loaded_vmcs->vmcs);
7365}
7366
Nadav Har'El49f705c2011-05-25 23:08:30 +03007367/*
7368 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7369 * used before) all generate the same failure when it is missing.
7370 */
7371static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7372{
7373 struct vcpu_vmx *vmx = to_vmx(vcpu);
7374 if (vmx->nested.current_vmptr == -1ull) {
7375 nested_vmx_failInvalid(vcpu);
7376 skip_emulated_instruction(vcpu);
7377 return 0;
7378 }
7379 return 1;
7380}
7381
7382static int handle_vmread(struct kvm_vcpu *vcpu)
7383{
7384 unsigned long field;
7385 u64 field_value;
7386 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7387 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7388 gva_t gva = 0;
7389
7390 if (!nested_vmx_check_permission(vcpu) ||
7391 !nested_vmx_check_vmcs12(vcpu))
7392 return 1;
7393
7394 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007395 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007396 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007397 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007398 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7399 skip_emulated_instruction(vcpu);
7400 return 1;
7401 }
7402 /*
7403 * Now copy part of this value to register or memory, as requested.
7404 * Note that the number of bits actually copied is 32 or 64 depending
7405 * on the guest's mode (32 or 64 bit), not on the given field's length.
7406 */
7407 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007408 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007409 field_value);
7410 } else {
7411 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007412 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007413 return 1;
7414 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7415 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7416 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7417 }
7418
7419 nested_vmx_succeed(vcpu);
7420 skip_emulated_instruction(vcpu);
7421 return 1;
7422}
7423
7424
7425static int handle_vmwrite(struct kvm_vcpu *vcpu)
7426{
7427 unsigned long field;
7428 gva_t gva;
7429 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7430 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007431 /* The value to write might be 32 or 64 bits, depending on L1's long
7432 * mode, and eventually we need to write that into a field of several
7433 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007434 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007435 * bits into the vmcs12 field.
7436 */
7437 u64 field_value = 0;
7438 struct x86_exception e;
7439
7440 if (!nested_vmx_check_permission(vcpu) ||
7441 !nested_vmx_check_vmcs12(vcpu))
7442 return 1;
7443
7444 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007445 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007446 (((vmx_instruction_info) >> 3) & 0xf));
7447 else {
7448 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007449 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007450 return 1;
7451 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007452 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007453 kvm_inject_page_fault(vcpu, &e);
7454 return 1;
7455 }
7456 }
7457
7458
Nadav Amit27e6fb52014-06-18 17:19:26 +03007459 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007460 if (vmcs_field_readonly(field)) {
7461 nested_vmx_failValid(vcpu,
7462 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7463 skip_emulated_instruction(vcpu);
7464 return 1;
7465 }
7466
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007467 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007468 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7469 skip_emulated_instruction(vcpu);
7470 return 1;
7471 }
7472
7473 nested_vmx_succeed(vcpu);
7474 skip_emulated_instruction(vcpu);
7475 return 1;
7476}
7477
Nadav Har'El63846662011-05-25 23:07:29 +03007478/* Emulate the VMPTRLD instruction */
7479static int handle_vmptrld(struct kvm_vcpu *vcpu)
7480{
7481 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007482 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007483
7484 if (!nested_vmx_check_permission(vcpu))
7485 return 1;
7486
Bandan Das4291b582014-05-06 02:19:18 -04007487 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007488 return 1;
7489
Nadav Har'El63846662011-05-25 23:07:29 +03007490 if (vmx->nested.current_vmptr != vmptr) {
7491 struct vmcs12 *new_vmcs12;
7492 struct page *page;
7493 page = nested_get_page(vcpu, vmptr);
7494 if (page == NULL) {
7495 nested_vmx_failInvalid(vcpu);
7496 skip_emulated_instruction(vcpu);
7497 return 1;
7498 }
7499 new_vmcs12 = kmap(page);
7500 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7501 kunmap(page);
7502 nested_release_page_clean(page);
7503 nested_vmx_failValid(vcpu,
7504 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7505 skip_emulated_instruction(vcpu);
7506 return 1;
7507 }
Nadav Har'El63846662011-05-25 23:07:29 +03007508
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007509 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007510 vmx->nested.current_vmptr = vmptr;
7511 vmx->nested.current_vmcs12 = new_vmcs12;
7512 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007513 /*
7514 * Load VMCS12 from guest memory since it is not already
7515 * cached.
7516 */
7517 memcpy(vmx->nested.cached_vmcs12,
7518 vmx->nested.current_vmcs12, VMCS12_SIZE);
7519
Abel Gordon012f83c2013-04-18 14:39:25 +03007520 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007521 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7522 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007523 vmcs_write64(VMCS_LINK_POINTER,
7524 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007525 vmx->nested.sync_shadow_vmcs = true;
7526 }
Nadav Har'El63846662011-05-25 23:07:29 +03007527 }
7528
7529 nested_vmx_succeed(vcpu);
7530 skip_emulated_instruction(vcpu);
7531 return 1;
7532}
7533
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007534/* Emulate the VMPTRST instruction */
7535static int handle_vmptrst(struct kvm_vcpu *vcpu)
7536{
7537 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7538 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7539 gva_t vmcs_gva;
7540 struct x86_exception e;
7541
7542 if (!nested_vmx_check_permission(vcpu))
7543 return 1;
7544
7545 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007546 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007547 return 1;
7548 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7549 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7550 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7551 sizeof(u64), &e)) {
7552 kvm_inject_page_fault(vcpu, &e);
7553 return 1;
7554 }
7555 nested_vmx_succeed(vcpu);
7556 skip_emulated_instruction(vcpu);
7557 return 1;
7558}
7559
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007560/* Emulate the INVEPT instruction */
7561static int handle_invept(struct kvm_vcpu *vcpu)
7562{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007563 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007564 u32 vmx_instruction_info, types;
7565 unsigned long type;
7566 gva_t gva;
7567 struct x86_exception e;
7568 struct {
7569 u64 eptp, gpa;
7570 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007571
Wincy Vanb9c237b2015-02-03 23:56:30 +08007572 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7573 SECONDARY_EXEC_ENABLE_EPT) ||
7574 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007575 kvm_queue_exception(vcpu, UD_VECTOR);
7576 return 1;
7577 }
7578
7579 if (!nested_vmx_check_permission(vcpu))
7580 return 1;
7581
7582 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7583 kvm_queue_exception(vcpu, UD_VECTOR);
7584 return 1;
7585 }
7586
7587 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007588 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007589
Wincy Vanb9c237b2015-02-03 23:56:30 +08007590 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007591
7592 if (!(types & (1UL << type))) {
7593 nested_vmx_failValid(vcpu,
7594 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007595 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007596 return 1;
7597 }
7598
7599 /* According to the Intel VMX instruction reference, the memory
7600 * operand is read even if it isn't needed (e.g., for type==global)
7601 */
7602 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007603 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007604 return 1;
7605 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7606 sizeof(operand), &e)) {
7607 kvm_inject_page_fault(vcpu, &e);
7608 return 1;
7609 }
7610
7611 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007612 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007613 /*
7614 * TODO: track mappings and invalidate
7615 * single context requests appropriately
7616 */
7617 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007618 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007619 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007620 nested_vmx_succeed(vcpu);
7621 break;
7622 default:
7623 BUG_ON(1);
7624 break;
7625 }
7626
7627 skip_emulated_instruction(vcpu);
7628 return 1;
7629}
7630
Petr Matouseka642fc32014-09-23 20:22:30 +02007631static int handle_invvpid(struct kvm_vcpu *vcpu)
7632{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007633 struct vcpu_vmx *vmx = to_vmx(vcpu);
7634 u32 vmx_instruction_info;
7635 unsigned long type, types;
7636 gva_t gva;
7637 struct x86_exception e;
7638 int vpid;
7639
7640 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7641 SECONDARY_EXEC_ENABLE_VPID) ||
7642 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7643 kvm_queue_exception(vcpu, UD_VECTOR);
7644 return 1;
7645 }
7646
7647 if (!nested_vmx_check_permission(vcpu))
7648 return 1;
7649
7650 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7651 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7652
7653 types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7;
7654
7655 if (!(types & (1UL << type))) {
7656 nested_vmx_failValid(vcpu,
7657 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007658 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007659 return 1;
7660 }
7661
7662 /* according to the intel vmx instruction reference, the memory
7663 * operand is read even if it isn't needed (e.g., for type==global)
7664 */
7665 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7666 vmx_instruction_info, false, &gva))
7667 return 1;
7668 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7669 sizeof(u32), &e)) {
7670 kvm_inject_page_fault(vcpu, &e);
7671 return 1;
7672 }
7673
7674 switch (type) {
Paolo Bonzinief697a72016-03-18 16:58:38 +01007675 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
7676 /*
7677 * Old versions of KVM use the single-context version so we
7678 * have to support it; just treat it the same as all-context.
7679 */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007680 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li5c614b32015-10-13 09:18:36 -07007681 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007682 nested_vmx_succeed(vcpu);
7683 break;
7684 default:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007685 /* Trap individual address invalidation invvpid calls */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007686 BUG_ON(1);
7687 break;
7688 }
7689
7690 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007691 return 1;
7692}
7693
Kai Huang843e4332015-01-28 10:54:28 +08007694static int handle_pml_full(struct kvm_vcpu *vcpu)
7695{
7696 unsigned long exit_qualification;
7697
7698 trace_kvm_pml_full(vcpu->vcpu_id);
7699
7700 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7701
7702 /*
7703 * PML buffer FULL happened while executing iret from NMI,
7704 * "blocked by NMI" bit has to be set before next VM entry.
7705 */
7706 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7707 cpu_has_virtual_nmis() &&
7708 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7709 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7710 GUEST_INTR_STATE_NMI);
7711
7712 /*
7713 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7714 * here.., and there's no userspace involvement needed for PML.
7715 */
7716 return 1;
7717}
7718
Yunhong Jiang64672c92016-06-13 14:19:59 -07007719static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7720{
7721 kvm_lapic_expired_hv_timer(vcpu);
7722 return 1;
7723}
7724
Nadav Har'El0140cae2011-05-25 23:06:28 +03007725/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007726 * The exit handlers return 1 if the exit was handled fully and guest execution
7727 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7728 * to be done to userspace and return 0.
7729 */
Mathias Krause772e0312012-08-30 01:30:19 +02007730static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007731 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7732 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007733 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007734 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007735 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007736 [EXIT_REASON_CR_ACCESS] = handle_cr,
7737 [EXIT_REASON_DR_ACCESS] = handle_dr,
7738 [EXIT_REASON_CPUID] = handle_cpuid,
7739 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7740 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7741 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7742 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007743 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007744 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007745 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007746 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007747 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007748 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007749 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007750 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007751 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007752 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007753 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007754 [EXIT_REASON_VMOFF] = handle_vmoff,
7755 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007756 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7757 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007758 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007759 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007760 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007761 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007762 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007763 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007764 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7765 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007766 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007767 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007768 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007769 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007770 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007771 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007772 [EXIT_REASON_XSAVES] = handle_xsaves,
7773 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007774 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007775 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007776};
7777
7778static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007779 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007780
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007781static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7782 struct vmcs12 *vmcs12)
7783{
7784 unsigned long exit_qualification;
7785 gpa_t bitmap, last_bitmap;
7786 unsigned int port;
7787 int size;
7788 u8 b;
7789
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007790 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007791 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007792
7793 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7794
7795 port = exit_qualification >> 16;
7796 size = (exit_qualification & 7) + 1;
7797
7798 last_bitmap = (gpa_t)-1;
7799 b = -1;
7800
7801 while (size > 0) {
7802 if (port < 0x8000)
7803 bitmap = vmcs12->io_bitmap_a;
7804 else if (port < 0x10000)
7805 bitmap = vmcs12->io_bitmap_b;
7806 else
Joe Perches1d804d02015-03-30 16:46:09 -07007807 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007808 bitmap += (port & 0x7fff) / 8;
7809
7810 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007811 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007812 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007813 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007814 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007815
7816 port++;
7817 size--;
7818 last_bitmap = bitmap;
7819 }
7820
Joe Perches1d804d02015-03-30 16:46:09 -07007821 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007822}
7823
Nadav Har'El644d7112011-05-25 23:12:35 +03007824/*
7825 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7826 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7827 * disinterest in the current event (read or write a specific MSR) by using an
7828 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7829 */
7830static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7831 struct vmcs12 *vmcs12, u32 exit_reason)
7832{
7833 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7834 gpa_t bitmap;
7835
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007836 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007837 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007838
7839 /*
7840 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7841 * for the four combinations of read/write and low/high MSR numbers.
7842 * First we need to figure out which of the four to use:
7843 */
7844 bitmap = vmcs12->msr_bitmap;
7845 if (exit_reason == EXIT_REASON_MSR_WRITE)
7846 bitmap += 2048;
7847 if (msr_index >= 0xc0000000) {
7848 msr_index -= 0xc0000000;
7849 bitmap += 1024;
7850 }
7851
7852 /* Then read the msr_index'th bit from this bitmap: */
7853 if (msr_index < 1024*8) {
7854 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007855 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007856 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007857 return 1 & (b >> (msr_index & 7));
7858 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007859 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007860}
7861
7862/*
7863 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7864 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7865 * intercept (via guest_host_mask etc.) the current event.
7866 */
7867static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7868 struct vmcs12 *vmcs12)
7869{
7870 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7871 int cr = exit_qualification & 15;
7872 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007873 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007874
7875 switch ((exit_qualification >> 4) & 3) {
7876 case 0: /* mov to cr */
7877 switch (cr) {
7878 case 0:
7879 if (vmcs12->cr0_guest_host_mask &
7880 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007881 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007882 break;
7883 case 3:
7884 if ((vmcs12->cr3_target_count >= 1 &&
7885 vmcs12->cr3_target_value0 == val) ||
7886 (vmcs12->cr3_target_count >= 2 &&
7887 vmcs12->cr3_target_value1 == val) ||
7888 (vmcs12->cr3_target_count >= 3 &&
7889 vmcs12->cr3_target_value2 == val) ||
7890 (vmcs12->cr3_target_count >= 4 &&
7891 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007892 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007893 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007894 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007895 break;
7896 case 4:
7897 if (vmcs12->cr4_guest_host_mask &
7898 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007899 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007900 break;
7901 case 8:
7902 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007903 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007904 break;
7905 }
7906 break;
7907 case 2: /* clts */
7908 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7909 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007910 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007911 break;
7912 case 1: /* mov from cr */
7913 switch (cr) {
7914 case 3:
7915 if (vmcs12->cpu_based_vm_exec_control &
7916 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007917 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007918 break;
7919 case 8:
7920 if (vmcs12->cpu_based_vm_exec_control &
7921 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007922 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007923 break;
7924 }
7925 break;
7926 case 3: /* lmsw */
7927 /*
7928 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7929 * cr0. Other attempted changes are ignored, with no exit.
7930 */
7931 if (vmcs12->cr0_guest_host_mask & 0xe &
7932 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007933 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007934 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7935 !(vmcs12->cr0_read_shadow & 0x1) &&
7936 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07007937 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007938 break;
7939 }
Joe Perches1d804d02015-03-30 16:46:09 -07007940 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007941}
7942
7943/*
7944 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
7945 * should handle it ourselves in L0 (and then continue L2). Only call this
7946 * when in is_guest_mode (L2).
7947 */
7948static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
7949{
Nadav Har'El644d7112011-05-25 23:12:35 +03007950 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7951 struct vcpu_vmx *vmx = to_vmx(vcpu);
7952 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01007953 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03007954
Jan Kiszka542060e2014-01-04 18:47:21 +01007955 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
7956 vmcs_readl(EXIT_QUALIFICATION),
7957 vmx->idt_vectoring_info,
7958 intr_info,
7959 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
7960 KVM_ISA_VMX);
7961
Nadav Har'El644d7112011-05-25 23:12:35 +03007962 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07007963 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007964
7965 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007966 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
7967 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07007968 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007969 }
7970
7971 switch (exit_reason) {
7972 case EXIT_REASON_EXCEPTION_NMI:
7973 if (!is_exception(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07007974 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007975 else if (is_page_fault(intr_info))
7976 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01007977 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01007978 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007979 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01007980 else if (is_debug(intr_info) &&
7981 vcpu->guest_debug &
7982 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
7983 return false;
7984 else if (is_breakpoint(intr_info) &&
7985 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
7986 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007987 return vmcs12->exception_bitmap &
7988 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
7989 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07007990 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007991 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07007992 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007993 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007994 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007995 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007996 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007997 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07007998 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007999 case EXIT_REASON_CPUID:
Marcelo Tosattibc613492014-09-18 18:24:57 -03008000 if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
Joe Perches1d804d02015-03-30 16:46:09 -07008001 return false;
8002 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008003 case EXIT_REASON_HLT:
8004 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8005 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008006 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008007 case EXIT_REASON_INVLPG:
8008 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8009 case EXIT_REASON_RDPMC:
8010 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008011 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008012 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8013 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8014 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8015 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8016 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8017 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008018 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008019 /*
8020 * VMX instructions trap unconditionally. This allows L1 to
8021 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8022 */
Joe Perches1d804d02015-03-30 16:46:09 -07008023 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008024 case EXIT_REASON_CR_ACCESS:
8025 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8026 case EXIT_REASON_DR_ACCESS:
8027 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8028 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008029 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008030 case EXIT_REASON_MSR_READ:
8031 case EXIT_REASON_MSR_WRITE:
8032 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8033 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008034 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008035 case EXIT_REASON_MWAIT_INSTRUCTION:
8036 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008037 case EXIT_REASON_MONITOR_TRAP_FLAG:
8038 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008039 case EXIT_REASON_MONITOR_INSTRUCTION:
8040 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8041 case EXIT_REASON_PAUSE_INSTRUCTION:
8042 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8043 nested_cpu_has2(vmcs12,
8044 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8045 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008046 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008047 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008048 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008049 case EXIT_REASON_APIC_ACCESS:
8050 return nested_cpu_has2(vmcs12,
8051 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008052 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008053 case EXIT_REASON_EOI_INDUCED:
8054 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008055 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008056 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008057 /*
8058 * L0 always deals with the EPT violation. If nested EPT is
8059 * used, and the nested mmu code discovers that the address is
8060 * missing in the guest EPT table (EPT12), the EPT violation
8061 * will be injected with nested_ept_inject_page_fault()
8062 */
Joe Perches1d804d02015-03-30 16:46:09 -07008063 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008064 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008065 /*
8066 * L2 never uses directly L1's EPT, but rather L0's own EPT
8067 * table (shadow on EPT) or a merged EPT table that L0 built
8068 * (EPT on EPT). So any problems with the structure of the
8069 * table is L0's fault.
8070 */
Joe Perches1d804d02015-03-30 16:46:09 -07008071 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008072 case EXIT_REASON_WBINVD:
8073 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8074 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008075 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008076 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8077 /*
8078 * This should never happen, since it is not possible to
8079 * set XSS to a non-zero value---neither in L1 nor in L2.
8080 * If if it were, XSS would have to be checked against
8081 * the XSS exit bitmap in vmcs12.
8082 */
8083 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008084 case EXIT_REASON_PREEMPTION_TIMER:
8085 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008086 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008087 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008088 }
8089}
8090
Avi Kivity586f9602010-11-18 13:09:54 +02008091static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8092{
8093 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8094 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8095}
8096
Kai Huanga3eaa862015-11-04 13:46:05 +08008097static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008098{
Kai Huanga3eaa862015-11-04 13:46:05 +08008099 if (vmx->pml_pg) {
8100 __free_page(vmx->pml_pg);
8101 vmx->pml_pg = NULL;
8102 }
Kai Huang843e4332015-01-28 10:54:28 +08008103}
8104
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008105static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008106{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008107 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008108 u64 *pml_buf;
8109 u16 pml_idx;
8110
8111 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8112
8113 /* Do nothing if PML buffer is empty */
8114 if (pml_idx == (PML_ENTITY_NUM - 1))
8115 return;
8116
8117 /* PML index always points to next available PML buffer entity */
8118 if (pml_idx >= PML_ENTITY_NUM)
8119 pml_idx = 0;
8120 else
8121 pml_idx++;
8122
8123 pml_buf = page_address(vmx->pml_pg);
8124 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8125 u64 gpa;
8126
8127 gpa = pml_buf[pml_idx];
8128 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008129 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008130 }
8131
8132 /* reset PML index */
8133 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8134}
8135
8136/*
8137 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8138 * Called before reporting dirty_bitmap to userspace.
8139 */
8140static void kvm_flush_pml_buffers(struct kvm *kvm)
8141{
8142 int i;
8143 struct kvm_vcpu *vcpu;
8144 /*
8145 * We only need to kick vcpu out of guest mode here, as PML buffer
8146 * is flushed at beginning of all VMEXITs, and it's obvious that only
8147 * vcpus running in guest are possible to have unflushed GPAs in PML
8148 * buffer.
8149 */
8150 kvm_for_each_vcpu(i, vcpu, kvm)
8151 kvm_vcpu_kick(vcpu);
8152}
8153
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008154static void vmx_dump_sel(char *name, uint32_t sel)
8155{
8156 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8157 name, vmcs_read32(sel),
8158 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8159 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8160 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8161}
8162
8163static void vmx_dump_dtsel(char *name, uint32_t limit)
8164{
8165 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8166 name, vmcs_read32(limit),
8167 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8168}
8169
8170static void dump_vmcs(void)
8171{
8172 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8173 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8174 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8175 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8176 u32 secondary_exec_control = 0;
8177 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008178 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008179 int i, n;
8180
8181 if (cpu_has_secondary_exec_ctrls())
8182 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8183
8184 pr_err("*** Guest State ***\n");
8185 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8186 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8187 vmcs_readl(CR0_GUEST_HOST_MASK));
8188 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8189 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8190 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8191 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8192 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8193 {
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008194 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8195 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8196 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8197 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008198 }
8199 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8200 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8201 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8202 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8203 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8204 vmcs_readl(GUEST_SYSENTER_ESP),
8205 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8206 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8207 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8208 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8209 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8210 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8211 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8212 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8213 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8214 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8215 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8216 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8217 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008218 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8219 efer, vmcs_read64(GUEST_IA32_PAT));
8220 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8221 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008222 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8223 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008224 pr_err("PerfGlobCtl = 0x%016llx\n",
8225 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008226 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008227 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008228 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8229 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8230 vmcs_read32(GUEST_ACTIVITY_STATE));
8231 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8232 pr_err("InterruptStatus = %04x\n",
8233 vmcs_read16(GUEST_INTR_STATUS));
8234
8235 pr_err("*** Host State ***\n");
8236 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8237 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8238 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8239 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8240 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8241 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8242 vmcs_read16(HOST_TR_SELECTOR));
8243 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8244 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8245 vmcs_readl(HOST_TR_BASE));
8246 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8247 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8248 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8249 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8250 vmcs_readl(HOST_CR4));
8251 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8252 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8253 vmcs_read32(HOST_IA32_SYSENTER_CS),
8254 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8255 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008256 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8257 vmcs_read64(HOST_IA32_EFER),
8258 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008259 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008260 pr_err("PerfGlobCtl = 0x%016llx\n",
8261 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008262
8263 pr_err("*** Control State ***\n");
8264 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8265 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8266 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8267 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8268 vmcs_read32(EXCEPTION_BITMAP),
8269 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8270 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8271 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8272 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8273 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8274 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8275 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8276 vmcs_read32(VM_EXIT_INTR_INFO),
8277 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8278 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8279 pr_err(" reason=%08x qualification=%016lx\n",
8280 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8281 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8282 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8283 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008284 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008285 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008286 pr_err("TSC Multiplier = 0x%016llx\n",
8287 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008288 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8289 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8290 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8291 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8292 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b42015-12-03 15:51:00 +01008293 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008294 n = vmcs_read32(CR3_TARGET_COUNT);
8295 for (i = 0; i + 1 < n; i += 4)
8296 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8297 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8298 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8299 if (i < n)
8300 pr_err("CR3 target%u=%016lx\n",
8301 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8302 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8303 pr_err("PLE Gap=%08x Window=%08x\n",
8304 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8305 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8306 pr_err("Virtual processor ID = 0x%04x\n",
8307 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8308}
8309
Avi Kivity6aa8b732006-12-10 02:21:36 -08008310/*
8311 * The guest has exited. See if we can fix it or if we need userspace
8312 * assistance.
8313 */
Avi Kivity851ba692009-08-24 11:10:17 +03008314static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008315{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008316 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008317 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008318 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008319
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008320 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8321
Kai Huang843e4332015-01-28 10:54:28 +08008322 /*
8323 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8324 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8325 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8326 * mode as if vcpus is in root mode, the PML buffer must has been
8327 * flushed already.
8328 */
8329 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008330 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008331
Mohammed Gamal80ced182009-09-01 12:48:18 +02008332 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008333 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008334 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008335
Nadav Har'El644d7112011-05-25 23:12:35 +03008336 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008337 nested_vmx_vmexit(vcpu, exit_reason,
8338 vmcs_read32(VM_EXIT_INTR_INFO),
8339 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008340 return 1;
8341 }
8342
Mohammed Gamal51207022010-05-31 22:40:54 +03008343 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008344 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008345 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8346 vcpu->run->fail_entry.hardware_entry_failure_reason
8347 = exit_reason;
8348 return 0;
8349 }
8350
Avi Kivity29bd8a72007-09-10 17:27:03 +03008351 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008352 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8353 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008354 = vmcs_read32(VM_INSTRUCTION_ERROR);
8355 return 0;
8356 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008357
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008358 /*
8359 * Note:
8360 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8361 * delivery event since it indicates guest is accessing MMIO.
8362 * The vm-exit can be triggered again after return to guest that
8363 * will cause infinite loop.
8364 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008365 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008366 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008367 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008368 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008369 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8370 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8371 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8372 vcpu->run->internal.ndata = 2;
8373 vcpu->run->internal.data[0] = vectoring_info;
8374 vcpu->run->internal.data[1] = exit_reason;
8375 return 0;
8376 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008377
Nadav Har'El644d7112011-05-25 23:12:35 +03008378 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8379 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008380 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008381 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008382 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008383 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008384 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008385 /*
8386 * This CPU don't support us in finding the end of an
8387 * NMI-blocked window if the guest runs with IRQs
8388 * disabled. So we pull the trigger after 1 s of
8389 * futile waiting, but inform the user about this.
8390 */
8391 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8392 "state on VCPU %d after 1 s timeout\n",
8393 __func__, vcpu->vcpu_id);
8394 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008395 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008396 }
8397
Avi Kivity6aa8b732006-12-10 02:21:36 -08008398 if (exit_reason < kvm_vmx_max_exit_handlers
8399 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008400 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008401 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008402 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8403 kvm_queue_exception(vcpu, UD_VECTOR);
8404 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008405 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008406}
8407
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008408static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008409{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008410 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8411
8412 if (is_guest_mode(vcpu) &&
8413 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8414 return;
8415
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008416 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008417 vmcs_write32(TPR_THRESHOLD, 0);
8418 return;
8419 }
8420
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008421 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008422}
8423
Yang Zhang8d146952013-01-25 10:18:50 +08008424static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8425{
8426 u32 sec_exec_control;
8427
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008428 /* Postpone execution until vmcs01 is the current VMCS. */
8429 if (is_guest_mode(vcpu)) {
8430 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8431 return;
8432 }
8433
Yang Zhang8d146952013-01-25 10:18:50 +08008434 /*
8435 * There is not point to enable virtualize x2apic without enable
8436 * apicv
8437 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08008438 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
Andrey Smetanind62caab2015-11-10 15:36:33 +03008439 !kvm_vcpu_apicv_active(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008440 return;
8441
Paolo Bonzini35754c92015-07-29 12:05:37 +02008442 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008443 return;
8444
8445 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8446
8447 if (set) {
8448 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8449 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8450 } else {
8451 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8452 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8453 }
8454 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8455
8456 vmx_set_msr_bitmap(vcpu);
8457}
8458
Tang Chen38b99172014-09-24 15:57:54 +08008459static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8460{
8461 struct vcpu_vmx *vmx = to_vmx(vcpu);
8462
8463 /*
8464 * Currently we do not handle the nested case where L2 has an
8465 * APIC access page of its own; that page is still pinned.
8466 * Hence, we skip the case where the VCPU is in guest mode _and_
8467 * L1 prepared an APIC access page for L2.
8468 *
8469 * For the case where L1 and L2 share the same APIC access page
8470 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8471 * in the vmcs12), this function will only update either the vmcs01
8472 * or the vmcs02. If the former, the vmcs02 will be updated by
8473 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8474 * the next L2->L1 exit.
8475 */
8476 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008477 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Tang Chen38b99172014-09-24 15:57:54 +08008478 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
8479 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8480}
8481
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008482static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008483{
8484 u16 status;
8485 u8 old;
8486
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008487 if (max_isr == -1)
8488 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008489
8490 status = vmcs_read16(GUEST_INTR_STATUS);
8491 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008492 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008493 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008494 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008495 vmcs_write16(GUEST_INTR_STATUS, status);
8496 }
8497}
8498
8499static void vmx_set_rvi(int vector)
8500{
8501 u16 status;
8502 u8 old;
8503
Wei Wang4114c272014-11-05 10:53:43 +08008504 if (vector == -1)
8505 vector = 0;
8506
Yang Zhangc7c9c562013-01-25 10:18:51 +08008507 status = vmcs_read16(GUEST_INTR_STATUS);
8508 old = (u8)status & 0xff;
8509 if ((u8)vector != old) {
8510 status &= ~0xff;
8511 status |= (u8)vector;
8512 vmcs_write16(GUEST_INTR_STATUS, status);
8513 }
8514}
8515
8516static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8517{
Wanpeng Li963fee12014-07-17 19:03:00 +08008518 if (!is_guest_mode(vcpu)) {
8519 vmx_set_rvi(max_irr);
8520 return;
8521 }
8522
Wei Wang4114c272014-11-05 10:53:43 +08008523 if (max_irr == -1)
8524 return;
8525
Wanpeng Li963fee12014-07-17 19:03:00 +08008526 /*
Wei Wang4114c272014-11-05 10:53:43 +08008527 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8528 * handles it.
8529 */
8530 if (nested_exit_on_intr(vcpu))
8531 return;
8532
8533 /*
8534 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008535 * is run without virtual interrupt delivery.
8536 */
8537 if (!kvm_event_needs_reinjection(vcpu) &&
8538 vmx_interrupt_allowed(vcpu)) {
8539 kvm_queue_interrupt(vcpu, max_irr, false);
8540 vmx_inject_irq(vcpu);
8541 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008542}
8543
Andrey Smetanin63086302015-11-10 15:36:32 +03008544static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008545{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008546 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008547 return;
8548
Yang Zhangc7c9c562013-01-25 10:18:51 +08008549 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8550 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8551 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8552 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8553}
8554
Avi Kivity51aa01d2010-07-20 14:31:20 +03008555static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008556{
Avi Kivity00eba012011-03-07 17:24:54 +02008557 u32 exit_intr_info;
8558
8559 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8560 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8561 return;
8562
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008563 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008564 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008565
8566 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008567 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008568 kvm_machine_check();
8569
Gleb Natapov20f65982009-05-11 13:35:55 +03008570 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008571 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008572 (exit_intr_info & INTR_INFO_VALID_MASK)) {
8573 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008574 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008575 kvm_after_handle_nmi(&vmx->vcpu);
8576 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008577}
Gleb Natapov20f65982009-05-11 13:35:55 +03008578
Yang Zhanga547c6d2013-04-11 19:25:10 +08008579static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8580{
8581 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008582 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008583
8584 /*
8585 * If external interrupt exists, IF bit is set in rflags/eflags on the
8586 * interrupt stack frame, and interrupt will be enabled on a return
8587 * from interrupt handler.
8588 */
8589 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8590 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8591 unsigned int vector;
8592 unsigned long entry;
8593 gate_desc *desc;
8594 struct vcpu_vmx *vmx = to_vmx(vcpu);
8595#ifdef CONFIG_X86_64
8596 unsigned long tmp;
8597#endif
8598
8599 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8600 desc = (gate_desc *)vmx->host_idt_base + vector;
8601 entry = gate_offset(*desc);
8602 asm volatile(
8603#ifdef CONFIG_X86_64
8604 "mov %%" _ASM_SP ", %[sp]\n\t"
8605 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8606 "push $%c[ss]\n\t"
8607 "push %[sp]\n\t"
8608#endif
8609 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008610 __ASM_SIZE(push) " $%c[cs]\n\t"
8611 "call *%[entry]\n\t"
8612 :
8613#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008614 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008615#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008616 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008617 :
8618 [entry]"r"(entry),
8619 [ss]"i"(__KERNEL_DS),
8620 [cs]"i"(__KERNEL_CS)
8621 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008622 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008623}
8624
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008625static bool vmx_has_high_real_mode_segbase(void)
8626{
8627 return enable_unrestricted_guest || emulate_invalid_guest_state;
8628}
8629
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008630static bool vmx_mpx_supported(void)
8631{
8632 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8633 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8634}
8635
Wanpeng Li55412b22014-12-02 19:21:30 +08008636static bool vmx_xsaves_supported(void)
8637{
8638 return vmcs_config.cpu_based_2nd_exec_ctrl &
8639 SECONDARY_EXEC_XSAVES;
8640}
8641
Avi Kivity51aa01d2010-07-20 14:31:20 +03008642static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8643{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008644 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008645 bool unblock_nmi;
8646 u8 vector;
8647 bool idtv_info_valid;
8648
8649 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008650
Avi Kivitycf393f72008-07-01 16:20:21 +03008651 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008652 if (vmx->nmi_known_unmasked)
8653 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008654 /*
8655 * Can't use vmx->exit_intr_info since we're not sure what
8656 * the exit reason is.
8657 */
8658 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008659 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8660 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8661 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008662 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008663 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8664 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008665 * SDM 3: 23.2.2 (September 2008)
8666 * Bit 12 is undefined in any of the following cases:
8667 * If the VM exit sets the valid bit in the IDT-vectoring
8668 * information field.
8669 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008670 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008671 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8672 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008673 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8674 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008675 else
8676 vmx->nmi_known_unmasked =
8677 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8678 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008679 } else if (unlikely(vmx->soft_vnmi_blocked))
8680 vmx->vnmi_blocked_time +=
8681 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008682}
8683
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008684static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008685 u32 idt_vectoring_info,
8686 int instr_len_field,
8687 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008688{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008689 u8 vector;
8690 int type;
8691 bool idtv_info_valid;
8692
8693 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008694
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008695 vcpu->arch.nmi_injected = false;
8696 kvm_clear_exception_queue(vcpu);
8697 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008698
8699 if (!idtv_info_valid)
8700 return;
8701
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008702 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008703
Avi Kivity668f6122008-07-02 09:28:55 +03008704 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8705 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008706
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008707 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008708 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008709 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008710 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008711 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008712 * Clear bit "block by NMI" before VM entry if a NMI
8713 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008714 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008715 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008716 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008717 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008718 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008719 /* fall through */
8720 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008721 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008722 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008723 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008724 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008725 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008726 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008727 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008728 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008729 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008730 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008731 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008732 break;
8733 default:
8734 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008735 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008736}
8737
Avi Kivity83422e12010-07-20 14:43:23 +03008738static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8739{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008740 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008741 VM_EXIT_INSTRUCTION_LEN,
8742 IDT_VECTORING_ERROR_CODE);
8743}
8744
Avi Kivityb463a6f2010-07-20 15:06:17 +03008745static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8746{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008747 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008748 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8749 VM_ENTRY_INSTRUCTION_LEN,
8750 VM_ENTRY_EXCEPTION_ERROR_CODE);
8751
8752 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8753}
8754
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008755static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8756{
8757 int i, nr_msrs;
8758 struct perf_guest_switch_msr *msrs;
8759
8760 msrs = perf_guest_get_msrs(&nr_msrs);
8761
8762 if (!msrs)
8763 return;
8764
8765 for (i = 0; i < nr_msrs; i++)
8766 if (msrs[i].host == msrs[i].guest)
8767 clear_atomic_switch_msr(vmx, msrs[i].msr);
8768 else
8769 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8770 msrs[i].host);
8771}
8772
Yunhong Jiang64672c92016-06-13 14:19:59 -07008773void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8774{
8775 struct vcpu_vmx *vmx = to_vmx(vcpu);
8776 u64 tscl;
8777 u32 delta_tsc;
8778
8779 if (vmx->hv_deadline_tsc == -1)
8780 return;
8781
8782 tscl = rdtsc();
8783 if (vmx->hv_deadline_tsc > tscl)
8784 /* sure to be 32 bit only because checked on set_hv_timer */
8785 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8786 cpu_preemption_timer_multi);
8787 else
8788 delta_tsc = 0;
8789
8790 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8791}
8792
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008793static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008794{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008795 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008796 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008797
8798 /* Record the guest's net vcpu time for enforced NMI injections. */
8799 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8800 vmx->entry_time = ktime_get();
8801
8802 /* Don't enter VMX if guest state is invalid, let the exit handler
8803 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008804 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008805 return;
8806
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008807 if (vmx->ple_window_dirty) {
8808 vmx->ple_window_dirty = false;
8809 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8810 }
8811
Abel Gordon012f83c2013-04-18 14:39:25 +03008812 if (vmx->nested.sync_shadow_vmcs) {
8813 copy_vmcs12_to_shadow(vmx);
8814 vmx->nested.sync_shadow_vmcs = false;
8815 }
8816
Avi Kivity104f2262010-11-18 13:12:52 +02008817 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8818 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8819 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8820 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8821
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008822 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008823 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8824 vmcs_writel(HOST_CR4, cr4);
8825 vmx->host_state.vmcs_host_cr4 = cr4;
8826 }
8827
Avi Kivity104f2262010-11-18 13:12:52 +02008828 /* When single-stepping over STI and MOV SS, we must clear the
8829 * corresponding interruptibility bits in the guest state. Otherwise
8830 * vmentry fails as it then expects bit 14 (BS) in pending debug
8831 * exceptions being set, but that's not correct for the guest debugging
8832 * case. */
8833 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8834 vmx_set_interrupt_shadow(vcpu, 0);
8835
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008836 if (vmx->guest_pkru_valid)
8837 __write_pkru(vmx->guest_pkru);
8838
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008839 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008840 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008841
Yunhong Jiang64672c92016-06-13 14:19:59 -07008842 vmx_arm_hv_timer(vcpu);
8843
Nadav Har'Eld462b812011-05-24 15:26:10 +03008844 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008845 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008846 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008847 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8848 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8849 "push %%" _ASM_CX " \n\t"
8850 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008851 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008852 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008853 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008854 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008855 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008856 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8857 "mov %%cr2, %%" _ASM_DX " \n\t"
8858 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008859 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008860 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008861 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008862 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008863 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008864 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008865 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8866 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8867 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8868 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8869 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8870 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008871#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008872 "mov %c[r8](%0), %%r8 \n\t"
8873 "mov %c[r9](%0), %%r9 \n\t"
8874 "mov %c[r10](%0), %%r10 \n\t"
8875 "mov %c[r11](%0), %%r11 \n\t"
8876 "mov %c[r12](%0), %%r12 \n\t"
8877 "mov %c[r13](%0), %%r13 \n\t"
8878 "mov %c[r14](%0), %%r14 \n\t"
8879 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008880#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008881 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008882
Avi Kivity6aa8b732006-12-10 02:21:36 -08008883 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008884 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008885 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008886 "jmp 2f \n\t"
8887 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8888 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008889 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008890 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008891 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008892 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8893 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8894 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8895 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8896 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8897 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8898 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008899#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008900 "mov %%r8, %c[r8](%0) \n\t"
8901 "mov %%r9, %c[r9](%0) \n\t"
8902 "mov %%r10, %c[r10](%0) \n\t"
8903 "mov %%r11, %c[r11](%0) \n\t"
8904 "mov %%r12, %c[r12](%0) \n\t"
8905 "mov %%r13, %c[r13](%0) \n\t"
8906 "mov %%r14, %c[r14](%0) \n\t"
8907 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008908#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008909 "mov %%cr2, %%" _ASM_AX " \n\t"
8910 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008911
Avi Kivityb188c81f2012-09-16 15:10:58 +03008912 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008913 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008914 ".pushsection .rodata \n\t"
8915 ".global vmx_return \n\t"
8916 "vmx_return: " _ASM_PTR " 2b \n\t"
8917 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008918 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008919 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008920 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03008921 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008922 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8923 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8924 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8925 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8926 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8927 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8928 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008929#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008930 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8931 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8932 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8933 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8934 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8935 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8936 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8937 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008938#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008939 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8940 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02008941 : "cc", "memory"
8942#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03008943 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008944 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008945#else
8946 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008947#endif
8948 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08008949
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008950 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
8951 if (debugctlmsr)
8952 update_debugctlmsr(debugctlmsr);
8953
Avi Kivityaa67f602012-08-01 16:48:03 +03008954#ifndef CONFIG_X86_64
8955 /*
8956 * The sysexit path does not restore ds/es, so we must set them to
8957 * a reasonable value ourselves.
8958 *
8959 * We can't defer this to vmx_load_host_state() since that function
8960 * may be executed in interrupt context, which saves and restore segments
8961 * around it, nullifying its effect.
8962 */
8963 loadsegment(ds, __USER_DS);
8964 loadsegment(es, __USER_DS);
8965#endif
8966
Avi Kivity6de4f3a2009-05-31 22:58:47 +03008967 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02008968 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008969 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03008970 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008971 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008972 vcpu->arch.regs_dirty = 0;
8973
Avi Kivity1155f762007-11-22 11:30:47 +02008974 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
8975
Nadav Har'Eld462b812011-05-24 15:26:10 +03008976 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02008977
Avi Kivity51aa01d2010-07-20 14:31:20 +03008978 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008979
Gleb Natapove0b890d2013-09-25 12:51:33 +03008980 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008981 * eager fpu is enabled if PKEY is supported and CR4 is switched
8982 * back on host, so it is safe to read guest PKRU from current
8983 * XSAVE.
8984 */
8985 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
8986 vmx->guest_pkru = __read_pkru();
8987 if (vmx->guest_pkru != vmx->host_pkru) {
8988 vmx->guest_pkru_valid = true;
8989 __write_pkru(vmx->host_pkru);
8990 } else
8991 vmx->guest_pkru_valid = false;
8992 }
8993
8994 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03008995 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
8996 * we did not inject a still-pending event to L1 now because of
8997 * nested_run_pending, we need to re-enable this bit.
8998 */
8999 if (vmx->nested.nested_run_pending)
9000 kvm_make_request(KVM_REQ_EVENT, vcpu);
9001
9002 vmx->nested.nested_run_pending = 0;
9003
Avi Kivity51aa01d2010-07-20 14:31:20 +03009004 vmx_complete_atomic_exit(vmx);
9005 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009006 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009007}
9008
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009009static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9010{
9011 struct vcpu_vmx *vmx = to_vmx(vcpu);
9012 int cpu;
9013
9014 if (vmx->loaded_vmcs == &vmx->vmcs01)
9015 return;
9016
9017 cpu = get_cpu();
9018 vmx->loaded_vmcs = &vmx->vmcs01;
9019 vmx_vcpu_put(vcpu);
9020 vmx_vcpu_load(vcpu, cpu);
9021 vcpu->cpu = cpu;
9022 put_cpu();
9023}
9024
Jim Mattson2f1fe812016-07-08 15:36:06 -07009025/*
9026 * Ensure that the current vmcs of the logical processor is the
9027 * vmcs01 of the vcpu before calling free_nested().
9028 */
9029static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9030{
9031 struct vcpu_vmx *vmx = to_vmx(vcpu);
9032 int r;
9033
9034 r = vcpu_load(vcpu);
9035 BUG_ON(r);
9036 vmx_load_vmcs01(vcpu);
9037 free_nested(vmx);
9038 vcpu_put(vcpu);
9039}
9040
Avi Kivity6aa8b732006-12-10 02:21:36 -08009041static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9042{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009043 struct vcpu_vmx *vmx = to_vmx(vcpu);
9044
Kai Huang843e4332015-01-28 10:54:28 +08009045 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009046 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009047 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009048 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009049 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009050 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009051 kfree(vmx->guest_msrs);
9052 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009053 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009054}
9055
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009056static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009057{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009058 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009059 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009060 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009061
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009062 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009063 return ERR_PTR(-ENOMEM);
9064
Wanpeng Li991e7a02015-09-16 17:30:05 +08009065 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009066
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009067 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9068 if (err)
9069 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009070
Peter Feiner4e595162016-07-07 14:49:58 -07009071 err = -ENOMEM;
9072
9073 /*
9074 * If PML is turned on, failure on enabling PML just results in failure
9075 * of creating the vcpu, therefore we can simplify PML logic (by
9076 * avoiding dealing with cases, such as enabling PML partially on vcpus
9077 * for the guest, etc.
9078 */
9079 if (enable_pml) {
9080 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9081 if (!vmx->pml_pg)
9082 goto uninit_vcpu;
9083 }
9084
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009085 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009086 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9087 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009088
Peter Feiner4e595162016-07-07 14:49:58 -07009089 if (!vmx->guest_msrs)
9090 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009091
Nadav Har'Eld462b812011-05-24 15:26:10 +03009092 vmx->loaded_vmcs = &vmx->vmcs01;
9093 vmx->loaded_vmcs->vmcs = alloc_vmcs();
9094 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009095 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009096 if (!vmm_exclusive)
9097 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
9098 loaded_vmcs_init(vmx->loaded_vmcs);
9099 if (!vmm_exclusive)
9100 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009101
Avi Kivity15ad7142007-07-11 18:17:21 +03009102 cpu = get_cpu();
9103 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009104 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009105 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009106 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009107 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009108 if (err)
9109 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009110 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009111 err = alloc_apic_access_page(kvm);
9112 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009113 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009114 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009115
Sheng Yangb927a3c2009-07-21 10:42:48 +08009116 if (enable_ept) {
9117 if (!kvm->arch.ept_identity_map_addr)
9118 kvm->arch.ept_identity_map_addr =
9119 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009120 err = init_rmode_identity_map(kvm);
9121 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009122 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009123 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009124
Wanpeng Li5c614b32015-10-13 09:18:36 -07009125 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009126 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009127 vmx->nested.vpid02 = allocate_vpid();
9128 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009129
Wincy Van705699a2015-02-03 23:58:17 +08009130 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009131 vmx->nested.current_vmptr = -1ull;
9132 vmx->nested.current_vmcs12 = NULL;
9133
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009134 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9135
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009136 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009137
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009138free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009139 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009140 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009141free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009142 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009143free_pml:
9144 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009145uninit_vcpu:
9146 kvm_vcpu_uninit(&vmx->vcpu);
9147free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009148 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009149 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009150 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009151}
9152
Yang, Sheng002c7f72007-07-31 14:23:01 +03009153static void __init vmx_check_processor_compat(void *rtn)
9154{
9155 struct vmcs_config vmcs_conf;
9156
9157 *(int *)rtn = 0;
9158 if (setup_vmcs_config(&vmcs_conf) < 0)
9159 *(int *)rtn = -EIO;
9160 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9161 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9162 smp_processor_id());
9163 *(int *)rtn = -EIO;
9164 }
9165}
9166
Sheng Yang67253af2008-04-25 10:20:22 +08009167static int get_ept_level(void)
9168{
9169 return VMX_EPT_DEFAULT_GAW + 1;
9170}
9171
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009172static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009173{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009174 u8 cache;
9175 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009176
Sheng Yang522c68c2009-04-27 20:35:43 +08009177 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009178 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009179 * 2. EPT with VT-d:
9180 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009181 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009182 * b. VT-d with snooping control feature: snooping control feature of
9183 * VT-d engine can guarantee the cache correctness. Just set it
9184 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009185 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009186 * consistent with host MTRR
9187 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009188 if (is_mmio) {
9189 cache = MTRR_TYPE_UNCACHABLE;
9190 goto exit;
9191 }
9192
9193 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009194 ipat = VMX_EPT_IPAT_BIT;
9195 cache = MTRR_TYPE_WRBACK;
9196 goto exit;
9197 }
9198
9199 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9200 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009201 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009202 cache = MTRR_TYPE_WRBACK;
9203 else
9204 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009205 goto exit;
9206 }
9207
Xiao Guangrongff536042015-06-15 16:55:22 +08009208 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009209
9210exit:
9211 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009212}
9213
Sheng Yang17cc3932010-01-05 19:02:27 +08009214static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009215{
Sheng Yang878403b2010-01-05 19:02:29 +08009216 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9217 return PT_DIRECTORY_LEVEL;
9218 else
9219 /* For shadow and EPT supported 1GB page */
9220 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009221}
9222
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009223static void vmcs_set_secondary_exec_control(u32 new_ctl)
9224{
9225 /*
9226 * These bits in the secondary execution controls field
9227 * are dynamic, the others are mostly based on the hypervisor
9228 * architecture and the guest's CPUID. Do not touch the
9229 * dynamic bits.
9230 */
9231 u32 mask =
9232 SECONDARY_EXEC_SHADOW_VMCS |
9233 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9234 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9235
9236 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9237
9238 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9239 (new_ctl & ~mask) | (cur_ctl & mask));
9240}
9241
Sheng Yang0e851882009-12-18 16:48:46 +08009242static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9243{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009244 struct kvm_cpuid_entry2 *best;
9245 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009246 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009247
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009248 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009249 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9250 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009251 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009252
Paolo Bonzini8b972652015-09-15 17:34:42 +02009253 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009254 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009255 vmx->nested.nested_vmx_secondary_ctls_high |=
9256 SECONDARY_EXEC_RDTSCP;
9257 else
9258 vmx->nested.nested_vmx_secondary_ctls_high &=
9259 ~SECONDARY_EXEC_RDTSCP;
9260 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009261 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009262
Mao, Junjiead756a12012-07-02 01:18:48 +00009263 /* Exposing INVPCID only when PCID is exposed */
9264 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9265 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009266 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9267 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009268 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009269
Mao, Junjiead756a12012-07-02 01:18:48 +00009270 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009271 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009272 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009273
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009274 if (cpu_has_secondary_exec_ctrls())
9275 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009276
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009277 if (nested_vmx_allowed(vcpu))
9278 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9279 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9280 else
9281 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9282 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009283}
9284
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009285static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9286{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009287 if (func == 1 && nested)
9288 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009289}
9290
Yang Zhang25d92082013-08-06 12:00:32 +03009291static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9292 struct x86_exception *fault)
9293{
Jan Kiszka533558b2014-01-04 18:47:20 +01009294 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9295 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009296
9297 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009298 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009299 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009300 exit_reason = EXIT_REASON_EPT_VIOLATION;
9301 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009302 vmcs12->guest_physical_address = fault->address;
9303}
9304
Nadav Har'El155a97a2013-08-05 11:07:16 +03009305/* Callbacks for nested_ept_init_mmu_context: */
9306
9307static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9308{
9309 /* return the page table to be shadowed - in our case, EPT12 */
9310 return get_vmcs12(vcpu)->ept_pointer;
9311}
9312
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009313static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009314{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009315 WARN_ON(mmu_is_nested(vcpu));
9316 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009317 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9318 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009319 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9320 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9321 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9322
9323 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009324}
9325
9326static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9327{
9328 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9329}
9330
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009331static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9332 u16 error_code)
9333{
9334 bool inequality, bit;
9335
9336 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9337 inequality =
9338 (error_code & vmcs12->page_fault_error_code_mask) !=
9339 vmcs12->page_fault_error_code_match;
9340 return inequality ^ bit;
9341}
9342
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009343static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9344 struct x86_exception *fault)
9345{
9346 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9347
9348 WARN_ON(!is_guest_mode(vcpu));
9349
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009350 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009351 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9352 vmcs_read32(VM_EXIT_INTR_INFO),
9353 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009354 else
9355 kvm_inject_page_fault(vcpu, fault);
9356}
9357
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009358static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9359 struct vmcs12 *vmcs12)
9360{
9361 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009362 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009363
9364 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009365 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9366 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009367 return false;
9368
9369 /*
9370 * Translate L1 physical address to host physical
9371 * address for vmcs02. Keep the page pinned, so this
9372 * physical address remains valid. We keep a reference
9373 * to it so we can release it later.
9374 */
9375 if (vmx->nested.apic_access_page) /* shouldn't happen */
9376 nested_release_page(vmx->nested.apic_access_page);
9377 vmx->nested.apic_access_page =
9378 nested_get_page(vcpu, vmcs12->apic_access_addr);
9379 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009380
9381 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009382 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9383 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009384 return false;
9385
9386 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9387 nested_release_page(vmx->nested.virtual_apic_page);
9388 vmx->nested.virtual_apic_page =
9389 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9390
9391 /*
9392 * Failing the vm entry is _not_ what the processor does
9393 * but it's basically the only possibility we have.
9394 * We could still enter the guest if CR8 load exits are
9395 * enabled, CR8 store exits are enabled, and virtualize APIC
9396 * access is disabled; in this case the processor would never
9397 * use the TPR shadow and we could simply clear the bit from
9398 * the execution control. But such a configuration is useless,
9399 * so let's keep the code simple.
9400 */
9401 if (!vmx->nested.virtual_apic_page)
9402 return false;
9403 }
9404
Wincy Van705699a2015-02-03 23:58:17 +08009405 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009406 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9407 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009408 return false;
9409
9410 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9411 kunmap(vmx->nested.pi_desc_page);
9412 nested_release_page(vmx->nested.pi_desc_page);
9413 }
9414 vmx->nested.pi_desc_page =
9415 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9416 if (!vmx->nested.pi_desc_page)
9417 return false;
9418
9419 vmx->nested.pi_desc =
9420 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9421 if (!vmx->nested.pi_desc) {
9422 nested_release_page_clean(vmx->nested.pi_desc_page);
9423 return false;
9424 }
9425 vmx->nested.pi_desc =
9426 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9427 (unsigned long)(vmcs12->posted_intr_desc_addr &
9428 (PAGE_SIZE - 1)));
9429 }
9430
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009431 return true;
9432}
9433
Jan Kiszkaf4124502014-03-07 20:03:13 +01009434static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9435{
9436 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9437 struct vcpu_vmx *vmx = to_vmx(vcpu);
9438
9439 if (vcpu->arch.virtual_tsc_khz == 0)
9440 return;
9441
9442 /* Make sure short timeouts reliably trigger an immediate vmexit.
9443 * hrtimer_start does not guarantee this. */
9444 if (preemption_timeout <= 1) {
9445 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9446 return;
9447 }
9448
9449 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9450 preemption_timeout *= 1000000;
9451 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9452 hrtimer_start(&vmx->nested.preemption_timer,
9453 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9454}
9455
Wincy Van3af18d92015-02-03 23:49:31 +08009456static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9457 struct vmcs12 *vmcs12)
9458{
9459 int maxphyaddr;
9460 u64 addr;
9461
9462 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9463 return 0;
9464
9465 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9466 WARN_ON(1);
9467 return -EINVAL;
9468 }
9469 maxphyaddr = cpuid_maxphyaddr(vcpu);
9470
9471 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9472 ((addr + PAGE_SIZE) >> maxphyaddr))
9473 return -EINVAL;
9474
9475 return 0;
9476}
9477
9478/*
9479 * Merge L0's and L1's MSR bitmap, return false to indicate that
9480 * we do not use the hardware.
9481 */
9482static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9483 struct vmcs12 *vmcs12)
9484{
Wincy Van82f0dd42015-02-03 23:57:18 +08009485 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009486 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009487 unsigned long *msr_bitmap_l1;
9488 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +08009489
Radim Krčmářd048c092016-08-08 20:16:22 +02009490 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +08009491 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9492 return false;
9493
9494 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
9495 if (!page) {
9496 WARN_ON(1);
9497 return false;
9498 }
Radim Krčmářd048c092016-08-08 20:16:22 +02009499 msr_bitmap_l1 = (unsigned long *)kmap(page);
9500 if (!msr_bitmap_l1) {
Wincy Vanf2b93282015-02-03 23:56:03 +08009501 nested_release_page_clean(page);
9502 WARN_ON(1);
9503 return false;
9504 }
9505
Radim Krčmářd048c092016-08-08 20:16:22 +02009506 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9507
Wincy Vanf2b93282015-02-03 23:56:03 +08009508 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009509 if (nested_cpu_has_apic_reg_virt(vmcs12))
9510 for (msr = 0x800; msr <= 0x8ff; msr++)
9511 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009512 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009513 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009514
9515 nested_vmx_disable_intercept_for_msr(
9516 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009517 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9518 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009519
Wincy Van608406e2015-02-03 23:57:51 +08009520 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009521 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009522 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009523 APIC_BASE_MSR + (APIC_EOI >> 4),
9524 MSR_TYPE_W);
9525 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009526 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009527 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9528 MSR_TYPE_W);
9529 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009530 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009531 kunmap(page);
9532 nested_release_page_clean(page);
9533
9534 return true;
9535}
9536
9537static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9538 struct vmcs12 *vmcs12)
9539{
Wincy Van82f0dd42015-02-03 23:57:18 +08009540 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009541 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009542 !nested_cpu_has_vid(vmcs12) &&
9543 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009544 return 0;
9545
9546 /*
9547 * If virtualize x2apic mode is enabled,
9548 * virtualize apic access must be disabled.
9549 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009550 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9551 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009552 return -EINVAL;
9553
Wincy Van608406e2015-02-03 23:57:51 +08009554 /*
9555 * If virtual interrupt delivery is enabled,
9556 * we must exit on external interrupts.
9557 */
9558 if (nested_cpu_has_vid(vmcs12) &&
9559 !nested_exit_on_intr(vcpu))
9560 return -EINVAL;
9561
Wincy Van705699a2015-02-03 23:58:17 +08009562 /*
9563 * bits 15:8 should be zero in posted_intr_nv,
9564 * the descriptor address has been already checked
9565 * in nested_get_vmcs12_pages.
9566 */
9567 if (nested_cpu_has_posted_intr(vmcs12) &&
9568 (!nested_cpu_has_vid(vmcs12) ||
9569 !nested_exit_intr_ack_set(vcpu) ||
9570 vmcs12->posted_intr_nv & 0xff00))
9571 return -EINVAL;
9572
Wincy Vanf2b93282015-02-03 23:56:03 +08009573 /* tpr shadow is needed by all apicv features. */
9574 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9575 return -EINVAL;
9576
9577 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009578}
9579
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009580static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9581 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009582 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009583{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009584 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009585 u64 count, addr;
9586
9587 if (vmcs12_read_any(vcpu, count_field, &count) ||
9588 vmcs12_read_any(vcpu, addr_field, &addr)) {
9589 WARN_ON(1);
9590 return -EINVAL;
9591 }
9592 if (count == 0)
9593 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009594 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009595 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9596 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
9597 pr_warn_ratelimited(
9598 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9599 addr_field, maxphyaddr, count, addr);
9600 return -EINVAL;
9601 }
9602 return 0;
9603}
9604
9605static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9606 struct vmcs12 *vmcs12)
9607{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009608 if (vmcs12->vm_exit_msr_load_count == 0 &&
9609 vmcs12->vm_exit_msr_store_count == 0 &&
9610 vmcs12->vm_entry_msr_load_count == 0)
9611 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009612 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009613 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009614 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009615 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009616 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009617 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009618 return -EINVAL;
9619 return 0;
9620}
9621
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009622static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9623 struct vmx_msr_entry *e)
9624{
9625 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009626 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009627 return -EINVAL;
9628 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9629 e->index == MSR_IA32_UCODE_REV)
9630 return -EINVAL;
9631 if (e->reserved != 0)
9632 return -EINVAL;
9633 return 0;
9634}
9635
9636static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9637 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009638{
9639 if (e->index == MSR_FS_BASE ||
9640 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009641 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9642 nested_vmx_msr_check_common(vcpu, e))
9643 return -EINVAL;
9644 return 0;
9645}
9646
9647static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9648 struct vmx_msr_entry *e)
9649{
9650 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9651 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009652 return -EINVAL;
9653 return 0;
9654}
9655
9656/*
9657 * Load guest's/host's msr at nested entry/exit.
9658 * return 0 for success, entry index for failure.
9659 */
9660static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9661{
9662 u32 i;
9663 struct vmx_msr_entry e;
9664 struct msr_data msr;
9665
9666 msr.host_initiated = false;
9667 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009668 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9669 &e, sizeof(e))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009670 pr_warn_ratelimited(
9671 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9672 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009673 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009674 }
9675 if (nested_vmx_load_msr_check(vcpu, &e)) {
9676 pr_warn_ratelimited(
9677 "%s check failed (%u, 0x%x, 0x%x)\n",
9678 __func__, i, e.index, e.reserved);
9679 goto fail;
9680 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009681 msr.index = e.index;
9682 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009683 if (kvm_set_msr(vcpu, &msr)) {
9684 pr_warn_ratelimited(
9685 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9686 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009687 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009688 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009689 }
9690 return 0;
9691fail:
9692 return i + 1;
9693}
9694
9695static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9696{
9697 u32 i;
9698 struct vmx_msr_entry e;
9699
9700 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009701 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009702 if (kvm_vcpu_read_guest(vcpu,
9703 gpa + i * sizeof(e),
9704 &e, 2 * sizeof(u32))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009705 pr_warn_ratelimited(
9706 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9707 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009708 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009709 }
9710 if (nested_vmx_store_msr_check(vcpu, &e)) {
9711 pr_warn_ratelimited(
9712 "%s check failed (%u, 0x%x, 0x%x)\n",
9713 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009714 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009715 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009716 msr_info.host_initiated = false;
9717 msr_info.index = e.index;
9718 if (kvm_get_msr(vcpu, &msr_info)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009719 pr_warn_ratelimited(
9720 "%s cannot read MSR (%u, 0x%x)\n",
9721 __func__, i, e.index);
9722 return -EINVAL;
9723 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009724 if (kvm_vcpu_write_guest(vcpu,
9725 gpa + i * sizeof(e) +
9726 offsetof(struct vmx_msr_entry, value),
9727 &msr_info.data, sizeof(msr_info.data))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009728 pr_warn_ratelimited(
9729 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009730 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009731 return -EINVAL;
9732 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009733 }
9734 return 0;
9735}
9736
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009737/*
9738 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9739 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009740 * 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 +03009741 * guest in a way that will both be appropriate to L1's requests, and our
9742 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9743 * function also has additional necessary side-effects, like setting various
9744 * vcpu->arch fields.
9745 */
9746static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9747{
9748 struct vcpu_vmx *vmx = to_vmx(vcpu);
9749 u32 exec_control;
9750
9751 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9752 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9753 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9754 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9755 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9756 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9757 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9758 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9759 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9760 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9761 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9762 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9763 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9764 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9765 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9766 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9767 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9768 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9769 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9770 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9771 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9772 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9773 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9774 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9775 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9776 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9777 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9778 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9779 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9780 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9781 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9782 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9783 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9784 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9785 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9786 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9787
Jan Kiszka2996fca2014-06-16 13:59:43 +02009788 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9789 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9790 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9791 } else {
9792 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9793 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9794 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009795 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9796 vmcs12->vm_entry_intr_info_field);
9797 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9798 vmcs12->vm_entry_exception_error_code);
9799 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9800 vmcs12->vm_entry_instruction_len);
9801 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9802 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009803 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009804 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009805 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9806 vmcs12->guest_pending_dbg_exceptions);
9807 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9808 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9809
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009810 if (nested_cpu_has_xsaves(vmcs12))
9811 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009812 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9813
Jan Kiszkaf4124502014-03-07 20:03:13 +01009814 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009815
Paolo Bonzini93140062016-07-06 13:23:51 +02009816 /* Preemption timer setting is only taken from vmcs01. */
9817 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9818 exec_control |= vmcs_config.pin_based_exec_ctrl;
9819 if (vmx->hv_deadline_tsc == -1)
9820 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9821
9822 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +08009823 if (nested_cpu_has_posted_intr(vmcs12)) {
9824 /*
9825 * Note that we use L0's vector here and in
9826 * vmx_deliver_nested_posted_interrupt.
9827 */
9828 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9829 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +08009830 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +08009831 vmcs_write64(POSTED_INTR_DESC_ADDR,
9832 page_to_phys(vmx->nested.pi_desc_page) +
9833 (unsigned long)(vmcs12->posted_intr_desc_addr &
9834 (PAGE_SIZE - 1)));
9835 } else
9836 exec_control &= ~PIN_BASED_POSTED_INTR;
9837
Jan Kiszkaf4124502014-03-07 20:03:13 +01009838 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009839
Jan Kiszkaf4124502014-03-07 20:03:13 +01009840 vmx->nested.preemption_timer_expired = false;
9841 if (nested_cpu_has_preemption_timer(vmcs12))
9842 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01009843
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009844 /*
9845 * Whether page-faults are trapped is determined by a combination of
9846 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
9847 * If enable_ept, L0 doesn't care about page faults and we should
9848 * set all of these to L1's desires. However, if !enable_ept, L0 does
9849 * care about (at least some) page faults, and because it is not easy
9850 * (if at all possible?) to merge L0 and L1's desires, we simply ask
9851 * to exit on each and every L2 page fault. This is done by setting
9852 * MASK=MATCH=0 and (see below) EB.PF=1.
9853 * Note that below we don't need special code to set EB.PF beyond the
9854 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
9855 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
9856 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
9857 *
9858 * A problem with this approach (when !enable_ept) is that L1 may be
9859 * injected with more page faults than it asked for. This could have
9860 * caused problems, but in practice existing hypervisors don't care.
9861 * To fix this, we will need to emulate the PFEC checking (on the L1
9862 * page tables), using walk_addr(), when injecting PFs to L1.
9863 */
9864 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
9865 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
9866 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
9867 enable_ept ? vmcs12->page_fault_error_code_match : 0);
9868
9869 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +01009870 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +08009871
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009872 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009873 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009874 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009875 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -07009876 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009877 if (nested_cpu_has(vmcs12,
9878 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
9879 exec_control |= vmcs12->secondary_vm_exec_control;
9880
9881 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
9882 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009883 * If translation failed, no matter: This feature asks
9884 * to exit when accessing the given address, and if it
9885 * can never be accessed, this feature won't do
9886 * anything anyway.
9887 */
9888 if (!vmx->nested.apic_access_page)
9889 exec_control &=
9890 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9891 else
9892 vmcs_write64(APIC_ACCESS_ADDR,
9893 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +08009894 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +02009895 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +01009896 exec_control |=
9897 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +08009898 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009899 }
9900
Wincy Van608406e2015-02-03 23:57:51 +08009901 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
9902 vmcs_write64(EOI_EXIT_BITMAP0,
9903 vmcs12->eoi_exit_bitmap0);
9904 vmcs_write64(EOI_EXIT_BITMAP1,
9905 vmcs12->eoi_exit_bitmap1);
9906 vmcs_write64(EOI_EXIT_BITMAP2,
9907 vmcs12->eoi_exit_bitmap2);
9908 vmcs_write64(EOI_EXIT_BITMAP3,
9909 vmcs12->eoi_exit_bitmap3);
9910 vmcs_write16(GUEST_INTR_STATUS,
9911 vmcs12->guest_intr_status);
9912 }
9913
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009914 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
9915 }
9916
9917
9918 /*
9919 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
9920 * Some constant fields are set here by vmx_set_constant_host_state().
9921 * Other fields are different per CPU, and will be set later when
9922 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
9923 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08009924 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009925
9926 /*
9927 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
9928 * entry, but only if the current (host) sp changed from the value
9929 * we wrote last (vmx->host_rsp). This cache is no longer relevant
9930 * if we switch vmcs, and rather than hold a separate cache per vmcs,
9931 * here we just force the write to happen on entry.
9932 */
9933 vmx->host_rsp = 0;
9934
9935 exec_control = vmx_exec_control(vmx); /* L0's desires */
9936 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
9937 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
9938 exec_control &= ~CPU_BASED_TPR_SHADOW;
9939 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009940
9941 if (exec_control & CPU_BASED_TPR_SHADOW) {
9942 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
9943 page_to_phys(vmx->nested.virtual_apic_page));
9944 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
9945 }
9946
Wincy Van3af18d92015-02-03 23:49:31 +08009947 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +02009948 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
9949 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9950 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
9951 else
Wincy Van3af18d92015-02-03 23:49:31 +08009952 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
9953
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009954 /*
Wincy Van3af18d92015-02-03 23:49:31 +08009955 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009956 * Rather, exit every time.
9957 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009958 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
9959 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
9960
9961 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
9962
9963 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
9964 * bitwise-or of what L1 wants to trap for L2, and what we want to
9965 * trap. Note that CR0.TS also needs updating - we do this later.
9966 */
9967 update_exception_bitmap(vcpu);
9968 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
9969 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
9970
Nadav Har'El8049d652013-08-05 11:07:06 +03009971 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
9972 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
9973 * bits are further modified by vmx_set_efer() below.
9974 */
Jan Kiszkaf4124502014-03-07 20:03:13 +01009975 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03009976
9977 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
9978 * emulated by vmx_set_efer(), below.
9979 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02009980 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03009981 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
9982 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009983 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
9984
Jan Kiszka44811c02013-08-04 17:17:27 +02009985 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009986 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02009987 vcpu->arch.pat = vmcs12->guest_ia32_pat;
9988 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009989 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
9990
9991
9992 set_cr4_guest_host_mask(vmx);
9993
Paolo Bonzini36be0b92014-02-24 12:30:04 +01009994 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
9995 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
9996
Nadav Har'El27fc51b2011-08-02 15:54:52 +03009997 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
9998 vmcs_write64(TSC_OFFSET,
9999 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
10000 else
10001 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010002
10003 if (enable_vpid) {
10004 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010005 * There is no direct mapping between vpid02 and vpid12, the
10006 * vpid02 is per-vCPU for L0 and reused while the value of
10007 * vpid12 is changed w/ one invvpid during nested vmentry.
10008 * The vpid12 is allocated by L1 for L2, so it will not
10009 * influence global bitmap(for vpid01 and vpid02 allocation)
10010 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010011 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010012 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10013 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10014 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10015 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10016 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10017 }
10018 } else {
10019 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10020 vmx_flush_tlb(vcpu);
10021 }
10022
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010023 }
10024
Nadav Har'El155a97a2013-08-05 11:07:16 +030010025 if (nested_cpu_has_ept(vmcs12)) {
10026 kvm_mmu_unload(vcpu);
10027 nested_ept_init_mmu_context(vcpu);
10028 }
10029
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010030 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10031 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010032 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010033 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10034 else
10035 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10036 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10037 vmx_set_efer(vcpu, vcpu->arch.efer);
10038
10039 /*
10040 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10041 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10042 * The CR0_READ_SHADOW is what L2 should have expected to read given
10043 * the specifications by L1; It's not enough to take
10044 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10045 * have more bits than L1 expected.
10046 */
10047 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10048 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10049
10050 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10051 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10052
10053 /* shadow page tables on either EPT or shadow page tables */
10054 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10055 kvm_mmu_reset_context(vcpu);
10056
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010057 if (!enable_ept)
10058 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10059
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010060 /*
10061 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10062 */
10063 if (enable_ept) {
10064 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10065 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10066 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10067 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10068 }
10069
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010070 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10071 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10072}
10073
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010074/*
10075 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10076 * for running an L2 nested guest.
10077 */
10078static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10079{
10080 struct vmcs12 *vmcs12;
10081 struct vcpu_vmx *vmx = to_vmx(vcpu);
10082 int cpu;
10083 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +020010084 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010085 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010086
10087 if (!nested_vmx_check_permission(vcpu) ||
10088 !nested_vmx_check_vmcs12(vcpu))
10089 return 1;
10090
10091 skip_emulated_instruction(vcpu);
10092 vmcs12 = get_vmcs12(vcpu);
10093
Abel Gordon012f83c2013-04-18 14:39:25 +030010094 if (enable_shadow_vmcs)
10095 copy_shadow_to_vmcs12(vmx);
10096
Nadav Har'El7c177932011-05-25 23:12:04 +030010097 /*
10098 * The nested entry process starts with enforcing various prerequisites
10099 * on vmcs12 as required by the Intel SDM, and act appropriately when
10100 * they fail: As the SDM explains, some conditions should cause the
10101 * instruction to fail, while others will cause the instruction to seem
10102 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10103 * To speed up the normal (success) code path, we should avoid checking
10104 * for misconfigurations which will anyway be caught by the processor
10105 * when using the merged vmcs02.
10106 */
10107 if (vmcs12->launch_state == launch) {
10108 nested_vmx_failValid(vcpu,
10109 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10110 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10111 return 1;
10112 }
10113
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010114 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10115 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010116 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10117 return 1;
10118 }
10119
Wincy Van3af18d92015-02-03 23:49:31 +080010120 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010121 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10122 return 1;
10123 }
10124
Wincy Van3af18d92015-02-03 23:49:31 +080010125 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010126 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10127 return 1;
10128 }
10129
Wincy Vanf2b93282015-02-03 23:56:03 +080010130 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10131 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10132 return 1;
10133 }
10134
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010135 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10136 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10137 return 1;
10138 }
10139
Nadav Har'El7c177932011-05-25 23:12:04 +030010140 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010141 vmx->nested.nested_vmx_true_procbased_ctls_low,
10142 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010143 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010144 vmx->nested.nested_vmx_secondary_ctls_low,
10145 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010146 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010147 vmx->nested.nested_vmx_pinbased_ctls_low,
10148 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010149 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010150 vmx->nested.nested_vmx_true_exit_ctls_low,
10151 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010152 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010153 vmx->nested.nested_vmx_true_entry_ctls_low,
10154 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010155 {
10156 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10157 return 1;
10158 }
10159
10160 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10161 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10162 nested_vmx_failValid(vcpu,
10163 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10164 return 1;
10165 }
10166
Wincy Vanb9c237b2015-02-03 23:56:30 +080010167 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010168 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10169 nested_vmx_entry_failure(vcpu, vmcs12,
10170 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10171 return 1;
10172 }
10173 if (vmcs12->vmcs_link_pointer != -1ull) {
10174 nested_vmx_entry_failure(vcpu, vmcs12,
10175 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10176 return 1;
10177 }
10178
10179 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010180 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010181 * are performed on the field for the IA32_EFER MSR:
10182 * - Bits reserved in the IA32_EFER MSR must be 0.
10183 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10184 * the IA-32e mode guest VM-exit control. It must also be identical
10185 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10186 * CR0.PG) is 1.
10187 */
10188 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10189 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10190 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10191 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10192 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10193 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10194 nested_vmx_entry_failure(vcpu, vmcs12,
10195 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10196 return 1;
10197 }
10198 }
10199
10200 /*
10201 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10202 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10203 * the values of the LMA and LME bits in the field must each be that of
10204 * the host address-space size VM-exit control.
10205 */
10206 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10207 ia32e = (vmcs12->vm_exit_controls &
10208 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10209 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10210 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10211 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10212 nested_vmx_entry_failure(vcpu, vmcs12,
10213 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10214 return 1;
10215 }
10216 }
10217
10218 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010219 * We're finally done with prerequisite checking, and can start with
10220 * the nested entry.
10221 */
10222
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010223 vmcs02 = nested_get_current_vmcs02(vmx);
10224 if (!vmcs02)
10225 return -ENOMEM;
10226
10227 enter_guest_mode(vcpu);
10228
10229 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
10230
Jan Kiszka2996fca2014-06-16 13:59:43 +020010231 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10232 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10233
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010234 cpu = get_cpu();
10235 vmx->loaded_vmcs = vmcs02;
10236 vmx_vcpu_put(vcpu);
10237 vmx_vcpu_load(vcpu, cpu);
10238 vcpu->cpu = cpu;
10239 put_cpu();
10240
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010241 vmx_segment_cache_clear(vmx);
10242
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010243 prepare_vmcs02(vcpu, vmcs12);
10244
Wincy Vanff651cb2014-12-11 08:52:58 +030010245 msr_entry_idx = nested_vmx_load_msr(vcpu,
10246 vmcs12->vm_entry_msr_load_addr,
10247 vmcs12->vm_entry_msr_load_count);
10248 if (msr_entry_idx) {
10249 leave_guest_mode(vcpu);
10250 vmx_load_vmcs01(vcpu);
10251 nested_vmx_entry_failure(vcpu, vmcs12,
10252 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10253 return 1;
10254 }
10255
10256 vmcs12->launch_state = 1;
10257
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010258 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010259 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010260
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010261 vmx->nested.nested_run_pending = 1;
10262
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010263 /*
10264 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10265 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10266 * returned as far as L1 is concerned. It will only return (and set
10267 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10268 */
10269 return 1;
10270}
10271
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010272/*
10273 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10274 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10275 * This function returns the new value we should put in vmcs12.guest_cr0.
10276 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10277 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10278 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10279 * didn't trap the bit, because if L1 did, so would L0).
10280 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10281 * been modified by L2, and L1 knows it. So just leave the old value of
10282 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10283 * isn't relevant, because if L0 traps this bit it can set it to anything.
10284 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10285 * changed these bits, and therefore they need to be updated, but L0
10286 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10287 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10288 */
10289static inline unsigned long
10290vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10291{
10292 return
10293 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10294 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10295 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10296 vcpu->arch.cr0_guest_owned_bits));
10297}
10298
10299static inline unsigned long
10300vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10301{
10302 return
10303 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10304 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10305 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10306 vcpu->arch.cr4_guest_owned_bits));
10307}
10308
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010309static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10310 struct vmcs12 *vmcs12)
10311{
10312 u32 idt_vectoring;
10313 unsigned int nr;
10314
Gleb Natapov851eb6672013-09-25 12:51:34 +030010315 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010316 nr = vcpu->arch.exception.nr;
10317 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10318
10319 if (kvm_exception_is_soft(nr)) {
10320 vmcs12->vm_exit_instruction_len =
10321 vcpu->arch.event_exit_inst_len;
10322 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10323 } else
10324 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10325
10326 if (vcpu->arch.exception.has_error_code) {
10327 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10328 vmcs12->idt_vectoring_error_code =
10329 vcpu->arch.exception.error_code;
10330 }
10331
10332 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010333 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010334 vmcs12->idt_vectoring_info_field =
10335 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10336 } else if (vcpu->arch.interrupt.pending) {
10337 nr = vcpu->arch.interrupt.nr;
10338 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10339
10340 if (vcpu->arch.interrupt.soft) {
10341 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10342 vmcs12->vm_entry_instruction_len =
10343 vcpu->arch.event_exit_inst_len;
10344 } else
10345 idt_vectoring |= INTR_TYPE_EXT_INTR;
10346
10347 vmcs12->idt_vectoring_info_field = idt_vectoring;
10348 }
10349}
10350
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010351static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10352{
10353 struct vcpu_vmx *vmx = to_vmx(vcpu);
10354
Jan Kiszkaf4124502014-03-07 20:03:13 +010010355 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10356 vmx->nested.preemption_timer_expired) {
10357 if (vmx->nested.nested_run_pending)
10358 return -EBUSY;
10359 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10360 return 0;
10361 }
10362
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010363 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010364 if (vmx->nested.nested_run_pending ||
10365 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010366 return -EBUSY;
10367 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10368 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10369 INTR_INFO_VALID_MASK, 0);
10370 /*
10371 * The NMI-triggered VM exit counts as injection:
10372 * clear this one and block further NMIs.
10373 */
10374 vcpu->arch.nmi_pending = 0;
10375 vmx_set_nmi_mask(vcpu, true);
10376 return 0;
10377 }
10378
10379 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10380 nested_exit_on_intr(vcpu)) {
10381 if (vmx->nested.nested_run_pending)
10382 return -EBUSY;
10383 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010384 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010385 }
10386
Wincy Van705699a2015-02-03 23:58:17 +080010387 return vmx_complete_nested_posted_interrupt(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010388}
10389
Jan Kiszkaf4124502014-03-07 20:03:13 +010010390static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10391{
10392 ktime_t remaining =
10393 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10394 u64 value;
10395
10396 if (ktime_to_ns(remaining) <= 0)
10397 return 0;
10398
10399 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10400 do_div(value, 1000000);
10401 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10402}
10403
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010404/*
10405 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10406 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10407 * and this function updates it to reflect the changes to the guest state while
10408 * L2 was running (and perhaps made some exits which were handled directly by L0
10409 * without going back to L1), and to reflect the exit reason.
10410 * Note that we do not have to copy here all VMCS fields, just those that
10411 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10412 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10413 * which already writes to vmcs12 directly.
10414 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010415static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10416 u32 exit_reason, u32 exit_intr_info,
10417 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010418{
10419 /* update guest state fields: */
10420 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10421 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10422
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010423 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10424 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10425 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10426
10427 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10428 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10429 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10430 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10431 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10432 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10433 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10434 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10435 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10436 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10437 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10438 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10439 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10440 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10441 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10442 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10443 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10444 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10445 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10446 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10447 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10448 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10449 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10450 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10451 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10452 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10453 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10454 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10455 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10456 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10457 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10458 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10459 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10460 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10461 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10462 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10463
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010464 vmcs12->guest_interruptibility_info =
10465 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10466 vmcs12->guest_pending_dbg_exceptions =
10467 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010468 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10469 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10470 else
10471 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010472
Jan Kiszkaf4124502014-03-07 20:03:13 +010010473 if (nested_cpu_has_preemption_timer(vmcs12)) {
10474 if (vmcs12->vm_exit_controls &
10475 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10476 vmcs12->vmx_preemption_timer_value =
10477 vmx_get_preemption_timer_value(vcpu);
10478 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10479 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010480
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010481 /*
10482 * In some cases (usually, nested EPT), L2 is allowed to change its
10483 * own CR3 without exiting. If it has changed it, we must keep it.
10484 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10485 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10486 *
10487 * Additionally, restore L2's PDPTR to vmcs12.
10488 */
10489 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010490 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010491 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10492 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10493 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10494 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10495 }
10496
Wincy Van608406e2015-02-03 23:57:51 +080010497 if (nested_cpu_has_vid(vmcs12))
10498 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10499
Jan Kiszkac18911a2013-03-13 16:06:41 +010010500 vmcs12->vm_entry_controls =
10501 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010502 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010503
Jan Kiszka2996fca2014-06-16 13:59:43 +020010504 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10505 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10506 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10507 }
10508
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010509 /* TODO: These cannot have changed unless we have MSR bitmaps and
10510 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010511 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010512 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010513 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10514 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010515 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10516 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10517 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010518 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010519 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010520 if (nested_cpu_has_xsaves(vmcs12))
10521 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010522
10523 /* update exit information fields: */
10524
Jan Kiszka533558b2014-01-04 18:47:20 +010010525 vmcs12->vm_exit_reason = exit_reason;
10526 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010527
Jan Kiszka533558b2014-01-04 18:47:20 +010010528 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010529 if ((vmcs12->vm_exit_intr_info &
10530 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10531 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10532 vmcs12->vm_exit_intr_error_code =
10533 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010534 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010535 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10536 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10537
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010538 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10539 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10540 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010541 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010542
10543 /*
10544 * Transfer the event that L0 or L1 may wanted to inject into
10545 * L2 to IDT_VECTORING_INFO_FIELD.
10546 */
10547 vmcs12_save_pending_event(vcpu, vmcs12);
10548 }
10549
10550 /*
10551 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10552 * preserved above and would only end up incorrectly in L1.
10553 */
10554 vcpu->arch.nmi_injected = false;
10555 kvm_clear_exception_queue(vcpu);
10556 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010557}
10558
10559/*
10560 * A part of what we need to when the nested L2 guest exits and we want to
10561 * run its L1 parent, is to reset L1's guest state to the host state specified
10562 * in vmcs12.
10563 * This function is to be called not only on normal nested exit, but also on
10564 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10565 * Failures During or After Loading Guest State").
10566 * This function should be called when the active VMCS is L1's (vmcs01).
10567 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010568static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10569 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010570{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010571 struct kvm_segment seg;
10572
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010573 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10574 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010575 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010576 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10577 else
10578 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10579 vmx_set_efer(vcpu, vcpu->arch.efer);
10580
10581 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10582 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010583 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010584 /*
10585 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10586 * actually changed, because it depends on the current state of
10587 * fpu_active (which may have changed).
10588 * Note that vmx_set_cr0 refers to efer set above.
10589 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010590 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010591 /*
10592 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10593 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10594 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10595 */
10596 update_exception_bitmap(vcpu);
10597 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10598 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10599
10600 /*
10601 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10602 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10603 */
10604 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10605 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10606
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010607 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010608
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010609 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10610 kvm_mmu_reset_context(vcpu);
10611
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010612 if (!enable_ept)
10613 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10614
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010615 if (enable_vpid) {
10616 /*
10617 * Trivially support vpid by letting L2s share their parent
10618 * L1's vpid. TODO: move to a more elaborate solution, giving
10619 * each L2 its own vpid and exposing the vpid feature to L1.
10620 */
10621 vmx_flush_tlb(vcpu);
10622 }
10623
10624
10625 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10626 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10627 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10628 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10629 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010630
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010631 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10632 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10633 vmcs_write64(GUEST_BNDCFGS, 0);
10634
Jan Kiszka44811c02013-08-04 17:17:27 +020010635 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010636 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010637 vcpu->arch.pat = vmcs12->host_ia32_pat;
10638 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010639 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10640 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10641 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010642
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010643 /* Set L1 segment info according to Intel SDM
10644 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10645 seg = (struct kvm_segment) {
10646 .base = 0,
10647 .limit = 0xFFFFFFFF,
10648 .selector = vmcs12->host_cs_selector,
10649 .type = 11,
10650 .present = 1,
10651 .s = 1,
10652 .g = 1
10653 };
10654 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10655 seg.l = 1;
10656 else
10657 seg.db = 1;
10658 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10659 seg = (struct kvm_segment) {
10660 .base = 0,
10661 .limit = 0xFFFFFFFF,
10662 .type = 3,
10663 .present = 1,
10664 .s = 1,
10665 .db = 1,
10666 .g = 1
10667 };
10668 seg.selector = vmcs12->host_ds_selector;
10669 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10670 seg.selector = vmcs12->host_es_selector;
10671 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10672 seg.selector = vmcs12->host_ss_selector;
10673 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10674 seg.selector = vmcs12->host_fs_selector;
10675 seg.base = vmcs12->host_fs_base;
10676 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10677 seg.selector = vmcs12->host_gs_selector;
10678 seg.base = vmcs12->host_gs_base;
10679 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10680 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010681 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010682 .limit = 0x67,
10683 .selector = vmcs12->host_tr_selector,
10684 .type = 11,
10685 .present = 1
10686 };
10687 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10688
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010689 kvm_set_dr(vcpu, 7, 0x400);
10690 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010691
Wincy Van3af18d92015-02-03 23:49:31 +080010692 if (cpu_has_vmx_msr_bitmap())
10693 vmx_set_msr_bitmap(vcpu);
10694
Wincy Vanff651cb2014-12-11 08:52:58 +030010695 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10696 vmcs12->vm_exit_msr_load_count))
10697 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010698}
10699
10700/*
10701 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10702 * and modify vmcs12 to make it see what it would expect to see there if
10703 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10704 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010705static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10706 u32 exit_intr_info,
10707 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010708{
10709 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010710 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10711
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010712 /* trying to cancel vmlaunch/vmresume is a bug */
10713 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10714
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010715 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010716 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10717 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010718
Wincy Vanff651cb2014-12-11 08:52:58 +030010719 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10720 vmcs12->vm_exit_msr_store_count))
10721 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10722
Wanpeng Lif3380ca2014-08-05 12:42:23 +080010723 vmx_load_vmcs01(vcpu);
10724
Bandan Das77b0f5d2014-04-19 18:17:45 -040010725 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10726 && nested_exit_intr_ack_set(vcpu)) {
10727 int irq = kvm_cpu_get_interrupt(vcpu);
10728 WARN_ON(irq < 0);
10729 vmcs12->vm_exit_intr_info = irq |
10730 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10731 }
10732
Jan Kiszka542060e2014-01-04 18:47:21 +010010733 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10734 vmcs12->exit_qualification,
10735 vmcs12->idt_vectoring_info_field,
10736 vmcs12->vm_exit_intr_info,
10737 vmcs12->vm_exit_intr_error_code,
10738 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010739
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010740 vm_entry_controls_reset_shadow(vmx);
10741 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010742 vmx_segment_cache_clear(vmx);
10743
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010744 /* if no vmcs02 cache requested, remove the one we used */
10745 if (VMCS02_POOL_SIZE == 0)
10746 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10747
10748 load_vmcs12_host_state(vcpu, vmcs12);
10749
Paolo Bonzini93140062016-07-06 13:23:51 +020010750 /* Update any VMCS fields that might have changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010751 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020010752 if (vmx->hv_deadline_tsc == -1)
10753 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10754 PIN_BASED_VMX_PREEMPTION_TIMER);
10755 else
10756 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10757 PIN_BASED_VMX_PREEMPTION_TIMER);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010758
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010759 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10760 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10761 vmx_set_virtual_x2apic_mode(vcpu,
10762 vcpu->arch.apic_base & X2APIC_ENABLE);
10763 }
10764
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010765 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10766 vmx->host_rsp = 0;
10767
10768 /* Unpin physical memory we referred to in vmcs02 */
10769 if (vmx->nested.apic_access_page) {
10770 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010771 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010772 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010773 if (vmx->nested.virtual_apic_page) {
10774 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010775 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010776 }
Wincy Van705699a2015-02-03 23:58:17 +080010777 if (vmx->nested.pi_desc_page) {
10778 kunmap(vmx->nested.pi_desc_page);
10779 nested_release_page(vmx->nested.pi_desc_page);
10780 vmx->nested.pi_desc_page = NULL;
10781 vmx->nested.pi_desc = NULL;
10782 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010783
10784 /*
Tang Chen38b99172014-09-24 15:57:54 +080010785 * We are now running in L2, mmu_notifier will force to reload the
10786 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10787 */
10788 kvm_vcpu_reload_apic_access_page(vcpu);
10789
10790 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010791 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10792 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10793 * success or failure flag accordingly.
10794 */
10795 if (unlikely(vmx->fail)) {
10796 vmx->fail = 0;
10797 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
10798 } else
10799 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010800 if (enable_shadow_vmcs)
10801 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010802
10803 /* in case we halted in L2 */
10804 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010805}
10806
Nadav Har'El7c177932011-05-25 23:12:04 +030010807/*
Jan Kiszka42124922014-01-04 18:47:19 +010010808 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10809 */
10810static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10811{
10812 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +010010813 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +010010814 free_nested(to_vmx(vcpu));
10815}
10816
10817/*
Nadav Har'El7c177932011-05-25 23:12:04 +030010818 * L1's failure to enter L2 is a subset of a normal exit, as explained in
10819 * 23.7 "VM-entry failures during or after loading guest state" (this also
10820 * lists the acceptable exit-reason and exit-qualification parameters).
10821 * It should only be called before L2 actually succeeded to run, and when
10822 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
10823 */
10824static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
10825 struct vmcs12 *vmcs12,
10826 u32 reason, unsigned long qualification)
10827{
10828 load_vmcs12_host_state(vcpu, vmcs12);
10829 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
10830 vmcs12->exit_qualification = qualification;
10831 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010832 if (enable_shadow_vmcs)
10833 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030010834}
10835
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010836static int vmx_check_intercept(struct kvm_vcpu *vcpu,
10837 struct x86_instruction_info *info,
10838 enum x86_intercept_stage stage)
10839{
10840 return X86EMUL_CONTINUE;
10841}
10842
Yunhong Jiang64672c92016-06-13 14:19:59 -070010843#ifdef CONFIG_X86_64
10844/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
10845static inline int u64_shl_div_u64(u64 a, unsigned int shift,
10846 u64 divisor, u64 *result)
10847{
10848 u64 low = a << shift, high = a >> (64 - shift);
10849
10850 /* To avoid the overflow on divq */
10851 if (high >= divisor)
10852 return 1;
10853
10854 /* Low hold the result, high hold rem which is discarded */
10855 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
10856 "rm" (divisor), "0" (low), "1" (high));
10857 *result = low;
10858
10859 return 0;
10860}
10861
10862static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
10863{
10864 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020010865 u64 tscl = rdtsc();
10866 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
10867 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010868
10869 /* Convert to host delta tsc if tsc scaling is enabled */
10870 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
10871 u64_shl_div_u64(delta_tsc,
10872 kvm_tsc_scaling_ratio_frac_bits,
10873 vcpu->arch.tsc_scaling_ratio,
10874 &delta_tsc))
10875 return -ERANGE;
10876
10877 /*
10878 * If the delta tsc can't fit in the 32 bit after the multi shift,
10879 * we can't use the preemption timer.
10880 * It's possible that it fits on later vmentries, but checking
10881 * on every vmentry is costly so we just use an hrtimer.
10882 */
10883 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
10884 return -ERANGE;
10885
10886 vmx->hv_deadline_tsc = tscl + delta_tsc;
10887 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10888 PIN_BASED_VMX_PREEMPTION_TIMER);
10889 return 0;
10890}
10891
10892static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
10893{
10894 struct vcpu_vmx *vmx = to_vmx(vcpu);
10895 vmx->hv_deadline_tsc = -1;
10896 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10897 PIN_BASED_VMX_PREEMPTION_TIMER);
10898}
10899#endif
10900
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010901static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010902{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020010903 if (ple_gap)
10904 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010905}
10906
Kai Huang843e4332015-01-28 10:54:28 +080010907static void vmx_slot_enable_log_dirty(struct kvm *kvm,
10908 struct kvm_memory_slot *slot)
10909{
10910 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
10911 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
10912}
10913
10914static void vmx_slot_disable_log_dirty(struct kvm *kvm,
10915 struct kvm_memory_slot *slot)
10916{
10917 kvm_mmu_slot_set_dirty(kvm, slot);
10918}
10919
10920static void vmx_flush_log_dirty(struct kvm *kvm)
10921{
10922 kvm_flush_pml_buffers(kvm);
10923}
10924
10925static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
10926 struct kvm_memory_slot *memslot,
10927 gfn_t offset, unsigned long mask)
10928{
10929 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
10930}
10931
Feng Wuefc64402015-09-18 22:29:51 +080010932/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080010933 * This routine does the following things for vCPU which is going
10934 * to be blocked if VT-d PI is enabled.
10935 * - Store the vCPU to the wakeup list, so when interrupts happen
10936 * we can find the right vCPU to wake up.
10937 * - Change the Posted-interrupt descriptor as below:
10938 * 'NDST' <-- vcpu->pre_pcpu
10939 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
10940 * - If 'ON' is set during this process, which means at least one
10941 * interrupt is posted for this vCPU, we cannot block it, in
10942 * this case, return 1, otherwise, return 0.
10943 *
10944 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070010945static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080010946{
10947 unsigned long flags;
10948 unsigned int dest;
10949 struct pi_desc old, new;
10950 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
10951
10952 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080010953 !irq_remapping_cap(IRQ_POSTING_CAP) ||
10954 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080010955 return 0;
10956
10957 vcpu->pre_pcpu = vcpu->cpu;
10958 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10959 vcpu->pre_pcpu), flags);
10960 list_add_tail(&vcpu->blocked_vcpu_list,
10961 &per_cpu(blocked_vcpu_on_cpu,
10962 vcpu->pre_pcpu));
10963 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
10964 vcpu->pre_pcpu), flags);
10965
10966 do {
10967 old.control = new.control = pi_desc->control;
10968
10969 /*
10970 * We should not block the vCPU if
10971 * an interrupt is posted for it.
10972 */
10973 if (pi_test_on(pi_desc) == 1) {
10974 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10975 vcpu->pre_pcpu), flags);
10976 list_del(&vcpu->blocked_vcpu_list);
10977 spin_unlock_irqrestore(
10978 &per_cpu(blocked_vcpu_on_cpu_lock,
10979 vcpu->pre_pcpu), flags);
10980 vcpu->pre_pcpu = -1;
10981
10982 return 1;
10983 }
10984
10985 WARN((pi_desc->sn == 1),
10986 "Warning: SN field of posted-interrupts "
10987 "is set before blocking\n");
10988
10989 /*
10990 * Since vCPU can be preempted during this process,
10991 * vcpu->cpu could be different with pre_pcpu, we
10992 * need to set pre_pcpu as the destination of wakeup
10993 * notification event, then we can find the right vCPU
10994 * to wakeup in wakeup handler if interrupts happen
10995 * when the vCPU is in blocked state.
10996 */
10997 dest = cpu_physical_id(vcpu->pre_pcpu);
10998
10999 if (x2apic_enabled())
11000 new.ndst = dest;
11001 else
11002 new.ndst = (dest << 8) & 0xFF00;
11003
11004 /* set 'NV' to 'wakeup vector' */
11005 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11006 } while (cmpxchg(&pi_desc->control, old.control,
11007 new.control) != old.control);
11008
11009 return 0;
11010}
11011
Yunhong Jiangbc225122016-06-13 14:19:58 -070011012static int vmx_pre_block(struct kvm_vcpu *vcpu)
11013{
11014 if (pi_pre_block(vcpu))
11015 return 1;
11016
Yunhong Jiang64672c92016-06-13 14:19:59 -070011017 if (kvm_lapic_hv_timer_in_use(vcpu))
11018 kvm_lapic_switch_to_sw_timer(vcpu);
11019
Yunhong Jiangbc225122016-06-13 14:19:58 -070011020 return 0;
11021}
11022
11023static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011024{
11025 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11026 struct pi_desc old, new;
11027 unsigned int dest;
11028 unsigned long flags;
11029
11030 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011031 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11032 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011033 return;
11034
11035 do {
11036 old.control = new.control = pi_desc->control;
11037
11038 dest = cpu_physical_id(vcpu->cpu);
11039
11040 if (x2apic_enabled())
11041 new.ndst = dest;
11042 else
11043 new.ndst = (dest << 8) & 0xFF00;
11044
11045 /* Allow posting non-urgent interrupts */
11046 new.sn = 0;
11047
11048 /* set 'NV' to 'notification vector' */
11049 new.nv = POSTED_INTR_VECTOR;
11050 } while (cmpxchg(&pi_desc->control, old.control,
11051 new.control) != old.control);
11052
11053 if(vcpu->pre_pcpu != -1) {
11054 spin_lock_irqsave(
11055 &per_cpu(blocked_vcpu_on_cpu_lock,
11056 vcpu->pre_pcpu), flags);
11057 list_del(&vcpu->blocked_vcpu_list);
11058 spin_unlock_irqrestore(
11059 &per_cpu(blocked_vcpu_on_cpu_lock,
11060 vcpu->pre_pcpu), flags);
11061 vcpu->pre_pcpu = -1;
11062 }
11063}
11064
Yunhong Jiangbc225122016-06-13 14:19:58 -070011065static void vmx_post_block(struct kvm_vcpu *vcpu)
11066{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011067 if (kvm_x86_ops->set_hv_timer)
11068 kvm_lapic_switch_to_hv_timer(vcpu);
11069
Yunhong Jiangbc225122016-06-13 14:19:58 -070011070 pi_post_block(vcpu);
11071}
11072
Feng Wubf9f6ac2015-09-18 22:29:55 +080011073/*
Feng Wuefc64402015-09-18 22:29:51 +080011074 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11075 *
11076 * @kvm: kvm
11077 * @host_irq: host irq of the interrupt
11078 * @guest_irq: gsi of the interrupt
11079 * @set: set or unset PI
11080 * returns 0 on success, < 0 on failure
11081 */
11082static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11083 uint32_t guest_irq, bool set)
11084{
11085 struct kvm_kernel_irq_routing_entry *e;
11086 struct kvm_irq_routing_table *irq_rt;
11087 struct kvm_lapic_irq irq;
11088 struct kvm_vcpu *vcpu;
11089 struct vcpu_data vcpu_info;
11090 int idx, ret = -EINVAL;
11091
11092 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011093 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11094 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011095 return 0;
11096
11097 idx = srcu_read_lock(&kvm->irq_srcu);
11098 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11099 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
11100
11101 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11102 if (e->type != KVM_IRQ_ROUTING_MSI)
11103 continue;
11104 /*
11105 * VT-d PI cannot support posting multicast/broadcast
11106 * interrupts to a vCPU, we still use interrupt remapping
11107 * for these kind of interrupts.
11108 *
11109 * For lowest-priority interrupts, we only support
11110 * those with single CPU as the destination, e.g. user
11111 * configures the interrupts via /proc/irq or uses
11112 * irqbalance to make the interrupts single-CPU.
11113 *
11114 * We will support full lowest-priority interrupt later.
11115 */
11116
Radim Krčmář371313132016-07-12 22:09:27 +020011117 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011118 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11119 /*
11120 * Make sure the IRTE is in remapped mode if
11121 * we don't handle it in posted mode.
11122 */
11123 ret = irq_set_vcpu_affinity(host_irq, NULL);
11124 if (ret < 0) {
11125 printk(KERN_INFO
11126 "failed to back to remapped mode, irq: %u\n",
11127 host_irq);
11128 goto out;
11129 }
11130
Feng Wuefc64402015-09-18 22:29:51 +080011131 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011132 }
Feng Wuefc64402015-09-18 22:29:51 +080011133
11134 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11135 vcpu_info.vector = irq.vector;
11136
Feng Wub6ce9782016-01-25 16:53:35 +080011137 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011138 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11139
11140 if (set)
11141 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11142 else {
11143 /* suppress notification event before unposting */
11144 pi_set_sn(vcpu_to_pi_desc(vcpu));
11145 ret = irq_set_vcpu_affinity(host_irq, NULL);
11146 pi_clear_sn(vcpu_to_pi_desc(vcpu));
11147 }
11148
11149 if (ret < 0) {
11150 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11151 __func__);
11152 goto out;
11153 }
11154 }
11155
11156 ret = 0;
11157out:
11158 srcu_read_unlock(&kvm->irq_srcu, idx);
11159 return ret;
11160}
11161
Ashok Rajc45dcc72016-06-22 14:59:56 +080011162static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11163{
11164 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11165 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11166 FEATURE_CONTROL_LMCE;
11167 else
11168 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11169 ~FEATURE_CONTROL_LMCE;
11170}
11171
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030011172static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011173 .cpu_has_kvm_support = cpu_has_kvm_support,
11174 .disabled_by_bios = vmx_disabled_by_bios,
11175 .hardware_setup = hardware_setup,
11176 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011177 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011178 .hardware_enable = hardware_enable,
11179 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011180 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020011181 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011182
11183 .vcpu_create = vmx_create_vcpu,
11184 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011185 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011186
Avi Kivity04d2cc72007-09-10 18:10:54 +030011187 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011188 .vcpu_load = vmx_vcpu_load,
11189 .vcpu_put = vmx_vcpu_put,
11190
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011191 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011192 .get_msr = vmx_get_msr,
11193 .set_msr = vmx_set_msr,
11194 .get_segment_base = vmx_get_segment_base,
11195 .get_segment = vmx_get_segment,
11196 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011197 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011198 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011199 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011200 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011201 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011202 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011203 .set_cr3 = vmx_set_cr3,
11204 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011205 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011206 .get_idt = vmx_get_idt,
11207 .set_idt = vmx_set_idt,
11208 .get_gdt = vmx_get_gdt,
11209 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011210 .get_dr6 = vmx_get_dr6,
11211 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011212 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011213 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011214 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011215 .get_rflags = vmx_get_rflags,
11216 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011217
11218 .get_pkru = vmx_get_pkru,
11219
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011220 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011221 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011222
11223 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011224
Avi Kivity6aa8b732006-12-10 02:21:36 -080011225 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011226 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011227 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011228 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11229 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011230 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011231 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011232 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011233 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011234 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011235 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011236 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011237 .get_nmi_mask = vmx_get_nmi_mask,
11238 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011239 .enable_nmi_window = enable_nmi_window,
11240 .enable_irq_window = enable_irq_window,
11241 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011242 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011243 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011244 .get_enable_apicv = vmx_get_enable_apicv,
11245 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011246 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11247 .hwapic_irr_update = vmx_hwapic_irr_update,
11248 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011249 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11250 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011251
Izik Eiduscbc94022007-10-25 00:29:55 +020011252 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011253 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011254 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011255
Avi Kivity586f9602010-11-18 13:09:54 +020011256 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011257
Sheng Yang17cc3932010-01-05 19:02:27 +080011258 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011259
11260 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011261
11262 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011263 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011264
11265 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011266
11267 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011268
Will Auldba904632012-11-29 12:42:50 -080011269 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011270 .write_tsc_offset = vmx_write_tsc_offset,
Haozhong Zhang58ea6762015-10-20 15:39:06 +080011271 .adjust_tsc_offset_guest = vmx_adjust_tsc_offset_guest,
Nadav Har'Eld5c17852011-08-02 15:54:20 +030011272 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011273
11274 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011275
11276 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011277 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011278 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011279 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011280
11281 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011282
11283 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011284
11285 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11286 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11287 .flush_log_dirty = vmx_flush_log_dirty,
11288 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f7f2015-06-19 15:45:05 +020011289
Feng Wubf9f6ac2015-09-18 22:29:55 +080011290 .pre_block = vmx_pre_block,
11291 .post_block = vmx_post_block,
11292
Wei Huang25462f7f2015-06-19 15:45:05 +020011293 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011294
11295 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011296
11297#ifdef CONFIG_X86_64
11298 .set_hv_timer = vmx_set_hv_timer,
11299 .cancel_hv_timer = vmx_cancel_hv_timer,
11300#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011301
11302 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011303};
11304
11305static int __init vmx_init(void)
11306{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011307 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11308 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011309 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011310 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011311
Dave Young2965faa2015-09-09 15:38:55 -070011312#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011313 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11314 crash_vmclear_local_loaded_vmcss);
11315#endif
11316
He, Qingfdef3ad2007-04-30 09:45:24 +030011317 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011318}
11319
11320static void __exit vmx_exit(void)
11321{
Dave Young2965faa2015-09-09 15:38:55 -070011322#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011323 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011324 synchronize_rcu();
11325#endif
11326
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011327 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011328}
11329
11330module_init(vmx_init)
11331module_exit(vmx_exit)