blob: 754c3a7f444a79d223647f655d283bd4b90fc270 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030036#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030037#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040038
Feng Wu28b835d2015-09-18 22:29:54 +080039#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080040#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080041#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020042#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020043#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080044#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020045#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020046#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010047#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080048#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010049#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080050#include <asm/irq_remapping.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Marcelo Tosatti229456f2009-06-17 09:22:14 -030052#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020053#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030054
Avi Kivity4ecac3f2008-05-13 13:23:38 +030055#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040056#define __ex_clear(x, reg) \
57 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030058
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Josh Triplette9bda3b2012-03-20 23:33:51 -070062static const struct x86_cpu_id vmx_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_VMX),
64 {}
65};
66MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
67
Rusty Russell476bc002012-01-13 09:32:18 +103068static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020069module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080070
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020072module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020073
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080076
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070078module_param_named(unrestricted_guest,
79 enable_unrestricted_guest, bool, S_IRUGO);
80
Xudong Hao83c3a332012-05-28 19:33:35 +080081static bool __read_mostly enable_ept_ad_bits = 1;
82module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
83
Avi Kivitya27685c2012-06-12 20:30:18 +030084static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020085module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080088module_param(vmm_exclusive, bool, S_IRUGO);
89
Rusty Russell476bc002012-01-13 09:32:18 +103090static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030091module_param(fasteoi, bool, S_IRUGO);
92
Yang Zhang5a717852013-04-11 19:25:16 +080093static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080094module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080095
Abel Gordonabc4fc52013-04-18 14:35:25 +030096static bool __read_mostly enable_shadow_vmcs = 1;
97module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030098/*
99 * If nested=1, nested virtualization is supported, i.e., guests may use
100 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
101 * use VMX instructions.
102 */
Rusty Russell476bc002012-01-13 09:32:18 +1030103static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300104module_param(nested, bool, S_IRUGO);
105
Wanpeng Li20300092014-12-02 19:14:59 +0800106static u64 __read_mostly host_xss;
107
Kai Huang843e4332015-01-28 10:54:28 +0800108static bool __read_mostly enable_pml = 1;
109module_param_named(pml, enable_pml, bool, S_IRUGO);
110
Haozhong Zhang64903d62015-10-20 15:39:09 +0800111#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
112
Yunhong Jiang64672c92016-06-13 14:19:59 -0700113/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
114static int __read_mostly cpu_preemption_timer_multi;
115static bool __read_mostly enable_preemption_timer = 1;
116#ifdef CONFIG_X86_64
117module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
118#endif
119
Gleb Natapov50378782013-02-04 16:00:28 +0200120#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
121#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200122#define KVM_VM_CR0_ALWAYS_ON \
123 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200124#define KVM_CR4_GUEST_OWNED_BITS \
125 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700126 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200127
Avi Kivitycdc0e242009-12-06 17:21:14 +0200128#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
129#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
130
Avi Kivity78ac8b42010-04-08 18:19:35 +0300131#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
132
Jan Kiszkaf41245002014-03-07 20:03:13 +0100133#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
134
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800135/*
136 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
137 * ple_gap: upper bound on the amount of time between two successive
138 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500139 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800140 * ple_window: upper bound on the amount of time a guest is allowed to execute
141 * in a PAUSE loop. Tests indicate that most spinlocks are held for
142 * less than 2^12 cycles
143 * Time is measured based on a counter that runs at the same rate as the TSC,
144 * refer SDM volume 3b section 21.6.13 & 22.1.3.
145 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200146#define KVM_VMX_DEFAULT_PLE_GAP 128
147#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
148#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
149#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
150#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
151 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
152
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
154module_param(ple_gap, int, S_IRUGO);
155
156static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
157module_param(ple_window, int, S_IRUGO);
158
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200159/* Default doubles per-vcpu window every exit. */
160static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
161module_param(ple_window_grow, int, S_IRUGO);
162
163/* Default resets per-vcpu window every exit to ple_window. */
164static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
165module_param(ple_window_shrink, int, S_IRUGO);
166
167/* Default is to compute the maximum so we can never overflow. */
168static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
169static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
170module_param(ple_window_max, int, S_IRUGO);
171
Avi Kivity83287ea422012-09-16 15:10:57 +0300172extern const ulong vmx_return;
173
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200174#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300175#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300176
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400177struct vmcs {
178 u32 revision_id;
179 u32 abort;
180 char data[0];
181};
182
Nadav Har'Eld462b812011-05-24 15:26:10 +0300183/*
184 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
185 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
186 * loaded on this CPU (so we can clear them if the CPU goes down).
187 */
188struct loaded_vmcs {
189 struct vmcs *vmcs;
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;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200424 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300425 /* L2 must run next, and mustn't decide to exit to L1. */
426 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300427 /*
428 * Guest pages referred to in vmcs02 with host-physical pointers, so
429 * we must keep them pinned while L2 runs.
430 */
431 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800432 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800433 struct page *pi_desc_page;
434 struct pi_desc *pi_desc;
435 bool pi_pending;
436 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100437
Radim Krčmářd048c092016-08-08 20:16:22 +0200438 unsigned long *msr_bitmap;
439
Jan Kiszkaf41245002014-03-07 20:03:13 +0100440 struct hrtimer preemption_timer;
441 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200442
443 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
444 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800445
Wanpeng Li5c614b32015-10-13 09:18:36 -0700446 u16 vpid02;
447 u16 last_vpid;
448
Wincy Vanb9c237b2015-02-03 23:56:30 +0800449 u32 nested_vmx_procbased_ctls_low;
450 u32 nested_vmx_procbased_ctls_high;
451 u32 nested_vmx_true_procbased_ctls_low;
452 u32 nested_vmx_secondary_ctls_low;
453 u32 nested_vmx_secondary_ctls_high;
454 u32 nested_vmx_pinbased_ctls_low;
455 u32 nested_vmx_pinbased_ctls_high;
456 u32 nested_vmx_exit_ctls_low;
457 u32 nested_vmx_exit_ctls_high;
458 u32 nested_vmx_true_exit_ctls_low;
459 u32 nested_vmx_entry_ctls_low;
460 u32 nested_vmx_entry_ctls_high;
461 u32 nested_vmx_true_entry_ctls_low;
462 u32 nested_vmx_misc_low;
463 u32 nested_vmx_misc_high;
464 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700465 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300466};
467
Yang Zhang01e439b2013-04-11 19:25:12 +0800468#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800469#define POSTED_INTR_SN 1
470
Yang Zhang01e439b2013-04-11 19:25:12 +0800471/* Posted-Interrupt Descriptor */
472struct pi_desc {
473 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800474 union {
475 struct {
476 /* bit 256 - Outstanding Notification */
477 u16 on : 1,
478 /* bit 257 - Suppress Notification */
479 sn : 1,
480 /* bit 271:258 - Reserved */
481 rsvd_1 : 14;
482 /* bit 279:272 - Notification Vector */
483 u8 nv;
484 /* bit 287:280 - Reserved */
485 u8 rsvd_2;
486 /* bit 319:288 - Notification Destination */
487 u32 ndst;
488 };
489 u64 control;
490 };
491 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800492} __aligned(64);
493
Yang Zhanga20ed542013-04-11 19:25:15 +0800494static bool pi_test_and_set_on(struct pi_desc *pi_desc)
495{
496 return test_and_set_bit(POSTED_INTR_ON,
497 (unsigned long *)&pi_desc->control);
498}
499
500static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
501{
502 return test_and_clear_bit(POSTED_INTR_ON,
503 (unsigned long *)&pi_desc->control);
504}
505
506static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
507{
508 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
509}
510
Feng Wuebbfc762015-09-18 22:29:46 +0800511static inline void pi_clear_sn(struct pi_desc *pi_desc)
512{
513 return clear_bit(POSTED_INTR_SN,
514 (unsigned long *)&pi_desc->control);
515}
516
517static inline void pi_set_sn(struct pi_desc *pi_desc)
518{
519 return set_bit(POSTED_INTR_SN,
520 (unsigned long *)&pi_desc->control);
521}
522
523static inline int pi_test_on(struct pi_desc *pi_desc)
524{
525 return test_bit(POSTED_INTR_ON,
526 (unsigned long *)&pi_desc->control);
527}
528
529static inline int pi_test_sn(struct pi_desc *pi_desc)
530{
531 return test_bit(POSTED_INTR_SN,
532 (unsigned long *)&pi_desc->control);
533}
534
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400535struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000536 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300537 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300538 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200539 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300540 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200541 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200542 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300543 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400544 int nmsrs;
545 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800546 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400547#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300548 u64 msr_host_kernel_gs_base;
549 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400550#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200551 u32 vm_entry_controls_shadow;
552 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300553 /*
554 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
555 * non-nested (L1) guest, it always points to vmcs01. For a nested
556 * guest (L2), it points to a different VMCS.
557 */
558 struct loaded_vmcs vmcs01;
559 struct loaded_vmcs *loaded_vmcs;
560 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300561 struct msr_autoload {
562 unsigned nr;
563 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
564 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
565 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400566 struct {
567 int loaded;
568 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300569#ifdef CONFIG_X86_64
570 u16 ds_sel, es_sel;
571#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200572 int gs_ldt_reload_needed;
573 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000574 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700575 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400576 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200577 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300578 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300579 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300580 struct kvm_segment segs[8];
581 } rmode;
582 struct {
583 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300584 struct kvm_save_segment {
585 u16 selector;
586 unsigned long base;
587 u32 limit;
588 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300589 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300590 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800591 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300592 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200593
594 /* Support for vnmi-less CPUs */
595 int soft_vnmi_blocked;
596 ktime_t entry_time;
597 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800598 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800599
Yang Zhang01e439b2013-04-11 19:25:12 +0800600 /* Posted interrupt descriptor */
601 struct pi_desc pi_desc;
602
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300603 /* Support for a guest hypervisor (nested VMX) */
604 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200605
606 /* Dynamic PLE window. */
607 int ple_window;
608 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800609
610 /* Support for PML */
611#define PML_ENTITY_NUM 512
612 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800613
Yunhong Jiang64672c92016-06-13 14:19:59 -0700614 /* apic deadline value in host tsc */
615 u64 hv_deadline_tsc;
616
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800617 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800618
619 bool guest_pkru_valid;
620 u32 guest_pkru;
621 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800622
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800623 /*
624 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
625 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
626 * in msr_ia32_feature_control_valid_bits.
627 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800628 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800629 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400630};
631
Avi Kivity2fb92db2011-04-27 19:42:18 +0300632enum segment_cache_field {
633 SEG_FIELD_SEL = 0,
634 SEG_FIELD_BASE = 1,
635 SEG_FIELD_LIMIT = 2,
636 SEG_FIELD_AR = 3,
637
638 SEG_FIELD_NR = 4
639};
640
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400641static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
642{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000643 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400644}
645
Feng Wuefc64402015-09-18 22:29:51 +0800646static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
647{
648 return &(to_vmx(vcpu)->pi_desc);
649}
650
Nadav Har'El22bd0352011-05-25 23:05:57 +0300651#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
652#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
653#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
654 [number##_HIGH] = VMCS12_OFFSET(name)+4
655
Abel Gordon4607c2d2013-04-18 14:35:55 +0300656
Bandan Dasfe2b2012014-04-21 15:20:14 -0400657static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300658 /*
659 * We do NOT shadow fields that are modified when L0
660 * traps and emulates any vmx instruction (e.g. VMPTRLD,
661 * VMXON...) executed by L1.
662 * For example, VM_INSTRUCTION_ERROR is read
663 * by L1 if a vmx instruction fails (part of the error path).
664 * Note the code assumes this logic. If for some reason
665 * we start shadowing these fields then we need to
666 * force a shadow sync when L0 emulates vmx instructions
667 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
668 * by nested_vmx_failValid)
669 */
670 VM_EXIT_REASON,
671 VM_EXIT_INTR_INFO,
672 VM_EXIT_INSTRUCTION_LEN,
673 IDT_VECTORING_INFO_FIELD,
674 IDT_VECTORING_ERROR_CODE,
675 VM_EXIT_INTR_ERROR_CODE,
676 EXIT_QUALIFICATION,
677 GUEST_LINEAR_ADDRESS,
678 GUEST_PHYSICAL_ADDRESS
679};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400680static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300681 ARRAY_SIZE(shadow_read_only_fields);
682
Bandan Dasfe2b2012014-04-21 15:20:14 -0400683static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800684 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300685 GUEST_RIP,
686 GUEST_RSP,
687 GUEST_CR0,
688 GUEST_CR3,
689 GUEST_CR4,
690 GUEST_INTERRUPTIBILITY_INFO,
691 GUEST_RFLAGS,
692 GUEST_CS_SELECTOR,
693 GUEST_CS_AR_BYTES,
694 GUEST_CS_LIMIT,
695 GUEST_CS_BASE,
696 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100697 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300698 CR0_GUEST_HOST_MASK,
699 CR0_READ_SHADOW,
700 CR4_READ_SHADOW,
701 TSC_OFFSET,
702 EXCEPTION_BITMAP,
703 CPU_BASED_VM_EXEC_CONTROL,
704 VM_ENTRY_EXCEPTION_ERROR_CODE,
705 VM_ENTRY_INTR_INFO_FIELD,
706 VM_ENTRY_INSTRUCTION_LEN,
707 VM_ENTRY_EXCEPTION_ERROR_CODE,
708 HOST_FS_BASE,
709 HOST_GS_BASE,
710 HOST_FS_SELECTOR,
711 HOST_GS_SELECTOR
712};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400713static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300714 ARRAY_SIZE(shadow_read_write_fields);
715
Mathias Krause772e0312012-08-30 01:30:19 +0200716static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300717 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800718 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300719 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
720 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
721 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
722 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
723 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
724 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
725 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
726 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800727 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300728 FIELD(HOST_ES_SELECTOR, host_es_selector),
729 FIELD(HOST_CS_SELECTOR, host_cs_selector),
730 FIELD(HOST_SS_SELECTOR, host_ss_selector),
731 FIELD(HOST_DS_SELECTOR, host_ds_selector),
732 FIELD(HOST_FS_SELECTOR, host_fs_selector),
733 FIELD(HOST_GS_SELECTOR, host_gs_selector),
734 FIELD(HOST_TR_SELECTOR, host_tr_selector),
735 FIELD64(IO_BITMAP_A, io_bitmap_a),
736 FIELD64(IO_BITMAP_B, io_bitmap_b),
737 FIELD64(MSR_BITMAP, msr_bitmap),
738 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
739 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
740 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
741 FIELD64(TSC_OFFSET, tsc_offset),
742 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
743 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800744 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300745 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800746 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
747 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
748 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
749 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800750 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300751 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
752 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
753 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
754 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
755 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
756 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
757 FIELD64(GUEST_PDPTR0, guest_pdptr0),
758 FIELD64(GUEST_PDPTR1, guest_pdptr1),
759 FIELD64(GUEST_PDPTR2, guest_pdptr2),
760 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100761 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300762 FIELD64(HOST_IA32_PAT, host_ia32_pat),
763 FIELD64(HOST_IA32_EFER, host_ia32_efer),
764 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
765 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
766 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
767 FIELD(EXCEPTION_BITMAP, exception_bitmap),
768 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
769 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
770 FIELD(CR3_TARGET_COUNT, cr3_target_count),
771 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
772 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
773 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
774 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
775 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
776 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
777 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
778 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
779 FIELD(TPR_THRESHOLD, tpr_threshold),
780 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
781 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
782 FIELD(VM_EXIT_REASON, vm_exit_reason),
783 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
784 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
785 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
786 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
787 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
788 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
789 FIELD(GUEST_ES_LIMIT, guest_es_limit),
790 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
791 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
792 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
793 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
794 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
795 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
796 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
797 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
798 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
799 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
800 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
801 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
802 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
803 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
804 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
805 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
806 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
807 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
808 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
809 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
810 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100811 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300812 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
813 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
814 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
815 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
816 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
817 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
818 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
819 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
820 FIELD(EXIT_QUALIFICATION, exit_qualification),
821 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
822 FIELD(GUEST_CR0, guest_cr0),
823 FIELD(GUEST_CR3, guest_cr3),
824 FIELD(GUEST_CR4, guest_cr4),
825 FIELD(GUEST_ES_BASE, guest_es_base),
826 FIELD(GUEST_CS_BASE, guest_cs_base),
827 FIELD(GUEST_SS_BASE, guest_ss_base),
828 FIELD(GUEST_DS_BASE, guest_ds_base),
829 FIELD(GUEST_FS_BASE, guest_fs_base),
830 FIELD(GUEST_GS_BASE, guest_gs_base),
831 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
832 FIELD(GUEST_TR_BASE, guest_tr_base),
833 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
834 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
835 FIELD(GUEST_DR7, guest_dr7),
836 FIELD(GUEST_RSP, guest_rsp),
837 FIELD(GUEST_RIP, guest_rip),
838 FIELD(GUEST_RFLAGS, guest_rflags),
839 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
840 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
841 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
842 FIELD(HOST_CR0, host_cr0),
843 FIELD(HOST_CR3, host_cr3),
844 FIELD(HOST_CR4, host_cr4),
845 FIELD(HOST_FS_BASE, host_fs_base),
846 FIELD(HOST_GS_BASE, host_gs_base),
847 FIELD(HOST_TR_BASE, host_tr_base),
848 FIELD(HOST_GDTR_BASE, host_gdtr_base),
849 FIELD(HOST_IDTR_BASE, host_idtr_base),
850 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
851 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
852 FIELD(HOST_RSP, host_rsp),
853 FIELD(HOST_RIP, host_rip),
854};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300855
856static inline short vmcs_field_to_offset(unsigned long field)
857{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100858 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
859
860 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
861 vmcs_field_to_offset_table[field] == 0)
862 return -ENOENT;
863
Nadav Har'El22bd0352011-05-25 23:05:57 +0300864 return vmcs_field_to_offset_table[field];
865}
866
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300867static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
868{
David Matlack4f2777b2016-07-13 17:16:37 -0700869 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300870}
871
872static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
873{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200874 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800875 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300876 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800877
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300878 return page;
879}
880
881static void nested_release_page(struct page *page)
882{
883 kvm_release_page_dirty(page);
884}
885
886static void nested_release_page_clean(struct page *page)
887{
888 kvm_release_page_clean(page);
889}
890
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300891static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800892static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800893static void kvm_cpu_vmxon(u64 addr);
894static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800895static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200896static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300897static void vmx_set_segment(struct kvm_vcpu *vcpu,
898 struct kvm_segment *var, int seg);
899static void vmx_get_segment(struct kvm_vcpu *vcpu,
900 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200901static bool guest_state_valid(struct kvm_vcpu *vcpu);
902static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300903static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300904static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800905static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300906
Avi Kivity6aa8b732006-12-10 02:21:36 -0800907static DEFINE_PER_CPU(struct vmcs *, vmxarea);
908static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300909/*
910 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
911 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
912 */
913static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300914static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915
Feng Wubf9f6ac2015-09-18 22:29:55 +0800916/*
917 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
918 * can find which vCPU should be waken up.
919 */
920static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
921static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
922
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200923static unsigned long *vmx_io_bitmap_a;
924static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200925static unsigned long *vmx_msr_bitmap_legacy;
926static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800927static unsigned long *vmx_msr_bitmap_legacy_x2apic;
928static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +0800929static unsigned long *vmx_msr_bitmap_legacy_x2apic_apicv_inactive;
930static unsigned long *vmx_msr_bitmap_longmode_x2apic_apicv_inactive;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300931static unsigned long *vmx_vmread_bitmap;
932static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300933
Avi Kivity110312c2010-12-21 12:54:20 +0200934static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200935static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200936
Sheng Yang2384d2b2008-01-17 15:14:33 +0800937static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
938static DEFINE_SPINLOCK(vmx_vpid_lock);
939
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300940static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941 int size;
942 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300943 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300945 u32 pin_based_exec_ctrl;
946 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800947 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300948 u32 vmexit_ctrl;
949 u32 vmentry_ctrl;
950} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951
Hannes Ederefff9e52008-11-28 17:02:06 +0100952static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800953 u32 ept;
954 u32 vpid;
955} vmx_capability;
956
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957#define VMX_SEGMENT_FIELD(seg) \
958 [VCPU_SREG_##seg] = { \
959 .selector = GUEST_##seg##_SELECTOR, \
960 .base = GUEST_##seg##_BASE, \
961 .limit = GUEST_##seg##_LIMIT, \
962 .ar_bytes = GUEST_##seg##_AR_BYTES, \
963 }
964
Mathias Krause772e0312012-08-30 01:30:19 +0200965static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966 unsigned selector;
967 unsigned base;
968 unsigned limit;
969 unsigned ar_bytes;
970} kvm_vmx_segment_fields[] = {
971 VMX_SEGMENT_FIELD(CS),
972 VMX_SEGMENT_FIELD(DS),
973 VMX_SEGMENT_FIELD(ES),
974 VMX_SEGMENT_FIELD(FS),
975 VMX_SEGMENT_FIELD(GS),
976 VMX_SEGMENT_FIELD(SS),
977 VMX_SEGMENT_FIELD(TR),
978 VMX_SEGMENT_FIELD(LDTR),
979};
980
Avi Kivity26bb0982009-09-07 11:14:12 +0300981static u64 host_efer;
982
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300983static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
984
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300985/*
Brian Gerst8c065852010-07-17 09:03:26 -0400986 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300987 * away by decrementing the array size.
988 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800989static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800990#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300991 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400993 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995
Jan Kiszka5bb16012016-02-09 20:14:21 +0100996static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997{
998 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
999 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001000 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1001}
1002
Jan Kiszka6f054852016-02-09 20:15:18 +01001003static inline bool is_debug(u32 intr_info)
1004{
1005 return is_exception_n(intr_info, DB_VECTOR);
1006}
1007
1008static inline bool is_breakpoint(u32 intr_info)
1009{
1010 return is_exception_n(intr_info, BP_VECTOR);
1011}
1012
Jan Kiszka5bb16012016-02-09 20:14:21 +01001013static inline bool is_page_fault(u32 intr_info)
1014{
1015 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016}
1017
Gui Jianfeng31299942010-03-15 17:29:09 +08001018static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001019{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001020 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001021}
1022
Gui Jianfeng31299942010-03-15 17:29:09 +08001023static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001024{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001025 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001026}
1027
Gui Jianfeng31299942010-03-15 17:29:09 +08001028static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029{
1030 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1031 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1032}
1033
Gui Jianfeng31299942010-03-15 17:29:09 +08001034static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001035{
1036 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1037 INTR_INFO_VALID_MASK)) ==
1038 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1039}
1040
Gui Jianfeng31299942010-03-15 17:29:09 +08001041static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001042{
Sheng Yang04547152009-04-01 15:52:31 +08001043 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001044}
1045
Gui Jianfeng31299942010-03-15 17:29:09 +08001046static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001047{
Sheng Yang04547152009-04-01 15:52:31 +08001048 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001049}
1050
Paolo Bonzini35754c92015-07-29 12:05:37 +02001051static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001052{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001053 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001054}
1055
Gui Jianfeng31299942010-03-15 17:29:09 +08001056static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001057{
Sheng Yang04547152009-04-01 15:52:31 +08001058 return vmcs_config.cpu_based_exec_ctrl &
1059 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001060}
1061
Avi Kivity774ead32007-12-26 13:57:04 +02001062static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001063{
Sheng Yang04547152009-04-01 15:52:31 +08001064 return vmcs_config.cpu_based_2nd_exec_ctrl &
1065 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1066}
1067
Yang Zhang8d146952013-01-25 10:18:50 +08001068static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1069{
1070 return vmcs_config.cpu_based_2nd_exec_ctrl &
1071 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1072}
1073
Yang Zhang83d4c282013-01-25 10:18:49 +08001074static inline bool cpu_has_vmx_apic_register_virt(void)
1075{
1076 return vmcs_config.cpu_based_2nd_exec_ctrl &
1077 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1078}
1079
Yang Zhangc7c9c562013-01-25 10:18:51 +08001080static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1081{
1082 return vmcs_config.cpu_based_2nd_exec_ctrl &
1083 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1084}
1085
Yunhong Jiang64672c92016-06-13 14:19:59 -07001086/*
1087 * Comment's format: document - errata name - stepping - processor name.
1088 * Refer from
1089 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1090 */
1091static u32 vmx_preemption_cpu_tfms[] = {
1092/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
10930x000206E6,
1094/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1095/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1096/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
10970x00020652,
1098/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
10990x00020655,
1100/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1101/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1102/*
1103 * 320767.pdf - AAP86 - B1 -
1104 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1105 */
11060x000106E5,
1107/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11080x000106A0,
1109/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11100x000106A1,
1111/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11120x000106A4,
1113 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1114 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1115 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11160x000106A5,
1117};
1118
1119static inline bool cpu_has_broken_vmx_preemption_timer(void)
1120{
1121 u32 eax = cpuid_eax(0x00000001), i;
1122
1123 /* Clear the reserved bits */
1124 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001125 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001126 if (eax == vmx_preemption_cpu_tfms[i])
1127 return true;
1128
1129 return false;
1130}
1131
1132static inline bool cpu_has_vmx_preemption_timer(void)
1133{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001134 return vmcs_config.pin_based_exec_ctrl &
1135 PIN_BASED_VMX_PREEMPTION_TIMER;
1136}
1137
Yang Zhang01e439b2013-04-11 19:25:12 +08001138static inline bool cpu_has_vmx_posted_intr(void)
1139{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001140 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1141 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001142}
1143
1144static inline bool cpu_has_vmx_apicv(void)
1145{
1146 return cpu_has_vmx_apic_register_virt() &&
1147 cpu_has_vmx_virtual_intr_delivery() &&
1148 cpu_has_vmx_posted_intr();
1149}
1150
Sheng Yang04547152009-04-01 15:52:31 +08001151static inline bool cpu_has_vmx_flexpriority(void)
1152{
1153 return cpu_has_vmx_tpr_shadow() &&
1154 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001155}
1156
Marcelo Tosattie7997942009-06-11 12:07:40 -03001157static inline bool cpu_has_vmx_ept_execute_only(void)
1158{
Gui Jianfeng31299942010-03-15 17:29:09 +08001159 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001160}
1161
Marcelo Tosattie7997942009-06-11 12:07:40 -03001162static inline bool cpu_has_vmx_ept_2m_page(void)
1163{
Gui Jianfeng31299942010-03-15 17:29:09 +08001164 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001165}
1166
Sheng Yang878403b2010-01-05 19:02:29 +08001167static inline bool cpu_has_vmx_ept_1g_page(void)
1168{
Gui Jianfeng31299942010-03-15 17:29:09 +08001169 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001170}
1171
Sheng Yang4bc9b982010-06-02 14:05:24 +08001172static inline bool cpu_has_vmx_ept_4levels(void)
1173{
1174 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1175}
1176
Xudong Hao83c3a332012-05-28 19:33:35 +08001177static inline bool cpu_has_vmx_ept_ad_bits(void)
1178{
1179 return vmx_capability.ept & VMX_EPT_AD_BIT;
1180}
1181
Gui Jianfeng31299942010-03-15 17:29:09 +08001182static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001183{
Gui Jianfeng31299942010-03-15 17:29:09 +08001184 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001185}
1186
Gui Jianfeng31299942010-03-15 17:29:09 +08001187static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001188{
Gui Jianfeng31299942010-03-15 17:29:09 +08001189 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001190}
1191
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001192static inline bool cpu_has_vmx_invvpid_single(void)
1193{
1194 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1195}
1196
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001197static inline bool cpu_has_vmx_invvpid_global(void)
1198{
1199 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1200}
1201
Gui Jianfeng31299942010-03-15 17:29:09 +08001202static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001203{
Sheng Yang04547152009-04-01 15:52:31 +08001204 return vmcs_config.cpu_based_2nd_exec_ctrl &
1205 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001206}
1207
Gui Jianfeng31299942010-03-15 17:29:09 +08001208static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001209{
1210 return vmcs_config.cpu_based_2nd_exec_ctrl &
1211 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1212}
1213
Gui Jianfeng31299942010-03-15 17:29:09 +08001214static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001215{
1216 return vmcs_config.cpu_based_2nd_exec_ctrl &
1217 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1218}
1219
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001220static inline bool cpu_has_vmx_basic_inout(void)
1221{
1222 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1223}
1224
Paolo Bonzini35754c92015-07-29 12:05:37 +02001225static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001226{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001227 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001228}
1229
Gui Jianfeng31299942010-03-15 17:29:09 +08001230static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001231{
Sheng Yang04547152009-04-01 15:52:31 +08001232 return vmcs_config.cpu_based_2nd_exec_ctrl &
1233 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001234}
1235
Gui Jianfeng31299942010-03-15 17:29:09 +08001236static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001237{
1238 return vmcs_config.cpu_based_2nd_exec_ctrl &
1239 SECONDARY_EXEC_RDTSCP;
1240}
1241
Mao, Junjiead756a12012-07-02 01:18:48 +00001242static inline bool cpu_has_vmx_invpcid(void)
1243{
1244 return vmcs_config.cpu_based_2nd_exec_ctrl &
1245 SECONDARY_EXEC_ENABLE_INVPCID;
1246}
1247
Gui Jianfeng31299942010-03-15 17:29:09 +08001248static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001249{
1250 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1251}
1252
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001253static inline bool cpu_has_vmx_wbinvd_exit(void)
1254{
1255 return vmcs_config.cpu_based_2nd_exec_ctrl &
1256 SECONDARY_EXEC_WBINVD_EXITING;
1257}
1258
Abel Gordonabc4fc52013-04-18 14:35:25 +03001259static inline bool cpu_has_vmx_shadow_vmcs(void)
1260{
1261 u64 vmx_msr;
1262 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1263 /* check if the cpu supports writing r/o exit information fields */
1264 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1265 return false;
1266
1267 return vmcs_config.cpu_based_2nd_exec_ctrl &
1268 SECONDARY_EXEC_SHADOW_VMCS;
1269}
1270
Kai Huang843e4332015-01-28 10:54:28 +08001271static inline bool cpu_has_vmx_pml(void)
1272{
1273 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1274}
1275
Haozhong Zhang64903d62015-10-20 15:39:09 +08001276static inline bool cpu_has_vmx_tsc_scaling(void)
1277{
1278 return vmcs_config.cpu_based_2nd_exec_ctrl &
1279 SECONDARY_EXEC_TSC_SCALING;
1280}
1281
Sheng Yang04547152009-04-01 15:52:31 +08001282static inline bool report_flexpriority(void)
1283{
1284 return flexpriority_enabled;
1285}
1286
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001287static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1288{
1289 return vmcs12->cpu_based_vm_exec_control & bit;
1290}
1291
1292static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1293{
1294 return (vmcs12->cpu_based_vm_exec_control &
1295 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1296 (vmcs12->secondary_vm_exec_control & bit);
1297}
1298
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001299static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001300{
1301 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1302}
1303
Jan Kiszkaf41245002014-03-07 20:03:13 +01001304static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1305{
1306 return vmcs12->pin_based_vm_exec_control &
1307 PIN_BASED_VMX_PREEMPTION_TIMER;
1308}
1309
Nadav Har'El155a97a2013-08-05 11:07:16 +03001310static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1311{
1312 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1313}
1314
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001315static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1316{
1317 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1318 vmx_xsaves_supported();
1319}
1320
Wincy Vanf2b93282015-02-03 23:56:03 +08001321static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1322{
1323 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1324}
1325
Wanpeng Li5c614b32015-10-13 09:18:36 -07001326static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1327{
1328 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1329}
1330
Wincy Van82f0dd42015-02-03 23:57:18 +08001331static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1332{
1333 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1334}
1335
Wincy Van608406e2015-02-03 23:57:51 +08001336static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1337{
1338 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1339}
1340
Wincy Van705699a2015-02-03 23:58:17 +08001341static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1342{
1343 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1344}
1345
Nadav Har'El644d7112011-05-25 23:12:35 +03001346static inline bool is_exception(u32 intr_info)
1347{
1348 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1349 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1350}
1351
Jan Kiszka533558b2014-01-04 18:47:20 +01001352static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1353 u32 exit_intr_info,
1354 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001355static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1356 struct vmcs12 *vmcs12,
1357 u32 reason, unsigned long qualification);
1358
Rusty Russell8b9cf982007-07-30 16:31:43 +10001359static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001360{
1361 int i;
1362
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001363 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001364 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001365 return i;
1366 return -1;
1367}
1368
Sheng Yang2384d2b2008-01-17 15:14:33 +08001369static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1370{
1371 struct {
1372 u64 vpid : 16;
1373 u64 rsvd : 48;
1374 u64 gva;
1375 } operand = { vpid, 0, gva };
1376
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001377 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001378 /* CF==1 or ZF==1 --> rc = -1 */
1379 "; ja 1f ; ud2 ; 1:"
1380 : : "a"(&operand), "c"(ext) : "cc", "memory");
1381}
1382
Sheng Yang14394422008-04-28 12:24:45 +08001383static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1384{
1385 struct {
1386 u64 eptp, gpa;
1387 } operand = {eptp, gpa};
1388
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001389 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001390 /* CF==1 or ZF==1 --> rc = -1 */
1391 "; ja 1f ; ud2 ; 1:\n"
1392 : : "a" (&operand), "c" (ext) : "cc", "memory");
1393}
1394
Avi Kivity26bb0982009-09-07 11:14:12 +03001395static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001396{
1397 int i;
1398
Rusty Russell8b9cf982007-07-30 16:31:43 +10001399 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001400 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001401 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001402 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001403}
1404
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405static void vmcs_clear(struct vmcs *vmcs)
1406{
1407 u64 phys_addr = __pa(vmcs);
1408 u8 error;
1409
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001410 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001411 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412 : "cc", "memory");
1413 if (error)
1414 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1415 vmcs, phys_addr);
1416}
1417
Nadav Har'Eld462b812011-05-24 15:26:10 +03001418static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1419{
1420 vmcs_clear(loaded_vmcs->vmcs);
1421 loaded_vmcs->cpu = -1;
1422 loaded_vmcs->launched = 0;
1423}
1424
Dongxiao Xu7725b892010-05-11 18:29:38 +08001425static void vmcs_load(struct vmcs *vmcs)
1426{
1427 u64 phys_addr = __pa(vmcs);
1428 u8 error;
1429
1430 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001431 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001432 : "cc", "memory");
1433 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001434 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001435 vmcs, phys_addr);
1436}
1437
Dave Young2965faa2015-09-09 15:38:55 -07001438#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001439/*
1440 * This bitmap is used to indicate whether the vmclear
1441 * operation is enabled on all cpus. All disabled by
1442 * default.
1443 */
1444static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1445
1446static inline void crash_enable_local_vmclear(int cpu)
1447{
1448 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1449}
1450
1451static inline void crash_disable_local_vmclear(int cpu)
1452{
1453 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1454}
1455
1456static inline int crash_local_vmclear_enabled(int cpu)
1457{
1458 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1459}
1460
1461static void crash_vmclear_local_loaded_vmcss(void)
1462{
1463 int cpu = raw_smp_processor_id();
1464 struct loaded_vmcs *v;
1465
1466 if (!crash_local_vmclear_enabled(cpu))
1467 return;
1468
1469 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1470 loaded_vmcss_on_cpu_link)
1471 vmcs_clear(v->vmcs);
1472}
1473#else
1474static inline void crash_enable_local_vmclear(int cpu) { }
1475static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001476#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001477
Nadav Har'Eld462b812011-05-24 15:26:10 +03001478static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001480 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001481 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001482
Nadav Har'Eld462b812011-05-24 15:26:10 +03001483 if (loaded_vmcs->cpu != cpu)
1484 return; /* vcpu migration can race with cpu offline */
1485 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001487 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001488 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001489
1490 /*
1491 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1492 * is before setting loaded_vmcs->vcpu to -1 which is done in
1493 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1494 * then adds the vmcs into percpu list before it is deleted.
1495 */
1496 smp_wmb();
1497
Nadav Har'Eld462b812011-05-24 15:26:10 +03001498 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001499 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500}
1501
Nadav Har'Eld462b812011-05-24 15:26:10 +03001502static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001503{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001504 int cpu = loaded_vmcs->cpu;
1505
1506 if (cpu != -1)
1507 smp_call_function_single(cpu,
1508 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001509}
1510
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001511static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001512{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001513 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001514 return;
1515
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001516 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001517 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001518}
1519
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001520static inline void vpid_sync_vcpu_global(void)
1521{
1522 if (cpu_has_vmx_invvpid_global())
1523 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1524}
1525
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001526static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001527{
1528 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001529 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001530 else
1531 vpid_sync_vcpu_global();
1532}
1533
Sheng Yang14394422008-04-28 12:24:45 +08001534static inline void ept_sync_global(void)
1535{
1536 if (cpu_has_vmx_invept_global())
1537 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1538}
1539
1540static inline void ept_sync_context(u64 eptp)
1541{
Avi Kivity089d0342009-03-23 18:26:32 +02001542 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001543 if (cpu_has_vmx_invept_context())
1544 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1545 else
1546 ept_sync_global();
1547 }
1548}
1549
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001550static __always_inline void vmcs_check16(unsigned long field)
1551{
1552 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1553 "16-bit accessor invalid for 64-bit field");
1554 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1555 "16-bit accessor invalid for 64-bit high field");
1556 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1557 "16-bit accessor invalid for 32-bit high field");
1558 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1559 "16-bit accessor invalid for natural width field");
1560}
1561
1562static __always_inline void vmcs_check32(unsigned long field)
1563{
1564 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1565 "32-bit accessor invalid for 16-bit field");
1566 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1567 "32-bit accessor invalid for natural width field");
1568}
1569
1570static __always_inline void vmcs_check64(unsigned long field)
1571{
1572 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1573 "64-bit accessor invalid for 16-bit field");
1574 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1575 "64-bit accessor invalid for 64-bit high field");
1576 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1577 "64-bit accessor invalid for 32-bit field");
1578 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1579 "64-bit accessor invalid for natural width field");
1580}
1581
1582static __always_inline void vmcs_checkl(unsigned long field)
1583{
1584 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1585 "Natural width accessor invalid for 16-bit field");
1586 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1587 "Natural width accessor invalid for 64-bit field");
1588 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1589 "Natural width accessor invalid for 64-bit high field");
1590 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1591 "Natural width accessor invalid for 32-bit field");
1592}
1593
1594static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
Avi Kivity5e520e62011-05-15 10:13:12 -04001596 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597
Avi Kivity5e520e62011-05-15 10:13:12 -04001598 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1599 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600 return value;
1601}
1602
Avi Kivity96304212011-05-15 10:13:13 -04001603static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001605 vmcs_check16(field);
1606 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607}
1608
Avi Kivity96304212011-05-15 10:13:13 -04001609static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001611 vmcs_check32(field);
1612 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613}
1614
Avi Kivity96304212011-05-15 10:13:13 -04001615static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001617 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001618#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001619 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001621 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622#endif
1623}
1624
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001625static __always_inline unsigned long vmcs_readl(unsigned long field)
1626{
1627 vmcs_checkl(field);
1628 return __vmcs_readl(field);
1629}
1630
Avi Kivitye52de1b2007-01-05 16:36:56 -08001631static noinline void vmwrite_error(unsigned long field, unsigned long value)
1632{
1633 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1634 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1635 dump_stack();
1636}
1637
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001638static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639{
1640 u8 error;
1641
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001642 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001643 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001644 if (unlikely(error))
1645 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646}
1647
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001648static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001650 vmcs_check16(field);
1651 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652}
1653
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001654static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001656 vmcs_check32(field);
1657 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658}
1659
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001660static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001662 vmcs_check64(field);
1663 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001664#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001666 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667#endif
1668}
1669
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001670static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001671{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001672 vmcs_checkl(field);
1673 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001674}
1675
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001676static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001677{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001678 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1679 "vmcs_clear_bits does not support 64-bit fields");
1680 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1681}
1682
1683static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1684{
1685 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1686 "vmcs_set_bits does not support 64-bit fields");
1687 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001688}
1689
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001690static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1691{
1692 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1693}
1694
Gleb Natapov2961e8762013-11-25 15:37:13 +02001695static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1696{
1697 vmcs_write32(VM_ENTRY_CONTROLS, val);
1698 vmx->vm_entry_controls_shadow = val;
1699}
1700
1701static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1702{
1703 if (vmx->vm_entry_controls_shadow != val)
1704 vm_entry_controls_init(vmx, val);
1705}
1706
1707static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1708{
1709 return vmx->vm_entry_controls_shadow;
1710}
1711
1712
1713static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1714{
1715 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1716}
1717
1718static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1719{
1720 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1721}
1722
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001723static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1724{
1725 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1726}
1727
Gleb Natapov2961e8762013-11-25 15:37:13 +02001728static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1729{
1730 vmcs_write32(VM_EXIT_CONTROLS, val);
1731 vmx->vm_exit_controls_shadow = val;
1732}
1733
1734static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1735{
1736 if (vmx->vm_exit_controls_shadow != val)
1737 vm_exit_controls_init(vmx, val);
1738}
1739
1740static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1741{
1742 return vmx->vm_exit_controls_shadow;
1743}
1744
1745
1746static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1747{
1748 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1749}
1750
1751static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1752{
1753 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1754}
1755
Avi Kivity2fb92db2011-04-27 19:42:18 +03001756static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1757{
1758 vmx->segment_cache.bitmask = 0;
1759}
1760
1761static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1762 unsigned field)
1763{
1764 bool ret;
1765 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1766
1767 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1768 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1769 vmx->segment_cache.bitmask = 0;
1770 }
1771 ret = vmx->segment_cache.bitmask & mask;
1772 vmx->segment_cache.bitmask |= mask;
1773 return ret;
1774}
1775
1776static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1777{
1778 u16 *p = &vmx->segment_cache.seg[seg].selector;
1779
1780 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1781 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1782 return *p;
1783}
1784
1785static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1786{
1787 ulong *p = &vmx->segment_cache.seg[seg].base;
1788
1789 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1790 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1791 return *p;
1792}
1793
1794static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1795{
1796 u32 *p = &vmx->segment_cache.seg[seg].limit;
1797
1798 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1799 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1800 return *p;
1801}
1802
1803static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1804{
1805 u32 *p = &vmx->segment_cache.seg[seg].ar;
1806
1807 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1808 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1809 return *p;
1810}
1811
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001812static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1813{
1814 u32 eb;
1815
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001816 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01001817 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001818 if ((vcpu->guest_debug &
1819 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1820 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1821 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001822 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001823 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001824 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001825 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001826 if (vcpu->fpu_active)
1827 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001828
1829 /* When we are running a nested L2 guest and L1 specified for it a
1830 * certain exception bitmap, we must trap the same exceptions and pass
1831 * them to L1. When running L2, we will only handle the exceptions
1832 * specified above if L1 did not want them.
1833 */
1834 if (is_guest_mode(vcpu))
1835 eb |= get_vmcs12(vcpu)->exception_bitmap;
1836
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001837 vmcs_write32(EXCEPTION_BITMAP, eb);
1838}
1839
Gleb Natapov2961e8762013-11-25 15:37:13 +02001840static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1841 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001842{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001843 vm_entry_controls_clearbit(vmx, entry);
1844 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001845}
1846
Avi Kivity61d2ef22010-04-28 16:40:38 +03001847static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1848{
1849 unsigned i;
1850 struct msr_autoload *m = &vmx->msr_autoload;
1851
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001852 switch (msr) {
1853 case MSR_EFER:
1854 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001855 clear_atomic_switch_msr_special(vmx,
1856 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001857 VM_EXIT_LOAD_IA32_EFER);
1858 return;
1859 }
1860 break;
1861 case MSR_CORE_PERF_GLOBAL_CTRL:
1862 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001863 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001864 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1865 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1866 return;
1867 }
1868 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001869 }
1870
Avi Kivity61d2ef22010-04-28 16:40:38 +03001871 for (i = 0; i < m->nr; ++i)
1872 if (m->guest[i].index == msr)
1873 break;
1874
1875 if (i == m->nr)
1876 return;
1877 --m->nr;
1878 m->guest[i] = m->guest[m->nr];
1879 m->host[i] = m->host[m->nr];
1880 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1881 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1882}
1883
Gleb Natapov2961e8762013-11-25 15:37:13 +02001884static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1885 unsigned long entry, unsigned long exit,
1886 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1887 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001888{
1889 vmcs_write64(guest_val_vmcs, guest_val);
1890 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001891 vm_entry_controls_setbit(vmx, entry);
1892 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001893}
1894
Avi Kivity61d2ef22010-04-28 16:40:38 +03001895static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1896 u64 guest_val, u64 host_val)
1897{
1898 unsigned i;
1899 struct msr_autoload *m = &vmx->msr_autoload;
1900
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001901 switch (msr) {
1902 case MSR_EFER:
1903 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001904 add_atomic_switch_msr_special(vmx,
1905 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001906 VM_EXIT_LOAD_IA32_EFER,
1907 GUEST_IA32_EFER,
1908 HOST_IA32_EFER,
1909 guest_val, host_val);
1910 return;
1911 }
1912 break;
1913 case MSR_CORE_PERF_GLOBAL_CTRL:
1914 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001915 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001916 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1917 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1918 GUEST_IA32_PERF_GLOBAL_CTRL,
1919 HOST_IA32_PERF_GLOBAL_CTRL,
1920 guest_val, host_val);
1921 return;
1922 }
1923 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001924 case MSR_IA32_PEBS_ENABLE:
1925 /* PEBS needs a quiescent period after being disabled (to write
1926 * a record). Disabling PEBS through VMX MSR swapping doesn't
1927 * provide that period, so a CPU could write host's record into
1928 * guest's memory.
1929 */
1930 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001931 }
1932
Avi Kivity61d2ef22010-04-28 16:40:38 +03001933 for (i = 0; i < m->nr; ++i)
1934 if (m->guest[i].index == msr)
1935 break;
1936
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001937 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001938 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001939 "Can't add msr %x\n", msr);
1940 return;
1941 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001942 ++m->nr;
1943 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1944 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1945 }
1946
1947 m->guest[i].index = msr;
1948 m->guest[i].value = guest_val;
1949 m->host[i].index = msr;
1950 m->host[i].value = host_val;
1951}
1952
Avi Kivity33ed6322007-05-02 16:54:03 +03001953static void reload_tss(void)
1954{
Avi Kivity33ed6322007-05-02 16:54:03 +03001955 /*
1956 * VT restores TR but not its size. Useless.
1957 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001958 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001959 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001960
Avi Kivityd3591922010-07-26 18:32:39 +03001961 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001962 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1963 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001964}
1965
Avi Kivity92c0d902009-10-29 11:00:16 +02001966static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001967{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001968 u64 guest_efer = vmx->vcpu.arch.efer;
1969 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03001970
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001971 if (!enable_ept) {
1972 /*
1973 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1974 * host CPUID is more efficient than testing guest CPUID
1975 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
1976 */
1977 if (boot_cpu_has(X86_FEATURE_SMEP))
1978 guest_efer |= EFER_NX;
1979 else if (!(guest_efer & EFER_NX))
1980 ignore_bits |= EFER_NX;
1981 }
Roel Kluin3a34a882009-08-04 02:08:45 -07001982
Avi Kivity51c6cf62007-08-29 03:48:05 +03001983 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001984 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03001985 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001986 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001987#ifdef CONFIG_X86_64
1988 ignore_bits |= EFER_LMA | EFER_LME;
1989 /* SCE is meaningful only in long mode on Intel */
1990 if (guest_efer & EFER_LMA)
1991 ignore_bits &= ~(u64)EFER_SCE;
1992#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03001993
1994 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001995
1996 /*
1997 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1998 * On CPUs that support "load IA32_EFER", always switch EFER
1999 * atomically, since it's faster than switching it manually.
2000 */
2001 if (cpu_has_load_ia32_efer ||
2002 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002003 if (!(guest_efer & EFER_LMA))
2004 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002005 if (guest_efer != host_efer)
2006 add_atomic_switch_msr(vmx, MSR_EFER,
2007 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002008 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002009 } else {
2010 guest_efer &= ~ignore_bits;
2011 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002012
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002013 vmx->guest_msrs[efer_offset].data = guest_efer;
2014 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2015
2016 return true;
2017 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002018}
2019
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002020static unsigned long segment_base(u16 selector)
2021{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002022 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002023 struct desc_struct *d;
2024 unsigned long table_base;
2025 unsigned long v;
2026
2027 if (!(selector & ~3))
2028 return 0;
2029
Avi Kivityd3591922010-07-26 18:32:39 +03002030 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002031
2032 if (selector & 4) { /* from ldt */
2033 u16 ldt_selector = kvm_read_ldt();
2034
2035 if (!(ldt_selector & ~3))
2036 return 0;
2037
2038 table_base = segment_base(ldt_selector);
2039 }
2040 d = (struct desc_struct *)(table_base + (selector & ~7));
2041 v = get_desc_base(d);
2042#ifdef CONFIG_X86_64
2043 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2044 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2045#endif
2046 return v;
2047}
2048
2049static inline unsigned long kvm_read_tr_base(void)
2050{
2051 u16 tr;
2052 asm("str %0" : "=g"(tr));
2053 return segment_base(tr);
2054}
2055
Avi Kivity04d2cc72007-09-10 18:10:54 +03002056static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002057{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002058 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002059 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002060
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002061 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002062 return;
2063
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002064 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002065 /*
2066 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2067 * allow segment selectors with cpl > 0 or ti == 1.
2068 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002069 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002070 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002071 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002072 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002073 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002074 vmx->host_state.fs_reload_needed = 0;
2075 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002076 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002077 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002078 }
Avi Kivity9581d442010-10-19 16:46:55 +02002079 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002080 if (!(vmx->host_state.gs_sel & 7))
2081 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002082 else {
2083 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002084 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002085 }
2086
2087#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002088 savesegment(ds, vmx->host_state.ds_sel);
2089 savesegment(es, vmx->host_state.es_sel);
2090#endif
2091
2092#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002093 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2094 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2095#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002096 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2097 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002098#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002099
2100#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002101 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2102 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002103 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002104#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002105 if (boot_cpu_has(X86_FEATURE_MPX))
2106 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002107 for (i = 0; i < vmx->save_nmsrs; ++i)
2108 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002109 vmx->guest_msrs[i].data,
2110 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002111}
2112
Avi Kivitya9b21b62008-06-24 11:48:49 +03002113static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002114{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002115 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002116 return;
2117
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002118 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002119 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002120#ifdef CONFIG_X86_64
2121 if (is_long_mode(&vmx->vcpu))
2122 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2123#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002124 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002125 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002126#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002127 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002128#else
2129 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002130#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002131 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002132 if (vmx->host_state.fs_reload_needed)
2133 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002134#ifdef CONFIG_X86_64
2135 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2136 loadsegment(ds, vmx->host_state.ds_sel);
2137 loadsegment(es, vmx->host_state.es_sel);
2138 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002139#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002140 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002141#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002142 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002143#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002144 if (vmx->host_state.msr_host_bndcfgs)
2145 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002146 /*
2147 * If the FPU is not active (through the host task or
2148 * the guest vcpu), then restore the cr0.TS bit.
2149 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002150 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002151 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002152 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002153}
2154
Avi Kivitya9b21b62008-06-24 11:48:49 +03002155static void vmx_load_host_state(struct vcpu_vmx *vmx)
2156{
2157 preempt_disable();
2158 __vmx_load_host_state(vmx);
2159 preempt_enable();
2160}
2161
Feng Wu28b835d2015-09-18 22:29:54 +08002162static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2163{
2164 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2165 struct pi_desc old, new;
2166 unsigned int dest;
2167
2168 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002169 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2170 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002171 return;
2172
2173 do {
2174 old.control = new.control = pi_desc->control;
2175
2176 /*
2177 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
2178 * are two possible cases:
2179 * 1. After running 'pre_block', context switch
2180 * happened. For this case, 'sn' was set in
2181 * vmx_vcpu_put(), so we need to clear it here.
2182 * 2. After running 'pre_block', we were blocked,
2183 * and woken up by some other guy. For this case,
2184 * we don't need to do anything, 'pi_post_block'
2185 * will do everything for us. However, we cannot
2186 * check whether it is case #1 or case #2 here
2187 * (maybe, not needed), so we also clear sn here,
2188 * I think it is not a big deal.
2189 */
2190 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
2191 if (vcpu->cpu != cpu) {
2192 dest = cpu_physical_id(cpu);
2193
2194 if (x2apic_enabled())
2195 new.ndst = dest;
2196 else
2197 new.ndst = (dest << 8) & 0xFF00;
2198 }
2199
2200 /* set 'NV' to 'notification vector' */
2201 new.nv = POSTED_INTR_VECTOR;
2202 }
2203
2204 /* Allow posting non-urgent interrupts */
2205 new.sn = 0;
2206 } while (cmpxchg(&pi_desc->control, old.control,
2207 new.control) != old.control);
2208}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002209
Peter Feinerc95ba922016-08-17 09:36:47 -07002210static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2211{
2212 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2213 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2214}
2215
Avi Kivity6aa8b732006-12-10 02:21:36 -08002216/*
2217 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2218 * vcpu mutex is already taken.
2219 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002220static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002222 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002223 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002224 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002226 if (!vmm_exclusive)
2227 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002228 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002229 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002231 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002232 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002233 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002234
2235 /*
2236 * Read loaded_vmcs->cpu should be before fetching
2237 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2238 * See the comments in __loaded_vmcs_clear().
2239 */
2240 smp_rmb();
2241
Nadav Har'Eld462b812011-05-24 15:26:10 +03002242 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2243 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002244 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002245 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002246 }
2247
2248 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2249 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2250 vmcs_load(vmx->loaded_vmcs->vmcs);
2251 }
2252
2253 if (!already_loaded) {
2254 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2255 unsigned long sysenter_esp;
2256
2257 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002258
Avi Kivity6aa8b732006-12-10 02:21:36 -08002259 /*
2260 * Linux uses per-cpu TSS and GDT, so set these when switching
2261 * processors.
2262 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002263 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002264 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265
2266 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2267 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002268
Nadav Har'Eld462b812011-05-24 15:26:10 +03002269 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 }
Feng Wu28b835d2015-09-18 22:29:54 +08002271
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002272 /* Setup TSC multiplier */
2273 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002274 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2275 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002276
Feng Wu28b835d2015-09-18 22:29:54 +08002277 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002278 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002279}
2280
2281static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2282{
2283 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2284
2285 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002286 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2287 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002288 return;
2289
2290 /* Set SN when the vCPU is preempted */
2291 if (vcpu->preempted)
2292 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293}
2294
2295static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2296{
Feng Wu28b835d2015-09-18 22:29:54 +08002297 vmx_vcpu_pi_put(vcpu);
2298
Avi Kivitya9b21b62008-06-24 11:48:49 +03002299 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002300 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002301 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2302 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002303 kvm_cpu_vmxoff();
2304 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305}
2306
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002307static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2308{
Avi Kivity81231c62010-01-24 16:26:40 +02002309 ulong cr0;
2310
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002311 if (vcpu->fpu_active)
2312 return;
2313 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002314 cr0 = vmcs_readl(GUEST_CR0);
2315 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2316 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2317 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002318 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002319 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002320 if (is_guest_mode(vcpu))
2321 vcpu->arch.cr0_guest_owned_bits &=
2322 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002323 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002324}
2325
Avi Kivityedcafe32009-12-30 18:07:40 +02002326static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2327
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002328/*
2329 * Return the cr0 value that a nested guest would read. This is a combination
2330 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2331 * its hypervisor (cr0_read_shadow).
2332 */
2333static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2334{
2335 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2336 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2337}
2338static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2339{
2340 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2341 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2342}
2343
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002344static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2345{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002346 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2347 * set this *before* calling this function.
2348 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002349 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002350 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002351 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002352 vcpu->arch.cr0_guest_owned_bits = 0;
2353 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002354 if (is_guest_mode(vcpu)) {
2355 /*
2356 * L1's specified read shadow might not contain the TS bit,
2357 * so now that we turned on shadowing of this bit, we need to
2358 * set this bit of the shadow. Like in nested_vmx_run we need
2359 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2360 * up-to-date here because we just decached cr0.TS (and we'll
2361 * only update vmcs12->guest_cr0 on nested exit).
2362 */
2363 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2364 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2365 (vcpu->arch.cr0 & X86_CR0_TS);
2366 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2367 } else
2368 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002369}
2370
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2372{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002373 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002374
Avi Kivity6de12732011-03-07 12:51:22 +02002375 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2376 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2377 rflags = vmcs_readl(GUEST_RFLAGS);
2378 if (to_vmx(vcpu)->rmode.vm86_active) {
2379 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2380 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2381 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2382 }
2383 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002384 }
Avi Kivity6de12732011-03-07 12:51:22 +02002385 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
2388static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2389{
Avi Kivity6de12732011-03-07 12:51:22 +02002390 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2391 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002392 if (to_vmx(vcpu)->rmode.vm86_active) {
2393 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002394 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002395 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 vmcs_writel(GUEST_RFLAGS, rflags);
2397}
2398
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002399static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2400{
2401 return to_vmx(vcpu)->guest_pkru;
2402}
2403
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002404static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002405{
2406 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2407 int ret = 0;
2408
2409 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002410 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002411 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002412 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002413
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002414 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002415}
2416
2417static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2418{
2419 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2420 u32 interruptibility = interruptibility_old;
2421
2422 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2423
Jan Kiszka48005f62010-02-19 19:38:07 +01002424 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002425 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002426 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002427 interruptibility |= GUEST_INTR_STATE_STI;
2428
2429 if ((interruptibility != interruptibility_old))
2430 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2431}
2432
Avi Kivity6aa8b732006-12-10 02:21:36 -08002433static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2434{
2435 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002437 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002439 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440
Glauber Costa2809f5d2009-05-12 16:21:05 -04002441 /* skipping an emulated instruction also counts */
2442 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443}
2444
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002445/*
2446 * KVM wants to inject page-faults which it got to the guest. This function
2447 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002448 */
Gleb Natapove011c662013-09-25 12:51:35 +03002449static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002450{
2451 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2452
Gleb Natapove011c662013-09-25 12:51:35 +03002453 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002454 return 0;
2455
Jan Kiszka533558b2014-01-04 18:47:20 +01002456 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2457 vmcs_read32(VM_EXIT_INTR_INFO),
2458 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002459 return 1;
2460}
2461
Avi Kivity298101d2007-11-25 13:41:11 +02002462static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002463 bool has_error_code, u32 error_code,
2464 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002465{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002466 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002467 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002468
Gleb Natapove011c662013-09-25 12:51:35 +03002469 if (!reinject && is_guest_mode(vcpu) &&
2470 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002471 return;
2472
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002473 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002474 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002475 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2476 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002477
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002478 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002479 int inc_eip = 0;
2480 if (kvm_exception_is_soft(nr))
2481 inc_eip = vcpu->arch.event_exit_inst_len;
2482 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002483 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002484 return;
2485 }
2486
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002487 if (kvm_exception_is_soft(nr)) {
2488 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2489 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002490 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2491 } else
2492 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2493
2494 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002495}
2496
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002497static bool vmx_rdtscp_supported(void)
2498{
2499 return cpu_has_vmx_rdtscp();
2500}
2501
Mao, Junjiead756a12012-07-02 01:18:48 +00002502static bool vmx_invpcid_supported(void)
2503{
2504 return cpu_has_vmx_invpcid() && enable_ept;
2505}
2506
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507/*
Eddie Donga75beee2007-05-17 18:55:15 +03002508 * Swap MSR entry in host/guest MSR entry array.
2509 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002510static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002511{
Avi Kivity26bb0982009-09-07 11:14:12 +03002512 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002513
2514 tmp = vmx->guest_msrs[to];
2515 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2516 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002517}
2518
Yang Zhang8d146952013-01-25 10:18:50 +08002519static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2520{
2521 unsigned long *msr_bitmap;
2522
Wincy Van670125b2015-03-04 14:31:56 +08002523 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002524 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002525 else if (cpu_has_secondary_exec_ctrls() &&
2526 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2527 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002528 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2529 if (is_long_mode(vcpu))
2530 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2531 else
2532 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2533 } else {
2534 if (is_long_mode(vcpu))
2535 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv_inactive;
2536 else
2537 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv_inactive;
2538 }
Yang Zhang8d146952013-01-25 10:18:50 +08002539 } else {
2540 if (is_long_mode(vcpu))
2541 msr_bitmap = vmx_msr_bitmap_longmode;
2542 else
2543 msr_bitmap = vmx_msr_bitmap_legacy;
2544 }
2545
2546 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2547}
2548
Eddie Donga75beee2007-05-17 18:55:15 +03002549/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002550 * Set up the vmcs to automatically save and restore system
2551 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2552 * mode, as fiddling with msrs is very expensive.
2553 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002554static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002555{
Avi Kivity26bb0982009-09-07 11:14:12 +03002556 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002557
Eddie Donga75beee2007-05-17 18:55:15 +03002558 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002559#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002560 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002561 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002562 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002563 move_msr_up(vmx, index, save_nmsrs++);
2564 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002565 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002566 move_msr_up(vmx, index, save_nmsrs++);
2567 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002568 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002569 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002570 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002571 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002572 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002573 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002574 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002575 * if efer.sce is enabled.
2576 */
Brian Gerst8c065852010-07-17 09:03:26 -04002577 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002578 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002579 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002580 }
Eddie Donga75beee2007-05-17 18:55:15 +03002581#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002582 index = __find_msr_index(vmx, MSR_EFER);
2583 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002584 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002585
Avi Kivity26bb0982009-09-07 11:14:12 +03002586 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002587
Yang Zhang8d146952013-01-25 10:18:50 +08002588 if (cpu_has_vmx_msr_bitmap())
2589 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002590}
2591
2592/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002594 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2595 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002597static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598{
2599 u64 host_tsc, tsc_offset;
2600
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002601 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002603 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604}
2605
2606/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002607 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002609static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002611 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002612 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002613 * We're here if L1 chose not to trap WRMSR to TSC. According
2614 * to the spec, this should set L1's TSC; The offset that L1
2615 * set for L2 remains unchanged, and still needs to be added
2616 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002617 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002618 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002619 /* recalculate vmcs02.TSC_OFFSET: */
2620 vmcs12 = get_vmcs12(vcpu);
2621 vmcs_write64(TSC_OFFSET, offset +
2622 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2623 vmcs12->tsc_offset : 0));
2624 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002625 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2626 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002627 vmcs_write64(TSC_OFFSET, offset);
2628 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002629}
2630
Nadav Har'El801d3422011-05-25 23:02:23 +03002631static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2632{
2633 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2634 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2635}
2636
2637/*
2638 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2639 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2640 * all guests if the "nested" module option is off, and can also be disabled
2641 * for a single guest by disabling its VMX cpuid bit.
2642 */
2643static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2644{
2645 return nested && guest_cpuid_has_vmx(vcpu);
2646}
2647
Avi Kivity6aa8b732006-12-10 02:21:36 -08002648/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002649 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2650 * returned for the various VMX controls MSRs when nested VMX is enabled.
2651 * The same values should also be used to verify that vmcs12 control fields are
2652 * valid during nested entry from L1 to L2.
2653 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2654 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2655 * bit in the high half is on if the corresponding bit in the control field
2656 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002657 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002658static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002659{
2660 /*
2661 * Note that as a general rule, the high half of the MSRs (bits in
2662 * the control fields which may be 1) should be initialized by the
2663 * intersection of the underlying hardware's MSR (i.e., features which
2664 * can be supported) and the list of features we want to expose -
2665 * because they are known to be properly supported in our code.
2666 * Also, usually, the low half of the MSRs (bits which must be 1) can
2667 * be set to 0, meaning that L1 may turn off any of these bits. The
2668 * reason is that if one of these bits is necessary, it will appear
2669 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2670 * fields of vmcs01 and vmcs02, will turn these bits off - and
2671 * nested_vmx_exit_handled() will not pass related exits to L1.
2672 * These rules have exceptions below.
2673 */
2674
2675 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002676 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002677 vmx->nested.nested_vmx_pinbased_ctls_low,
2678 vmx->nested.nested_vmx_pinbased_ctls_high);
2679 vmx->nested.nested_vmx_pinbased_ctls_low |=
2680 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2681 vmx->nested.nested_vmx_pinbased_ctls_high &=
2682 PIN_BASED_EXT_INTR_MASK |
2683 PIN_BASED_NMI_EXITING |
2684 PIN_BASED_VIRTUAL_NMIS;
2685 vmx->nested.nested_vmx_pinbased_ctls_high |=
2686 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002687 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002688 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002689 vmx->nested.nested_vmx_pinbased_ctls_high |=
2690 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002691
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002692 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002693 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002694 vmx->nested.nested_vmx_exit_ctls_low,
2695 vmx->nested.nested_vmx_exit_ctls_high);
2696 vmx->nested.nested_vmx_exit_ctls_low =
2697 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002698
Wincy Vanb9c237b2015-02-03 23:56:30 +08002699 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002700#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002701 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002702#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002703 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002704 vmx->nested.nested_vmx_exit_ctls_high |=
2705 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002706 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002707 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2708
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002709 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002710 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002711
Jan Kiszka2996fca2014-06-16 13:59:43 +02002712 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002713 vmx->nested.nested_vmx_true_exit_ctls_low =
2714 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002715 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2716
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002717 /* entry controls */
2718 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002719 vmx->nested.nested_vmx_entry_ctls_low,
2720 vmx->nested.nested_vmx_entry_ctls_high);
2721 vmx->nested.nested_vmx_entry_ctls_low =
2722 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2723 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002724#ifdef CONFIG_X86_64
2725 VM_ENTRY_IA32E_MODE |
2726#endif
2727 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002728 vmx->nested.nested_vmx_entry_ctls_high |=
2729 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002730 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002731 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002732
Jan Kiszka2996fca2014-06-16 13:59:43 +02002733 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002734 vmx->nested.nested_vmx_true_entry_ctls_low =
2735 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002736 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2737
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002738 /* cpu-based controls */
2739 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002740 vmx->nested.nested_vmx_procbased_ctls_low,
2741 vmx->nested.nested_vmx_procbased_ctls_high);
2742 vmx->nested.nested_vmx_procbased_ctls_low =
2743 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2744 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002745 CPU_BASED_VIRTUAL_INTR_PENDING |
2746 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002747 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2748 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2749 CPU_BASED_CR3_STORE_EXITING |
2750#ifdef CONFIG_X86_64
2751 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2752#endif
2753 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002754 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2755 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2756 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2757 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002758 /*
2759 * We can allow some features even when not supported by the
2760 * hardware. For example, L1 can specify an MSR bitmap - and we
2761 * can use it to avoid exits to L1 - even when L0 runs L2
2762 * without MSR bitmaps.
2763 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002764 vmx->nested.nested_vmx_procbased_ctls_high |=
2765 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002766 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002767
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002768 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002769 vmx->nested.nested_vmx_true_procbased_ctls_low =
2770 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002771 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2772
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002773 /* secondary cpu-based controls */
2774 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002775 vmx->nested.nested_vmx_secondary_ctls_low,
2776 vmx->nested.nested_vmx_secondary_ctls_high);
2777 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2778 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002779 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002780 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002781 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002782 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002783 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002784 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002785 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002786 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002787
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002788 if (enable_ept) {
2789 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002790 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002791 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002792 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002793 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2794 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002795 if (cpu_has_vmx_ept_execute_only())
2796 vmx->nested.nested_vmx_ept_caps |=
2797 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002798 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002799 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2800 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002801 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002802 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002803
Paolo Bonzinief697a72016-03-18 16:58:38 +01002804 /*
2805 * Old versions of KVM use the single-context version without
2806 * checking for support, so declare that it is supported even
2807 * though it is treated as global context. The alternative is
2808 * not failing the single-context invvpid, and it is worse.
2809 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002810 if (enable_vpid)
2811 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Paolo Bonzinief697a72016-03-18 16:58:38 +01002812 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT |
Wanpeng Li089d7b62015-10-13 09:18:37 -07002813 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
2814 else
2815 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002816
Radim Krčmář0790ec12015-03-17 14:02:32 +01002817 if (enable_unrestricted_guest)
2818 vmx->nested.nested_vmx_secondary_ctls_high |=
2819 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2820
Jan Kiszkac18911a2013-03-13 16:06:41 +01002821 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002822 rdmsr(MSR_IA32_VMX_MISC,
2823 vmx->nested.nested_vmx_misc_low,
2824 vmx->nested.nested_vmx_misc_high);
2825 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2826 vmx->nested.nested_vmx_misc_low |=
2827 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002828 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002829 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002830}
2831
2832static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2833{
2834 /*
2835 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2836 */
2837 return ((control & high) | low) == control;
2838}
2839
2840static inline u64 vmx_control_msr(u32 low, u32 high)
2841{
2842 return low | ((u64)high << 32);
2843}
2844
Jan Kiszkacae50132014-01-04 18:47:22 +01002845/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002846static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2847{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002848 struct vcpu_vmx *vmx = to_vmx(vcpu);
2849
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002850 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002851 case MSR_IA32_VMX_BASIC:
2852 /*
2853 * This MSR reports some information about VMX support. We
2854 * should return information about the VMX we emulate for the
2855 * guest, and the VMCS structure we give it - not about the
2856 * VMX support of the underlying hardware.
2857 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002858 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002859 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2860 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03002861 if (cpu_has_vmx_basic_inout())
2862 *pdata |= VMX_BASIC_INOUT;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002863 break;
2864 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2865 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002866 *pdata = vmx_control_msr(
2867 vmx->nested.nested_vmx_pinbased_ctls_low,
2868 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002869 break;
2870 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002871 *pdata = vmx_control_msr(
2872 vmx->nested.nested_vmx_true_procbased_ctls_low,
2873 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002874 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002875 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002876 *pdata = vmx_control_msr(
2877 vmx->nested.nested_vmx_procbased_ctls_low,
2878 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002879 break;
2880 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002881 *pdata = vmx_control_msr(
2882 vmx->nested.nested_vmx_true_exit_ctls_low,
2883 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002884 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002885 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002886 *pdata = vmx_control_msr(
2887 vmx->nested.nested_vmx_exit_ctls_low,
2888 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002889 break;
2890 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002891 *pdata = vmx_control_msr(
2892 vmx->nested.nested_vmx_true_entry_ctls_low,
2893 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002894 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002895 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002896 *pdata = vmx_control_msr(
2897 vmx->nested.nested_vmx_entry_ctls_low,
2898 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002899 break;
2900 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002901 *pdata = vmx_control_msr(
2902 vmx->nested.nested_vmx_misc_low,
2903 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002904 break;
2905 /*
2906 * These MSRs specify bits which the guest must keep fixed (on or off)
2907 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2908 * We picked the standard core2 setting.
2909 */
2910#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2911#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2912 case MSR_IA32_VMX_CR0_FIXED0:
2913 *pdata = VMXON_CR0_ALWAYSON;
2914 break;
2915 case MSR_IA32_VMX_CR0_FIXED1:
2916 *pdata = -1ULL;
2917 break;
2918 case MSR_IA32_VMX_CR4_FIXED0:
2919 *pdata = VMXON_CR4_ALWAYSON;
2920 break;
2921 case MSR_IA32_VMX_CR4_FIXED1:
2922 *pdata = -1ULL;
2923 break;
2924 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002925 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002926 break;
2927 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002928 *pdata = vmx_control_msr(
2929 vmx->nested.nested_vmx_secondary_ctls_low,
2930 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002931 break;
2932 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07002933 *pdata = vmx->nested.nested_vmx_ept_caps |
2934 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002935 break;
2936 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002937 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002938 }
2939
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002940 return 0;
2941}
2942
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002943static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2944 uint64_t val)
2945{
2946 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2947
2948 return !(val & ~valid_bits);
2949}
2950
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002951/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002952 * Reads an msr value (of 'msr_index') into 'pdata'.
2953 * Returns 0 on success, non-0 otherwise.
2954 * Assumes vcpu_load() was already called.
2955 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002956static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957{
Avi Kivity26bb0982009-09-07 11:14:12 +03002958 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002960 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002961#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002963 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 break;
2965 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002966 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002968 case MSR_KERNEL_GS_BASE:
2969 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002970 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002971 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002972#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002974 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302975 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002976 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977 break;
2978 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002979 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980 break;
2981 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002982 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 break;
2984 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002985 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002986 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002987 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002988 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002989 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002990 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002991 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002992 case MSR_IA32_MCG_EXT_CTL:
2993 if (!msr_info->host_initiated &&
2994 !(to_vmx(vcpu)->msr_ia32_feature_control &
2995 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01002996 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08002997 msr_info->data = vcpu->arch.mcg_ext_ctl;
2998 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002999 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003000 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003001 break;
3002 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3003 if (!nested_vmx_allowed(vcpu))
3004 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003005 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003006 case MSR_IA32_XSS:
3007 if (!vmx_xsaves_supported())
3008 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003009 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003010 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003011 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003012 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003013 return 1;
3014 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003016 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003017 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003018 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003019 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003020 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003021 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 }
3023
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024 return 0;
3025}
3026
Jan Kiszkacae50132014-01-04 18:47:22 +01003027static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3028
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029/*
3030 * Writes msr value into into the appropriate "register".
3031 * Returns 0 on success, non-0 otherwise.
3032 * Assumes vcpu_load() was already called.
3033 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003034static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003036 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003037 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003038 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003039 u32 msr_index = msr_info->index;
3040 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003041
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003043 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003044 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003045 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003046#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003048 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049 vmcs_writel(GUEST_FS_BASE, data);
3050 break;
3051 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003052 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 vmcs_writel(GUEST_GS_BASE, data);
3054 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003055 case MSR_KERNEL_GS_BASE:
3056 vmx_load_host_state(vmx);
3057 vmx->msr_guest_kernel_gs_base = data;
3058 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059#endif
3060 case MSR_IA32_SYSENTER_CS:
3061 vmcs_write32(GUEST_SYSENTER_CS, data);
3062 break;
3063 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003064 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065 break;
3066 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003067 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003069 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003070 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003071 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003072 vmcs_write64(GUEST_BNDCFGS, data);
3073 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303074 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003075 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003077 case MSR_IA32_CR_PAT:
3078 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003079 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3080 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003081 vmcs_write64(GUEST_IA32_PAT, data);
3082 vcpu->arch.pat = data;
3083 break;
3084 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003085 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003086 break;
Will Auldba904632012-11-29 12:42:50 -08003087 case MSR_IA32_TSC_ADJUST:
3088 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003089 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003090 case MSR_IA32_MCG_EXT_CTL:
3091 if ((!msr_info->host_initiated &&
3092 !(to_vmx(vcpu)->msr_ia32_feature_control &
3093 FEATURE_CONTROL_LMCE)) ||
3094 (data & ~MCG_EXT_CTL_LMCE_EN))
3095 return 1;
3096 vcpu->arch.mcg_ext_ctl = data;
3097 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003098 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003099 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003100 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003101 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3102 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003103 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003104 if (msr_info->host_initiated && data == 0)
3105 vmx_leave_nested(vcpu);
3106 break;
3107 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3108 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003109 case MSR_IA32_XSS:
3110 if (!vmx_xsaves_supported())
3111 return 1;
3112 /*
3113 * The only supported bit as of Skylake is bit 8, but
3114 * it is not supported on KVM.
3115 */
3116 if (data != 0)
3117 return 1;
3118 vcpu->arch.ia32_xss = data;
3119 if (vcpu->arch.ia32_xss != host_xss)
3120 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3121 vcpu->arch.ia32_xss, host_xss);
3122 else
3123 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3124 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003125 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003126 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003127 return 1;
3128 /* Check reserved bit, higher 32 bits should be zero */
3129 if ((data >> 32) != 0)
3130 return 1;
3131 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003133 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003134 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003135 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003136 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003137 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3138 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003139 ret = kvm_set_shared_msr(msr->index, msr->data,
3140 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003141 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003142 if (ret)
3143 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003144 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003145 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003147 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148 }
3149
Eddie Dong2cc51562007-05-21 07:28:09 +03003150 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151}
3152
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003153static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003155 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3156 switch (reg) {
3157 case VCPU_REGS_RSP:
3158 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3159 break;
3160 case VCPU_REGS_RIP:
3161 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3162 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003163 case VCPU_EXREG_PDPTR:
3164 if (enable_ept)
3165 ept_save_pdptrs(vcpu);
3166 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003167 default:
3168 break;
3169 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170}
3171
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172static __init int cpu_has_kvm_support(void)
3173{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003174 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175}
3176
3177static __init int vmx_disabled_by_bios(void)
3178{
3179 u64 msr;
3180
3181 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003182 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003183 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003184 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3185 && tboot_enabled())
3186 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003187 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003188 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003189 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003190 && !tboot_enabled()) {
3191 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003192 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003193 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003194 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003195 /* launched w/o TXT and VMX disabled */
3196 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3197 && !tboot_enabled())
3198 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003199 }
3200
3201 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202}
3203
Dongxiao Xu7725b892010-05-11 18:29:38 +08003204static void kvm_cpu_vmxon(u64 addr)
3205{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003206 intel_pt_handle_vmx(1);
3207
Dongxiao Xu7725b892010-05-11 18:29:38 +08003208 asm volatile (ASM_VMX_VMXON_RAX
3209 : : "a"(&addr), "m"(addr)
3210 : "memory", "cc");
3211}
3212
Radim Krčmář13a34e02014-08-28 15:13:03 +02003213static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214{
3215 int cpu = raw_smp_processor_id();
3216 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003217 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003219 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003220 return -EBUSY;
3221
Nadav Har'Eld462b812011-05-24 15:26:10 +03003222 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003223 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3224 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003225
3226 /*
3227 * Now we can enable the vmclear operation in kdump
3228 * since the loaded_vmcss_on_cpu list on this cpu
3229 * has been initialized.
3230 *
3231 * Though the cpu is not in VMX operation now, there
3232 * is no problem to enable the vmclear operation
3233 * for the loaded_vmcss_on_cpu list is empty!
3234 */
3235 crash_enable_local_vmclear(cpu);
3236
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003238
3239 test_bits = FEATURE_CONTROL_LOCKED;
3240 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3241 if (tboot_enabled())
3242 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3243
3244 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003246 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3247 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003248 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003249
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003250 if (vmm_exclusive) {
3251 kvm_cpu_vmxon(phys_addr);
3252 ept_sync_global();
3253 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003254
Christoph Lameter89cbc762014-08-17 12:30:40 -05003255 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003256
Alexander Graf10474ae2009-09-15 11:37:46 +02003257 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258}
3259
Nadav Har'Eld462b812011-05-24 15:26:10 +03003260static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003261{
3262 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003263 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003264
Nadav Har'Eld462b812011-05-24 15:26:10 +03003265 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3266 loaded_vmcss_on_cpu_link)
3267 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003268}
3269
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003270
3271/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3272 * tricks.
3273 */
3274static void kvm_cpu_vmxoff(void)
3275{
3276 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003277
3278 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003279}
3280
Radim Krčmář13a34e02014-08-28 15:13:03 +02003281static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003283 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003284 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003285 kvm_cpu_vmxoff();
3286 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003287 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288}
3289
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003290static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003291 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292{
3293 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003294 u32 ctl = ctl_min | ctl_opt;
3295
3296 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3297
3298 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3299 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3300
3301 /* Ensure minimum (required) set of control bits are supported. */
3302 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003303 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003304
3305 *result = ctl;
3306 return 0;
3307}
3308
Avi Kivity110312c2010-12-21 12:54:20 +02003309static __init bool allow_1_setting(u32 msr, u32 ctl)
3310{
3311 u32 vmx_msr_low, vmx_msr_high;
3312
3313 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3314 return vmx_msr_high & ctl;
3315}
3316
Yang, Sheng002c7f72007-07-31 14:23:01 +03003317static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003318{
3319 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003320 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003321 u32 _pin_based_exec_control = 0;
3322 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003323 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003324 u32 _vmexit_control = 0;
3325 u32 _vmentry_control = 0;
3326
Raghavendra K T10166742012-02-07 23:19:20 +05303327 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003328#ifdef CONFIG_X86_64
3329 CPU_BASED_CR8_LOAD_EXITING |
3330 CPU_BASED_CR8_STORE_EXITING |
3331#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003332 CPU_BASED_CR3_LOAD_EXITING |
3333 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003334 CPU_BASED_USE_IO_BITMAPS |
3335 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003336 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003337 CPU_BASED_MWAIT_EXITING |
3338 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003339 CPU_BASED_INVLPG_EXITING |
3340 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003341
Sheng Yangf78e0e22007-10-29 09:40:42 +08003342 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003343 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003344 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003345 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3346 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003347 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003348#ifdef CONFIG_X86_64
3349 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3350 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3351 ~CPU_BASED_CR8_STORE_EXITING;
3352#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003353 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003354 min2 = 0;
3355 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003356 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003357 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003358 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003359 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003360 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003361 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003362 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003363 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003364 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003365 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003366 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003367 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003368 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003369 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003370 if (adjust_vmx_controls(min2, opt2,
3371 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003372 &_cpu_based_2nd_exec_control) < 0)
3373 return -EIO;
3374 }
3375#ifndef CONFIG_X86_64
3376 if (!(_cpu_based_2nd_exec_control &
3377 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3378 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3379#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003380
3381 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3382 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003383 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003384 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3385 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003386
Sheng Yangd56f5462008-04-25 10:13:16 +08003387 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003388 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3389 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003390 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3391 CPU_BASED_CR3_STORE_EXITING |
3392 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003393 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3394 vmx_capability.ept, vmx_capability.vpid);
3395 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003396
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003397 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003398#ifdef CONFIG_X86_64
3399 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3400#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003401 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003402 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003403 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3404 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003405 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003406
Yang Zhang01e439b2013-04-11 19:25:12 +08003407 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003408 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3409 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003410 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3411 &_pin_based_exec_control) < 0)
3412 return -EIO;
3413
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003414 if (cpu_has_broken_vmx_preemption_timer())
3415 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003416 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003417 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003418 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3419
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003420 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003421 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003422 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3423 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003424 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003426 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003427
3428 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3429 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003430 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003431
3432#ifdef CONFIG_X86_64
3433 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3434 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003435 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003436#endif
3437
3438 /* Require Write-Back (WB) memory type for VMCS accesses. */
3439 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003440 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003441
Yang, Sheng002c7f72007-07-31 14:23:01 +03003442 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003443 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003444 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003445 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003446
Yang, Sheng002c7f72007-07-31 14:23:01 +03003447 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3448 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003449 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003450 vmcs_conf->vmexit_ctrl = _vmexit_control;
3451 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003452
Avi Kivity110312c2010-12-21 12:54:20 +02003453 cpu_has_load_ia32_efer =
3454 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3455 VM_ENTRY_LOAD_IA32_EFER)
3456 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3457 VM_EXIT_LOAD_IA32_EFER);
3458
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003459 cpu_has_load_perf_global_ctrl =
3460 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3461 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3462 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3463 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3464
3465 /*
3466 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003467 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003468 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3469 *
3470 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3471 *
3472 * AAK155 (model 26)
3473 * AAP115 (model 30)
3474 * AAT100 (model 37)
3475 * BC86,AAY89,BD102 (model 44)
3476 * BA97 (model 46)
3477 *
3478 */
3479 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3480 switch (boot_cpu_data.x86_model) {
3481 case 26:
3482 case 30:
3483 case 37:
3484 case 44:
3485 case 46:
3486 cpu_has_load_perf_global_ctrl = false;
3487 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3488 "does not work properly. Using workaround\n");
3489 break;
3490 default:
3491 break;
3492 }
3493 }
3494
Borislav Petkov782511b2016-04-04 22:25:03 +02003495 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003496 rdmsrl(MSR_IA32_XSS, host_xss);
3497
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003498 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003499}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500
3501static struct vmcs *alloc_vmcs_cpu(int cpu)
3502{
3503 int node = cpu_to_node(cpu);
3504 struct page *pages;
3505 struct vmcs *vmcs;
3506
Vlastimil Babka96db8002015-09-08 15:03:50 -07003507 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508 if (!pages)
3509 return NULL;
3510 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003511 memset(vmcs, 0, vmcs_config.size);
3512 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 return vmcs;
3514}
3515
3516static struct vmcs *alloc_vmcs(void)
3517{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003518 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519}
3520
3521static void free_vmcs(struct vmcs *vmcs)
3522{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003523 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524}
3525
Nadav Har'Eld462b812011-05-24 15:26:10 +03003526/*
3527 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3528 */
3529static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3530{
3531 if (!loaded_vmcs->vmcs)
3532 return;
3533 loaded_vmcs_clear(loaded_vmcs);
3534 free_vmcs(loaded_vmcs->vmcs);
3535 loaded_vmcs->vmcs = NULL;
3536}
3537
Sam Ravnborg39959582007-06-01 00:47:13 -07003538static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003539{
3540 int cpu;
3541
Zachary Amsden3230bb42009-09-29 11:38:37 -10003542 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003543 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003544 per_cpu(vmxarea, cpu) = NULL;
3545 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003546}
3547
Bandan Dasfe2b2012014-04-21 15:20:14 -04003548static void init_vmcs_shadow_fields(void)
3549{
3550 int i, j;
3551
3552 /* No checks for read only fields yet */
3553
3554 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3555 switch (shadow_read_write_fields[i]) {
3556 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003557 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003558 continue;
3559 break;
3560 default:
3561 break;
3562 }
3563
3564 if (j < i)
3565 shadow_read_write_fields[j] =
3566 shadow_read_write_fields[i];
3567 j++;
3568 }
3569 max_shadow_read_write_fields = j;
3570
3571 /* shadowed fields guest access without vmexit */
3572 for (i = 0; i < max_shadow_read_write_fields; i++) {
3573 clear_bit(shadow_read_write_fields[i],
3574 vmx_vmwrite_bitmap);
3575 clear_bit(shadow_read_write_fields[i],
3576 vmx_vmread_bitmap);
3577 }
3578 for (i = 0; i < max_shadow_read_only_fields; i++)
3579 clear_bit(shadow_read_only_fields[i],
3580 vmx_vmread_bitmap);
3581}
3582
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583static __init int alloc_kvm_area(void)
3584{
3585 int cpu;
3586
Zachary Amsden3230bb42009-09-29 11:38:37 -10003587 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003588 struct vmcs *vmcs;
3589
3590 vmcs = alloc_vmcs_cpu(cpu);
3591 if (!vmcs) {
3592 free_kvm_area();
3593 return -ENOMEM;
3594 }
3595
3596 per_cpu(vmxarea, cpu) = vmcs;
3597 }
3598 return 0;
3599}
3600
Gleb Natapov14168782013-01-21 15:36:49 +02003601static bool emulation_required(struct kvm_vcpu *vcpu)
3602{
3603 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3604}
3605
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003606static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003607 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003609 if (!emulate_invalid_guest_state) {
3610 /*
3611 * CS and SS RPL should be equal during guest entry according
3612 * to VMX spec, but in reality it is not always so. Since vcpu
3613 * is in the middle of the transition from real mode to
3614 * protected mode it is safe to assume that RPL 0 is a good
3615 * default value.
3616 */
3617 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003618 save->selector &= ~SEGMENT_RPL_MASK;
3619 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003620 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003621 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003622 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003623}
3624
3625static void enter_pmode(struct kvm_vcpu *vcpu)
3626{
3627 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003628 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003629
Gleb Natapovd99e4152012-12-20 16:57:45 +02003630 /*
3631 * Update real mode segment cache. It may be not up-to-date if sement
3632 * register was written while vcpu was in a guest mode.
3633 */
3634 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3635 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3636 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3637 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3638 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3639 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3640
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003641 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642
Avi Kivity2fb92db2011-04-27 19:42:18 +03003643 vmx_segment_cache_clear(vmx);
3644
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003645 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003646
3647 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003648 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3649 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003650 vmcs_writel(GUEST_RFLAGS, flags);
3651
Rusty Russell66aee912007-07-17 23:34:16 +10003652 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3653 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003654
3655 update_exception_bitmap(vcpu);
3656
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003657 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3658 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3659 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3660 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3661 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3662 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003663}
3664
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003665static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003666{
Mathias Krause772e0312012-08-30 01:30:19 +02003667 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003668 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669
Gleb Natapovd99e4152012-12-20 16:57:45 +02003670 var.dpl = 0x3;
3671 if (seg == VCPU_SREG_CS)
3672 var.type = 0x3;
3673
3674 if (!emulate_invalid_guest_state) {
3675 var.selector = var.base >> 4;
3676 var.base = var.base & 0xffff0;
3677 var.limit = 0xffff;
3678 var.g = 0;
3679 var.db = 0;
3680 var.present = 1;
3681 var.s = 1;
3682 var.l = 0;
3683 var.unusable = 0;
3684 var.type = 0x3;
3685 var.avl = 0;
3686 if (save->base & 0xf)
3687 printk_once(KERN_WARNING "kvm: segment base is not "
3688 "paragraph aligned when entering "
3689 "protected mode (seg=%d)", seg);
3690 }
3691
3692 vmcs_write16(sf->selector, var.selector);
3693 vmcs_write32(sf->base, var.base);
3694 vmcs_write32(sf->limit, var.limit);
3695 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696}
3697
3698static void enter_rmode(struct kvm_vcpu *vcpu)
3699{
3700 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003701 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003702
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003703 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3704 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3705 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3706 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3707 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003708 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3709 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003710
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003711 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712
Gleb Natapov776e58e2011-03-13 12:34:27 +02003713 /*
3714 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003715 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003716 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003717 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003718 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3719 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003720
Avi Kivity2fb92db2011-04-27 19:42:18 +03003721 vmx_segment_cache_clear(vmx);
3722
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003723 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003724 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003725 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3726
3727 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003728 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003729
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003730 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731
3732 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003733 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734 update_exception_bitmap(vcpu);
3735
Gleb Natapovd99e4152012-12-20 16:57:45 +02003736 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3737 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3738 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3739 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3740 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3741 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003742
Eddie Dong8668a3c2007-10-10 14:26:45 +08003743 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003744}
3745
Amit Shah401d10d2009-02-20 22:53:37 +05303746static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3747{
3748 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003749 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3750
3751 if (!msr)
3752 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303753
Avi Kivity44ea2b12009-09-06 15:55:37 +03003754 /*
3755 * Force kernel_gs_base reloading before EFER changes, as control
3756 * of this msr depends on is_long_mode().
3757 */
3758 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003759 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303760 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003761 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303762 msr->data = efer;
3763 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003764 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303765
3766 msr->data = efer & ~EFER_LME;
3767 }
3768 setup_msrs(vmx);
3769}
3770
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003771#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772
3773static void enter_lmode(struct kvm_vcpu *vcpu)
3774{
3775 u32 guest_tr_ar;
3776
Avi Kivity2fb92db2011-04-27 19:42:18 +03003777 vmx_segment_cache_clear(to_vmx(vcpu));
3778
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003780 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003781 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3782 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003783 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003784 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3785 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003786 }
Avi Kivityda38f432010-07-06 11:30:49 +03003787 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788}
3789
3790static void exit_lmode(struct kvm_vcpu *vcpu)
3791{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003792 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003793 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794}
3795
3796#endif
3797
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003798static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003799{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003800 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003801 if (enable_ept) {
3802 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3803 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003804 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003805 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003806}
3807
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003808static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3809{
3810 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3811}
3812
Avi Kivitye8467fd2009-12-29 18:43:06 +02003813static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3814{
3815 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3816
3817 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3818 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3819}
3820
Avi Kivityaff48ba2010-12-05 18:56:11 +02003821static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3822{
3823 if (enable_ept && is_paging(vcpu))
3824 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3825 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3826}
3827
Anthony Liguori25c4c272007-04-27 09:29:21 +03003828static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003829{
Avi Kivityfc78f512009-12-07 12:16:48 +02003830 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3831
3832 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3833 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003834}
3835
Sheng Yang14394422008-04-28 12:24:45 +08003836static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3837{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003838 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3839
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003840 if (!test_bit(VCPU_EXREG_PDPTR,
3841 (unsigned long *)&vcpu->arch.regs_dirty))
3842 return;
3843
Sheng Yang14394422008-04-28 12:24:45 +08003844 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003845 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3846 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3847 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3848 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003849 }
3850}
3851
Avi Kivity8f5d5492009-05-31 18:41:29 +03003852static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3853{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003854 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3855
Avi Kivity8f5d5492009-05-31 18:41:29 +03003856 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003857 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3858 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3859 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3860 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003861 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003862
3863 __set_bit(VCPU_EXREG_PDPTR,
3864 (unsigned long *)&vcpu->arch.regs_avail);
3865 __set_bit(VCPU_EXREG_PDPTR,
3866 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003867}
3868
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003869static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003870
3871static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3872 unsigned long cr0,
3873 struct kvm_vcpu *vcpu)
3874{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003875 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3876 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003877 if (!(cr0 & X86_CR0_PG)) {
3878 /* From paging/starting to nonpaging */
3879 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003880 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003881 (CPU_BASED_CR3_LOAD_EXITING |
3882 CPU_BASED_CR3_STORE_EXITING));
3883 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003884 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003885 } else if (!is_paging(vcpu)) {
3886 /* From nonpaging to paging */
3887 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003888 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003889 ~(CPU_BASED_CR3_LOAD_EXITING |
3890 CPU_BASED_CR3_STORE_EXITING));
3891 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003892 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003893 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003894
3895 if (!(cr0 & X86_CR0_WP))
3896 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003897}
3898
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3900{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003901 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003902 unsigned long hw_cr0;
3903
Gleb Natapov50378782013-02-04 16:00:28 +02003904 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003905 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003906 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003907 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003908 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003909
Gleb Natapov218e7632013-01-21 15:36:45 +02003910 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3911 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912
Gleb Natapov218e7632013-01-21 15:36:45 +02003913 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3914 enter_rmode(vcpu);
3915 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003917#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003918 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003919 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003921 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922 exit_lmode(vcpu);
3923 }
3924#endif
3925
Avi Kivity089d0342009-03-23 18:26:32 +02003926 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003927 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3928
Avi Kivity02daab22009-12-30 12:40:26 +02003929 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003930 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003931
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003933 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003934 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003935
3936 /* depends on vcpu->arch.cr0 to be set to a new value */
3937 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938}
3939
Sheng Yang14394422008-04-28 12:24:45 +08003940static u64 construct_eptp(unsigned long root_hpa)
3941{
3942 u64 eptp;
3943
3944 /* TODO write the value reading from MSR */
3945 eptp = VMX_EPT_DEFAULT_MT |
3946 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003947 if (enable_ept_ad_bits)
3948 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003949 eptp |= (root_hpa & PAGE_MASK);
3950
3951 return eptp;
3952}
3953
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3955{
Sheng Yang14394422008-04-28 12:24:45 +08003956 unsigned long guest_cr3;
3957 u64 eptp;
3958
3959 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003960 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003961 eptp = construct_eptp(cr3);
3962 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003963 if (is_paging(vcpu) || is_guest_mode(vcpu))
3964 guest_cr3 = kvm_read_cr3(vcpu);
3965 else
3966 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003967 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003968 }
3969
Sheng Yang2384d2b2008-01-17 15:14:33 +08003970 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003971 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003972}
3973
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003974static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975{
Ben Serebrin085e68e2015-04-16 11:58:05 -07003976 /*
3977 * Pass through host's Machine Check Enable value to hw_cr4, which
3978 * is in force while we are in guest mode. Do not let guests control
3979 * this bit, even if host CR4.MCE == 0.
3980 */
3981 unsigned long hw_cr4 =
3982 (cr4_read_shadow() & X86_CR4_MCE) |
3983 (cr4 & ~X86_CR4_MCE) |
3984 (to_vmx(vcpu)->rmode.vm86_active ?
3985 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08003986
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003987 if (cr4 & X86_CR4_VMXE) {
3988 /*
3989 * To use VMXON (and later other VMX instructions), a guest
3990 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3991 * So basically the check on whether to allow nested VMX
3992 * is here.
3993 */
3994 if (!nested_vmx_allowed(vcpu))
3995 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003996 }
3997 if (to_vmx(vcpu)->nested.vmxon &&
3998 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003999 return 1;
4000
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004001 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004002 if (enable_ept) {
4003 if (!is_paging(vcpu)) {
4004 hw_cr4 &= ~X86_CR4_PAE;
4005 hw_cr4 |= X86_CR4_PSE;
4006 } else if (!(cr4 & X86_CR4_PAE)) {
4007 hw_cr4 &= ~X86_CR4_PAE;
4008 }
4009 }
Sheng Yang14394422008-04-28 12:24:45 +08004010
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004011 if (!enable_unrestricted_guest && !is_paging(vcpu))
4012 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004013 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4014 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4015 * to be manually disabled when guest switches to non-paging
4016 * mode.
4017 *
4018 * If !enable_unrestricted_guest, the CPU is always running
4019 * with CR0.PG=1 and CR4 needs to be modified.
4020 * If enable_unrestricted_guest, the CPU automatically
4021 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004022 */
Huaitong Handdba2622016-03-22 16:51:15 +08004023 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004024
Sheng Yang14394422008-04-28 12:24:45 +08004025 vmcs_writel(CR4_READ_SHADOW, cr4);
4026 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004027 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004028}
4029
Avi Kivity6aa8b732006-12-10 02:21:36 -08004030static void vmx_get_segment(struct kvm_vcpu *vcpu,
4031 struct kvm_segment *var, int seg)
4032{
Avi Kivitya9179492011-01-03 14:28:52 +02004033 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004034 u32 ar;
4035
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004036 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004037 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004038 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004039 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004040 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004041 var->base = vmx_read_guest_seg_base(vmx, seg);
4042 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4043 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004044 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004045 var->base = vmx_read_guest_seg_base(vmx, seg);
4046 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4047 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4048 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004049 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004050 var->type = ar & 15;
4051 var->s = (ar >> 4) & 1;
4052 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004053 /*
4054 * Some userspaces do not preserve unusable property. Since usable
4055 * segment has to be present according to VMX spec we can use present
4056 * property to amend userspace bug by making unusable segment always
4057 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4058 * segment as unusable.
4059 */
4060 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004061 var->avl = (ar >> 12) & 1;
4062 var->l = (ar >> 13) & 1;
4063 var->db = (ar >> 14) & 1;
4064 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065}
4066
Avi Kivitya9179492011-01-03 14:28:52 +02004067static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4068{
Avi Kivitya9179492011-01-03 14:28:52 +02004069 struct kvm_segment s;
4070
4071 if (to_vmx(vcpu)->rmode.vm86_active) {
4072 vmx_get_segment(vcpu, &s, seg);
4073 return s.base;
4074 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004075 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004076}
4077
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004078static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004079{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004080 struct vcpu_vmx *vmx = to_vmx(vcpu);
4081
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004082 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004083 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004084 else {
4085 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004086 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004087 }
Avi Kivity69c73022011-03-07 15:26:44 +02004088}
4089
Avi Kivity653e3102007-05-07 10:55:37 +03004090static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092 u32 ar;
4093
Avi Kivityf0495f92012-06-07 17:06:10 +03004094 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 ar = 1 << 16;
4096 else {
4097 ar = var->type & 15;
4098 ar |= (var->s & 1) << 4;
4099 ar |= (var->dpl & 3) << 5;
4100 ar |= (var->present & 1) << 7;
4101 ar |= (var->avl & 1) << 12;
4102 ar |= (var->l & 1) << 13;
4103 ar |= (var->db & 1) << 14;
4104 ar |= (var->g & 1) << 15;
4105 }
Avi Kivity653e3102007-05-07 10:55:37 +03004106
4107 return ar;
4108}
4109
4110static void vmx_set_segment(struct kvm_vcpu *vcpu,
4111 struct kvm_segment *var, int seg)
4112{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004113 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004114 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004115
Avi Kivity2fb92db2011-04-27 19:42:18 +03004116 vmx_segment_cache_clear(vmx);
4117
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004118 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4119 vmx->rmode.segs[seg] = *var;
4120 if (seg == VCPU_SREG_TR)
4121 vmcs_write16(sf->selector, var->selector);
4122 else if (var->s)
4123 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004124 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004125 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004126
Avi Kivity653e3102007-05-07 10:55:37 +03004127 vmcs_writel(sf->base, var->base);
4128 vmcs_write32(sf->limit, var->limit);
4129 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004130
4131 /*
4132 * Fix the "Accessed" bit in AR field of segment registers for older
4133 * qemu binaries.
4134 * IA32 arch specifies that at the time of processor reset the
4135 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004136 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004137 * state vmexit when "unrestricted guest" mode is turned on.
4138 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4139 * tree. Newer qemu binaries with that qemu fix would not need this
4140 * kvm hack.
4141 */
4142 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004143 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004144
Gleb Natapovf924d662012-12-12 19:10:55 +02004145 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004146
4147out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004148 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149}
4150
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4152{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004153 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154
4155 *db = (ar >> 14) & 1;
4156 *l = (ar >> 13) & 1;
4157}
4158
Gleb Natapov89a27f42010-02-16 10:51:48 +02004159static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004161 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4162 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163}
4164
Gleb Natapov89a27f42010-02-16 10:51:48 +02004165static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004167 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4168 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169}
4170
Gleb Natapov89a27f42010-02-16 10:51:48 +02004171static void vmx_get_gdt(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_GDTR_LIMIT);
4174 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175}
4176
Gleb Natapov89a27f42010-02-16 10:51:48 +02004177static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004179 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4180 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181}
4182
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004183static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4184{
4185 struct kvm_segment var;
4186 u32 ar;
4187
4188 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004189 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004190 if (seg == VCPU_SREG_CS)
4191 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004192 ar = vmx_segment_access_rights(&var);
4193
4194 if (var.base != (var.selector << 4))
4195 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004196 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004197 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004198 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004199 return false;
4200
4201 return true;
4202}
4203
4204static bool code_segment_valid(struct kvm_vcpu *vcpu)
4205{
4206 struct kvm_segment cs;
4207 unsigned int cs_rpl;
4208
4209 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004210 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004211
Avi Kivity1872a3f2009-01-04 23:26:52 +02004212 if (cs.unusable)
4213 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004214 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004215 return false;
4216 if (!cs.s)
4217 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004218 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004219 if (cs.dpl > cs_rpl)
4220 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004221 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004222 if (cs.dpl != cs_rpl)
4223 return false;
4224 }
4225 if (!cs.present)
4226 return false;
4227
4228 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4229 return true;
4230}
4231
4232static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4233{
4234 struct kvm_segment ss;
4235 unsigned int ss_rpl;
4236
4237 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004238 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004239
Avi Kivity1872a3f2009-01-04 23:26:52 +02004240 if (ss.unusable)
4241 return true;
4242 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004243 return false;
4244 if (!ss.s)
4245 return false;
4246 if (ss.dpl != ss_rpl) /* DPL != RPL */
4247 return false;
4248 if (!ss.present)
4249 return false;
4250
4251 return true;
4252}
4253
4254static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4255{
4256 struct kvm_segment var;
4257 unsigned int rpl;
4258
4259 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004260 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004261
Avi Kivity1872a3f2009-01-04 23:26:52 +02004262 if (var.unusable)
4263 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004264 if (!var.s)
4265 return false;
4266 if (!var.present)
4267 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004268 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004269 if (var.dpl < rpl) /* DPL < RPL */
4270 return false;
4271 }
4272
4273 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4274 * rights flags
4275 */
4276 return true;
4277}
4278
4279static bool tr_valid(struct kvm_vcpu *vcpu)
4280{
4281 struct kvm_segment tr;
4282
4283 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4284
Avi Kivity1872a3f2009-01-04 23:26:52 +02004285 if (tr.unusable)
4286 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004287 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004288 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004289 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004290 return false;
4291 if (!tr.present)
4292 return false;
4293
4294 return true;
4295}
4296
4297static bool ldtr_valid(struct kvm_vcpu *vcpu)
4298{
4299 struct kvm_segment ldtr;
4300
4301 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4302
Avi Kivity1872a3f2009-01-04 23:26:52 +02004303 if (ldtr.unusable)
4304 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004305 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004306 return false;
4307 if (ldtr.type != 2)
4308 return false;
4309 if (!ldtr.present)
4310 return false;
4311
4312 return true;
4313}
4314
4315static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4316{
4317 struct kvm_segment cs, ss;
4318
4319 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4320 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4321
Nadav Amitb32a9912015-03-29 16:33:04 +03004322 return ((cs.selector & SEGMENT_RPL_MASK) ==
4323 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004324}
4325
4326/*
4327 * Check if guest state is valid. Returns true if valid, false if
4328 * not.
4329 * We assume that registers are always usable
4330 */
4331static bool guest_state_valid(struct kvm_vcpu *vcpu)
4332{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004333 if (enable_unrestricted_guest)
4334 return true;
4335
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004336 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004337 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004338 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4339 return false;
4340 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4341 return false;
4342 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4343 return false;
4344 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4345 return false;
4346 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4347 return false;
4348 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4349 return false;
4350 } else {
4351 /* protected mode guest state checks */
4352 if (!cs_ss_rpl_check(vcpu))
4353 return false;
4354 if (!code_segment_valid(vcpu))
4355 return false;
4356 if (!stack_segment_valid(vcpu))
4357 return false;
4358 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4359 return false;
4360 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4361 return false;
4362 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4363 return false;
4364 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4365 return false;
4366 if (!tr_valid(vcpu))
4367 return false;
4368 if (!ldtr_valid(vcpu))
4369 return false;
4370 }
4371 /* TODO:
4372 * - Add checks on RIP
4373 * - Add checks on RFLAGS
4374 */
4375
4376 return true;
4377}
4378
Mike Dayd77c26f2007-10-08 09:02:08 -04004379static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004380{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004381 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004382 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004383 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004384
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004385 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004386 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004387 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4388 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004389 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004390 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004391 r = kvm_write_guest_page(kvm, fn++, &data,
4392 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004393 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004394 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004395 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4396 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004397 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004398 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4399 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004400 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004401 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004402 r = kvm_write_guest_page(kvm, fn, &data,
4403 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4404 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004405out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004406 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004407 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004408}
4409
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004410static int init_rmode_identity_map(struct kvm *kvm)
4411{
Tang Chenf51770e2014-09-16 18:41:59 +08004412 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004413 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004414 u32 tmp;
4415
Avi Kivity089d0342009-03-23 18:26:32 +02004416 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004417 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004418
4419 /* Protect kvm->arch.ept_identity_pagetable_done. */
4420 mutex_lock(&kvm->slots_lock);
4421
Tang Chenf51770e2014-09-16 18:41:59 +08004422 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004423 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004424
Sheng Yangb927a3c2009-07-21 10:42:48 +08004425 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004426
4427 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004428 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004429 goto out2;
4430
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004431 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004432 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4433 if (r < 0)
4434 goto out;
4435 /* Set up identity-mapping pagetable for EPT in real mode */
4436 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4437 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4438 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4439 r = kvm_write_guest_page(kvm, identity_map_pfn,
4440 &tmp, i * sizeof(tmp), sizeof(tmp));
4441 if (r < 0)
4442 goto out;
4443 }
4444 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004445
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004446out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004447 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004448
4449out2:
4450 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004451 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004452}
4453
Avi Kivity6aa8b732006-12-10 02:21:36 -08004454static void seg_setup(int seg)
4455{
Mathias Krause772e0312012-08-30 01:30:19 +02004456 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004457 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004458
4459 vmcs_write16(sf->selector, 0);
4460 vmcs_writel(sf->base, 0);
4461 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004462 ar = 0x93;
4463 if (seg == VCPU_SREG_CS)
4464 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004465
4466 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004467}
4468
Sheng Yangf78e0e22007-10-29 09:40:42 +08004469static int alloc_apic_access_page(struct kvm *kvm)
4470{
Xiao Guangrong44841412012-09-07 14:14:20 +08004471 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004472 int r = 0;
4473
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004474 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004475 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004476 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004477 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4478 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004479 if (r)
4480 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004481
Tang Chen73a6d942014-09-11 13:38:00 +08004482 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004483 if (is_error_page(page)) {
4484 r = -EFAULT;
4485 goto out;
4486 }
4487
Tang Chenc24ae0d2014-09-24 15:57:58 +08004488 /*
4489 * Do not pin the page in memory, so that memory hot-unplug
4490 * is able to migrate it.
4491 */
4492 put_page(page);
4493 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004494out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004495 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004496 return r;
4497}
4498
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004499static int alloc_identity_pagetable(struct kvm *kvm)
4500{
Tang Chena255d472014-09-16 18:41:58 +08004501 /* Called with kvm->slots_lock held. */
4502
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004503 int r = 0;
4504
Tang Chena255d472014-09-16 18:41:58 +08004505 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4506
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004507 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4508 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004509
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004510 return r;
4511}
4512
Wanpeng Li991e7a02015-09-16 17:30:05 +08004513static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004514{
4515 int vpid;
4516
Avi Kivity919818a2009-03-23 18:01:29 +02004517 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004518 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004519 spin_lock(&vmx_vpid_lock);
4520 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004521 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004522 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004523 else
4524 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004525 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004526 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004527}
4528
Wanpeng Li991e7a02015-09-16 17:30:05 +08004529static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004530{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004531 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004532 return;
4533 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004534 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004535 spin_unlock(&vmx_vpid_lock);
4536}
4537
Yang Zhang8d146952013-01-25 10:18:50 +08004538#define MSR_TYPE_R 1
4539#define MSR_TYPE_W 2
4540static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4541 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004542{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004543 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004544
4545 if (!cpu_has_vmx_msr_bitmap())
4546 return;
4547
4548 /*
4549 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4550 * have the write-low and read-high bitmap offsets the wrong way round.
4551 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4552 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004553 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004554 if (type & MSR_TYPE_R)
4555 /* read-low */
4556 __clear_bit(msr, msr_bitmap + 0x000 / f);
4557
4558 if (type & MSR_TYPE_W)
4559 /* write-low */
4560 __clear_bit(msr, msr_bitmap + 0x800 / f);
4561
Sheng Yang25c5f222008-03-28 13:18:56 +08004562 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4563 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004564 if (type & MSR_TYPE_R)
4565 /* read-high */
4566 __clear_bit(msr, msr_bitmap + 0x400 / f);
4567
4568 if (type & MSR_TYPE_W)
4569 /* write-high */
4570 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4571
4572 }
4573}
4574
4575static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4576 u32 msr, int type)
4577{
4578 int f = sizeof(unsigned long);
4579
4580 if (!cpu_has_vmx_msr_bitmap())
4581 return;
4582
4583 /*
4584 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4585 * have the write-low and read-high bitmap offsets the wrong way round.
4586 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4587 */
4588 if (msr <= 0x1fff) {
4589 if (type & MSR_TYPE_R)
4590 /* read-low */
4591 __set_bit(msr, msr_bitmap + 0x000 / f);
4592
4593 if (type & MSR_TYPE_W)
4594 /* write-low */
4595 __set_bit(msr, msr_bitmap + 0x800 / f);
4596
4597 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4598 msr &= 0x1fff;
4599 if (type & MSR_TYPE_R)
4600 /* read-high */
4601 __set_bit(msr, msr_bitmap + 0x400 / f);
4602
4603 if (type & MSR_TYPE_W)
4604 /* write-high */
4605 __set_bit(msr, msr_bitmap + 0xc00 / f);
4606
Sheng Yang25c5f222008-03-28 13:18:56 +08004607 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004608}
4609
Wincy Vanf2b93282015-02-03 23:56:03 +08004610/*
4611 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4612 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4613 */
4614static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4615 unsigned long *msr_bitmap_nested,
4616 u32 msr, int type)
4617{
4618 int f = sizeof(unsigned long);
4619
4620 if (!cpu_has_vmx_msr_bitmap()) {
4621 WARN_ON(1);
4622 return;
4623 }
4624
4625 /*
4626 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4627 * have the write-low and read-high bitmap offsets the wrong way round.
4628 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4629 */
4630 if (msr <= 0x1fff) {
4631 if (type & MSR_TYPE_R &&
4632 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4633 /* read-low */
4634 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4635
4636 if (type & MSR_TYPE_W &&
4637 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4638 /* write-low */
4639 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4640
4641 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4642 msr &= 0x1fff;
4643 if (type & MSR_TYPE_R &&
4644 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4645 /* read-high */
4646 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4647
4648 if (type & MSR_TYPE_W &&
4649 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4650 /* write-high */
4651 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4652
4653 }
4654}
4655
Avi Kivity58972972009-02-24 22:26:47 +02004656static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4657{
4658 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004659 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4660 msr, MSR_TYPE_R | MSR_TYPE_W);
4661 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4662 msr, MSR_TYPE_R | MSR_TYPE_W);
4663}
4664
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004665static void vmx_enable_intercept_msr_read_x2apic(u32 msr, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004666{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004667 if (apicv_active) {
4668 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4669 msr, MSR_TYPE_R);
4670 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4671 msr, MSR_TYPE_R);
4672 } else {
4673 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive,
4674 msr, MSR_TYPE_R);
4675 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive,
4676 msr, MSR_TYPE_R);
4677 }
Yang Zhang8d146952013-01-25 10:18:50 +08004678}
4679
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004680static void vmx_disable_intercept_msr_read_x2apic(u32 msr, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004681{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004682 if (apicv_active) {
4683 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4684 msr, MSR_TYPE_R);
4685 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4686 msr, MSR_TYPE_R);
4687 } else {
4688 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive,
4689 msr, MSR_TYPE_R);
4690 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive,
4691 msr, MSR_TYPE_R);
4692 }
Yang Zhang8d146952013-01-25 10:18:50 +08004693}
4694
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004695static void vmx_disable_intercept_msr_write_x2apic(u32 msr, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004696{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004697 if (apicv_active) {
4698 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4699 msr, MSR_TYPE_W);
4700 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4701 msr, MSR_TYPE_W);
4702 } else {
4703 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive,
4704 msr, MSR_TYPE_W);
4705 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive,
4706 msr, MSR_TYPE_W);
4707 }
Avi Kivity58972972009-02-24 22:26:47 +02004708}
4709
Andrey Smetanind62caab2015-11-10 15:36:33 +03004710static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004711{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004712 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004713}
4714
Wincy Van705699a2015-02-03 23:58:17 +08004715static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
4716{
4717 struct vcpu_vmx *vmx = to_vmx(vcpu);
4718 int max_irr;
4719 void *vapic_page;
4720 u16 status;
4721
4722 if (vmx->nested.pi_desc &&
4723 vmx->nested.pi_pending) {
4724 vmx->nested.pi_pending = false;
4725 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4726 return 0;
4727
4728 max_irr = find_last_bit(
4729 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4730
4731 if (max_irr == 256)
4732 return 0;
4733
4734 vapic_page = kmap(vmx->nested.virtual_apic_page);
4735 if (!vapic_page) {
4736 WARN_ON(1);
4737 return -ENOMEM;
4738 }
4739 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4740 kunmap(vmx->nested.virtual_apic_page);
4741
4742 status = vmcs_read16(GUEST_INTR_STATUS);
4743 if ((u8)max_irr > ((u8)status & 0xff)) {
4744 status &= ~0xff;
4745 status |= (u8)max_irr;
4746 vmcs_write16(GUEST_INTR_STATUS, status);
4747 }
4748 }
4749 return 0;
4750}
4751
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004752static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4753{
4754#ifdef CONFIG_SMP
4755 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004756 struct vcpu_vmx *vmx = to_vmx(vcpu);
4757
4758 /*
4759 * Currently, we don't support urgent interrupt,
4760 * all interrupts are recognized as non-urgent
4761 * interrupt, so we cannot post interrupts when
4762 * 'SN' is set.
4763 *
4764 * If the vcpu is in guest mode, it means it is
4765 * running instead of being scheduled out and
4766 * waiting in the run queue, and that's the only
4767 * case when 'SN' is set currently, warning if
4768 * 'SN' is set.
4769 */
4770 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
4771
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004772 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4773 POSTED_INTR_VECTOR);
4774 return true;
4775 }
4776#endif
4777 return false;
4778}
4779
Wincy Van705699a2015-02-03 23:58:17 +08004780static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4781 int vector)
4782{
4783 struct vcpu_vmx *vmx = to_vmx(vcpu);
4784
4785 if (is_guest_mode(vcpu) &&
4786 vector == vmx->nested.posted_intr_nv) {
4787 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004788 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004789 /*
4790 * If a posted intr is not recognized by hardware,
4791 * we will accomplish it in the next vmentry.
4792 */
4793 vmx->nested.pi_pending = true;
4794 kvm_make_request(KVM_REQ_EVENT, vcpu);
4795 return 0;
4796 }
4797 return -1;
4798}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004800 * Send interrupt to vcpu via posted interrupt way.
4801 * 1. If target vcpu is running(non-root mode), send posted interrupt
4802 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4803 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4804 * interrupt from PIR in next vmentry.
4805 */
4806static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4807{
4808 struct vcpu_vmx *vmx = to_vmx(vcpu);
4809 int r;
4810
Wincy Van705699a2015-02-03 23:58:17 +08004811 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4812 if (!r)
4813 return;
4814
Yang Zhanga20ed542013-04-11 19:25:15 +08004815 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4816 return;
4817
4818 r = pi_test_and_set_on(&vmx->pi_desc);
4819 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004820 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08004821 kvm_vcpu_kick(vcpu);
4822}
4823
4824static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4825{
4826 struct vcpu_vmx *vmx = to_vmx(vcpu);
4827
4828 if (!pi_test_and_clear_on(&vmx->pi_desc))
4829 return;
4830
4831 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4832}
4833
Avi Kivity6aa8b732006-12-10 02:21:36 -08004834/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004835 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4836 * will not change in the lifetime of the guest.
4837 * Note that host-state that does change is set elsewhere. E.g., host-state
4838 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4839 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004840static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004841{
4842 u32 low32, high32;
4843 unsigned long tmpl;
4844 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004845 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004846
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004847 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004848 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4849
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004850 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004851 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004852 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4853 vmx->host_state.vmcs_host_cr4 = cr4;
4854
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004855 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004856#ifdef CONFIG_X86_64
4857 /*
4858 * Load null selectors, so we can avoid reloading them in
4859 * __vmx_load_host_state(), in case userspace uses the null selectors
4860 * too (the expected case).
4861 */
4862 vmcs_write16(HOST_DS_SELECTOR, 0);
4863 vmcs_write16(HOST_ES_SELECTOR, 0);
4864#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004865 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4866 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004867#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004868 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4869 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4870
4871 native_store_idt(&dt);
4872 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004873 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004874
Avi Kivity83287ea422012-09-16 15:10:57 +03004875 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004876
4877 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4878 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4879 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4880 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4881
4882 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4883 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4884 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4885 }
4886}
4887
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004888static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4889{
4890 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4891 if (enable_ept)
4892 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004893 if (is_guest_mode(&vmx->vcpu))
4894 vmx->vcpu.arch.cr4_guest_owned_bits &=
4895 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004896 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4897}
4898
Yang Zhang01e439b2013-04-11 19:25:12 +08004899static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4900{
4901 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4902
Andrey Smetanind62caab2015-11-10 15:36:33 +03004903 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004904 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07004905 /* Enable the preemption timer dynamically */
4906 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004907 return pin_based_exec_ctrl;
4908}
4909
Andrey Smetanind62caab2015-11-10 15:36:33 +03004910static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4911{
4912 struct vcpu_vmx *vmx = to_vmx(vcpu);
4913
4914 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004915 if (cpu_has_secondary_exec_ctrls()) {
4916 if (kvm_vcpu_apicv_active(vcpu))
4917 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4918 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4919 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4920 else
4921 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4922 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4923 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4924 }
4925
4926 if (cpu_has_vmx_msr_bitmap())
4927 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004928}
4929
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004930static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4931{
4932 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004933
4934 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4935 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4936
Paolo Bonzini35754c92015-07-29 12:05:37 +02004937 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004938 exec_control &= ~CPU_BASED_TPR_SHADOW;
4939#ifdef CONFIG_X86_64
4940 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4941 CPU_BASED_CR8_LOAD_EXITING;
4942#endif
4943 }
4944 if (!enable_ept)
4945 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4946 CPU_BASED_CR3_LOAD_EXITING |
4947 CPU_BASED_INVLPG_EXITING;
4948 return exec_control;
4949}
4950
4951static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4952{
4953 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004954 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004955 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4956 if (vmx->vpid == 0)
4957 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4958 if (!enable_ept) {
4959 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4960 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004961 /* Enable INVPCID for non-ept guests may cause performance regression. */
4962 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004963 }
4964 if (!enable_unrestricted_guest)
4965 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4966 if (!ple_gap)
4967 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03004968 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004969 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4970 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004971 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004972 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4973 (handle_vmptrld).
4974 We can NOT enable shadow_vmcs here because we don't have yet
4975 a current VMCS12
4976 */
4977 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004978
4979 if (!enable_pml)
4980 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004981
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004982 return exec_control;
4983}
4984
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004985static void ept_set_mmio_spte_mask(void)
4986{
4987 /*
4988 * EPT Misconfigurations can be generated if the value of bits 2:0
4989 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004990 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004991 * spte.
4992 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004993 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004994}
4995
Wanpeng Lif53cd632014-12-02 19:14:58 +08004996#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004997/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998 * Sets up the vmcs for emulated real mode.
4999 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005000static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005002#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005003 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005004#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005005 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005008 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5009 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005010
Abel Gordon4607c2d2013-04-18 14:35:55 +03005011 if (enable_shadow_vmcs) {
5012 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5013 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5014 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005015 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005016 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005017
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5019
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005021 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005022 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005023
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005024 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005025
Dan Williamsdfa169b2016-06-02 11:17:24 -07005026 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005027 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5028 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005029 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005030
Andrey Smetanind62caab2015-11-10 15:36:33 +03005031 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005032 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5033 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5034 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5035 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5036
5037 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005038
Li RongQing0bcf2612015-12-03 13:29:34 +08005039 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005040 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005041 }
5042
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005043 if (ple_gap) {
5044 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005045 vmx->ple_window = ple_window;
5046 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005047 }
5048
Xiao Guangrongc3707952011-07-12 03:28:04 +08005049 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5050 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5052
Avi Kivity9581d442010-10-19 16:46:55 +02005053 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5054 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005055 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005056#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005057 rdmsrl(MSR_FS_BASE, a);
5058 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5059 rdmsrl(MSR_GS_BASE, a);
5060 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5061#else
5062 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5063 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5064#endif
5065
Eddie Dong2cc51562007-05-21 07:28:09 +03005066 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5067 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005068 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005069 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005070 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005071
Radim Krčmář74545702015-04-27 15:11:25 +02005072 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5073 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005074
Paolo Bonzini03916db2014-07-24 14:21:57 +02005075 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005076 u32 index = vmx_msr_index[i];
5077 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005078 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079
5080 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5081 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005082 if (wrmsr_safe(index, data_low, data_high) < 0)
5083 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005084 vmx->guest_msrs[j].index = i;
5085 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005086 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005087 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005088 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005089
Gleb Natapov2961e8762013-11-25 15:37:13 +02005090
5091 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005092
5093 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005094 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005095
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005096 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005097 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005098
Wanpeng Lif53cd632014-12-02 19:14:58 +08005099 if (vmx_xsaves_supported())
5100 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5101
Peter Feiner4e595162016-07-07 14:49:58 -07005102 if (enable_pml) {
5103 ASSERT(vmx->pml_pg);
5104 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5105 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5106 }
5107
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005108 return 0;
5109}
5110
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005111static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005112{
5113 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005114 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005115 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005116
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005117 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005118
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005119 vmx->soft_vnmi_blocked = 0;
5120
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005121 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005122 kvm_set_cr8(vcpu, 0);
5123
5124 if (!init_event) {
5125 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5126 MSR_IA32_APICBASE_ENABLE;
5127 if (kvm_vcpu_is_reset_bsp(vcpu))
5128 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5129 apic_base_msr.host_initiated = true;
5130 kvm_set_apic_base(vcpu, &apic_base_msr);
5131 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005132
Avi Kivity2fb92db2011-04-27 19:42:18 +03005133 vmx_segment_cache_clear(vmx);
5134
Avi Kivity5706be02008-08-20 15:07:31 +03005135 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005136 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005137 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005138
5139 seg_setup(VCPU_SREG_DS);
5140 seg_setup(VCPU_SREG_ES);
5141 seg_setup(VCPU_SREG_FS);
5142 seg_setup(VCPU_SREG_GS);
5143 seg_setup(VCPU_SREG_SS);
5144
5145 vmcs_write16(GUEST_TR_SELECTOR, 0);
5146 vmcs_writel(GUEST_TR_BASE, 0);
5147 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5148 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5149
5150 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5151 vmcs_writel(GUEST_LDTR_BASE, 0);
5152 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5153 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5154
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005155 if (!init_event) {
5156 vmcs_write32(GUEST_SYSENTER_CS, 0);
5157 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5158 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5159 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5160 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005161
5162 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005163 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005164
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005165 vmcs_writel(GUEST_GDTR_BASE, 0);
5166 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5167
5168 vmcs_writel(GUEST_IDTR_BASE, 0);
5169 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5170
Anthony Liguori443381a2010-12-06 10:53:38 -06005171 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005172 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005173 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005174
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005175 setup_msrs(vmx);
5176
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5178
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005179 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005180 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005181 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005182 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005183 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005184 vmcs_write32(TPR_THRESHOLD, 0);
5185 }
5186
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005187 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005188
Andrey Smetanind62caab2015-11-10 15:36:33 +03005189 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005190 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5191
Sheng Yang2384d2b2008-01-17 15:14:33 +08005192 if (vmx->vpid != 0)
5193 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5194
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005195 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005196 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005197 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005198 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005199 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005200 vmx_fpu_activate(vcpu);
5201 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005202
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005203 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005204}
5205
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005206/*
5207 * In nested virtualization, check if L1 asked to exit on external interrupts.
5208 * For most existing hypervisors, this will always return true.
5209 */
5210static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5211{
5212 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5213 PIN_BASED_EXT_INTR_MASK;
5214}
5215
Bandan Das77b0f5d2014-04-19 18:17:45 -04005216/*
5217 * In nested virtualization, check if L1 has set
5218 * VM_EXIT_ACK_INTR_ON_EXIT
5219 */
5220static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5221{
5222 return get_vmcs12(vcpu)->vm_exit_controls &
5223 VM_EXIT_ACK_INTR_ON_EXIT;
5224}
5225
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005226static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5227{
5228 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5229 PIN_BASED_NMI_EXITING;
5230}
5231
Jan Kiszkac9a79532014-03-07 20:03:15 +01005232static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005233{
5234 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005235
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005236 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5237 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5238 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5239}
5240
Jan Kiszkac9a79532014-03-07 20:03:15 +01005241static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005242{
5243 u32 cpu_based_vm_exec_control;
5244
Jan Kiszkac9a79532014-03-07 20:03:15 +01005245 if (!cpu_has_virtual_nmis() ||
5246 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5247 enable_irq_window(vcpu);
5248 return;
5249 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005250
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005251 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5252 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5253 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5254}
5255
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005256static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005257{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005258 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005259 uint32_t intr;
5260 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005261
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005262 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005263
Avi Kivityfa89a812008-09-01 15:57:51 +03005264 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005265 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005266 int inc_eip = 0;
5267 if (vcpu->arch.interrupt.soft)
5268 inc_eip = vcpu->arch.event_exit_inst_len;
5269 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005270 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005271 return;
5272 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005273 intr = irq | INTR_INFO_VALID_MASK;
5274 if (vcpu->arch.interrupt.soft) {
5275 intr |= INTR_TYPE_SOFT_INTR;
5276 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5277 vmx->vcpu.arch.event_exit_inst_len);
5278 } else
5279 intr |= INTR_TYPE_EXT_INTR;
5280 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005281}
5282
Sheng Yangf08864b2008-05-15 18:23:25 +08005283static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5284{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005285 struct vcpu_vmx *vmx = to_vmx(vcpu);
5286
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005287 if (!is_guest_mode(vcpu)) {
5288 if (!cpu_has_virtual_nmis()) {
5289 /*
5290 * Tracking the NMI-blocked state in software is built upon
5291 * finding the next open IRQ window. This, in turn, depends on
5292 * well-behaving guests: They have to keep IRQs disabled at
5293 * least as long as the NMI handler runs. Otherwise we may
5294 * cause NMI nesting, maybe breaking the guest. But as this is
5295 * highly unlikely, we can live with the residual risk.
5296 */
5297 vmx->soft_vnmi_blocked = 1;
5298 vmx->vnmi_blocked_time = 0;
5299 }
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005300
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005301 ++vcpu->stat.nmi_injections;
5302 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005303 }
5304
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005305 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005306 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005307 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005308 return;
5309 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005310
Sheng Yangf08864b2008-05-15 18:23:25 +08005311 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5312 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005313}
5314
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005315static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5316{
5317 if (!cpu_has_virtual_nmis())
5318 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005319 if (to_vmx(vcpu)->nmi_known_unmasked)
5320 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005321 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005322}
5323
5324static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5325{
5326 struct vcpu_vmx *vmx = to_vmx(vcpu);
5327
5328 if (!cpu_has_virtual_nmis()) {
5329 if (vmx->soft_vnmi_blocked != masked) {
5330 vmx->soft_vnmi_blocked = masked;
5331 vmx->vnmi_blocked_time = 0;
5332 }
5333 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005334 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005335 if (masked)
5336 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5337 GUEST_INTR_STATE_NMI);
5338 else
5339 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5340 GUEST_INTR_STATE_NMI);
5341 }
5342}
5343
Jan Kiszka2505dc92013-04-14 12:12:47 +02005344static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5345{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005346 if (to_vmx(vcpu)->nested.nested_run_pending)
5347 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005348
Jan Kiszka2505dc92013-04-14 12:12:47 +02005349 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5350 return 0;
5351
5352 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5353 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5354 | GUEST_INTR_STATE_NMI));
5355}
5356
Gleb Natapov78646122009-03-23 12:12:11 +02005357static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5358{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005359 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5360 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005361 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5362 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005363}
5364
Izik Eiduscbc94022007-10-25 00:29:55 +02005365static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5366{
5367 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005368
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005369 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5370 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005371 if (ret)
5372 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005373 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005374 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005375}
5376
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005377static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005378{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005379 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005380 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005381 /*
5382 * Update instruction length as we may reinject the exception
5383 * from user space while in guest debugging mode.
5384 */
5385 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5386 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005387 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005388 return false;
5389 /* fall through */
5390 case DB_VECTOR:
5391 if (vcpu->guest_debug &
5392 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5393 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005394 /* fall through */
5395 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005396 case OF_VECTOR:
5397 case BR_VECTOR:
5398 case UD_VECTOR:
5399 case DF_VECTOR:
5400 case SS_VECTOR:
5401 case GP_VECTOR:
5402 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005403 return true;
5404 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005405 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005406 return false;
5407}
5408
5409static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5410 int vec, u32 err_code)
5411{
5412 /*
5413 * Instruction with address size override prefix opcode 0x67
5414 * Cause the #SS fault with 0 error code in VM86 mode.
5415 */
5416 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5417 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5418 if (vcpu->arch.halt_request) {
5419 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005420 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005421 }
5422 return 1;
5423 }
5424 return 0;
5425 }
5426
5427 /*
5428 * Forward all other exceptions that are valid in real mode.
5429 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5430 * the required debugging infrastructure rework.
5431 */
5432 kvm_queue_exception(vcpu, vec);
5433 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005434}
5435
Andi Kleena0861c02009-06-08 17:37:09 +08005436/*
5437 * Trigger machine check on the host. We assume all the MSRs are already set up
5438 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5439 * We pass a fake environment to the machine check handler because we want
5440 * the guest to be always treated like user space, no matter what context
5441 * it used internally.
5442 */
5443static void kvm_machine_check(void)
5444{
5445#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5446 struct pt_regs regs = {
5447 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5448 .flags = X86_EFLAGS_IF,
5449 };
5450
5451 do_machine_check(&regs, 0);
5452#endif
5453}
5454
Avi Kivity851ba692009-08-24 11:10:17 +03005455static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005456{
5457 /* already handled by vcpu_run */
5458 return 1;
5459}
5460
Avi Kivity851ba692009-08-24 11:10:17 +03005461static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005462{
Avi Kivity1155f762007-11-22 11:30:47 +02005463 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005464 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005465 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005466 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005467 u32 vect_info;
5468 enum emulation_result er;
5469
Avi Kivity1155f762007-11-22 11:30:47 +02005470 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005471 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005472
Andi Kleena0861c02009-06-08 17:37:09 +08005473 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005474 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005475
Jan Kiszkae4a41882008-09-26 09:30:46 +02005476 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02005477 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005478
5479 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005480 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005481 return 1;
5482 }
5483
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005484 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005485 if (is_guest_mode(vcpu)) {
5486 kvm_queue_exception(vcpu, UD_VECTOR);
5487 return 1;
5488 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005489 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005490 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005491 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005492 return 1;
5493 }
5494
Avi Kivity6aa8b732006-12-10 02:21:36 -08005495 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005496 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005497 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005498
5499 /*
5500 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5501 * MMIO, it is better to report an internal error.
5502 * See the comments in vmx_handle_exit.
5503 */
5504 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5505 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5506 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5507 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005508 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005509 vcpu->run->internal.data[0] = vect_info;
5510 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005511 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005512 return 0;
5513 }
5514
Avi Kivity6aa8b732006-12-10 02:21:36 -08005515 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005516 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005517 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005518 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005519 trace_kvm_page_fault(cr2, error_code);
5520
Gleb Natapov3298b752009-05-11 13:35:46 +03005521 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005522 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005523 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005524 }
5525
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005526 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005527
5528 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5529 return handle_rmode_exception(vcpu, ex_no, error_code);
5530
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005531 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005532 case AC_VECTOR:
5533 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5534 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005535 case DB_VECTOR:
5536 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5537 if (!(vcpu->guest_debug &
5538 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005539 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005540 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005541 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5542 skip_emulated_instruction(vcpu);
5543
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005544 kvm_queue_exception(vcpu, DB_VECTOR);
5545 return 1;
5546 }
5547 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5548 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5549 /* fall through */
5550 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005551 /*
5552 * Update instruction length as we may reinject #BP from
5553 * user space while in guest debugging mode. Reading it for
5554 * #DB as well causes no harm, it is not used in that case.
5555 */
5556 vmx->vcpu.arch.event_exit_inst_len =
5557 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005559 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005560 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5561 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005562 break;
5563 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005564 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5565 kvm_run->ex.exception = ex_no;
5566 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005567 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005568 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005569 return 0;
5570}
5571
Avi Kivity851ba692009-08-24 11:10:17 +03005572static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005574 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005575 return 1;
5576}
5577
Avi Kivity851ba692009-08-24 11:10:17 +03005578static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005579{
Avi Kivity851ba692009-08-24 11:10:17 +03005580 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005581 return 0;
5582}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005583
Avi Kivity851ba692009-08-24 11:10:17 +03005584static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005585{
He, Qingbfdaab02007-09-12 14:18:28 +08005586 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005587 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005588 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005589
He, Qingbfdaab02007-09-12 14:18:28 +08005590 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005591 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005592 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005593
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005594 ++vcpu->stat.io_exits;
5595
5596 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005597 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005598
5599 port = exit_qualification >> 16;
5600 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005601 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005602
5603 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005604}
5605
Ingo Molnar102d8322007-02-19 14:37:47 +02005606static void
5607vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5608{
5609 /*
5610 * Patch in the VMCALL instruction:
5611 */
5612 hypercall[0] = 0x0f;
5613 hypercall[1] = 0x01;
5614 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005615}
5616
Wincy Vanb9c237b2015-02-03 23:56:30 +08005617static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005618{
5619 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005620 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005621
Wincy Vanb9c237b2015-02-03 23:56:30 +08005622 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005623 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5624 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5625 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5626 return (val & always_on) == always_on;
5627}
5628
Guo Chao0fa06072012-06-28 15:16:19 +08005629/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005630static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5631{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005632 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005633 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5634 unsigned long orig_val = val;
5635
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005636 /*
5637 * We get here when L2 changed cr0 in a way that did not change
5638 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005639 * but did change L0 shadowed bits. So we first calculate the
5640 * effective cr0 value that L1 would like to write into the
5641 * hardware. It consists of the L2-owned bits from the new
5642 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005643 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005644 val = (val & ~vmcs12->cr0_guest_host_mask) |
5645 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5646
Wincy Vanb9c237b2015-02-03 23:56:30 +08005647 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005648 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005649
5650 if (kvm_set_cr0(vcpu, val))
5651 return 1;
5652 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005653 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005654 } else {
5655 if (to_vmx(vcpu)->nested.vmxon &&
5656 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5657 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005658 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005659 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005660}
5661
5662static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5663{
5664 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005665 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5666 unsigned long orig_val = val;
5667
5668 /* analogously to handle_set_cr0 */
5669 val = (val & ~vmcs12->cr4_guest_host_mask) |
5670 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5671 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005672 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005673 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005674 return 0;
5675 } else
5676 return kvm_set_cr4(vcpu, val);
5677}
5678
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005679/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005680static void handle_clts(struct kvm_vcpu *vcpu)
5681{
5682 if (is_guest_mode(vcpu)) {
5683 /*
5684 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5685 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5686 * just pretend it's off (also in arch.cr0 for fpu_activate).
5687 */
5688 vmcs_writel(CR0_READ_SHADOW,
5689 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5690 vcpu->arch.cr0 &= ~X86_CR0_TS;
5691 } else
5692 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5693}
5694
Avi Kivity851ba692009-08-24 11:10:17 +03005695static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005696{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005697 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005698 int cr;
5699 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005700 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005701
He, Qingbfdaab02007-09-12 14:18:28 +08005702 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005703 cr = exit_qualification & 15;
5704 reg = (exit_qualification >> 8) & 15;
5705 switch ((exit_qualification >> 4) & 3) {
5706 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005707 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005708 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005709 switch (cr) {
5710 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005711 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005712 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005713 return 1;
5714 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005715 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005716 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005717 return 1;
5718 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005719 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005720 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005721 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005722 case 8: {
5723 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005724 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005725 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005726 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005727 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005728 return 1;
5729 if (cr8_prev <= cr8)
5730 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005731 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005732 return 0;
5733 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005734 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005735 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005736 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005737 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005738 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005739 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005740 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005741 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005742 case 1: /*mov from cr*/
5743 switch (cr) {
5744 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005745 val = kvm_read_cr3(vcpu);
5746 kvm_register_write(vcpu, reg, val);
5747 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005748 skip_emulated_instruction(vcpu);
5749 return 1;
5750 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005751 val = kvm_get_cr8(vcpu);
5752 kvm_register_write(vcpu, reg, val);
5753 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005754 skip_emulated_instruction(vcpu);
5755 return 1;
5756 }
5757 break;
5758 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005759 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005760 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005761 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005762
5763 skip_emulated_instruction(vcpu);
5764 return 1;
5765 default:
5766 break;
5767 }
Avi Kivity851ba692009-08-24 11:10:17 +03005768 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005769 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005770 (int)(exit_qualification >> 4) & 3, cr);
5771 return 0;
5772}
5773
Avi Kivity851ba692009-08-24 11:10:17 +03005774static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005775{
He, Qingbfdaab02007-09-12 14:18:28 +08005776 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005777 int dr, dr7, reg;
5778
5779 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5780 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5781
5782 /* First, if DR does not exist, trigger UD */
5783 if (!kvm_require_dr(vcpu, dr))
5784 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005785
Jan Kiszkaf2483412010-01-20 18:20:20 +01005786 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005787 if (!kvm_require_cpl(vcpu, 0))
5788 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005789 dr7 = vmcs_readl(GUEST_DR7);
5790 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005791 /*
5792 * As the vm-exit takes precedence over the debug trap, we
5793 * need to emulate the latter, either for the host or the
5794 * guest debugging itself.
5795 */
5796 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005797 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005798 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005799 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005800 vcpu->run->debug.arch.exception = DB_VECTOR;
5801 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005802 return 0;
5803 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005804 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005805 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005806 kvm_queue_exception(vcpu, DB_VECTOR);
5807 return 1;
5808 }
5809 }
5810
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005811 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01005812 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5813 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005814
5815 /*
5816 * No more DR vmexits; force a reload of the debug registers
5817 * and reenter on this instruction. The next vmexit will
5818 * retrieve the full state of the debug registers.
5819 */
5820 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5821 return 1;
5822 }
5823
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005824 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5825 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005826 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005827
5828 if (kvm_get_dr(vcpu, dr, &val))
5829 return 1;
5830 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005831 } else
Nadav Amit57773922014-06-18 17:19:23 +03005832 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005833 return 1;
5834
Avi Kivity6aa8b732006-12-10 02:21:36 -08005835 skip_emulated_instruction(vcpu);
5836 return 1;
5837}
5838
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005839static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5840{
5841 return vcpu->arch.dr6;
5842}
5843
5844static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5845{
5846}
5847
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005848static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5849{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005850 get_debugreg(vcpu->arch.db[0], 0);
5851 get_debugreg(vcpu->arch.db[1], 1);
5852 get_debugreg(vcpu->arch.db[2], 2);
5853 get_debugreg(vcpu->arch.db[3], 3);
5854 get_debugreg(vcpu->arch.dr6, 6);
5855 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5856
5857 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01005858 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005859}
5860
Gleb Natapov020df072010-04-13 10:05:23 +03005861static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5862{
5863 vmcs_writel(GUEST_DR7, val);
5864}
5865
Avi Kivity851ba692009-08-24 11:10:17 +03005866static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005867{
Avi Kivity06465c52007-02-28 20:46:53 +02005868 kvm_emulate_cpuid(vcpu);
5869 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005870}
5871
Avi Kivity851ba692009-08-24 11:10:17 +03005872static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005873{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005874 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005875 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005876
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005877 msr_info.index = ecx;
5878 msr_info.host_initiated = false;
5879 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005880 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005881 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005882 return 1;
5883 }
5884
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005885 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005886
Avi Kivity6aa8b732006-12-10 02:21:36 -08005887 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005888 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5889 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005890 skip_emulated_instruction(vcpu);
5891 return 1;
5892}
5893
Avi Kivity851ba692009-08-24 11:10:17 +03005894static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005895{
Will Auld8fe8ab42012-11-29 12:42:12 -08005896 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005897 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5898 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5899 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005900
Will Auld8fe8ab42012-11-29 12:42:12 -08005901 msr.data = data;
5902 msr.index = ecx;
5903 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005904 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005905 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005906 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005907 return 1;
5908 }
5909
Avi Kivity59200272010-01-25 19:47:02 +02005910 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005911 skip_emulated_instruction(vcpu);
5912 return 1;
5913}
5914
Avi Kivity851ba692009-08-24 11:10:17 +03005915static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005916{
Avi Kivity3842d132010-07-27 12:30:24 +03005917 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005918 return 1;
5919}
5920
Avi Kivity851ba692009-08-24 11:10:17 +03005921static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005922{
Eddie Dong85f455f2007-07-06 12:20:49 +03005923 u32 cpu_based_vm_exec_control;
5924
5925 /* clear pending irq */
5926 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5927 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5928 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005929
Avi Kivity3842d132010-07-27 12:30:24 +03005930 kvm_make_request(KVM_REQ_EVENT, vcpu);
5931
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005932 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005933 return 1;
5934}
5935
Avi Kivity851ba692009-08-24 11:10:17 +03005936static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005937{
Avi Kivityd3bef152007-06-05 15:53:05 +03005938 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005939}
5940
Avi Kivity851ba692009-08-24 11:10:17 +03005941static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005942{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005943 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005944}
5945
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005946static int handle_invd(struct kvm_vcpu *vcpu)
5947{
Andre Przywara51d8b662010-12-21 11:12:02 +01005948 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005949}
5950
Avi Kivity851ba692009-08-24 11:10:17 +03005951static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005952{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005953 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005954
5955 kvm_mmu_invlpg(vcpu, exit_qualification);
5956 skip_emulated_instruction(vcpu);
5957 return 1;
5958}
5959
Avi Kivityfee84b02011-11-10 14:57:25 +02005960static int handle_rdpmc(struct kvm_vcpu *vcpu)
5961{
5962 int err;
5963
5964 err = kvm_rdpmc(vcpu);
5965 kvm_complete_insn_gp(vcpu, err);
5966
5967 return 1;
5968}
5969
Avi Kivity851ba692009-08-24 11:10:17 +03005970static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005971{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005972 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005973 return 1;
5974}
5975
Dexuan Cui2acf9232010-06-10 11:27:12 +08005976static int handle_xsetbv(struct kvm_vcpu *vcpu)
5977{
5978 u64 new_bv = kvm_read_edx_eax(vcpu);
5979 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5980
5981 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5982 skip_emulated_instruction(vcpu);
5983 return 1;
5984}
5985
Wanpeng Lif53cd632014-12-02 19:14:58 +08005986static int handle_xsaves(struct kvm_vcpu *vcpu)
5987{
5988 skip_emulated_instruction(vcpu);
5989 WARN(1, "this should never happen\n");
5990 return 1;
5991}
5992
5993static int handle_xrstors(struct kvm_vcpu *vcpu)
5994{
5995 skip_emulated_instruction(vcpu);
5996 WARN(1, "this should never happen\n");
5997 return 1;
5998}
5999
Avi Kivity851ba692009-08-24 11:10:17 +03006000static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006001{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006002 if (likely(fasteoi)) {
6003 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6004 int access_type, offset;
6005
6006 access_type = exit_qualification & APIC_ACCESS_TYPE;
6007 offset = exit_qualification & APIC_ACCESS_OFFSET;
6008 /*
6009 * Sane guest uses MOV to write EOI, with written value
6010 * not cared. So make a short-circuit here by avoiding
6011 * heavy instruction emulation.
6012 */
6013 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6014 (offset == APIC_EOI)) {
6015 kvm_lapic_set_eoi(vcpu);
6016 skip_emulated_instruction(vcpu);
6017 return 1;
6018 }
6019 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006020 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006021}
6022
Yang Zhangc7c9c562013-01-25 10:18:51 +08006023static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6024{
6025 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6026 int vector = exit_qualification & 0xff;
6027
6028 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6029 kvm_apic_set_eoi_accelerated(vcpu, vector);
6030 return 1;
6031}
6032
Yang Zhang83d4c282013-01-25 10:18:49 +08006033static int handle_apic_write(struct kvm_vcpu *vcpu)
6034{
6035 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6036 u32 offset = exit_qualification & 0xfff;
6037
6038 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6039 kvm_apic_write_nodecode(vcpu, offset);
6040 return 1;
6041}
6042
Avi Kivity851ba692009-08-24 11:10:17 +03006043static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006044{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006045 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006046 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006047 bool has_error_code = false;
6048 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006049 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006050 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006051
6052 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006053 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006054 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006055
6056 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6057
6058 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006059 if (reason == TASK_SWITCH_GATE && idt_v) {
6060 switch (type) {
6061 case INTR_TYPE_NMI_INTR:
6062 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006063 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006064 break;
6065 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006066 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006067 kvm_clear_interrupt_queue(vcpu);
6068 break;
6069 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006070 if (vmx->idt_vectoring_info &
6071 VECTORING_INFO_DELIVER_CODE_MASK) {
6072 has_error_code = true;
6073 error_code =
6074 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6075 }
6076 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006077 case INTR_TYPE_SOFT_EXCEPTION:
6078 kvm_clear_exception_queue(vcpu);
6079 break;
6080 default:
6081 break;
6082 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006083 }
Izik Eidus37817f22008-03-24 23:14:53 +02006084 tss_selector = exit_qualification;
6085
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006086 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6087 type != INTR_TYPE_EXT_INTR &&
6088 type != INTR_TYPE_NMI_INTR))
6089 skip_emulated_instruction(vcpu);
6090
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006091 if (kvm_task_switch(vcpu, tss_selector,
6092 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6093 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006094 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6095 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6096 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006097 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006098 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006099
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006100 /*
6101 * TODO: What about debug traps on tss switch?
6102 * Are we supposed to inject them and update dr6?
6103 */
6104
6105 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006106}
6107
Avi Kivity851ba692009-08-24 11:10:17 +03006108static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006109{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006110 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006111 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006112 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006113 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006114
Sheng Yangf9c617f2009-03-25 10:08:52 +08006115 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006116
Sheng Yang14394422008-04-28 12:24:45 +08006117 gla_validity = (exit_qualification >> 7) & 0x3;
Liang Li72e0ae52016-08-18 15:49:19 +08006118 if (gla_validity == 0x2) {
Sheng Yang14394422008-04-28 12:24:45 +08006119 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6120 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6121 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006122 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006123 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6124 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006125 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6126 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006127 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006128 }
6129
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006130 /*
6131 * EPT violation happened while executing iret from NMI,
6132 * "blocked by NMI" bit has to be set before next VM entry.
6133 * There are errata that may cause this bit to not be set:
6134 * AAK134, BY25.
6135 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006136 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6137 cpu_has_virtual_nmis() &&
6138 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006139 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6140
Sheng Yang14394422008-04-28 12:24:45 +08006141 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006142 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006143
Bandan Dasd95c5562016-07-12 18:18:51 -04006144 /* it is a read fault? */
6145 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6146 /* it is a write fault? */
6147 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006148 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006149 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006150 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006151 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006152
Yang Zhang25d92082013-08-06 12:00:32 +03006153 vcpu->arch.exit_qualification = exit_qualification;
6154
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006155 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006156}
6157
Avi Kivity851ba692009-08-24 11:10:17 +03006158static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006159{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006160 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006161 gpa_t gpa;
6162
6163 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006164 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006165 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006166 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006167 return 1;
6168 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006169
Paolo Bonzini450869d2015-11-04 13:41:21 +01006170 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006171 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006172 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6173 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006174
6175 if (unlikely(ret == RET_MMIO_PF_INVALID))
6176 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6177
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006178 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006179 return 1;
6180
6181 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006182 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006183
Avi Kivity851ba692009-08-24 11:10:17 +03006184 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6185 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006186
6187 return 0;
6188}
6189
Avi Kivity851ba692009-08-24 11:10:17 +03006190static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006191{
6192 u32 cpu_based_vm_exec_control;
6193
6194 /* clear pending NMI */
6195 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6196 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6197 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6198 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006199 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006200
6201 return 1;
6202}
6203
Mohammed Gamal80ced182009-09-01 12:48:18 +02006204static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006205{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006206 struct vcpu_vmx *vmx = to_vmx(vcpu);
6207 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006208 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006209 u32 cpu_exec_ctrl;
6210 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006211 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006212
6213 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6214 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006215
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006216 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006217 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006218 return handle_interrupt_window(&vmx->vcpu);
6219
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006220 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6221 return 1;
6222
Gleb Natapov991eebf2013-04-11 12:10:51 +03006223 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006224
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006225 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006226 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006227 ret = 0;
6228 goto out;
6229 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006230
Avi Kivityde5f70e2012-06-12 20:22:28 +03006231 if (err != EMULATE_DONE) {
6232 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6233 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6234 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006235 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006236 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006237
Gleb Natapov8d76c492013-05-08 18:38:44 +03006238 if (vcpu->arch.halt_request) {
6239 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006240 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006241 goto out;
6242 }
6243
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006244 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006245 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006246 if (need_resched())
6247 schedule();
6248 }
6249
Mohammed Gamal80ced182009-09-01 12:48:18 +02006250out:
6251 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006252}
6253
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006254static int __grow_ple_window(int val)
6255{
6256 if (ple_window_grow < 1)
6257 return ple_window;
6258
6259 val = min(val, ple_window_actual_max);
6260
6261 if (ple_window_grow < ple_window)
6262 val *= ple_window_grow;
6263 else
6264 val += ple_window_grow;
6265
6266 return val;
6267}
6268
6269static int __shrink_ple_window(int val, int modifier, int minimum)
6270{
6271 if (modifier < 1)
6272 return ple_window;
6273
6274 if (modifier < ple_window)
6275 val /= modifier;
6276 else
6277 val -= modifier;
6278
6279 return max(val, minimum);
6280}
6281
6282static void grow_ple_window(struct kvm_vcpu *vcpu)
6283{
6284 struct vcpu_vmx *vmx = to_vmx(vcpu);
6285 int old = vmx->ple_window;
6286
6287 vmx->ple_window = __grow_ple_window(old);
6288
6289 if (vmx->ple_window != old)
6290 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006291
6292 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006293}
6294
6295static void shrink_ple_window(struct kvm_vcpu *vcpu)
6296{
6297 struct vcpu_vmx *vmx = to_vmx(vcpu);
6298 int old = vmx->ple_window;
6299
6300 vmx->ple_window = __shrink_ple_window(old,
6301 ple_window_shrink, ple_window);
6302
6303 if (vmx->ple_window != old)
6304 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006305
6306 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006307}
6308
6309/*
6310 * ple_window_actual_max is computed to be one grow_ple_window() below
6311 * ple_window_max. (See __grow_ple_window for the reason.)
6312 * This prevents overflows, because ple_window_max is int.
6313 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6314 * this process.
6315 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6316 */
6317static void update_ple_window_actual_max(void)
6318{
6319 ple_window_actual_max =
6320 __shrink_ple_window(max(ple_window_max, ple_window),
6321 ple_window_grow, INT_MIN);
6322}
6323
Feng Wubf9f6ac2015-09-18 22:29:55 +08006324/*
6325 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6326 */
6327static void wakeup_handler(void)
6328{
6329 struct kvm_vcpu *vcpu;
6330 int cpu = smp_processor_id();
6331
6332 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6333 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6334 blocked_vcpu_list) {
6335 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6336
6337 if (pi_test_on(pi_desc) == 1)
6338 kvm_vcpu_kick(vcpu);
6339 }
6340 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6341}
6342
Tiejun Chenf2c76482014-10-28 10:14:47 +08006343static __init int hardware_setup(void)
6344{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006345 int r = -ENOMEM, i, msr;
6346
6347 rdmsrl_safe(MSR_EFER, &host_efer);
6348
6349 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6350 kvm_define_shared_msr(i, vmx_msr_index[i]);
6351
6352 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6353 if (!vmx_io_bitmap_a)
6354 return r;
6355
6356 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6357 if (!vmx_io_bitmap_b)
6358 goto out;
6359
6360 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
6361 if (!vmx_msr_bitmap_legacy)
6362 goto out1;
6363
6364 vmx_msr_bitmap_legacy_x2apic =
6365 (unsigned long *)__get_free_page(GFP_KERNEL);
6366 if (!vmx_msr_bitmap_legacy_x2apic)
6367 goto out2;
6368
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006369 vmx_msr_bitmap_legacy_x2apic_apicv_inactive =
6370 (unsigned long *)__get_free_page(GFP_KERNEL);
6371 if (!vmx_msr_bitmap_legacy_x2apic_apicv_inactive)
6372 goto out3;
6373
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006374 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
6375 if (!vmx_msr_bitmap_longmode)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006376 goto out4;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006377
6378 vmx_msr_bitmap_longmode_x2apic =
6379 (unsigned long *)__get_free_page(GFP_KERNEL);
6380 if (!vmx_msr_bitmap_longmode_x2apic)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006381 goto out5;
6382
6383 vmx_msr_bitmap_longmode_x2apic_apicv_inactive =
6384 (unsigned long *)__get_free_page(GFP_KERNEL);
6385 if (!vmx_msr_bitmap_longmode_x2apic_apicv_inactive)
6386 goto out6;
Wincy Van3af18d92015-02-03 23:49:31 +08006387
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006388 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6389 if (!vmx_vmread_bitmap)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006390 goto out7;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006391
6392 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6393 if (!vmx_vmwrite_bitmap)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006394 goto out8;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006395
6396 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6397 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6398
6399 /*
6400 * Allow direct access to the PC debug port (it is often used for I/O
6401 * delays, but the vmexits simply slow things down).
6402 */
6403 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6404 clear_bit(0x80, vmx_io_bitmap_a);
6405
6406 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6407
6408 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6409 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6410
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006411 if (setup_vmcs_config(&vmcs_config) < 0) {
6412 r = -EIO;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006413 goto out9;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006414 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006415
6416 if (boot_cpu_has(X86_FEATURE_NX))
6417 kvm_enable_efer_bits(EFER_NX);
6418
6419 if (!cpu_has_vmx_vpid())
6420 enable_vpid = 0;
6421 if (!cpu_has_vmx_shadow_vmcs())
6422 enable_shadow_vmcs = 0;
6423 if (enable_shadow_vmcs)
6424 init_vmcs_shadow_fields();
6425
6426 if (!cpu_has_vmx_ept() ||
6427 !cpu_has_vmx_ept_4levels()) {
6428 enable_ept = 0;
6429 enable_unrestricted_guest = 0;
6430 enable_ept_ad_bits = 0;
6431 }
6432
6433 if (!cpu_has_vmx_ept_ad_bits())
6434 enable_ept_ad_bits = 0;
6435
6436 if (!cpu_has_vmx_unrestricted_guest())
6437 enable_unrestricted_guest = 0;
6438
Paolo Bonziniad15a292015-01-30 16:18:49 +01006439 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006440 flexpriority_enabled = 0;
6441
Paolo Bonziniad15a292015-01-30 16:18:49 +01006442 /*
6443 * set_apic_access_page_addr() is used to reload apic access
6444 * page upon invalidation. No need to do anything if not
6445 * using the APIC_ACCESS_ADDR VMCS field.
6446 */
6447 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006448 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006449
6450 if (!cpu_has_vmx_tpr_shadow())
6451 kvm_x86_ops->update_cr8_intercept = NULL;
6452
6453 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6454 kvm_disable_largepages();
6455
6456 if (!cpu_has_vmx_ple())
6457 ple_gap = 0;
6458
6459 if (!cpu_has_vmx_apicv())
6460 enable_apicv = 0;
6461
Haozhong Zhang64903d62015-10-20 15:39:09 +08006462 if (cpu_has_vmx_tsc_scaling()) {
6463 kvm_has_tsc_control = true;
6464 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6465 kvm_tsc_scaling_ratio_frac_bits = 48;
6466 }
6467
Tiejun Chenbaa03522014-12-23 16:21:11 +08006468 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6469 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6470 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6471 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6472 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6473 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6474 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6475
6476 memcpy(vmx_msr_bitmap_legacy_x2apic,
6477 vmx_msr_bitmap_legacy, PAGE_SIZE);
6478 memcpy(vmx_msr_bitmap_longmode_x2apic,
6479 vmx_msr_bitmap_longmode, PAGE_SIZE);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006480 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv_inactive,
6481 vmx_msr_bitmap_legacy, PAGE_SIZE);
6482 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv_inactive,
6483 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006484
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006485 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6486
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006487 /*
6488 * enable_apicv && kvm_vcpu_apicv_active()
6489 */
Roman Kagan3ce424e2016-05-18 17:48:20 +03006490 for (msr = 0x800; msr <= 0x8ff; msr++)
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006491 vmx_disable_intercept_msr_read_x2apic(msr, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006492
Roman Kagan3ce424e2016-05-18 17:48:20 +03006493 /* TMCCT */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006494 vmx_enable_intercept_msr_read_x2apic(0x839, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006495 /* TPR */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006496 vmx_disable_intercept_msr_write_x2apic(0x808, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006497 /* EOI */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006498 vmx_disable_intercept_msr_write_x2apic(0x80b, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006499 /* SELF-IPI */
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006500 vmx_disable_intercept_msr_write_x2apic(0x83f, true);
6501
6502 /*
6503 * (enable_apicv && !kvm_vcpu_apicv_active()) ||
6504 * !enable_apicv
6505 */
6506 /* TPR */
6507 vmx_disable_intercept_msr_read_x2apic(0x808, false);
6508 vmx_disable_intercept_msr_write_x2apic(0x808, false);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006509
6510 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006511 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006512 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6513 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006514 0ull, VMX_EPT_EXECUTABLE_MASK,
6515 cpu_has_vmx_ept_execute_only() ?
6516 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006517 ept_set_mmio_spte_mask();
6518 kvm_enable_tdp();
6519 } else
6520 kvm_disable_tdp();
6521
6522 update_ple_window_actual_max();
6523
Kai Huang843e4332015-01-28 10:54:28 +08006524 /*
6525 * Only enable PML when hardware supports PML feature, and both EPT
6526 * and EPT A/D bit features are enabled -- PML depends on them to work.
6527 */
6528 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6529 enable_pml = 0;
6530
6531 if (!enable_pml) {
6532 kvm_x86_ops->slot_enable_log_dirty = NULL;
6533 kvm_x86_ops->slot_disable_log_dirty = NULL;
6534 kvm_x86_ops->flush_log_dirty = NULL;
6535 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6536 }
6537
Yunhong Jiang64672c92016-06-13 14:19:59 -07006538 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6539 u64 vmx_msr;
6540
6541 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6542 cpu_preemption_timer_multi =
6543 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6544 } else {
6545 kvm_x86_ops->set_hv_timer = NULL;
6546 kvm_x86_ops->cancel_hv_timer = NULL;
6547 }
6548
Feng Wubf9f6ac2015-09-18 22:29:55 +08006549 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6550
Ashok Rajc45dcc72016-06-22 14:59:56 +08006551 kvm_mce_cap_supported |= MCG_LMCE_P;
6552
Tiejun Chenf2c76482014-10-28 10:14:47 +08006553 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006554
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006555out9:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006556 free_page((unsigned long)vmx_vmwrite_bitmap);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006557out8:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006558 free_page((unsigned long)vmx_vmread_bitmap);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006559out7:
6560 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv_inactive);
Wincy Van3af18d92015-02-03 23:49:31 +08006561out6:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006562 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006563out5:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006564 free_page((unsigned long)vmx_msr_bitmap_longmode);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006565out4:
6566 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv_inactive);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006567out3:
6568 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6569out2:
6570 free_page((unsigned long)vmx_msr_bitmap_legacy);
6571out1:
6572 free_page((unsigned long)vmx_io_bitmap_b);
6573out:
6574 free_page((unsigned long)vmx_io_bitmap_a);
6575
6576 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006577}
6578
6579static __exit void hardware_unsetup(void)
6580{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006581 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006582 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv_inactive);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006583 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006584 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv_inactive);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006585 free_page((unsigned long)vmx_msr_bitmap_legacy);
6586 free_page((unsigned long)vmx_msr_bitmap_longmode);
6587 free_page((unsigned long)vmx_io_bitmap_b);
6588 free_page((unsigned long)vmx_io_bitmap_a);
6589 free_page((unsigned long)vmx_vmwrite_bitmap);
6590 free_page((unsigned long)vmx_vmread_bitmap);
6591
Tiejun Chenf2c76482014-10-28 10:14:47 +08006592 free_kvm_area();
6593}
6594
Avi Kivity6aa8b732006-12-10 02:21:36 -08006595/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006596 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6597 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6598 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006599static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006600{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006601 if (ple_gap)
6602 grow_ple_window(vcpu);
6603
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006604 skip_emulated_instruction(vcpu);
6605 kvm_vcpu_on_spin(vcpu);
6606
6607 return 1;
6608}
6609
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006610static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006611{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006612 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006613 return 1;
6614}
6615
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006616static int handle_mwait(struct kvm_vcpu *vcpu)
6617{
6618 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6619 return handle_nop(vcpu);
6620}
6621
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006622static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6623{
6624 return 1;
6625}
6626
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006627static int handle_monitor(struct kvm_vcpu *vcpu)
6628{
6629 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6630 return handle_nop(vcpu);
6631}
6632
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006633/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006634 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6635 * We could reuse a single VMCS for all the L2 guests, but we also want the
6636 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6637 * allows keeping them loaded on the processor, and in the future will allow
6638 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6639 * every entry if they never change.
6640 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6641 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6642 *
6643 * The following functions allocate and free a vmcs02 in this pool.
6644 */
6645
6646/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6647static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6648{
6649 struct vmcs02_list *item;
6650 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6651 if (item->vmptr == vmx->nested.current_vmptr) {
6652 list_move(&item->list, &vmx->nested.vmcs02_pool);
6653 return &item->vmcs02;
6654 }
6655
6656 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6657 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08006658 item = list_last_entry(&vmx->nested.vmcs02_pool,
6659 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006660 item->vmptr = vmx->nested.current_vmptr;
6661 list_move(&item->list, &vmx->nested.vmcs02_pool);
6662 return &item->vmcs02;
6663 }
6664
6665 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006666 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006667 if (!item)
6668 return NULL;
6669 item->vmcs02.vmcs = alloc_vmcs();
6670 if (!item->vmcs02.vmcs) {
6671 kfree(item);
6672 return NULL;
6673 }
6674 loaded_vmcs_init(&item->vmcs02);
6675 item->vmptr = vmx->nested.current_vmptr;
6676 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6677 vmx->nested.vmcs02_num++;
6678 return &item->vmcs02;
6679}
6680
6681/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6682static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6683{
6684 struct vmcs02_list *item;
6685 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6686 if (item->vmptr == vmptr) {
6687 free_loaded_vmcs(&item->vmcs02);
6688 list_del(&item->list);
6689 kfree(item);
6690 vmx->nested.vmcs02_num--;
6691 return;
6692 }
6693}
6694
6695/*
6696 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006697 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6698 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006699 */
6700static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6701{
6702 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006703
6704 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006705 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006706 /*
6707 * Something will leak if the above WARN triggers. Better than
6708 * a use-after-free.
6709 */
6710 if (vmx->loaded_vmcs == &item->vmcs02)
6711 continue;
6712
6713 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006714 list_del(&item->list);
6715 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006716 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006717 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006718}
6719
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006720/*
6721 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6722 * set the success or error code of an emulated VMX instruction, as specified
6723 * by Vol 2B, VMX Instruction Reference, "Conventions".
6724 */
6725static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6726{
6727 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6728 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6729 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6730}
6731
6732static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6733{
6734 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6735 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6736 X86_EFLAGS_SF | X86_EFLAGS_OF))
6737 | X86_EFLAGS_CF);
6738}
6739
Abel Gordon145c28d2013-04-18 14:36:55 +03006740static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006741 u32 vm_instruction_error)
6742{
6743 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6744 /*
6745 * failValid writes the error number to the current VMCS, which
6746 * can't be done there isn't a current VMCS.
6747 */
6748 nested_vmx_failInvalid(vcpu);
6749 return;
6750 }
6751 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6752 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6753 X86_EFLAGS_SF | X86_EFLAGS_OF))
6754 | X86_EFLAGS_ZF);
6755 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6756 /*
6757 * We don't need to force a shadow sync because
6758 * VM_INSTRUCTION_ERROR is not shadowed
6759 */
6760}
Abel Gordon145c28d2013-04-18 14:36:55 +03006761
Wincy Vanff651cb2014-12-11 08:52:58 +03006762static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6763{
6764 /* TODO: not to reset guest simply here. */
6765 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006766 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006767}
6768
Jan Kiszkaf41245002014-03-07 20:03:13 +01006769static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6770{
6771 struct vcpu_vmx *vmx =
6772 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6773
6774 vmx->nested.preemption_timer_expired = true;
6775 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6776 kvm_vcpu_kick(&vmx->vcpu);
6777
6778 return HRTIMER_NORESTART;
6779}
6780
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006781/*
Bandan Das19677e32014-05-06 02:19:15 -04006782 * Decode the memory-address operand of a vmx instruction, as recorded on an
6783 * exit caused by such an instruction (run by a guest hypervisor).
6784 * On success, returns 0. When the operand is invalid, returns 1 and throws
6785 * #UD or #GP.
6786 */
6787static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6788 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006789 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006790{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006791 gva_t off;
6792 bool exn;
6793 struct kvm_segment s;
6794
Bandan Das19677e32014-05-06 02:19:15 -04006795 /*
6796 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6797 * Execution", on an exit, vmx_instruction_info holds most of the
6798 * addressing components of the operand. Only the displacement part
6799 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6800 * For how an actual address is calculated from all these components,
6801 * refer to Vol. 1, "Operand Addressing".
6802 */
6803 int scaling = vmx_instruction_info & 3;
6804 int addr_size = (vmx_instruction_info >> 7) & 7;
6805 bool is_reg = vmx_instruction_info & (1u << 10);
6806 int seg_reg = (vmx_instruction_info >> 15) & 7;
6807 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6808 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6809 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6810 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6811
6812 if (is_reg) {
6813 kvm_queue_exception(vcpu, UD_VECTOR);
6814 return 1;
6815 }
6816
6817 /* Addr = segment_base + offset */
6818 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006819 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006820 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006821 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006822 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006823 off += kvm_register_read(vcpu, index_reg)<<scaling;
6824 vmx_get_segment(vcpu, &s, seg_reg);
6825 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006826
6827 if (addr_size == 1) /* 32 bit */
6828 *ret &= 0xffffffff;
6829
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006830 /* Checks for #GP/#SS exceptions. */
6831 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006832 if (is_long_mode(vcpu)) {
6833 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6834 * non-canonical form. This is the only check on the memory
6835 * destination for long mode!
6836 */
6837 exn = is_noncanonical_address(*ret);
6838 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006839 /* Protected mode: apply checks for segment validity in the
6840 * following order:
6841 * - segment type check (#GP(0) may be thrown)
6842 * - usability check (#GP(0)/#SS(0))
6843 * - limit check (#GP(0)/#SS(0))
6844 */
6845 if (wr)
6846 /* #GP(0) if the destination operand is located in a
6847 * read-only data segment or any code segment.
6848 */
6849 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6850 else
6851 /* #GP(0) if the source operand is located in an
6852 * execute-only code segment
6853 */
6854 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006855 if (exn) {
6856 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6857 return 1;
6858 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006859 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6860 */
6861 exn = (s.unusable != 0);
6862 /* Protected mode: #GP(0)/#SS(0) if the memory
6863 * operand is outside the segment limit.
6864 */
6865 exn = exn || (off + sizeof(u64) > s.limit);
6866 }
6867 if (exn) {
6868 kvm_queue_exception_e(vcpu,
6869 seg_reg == VCPU_SREG_SS ?
6870 SS_VECTOR : GP_VECTOR,
6871 0);
6872 return 1;
6873 }
6874
Bandan Das19677e32014-05-06 02:19:15 -04006875 return 0;
6876}
6877
6878/*
Bandan Das3573e222014-05-06 02:19:16 -04006879 * This function performs the various checks including
6880 * - if it's 4KB aligned
6881 * - No bits beyond the physical address width are set
6882 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006883 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006884 */
Bandan Das4291b582014-05-06 02:19:18 -04006885static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6886 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006887{
6888 gva_t gva;
6889 gpa_t vmptr;
6890 struct x86_exception e;
6891 struct page *page;
6892 struct vcpu_vmx *vmx = to_vmx(vcpu);
6893 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6894
6895 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006896 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006897 return 1;
6898
6899 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6900 sizeof(vmptr), &e)) {
6901 kvm_inject_page_fault(vcpu, &e);
6902 return 1;
6903 }
6904
6905 switch (exit_reason) {
6906 case EXIT_REASON_VMON:
6907 /*
6908 * SDM 3: 24.11.5
6909 * The first 4 bytes of VMXON region contain the supported
6910 * VMCS revision identifier
6911 *
6912 * Note - IA32_VMX_BASIC[48] will never be 1
6913 * for the nested case;
6914 * which replaces physical address width with 32
6915 *
6916 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006917 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006918 nested_vmx_failInvalid(vcpu);
6919 skip_emulated_instruction(vcpu);
6920 return 1;
6921 }
6922
6923 page = nested_get_page(vcpu, vmptr);
6924 if (page == NULL ||
6925 *(u32 *)kmap(page) != VMCS12_REVISION) {
6926 nested_vmx_failInvalid(vcpu);
6927 kunmap(page);
6928 skip_emulated_instruction(vcpu);
6929 return 1;
6930 }
6931 kunmap(page);
6932 vmx->nested.vmxon_ptr = vmptr;
6933 break;
Bandan Das4291b582014-05-06 02:19:18 -04006934 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006935 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006936 nested_vmx_failValid(vcpu,
6937 VMXERR_VMCLEAR_INVALID_ADDRESS);
6938 skip_emulated_instruction(vcpu);
6939 return 1;
6940 }
Bandan Das3573e222014-05-06 02:19:16 -04006941
Bandan Das4291b582014-05-06 02:19:18 -04006942 if (vmptr == vmx->nested.vmxon_ptr) {
6943 nested_vmx_failValid(vcpu,
6944 VMXERR_VMCLEAR_VMXON_POINTER);
6945 skip_emulated_instruction(vcpu);
6946 return 1;
6947 }
6948 break;
6949 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006950 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006951 nested_vmx_failValid(vcpu,
6952 VMXERR_VMPTRLD_INVALID_ADDRESS);
6953 skip_emulated_instruction(vcpu);
6954 return 1;
6955 }
6956
6957 if (vmptr == vmx->nested.vmxon_ptr) {
6958 nested_vmx_failValid(vcpu,
6959 VMXERR_VMCLEAR_VMXON_POINTER);
6960 skip_emulated_instruction(vcpu);
6961 return 1;
6962 }
6963 break;
Bandan Das3573e222014-05-06 02:19:16 -04006964 default:
6965 return 1; /* shouldn't happen */
6966 }
6967
Bandan Das4291b582014-05-06 02:19:18 -04006968 if (vmpointer)
6969 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006970 return 0;
6971}
6972
6973/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006974 * Emulate the VMXON instruction.
6975 * Currently, we just remember that VMX is active, and do not save or even
6976 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6977 * do not currently need to store anything in that guest-allocated memory
6978 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6979 * argument is different from the VMXON pointer (which the spec says they do).
6980 */
6981static int handle_vmon(struct kvm_vcpu *vcpu)
6982{
6983 struct kvm_segment cs;
6984 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03006985 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006986 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6987 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006988
6989 /* The Intel VMX Instruction Reference lists a bunch of bits that
6990 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6991 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6992 * Otherwise, we should fail with #UD. We test these now:
6993 */
6994 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6995 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6996 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6997 kvm_queue_exception(vcpu, UD_VECTOR);
6998 return 1;
6999 }
7000
7001 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7002 if (is_long_mode(vcpu) && !cs.l) {
7003 kvm_queue_exception(vcpu, UD_VECTOR);
7004 return 1;
7005 }
7006
7007 if (vmx_get_cpl(vcpu)) {
7008 kvm_inject_gp(vcpu, 0);
7009 return 1;
7010 }
Bandan Das3573e222014-05-06 02:19:16 -04007011
Bandan Das4291b582014-05-06 02:19:18 -04007012 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04007013 return 1;
7014
Abel Gordon145c28d2013-04-18 14:36:55 +03007015 if (vmx->nested.vmxon) {
7016 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
7017 skip_emulated_instruction(vcpu);
7018 return 1;
7019 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007020
Haozhong Zhang3b840802016-06-22 14:59:54 +08007021 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007022 != VMXON_NEEDED_FEATURES) {
7023 kvm_inject_gp(vcpu, 0);
7024 return 1;
7025 }
7026
Radim Krčmářd048c092016-08-08 20:16:22 +02007027 if (cpu_has_vmx_msr_bitmap()) {
7028 vmx->nested.msr_bitmap =
7029 (unsigned long *)__get_free_page(GFP_KERNEL);
7030 if (!vmx->nested.msr_bitmap)
7031 goto out_msr_bitmap;
7032 }
7033
David Matlack4f2777b2016-07-13 17:16:37 -07007034 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7035 if (!vmx->nested.cached_vmcs12)
Radim Krčmářd048c092016-08-08 20:16:22 +02007036 goto out_cached_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -07007037
Abel Gordon8de48832013-04-18 14:37:25 +03007038 if (enable_shadow_vmcs) {
7039 shadow_vmcs = alloc_vmcs();
Radim Krčmářd048c092016-08-08 20:16:22 +02007040 if (!shadow_vmcs)
7041 goto out_shadow_vmcs;
Abel Gordon8de48832013-04-18 14:37:25 +03007042 /* mark vmcs as shadow */
7043 shadow_vmcs->revision_id |= (1u << 31);
7044 /* init shadow vmcs */
7045 vmcs_clear(shadow_vmcs);
7046 vmx->nested.current_shadow_vmcs = shadow_vmcs;
7047 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007048
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007049 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7050 vmx->nested.vmcs02_num = 0;
7051
Jan Kiszkaf41245002014-03-07 20:03:13 +01007052 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
Wanpeng Lif15a75e2016-08-30 16:14:01 +08007053 HRTIMER_MODE_REL_PINNED);
Jan Kiszkaf41245002014-03-07 20:03:13 +01007054 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7055
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007056 vmx->nested.vmxon = true;
7057
7058 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007059 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007060 return 1;
Radim Krčmářd048c092016-08-08 20:16:22 +02007061
7062out_shadow_vmcs:
7063 kfree(vmx->nested.cached_vmcs12);
7064
7065out_cached_vmcs12:
7066 free_page((unsigned long)vmx->nested.msr_bitmap);
7067
7068out_msr_bitmap:
7069 return -ENOMEM;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007070}
7071
7072/*
7073 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7074 * for running VMX instructions (except VMXON, whose prerequisites are
7075 * slightly different). It also specifies what exception to inject otherwise.
7076 */
7077static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7078{
7079 struct kvm_segment cs;
7080 struct vcpu_vmx *vmx = to_vmx(vcpu);
7081
7082 if (!vmx->nested.vmxon) {
7083 kvm_queue_exception(vcpu, UD_VECTOR);
7084 return 0;
7085 }
7086
7087 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7088 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7089 (is_long_mode(vcpu) && !cs.l)) {
7090 kvm_queue_exception(vcpu, UD_VECTOR);
7091 return 0;
7092 }
7093
7094 if (vmx_get_cpl(vcpu)) {
7095 kvm_inject_gp(vcpu, 0);
7096 return 0;
7097 }
7098
7099 return 1;
7100}
7101
Abel Gordone7953d72013-04-18 14:37:55 +03007102static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7103{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007104 if (vmx->nested.current_vmptr == -1ull)
7105 return;
7106
7107 /* current_vmptr and current_vmcs12 are always set/reset together */
7108 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7109 return;
7110
Abel Gordon012f83c2013-04-18 14:39:25 +03007111 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007112 /* copy to memory all shadowed fields in case
7113 they were modified */
7114 copy_shadow_to_vmcs12(vmx);
7115 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007116 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7117 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007118 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007119 }
Wincy Van705699a2015-02-03 23:58:17 +08007120 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007121
7122 /* Flush VMCS12 to guest memory */
7123 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7124 VMCS12_SIZE);
7125
Abel Gordone7953d72013-04-18 14:37:55 +03007126 kunmap(vmx->nested.current_vmcs12_page);
7127 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007128 vmx->nested.current_vmptr = -1ull;
7129 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007130}
7131
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007132/*
7133 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7134 * just stops using VMX.
7135 */
7136static void free_nested(struct vcpu_vmx *vmx)
7137{
7138 if (!vmx->nested.vmxon)
7139 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007140
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007141 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007142 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007143 nested_release_vmcs12(vmx);
Radim Krčmářd048c092016-08-08 20:16:22 +02007144 if (vmx->nested.msr_bitmap) {
7145 free_page((unsigned long)vmx->nested.msr_bitmap);
7146 vmx->nested.msr_bitmap = NULL;
7147 }
Abel Gordone7953d72013-04-18 14:37:55 +03007148 if (enable_shadow_vmcs)
7149 free_vmcs(vmx->nested.current_shadow_vmcs);
David Matlack4f2777b2016-07-13 17:16:37 -07007150 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007151 /* Unpin physical memory we referred to in current vmcs02 */
7152 if (vmx->nested.apic_access_page) {
7153 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007154 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007155 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007156 if (vmx->nested.virtual_apic_page) {
7157 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007158 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007159 }
Wincy Van705699a2015-02-03 23:58:17 +08007160 if (vmx->nested.pi_desc_page) {
7161 kunmap(vmx->nested.pi_desc_page);
7162 nested_release_page(vmx->nested.pi_desc_page);
7163 vmx->nested.pi_desc_page = NULL;
7164 vmx->nested.pi_desc = NULL;
7165 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007166
7167 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007168}
7169
7170/* Emulate the VMXOFF instruction */
7171static int handle_vmoff(struct kvm_vcpu *vcpu)
7172{
7173 if (!nested_vmx_check_permission(vcpu))
7174 return 1;
7175 free_nested(to_vmx(vcpu));
7176 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007177 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007178 return 1;
7179}
7180
Nadav Har'El27d6c862011-05-25 23:06:59 +03007181/* Emulate the VMCLEAR instruction */
7182static int handle_vmclear(struct kvm_vcpu *vcpu)
7183{
7184 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007185 gpa_t vmptr;
7186 struct vmcs12 *vmcs12;
7187 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007188
7189 if (!nested_vmx_check_permission(vcpu))
7190 return 1;
7191
Bandan Das4291b582014-05-06 02:19:18 -04007192 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007193 return 1;
7194
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007195 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007196 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007197
7198 page = nested_get_page(vcpu, vmptr);
7199 if (page == NULL) {
7200 /*
7201 * For accurate processor emulation, VMCLEAR beyond available
7202 * physical memory should do nothing at all. However, it is
7203 * possible that a nested vmx bug, not a guest hypervisor bug,
7204 * resulted in this case, so let's shut down before doing any
7205 * more damage:
7206 */
7207 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7208 return 1;
7209 }
7210 vmcs12 = kmap(page);
7211 vmcs12->launch_state = 0;
7212 kunmap(page);
7213 nested_release_page(page);
7214
7215 nested_free_vmcs02(vmx, vmptr);
7216
7217 skip_emulated_instruction(vcpu);
7218 nested_vmx_succeed(vcpu);
7219 return 1;
7220}
7221
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007222static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7223
7224/* Emulate the VMLAUNCH instruction */
7225static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7226{
7227 return nested_vmx_run(vcpu, true);
7228}
7229
7230/* Emulate the VMRESUME instruction */
7231static int handle_vmresume(struct kvm_vcpu *vcpu)
7232{
7233
7234 return nested_vmx_run(vcpu, false);
7235}
7236
Nadav Har'El49f705c2011-05-25 23:08:30 +03007237enum vmcs_field_type {
7238 VMCS_FIELD_TYPE_U16 = 0,
7239 VMCS_FIELD_TYPE_U64 = 1,
7240 VMCS_FIELD_TYPE_U32 = 2,
7241 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7242};
7243
7244static inline int vmcs_field_type(unsigned long field)
7245{
7246 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7247 return VMCS_FIELD_TYPE_U32;
7248 return (field >> 13) & 0x3 ;
7249}
7250
7251static inline int vmcs_field_readonly(unsigned long field)
7252{
7253 return (((field >> 10) & 0x3) == 1);
7254}
7255
7256/*
7257 * Read a vmcs12 field. Since these can have varying lengths and we return
7258 * one type, we chose the biggest type (u64) and zero-extend the return value
7259 * to that size. Note that the caller, handle_vmread, might need to use only
7260 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7261 * 64-bit fields are to be returned).
7262 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007263static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7264 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007265{
7266 short offset = vmcs_field_to_offset(field);
7267 char *p;
7268
7269 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007270 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007271
7272 p = ((char *)(get_vmcs12(vcpu))) + offset;
7273
7274 switch (vmcs_field_type(field)) {
7275 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7276 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007277 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007278 case VMCS_FIELD_TYPE_U16:
7279 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007280 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007281 case VMCS_FIELD_TYPE_U32:
7282 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007283 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007284 case VMCS_FIELD_TYPE_U64:
7285 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007286 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007287 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007288 WARN_ON(1);
7289 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007290 }
7291}
7292
Abel Gordon20b97fe2013-04-18 14:36:25 +03007293
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007294static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7295 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007296 short offset = vmcs_field_to_offset(field);
7297 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7298 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007299 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007300
7301 switch (vmcs_field_type(field)) {
7302 case VMCS_FIELD_TYPE_U16:
7303 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007304 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007305 case VMCS_FIELD_TYPE_U32:
7306 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007307 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007308 case VMCS_FIELD_TYPE_U64:
7309 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007310 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007311 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7312 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007313 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007314 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007315 WARN_ON(1);
7316 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007317 }
7318
7319}
7320
Abel Gordon16f5b902013-04-18 14:38:25 +03007321static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7322{
7323 int i;
7324 unsigned long field;
7325 u64 field_value;
7326 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007327 const unsigned long *fields = shadow_read_write_fields;
7328 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007329
Jan Kiszka282da872014-10-08 18:05:39 +02007330 preempt_disable();
7331
Abel Gordon16f5b902013-04-18 14:38:25 +03007332 vmcs_load(shadow_vmcs);
7333
7334 for (i = 0; i < num_fields; i++) {
7335 field = fields[i];
7336 switch (vmcs_field_type(field)) {
7337 case VMCS_FIELD_TYPE_U16:
7338 field_value = vmcs_read16(field);
7339 break;
7340 case VMCS_FIELD_TYPE_U32:
7341 field_value = vmcs_read32(field);
7342 break;
7343 case VMCS_FIELD_TYPE_U64:
7344 field_value = vmcs_read64(field);
7345 break;
7346 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7347 field_value = vmcs_readl(field);
7348 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007349 default:
7350 WARN_ON(1);
7351 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007352 }
7353 vmcs12_write_any(&vmx->vcpu, field, field_value);
7354 }
7355
7356 vmcs_clear(shadow_vmcs);
7357 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007358
7359 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007360}
7361
Abel Gordonc3114422013-04-18 14:38:55 +03007362static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7363{
Mathias Krausec2bae892013-06-26 20:36:21 +02007364 const unsigned long *fields[] = {
7365 shadow_read_write_fields,
7366 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007367 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007368 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007369 max_shadow_read_write_fields,
7370 max_shadow_read_only_fields
7371 };
7372 int i, q;
7373 unsigned long field;
7374 u64 field_value = 0;
7375 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
7376
7377 vmcs_load(shadow_vmcs);
7378
Mathias Krausec2bae892013-06-26 20:36:21 +02007379 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007380 for (i = 0; i < max_fields[q]; i++) {
7381 field = fields[q][i];
7382 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7383
7384 switch (vmcs_field_type(field)) {
7385 case VMCS_FIELD_TYPE_U16:
7386 vmcs_write16(field, (u16)field_value);
7387 break;
7388 case VMCS_FIELD_TYPE_U32:
7389 vmcs_write32(field, (u32)field_value);
7390 break;
7391 case VMCS_FIELD_TYPE_U64:
7392 vmcs_write64(field, (u64)field_value);
7393 break;
7394 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7395 vmcs_writel(field, (long)field_value);
7396 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007397 default:
7398 WARN_ON(1);
7399 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007400 }
7401 }
7402 }
7403
7404 vmcs_clear(shadow_vmcs);
7405 vmcs_load(vmx->loaded_vmcs->vmcs);
7406}
7407
Nadav Har'El49f705c2011-05-25 23:08:30 +03007408/*
7409 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7410 * used before) all generate the same failure when it is missing.
7411 */
7412static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7413{
7414 struct vcpu_vmx *vmx = to_vmx(vcpu);
7415 if (vmx->nested.current_vmptr == -1ull) {
7416 nested_vmx_failInvalid(vcpu);
7417 skip_emulated_instruction(vcpu);
7418 return 0;
7419 }
7420 return 1;
7421}
7422
7423static int handle_vmread(struct kvm_vcpu *vcpu)
7424{
7425 unsigned long field;
7426 u64 field_value;
7427 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7428 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7429 gva_t gva = 0;
7430
7431 if (!nested_vmx_check_permission(vcpu) ||
7432 !nested_vmx_check_vmcs12(vcpu))
7433 return 1;
7434
7435 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007436 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007437 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007438 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007439 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7440 skip_emulated_instruction(vcpu);
7441 return 1;
7442 }
7443 /*
7444 * Now copy part of this value to register or memory, as requested.
7445 * Note that the number of bits actually copied is 32 or 64 depending
7446 * on the guest's mode (32 or 64 bit), not on the given field's length.
7447 */
7448 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007449 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007450 field_value);
7451 } else {
7452 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007453 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007454 return 1;
7455 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7456 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7457 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7458 }
7459
7460 nested_vmx_succeed(vcpu);
7461 skip_emulated_instruction(vcpu);
7462 return 1;
7463}
7464
7465
7466static int handle_vmwrite(struct kvm_vcpu *vcpu)
7467{
7468 unsigned long field;
7469 gva_t gva;
7470 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7471 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007472 /* The value to write might be 32 or 64 bits, depending on L1's long
7473 * mode, and eventually we need to write that into a field of several
7474 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007475 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007476 * bits into the vmcs12 field.
7477 */
7478 u64 field_value = 0;
7479 struct x86_exception e;
7480
7481 if (!nested_vmx_check_permission(vcpu) ||
7482 !nested_vmx_check_vmcs12(vcpu))
7483 return 1;
7484
7485 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007486 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007487 (((vmx_instruction_info) >> 3) & 0xf));
7488 else {
7489 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007490 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007491 return 1;
7492 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007493 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007494 kvm_inject_page_fault(vcpu, &e);
7495 return 1;
7496 }
7497 }
7498
7499
Nadav Amit27e6fb52014-06-18 17:19:26 +03007500 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007501 if (vmcs_field_readonly(field)) {
7502 nested_vmx_failValid(vcpu,
7503 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7504 skip_emulated_instruction(vcpu);
7505 return 1;
7506 }
7507
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007508 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007509 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7510 skip_emulated_instruction(vcpu);
7511 return 1;
7512 }
7513
7514 nested_vmx_succeed(vcpu);
7515 skip_emulated_instruction(vcpu);
7516 return 1;
7517}
7518
Nadav Har'El63846662011-05-25 23:07:29 +03007519/* Emulate the VMPTRLD instruction */
7520static int handle_vmptrld(struct kvm_vcpu *vcpu)
7521{
7522 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007523 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007524
7525 if (!nested_vmx_check_permission(vcpu))
7526 return 1;
7527
Bandan Das4291b582014-05-06 02:19:18 -04007528 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007529 return 1;
7530
Nadav Har'El63846662011-05-25 23:07:29 +03007531 if (vmx->nested.current_vmptr != vmptr) {
7532 struct vmcs12 *new_vmcs12;
7533 struct page *page;
7534 page = nested_get_page(vcpu, vmptr);
7535 if (page == NULL) {
7536 nested_vmx_failInvalid(vcpu);
7537 skip_emulated_instruction(vcpu);
7538 return 1;
7539 }
7540 new_vmcs12 = kmap(page);
7541 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7542 kunmap(page);
7543 nested_release_page_clean(page);
7544 nested_vmx_failValid(vcpu,
7545 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7546 skip_emulated_instruction(vcpu);
7547 return 1;
7548 }
Nadav Har'El63846662011-05-25 23:07:29 +03007549
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007550 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007551 vmx->nested.current_vmptr = vmptr;
7552 vmx->nested.current_vmcs12 = new_vmcs12;
7553 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007554 /*
7555 * Load VMCS12 from guest memory since it is not already
7556 * cached.
7557 */
7558 memcpy(vmx->nested.cached_vmcs12,
7559 vmx->nested.current_vmcs12, VMCS12_SIZE);
7560
Abel Gordon012f83c2013-04-18 14:39:25 +03007561 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007562 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7563 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007564 vmcs_write64(VMCS_LINK_POINTER,
7565 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007566 vmx->nested.sync_shadow_vmcs = true;
7567 }
Nadav Har'El63846662011-05-25 23:07:29 +03007568 }
7569
7570 nested_vmx_succeed(vcpu);
7571 skip_emulated_instruction(vcpu);
7572 return 1;
7573}
7574
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007575/* Emulate the VMPTRST instruction */
7576static int handle_vmptrst(struct kvm_vcpu *vcpu)
7577{
7578 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7579 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7580 gva_t vmcs_gva;
7581 struct x86_exception e;
7582
7583 if (!nested_vmx_check_permission(vcpu))
7584 return 1;
7585
7586 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007587 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007588 return 1;
7589 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7590 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7591 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7592 sizeof(u64), &e)) {
7593 kvm_inject_page_fault(vcpu, &e);
7594 return 1;
7595 }
7596 nested_vmx_succeed(vcpu);
7597 skip_emulated_instruction(vcpu);
7598 return 1;
7599}
7600
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007601/* Emulate the INVEPT instruction */
7602static int handle_invept(struct kvm_vcpu *vcpu)
7603{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007604 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007605 u32 vmx_instruction_info, types;
7606 unsigned long type;
7607 gva_t gva;
7608 struct x86_exception e;
7609 struct {
7610 u64 eptp, gpa;
7611 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007612
Wincy Vanb9c237b2015-02-03 23:56:30 +08007613 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7614 SECONDARY_EXEC_ENABLE_EPT) ||
7615 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007616 kvm_queue_exception(vcpu, UD_VECTOR);
7617 return 1;
7618 }
7619
7620 if (!nested_vmx_check_permission(vcpu))
7621 return 1;
7622
7623 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7624 kvm_queue_exception(vcpu, UD_VECTOR);
7625 return 1;
7626 }
7627
7628 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007629 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007630
Wincy Vanb9c237b2015-02-03 23:56:30 +08007631 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007632
Jim Mattson85c856b2016-10-26 08:38:38 -07007633 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007634 nested_vmx_failValid(vcpu,
7635 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007636 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007637 return 1;
7638 }
7639
7640 /* According to the Intel VMX instruction reference, the memory
7641 * operand is read even if it isn't needed (e.g., for type==global)
7642 */
7643 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007644 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007645 return 1;
7646 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7647 sizeof(operand), &e)) {
7648 kvm_inject_page_fault(vcpu, &e);
7649 return 1;
7650 }
7651
7652 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007653 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007654 /*
7655 * TODO: track mappings and invalidate
7656 * single context requests appropriately
7657 */
7658 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007659 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007660 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007661 nested_vmx_succeed(vcpu);
7662 break;
7663 default:
7664 BUG_ON(1);
7665 break;
7666 }
7667
7668 skip_emulated_instruction(vcpu);
7669 return 1;
7670}
7671
Petr Matouseka642fc32014-09-23 20:22:30 +02007672static int handle_invvpid(struct kvm_vcpu *vcpu)
7673{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007674 struct vcpu_vmx *vmx = to_vmx(vcpu);
7675 u32 vmx_instruction_info;
7676 unsigned long type, types;
7677 gva_t gva;
7678 struct x86_exception e;
7679 int vpid;
7680
7681 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7682 SECONDARY_EXEC_ENABLE_VPID) ||
7683 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7684 kvm_queue_exception(vcpu, UD_VECTOR);
7685 return 1;
7686 }
7687
7688 if (!nested_vmx_check_permission(vcpu))
7689 return 1;
7690
7691 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7692 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7693
7694 types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7;
7695
Jim Mattson85c856b2016-10-26 08:38:38 -07007696 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007697 nested_vmx_failValid(vcpu,
7698 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007699 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007700 return 1;
7701 }
7702
7703 /* according to the intel vmx instruction reference, the memory
7704 * operand is read even if it isn't needed (e.g., for type==global)
7705 */
7706 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7707 vmx_instruction_info, false, &gva))
7708 return 1;
7709 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7710 sizeof(u32), &e)) {
7711 kvm_inject_page_fault(vcpu, &e);
7712 return 1;
7713 }
7714
7715 switch (type) {
Paolo Bonzinief697a72016-03-18 16:58:38 +01007716 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
7717 /*
7718 * Old versions of KVM use the single-context version so we
7719 * have to support it; just treat it the same as all-context.
7720 */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007721 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li5c614b32015-10-13 09:18:36 -07007722 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007723 nested_vmx_succeed(vcpu);
7724 break;
7725 default:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007726 /* Trap individual address invalidation invvpid calls */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007727 BUG_ON(1);
7728 break;
7729 }
7730
7731 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007732 return 1;
7733}
7734
Kai Huang843e4332015-01-28 10:54:28 +08007735static int handle_pml_full(struct kvm_vcpu *vcpu)
7736{
7737 unsigned long exit_qualification;
7738
7739 trace_kvm_pml_full(vcpu->vcpu_id);
7740
7741 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7742
7743 /*
7744 * PML buffer FULL happened while executing iret from NMI,
7745 * "blocked by NMI" bit has to be set before next VM entry.
7746 */
7747 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7748 cpu_has_virtual_nmis() &&
7749 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7750 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7751 GUEST_INTR_STATE_NMI);
7752
7753 /*
7754 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7755 * here.., and there's no userspace involvement needed for PML.
7756 */
7757 return 1;
7758}
7759
Yunhong Jiang64672c92016-06-13 14:19:59 -07007760static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7761{
7762 kvm_lapic_expired_hv_timer(vcpu);
7763 return 1;
7764}
7765
Nadav Har'El0140cae2011-05-25 23:06:28 +03007766/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007767 * The exit handlers return 1 if the exit was handled fully and guest execution
7768 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7769 * to be done to userspace and return 0.
7770 */
Mathias Krause772e0312012-08-30 01:30:19 +02007771static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007772 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7773 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007774 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007775 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007776 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007777 [EXIT_REASON_CR_ACCESS] = handle_cr,
7778 [EXIT_REASON_DR_ACCESS] = handle_dr,
7779 [EXIT_REASON_CPUID] = handle_cpuid,
7780 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7781 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7782 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7783 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007784 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007785 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007786 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007787 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007788 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007789 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007790 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007791 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007792 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007793 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007794 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007795 [EXIT_REASON_VMOFF] = handle_vmoff,
7796 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007797 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7798 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007799 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007800 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007801 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007802 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007803 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007804 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007805 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7806 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007807 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007808 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007809 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007810 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007811 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007812 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007813 [EXIT_REASON_XSAVES] = handle_xsaves,
7814 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007815 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007816 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007817};
7818
7819static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007820 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007821
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007822static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7823 struct vmcs12 *vmcs12)
7824{
7825 unsigned long exit_qualification;
7826 gpa_t bitmap, last_bitmap;
7827 unsigned int port;
7828 int size;
7829 u8 b;
7830
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007831 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007832 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007833
7834 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7835
7836 port = exit_qualification >> 16;
7837 size = (exit_qualification & 7) + 1;
7838
7839 last_bitmap = (gpa_t)-1;
7840 b = -1;
7841
7842 while (size > 0) {
7843 if (port < 0x8000)
7844 bitmap = vmcs12->io_bitmap_a;
7845 else if (port < 0x10000)
7846 bitmap = vmcs12->io_bitmap_b;
7847 else
Joe Perches1d804d02015-03-30 16:46:09 -07007848 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007849 bitmap += (port & 0x7fff) / 8;
7850
7851 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007852 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007853 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007854 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007855 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007856
7857 port++;
7858 size--;
7859 last_bitmap = bitmap;
7860 }
7861
Joe Perches1d804d02015-03-30 16:46:09 -07007862 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007863}
7864
Nadav Har'El644d7112011-05-25 23:12:35 +03007865/*
7866 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7867 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7868 * disinterest in the current event (read or write a specific MSR) by using an
7869 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7870 */
7871static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7872 struct vmcs12 *vmcs12, u32 exit_reason)
7873{
7874 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7875 gpa_t bitmap;
7876
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007877 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007878 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007879
7880 /*
7881 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7882 * for the four combinations of read/write and low/high MSR numbers.
7883 * First we need to figure out which of the four to use:
7884 */
7885 bitmap = vmcs12->msr_bitmap;
7886 if (exit_reason == EXIT_REASON_MSR_WRITE)
7887 bitmap += 2048;
7888 if (msr_index >= 0xc0000000) {
7889 msr_index -= 0xc0000000;
7890 bitmap += 1024;
7891 }
7892
7893 /* Then read the msr_index'th bit from this bitmap: */
7894 if (msr_index < 1024*8) {
7895 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007896 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007897 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007898 return 1 & (b >> (msr_index & 7));
7899 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007900 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007901}
7902
7903/*
7904 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7905 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7906 * intercept (via guest_host_mask etc.) the current event.
7907 */
7908static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7909 struct vmcs12 *vmcs12)
7910{
7911 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7912 int cr = exit_qualification & 15;
7913 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007914 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007915
7916 switch ((exit_qualification >> 4) & 3) {
7917 case 0: /* mov to cr */
7918 switch (cr) {
7919 case 0:
7920 if (vmcs12->cr0_guest_host_mask &
7921 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007922 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007923 break;
7924 case 3:
7925 if ((vmcs12->cr3_target_count >= 1 &&
7926 vmcs12->cr3_target_value0 == val) ||
7927 (vmcs12->cr3_target_count >= 2 &&
7928 vmcs12->cr3_target_value1 == val) ||
7929 (vmcs12->cr3_target_count >= 3 &&
7930 vmcs12->cr3_target_value2 == val) ||
7931 (vmcs12->cr3_target_count >= 4 &&
7932 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007933 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007934 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007935 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007936 break;
7937 case 4:
7938 if (vmcs12->cr4_guest_host_mask &
7939 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007940 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007941 break;
7942 case 8:
7943 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007944 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007945 break;
7946 }
7947 break;
7948 case 2: /* clts */
7949 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7950 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007951 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007952 break;
7953 case 1: /* mov from cr */
7954 switch (cr) {
7955 case 3:
7956 if (vmcs12->cpu_based_vm_exec_control &
7957 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007958 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007959 break;
7960 case 8:
7961 if (vmcs12->cpu_based_vm_exec_control &
7962 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007963 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007964 break;
7965 }
7966 break;
7967 case 3: /* lmsw */
7968 /*
7969 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7970 * cr0. Other attempted changes are ignored, with no exit.
7971 */
7972 if (vmcs12->cr0_guest_host_mask & 0xe &
7973 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007974 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007975 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7976 !(vmcs12->cr0_read_shadow & 0x1) &&
7977 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07007978 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007979 break;
7980 }
Joe Perches1d804d02015-03-30 16:46:09 -07007981 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007982}
7983
7984/*
7985 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
7986 * should handle it ourselves in L0 (and then continue L2). Only call this
7987 * when in is_guest_mode (L2).
7988 */
7989static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
7990{
Nadav Har'El644d7112011-05-25 23:12:35 +03007991 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7992 struct vcpu_vmx *vmx = to_vmx(vcpu);
7993 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01007994 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03007995
Jan Kiszka542060e2014-01-04 18:47:21 +01007996 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
7997 vmcs_readl(EXIT_QUALIFICATION),
7998 vmx->idt_vectoring_info,
7999 intr_info,
8000 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8001 KVM_ISA_VMX);
8002
Nadav Har'El644d7112011-05-25 23:12:35 +03008003 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008004 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008005
8006 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008007 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8008 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008009 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008010 }
8011
8012 switch (exit_reason) {
8013 case EXIT_REASON_EXCEPTION_NMI:
8014 if (!is_exception(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008015 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008016 else if (is_page_fault(intr_info))
8017 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008018 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008019 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008020 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008021 else if (is_debug(intr_info) &&
8022 vcpu->guest_debug &
8023 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8024 return false;
8025 else if (is_breakpoint(intr_info) &&
8026 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8027 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008028 return vmcs12->exception_bitmap &
8029 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8030 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008031 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008032 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008033 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008034 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008035 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008036 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008037 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008038 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008039 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008040 case EXIT_REASON_CPUID:
Marcelo Tosattibc613492014-09-18 18:24:57 -03008041 if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
Joe Perches1d804d02015-03-30 16:46:09 -07008042 return false;
8043 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008044 case EXIT_REASON_HLT:
8045 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8046 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008047 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008048 case EXIT_REASON_INVLPG:
8049 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8050 case EXIT_REASON_RDPMC:
8051 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008052 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008053 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8054 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8055 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8056 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8057 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8058 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008059 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008060 /*
8061 * VMX instructions trap unconditionally. This allows L1 to
8062 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8063 */
Joe Perches1d804d02015-03-30 16:46:09 -07008064 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008065 case EXIT_REASON_CR_ACCESS:
8066 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8067 case EXIT_REASON_DR_ACCESS:
8068 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8069 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008070 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008071 case EXIT_REASON_MSR_READ:
8072 case EXIT_REASON_MSR_WRITE:
8073 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8074 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008075 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008076 case EXIT_REASON_MWAIT_INSTRUCTION:
8077 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008078 case EXIT_REASON_MONITOR_TRAP_FLAG:
8079 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008080 case EXIT_REASON_MONITOR_INSTRUCTION:
8081 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8082 case EXIT_REASON_PAUSE_INSTRUCTION:
8083 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8084 nested_cpu_has2(vmcs12,
8085 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8086 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008087 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008088 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008089 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008090 case EXIT_REASON_APIC_ACCESS:
8091 return nested_cpu_has2(vmcs12,
8092 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008093 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008094 case EXIT_REASON_EOI_INDUCED:
8095 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008096 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008097 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008098 /*
8099 * L0 always deals with the EPT violation. If nested EPT is
8100 * used, and the nested mmu code discovers that the address is
8101 * missing in the guest EPT table (EPT12), the EPT violation
8102 * will be injected with nested_ept_inject_page_fault()
8103 */
Joe Perches1d804d02015-03-30 16:46:09 -07008104 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008105 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008106 /*
8107 * L2 never uses directly L1's EPT, but rather L0's own EPT
8108 * table (shadow on EPT) or a merged EPT table that L0 built
8109 * (EPT on EPT). So any problems with the structure of the
8110 * table is L0's fault.
8111 */
Joe Perches1d804d02015-03-30 16:46:09 -07008112 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008113 case EXIT_REASON_WBINVD:
8114 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8115 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008116 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008117 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8118 /*
8119 * This should never happen, since it is not possible to
8120 * set XSS to a non-zero value---neither in L1 nor in L2.
8121 * If if it were, XSS would have to be checked against
8122 * the XSS exit bitmap in vmcs12.
8123 */
8124 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008125 case EXIT_REASON_PREEMPTION_TIMER:
8126 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008127 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008128 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008129 }
8130}
8131
Avi Kivity586f9602010-11-18 13:09:54 +02008132static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8133{
8134 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8135 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8136}
8137
Kai Huanga3eaa862015-11-04 13:46:05 +08008138static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008139{
Kai Huanga3eaa862015-11-04 13:46:05 +08008140 if (vmx->pml_pg) {
8141 __free_page(vmx->pml_pg);
8142 vmx->pml_pg = NULL;
8143 }
Kai Huang843e4332015-01-28 10:54:28 +08008144}
8145
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008146static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008147{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008148 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008149 u64 *pml_buf;
8150 u16 pml_idx;
8151
8152 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8153
8154 /* Do nothing if PML buffer is empty */
8155 if (pml_idx == (PML_ENTITY_NUM - 1))
8156 return;
8157
8158 /* PML index always points to next available PML buffer entity */
8159 if (pml_idx >= PML_ENTITY_NUM)
8160 pml_idx = 0;
8161 else
8162 pml_idx++;
8163
8164 pml_buf = page_address(vmx->pml_pg);
8165 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8166 u64 gpa;
8167
8168 gpa = pml_buf[pml_idx];
8169 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008170 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008171 }
8172
8173 /* reset PML index */
8174 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8175}
8176
8177/*
8178 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8179 * Called before reporting dirty_bitmap to userspace.
8180 */
8181static void kvm_flush_pml_buffers(struct kvm *kvm)
8182{
8183 int i;
8184 struct kvm_vcpu *vcpu;
8185 /*
8186 * We only need to kick vcpu out of guest mode here, as PML buffer
8187 * is flushed at beginning of all VMEXITs, and it's obvious that only
8188 * vcpus running in guest are possible to have unflushed GPAs in PML
8189 * buffer.
8190 */
8191 kvm_for_each_vcpu(i, vcpu, kvm)
8192 kvm_vcpu_kick(vcpu);
8193}
8194
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008195static void vmx_dump_sel(char *name, uint32_t sel)
8196{
8197 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8198 name, vmcs_read32(sel),
8199 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8200 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8201 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8202}
8203
8204static void vmx_dump_dtsel(char *name, uint32_t limit)
8205{
8206 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8207 name, vmcs_read32(limit),
8208 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8209}
8210
8211static void dump_vmcs(void)
8212{
8213 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8214 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8215 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8216 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8217 u32 secondary_exec_control = 0;
8218 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008219 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008220 int i, n;
8221
8222 if (cpu_has_secondary_exec_ctrls())
8223 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8224
8225 pr_err("*** Guest State ***\n");
8226 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8227 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8228 vmcs_readl(CR0_GUEST_HOST_MASK));
8229 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8230 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8231 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8232 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8233 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8234 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008235 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8236 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8237 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8238 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008239 }
8240 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8241 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8242 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8243 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8244 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8245 vmcs_readl(GUEST_SYSENTER_ESP),
8246 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8247 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8248 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8249 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8250 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8251 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8252 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8253 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8254 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8255 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8256 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8257 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8258 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008259 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8260 efer, vmcs_read64(GUEST_IA32_PAT));
8261 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8262 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008263 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8264 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008265 pr_err("PerfGlobCtl = 0x%016llx\n",
8266 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008267 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008268 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008269 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8270 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8271 vmcs_read32(GUEST_ACTIVITY_STATE));
8272 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8273 pr_err("InterruptStatus = %04x\n",
8274 vmcs_read16(GUEST_INTR_STATUS));
8275
8276 pr_err("*** Host State ***\n");
8277 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8278 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8279 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8280 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8281 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8282 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8283 vmcs_read16(HOST_TR_SELECTOR));
8284 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8285 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8286 vmcs_readl(HOST_TR_BASE));
8287 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8288 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8289 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8290 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8291 vmcs_readl(HOST_CR4));
8292 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8293 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8294 vmcs_read32(HOST_IA32_SYSENTER_CS),
8295 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8296 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008297 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8298 vmcs_read64(HOST_IA32_EFER),
8299 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008300 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008301 pr_err("PerfGlobCtl = 0x%016llx\n",
8302 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008303
8304 pr_err("*** Control State ***\n");
8305 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8306 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8307 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8308 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8309 vmcs_read32(EXCEPTION_BITMAP),
8310 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8311 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8312 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8313 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8314 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8315 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8316 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8317 vmcs_read32(VM_EXIT_INTR_INFO),
8318 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8319 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8320 pr_err(" reason=%08x qualification=%016lx\n",
8321 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8322 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8323 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8324 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008325 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008326 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008327 pr_err("TSC Multiplier = 0x%016llx\n",
8328 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008329 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8330 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8331 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8332 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8333 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008334 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008335 n = vmcs_read32(CR3_TARGET_COUNT);
8336 for (i = 0; i + 1 < n; i += 4)
8337 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8338 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8339 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8340 if (i < n)
8341 pr_err("CR3 target%u=%016lx\n",
8342 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8343 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8344 pr_err("PLE Gap=%08x Window=%08x\n",
8345 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8346 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8347 pr_err("Virtual processor ID = 0x%04x\n",
8348 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8349}
8350
Avi Kivity6aa8b732006-12-10 02:21:36 -08008351/*
8352 * The guest has exited. See if we can fix it or if we need userspace
8353 * assistance.
8354 */
Avi Kivity851ba692009-08-24 11:10:17 +03008355static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008356{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008357 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008358 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008359 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008360
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008361 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8362
Kai Huang843e4332015-01-28 10:54:28 +08008363 /*
8364 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8365 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8366 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8367 * mode as if vcpus is in root mode, the PML buffer must has been
8368 * flushed already.
8369 */
8370 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008371 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008372
Mohammed Gamal80ced182009-09-01 12:48:18 +02008373 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008374 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008375 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008376
Nadav Har'El644d7112011-05-25 23:12:35 +03008377 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008378 nested_vmx_vmexit(vcpu, exit_reason,
8379 vmcs_read32(VM_EXIT_INTR_INFO),
8380 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008381 return 1;
8382 }
8383
Mohammed Gamal51207022010-05-31 22:40:54 +03008384 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008385 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008386 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8387 vcpu->run->fail_entry.hardware_entry_failure_reason
8388 = exit_reason;
8389 return 0;
8390 }
8391
Avi Kivity29bd8a72007-09-10 17:27:03 +03008392 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008393 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8394 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008395 = vmcs_read32(VM_INSTRUCTION_ERROR);
8396 return 0;
8397 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008398
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008399 /*
8400 * Note:
8401 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8402 * delivery event since it indicates guest is accessing MMIO.
8403 * The vm-exit can be triggered again after return to guest that
8404 * will cause infinite loop.
8405 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008406 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008407 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008408 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008409 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008410 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8411 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8412 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8413 vcpu->run->internal.ndata = 2;
8414 vcpu->run->internal.data[0] = vectoring_info;
8415 vcpu->run->internal.data[1] = exit_reason;
8416 return 0;
8417 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008418
Nadav Har'El644d7112011-05-25 23:12:35 +03008419 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8420 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008421 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008422 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008423 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008424 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008425 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008426 /*
8427 * This CPU don't support us in finding the end of an
8428 * NMI-blocked window if the guest runs with IRQs
8429 * disabled. So we pull the trigger after 1 s of
8430 * futile waiting, but inform the user about this.
8431 */
8432 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8433 "state on VCPU %d after 1 s timeout\n",
8434 __func__, vcpu->vcpu_id);
8435 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008436 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008437 }
8438
Avi Kivity6aa8b732006-12-10 02:21:36 -08008439 if (exit_reason < kvm_vmx_max_exit_handlers
8440 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008441 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008442 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008443 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8444 kvm_queue_exception(vcpu, UD_VECTOR);
8445 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008446 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008447}
8448
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008449static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008450{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008451 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8452
8453 if (is_guest_mode(vcpu) &&
8454 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8455 return;
8456
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008457 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008458 vmcs_write32(TPR_THRESHOLD, 0);
8459 return;
8460 }
8461
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008462 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008463}
8464
Yang Zhang8d146952013-01-25 10:18:50 +08008465static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8466{
8467 u32 sec_exec_control;
8468
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008469 /* Postpone execution until vmcs01 is the current VMCS. */
8470 if (is_guest_mode(vcpu)) {
8471 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8472 return;
8473 }
8474
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008475 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008476 return;
8477
Paolo Bonzini35754c92015-07-29 12:05:37 +02008478 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008479 return;
8480
8481 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8482
8483 if (set) {
8484 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8485 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8486 } else {
8487 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8488 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8489 }
8490 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8491
8492 vmx_set_msr_bitmap(vcpu);
8493}
8494
Tang Chen38b99172014-09-24 15:57:54 +08008495static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8496{
8497 struct vcpu_vmx *vmx = to_vmx(vcpu);
8498
8499 /*
8500 * Currently we do not handle the nested case where L2 has an
8501 * APIC access page of its own; that page is still pinned.
8502 * Hence, we skip the case where the VCPU is in guest mode _and_
8503 * L1 prepared an APIC access page for L2.
8504 *
8505 * For the case where L1 and L2 share the same APIC access page
8506 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8507 * in the vmcs12), this function will only update either the vmcs01
8508 * or the vmcs02. If the former, the vmcs02 will be updated by
8509 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8510 * the next L2->L1 exit.
8511 */
8512 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008513 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Tang Chen38b99172014-09-24 15:57:54 +08008514 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
8515 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8516}
8517
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008518static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008519{
8520 u16 status;
8521 u8 old;
8522
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008523 if (max_isr == -1)
8524 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008525
8526 status = vmcs_read16(GUEST_INTR_STATUS);
8527 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008528 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008529 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008530 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008531 vmcs_write16(GUEST_INTR_STATUS, status);
8532 }
8533}
8534
8535static void vmx_set_rvi(int vector)
8536{
8537 u16 status;
8538 u8 old;
8539
Wei Wang4114c272014-11-05 10:53:43 +08008540 if (vector == -1)
8541 vector = 0;
8542
Yang Zhangc7c9c562013-01-25 10:18:51 +08008543 status = vmcs_read16(GUEST_INTR_STATUS);
8544 old = (u8)status & 0xff;
8545 if ((u8)vector != old) {
8546 status &= ~0xff;
8547 status |= (u8)vector;
8548 vmcs_write16(GUEST_INTR_STATUS, status);
8549 }
8550}
8551
8552static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8553{
Wanpeng Li963fee12014-07-17 19:03:00 +08008554 if (!is_guest_mode(vcpu)) {
8555 vmx_set_rvi(max_irr);
8556 return;
8557 }
8558
Wei Wang4114c272014-11-05 10:53:43 +08008559 if (max_irr == -1)
8560 return;
8561
Wanpeng Li963fee12014-07-17 19:03:00 +08008562 /*
Wei Wang4114c272014-11-05 10:53:43 +08008563 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8564 * handles it.
8565 */
8566 if (nested_exit_on_intr(vcpu))
8567 return;
8568
8569 /*
8570 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008571 * is run without virtual interrupt delivery.
8572 */
8573 if (!kvm_event_needs_reinjection(vcpu) &&
8574 vmx_interrupt_allowed(vcpu)) {
8575 kvm_queue_interrupt(vcpu, max_irr, false);
8576 vmx_inject_irq(vcpu);
8577 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008578}
8579
Andrey Smetanin63086302015-11-10 15:36:32 +03008580static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008581{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008582 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008583 return;
8584
Yang Zhangc7c9c562013-01-25 10:18:51 +08008585 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8586 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8587 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8588 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8589}
8590
Avi Kivity51aa01d2010-07-20 14:31:20 +03008591static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008592{
Avi Kivity00eba012011-03-07 17:24:54 +02008593 u32 exit_intr_info;
8594
8595 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8596 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8597 return;
8598
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008599 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008600 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008601
8602 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008603 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008604 kvm_machine_check();
8605
Gleb Natapov20f65982009-05-11 13:35:55 +03008606 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008607 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008608 (exit_intr_info & INTR_INFO_VALID_MASK)) {
8609 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008610 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008611 kvm_after_handle_nmi(&vmx->vcpu);
8612 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008613}
Gleb Natapov20f65982009-05-11 13:35:55 +03008614
Yang Zhanga547c6d2013-04-11 19:25:10 +08008615static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8616{
8617 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008618 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008619
8620 /*
8621 * If external interrupt exists, IF bit is set in rflags/eflags on the
8622 * interrupt stack frame, and interrupt will be enabled on a return
8623 * from interrupt handler.
8624 */
8625 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8626 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8627 unsigned int vector;
8628 unsigned long entry;
8629 gate_desc *desc;
8630 struct vcpu_vmx *vmx = to_vmx(vcpu);
8631#ifdef CONFIG_X86_64
8632 unsigned long tmp;
8633#endif
8634
8635 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8636 desc = (gate_desc *)vmx->host_idt_base + vector;
8637 entry = gate_offset(*desc);
8638 asm volatile(
8639#ifdef CONFIG_X86_64
8640 "mov %%" _ASM_SP ", %[sp]\n\t"
8641 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8642 "push $%c[ss]\n\t"
8643 "push %[sp]\n\t"
8644#endif
8645 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008646 __ASM_SIZE(push) " $%c[cs]\n\t"
8647 "call *%[entry]\n\t"
8648 :
8649#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008650 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008651#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008652 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008653 :
8654 [entry]"r"(entry),
8655 [ss]"i"(__KERNEL_DS),
8656 [cs]"i"(__KERNEL_CS)
8657 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008658 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008659}
8660
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008661static bool vmx_has_high_real_mode_segbase(void)
8662{
8663 return enable_unrestricted_guest || emulate_invalid_guest_state;
8664}
8665
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008666static bool vmx_mpx_supported(void)
8667{
8668 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8669 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8670}
8671
Wanpeng Li55412b22014-12-02 19:21:30 +08008672static bool vmx_xsaves_supported(void)
8673{
8674 return vmcs_config.cpu_based_2nd_exec_ctrl &
8675 SECONDARY_EXEC_XSAVES;
8676}
8677
Avi Kivity51aa01d2010-07-20 14:31:20 +03008678static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8679{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008680 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008681 bool unblock_nmi;
8682 u8 vector;
8683 bool idtv_info_valid;
8684
8685 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008686
Avi Kivitycf393f72008-07-01 16:20:21 +03008687 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008688 if (vmx->nmi_known_unmasked)
8689 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008690 /*
8691 * Can't use vmx->exit_intr_info since we're not sure what
8692 * the exit reason is.
8693 */
8694 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008695 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8696 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8697 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008698 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008699 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8700 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008701 * SDM 3: 23.2.2 (September 2008)
8702 * Bit 12 is undefined in any of the following cases:
8703 * If the VM exit sets the valid bit in the IDT-vectoring
8704 * information field.
8705 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008706 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008707 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8708 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008709 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8710 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008711 else
8712 vmx->nmi_known_unmasked =
8713 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8714 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008715 } else if (unlikely(vmx->soft_vnmi_blocked))
8716 vmx->vnmi_blocked_time +=
8717 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008718}
8719
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008720static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008721 u32 idt_vectoring_info,
8722 int instr_len_field,
8723 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008724{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008725 u8 vector;
8726 int type;
8727 bool idtv_info_valid;
8728
8729 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008730
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008731 vcpu->arch.nmi_injected = false;
8732 kvm_clear_exception_queue(vcpu);
8733 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008734
8735 if (!idtv_info_valid)
8736 return;
8737
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008738 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008739
Avi Kivity668f6122008-07-02 09:28:55 +03008740 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8741 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008742
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008743 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008744 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008745 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008746 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008747 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008748 * Clear bit "block by NMI" before VM entry if a NMI
8749 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008750 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008751 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008752 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008753 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008754 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008755 /* fall through */
8756 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008757 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008758 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008759 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008760 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008761 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008762 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008763 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008764 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008765 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008766 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008767 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008768 break;
8769 default:
8770 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008771 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008772}
8773
Avi Kivity83422e12010-07-20 14:43:23 +03008774static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8775{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008776 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008777 VM_EXIT_INSTRUCTION_LEN,
8778 IDT_VECTORING_ERROR_CODE);
8779}
8780
Avi Kivityb463a6f2010-07-20 15:06:17 +03008781static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8782{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008783 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008784 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8785 VM_ENTRY_INSTRUCTION_LEN,
8786 VM_ENTRY_EXCEPTION_ERROR_CODE);
8787
8788 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8789}
8790
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008791static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8792{
8793 int i, nr_msrs;
8794 struct perf_guest_switch_msr *msrs;
8795
8796 msrs = perf_guest_get_msrs(&nr_msrs);
8797
8798 if (!msrs)
8799 return;
8800
8801 for (i = 0; i < nr_msrs; i++)
8802 if (msrs[i].host == msrs[i].guest)
8803 clear_atomic_switch_msr(vmx, msrs[i].msr);
8804 else
8805 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8806 msrs[i].host);
8807}
8808
Yunhong Jiang64672c92016-06-13 14:19:59 -07008809void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8810{
8811 struct vcpu_vmx *vmx = to_vmx(vcpu);
8812 u64 tscl;
8813 u32 delta_tsc;
8814
8815 if (vmx->hv_deadline_tsc == -1)
8816 return;
8817
8818 tscl = rdtsc();
8819 if (vmx->hv_deadline_tsc > tscl)
8820 /* sure to be 32 bit only because checked on set_hv_timer */
8821 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8822 cpu_preemption_timer_multi);
8823 else
8824 delta_tsc = 0;
8825
8826 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8827}
8828
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008829static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008830{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008831 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008832 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008833
8834 /* Record the guest's net vcpu time for enforced NMI injections. */
8835 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8836 vmx->entry_time = ktime_get();
8837
8838 /* Don't enter VMX if guest state is invalid, let the exit handler
8839 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008840 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008841 return;
8842
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008843 if (vmx->ple_window_dirty) {
8844 vmx->ple_window_dirty = false;
8845 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8846 }
8847
Abel Gordon012f83c2013-04-18 14:39:25 +03008848 if (vmx->nested.sync_shadow_vmcs) {
8849 copy_vmcs12_to_shadow(vmx);
8850 vmx->nested.sync_shadow_vmcs = false;
8851 }
8852
Avi Kivity104f2262010-11-18 13:12:52 +02008853 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8854 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8855 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8856 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8857
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008858 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008859 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8860 vmcs_writel(HOST_CR4, cr4);
8861 vmx->host_state.vmcs_host_cr4 = cr4;
8862 }
8863
Avi Kivity104f2262010-11-18 13:12:52 +02008864 /* When single-stepping over STI and MOV SS, we must clear the
8865 * corresponding interruptibility bits in the guest state. Otherwise
8866 * vmentry fails as it then expects bit 14 (BS) in pending debug
8867 * exceptions being set, but that's not correct for the guest debugging
8868 * case. */
8869 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8870 vmx_set_interrupt_shadow(vcpu, 0);
8871
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008872 if (vmx->guest_pkru_valid)
8873 __write_pkru(vmx->guest_pkru);
8874
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008875 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008876 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008877
Yunhong Jiang64672c92016-06-13 14:19:59 -07008878 vmx_arm_hv_timer(vcpu);
8879
Nadav Har'Eld462b812011-05-24 15:26:10 +03008880 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008881 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008882 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008883 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8884 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8885 "push %%" _ASM_CX " \n\t"
8886 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03008887 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008888 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008889 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03008890 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008891 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008892 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8893 "mov %%cr2, %%" _ASM_DX " \n\t"
8894 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008895 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008896 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008897 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008898 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008899 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008900 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008901 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8902 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8903 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8904 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8905 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8906 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008907#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008908 "mov %c[r8](%0), %%r8 \n\t"
8909 "mov %c[r9](%0), %%r9 \n\t"
8910 "mov %c[r10](%0), %%r10 \n\t"
8911 "mov %c[r11](%0), %%r11 \n\t"
8912 "mov %c[r12](%0), %%r12 \n\t"
8913 "mov %c[r13](%0), %%r13 \n\t"
8914 "mov %c[r14](%0), %%r14 \n\t"
8915 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008916#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008917 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008918
Avi Kivity6aa8b732006-12-10 02:21:36 -08008919 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008920 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008921 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008922 "jmp 2f \n\t"
8923 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8924 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008925 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008926 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008927 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008928 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8929 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8930 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8931 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8932 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8933 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8934 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008935#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008936 "mov %%r8, %c[r8](%0) \n\t"
8937 "mov %%r9, %c[r9](%0) \n\t"
8938 "mov %%r10, %c[r10](%0) \n\t"
8939 "mov %%r11, %c[r11](%0) \n\t"
8940 "mov %%r12, %c[r12](%0) \n\t"
8941 "mov %%r13, %c[r13](%0) \n\t"
8942 "mov %%r14, %c[r14](%0) \n\t"
8943 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008944#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008945 "mov %%cr2, %%" _ASM_AX " \n\t"
8946 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008947
Avi Kivityb188c81f2012-09-16 15:10:58 +03008948 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008949 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008950 ".pushsection .rodata \n\t"
8951 ".global vmx_return \n\t"
8952 "vmx_return: " _ASM_PTR " 2b \n\t"
8953 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008954 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008955 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008956 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03008957 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008958 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8959 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8960 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8961 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8962 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8963 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8964 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008965#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008966 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8967 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8968 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8969 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8970 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8971 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8972 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8973 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008974#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008975 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8976 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02008977 : "cc", "memory"
8978#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03008979 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008980 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008981#else
8982 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008983#endif
8984 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08008985
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008986 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
8987 if (debugctlmsr)
8988 update_debugctlmsr(debugctlmsr);
8989
Avi Kivityaa67f602012-08-01 16:48:03 +03008990#ifndef CONFIG_X86_64
8991 /*
8992 * The sysexit path does not restore ds/es, so we must set them to
8993 * a reasonable value ourselves.
8994 *
8995 * We can't defer this to vmx_load_host_state() since that function
8996 * may be executed in interrupt context, which saves and restore segments
8997 * around it, nullifying its effect.
8998 */
8999 loadsegment(ds, __USER_DS);
9000 loadsegment(es, __USER_DS);
9001#endif
9002
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009003 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009004 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009005 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009006 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009007 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009008 vcpu->arch.regs_dirty = 0;
9009
Avi Kivity1155f762007-11-22 11:30:47 +02009010 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9011
Nadav Har'Eld462b812011-05-24 15:26:10 +03009012 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009013
Avi Kivity51aa01d2010-07-20 14:31:20 +03009014 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009015
Gleb Natapove0b890d2013-09-25 12:51:33 +03009016 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009017 * eager fpu is enabled if PKEY is supported and CR4 is switched
9018 * back on host, so it is safe to read guest PKRU from current
9019 * XSAVE.
9020 */
9021 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9022 vmx->guest_pkru = __read_pkru();
9023 if (vmx->guest_pkru != vmx->host_pkru) {
9024 vmx->guest_pkru_valid = true;
9025 __write_pkru(vmx->host_pkru);
9026 } else
9027 vmx->guest_pkru_valid = false;
9028 }
9029
9030 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009031 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9032 * we did not inject a still-pending event to L1 now because of
9033 * nested_run_pending, we need to re-enable this bit.
9034 */
9035 if (vmx->nested.nested_run_pending)
9036 kvm_make_request(KVM_REQ_EVENT, vcpu);
9037
9038 vmx->nested.nested_run_pending = 0;
9039
Avi Kivity51aa01d2010-07-20 14:31:20 +03009040 vmx_complete_atomic_exit(vmx);
9041 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009042 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009043}
9044
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009045static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
9046{
9047 struct vcpu_vmx *vmx = to_vmx(vcpu);
9048 int cpu;
9049
9050 if (vmx->loaded_vmcs == &vmx->vmcs01)
9051 return;
9052
9053 cpu = get_cpu();
9054 vmx->loaded_vmcs = &vmx->vmcs01;
9055 vmx_vcpu_put(vcpu);
9056 vmx_vcpu_load(vcpu, cpu);
9057 vcpu->cpu = cpu;
9058 put_cpu();
9059}
9060
Jim Mattson2f1fe812016-07-08 15:36:06 -07009061/*
9062 * Ensure that the current vmcs of the logical processor is the
9063 * vmcs01 of the vcpu before calling free_nested().
9064 */
9065static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9066{
9067 struct vcpu_vmx *vmx = to_vmx(vcpu);
9068 int r;
9069
9070 r = vcpu_load(vcpu);
9071 BUG_ON(r);
9072 vmx_load_vmcs01(vcpu);
9073 free_nested(vmx);
9074 vcpu_put(vcpu);
9075}
9076
Avi Kivity6aa8b732006-12-10 02:21:36 -08009077static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9078{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009079 struct vcpu_vmx *vmx = to_vmx(vcpu);
9080
Kai Huang843e4332015-01-28 10:54:28 +08009081 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009082 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009083 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009084 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009085 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009086 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009087 kfree(vmx->guest_msrs);
9088 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009089 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009090}
9091
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009092static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009093{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009094 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009095 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009096 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009097
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009098 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009099 return ERR_PTR(-ENOMEM);
9100
Wanpeng Li991e7a02015-09-16 17:30:05 +08009101 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009102
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009103 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9104 if (err)
9105 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009106
Peter Feiner4e595162016-07-07 14:49:58 -07009107 err = -ENOMEM;
9108
9109 /*
9110 * If PML is turned on, failure on enabling PML just results in failure
9111 * of creating the vcpu, therefore we can simplify PML logic (by
9112 * avoiding dealing with cases, such as enabling PML partially on vcpus
9113 * for the guest, etc.
9114 */
9115 if (enable_pml) {
9116 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9117 if (!vmx->pml_pg)
9118 goto uninit_vcpu;
9119 }
9120
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009121 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009122 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9123 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009124
Peter Feiner4e595162016-07-07 14:49:58 -07009125 if (!vmx->guest_msrs)
9126 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009127
Nadav Har'Eld462b812011-05-24 15:26:10 +03009128 vmx->loaded_vmcs = &vmx->vmcs01;
9129 vmx->loaded_vmcs->vmcs = alloc_vmcs();
9130 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009131 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009132 if (!vmm_exclusive)
9133 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
9134 loaded_vmcs_init(vmx->loaded_vmcs);
9135 if (!vmm_exclusive)
9136 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009137
Avi Kivity15ad7142007-07-11 18:17:21 +03009138 cpu = get_cpu();
9139 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009140 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009141 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009142 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009143 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009144 if (err)
9145 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009146 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009147 err = alloc_apic_access_page(kvm);
9148 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009149 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009150 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009151
Sheng Yangb927a3c2009-07-21 10:42:48 +08009152 if (enable_ept) {
9153 if (!kvm->arch.ept_identity_map_addr)
9154 kvm->arch.ept_identity_map_addr =
9155 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009156 err = init_rmode_identity_map(kvm);
9157 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009158 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009159 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009160
Wanpeng Li5c614b32015-10-13 09:18:36 -07009161 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009162 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009163 vmx->nested.vpid02 = allocate_vpid();
9164 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009165
Wincy Van705699a2015-02-03 23:58:17 +08009166 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009167 vmx->nested.current_vmptr = -1ull;
9168 vmx->nested.current_vmcs12 = NULL;
9169
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009170 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9171
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009172 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009173
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009174free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009175 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009176 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009177free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009178 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009179free_pml:
9180 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009181uninit_vcpu:
9182 kvm_vcpu_uninit(&vmx->vcpu);
9183free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009184 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009185 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009186 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009187}
9188
Yang, Sheng002c7f72007-07-31 14:23:01 +03009189static void __init vmx_check_processor_compat(void *rtn)
9190{
9191 struct vmcs_config vmcs_conf;
9192
9193 *(int *)rtn = 0;
9194 if (setup_vmcs_config(&vmcs_conf) < 0)
9195 *(int *)rtn = -EIO;
9196 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9197 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9198 smp_processor_id());
9199 *(int *)rtn = -EIO;
9200 }
9201}
9202
Sheng Yang67253af2008-04-25 10:20:22 +08009203static int get_ept_level(void)
9204{
9205 return VMX_EPT_DEFAULT_GAW + 1;
9206}
9207
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009208static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009209{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009210 u8 cache;
9211 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009212
Sheng Yang522c68c2009-04-27 20:35:43 +08009213 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009214 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009215 * 2. EPT with VT-d:
9216 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009217 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009218 * b. VT-d with snooping control feature: snooping control feature of
9219 * VT-d engine can guarantee the cache correctness. Just set it
9220 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009221 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009222 * consistent with host MTRR
9223 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009224 if (is_mmio) {
9225 cache = MTRR_TYPE_UNCACHABLE;
9226 goto exit;
9227 }
9228
9229 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009230 ipat = VMX_EPT_IPAT_BIT;
9231 cache = MTRR_TYPE_WRBACK;
9232 goto exit;
9233 }
9234
9235 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9236 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009237 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009238 cache = MTRR_TYPE_WRBACK;
9239 else
9240 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009241 goto exit;
9242 }
9243
Xiao Guangrongff536042015-06-15 16:55:22 +08009244 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009245
9246exit:
9247 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009248}
9249
Sheng Yang17cc3932010-01-05 19:02:27 +08009250static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009251{
Sheng Yang878403b2010-01-05 19:02:29 +08009252 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9253 return PT_DIRECTORY_LEVEL;
9254 else
9255 /* For shadow and EPT supported 1GB page */
9256 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009257}
9258
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009259static void vmcs_set_secondary_exec_control(u32 new_ctl)
9260{
9261 /*
9262 * These bits in the secondary execution controls field
9263 * are dynamic, the others are mostly based on the hypervisor
9264 * architecture and the guest's CPUID. Do not touch the
9265 * dynamic bits.
9266 */
9267 u32 mask =
9268 SECONDARY_EXEC_SHADOW_VMCS |
9269 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9270 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9271
9272 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9273
9274 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9275 (new_ctl & ~mask) | (cur_ctl & mask));
9276}
9277
Sheng Yang0e851882009-12-18 16:48:46 +08009278static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9279{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009280 struct kvm_cpuid_entry2 *best;
9281 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009282 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009283
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009284 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009285 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9286 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009287 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009288
Paolo Bonzini8b972652015-09-15 17:34:42 +02009289 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009290 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009291 vmx->nested.nested_vmx_secondary_ctls_high |=
9292 SECONDARY_EXEC_RDTSCP;
9293 else
9294 vmx->nested.nested_vmx_secondary_ctls_high &=
9295 ~SECONDARY_EXEC_RDTSCP;
9296 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009297 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009298
Mao, Junjiead756a12012-07-02 01:18:48 +00009299 /* Exposing INVPCID only when PCID is exposed */
9300 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9301 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009302 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9303 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009304 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009305
Mao, Junjiead756a12012-07-02 01:18:48 +00009306 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009307 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009308 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009309
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009310 if (cpu_has_secondary_exec_ctrls())
9311 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009312
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009313 if (nested_vmx_allowed(vcpu))
9314 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9315 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9316 else
9317 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9318 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009319}
9320
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009321static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9322{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009323 if (func == 1 && nested)
9324 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009325}
9326
Yang Zhang25d92082013-08-06 12:00:32 +03009327static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9328 struct x86_exception *fault)
9329{
Jan Kiszka533558b2014-01-04 18:47:20 +01009330 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9331 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009332
9333 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009334 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009335 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009336 exit_reason = EXIT_REASON_EPT_VIOLATION;
9337 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009338 vmcs12->guest_physical_address = fault->address;
9339}
9340
Nadav Har'El155a97a2013-08-05 11:07:16 +03009341/* Callbacks for nested_ept_init_mmu_context: */
9342
9343static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9344{
9345 /* return the page table to be shadowed - in our case, EPT12 */
9346 return get_vmcs12(vcpu)->ept_pointer;
9347}
9348
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009349static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009350{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009351 WARN_ON(mmu_is_nested(vcpu));
9352 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009353 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9354 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009355 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9356 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9357 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9358
9359 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009360}
9361
9362static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9363{
9364 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9365}
9366
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009367static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9368 u16 error_code)
9369{
9370 bool inequality, bit;
9371
9372 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9373 inequality =
9374 (error_code & vmcs12->page_fault_error_code_mask) !=
9375 vmcs12->page_fault_error_code_match;
9376 return inequality ^ bit;
9377}
9378
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009379static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9380 struct x86_exception *fault)
9381{
9382 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9383
9384 WARN_ON(!is_guest_mode(vcpu));
9385
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009386 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009387 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9388 vmcs_read32(VM_EXIT_INTR_INFO),
9389 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009390 else
9391 kvm_inject_page_fault(vcpu, fault);
9392}
9393
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009394static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9395 struct vmcs12 *vmcs12)
9396{
9397 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009398 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009399
9400 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009401 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9402 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009403 return false;
9404
9405 /*
9406 * Translate L1 physical address to host physical
9407 * address for vmcs02. Keep the page pinned, so this
9408 * physical address remains valid. We keep a reference
9409 * to it so we can release it later.
9410 */
9411 if (vmx->nested.apic_access_page) /* shouldn't happen */
9412 nested_release_page(vmx->nested.apic_access_page);
9413 vmx->nested.apic_access_page =
9414 nested_get_page(vcpu, vmcs12->apic_access_addr);
9415 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009416
9417 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009418 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9419 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009420 return false;
9421
9422 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9423 nested_release_page(vmx->nested.virtual_apic_page);
9424 vmx->nested.virtual_apic_page =
9425 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9426
9427 /*
9428 * Failing the vm entry is _not_ what the processor does
9429 * but it's basically the only possibility we have.
9430 * We could still enter the guest if CR8 load exits are
9431 * enabled, CR8 store exits are enabled, and virtualize APIC
9432 * access is disabled; in this case the processor would never
9433 * use the TPR shadow and we could simply clear the bit from
9434 * the execution control. But such a configuration is useless,
9435 * so let's keep the code simple.
9436 */
9437 if (!vmx->nested.virtual_apic_page)
9438 return false;
9439 }
9440
Wincy Van705699a2015-02-03 23:58:17 +08009441 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009442 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9443 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009444 return false;
9445
9446 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9447 kunmap(vmx->nested.pi_desc_page);
9448 nested_release_page(vmx->nested.pi_desc_page);
9449 }
9450 vmx->nested.pi_desc_page =
9451 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9452 if (!vmx->nested.pi_desc_page)
9453 return false;
9454
9455 vmx->nested.pi_desc =
9456 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9457 if (!vmx->nested.pi_desc) {
9458 nested_release_page_clean(vmx->nested.pi_desc_page);
9459 return false;
9460 }
9461 vmx->nested.pi_desc =
9462 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9463 (unsigned long)(vmcs12->posted_intr_desc_addr &
9464 (PAGE_SIZE - 1)));
9465 }
9466
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009467 return true;
9468}
9469
Jan Kiszkaf41245002014-03-07 20:03:13 +01009470static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9471{
9472 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9473 struct vcpu_vmx *vmx = to_vmx(vcpu);
9474
9475 if (vcpu->arch.virtual_tsc_khz == 0)
9476 return;
9477
9478 /* Make sure short timeouts reliably trigger an immediate vmexit.
9479 * hrtimer_start does not guarantee this. */
9480 if (preemption_timeout <= 1) {
9481 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9482 return;
9483 }
9484
9485 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9486 preemption_timeout *= 1000000;
9487 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9488 hrtimer_start(&vmx->nested.preemption_timer,
9489 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9490}
9491
Wincy Van3af18d92015-02-03 23:49:31 +08009492static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9493 struct vmcs12 *vmcs12)
9494{
9495 int maxphyaddr;
9496 u64 addr;
9497
9498 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9499 return 0;
9500
9501 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9502 WARN_ON(1);
9503 return -EINVAL;
9504 }
9505 maxphyaddr = cpuid_maxphyaddr(vcpu);
9506
9507 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9508 ((addr + PAGE_SIZE) >> maxphyaddr))
9509 return -EINVAL;
9510
9511 return 0;
9512}
9513
9514/*
9515 * Merge L0's and L1's MSR bitmap, return false to indicate that
9516 * we do not use the hardware.
9517 */
9518static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9519 struct vmcs12 *vmcs12)
9520{
Wincy Van82f0dd42015-02-03 23:57:18 +08009521 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009522 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009523 unsigned long *msr_bitmap_l1;
9524 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +08009525
Radim Krčmářd048c092016-08-08 20:16:22 +02009526 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +08009527 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9528 return false;
9529
9530 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
9531 if (!page) {
9532 WARN_ON(1);
9533 return false;
9534 }
Radim Krčmářd048c092016-08-08 20:16:22 +02009535 msr_bitmap_l1 = (unsigned long *)kmap(page);
9536 if (!msr_bitmap_l1) {
Wincy Vanf2b93282015-02-03 23:56:03 +08009537 nested_release_page_clean(page);
9538 WARN_ON(1);
9539 return false;
9540 }
9541
Radim Krčmářd048c092016-08-08 20:16:22 +02009542 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9543
Wincy Vanf2b93282015-02-03 23:56:03 +08009544 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009545 if (nested_cpu_has_apic_reg_virt(vmcs12))
9546 for (msr = 0x800; msr <= 0x8ff; msr++)
9547 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009548 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009549 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009550
9551 nested_vmx_disable_intercept_for_msr(
9552 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009553 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9554 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009555
Wincy Van608406e2015-02-03 23:57:51 +08009556 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009557 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009558 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009559 APIC_BASE_MSR + (APIC_EOI >> 4),
9560 MSR_TYPE_W);
9561 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009562 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009563 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9564 MSR_TYPE_W);
9565 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009566 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009567 kunmap(page);
9568 nested_release_page_clean(page);
9569
9570 return true;
9571}
9572
9573static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9574 struct vmcs12 *vmcs12)
9575{
Wincy Van82f0dd42015-02-03 23:57:18 +08009576 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009577 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009578 !nested_cpu_has_vid(vmcs12) &&
9579 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009580 return 0;
9581
9582 /*
9583 * If virtualize x2apic mode is enabled,
9584 * virtualize apic access must be disabled.
9585 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009586 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9587 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009588 return -EINVAL;
9589
Wincy Van608406e2015-02-03 23:57:51 +08009590 /*
9591 * If virtual interrupt delivery is enabled,
9592 * we must exit on external interrupts.
9593 */
9594 if (nested_cpu_has_vid(vmcs12) &&
9595 !nested_exit_on_intr(vcpu))
9596 return -EINVAL;
9597
Wincy Van705699a2015-02-03 23:58:17 +08009598 /*
9599 * bits 15:8 should be zero in posted_intr_nv,
9600 * the descriptor address has been already checked
9601 * in nested_get_vmcs12_pages.
9602 */
9603 if (nested_cpu_has_posted_intr(vmcs12) &&
9604 (!nested_cpu_has_vid(vmcs12) ||
9605 !nested_exit_intr_ack_set(vcpu) ||
9606 vmcs12->posted_intr_nv & 0xff00))
9607 return -EINVAL;
9608
Wincy Vanf2b93282015-02-03 23:56:03 +08009609 /* tpr shadow is needed by all apicv features. */
9610 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9611 return -EINVAL;
9612
9613 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009614}
9615
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009616static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9617 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009618 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009619{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009620 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009621 u64 count, addr;
9622
9623 if (vmcs12_read_any(vcpu, count_field, &count) ||
9624 vmcs12_read_any(vcpu, addr_field, &addr)) {
9625 WARN_ON(1);
9626 return -EINVAL;
9627 }
9628 if (count == 0)
9629 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009630 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009631 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9632 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009633 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009634 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9635 addr_field, maxphyaddr, count, addr);
9636 return -EINVAL;
9637 }
9638 return 0;
9639}
9640
9641static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9642 struct vmcs12 *vmcs12)
9643{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009644 if (vmcs12->vm_exit_msr_load_count == 0 &&
9645 vmcs12->vm_exit_msr_store_count == 0 &&
9646 vmcs12->vm_entry_msr_load_count == 0)
9647 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009648 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009649 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009650 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009651 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009652 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009653 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009654 return -EINVAL;
9655 return 0;
9656}
9657
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009658static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9659 struct vmx_msr_entry *e)
9660{
9661 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009662 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009663 return -EINVAL;
9664 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9665 e->index == MSR_IA32_UCODE_REV)
9666 return -EINVAL;
9667 if (e->reserved != 0)
9668 return -EINVAL;
9669 return 0;
9670}
9671
9672static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9673 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009674{
9675 if (e->index == MSR_FS_BASE ||
9676 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009677 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9678 nested_vmx_msr_check_common(vcpu, e))
9679 return -EINVAL;
9680 return 0;
9681}
9682
9683static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9684 struct vmx_msr_entry *e)
9685{
9686 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9687 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009688 return -EINVAL;
9689 return 0;
9690}
9691
9692/*
9693 * Load guest's/host's msr at nested entry/exit.
9694 * return 0 for success, entry index for failure.
9695 */
9696static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9697{
9698 u32 i;
9699 struct vmx_msr_entry e;
9700 struct msr_data msr;
9701
9702 msr.host_initiated = false;
9703 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009704 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9705 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009706 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009707 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9708 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009709 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009710 }
9711 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009712 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009713 "%s check failed (%u, 0x%x, 0x%x)\n",
9714 __func__, i, e.index, e.reserved);
9715 goto fail;
9716 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009717 msr.index = e.index;
9718 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009719 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009720 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009721 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9722 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009723 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009724 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009725 }
9726 return 0;
9727fail:
9728 return i + 1;
9729}
9730
9731static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9732{
9733 u32 i;
9734 struct vmx_msr_entry e;
9735
9736 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009737 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009738 if (kvm_vcpu_read_guest(vcpu,
9739 gpa + i * sizeof(e),
9740 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009741 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009742 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9743 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009744 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009745 }
9746 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009747 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009748 "%s check failed (%u, 0x%x, 0x%x)\n",
9749 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009750 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009751 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009752 msr_info.host_initiated = false;
9753 msr_info.index = e.index;
9754 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009755 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009756 "%s cannot read MSR (%u, 0x%x)\n",
9757 __func__, i, e.index);
9758 return -EINVAL;
9759 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009760 if (kvm_vcpu_write_guest(vcpu,
9761 gpa + i * sizeof(e) +
9762 offsetof(struct vmx_msr_entry, value),
9763 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009764 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009765 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009766 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009767 return -EINVAL;
9768 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009769 }
9770 return 0;
9771}
9772
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009773/*
9774 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9775 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009776 * 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 +03009777 * guest in a way that will both be appropriate to L1's requests, and our
9778 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9779 * function also has additional necessary side-effects, like setting various
9780 * vcpu->arch fields.
9781 */
9782static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9783{
9784 struct vcpu_vmx *vmx = to_vmx(vcpu);
9785 u32 exec_control;
9786
9787 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9788 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9789 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9790 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9791 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9792 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9793 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9794 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9795 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9796 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9797 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9798 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9799 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9800 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9801 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9802 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9803 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9804 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9805 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9806 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9807 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9808 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9809 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9810 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9811 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9812 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9813 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9814 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9815 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9816 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9817 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9818 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9819 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9820 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9821 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9822 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9823
Jan Kiszka2996fca2014-06-16 13:59:43 +02009824 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9825 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9826 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9827 } else {
9828 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9829 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9830 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009831 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9832 vmcs12->vm_entry_intr_info_field);
9833 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9834 vmcs12->vm_entry_exception_error_code);
9835 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9836 vmcs12->vm_entry_instruction_len);
9837 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9838 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009839 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009840 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009841 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9842 vmcs12->guest_pending_dbg_exceptions);
9843 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9844 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9845
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009846 if (nested_cpu_has_xsaves(vmcs12))
9847 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009848 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9849
Jan Kiszkaf41245002014-03-07 20:03:13 +01009850 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009851
Paolo Bonzini9314006db2016-07-06 13:23:51 +02009852 /* Preemption timer setting is only taken from vmcs01. */
9853 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9854 exec_control |= vmcs_config.pin_based_exec_ctrl;
9855 if (vmx->hv_deadline_tsc == -1)
9856 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9857
9858 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +08009859 if (nested_cpu_has_posted_intr(vmcs12)) {
9860 /*
9861 * Note that we use L0's vector here and in
9862 * vmx_deliver_nested_posted_interrupt.
9863 */
9864 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9865 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +08009866 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +08009867 vmcs_write64(POSTED_INTR_DESC_ADDR,
9868 page_to_phys(vmx->nested.pi_desc_page) +
9869 (unsigned long)(vmcs12->posted_intr_desc_addr &
9870 (PAGE_SIZE - 1)));
9871 } else
9872 exec_control &= ~PIN_BASED_POSTED_INTR;
9873
Jan Kiszkaf41245002014-03-07 20:03:13 +01009874 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009875
Jan Kiszkaf41245002014-03-07 20:03:13 +01009876 vmx->nested.preemption_timer_expired = false;
9877 if (nested_cpu_has_preemption_timer(vmcs12))
9878 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01009879
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009880 /*
9881 * Whether page-faults are trapped is determined by a combination of
9882 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
9883 * If enable_ept, L0 doesn't care about page faults and we should
9884 * set all of these to L1's desires. However, if !enable_ept, L0 does
9885 * care about (at least some) page faults, and because it is not easy
9886 * (if at all possible?) to merge L0 and L1's desires, we simply ask
9887 * to exit on each and every L2 page fault. This is done by setting
9888 * MASK=MATCH=0 and (see below) EB.PF=1.
9889 * Note that below we don't need special code to set EB.PF beyond the
9890 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
9891 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
9892 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
9893 *
9894 * A problem with this approach (when !enable_ept) is that L1 may be
9895 * injected with more page faults than it asked for. This could have
9896 * caused problems, but in practice existing hypervisors don't care.
9897 * To fix this, we will need to emulate the PFEC checking (on the L1
9898 * page tables), using walk_addr(), when injecting PFs to L1.
9899 */
9900 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
9901 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
9902 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
9903 enable_ept ? vmcs12->page_fault_error_code_match : 0);
9904
9905 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +01009906 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +08009907
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009908 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009909 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009910 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009911 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -07009912 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009913 if (nested_cpu_has(vmcs12,
9914 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
9915 exec_control |= vmcs12->secondary_vm_exec_control;
9916
9917 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
9918 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009919 * If translation failed, no matter: This feature asks
9920 * to exit when accessing the given address, and if it
9921 * can never be accessed, this feature won't do
9922 * anything anyway.
9923 */
9924 if (!vmx->nested.apic_access_page)
9925 exec_control &=
9926 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9927 else
9928 vmcs_write64(APIC_ACCESS_ADDR,
9929 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +08009930 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +02009931 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +01009932 exec_control |=
9933 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +08009934 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009935 }
9936
Wincy Van608406e2015-02-03 23:57:51 +08009937 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
9938 vmcs_write64(EOI_EXIT_BITMAP0,
9939 vmcs12->eoi_exit_bitmap0);
9940 vmcs_write64(EOI_EXIT_BITMAP1,
9941 vmcs12->eoi_exit_bitmap1);
9942 vmcs_write64(EOI_EXIT_BITMAP2,
9943 vmcs12->eoi_exit_bitmap2);
9944 vmcs_write64(EOI_EXIT_BITMAP3,
9945 vmcs12->eoi_exit_bitmap3);
9946 vmcs_write16(GUEST_INTR_STATUS,
9947 vmcs12->guest_intr_status);
9948 }
9949
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009950 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
9951 }
9952
9953
9954 /*
9955 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
9956 * Some constant fields are set here by vmx_set_constant_host_state().
9957 * Other fields are different per CPU, and will be set later when
9958 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
9959 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08009960 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009961
9962 /*
9963 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
9964 * entry, but only if the current (host) sp changed from the value
9965 * we wrote last (vmx->host_rsp). This cache is no longer relevant
9966 * if we switch vmcs, and rather than hold a separate cache per vmcs,
9967 * here we just force the write to happen on entry.
9968 */
9969 vmx->host_rsp = 0;
9970
9971 exec_control = vmx_exec_control(vmx); /* L0's desires */
9972 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
9973 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
9974 exec_control &= ~CPU_BASED_TPR_SHADOW;
9975 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009976
9977 if (exec_control & CPU_BASED_TPR_SHADOW) {
9978 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
9979 page_to_phys(vmx->nested.virtual_apic_page));
9980 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
9981 }
9982
Wincy Van3af18d92015-02-03 23:49:31 +08009983 if (cpu_has_vmx_msr_bitmap() &&
Radim Krčmářd048c092016-08-08 20:16:22 +02009984 exec_control & CPU_BASED_USE_MSR_BITMAPS &&
9985 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9986 ; /* MSR_BITMAP will be set by following vmx_set_efer. */
9987 else
Wincy Van3af18d92015-02-03 23:49:31 +08009988 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
9989
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009990 /*
Wincy Van3af18d92015-02-03 23:49:31 +08009991 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009992 * Rather, exit every time.
9993 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009994 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
9995 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
9996
9997 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
9998
9999 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10000 * bitwise-or of what L1 wants to trap for L2, and what we want to
10001 * trap. Note that CR0.TS also needs updating - we do this later.
10002 */
10003 update_exception_bitmap(vcpu);
10004 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10005 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10006
Nadav Har'El8049d652013-08-05 11:07:06 +030010007 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10008 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10009 * bits are further modified by vmx_set_efer() below.
10010 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010011 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010012
10013 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10014 * emulated by vmx_set_efer(), below.
10015 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010016 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010017 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10018 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010019 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10020
Jan Kiszka44811c02013-08-04 17:17:27 +020010021 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010022 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010023 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10024 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010025 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10026
10027
10028 set_cr4_guest_host_mask(vmx);
10029
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010030 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10031 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10032
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010033 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10034 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010035 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010036 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010037 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010038 if (kvm_has_tsc_control)
10039 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010040
10041 if (enable_vpid) {
10042 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010043 * There is no direct mapping between vpid02 and vpid12, the
10044 * vpid02 is per-vCPU for L0 and reused while the value of
10045 * vpid12 is changed w/ one invvpid during nested vmentry.
10046 * The vpid12 is allocated by L1 for L2, so it will not
10047 * influence global bitmap(for vpid01 and vpid02 allocation)
10048 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010049 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010050 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10051 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10052 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10053 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10054 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10055 }
10056 } else {
10057 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10058 vmx_flush_tlb(vcpu);
10059 }
10060
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010061 }
10062
Nadav Har'El155a97a2013-08-05 11:07:16 +030010063 if (nested_cpu_has_ept(vmcs12)) {
10064 kvm_mmu_unload(vcpu);
10065 nested_ept_init_mmu_context(vcpu);
10066 }
10067
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010068 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10069 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010070 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010071 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10072 else
10073 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10074 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10075 vmx_set_efer(vcpu, vcpu->arch.efer);
10076
10077 /*
10078 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10079 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10080 * The CR0_READ_SHADOW is what L2 should have expected to read given
10081 * the specifications by L1; It's not enough to take
10082 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10083 * have more bits than L1 expected.
10084 */
10085 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10086 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10087
10088 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10089 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10090
10091 /* shadow page tables on either EPT or shadow page tables */
10092 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10093 kvm_mmu_reset_context(vcpu);
10094
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010095 if (!enable_ept)
10096 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10097
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010098 /*
10099 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10100 */
10101 if (enable_ept) {
10102 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10103 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10104 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10105 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10106 }
10107
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010108 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10109 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10110}
10111
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010112/*
10113 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10114 * for running an L2 nested guest.
10115 */
10116static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10117{
10118 struct vmcs12 *vmcs12;
10119 struct vcpu_vmx *vmx = to_vmx(vcpu);
10120 int cpu;
10121 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +020010122 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010123 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010124
10125 if (!nested_vmx_check_permission(vcpu) ||
10126 !nested_vmx_check_vmcs12(vcpu))
10127 return 1;
10128
10129 skip_emulated_instruction(vcpu);
10130 vmcs12 = get_vmcs12(vcpu);
10131
Abel Gordon012f83c2013-04-18 14:39:25 +030010132 if (enable_shadow_vmcs)
10133 copy_shadow_to_vmcs12(vmx);
10134
Nadav Har'El7c177932011-05-25 23:12:04 +030010135 /*
10136 * The nested entry process starts with enforcing various prerequisites
10137 * on vmcs12 as required by the Intel SDM, and act appropriately when
10138 * they fail: As the SDM explains, some conditions should cause the
10139 * instruction to fail, while others will cause the instruction to seem
10140 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10141 * To speed up the normal (success) code path, we should avoid checking
10142 * for misconfigurations which will anyway be caught by the processor
10143 * when using the merged vmcs02.
10144 */
10145 if (vmcs12->launch_state == launch) {
10146 nested_vmx_failValid(vcpu,
10147 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10148 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10149 return 1;
10150 }
10151
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010152 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10153 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010154 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10155 return 1;
10156 }
10157
Wincy Van3af18d92015-02-03 23:49:31 +080010158 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010159 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10160 return 1;
10161 }
10162
Wincy Van3af18d92015-02-03 23:49:31 +080010163 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010164 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10165 return 1;
10166 }
10167
Wincy Vanf2b93282015-02-03 23:56:03 +080010168 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10169 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10170 return 1;
10171 }
10172
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010173 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10174 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10175 return 1;
10176 }
10177
Nadav Har'El7c177932011-05-25 23:12:04 +030010178 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010179 vmx->nested.nested_vmx_true_procbased_ctls_low,
10180 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010181 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010182 vmx->nested.nested_vmx_secondary_ctls_low,
10183 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010184 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010185 vmx->nested.nested_vmx_pinbased_ctls_low,
10186 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010187 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010188 vmx->nested.nested_vmx_true_exit_ctls_low,
10189 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010190 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010191 vmx->nested.nested_vmx_true_entry_ctls_low,
10192 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010193 {
10194 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10195 return 1;
10196 }
10197
10198 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10199 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10200 nested_vmx_failValid(vcpu,
10201 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10202 return 1;
10203 }
10204
Wincy Vanb9c237b2015-02-03 23:56:30 +080010205 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010206 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10207 nested_vmx_entry_failure(vcpu, vmcs12,
10208 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10209 return 1;
10210 }
10211 if (vmcs12->vmcs_link_pointer != -1ull) {
10212 nested_vmx_entry_failure(vcpu, vmcs12,
10213 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10214 return 1;
10215 }
10216
10217 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010218 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010219 * are performed on the field for the IA32_EFER MSR:
10220 * - Bits reserved in the IA32_EFER MSR must be 0.
10221 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10222 * the IA-32e mode guest VM-exit control. It must also be identical
10223 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10224 * CR0.PG) is 1.
10225 */
10226 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10227 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10228 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10229 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10230 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10231 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10232 nested_vmx_entry_failure(vcpu, vmcs12,
10233 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10234 return 1;
10235 }
10236 }
10237
10238 /*
10239 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10240 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10241 * the values of the LMA and LME bits in the field must each be that of
10242 * the host address-space size VM-exit control.
10243 */
10244 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10245 ia32e = (vmcs12->vm_exit_controls &
10246 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10247 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10248 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10249 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10250 nested_vmx_entry_failure(vcpu, vmcs12,
10251 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10252 return 1;
10253 }
10254 }
10255
10256 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010257 * We're finally done with prerequisite checking, and can start with
10258 * the nested entry.
10259 */
10260
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010261 vmcs02 = nested_get_current_vmcs02(vmx);
10262 if (!vmcs02)
10263 return -ENOMEM;
10264
10265 enter_guest_mode(vcpu);
10266
Jan Kiszka2996fca2014-06-16 13:59:43 +020010267 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10268 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10269
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010270 cpu = get_cpu();
10271 vmx->loaded_vmcs = vmcs02;
10272 vmx_vcpu_put(vcpu);
10273 vmx_vcpu_load(vcpu, cpu);
10274 vcpu->cpu = cpu;
10275 put_cpu();
10276
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010277 vmx_segment_cache_clear(vmx);
10278
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010279 prepare_vmcs02(vcpu, vmcs12);
10280
Wincy Vanff651cb2014-12-11 08:52:58 +030010281 msr_entry_idx = nested_vmx_load_msr(vcpu,
10282 vmcs12->vm_entry_msr_load_addr,
10283 vmcs12->vm_entry_msr_load_count);
10284 if (msr_entry_idx) {
10285 leave_guest_mode(vcpu);
10286 vmx_load_vmcs01(vcpu);
10287 nested_vmx_entry_failure(vcpu, vmcs12,
10288 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10289 return 1;
10290 }
10291
10292 vmcs12->launch_state = 1;
10293
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010294 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010295 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010296
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010297 vmx->nested.nested_run_pending = 1;
10298
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010299 /*
10300 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10301 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10302 * returned as far as L1 is concerned. It will only return (and set
10303 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10304 */
10305 return 1;
10306}
10307
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010308/*
10309 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10310 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10311 * This function returns the new value we should put in vmcs12.guest_cr0.
10312 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10313 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10314 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10315 * didn't trap the bit, because if L1 did, so would L0).
10316 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10317 * been modified by L2, and L1 knows it. So just leave the old value of
10318 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10319 * isn't relevant, because if L0 traps this bit it can set it to anything.
10320 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10321 * changed these bits, and therefore they need to be updated, but L0
10322 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10323 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10324 */
10325static inline unsigned long
10326vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10327{
10328 return
10329 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10330 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10331 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10332 vcpu->arch.cr0_guest_owned_bits));
10333}
10334
10335static inline unsigned long
10336vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10337{
10338 return
10339 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10340 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10341 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10342 vcpu->arch.cr4_guest_owned_bits));
10343}
10344
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010345static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10346 struct vmcs12 *vmcs12)
10347{
10348 u32 idt_vectoring;
10349 unsigned int nr;
10350
Gleb Natapov851eb6672013-09-25 12:51:34 +030010351 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010352 nr = vcpu->arch.exception.nr;
10353 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10354
10355 if (kvm_exception_is_soft(nr)) {
10356 vmcs12->vm_exit_instruction_len =
10357 vcpu->arch.event_exit_inst_len;
10358 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10359 } else
10360 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10361
10362 if (vcpu->arch.exception.has_error_code) {
10363 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10364 vmcs12->idt_vectoring_error_code =
10365 vcpu->arch.exception.error_code;
10366 }
10367
10368 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010369 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010370 vmcs12->idt_vectoring_info_field =
10371 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10372 } else if (vcpu->arch.interrupt.pending) {
10373 nr = vcpu->arch.interrupt.nr;
10374 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10375
10376 if (vcpu->arch.interrupt.soft) {
10377 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10378 vmcs12->vm_entry_instruction_len =
10379 vcpu->arch.event_exit_inst_len;
10380 } else
10381 idt_vectoring |= INTR_TYPE_EXT_INTR;
10382
10383 vmcs12->idt_vectoring_info_field = idt_vectoring;
10384 }
10385}
10386
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010387static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10388{
10389 struct vcpu_vmx *vmx = to_vmx(vcpu);
10390
Jan Kiszkaf41245002014-03-07 20:03:13 +010010391 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10392 vmx->nested.preemption_timer_expired) {
10393 if (vmx->nested.nested_run_pending)
10394 return -EBUSY;
10395 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10396 return 0;
10397 }
10398
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010399 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010400 if (vmx->nested.nested_run_pending ||
10401 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010402 return -EBUSY;
10403 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10404 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10405 INTR_INFO_VALID_MASK, 0);
10406 /*
10407 * The NMI-triggered VM exit counts as injection:
10408 * clear this one and block further NMIs.
10409 */
10410 vcpu->arch.nmi_pending = 0;
10411 vmx_set_nmi_mask(vcpu, true);
10412 return 0;
10413 }
10414
10415 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10416 nested_exit_on_intr(vcpu)) {
10417 if (vmx->nested.nested_run_pending)
10418 return -EBUSY;
10419 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010420 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010421 }
10422
Wincy Van705699a2015-02-03 23:58:17 +080010423 return vmx_complete_nested_posted_interrupt(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010424}
10425
Jan Kiszkaf41245002014-03-07 20:03:13 +010010426static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10427{
10428 ktime_t remaining =
10429 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10430 u64 value;
10431
10432 if (ktime_to_ns(remaining) <= 0)
10433 return 0;
10434
10435 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10436 do_div(value, 1000000);
10437 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10438}
10439
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010440/*
10441 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10442 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10443 * and this function updates it to reflect the changes to the guest state while
10444 * L2 was running (and perhaps made some exits which were handled directly by L0
10445 * without going back to L1), and to reflect the exit reason.
10446 * Note that we do not have to copy here all VMCS fields, just those that
10447 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10448 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10449 * which already writes to vmcs12 directly.
10450 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010451static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10452 u32 exit_reason, u32 exit_intr_info,
10453 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010454{
10455 /* update guest state fields: */
10456 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10457 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10458
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010459 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10460 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10461 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10462
10463 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10464 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10465 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10466 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10467 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10468 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10469 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10470 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10471 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10472 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10473 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10474 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10475 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10476 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10477 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10478 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10479 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10480 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10481 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10482 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10483 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10484 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10485 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10486 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10487 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10488 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10489 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10490 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10491 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10492 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10493 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10494 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10495 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10496 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10497 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10498 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10499
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010500 vmcs12->guest_interruptibility_info =
10501 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10502 vmcs12->guest_pending_dbg_exceptions =
10503 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010504 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10505 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10506 else
10507 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010508
Jan Kiszkaf41245002014-03-07 20:03:13 +010010509 if (nested_cpu_has_preemption_timer(vmcs12)) {
10510 if (vmcs12->vm_exit_controls &
10511 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10512 vmcs12->vmx_preemption_timer_value =
10513 vmx_get_preemption_timer_value(vcpu);
10514 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10515 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010516
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010517 /*
10518 * In some cases (usually, nested EPT), L2 is allowed to change its
10519 * own CR3 without exiting. If it has changed it, we must keep it.
10520 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10521 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10522 *
10523 * Additionally, restore L2's PDPTR to vmcs12.
10524 */
10525 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010526 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010527 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10528 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10529 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10530 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10531 }
10532
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010533 if (nested_cpu_has_ept(vmcs12))
10534 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
10535
Wincy Van608406e2015-02-03 23:57:51 +080010536 if (nested_cpu_has_vid(vmcs12))
10537 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10538
Jan Kiszkac18911a2013-03-13 16:06:41 +010010539 vmcs12->vm_entry_controls =
10540 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010541 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010542
Jan Kiszka2996fca2014-06-16 13:59:43 +020010543 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10544 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10545 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10546 }
10547
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010548 /* TODO: These cannot have changed unless we have MSR bitmaps and
10549 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010550 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010551 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010552 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10553 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010554 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10555 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10556 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010557 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010558 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010559 if (nested_cpu_has_xsaves(vmcs12))
10560 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010561
10562 /* update exit information fields: */
10563
Jan Kiszka533558b2014-01-04 18:47:20 +010010564 vmcs12->vm_exit_reason = exit_reason;
10565 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010566
Jan Kiszka533558b2014-01-04 18:47:20 +010010567 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010568 if ((vmcs12->vm_exit_intr_info &
10569 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10570 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10571 vmcs12->vm_exit_intr_error_code =
10572 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010573 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010574 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10575 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10576
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010577 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10578 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10579 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010580 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010581
10582 /*
10583 * Transfer the event that L0 or L1 may wanted to inject into
10584 * L2 to IDT_VECTORING_INFO_FIELD.
10585 */
10586 vmcs12_save_pending_event(vcpu, vmcs12);
10587 }
10588
10589 /*
10590 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10591 * preserved above and would only end up incorrectly in L1.
10592 */
10593 vcpu->arch.nmi_injected = false;
10594 kvm_clear_exception_queue(vcpu);
10595 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010596}
10597
10598/*
10599 * A part of what we need to when the nested L2 guest exits and we want to
10600 * run its L1 parent, is to reset L1's guest state to the host state specified
10601 * in vmcs12.
10602 * This function is to be called not only on normal nested exit, but also on
10603 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10604 * Failures During or After Loading Guest State").
10605 * This function should be called when the active VMCS is L1's (vmcs01).
10606 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010607static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10608 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010609{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010610 struct kvm_segment seg;
10611
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010612 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10613 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010614 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010615 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10616 else
10617 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10618 vmx_set_efer(vcpu, vcpu->arch.efer);
10619
10620 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10621 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010622 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010623 /*
10624 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10625 * actually changed, because it depends on the current state of
10626 * fpu_active (which may have changed).
10627 * Note that vmx_set_cr0 refers to efer set above.
10628 */
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020010629 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010630 /*
10631 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10632 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10633 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10634 */
10635 update_exception_bitmap(vcpu);
10636 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10637 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10638
10639 /*
10640 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10641 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10642 */
10643 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10644 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10645
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010646 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010647
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010648 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10649 kvm_mmu_reset_context(vcpu);
10650
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010651 if (!enable_ept)
10652 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10653
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010654 if (enable_vpid) {
10655 /*
10656 * Trivially support vpid by letting L2s share their parent
10657 * L1's vpid. TODO: move to a more elaborate solution, giving
10658 * each L2 its own vpid and exposing the vpid feature to L1.
10659 */
10660 vmx_flush_tlb(vcpu);
10661 }
10662
10663
10664 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10665 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10666 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10667 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10668 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010669
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010670 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10671 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10672 vmcs_write64(GUEST_BNDCFGS, 0);
10673
Jan Kiszka44811c02013-08-04 17:17:27 +020010674 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010675 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010676 vcpu->arch.pat = vmcs12->host_ia32_pat;
10677 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010678 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10679 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10680 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010681
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010682 /* Set L1 segment info according to Intel SDM
10683 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10684 seg = (struct kvm_segment) {
10685 .base = 0,
10686 .limit = 0xFFFFFFFF,
10687 .selector = vmcs12->host_cs_selector,
10688 .type = 11,
10689 .present = 1,
10690 .s = 1,
10691 .g = 1
10692 };
10693 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10694 seg.l = 1;
10695 else
10696 seg.db = 1;
10697 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10698 seg = (struct kvm_segment) {
10699 .base = 0,
10700 .limit = 0xFFFFFFFF,
10701 .type = 3,
10702 .present = 1,
10703 .s = 1,
10704 .db = 1,
10705 .g = 1
10706 };
10707 seg.selector = vmcs12->host_ds_selector;
10708 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10709 seg.selector = vmcs12->host_es_selector;
10710 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10711 seg.selector = vmcs12->host_ss_selector;
10712 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10713 seg.selector = vmcs12->host_fs_selector;
10714 seg.base = vmcs12->host_fs_base;
10715 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10716 seg.selector = vmcs12->host_gs_selector;
10717 seg.base = vmcs12->host_gs_base;
10718 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10719 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010720 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010721 .limit = 0x67,
10722 .selector = vmcs12->host_tr_selector,
10723 .type = 11,
10724 .present = 1
10725 };
10726 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10727
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010728 kvm_set_dr(vcpu, 7, 0x400);
10729 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010730
Wincy Van3af18d92015-02-03 23:49:31 +080010731 if (cpu_has_vmx_msr_bitmap())
10732 vmx_set_msr_bitmap(vcpu);
10733
Wincy Vanff651cb2014-12-11 08:52:58 +030010734 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10735 vmcs12->vm_exit_msr_load_count))
10736 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010737}
10738
10739/*
10740 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10741 * and modify vmcs12 to make it see what it would expect to see there if
10742 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10743 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010744static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10745 u32 exit_intr_info,
10746 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010747{
10748 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010749 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10750
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010751 /* trying to cancel vmlaunch/vmresume is a bug */
10752 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10753
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010754 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010755 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10756 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010757
Wincy Vanff651cb2014-12-11 08:52:58 +030010758 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10759 vmcs12->vm_exit_msr_store_count))
10760 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10761
Wanpeng Lif3380ca52014-08-05 12:42:23 +080010762 vmx_load_vmcs01(vcpu);
10763
Bandan Das77b0f5d2014-04-19 18:17:45 -040010764 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10765 && nested_exit_intr_ack_set(vcpu)) {
10766 int irq = kvm_cpu_get_interrupt(vcpu);
10767 WARN_ON(irq < 0);
10768 vmcs12->vm_exit_intr_info = irq |
10769 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10770 }
10771
Jan Kiszka542060e2014-01-04 18:47:21 +010010772 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10773 vmcs12->exit_qualification,
10774 vmcs12->idt_vectoring_info_field,
10775 vmcs12->vm_exit_intr_info,
10776 vmcs12->vm_exit_intr_error_code,
10777 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010778
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010779 vm_entry_controls_reset_shadow(vmx);
10780 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010781 vmx_segment_cache_clear(vmx);
10782
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010783 /* if no vmcs02 cache requested, remove the one we used */
10784 if (VMCS02_POOL_SIZE == 0)
10785 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10786
10787 load_vmcs12_host_state(vcpu, vmcs12);
10788
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010789 /* Update any VMCS fields that might have changed while L2 ran */
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010790 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010791 if (vmx->hv_deadline_tsc == -1)
10792 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10793 PIN_BASED_VMX_PREEMPTION_TIMER);
10794 else
10795 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10796 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070010797 if (kvm_has_tsc_control)
10798 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010799
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010800 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10801 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10802 vmx_set_virtual_x2apic_mode(vcpu,
10803 vcpu->arch.apic_base & X2APIC_ENABLE);
10804 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010805
10806 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10807 vmx->host_rsp = 0;
10808
10809 /* Unpin physical memory we referred to in vmcs02 */
10810 if (vmx->nested.apic_access_page) {
10811 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010812 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010813 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010814 if (vmx->nested.virtual_apic_page) {
10815 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010816 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010817 }
Wincy Van705699a2015-02-03 23:58:17 +080010818 if (vmx->nested.pi_desc_page) {
10819 kunmap(vmx->nested.pi_desc_page);
10820 nested_release_page(vmx->nested.pi_desc_page);
10821 vmx->nested.pi_desc_page = NULL;
10822 vmx->nested.pi_desc = NULL;
10823 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010824
10825 /*
Tang Chen38b99172014-09-24 15:57:54 +080010826 * We are now running in L2, mmu_notifier will force to reload the
10827 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10828 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080010829 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080010830
10831 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010832 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10833 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10834 * success or failure flag accordingly.
10835 */
10836 if (unlikely(vmx->fail)) {
10837 vmx->fail = 0;
10838 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
10839 } else
10840 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010841 if (enable_shadow_vmcs)
10842 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010843
10844 /* in case we halted in L2 */
10845 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010846}
10847
Nadav Har'El7c177932011-05-25 23:12:04 +030010848/*
Jan Kiszka42124922014-01-04 18:47:19 +010010849 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10850 */
10851static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10852{
10853 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +010010854 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +010010855 free_nested(to_vmx(vcpu));
10856}
10857
10858/*
Nadav Har'El7c177932011-05-25 23:12:04 +030010859 * L1's failure to enter L2 is a subset of a normal exit, as explained in
10860 * 23.7 "VM-entry failures during or after loading guest state" (this also
10861 * lists the acceptable exit-reason and exit-qualification parameters).
10862 * It should only be called before L2 actually succeeded to run, and when
10863 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
10864 */
10865static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
10866 struct vmcs12 *vmcs12,
10867 u32 reason, unsigned long qualification)
10868{
10869 load_vmcs12_host_state(vcpu, vmcs12);
10870 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
10871 vmcs12->exit_qualification = qualification;
10872 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010873 if (enable_shadow_vmcs)
10874 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030010875}
10876
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010877static int vmx_check_intercept(struct kvm_vcpu *vcpu,
10878 struct x86_instruction_info *info,
10879 enum x86_intercept_stage stage)
10880{
10881 return X86EMUL_CONTINUE;
10882}
10883
Yunhong Jiang64672c92016-06-13 14:19:59 -070010884#ifdef CONFIG_X86_64
10885/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
10886static inline int u64_shl_div_u64(u64 a, unsigned int shift,
10887 u64 divisor, u64 *result)
10888{
10889 u64 low = a << shift, high = a >> (64 - shift);
10890
10891 /* To avoid the overflow on divq */
10892 if (high >= divisor)
10893 return 1;
10894
10895 /* Low hold the result, high hold rem which is discarded */
10896 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
10897 "rm" (divisor), "0" (low), "1" (high));
10898 *result = low;
10899
10900 return 0;
10901}
10902
10903static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
10904{
10905 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020010906 u64 tscl = rdtsc();
10907 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
10908 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010909
10910 /* Convert to host delta tsc if tsc scaling is enabled */
10911 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
10912 u64_shl_div_u64(delta_tsc,
10913 kvm_tsc_scaling_ratio_frac_bits,
10914 vcpu->arch.tsc_scaling_ratio,
10915 &delta_tsc))
10916 return -ERANGE;
10917
10918 /*
10919 * If the delta tsc can't fit in the 32 bit after the multi shift,
10920 * we can't use the preemption timer.
10921 * It's possible that it fits on later vmentries, but checking
10922 * on every vmentry is costly so we just use an hrtimer.
10923 */
10924 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
10925 return -ERANGE;
10926
10927 vmx->hv_deadline_tsc = tscl + delta_tsc;
10928 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10929 PIN_BASED_VMX_PREEMPTION_TIMER);
10930 return 0;
10931}
10932
10933static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
10934{
10935 struct vcpu_vmx *vmx = to_vmx(vcpu);
10936 vmx->hv_deadline_tsc = -1;
10937 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10938 PIN_BASED_VMX_PREEMPTION_TIMER);
10939}
10940#endif
10941
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010942static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010943{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020010944 if (ple_gap)
10945 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010946}
10947
Kai Huang843e4332015-01-28 10:54:28 +080010948static void vmx_slot_enable_log_dirty(struct kvm *kvm,
10949 struct kvm_memory_slot *slot)
10950{
10951 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
10952 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
10953}
10954
10955static void vmx_slot_disable_log_dirty(struct kvm *kvm,
10956 struct kvm_memory_slot *slot)
10957{
10958 kvm_mmu_slot_set_dirty(kvm, slot);
10959}
10960
10961static void vmx_flush_log_dirty(struct kvm *kvm)
10962{
10963 kvm_flush_pml_buffers(kvm);
10964}
10965
10966static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
10967 struct kvm_memory_slot *memslot,
10968 gfn_t offset, unsigned long mask)
10969{
10970 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
10971}
10972
Feng Wuefc64402015-09-18 22:29:51 +080010973/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080010974 * This routine does the following things for vCPU which is going
10975 * to be blocked if VT-d PI is enabled.
10976 * - Store the vCPU to the wakeup list, so when interrupts happen
10977 * we can find the right vCPU to wake up.
10978 * - Change the Posted-interrupt descriptor as below:
10979 * 'NDST' <-- vcpu->pre_pcpu
10980 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
10981 * - If 'ON' is set during this process, which means at least one
10982 * interrupt is posted for this vCPU, we cannot block it, in
10983 * this case, return 1, otherwise, return 0.
10984 *
10985 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070010986static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080010987{
10988 unsigned long flags;
10989 unsigned int dest;
10990 struct pi_desc old, new;
10991 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
10992
10993 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080010994 !irq_remapping_cap(IRQ_POSTING_CAP) ||
10995 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080010996 return 0;
10997
10998 vcpu->pre_pcpu = vcpu->cpu;
10999 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
11000 vcpu->pre_pcpu), flags);
11001 list_add_tail(&vcpu->blocked_vcpu_list,
11002 &per_cpu(blocked_vcpu_on_cpu,
11003 vcpu->pre_pcpu));
11004 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
11005 vcpu->pre_pcpu), flags);
11006
11007 do {
11008 old.control = new.control = pi_desc->control;
11009
11010 /*
11011 * We should not block the vCPU if
11012 * an interrupt is posted for it.
11013 */
11014 if (pi_test_on(pi_desc) == 1) {
11015 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
11016 vcpu->pre_pcpu), flags);
11017 list_del(&vcpu->blocked_vcpu_list);
11018 spin_unlock_irqrestore(
11019 &per_cpu(blocked_vcpu_on_cpu_lock,
11020 vcpu->pre_pcpu), flags);
11021 vcpu->pre_pcpu = -1;
11022
11023 return 1;
11024 }
11025
11026 WARN((pi_desc->sn == 1),
11027 "Warning: SN field of posted-interrupts "
11028 "is set before blocking\n");
11029
11030 /*
11031 * Since vCPU can be preempted during this process,
11032 * vcpu->cpu could be different with pre_pcpu, we
11033 * need to set pre_pcpu as the destination of wakeup
11034 * notification event, then we can find the right vCPU
11035 * to wakeup in wakeup handler if interrupts happen
11036 * when the vCPU is in blocked state.
11037 */
11038 dest = cpu_physical_id(vcpu->pre_pcpu);
11039
11040 if (x2apic_enabled())
11041 new.ndst = dest;
11042 else
11043 new.ndst = (dest << 8) & 0xFF00;
11044
11045 /* set 'NV' to 'wakeup vector' */
11046 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11047 } while (cmpxchg(&pi_desc->control, old.control,
11048 new.control) != old.control);
11049
11050 return 0;
11051}
11052
Yunhong Jiangbc225122016-06-13 14:19:58 -070011053static int vmx_pre_block(struct kvm_vcpu *vcpu)
11054{
11055 if (pi_pre_block(vcpu))
11056 return 1;
11057
Yunhong Jiang64672c92016-06-13 14:19:59 -070011058 if (kvm_lapic_hv_timer_in_use(vcpu))
11059 kvm_lapic_switch_to_sw_timer(vcpu);
11060
Yunhong Jiangbc225122016-06-13 14:19:58 -070011061 return 0;
11062}
11063
11064static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011065{
11066 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11067 struct pi_desc old, new;
11068 unsigned int dest;
11069 unsigned long flags;
11070
11071 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011072 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11073 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011074 return;
11075
11076 do {
11077 old.control = new.control = pi_desc->control;
11078
11079 dest = cpu_physical_id(vcpu->cpu);
11080
11081 if (x2apic_enabled())
11082 new.ndst = dest;
11083 else
11084 new.ndst = (dest << 8) & 0xFF00;
11085
11086 /* Allow posting non-urgent interrupts */
11087 new.sn = 0;
11088
11089 /* set 'NV' to 'notification vector' */
11090 new.nv = POSTED_INTR_VECTOR;
11091 } while (cmpxchg(&pi_desc->control, old.control,
11092 new.control) != old.control);
11093
11094 if(vcpu->pre_pcpu != -1) {
11095 spin_lock_irqsave(
11096 &per_cpu(blocked_vcpu_on_cpu_lock,
11097 vcpu->pre_pcpu), flags);
11098 list_del(&vcpu->blocked_vcpu_list);
11099 spin_unlock_irqrestore(
11100 &per_cpu(blocked_vcpu_on_cpu_lock,
11101 vcpu->pre_pcpu), flags);
11102 vcpu->pre_pcpu = -1;
11103 }
11104}
11105
Yunhong Jiangbc225122016-06-13 14:19:58 -070011106static void vmx_post_block(struct kvm_vcpu *vcpu)
11107{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011108 if (kvm_x86_ops->set_hv_timer)
11109 kvm_lapic_switch_to_hv_timer(vcpu);
11110
Yunhong Jiangbc225122016-06-13 14:19:58 -070011111 pi_post_block(vcpu);
11112}
11113
Feng Wubf9f6ac2015-09-18 22:29:55 +080011114/*
Feng Wuefc64402015-09-18 22:29:51 +080011115 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11116 *
11117 * @kvm: kvm
11118 * @host_irq: host irq of the interrupt
11119 * @guest_irq: gsi of the interrupt
11120 * @set: set or unset PI
11121 * returns 0 on success, < 0 on failure
11122 */
11123static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11124 uint32_t guest_irq, bool set)
11125{
11126 struct kvm_kernel_irq_routing_entry *e;
11127 struct kvm_irq_routing_table *irq_rt;
11128 struct kvm_lapic_irq irq;
11129 struct kvm_vcpu *vcpu;
11130 struct vcpu_data vcpu_info;
11131 int idx, ret = -EINVAL;
11132
11133 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011134 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11135 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011136 return 0;
11137
11138 idx = srcu_read_lock(&kvm->irq_srcu);
11139 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11140 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
11141
11142 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11143 if (e->type != KVM_IRQ_ROUTING_MSI)
11144 continue;
11145 /*
11146 * VT-d PI cannot support posting multicast/broadcast
11147 * interrupts to a vCPU, we still use interrupt remapping
11148 * for these kind of interrupts.
11149 *
11150 * For lowest-priority interrupts, we only support
11151 * those with single CPU as the destination, e.g. user
11152 * configures the interrupts via /proc/irq or uses
11153 * irqbalance to make the interrupts single-CPU.
11154 *
11155 * We will support full lowest-priority interrupt later.
11156 */
11157
Radim Krčmář371313132016-07-12 22:09:27 +020011158 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011159 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11160 /*
11161 * Make sure the IRTE is in remapped mode if
11162 * we don't handle it in posted mode.
11163 */
11164 ret = irq_set_vcpu_affinity(host_irq, NULL);
11165 if (ret < 0) {
11166 printk(KERN_INFO
11167 "failed to back to remapped mode, irq: %u\n",
11168 host_irq);
11169 goto out;
11170 }
11171
Feng Wuefc64402015-09-18 22:29:51 +080011172 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011173 }
Feng Wuefc64402015-09-18 22:29:51 +080011174
11175 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11176 vcpu_info.vector = irq.vector;
11177
Feng Wub6ce9782016-01-25 16:53:35 +080011178 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011179 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11180
11181 if (set)
11182 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11183 else {
11184 /* suppress notification event before unposting */
11185 pi_set_sn(vcpu_to_pi_desc(vcpu));
11186 ret = irq_set_vcpu_affinity(host_irq, NULL);
11187 pi_clear_sn(vcpu_to_pi_desc(vcpu));
11188 }
11189
11190 if (ret < 0) {
11191 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11192 __func__);
11193 goto out;
11194 }
11195 }
11196
11197 ret = 0;
11198out:
11199 srcu_read_unlock(&kvm->irq_srcu, idx);
11200 return ret;
11201}
11202
Ashok Rajc45dcc72016-06-22 14:59:56 +080011203static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11204{
11205 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11206 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11207 FEATURE_CONTROL_LMCE;
11208 else
11209 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11210 ~FEATURE_CONTROL_LMCE;
11211}
11212
Kees Cook404f6aa2016-08-08 16:29:06 -070011213static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011214 .cpu_has_kvm_support = cpu_has_kvm_support,
11215 .disabled_by_bios = vmx_disabled_by_bios,
11216 .hardware_setup = hardware_setup,
11217 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011218 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011219 .hardware_enable = hardware_enable,
11220 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011221 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020011222 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011223
11224 .vcpu_create = vmx_create_vcpu,
11225 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011226 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011227
Avi Kivity04d2cc72007-09-10 18:10:54 +030011228 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011229 .vcpu_load = vmx_vcpu_load,
11230 .vcpu_put = vmx_vcpu_put,
11231
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011232 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011233 .get_msr = vmx_get_msr,
11234 .set_msr = vmx_set_msr,
11235 .get_segment_base = vmx_get_segment_base,
11236 .get_segment = vmx_get_segment,
11237 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011238 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011239 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011240 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011241 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011242 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011243 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011244 .set_cr3 = vmx_set_cr3,
11245 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011246 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011247 .get_idt = vmx_get_idt,
11248 .set_idt = vmx_set_idt,
11249 .get_gdt = vmx_get_gdt,
11250 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011251 .get_dr6 = vmx_get_dr6,
11252 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011253 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011254 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011255 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011256 .get_rflags = vmx_get_rflags,
11257 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011258
11259 .get_pkru = vmx_get_pkru,
11260
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011261 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011262 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011263
11264 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011265
Avi Kivity6aa8b732006-12-10 02:21:36 -080011266 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011267 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011268 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011269 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11270 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011271 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011272 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011273 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011274 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011275 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011276 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011277 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011278 .get_nmi_mask = vmx_get_nmi_mask,
11279 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011280 .enable_nmi_window = enable_nmi_window,
11281 .enable_irq_window = enable_irq_window,
11282 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011283 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011284 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011285 .get_enable_apicv = vmx_get_enable_apicv,
11286 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011287 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11288 .hwapic_irr_update = vmx_hwapic_irr_update,
11289 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011290 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11291 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011292
Izik Eiduscbc94022007-10-25 00:29:55 +020011293 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011294 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011295 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011296
Avi Kivity586f9602010-11-18 13:09:54 +020011297 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011298
Sheng Yang17cc3932010-01-05 19:02:27 +080011299 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011300
11301 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011302
11303 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011304 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011305
11306 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011307
11308 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011309
11310 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011311
11312 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011313
11314 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011315 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011316 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011317 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011318
11319 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011320
11321 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011322
11323 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11324 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11325 .flush_log_dirty = vmx_flush_log_dirty,
11326 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011327
Feng Wubf9f6ac2015-09-18 22:29:55 +080011328 .pre_block = vmx_pre_block,
11329 .post_block = vmx_post_block,
11330
Wei Huang25462f72015-06-19 15:45:05 +020011331 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011332
11333 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011334
11335#ifdef CONFIG_X86_64
11336 .set_hv_timer = vmx_set_hv_timer,
11337 .cancel_hv_timer = vmx_cancel_hv_timer,
11338#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011339
11340 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011341};
11342
11343static int __init vmx_init(void)
11344{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011345 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11346 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011347 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011348 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011349
Dave Young2965faa2015-09-09 15:38:55 -070011350#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011351 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11352 crash_vmclear_local_loaded_vmcss);
11353#endif
11354
He, Qingfdef3ad2007-04-30 09:45:24 +030011355 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011356}
11357
11358static void __exit vmx_exit(void)
11359{
Dave Young2965faa2015-09-09 15:38:55 -070011360#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011361 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011362 synchronize_rcu();
11363#endif
11364
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011365 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011366}
11367
11368module_init(vmx_init)
11369module_exit(vmx_exit)