blob: a45d8580f91e7e8459666d7c1f0076ab1803b821 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030036#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030037#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040038
Feng Wu28b835d2015-09-18 22:29:54 +080039#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080040#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080041#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020042#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020043#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080044#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020045#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020046#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010047#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080048#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010049#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080050#include <asm/irq_remapping.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Marcelo Tosatti229456f2009-06-17 09:22:14 -030052#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020053#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030054
Avi Kivity4ecac3f2008-05-13 13:23:38 +030055#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040056#define __ex_clear(x, reg) \
57 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030058
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Josh Triplette9bda3b2012-03-20 23:33:51 -070062static const struct x86_cpu_id vmx_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_VMX),
64 {}
65};
66MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
67
Rusty Russell476bc002012-01-13 09:32:18 +103068static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020069module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080070
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020072module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020073
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080076
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070078module_param_named(unrestricted_guest,
79 enable_unrestricted_guest, bool, S_IRUGO);
80
Xudong Hao83c3a332012-05-28 19:33:35 +080081static bool __read_mostly enable_ept_ad_bits = 1;
82module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
83
Avi Kivitya27685c2012-06-12 20:30:18 +030084static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020085module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080088module_param(vmm_exclusive, bool, S_IRUGO);
89
Rusty Russell476bc002012-01-13 09:32:18 +103090static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030091module_param(fasteoi, bool, S_IRUGO);
92
Yang Zhang5a717852013-04-11 19:25:16 +080093static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080094module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080095
Abel Gordonabc4fc52013-04-18 14:35:25 +030096static bool __read_mostly enable_shadow_vmcs = 1;
97module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030098/*
99 * If nested=1, nested virtualization is supported, i.e., guests may use
100 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
101 * use VMX instructions.
102 */
Rusty Russell476bc002012-01-13 09:32:18 +1030103static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300104module_param(nested, bool, S_IRUGO);
105
Wanpeng Li20300092014-12-02 19:14:59 +0800106static u64 __read_mostly host_xss;
107
Kai Huang843e4332015-01-28 10:54:28 +0800108static bool __read_mostly enable_pml = 1;
109module_param_named(pml, enable_pml, bool, S_IRUGO);
110
Haozhong Zhang64903d62015-10-20 15:39:09 +0800111#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
112
Yunhong Jiang64672c92016-06-13 14:19:59 -0700113/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
114static int __read_mostly cpu_preemption_timer_multi;
115static bool __read_mostly enable_preemption_timer = 1;
116#ifdef CONFIG_X86_64
117module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
118#endif
119
Gleb Natapov50378782013-02-04 16:00:28 +0200120#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
121#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200122#define KVM_VM_CR0_ALWAYS_ON \
123 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200124#define KVM_CR4_GUEST_OWNED_BITS \
125 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700126 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200127
Avi Kivitycdc0e242009-12-06 17:21:14 +0200128#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
129#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
130
Avi Kivity78ac8b42010-04-08 18:19:35 +0300131#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
132
Jan Kiszkaf4124502014-03-07 20:03:13 +0100133#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
134
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800135/*
136 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
137 * ple_gap: upper bound on the amount of time between two successive
138 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500139 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800140 * ple_window: upper bound on the amount of time a guest is allowed to execute
141 * in a PAUSE loop. Tests indicate that most spinlocks are held for
142 * less than 2^12 cycles
143 * Time is measured based on a counter that runs at the same rate as the TSC,
144 * refer SDM volume 3b section 21.6.13 & 22.1.3.
145 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200146#define KVM_VMX_DEFAULT_PLE_GAP 128
147#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
148#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
149#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
150#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
151 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
152
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800153static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
154module_param(ple_gap, int, S_IRUGO);
155
156static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
157module_param(ple_window, int, S_IRUGO);
158
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200159/* Default doubles per-vcpu window every exit. */
160static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
161module_param(ple_window_grow, int, S_IRUGO);
162
163/* Default resets per-vcpu window every exit to ple_window. */
164static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
165module_param(ple_window_shrink, int, S_IRUGO);
166
167/* Default is to compute the maximum so we can never overflow. */
168static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
169static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
170module_param(ple_window_max, int, S_IRUGO);
171
Avi Kivity83287ea422012-09-16 15:10:57 +0300172extern const ulong vmx_return;
173
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200174#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300175#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300176
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400177struct vmcs {
178 u32 revision_id;
179 u32 abort;
180 char data[0];
181};
182
Nadav Har'Eld462b812011-05-24 15:26:10 +0300183/*
184 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
185 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
186 * loaded on this CPU (so we can clear them if the CPU goes down).
187 */
188struct loaded_vmcs {
189 struct vmcs *vmcs;
190 int cpu;
191 int launched;
192 struct list_head loaded_vmcss_on_cpu_link;
193};
194
Avi Kivity26bb0982009-09-07 11:14:12 +0300195struct shared_msr_entry {
196 unsigned index;
197 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200198 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300199};
200
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300201/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300202 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
203 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
204 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
205 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
206 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
207 * More than one of these structures may exist, if L1 runs multiple L2 guests.
208 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
209 * underlying hardware which will be used to run L2.
210 * This structure is packed to ensure that its layout is identical across
211 * machines (necessary for live migration).
212 * If there are changes in this struct, VMCS12_REVISION must be changed.
213 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300214typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300215struct __packed vmcs12 {
216 /* According to the Intel spec, a VMCS region must start with the
217 * following two fields. Then follow implementation-specific data.
218 */
219 u32 revision_id;
220 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300221
Nadav Har'El27d6c862011-05-25 23:06:59 +0300222 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
223 u32 padding[7]; /* room for future expansion */
224
Nadav Har'El22bd0352011-05-25 23:05:57 +0300225 u64 io_bitmap_a;
226 u64 io_bitmap_b;
227 u64 msr_bitmap;
228 u64 vm_exit_msr_store_addr;
229 u64 vm_exit_msr_load_addr;
230 u64 vm_entry_msr_load_addr;
231 u64 tsc_offset;
232 u64 virtual_apic_page_addr;
233 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800234 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300235 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800236 u64 eoi_exit_bitmap0;
237 u64 eoi_exit_bitmap1;
238 u64 eoi_exit_bitmap2;
239 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800240 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300241 u64 guest_physical_address;
242 u64 vmcs_link_pointer;
243 u64 guest_ia32_debugctl;
244 u64 guest_ia32_pat;
245 u64 guest_ia32_efer;
246 u64 guest_ia32_perf_global_ctrl;
247 u64 guest_pdptr0;
248 u64 guest_pdptr1;
249 u64 guest_pdptr2;
250 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100251 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300252 u64 host_ia32_pat;
253 u64 host_ia32_efer;
254 u64 host_ia32_perf_global_ctrl;
255 u64 padding64[8]; /* room for future expansion */
256 /*
257 * To allow migration of L1 (complete with its L2 guests) between
258 * machines of different natural widths (32 or 64 bit), we cannot have
259 * unsigned long fields with no explict size. We use u64 (aliased
260 * natural_width) instead. Luckily, x86 is little-endian.
261 */
262 natural_width cr0_guest_host_mask;
263 natural_width cr4_guest_host_mask;
264 natural_width cr0_read_shadow;
265 natural_width cr4_read_shadow;
266 natural_width cr3_target_value0;
267 natural_width cr3_target_value1;
268 natural_width cr3_target_value2;
269 natural_width cr3_target_value3;
270 natural_width exit_qualification;
271 natural_width guest_linear_address;
272 natural_width guest_cr0;
273 natural_width guest_cr3;
274 natural_width guest_cr4;
275 natural_width guest_es_base;
276 natural_width guest_cs_base;
277 natural_width guest_ss_base;
278 natural_width guest_ds_base;
279 natural_width guest_fs_base;
280 natural_width guest_gs_base;
281 natural_width guest_ldtr_base;
282 natural_width guest_tr_base;
283 natural_width guest_gdtr_base;
284 natural_width guest_idtr_base;
285 natural_width guest_dr7;
286 natural_width guest_rsp;
287 natural_width guest_rip;
288 natural_width guest_rflags;
289 natural_width guest_pending_dbg_exceptions;
290 natural_width guest_sysenter_esp;
291 natural_width guest_sysenter_eip;
292 natural_width host_cr0;
293 natural_width host_cr3;
294 natural_width host_cr4;
295 natural_width host_fs_base;
296 natural_width host_gs_base;
297 natural_width host_tr_base;
298 natural_width host_gdtr_base;
299 natural_width host_idtr_base;
300 natural_width host_ia32_sysenter_esp;
301 natural_width host_ia32_sysenter_eip;
302 natural_width host_rsp;
303 natural_width host_rip;
304 natural_width paddingl[8]; /* room for future expansion */
305 u32 pin_based_vm_exec_control;
306 u32 cpu_based_vm_exec_control;
307 u32 exception_bitmap;
308 u32 page_fault_error_code_mask;
309 u32 page_fault_error_code_match;
310 u32 cr3_target_count;
311 u32 vm_exit_controls;
312 u32 vm_exit_msr_store_count;
313 u32 vm_exit_msr_load_count;
314 u32 vm_entry_controls;
315 u32 vm_entry_msr_load_count;
316 u32 vm_entry_intr_info_field;
317 u32 vm_entry_exception_error_code;
318 u32 vm_entry_instruction_len;
319 u32 tpr_threshold;
320 u32 secondary_vm_exec_control;
321 u32 vm_instruction_error;
322 u32 vm_exit_reason;
323 u32 vm_exit_intr_info;
324 u32 vm_exit_intr_error_code;
325 u32 idt_vectoring_info_field;
326 u32 idt_vectoring_error_code;
327 u32 vm_exit_instruction_len;
328 u32 vmx_instruction_info;
329 u32 guest_es_limit;
330 u32 guest_cs_limit;
331 u32 guest_ss_limit;
332 u32 guest_ds_limit;
333 u32 guest_fs_limit;
334 u32 guest_gs_limit;
335 u32 guest_ldtr_limit;
336 u32 guest_tr_limit;
337 u32 guest_gdtr_limit;
338 u32 guest_idtr_limit;
339 u32 guest_es_ar_bytes;
340 u32 guest_cs_ar_bytes;
341 u32 guest_ss_ar_bytes;
342 u32 guest_ds_ar_bytes;
343 u32 guest_fs_ar_bytes;
344 u32 guest_gs_ar_bytes;
345 u32 guest_ldtr_ar_bytes;
346 u32 guest_tr_ar_bytes;
347 u32 guest_interruptibility_info;
348 u32 guest_activity_state;
349 u32 guest_sysenter_cs;
350 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100351 u32 vmx_preemption_timer_value;
352 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300353 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800354 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300355 u16 guest_es_selector;
356 u16 guest_cs_selector;
357 u16 guest_ss_selector;
358 u16 guest_ds_selector;
359 u16 guest_fs_selector;
360 u16 guest_gs_selector;
361 u16 guest_ldtr_selector;
362 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800363 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300364 u16 host_es_selector;
365 u16 host_cs_selector;
366 u16 host_ss_selector;
367 u16 host_ds_selector;
368 u16 host_fs_selector;
369 u16 host_gs_selector;
370 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300371};
372
373/*
374 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
375 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
376 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
377 */
378#define VMCS12_REVISION 0x11e57ed0
379
380/*
381 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
382 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
383 * current implementation, 4K are reserved to avoid future complications.
384 */
385#define VMCS12_SIZE 0x1000
386
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300387/* Used to remember the last vmcs02 used for some recently used vmcs12s */
388struct vmcs02_list {
389 struct list_head list;
390 gpa_t vmptr;
391 struct loaded_vmcs vmcs02;
392};
393
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300394/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300395 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
396 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
397 */
398struct nested_vmx {
399 /* Has the level1 guest done vmxon? */
400 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400401 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300402
403 /* The guest-physical address of the current VMCS L1 keeps for L2 */
404 gpa_t current_vmptr;
405 /* The host-usable pointer to the above */
406 struct page *current_vmcs12_page;
407 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700408 /*
409 * Cache of the guest's VMCS, existing outside of guest memory.
410 * Loaded from guest memory during VMPTRLD. Flushed to guest
411 * memory during VMXOFF, VMCLEAR, VMPTRLD.
412 */
413 struct vmcs12 *cached_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300414 struct vmcs *current_shadow_vmcs;
Abel Gordon012f83c2013-04-18 14:39:25 +0300415 /*
416 * Indicates if the shadow vmcs must be updated with the
417 * data hold by vmcs12
418 */
419 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300420
421 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
422 struct list_head vmcs02_pool;
423 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300424 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300425 /* L2 must run next, and mustn't decide to exit to L1. */
426 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300427 /*
428 * Guest pages referred to in vmcs02 with host-physical pointers, so
429 * we must keep them pinned while L2 runs.
430 */
431 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800432 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800433 struct page *pi_desc_page;
434 struct pi_desc *pi_desc;
435 bool pi_pending;
436 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100437
438 struct hrtimer preemption_timer;
439 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200440
441 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
442 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800443
Wanpeng Li5c614b32015-10-13 09:18:36 -0700444 u16 vpid02;
445 u16 last_vpid;
446
Wincy Vanb9c237b2015-02-03 23:56:30 +0800447 u32 nested_vmx_procbased_ctls_low;
448 u32 nested_vmx_procbased_ctls_high;
449 u32 nested_vmx_true_procbased_ctls_low;
450 u32 nested_vmx_secondary_ctls_low;
451 u32 nested_vmx_secondary_ctls_high;
452 u32 nested_vmx_pinbased_ctls_low;
453 u32 nested_vmx_pinbased_ctls_high;
454 u32 nested_vmx_exit_ctls_low;
455 u32 nested_vmx_exit_ctls_high;
456 u32 nested_vmx_true_exit_ctls_low;
457 u32 nested_vmx_entry_ctls_low;
458 u32 nested_vmx_entry_ctls_high;
459 u32 nested_vmx_true_entry_ctls_low;
460 u32 nested_vmx_misc_low;
461 u32 nested_vmx_misc_high;
462 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700463 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300464};
465
Yang Zhang01e439b2013-04-11 19:25:12 +0800466#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800467#define POSTED_INTR_SN 1
468
Yang Zhang01e439b2013-04-11 19:25:12 +0800469/* Posted-Interrupt Descriptor */
470struct pi_desc {
471 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800472 union {
473 struct {
474 /* bit 256 - Outstanding Notification */
475 u16 on : 1,
476 /* bit 257 - Suppress Notification */
477 sn : 1,
478 /* bit 271:258 - Reserved */
479 rsvd_1 : 14;
480 /* bit 279:272 - Notification Vector */
481 u8 nv;
482 /* bit 287:280 - Reserved */
483 u8 rsvd_2;
484 /* bit 319:288 - Notification Destination */
485 u32 ndst;
486 };
487 u64 control;
488 };
489 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800490} __aligned(64);
491
Yang Zhanga20ed542013-04-11 19:25:15 +0800492static bool pi_test_and_set_on(struct pi_desc *pi_desc)
493{
494 return test_and_set_bit(POSTED_INTR_ON,
495 (unsigned long *)&pi_desc->control);
496}
497
498static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
499{
500 return test_and_clear_bit(POSTED_INTR_ON,
501 (unsigned long *)&pi_desc->control);
502}
503
504static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
505{
506 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
507}
508
Feng Wuebbfc762015-09-18 22:29:46 +0800509static inline void pi_clear_sn(struct pi_desc *pi_desc)
510{
511 return clear_bit(POSTED_INTR_SN,
512 (unsigned long *)&pi_desc->control);
513}
514
515static inline void pi_set_sn(struct pi_desc *pi_desc)
516{
517 return set_bit(POSTED_INTR_SN,
518 (unsigned long *)&pi_desc->control);
519}
520
521static inline int pi_test_on(struct pi_desc *pi_desc)
522{
523 return test_bit(POSTED_INTR_ON,
524 (unsigned long *)&pi_desc->control);
525}
526
527static inline int pi_test_sn(struct pi_desc *pi_desc)
528{
529 return test_bit(POSTED_INTR_SN,
530 (unsigned long *)&pi_desc->control);
531}
532
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400533struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000534 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300535 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300536 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200537 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300538 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200539 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200540 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300541 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400542 int nmsrs;
543 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800544 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400545#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300546 u64 msr_host_kernel_gs_base;
547 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400548#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200549 u32 vm_entry_controls_shadow;
550 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300551 /*
552 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
553 * non-nested (L1) guest, it always points to vmcs01. For a nested
554 * guest (L2), it points to a different VMCS.
555 */
556 struct loaded_vmcs vmcs01;
557 struct loaded_vmcs *loaded_vmcs;
558 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300559 struct msr_autoload {
560 unsigned nr;
561 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
562 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
563 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400564 struct {
565 int loaded;
566 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300567#ifdef CONFIG_X86_64
568 u16 ds_sel, es_sel;
569#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200570 int gs_ldt_reload_needed;
571 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000572 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700573 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400574 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200575 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300576 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300577 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300578 struct kvm_segment segs[8];
579 } rmode;
580 struct {
581 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300582 struct kvm_save_segment {
583 u16 selector;
584 unsigned long base;
585 u32 limit;
586 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300587 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300588 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800589 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300590 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200591
592 /* Support for vnmi-less CPUs */
593 int soft_vnmi_blocked;
594 ktime_t entry_time;
595 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800596 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800597
Yang Zhang01e439b2013-04-11 19:25:12 +0800598 /* Posted interrupt descriptor */
599 struct pi_desc pi_desc;
600
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300601 /* Support for a guest hypervisor (nested VMX) */
602 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200603
604 /* Dynamic PLE window. */
605 int ple_window;
606 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800607
608 /* Support for PML */
609#define PML_ENTITY_NUM 512
610 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800611
Yunhong Jiang64672c92016-06-13 14:19:59 -0700612 /* apic deadline value in host tsc */
613 u64 hv_deadline_tsc;
614
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800615 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800616
617 bool guest_pkru_valid;
618 u32 guest_pkru;
619 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800620
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800621 /*
622 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
623 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
624 * in msr_ia32_feature_control_valid_bits.
625 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800626 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800627 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400628};
629
Avi Kivity2fb92db2011-04-27 19:42:18 +0300630enum segment_cache_field {
631 SEG_FIELD_SEL = 0,
632 SEG_FIELD_BASE = 1,
633 SEG_FIELD_LIMIT = 2,
634 SEG_FIELD_AR = 3,
635
636 SEG_FIELD_NR = 4
637};
638
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400639static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
640{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000641 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400642}
643
Feng Wuefc64402015-09-18 22:29:51 +0800644static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
645{
646 return &(to_vmx(vcpu)->pi_desc);
647}
648
Nadav Har'El22bd0352011-05-25 23:05:57 +0300649#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
650#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
651#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
652 [number##_HIGH] = VMCS12_OFFSET(name)+4
653
Abel Gordon4607c2d2013-04-18 14:35:55 +0300654
Bandan Dasfe2b2012014-04-21 15:20:14 -0400655static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300656 /*
657 * We do NOT shadow fields that are modified when L0
658 * traps and emulates any vmx instruction (e.g. VMPTRLD,
659 * VMXON...) executed by L1.
660 * For example, VM_INSTRUCTION_ERROR is read
661 * by L1 if a vmx instruction fails (part of the error path).
662 * Note the code assumes this logic. If for some reason
663 * we start shadowing these fields then we need to
664 * force a shadow sync when L0 emulates vmx instructions
665 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
666 * by nested_vmx_failValid)
667 */
668 VM_EXIT_REASON,
669 VM_EXIT_INTR_INFO,
670 VM_EXIT_INSTRUCTION_LEN,
671 IDT_VECTORING_INFO_FIELD,
672 IDT_VECTORING_ERROR_CODE,
673 VM_EXIT_INTR_ERROR_CODE,
674 EXIT_QUALIFICATION,
675 GUEST_LINEAR_ADDRESS,
676 GUEST_PHYSICAL_ADDRESS
677};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400678static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300679 ARRAY_SIZE(shadow_read_only_fields);
680
Bandan Dasfe2b2012014-04-21 15:20:14 -0400681static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800682 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300683 GUEST_RIP,
684 GUEST_RSP,
685 GUEST_CR0,
686 GUEST_CR3,
687 GUEST_CR4,
688 GUEST_INTERRUPTIBILITY_INFO,
689 GUEST_RFLAGS,
690 GUEST_CS_SELECTOR,
691 GUEST_CS_AR_BYTES,
692 GUEST_CS_LIMIT,
693 GUEST_CS_BASE,
694 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100695 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300696 CR0_GUEST_HOST_MASK,
697 CR0_READ_SHADOW,
698 CR4_READ_SHADOW,
699 TSC_OFFSET,
700 EXCEPTION_BITMAP,
701 CPU_BASED_VM_EXEC_CONTROL,
702 VM_ENTRY_EXCEPTION_ERROR_CODE,
703 VM_ENTRY_INTR_INFO_FIELD,
704 VM_ENTRY_INSTRUCTION_LEN,
705 VM_ENTRY_EXCEPTION_ERROR_CODE,
706 HOST_FS_BASE,
707 HOST_GS_BASE,
708 HOST_FS_SELECTOR,
709 HOST_GS_SELECTOR
710};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400711static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300712 ARRAY_SIZE(shadow_read_write_fields);
713
Mathias Krause772e0312012-08-30 01:30:19 +0200714static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300715 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800716 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300717 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
718 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
719 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
720 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
721 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
722 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
723 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
724 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800725 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300726 FIELD(HOST_ES_SELECTOR, host_es_selector),
727 FIELD(HOST_CS_SELECTOR, host_cs_selector),
728 FIELD(HOST_SS_SELECTOR, host_ss_selector),
729 FIELD(HOST_DS_SELECTOR, host_ds_selector),
730 FIELD(HOST_FS_SELECTOR, host_fs_selector),
731 FIELD(HOST_GS_SELECTOR, host_gs_selector),
732 FIELD(HOST_TR_SELECTOR, host_tr_selector),
733 FIELD64(IO_BITMAP_A, io_bitmap_a),
734 FIELD64(IO_BITMAP_B, io_bitmap_b),
735 FIELD64(MSR_BITMAP, msr_bitmap),
736 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
737 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
738 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
739 FIELD64(TSC_OFFSET, tsc_offset),
740 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
741 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800742 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300743 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800744 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
745 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
746 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
747 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800748 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300749 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
750 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
751 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
752 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
753 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
754 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
755 FIELD64(GUEST_PDPTR0, guest_pdptr0),
756 FIELD64(GUEST_PDPTR1, guest_pdptr1),
757 FIELD64(GUEST_PDPTR2, guest_pdptr2),
758 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100759 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300760 FIELD64(HOST_IA32_PAT, host_ia32_pat),
761 FIELD64(HOST_IA32_EFER, host_ia32_efer),
762 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
763 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
764 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
765 FIELD(EXCEPTION_BITMAP, exception_bitmap),
766 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
767 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
768 FIELD(CR3_TARGET_COUNT, cr3_target_count),
769 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
770 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
771 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
772 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
773 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
774 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
775 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
776 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
777 FIELD(TPR_THRESHOLD, tpr_threshold),
778 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
779 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
780 FIELD(VM_EXIT_REASON, vm_exit_reason),
781 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
782 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
783 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
784 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
785 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
786 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
787 FIELD(GUEST_ES_LIMIT, guest_es_limit),
788 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
789 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
790 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
791 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
792 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
793 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
794 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
795 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
796 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
797 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
798 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
799 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
800 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
801 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
802 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
803 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
804 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
805 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
806 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
807 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
808 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100809 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300810 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
811 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
812 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
813 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
814 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
815 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
816 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
817 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
818 FIELD(EXIT_QUALIFICATION, exit_qualification),
819 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
820 FIELD(GUEST_CR0, guest_cr0),
821 FIELD(GUEST_CR3, guest_cr3),
822 FIELD(GUEST_CR4, guest_cr4),
823 FIELD(GUEST_ES_BASE, guest_es_base),
824 FIELD(GUEST_CS_BASE, guest_cs_base),
825 FIELD(GUEST_SS_BASE, guest_ss_base),
826 FIELD(GUEST_DS_BASE, guest_ds_base),
827 FIELD(GUEST_FS_BASE, guest_fs_base),
828 FIELD(GUEST_GS_BASE, guest_gs_base),
829 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
830 FIELD(GUEST_TR_BASE, guest_tr_base),
831 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
832 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
833 FIELD(GUEST_DR7, guest_dr7),
834 FIELD(GUEST_RSP, guest_rsp),
835 FIELD(GUEST_RIP, guest_rip),
836 FIELD(GUEST_RFLAGS, guest_rflags),
837 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
838 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
839 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
840 FIELD(HOST_CR0, host_cr0),
841 FIELD(HOST_CR3, host_cr3),
842 FIELD(HOST_CR4, host_cr4),
843 FIELD(HOST_FS_BASE, host_fs_base),
844 FIELD(HOST_GS_BASE, host_gs_base),
845 FIELD(HOST_TR_BASE, host_tr_base),
846 FIELD(HOST_GDTR_BASE, host_gdtr_base),
847 FIELD(HOST_IDTR_BASE, host_idtr_base),
848 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
849 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
850 FIELD(HOST_RSP, host_rsp),
851 FIELD(HOST_RIP, host_rip),
852};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300853
854static inline short vmcs_field_to_offset(unsigned long field)
855{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100856 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
857
858 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
859 vmcs_field_to_offset_table[field] == 0)
860 return -ENOENT;
861
Nadav Har'El22bd0352011-05-25 23:05:57 +0300862 return vmcs_field_to_offset_table[field];
863}
864
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300865static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
866{
David Matlack4f2777b2016-07-13 17:16:37 -0700867 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300868}
869
870static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
871{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200872 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800873 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300874 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800875
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300876 return page;
877}
878
879static void nested_release_page(struct page *page)
880{
881 kvm_release_page_dirty(page);
882}
883
884static void nested_release_page_clean(struct page *page)
885{
886 kvm_release_page_clean(page);
887}
888
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300889static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800890static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800891static void kvm_cpu_vmxon(u64 addr);
892static void kvm_cpu_vmxoff(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800893static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200894static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300895static void vmx_set_segment(struct kvm_vcpu *vcpu,
896 struct kvm_segment *var, int seg);
897static void vmx_get_segment(struct kvm_vcpu *vcpu,
898 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200899static bool guest_state_valid(struct kvm_vcpu *vcpu);
900static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300901static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300902static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800903static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300904
Avi Kivity6aa8b732006-12-10 02:21:36 -0800905static DEFINE_PER_CPU(struct vmcs *, vmxarea);
906static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300907/*
908 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
909 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
910 */
911static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300912static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800913
Feng Wubf9f6ac2015-09-18 22:29:55 +0800914/*
915 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
916 * can find which vCPU should be waken up.
917 */
918static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
919static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
920
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200921static unsigned long *vmx_io_bitmap_a;
922static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200923static unsigned long *vmx_msr_bitmap_legacy;
924static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800925static unsigned long *vmx_msr_bitmap_legacy_x2apic;
926static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Wincy Van3af18d92015-02-03 23:49:31 +0800927static unsigned long *vmx_msr_bitmap_nested;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300928static unsigned long *vmx_vmread_bitmap;
929static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300930
Avi Kivity110312c2010-12-21 12:54:20 +0200931static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200932static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200933
Sheng Yang2384d2b2008-01-17 15:14:33 +0800934static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
935static DEFINE_SPINLOCK(vmx_vpid_lock);
936
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300937static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 int size;
939 int order;
940 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300941 u32 pin_based_exec_ctrl;
942 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800943 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300944 u32 vmexit_ctrl;
945 u32 vmentry_ctrl;
946} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947
Hannes Ederefff9e52008-11-28 17:02:06 +0100948static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800949 u32 ept;
950 u32 vpid;
951} vmx_capability;
952
Avi Kivity6aa8b732006-12-10 02:21:36 -0800953#define VMX_SEGMENT_FIELD(seg) \
954 [VCPU_SREG_##seg] = { \
955 .selector = GUEST_##seg##_SELECTOR, \
956 .base = GUEST_##seg##_BASE, \
957 .limit = GUEST_##seg##_LIMIT, \
958 .ar_bytes = GUEST_##seg##_AR_BYTES, \
959 }
960
Mathias Krause772e0312012-08-30 01:30:19 +0200961static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962 unsigned selector;
963 unsigned base;
964 unsigned limit;
965 unsigned ar_bytes;
966} kvm_vmx_segment_fields[] = {
967 VMX_SEGMENT_FIELD(CS),
968 VMX_SEGMENT_FIELD(DS),
969 VMX_SEGMENT_FIELD(ES),
970 VMX_SEGMENT_FIELD(FS),
971 VMX_SEGMENT_FIELD(GS),
972 VMX_SEGMENT_FIELD(SS),
973 VMX_SEGMENT_FIELD(TR),
974 VMX_SEGMENT_FIELD(LDTR),
975};
976
Avi Kivity26bb0982009-09-07 11:14:12 +0300977static u64 host_efer;
978
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300979static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
980
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300981/*
Brian Gerst8c065852010-07-17 09:03:26 -0400982 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300983 * away by decrementing the array size.
984 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800986#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300987 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400989 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991
Jan Kiszka5bb16012016-02-09 20:14:21 +0100992static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993{
994 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
995 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +0100996 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
997}
998
Jan Kiszka6f054852016-02-09 20:15:18 +0100999static inline bool is_debug(u32 intr_info)
1000{
1001 return is_exception_n(intr_info, DB_VECTOR);
1002}
1003
1004static inline bool is_breakpoint(u32 intr_info)
1005{
1006 return is_exception_n(intr_info, BP_VECTOR);
1007}
1008
Jan Kiszka5bb16012016-02-09 20:14:21 +01001009static inline bool is_page_fault(u32 intr_info)
1010{
1011 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012}
1013
Gui Jianfeng31299942010-03-15 17:29:09 +08001014static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001015{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001016 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001017}
1018
Gui Jianfeng31299942010-03-15 17:29:09 +08001019static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001020{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001021 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001022}
1023
Gui Jianfeng31299942010-03-15 17:29:09 +08001024static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025{
1026 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1027 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1028}
1029
Gui Jianfeng31299942010-03-15 17:29:09 +08001030static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001031{
1032 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1033 INTR_INFO_VALID_MASK)) ==
1034 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1035}
1036
Gui Jianfeng31299942010-03-15 17:29:09 +08001037static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001038{
Sheng Yang04547152009-04-01 15:52:31 +08001039 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001040}
1041
Gui Jianfeng31299942010-03-15 17:29:09 +08001042static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001043{
Sheng Yang04547152009-04-01 15:52:31 +08001044 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001045}
1046
Paolo Bonzini35754c92015-07-29 12:05:37 +02001047static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001048{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001049 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001050}
1051
Gui Jianfeng31299942010-03-15 17:29:09 +08001052static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001053{
Sheng Yang04547152009-04-01 15:52:31 +08001054 return vmcs_config.cpu_based_exec_ctrl &
1055 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001056}
1057
Avi Kivity774ead32007-12-26 13:57:04 +02001058static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001059{
Sheng Yang04547152009-04-01 15:52:31 +08001060 return vmcs_config.cpu_based_2nd_exec_ctrl &
1061 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1062}
1063
Yang Zhang8d146952013-01-25 10:18:50 +08001064static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1065{
1066 return vmcs_config.cpu_based_2nd_exec_ctrl &
1067 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1068}
1069
Yang Zhang83d4c282013-01-25 10:18:49 +08001070static inline bool cpu_has_vmx_apic_register_virt(void)
1071{
1072 return vmcs_config.cpu_based_2nd_exec_ctrl &
1073 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1074}
1075
Yang Zhangc7c9c562013-01-25 10:18:51 +08001076static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1077{
1078 return vmcs_config.cpu_based_2nd_exec_ctrl &
1079 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1080}
1081
Yunhong Jiang64672c92016-06-13 14:19:59 -07001082/*
1083 * Comment's format: document - errata name - stepping - processor name.
1084 * Refer from
1085 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1086 */
1087static u32 vmx_preemption_cpu_tfms[] = {
1088/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
10890x000206E6,
1090/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1091/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1092/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
10930x00020652,
1094/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
10950x00020655,
1096/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1097/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1098/*
1099 * 320767.pdf - AAP86 - B1 -
1100 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1101 */
11020x000106E5,
1103/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11040x000106A0,
1105/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11060x000106A1,
1107/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11080x000106A4,
1109 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1110 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1111 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11120x000106A5,
1113};
1114
1115static inline bool cpu_has_broken_vmx_preemption_timer(void)
1116{
1117 u32 eax = cpuid_eax(0x00000001), i;
1118
1119 /* Clear the reserved bits */
1120 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001121 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001122 if (eax == vmx_preemption_cpu_tfms[i])
1123 return true;
1124
1125 return false;
1126}
1127
1128static inline bool cpu_has_vmx_preemption_timer(void)
1129{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001130 return vmcs_config.pin_based_exec_ctrl &
1131 PIN_BASED_VMX_PREEMPTION_TIMER;
1132}
1133
Yang Zhang01e439b2013-04-11 19:25:12 +08001134static inline bool cpu_has_vmx_posted_intr(void)
1135{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001136 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1137 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001138}
1139
1140static inline bool cpu_has_vmx_apicv(void)
1141{
1142 return cpu_has_vmx_apic_register_virt() &&
1143 cpu_has_vmx_virtual_intr_delivery() &&
1144 cpu_has_vmx_posted_intr();
1145}
1146
Sheng Yang04547152009-04-01 15:52:31 +08001147static inline bool cpu_has_vmx_flexpriority(void)
1148{
1149 return cpu_has_vmx_tpr_shadow() &&
1150 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001151}
1152
Marcelo Tosattie7997942009-06-11 12:07:40 -03001153static inline bool cpu_has_vmx_ept_execute_only(void)
1154{
Gui Jianfeng31299942010-03-15 17:29:09 +08001155 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001156}
1157
Marcelo Tosattie7997942009-06-11 12:07:40 -03001158static inline bool cpu_has_vmx_ept_2m_page(void)
1159{
Gui Jianfeng31299942010-03-15 17:29:09 +08001160 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001161}
1162
Sheng Yang878403b2010-01-05 19:02:29 +08001163static inline bool cpu_has_vmx_ept_1g_page(void)
1164{
Gui Jianfeng31299942010-03-15 17:29:09 +08001165 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001166}
1167
Sheng Yang4bc9b982010-06-02 14:05:24 +08001168static inline bool cpu_has_vmx_ept_4levels(void)
1169{
1170 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1171}
1172
Xudong Hao83c3a332012-05-28 19:33:35 +08001173static inline bool cpu_has_vmx_ept_ad_bits(void)
1174{
1175 return vmx_capability.ept & VMX_EPT_AD_BIT;
1176}
1177
Gui Jianfeng31299942010-03-15 17:29:09 +08001178static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001179{
Gui Jianfeng31299942010-03-15 17:29:09 +08001180 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001181}
1182
Gui Jianfeng31299942010-03-15 17:29:09 +08001183static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001184{
Gui Jianfeng31299942010-03-15 17:29:09 +08001185 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001186}
1187
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001188static inline bool cpu_has_vmx_invvpid_single(void)
1189{
1190 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1191}
1192
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001193static inline bool cpu_has_vmx_invvpid_global(void)
1194{
1195 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1196}
1197
Gui Jianfeng31299942010-03-15 17:29:09 +08001198static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001199{
Sheng Yang04547152009-04-01 15:52:31 +08001200 return vmcs_config.cpu_based_2nd_exec_ctrl &
1201 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001202}
1203
Gui Jianfeng31299942010-03-15 17:29:09 +08001204static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001205{
1206 return vmcs_config.cpu_based_2nd_exec_ctrl &
1207 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1208}
1209
Gui Jianfeng31299942010-03-15 17:29:09 +08001210static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001211{
1212 return vmcs_config.cpu_based_2nd_exec_ctrl &
1213 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1214}
1215
Paolo Bonzini35754c92015-07-29 12:05:37 +02001216static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001217{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001218 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001219}
1220
Gui Jianfeng31299942010-03-15 17:29:09 +08001221static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001222{
Sheng Yang04547152009-04-01 15:52:31 +08001223 return vmcs_config.cpu_based_2nd_exec_ctrl &
1224 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001225}
1226
Gui Jianfeng31299942010-03-15 17:29:09 +08001227static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001228{
1229 return vmcs_config.cpu_based_2nd_exec_ctrl &
1230 SECONDARY_EXEC_RDTSCP;
1231}
1232
Mao, Junjiead756a12012-07-02 01:18:48 +00001233static inline bool cpu_has_vmx_invpcid(void)
1234{
1235 return vmcs_config.cpu_based_2nd_exec_ctrl &
1236 SECONDARY_EXEC_ENABLE_INVPCID;
1237}
1238
Gui Jianfeng31299942010-03-15 17:29:09 +08001239static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001240{
1241 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1242}
1243
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001244static inline bool cpu_has_vmx_wbinvd_exit(void)
1245{
1246 return vmcs_config.cpu_based_2nd_exec_ctrl &
1247 SECONDARY_EXEC_WBINVD_EXITING;
1248}
1249
Abel Gordonabc4fc52013-04-18 14:35:25 +03001250static inline bool cpu_has_vmx_shadow_vmcs(void)
1251{
1252 u64 vmx_msr;
1253 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1254 /* check if the cpu supports writing r/o exit information fields */
1255 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1256 return false;
1257
1258 return vmcs_config.cpu_based_2nd_exec_ctrl &
1259 SECONDARY_EXEC_SHADOW_VMCS;
1260}
1261
Kai Huang843e4332015-01-28 10:54:28 +08001262static inline bool cpu_has_vmx_pml(void)
1263{
1264 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1265}
1266
Haozhong Zhang64903d62015-10-20 15:39:09 +08001267static inline bool cpu_has_vmx_tsc_scaling(void)
1268{
1269 return vmcs_config.cpu_based_2nd_exec_ctrl &
1270 SECONDARY_EXEC_TSC_SCALING;
1271}
1272
Sheng Yang04547152009-04-01 15:52:31 +08001273static inline bool report_flexpriority(void)
1274{
1275 return flexpriority_enabled;
1276}
1277
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001278static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1279{
1280 return vmcs12->cpu_based_vm_exec_control & bit;
1281}
1282
1283static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1284{
1285 return (vmcs12->cpu_based_vm_exec_control &
1286 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1287 (vmcs12->secondary_vm_exec_control & bit);
1288}
1289
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001290static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001291{
1292 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1293}
1294
Jan Kiszkaf4124502014-03-07 20:03:13 +01001295static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1296{
1297 return vmcs12->pin_based_vm_exec_control &
1298 PIN_BASED_VMX_PREEMPTION_TIMER;
1299}
1300
Nadav Har'El155a97a2013-08-05 11:07:16 +03001301static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1302{
1303 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1304}
1305
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001306static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1307{
1308 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1309 vmx_xsaves_supported();
1310}
1311
Wincy Vanf2b93282015-02-03 23:56:03 +08001312static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1313{
1314 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1315}
1316
Wanpeng Li5c614b32015-10-13 09:18:36 -07001317static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1318{
1319 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1320}
1321
Wincy Van82f0dd42015-02-03 23:57:18 +08001322static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1323{
1324 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1325}
1326
Wincy Van608406e2015-02-03 23:57:51 +08001327static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1328{
1329 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1330}
1331
Wincy Van705699a2015-02-03 23:58:17 +08001332static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1333{
1334 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1335}
1336
Nadav Har'El644d7112011-05-25 23:12:35 +03001337static inline bool is_exception(u32 intr_info)
1338{
1339 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1340 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1341}
1342
Jan Kiszka533558b2014-01-04 18:47:20 +01001343static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1344 u32 exit_intr_info,
1345 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001346static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1347 struct vmcs12 *vmcs12,
1348 u32 reason, unsigned long qualification);
1349
Rusty Russell8b9cf982007-07-30 16:31:43 +10001350static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001351{
1352 int i;
1353
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001354 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001355 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001356 return i;
1357 return -1;
1358}
1359
Sheng Yang2384d2b2008-01-17 15:14:33 +08001360static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1361{
1362 struct {
1363 u64 vpid : 16;
1364 u64 rsvd : 48;
1365 u64 gva;
1366 } operand = { vpid, 0, gva };
1367
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001368 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001369 /* CF==1 or ZF==1 --> rc = -1 */
1370 "; ja 1f ; ud2 ; 1:"
1371 : : "a"(&operand), "c"(ext) : "cc", "memory");
1372}
1373
Sheng Yang14394422008-04-28 12:24:45 +08001374static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1375{
1376 struct {
1377 u64 eptp, gpa;
1378 } operand = {eptp, gpa};
1379
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001380 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001381 /* CF==1 or ZF==1 --> rc = -1 */
1382 "; ja 1f ; ud2 ; 1:\n"
1383 : : "a" (&operand), "c" (ext) : "cc", "memory");
1384}
1385
Avi Kivity26bb0982009-09-07 11:14:12 +03001386static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001387{
1388 int i;
1389
Rusty Russell8b9cf982007-07-30 16:31:43 +10001390 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001391 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001392 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001393 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001394}
1395
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396static void vmcs_clear(struct vmcs *vmcs)
1397{
1398 u64 phys_addr = __pa(vmcs);
1399 u8 error;
1400
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001401 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001402 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403 : "cc", "memory");
1404 if (error)
1405 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1406 vmcs, phys_addr);
1407}
1408
Nadav Har'Eld462b812011-05-24 15:26:10 +03001409static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1410{
1411 vmcs_clear(loaded_vmcs->vmcs);
1412 loaded_vmcs->cpu = -1;
1413 loaded_vmcs->launched = 0;
1414}
1415
Dongxiao Xu7725b892010-05-11 18:29:38 +08001416static void vmcs_load(struct vmcs *vmcs)
1417{
1418 u64 phys_addr = __pa(vmcs);
1419 u8 error;
1420
1421 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001422 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001423 : "cc", "memory");
1424 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001425 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001426 vmcs, phys_addr);
1427}
1428
Dave Young2965faa2015-09-09 15:38:55 -07001429#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001430/*
1431 * This bitmap is used to indicate whether the vmclear
1432 * operation is enabled on all cpus. All disabled by
1433 * default.
1434 */
1435static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1436
1437static inline void crash_enable_local_vmclear(int cpu)
1438{
1439 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1440}
1441
1442static inline void crash_disable_local_vmclear(int cpu)
1443{
1444 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1445}
1446
1447static inline int crash_local_vmclear_enabled(int cpu)
1448{
1449 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1450}
1451
1452static void crash_vmclear_local_loaded_vmcss(void)
1453{
1454 int cpu = raw_smp_processor_id();
1455 struct loaded_vmcs *v;
1456
1457 if (!crash_local_vmclear_enabled(cpu))
1458 return;
1459
1460 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1461 loaded_vmcss_on_cpu_link)
1462 vmcs_clear(v->vmcs);
1463}
1464#else
1465static inline void crash_enable_local_vmclear(int cpu) { }
1466static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001467#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001468
Nadav Har'Eld462b812011-05-24 15:26:10 +03001469static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001471 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001472 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473
Nadav Har'Eld462b812011-05-24 15:26:10 +03001474 if (loaded_vmcs->cpu != cpu)
1475 return; /* vcpu migration can race with cpu offline */
1476 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001478 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001479 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001480
1481 /*
1482 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1483 * is before setting loaded_vmcs->vcpu to -1 which is done in
1484 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1485 * then adds the vmcs into percpu list before it is deleted.
1486 */
1487 smp_wmb();
1488
Nadav Har'Eld462b812011-05-24 15:26:10 +03001489 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001490 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001491}
1492
Nadav Har'Eld462b812011-05-24 15:26:10 +03001493static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001494{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001495 int cpu = loaded_vmcs->cpu;
1496
1497 if (cpu != -1)
1498 smp_call_function_single(cpu,
1499 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001500}
1501
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001502static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001503{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001504 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001505 return;
1506
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001507 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001508 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001509}
1510
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001511static inline void vpid_sync_vcpu_global(void)
1512{
1513 if (cpu_has_vmx_invvpid_global())
1514 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1515}
1516
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001517static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001518{
1519 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001520 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001521 else
1522 vpid_sync_vcpu_global();
1523}
1524
Sheng Yang14394422008-04-28 12:24:45 +08001525static inline void ept_sync_global(void)
1526{
1527 if (cpu_has_vmx_invept_global())
1528 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1529}
1530
1531static inline void ept_sync_context(u64 eptp)
1532{
Avi Kivity089d0342009-03-23 18:26:32 +02001533 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001534 if (cpu_has_vmx_invept_context())
1535 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1536 else
1537 ept_sync_global();
1538 }
1539}
1540
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001541static __always_inline void vmcs_check16(unsigned long field)
1542{
1543 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1544 "16-bit accessor invalid for 64-bit field");
1545 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1546 "16-bit accessor invalid for 64-bit high field");
1547 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1548 "16-bit accessor invalid for 32-bit high field");
1549 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1550 "16-bit accessor invalid for natural width field");
1551}
1552
1553static __always_inline void vmcs_check32(unsigned long field)
1554{
1555 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1556 "32-bit accessor invalid for 16-bit field");
1557 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1558 "32-bit accessor invalid for natural width field");
1559}
1560
1561static __always_inline void vmcs_check64(unsigned long field)
1562{
1563 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1564 "64-bit accessor invalid for 16-bit field");
1565 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1566 "64-bit accessor invalid for 64-bit high field");
1567 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1568 "64-bit accessor invalid for 32-bit field");
1569 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1570 "64-bit accessor invalid for natural width field");
1571}
1572
1573static __always_inline void vmcs_checkl(unsigned long field)
1574{
1575 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1576 "Natural width accessor invalid for 16-bit field");
1577 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1578 "Natural width accessor invalid for 64-bit field");
1579 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1580 "Natural width accessor invalid for 64-bit high field");
1581 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1582 "Natural width accessor invalid for 32-bit field");
1583}
1584
1585static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586{
Avi Kivity5e520e62011-05-15 10:13:12 -04001587 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588
Avi Kivity5e520e62011-05-15 10:13:12 -04001589 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1590 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001591 return value;
1592}
1593
Avi Kivity96304212011-05-15 10:13:13 -04001594static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001596 vmcs_check16(field);
1597 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001598}
1599
Avi Kivity96304212011-05-15 10:13:13 -04001600static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001601{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001602 vmcs_check32(field);
1603 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001604}
1605
Avi Kivity96304212011-05-15 10:13:13 -04001606static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001608 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001609#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001610 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001612 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613#endif
1614}
1615
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001616static __always_inline unsigned long vmcs_readl(unsigned long field)
1617{
1618 vmcs_checkl(field);
1619 return __vmcs_readl(field);
1620}
1621
Avi Kivitye52de1b2007-01-05 16:36:56 -08001622static noinline void vmwrite_error(unsigned long field, unsigned long value)
1623{
1624 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1625 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1626 dump_stack();
1627}
1628
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001629static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630{
1631 u8 error;
1632
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001633 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001634 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001635 if (unlikely(error))
1636 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637}
1638
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001639static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001641 vmcs_check16(field);
1642 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643}
1644
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001645static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001647 vmcs_check32(field);
1648 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649}
1650
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001651static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001653 vmcs_check64(field);
1654 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001655#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001657 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658#endif
1659}
1660
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001661static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001662{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001663 vmcs_checkl(field);
1664 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001665}
1666
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001667static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001668{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001669 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1670 "vmcs_clear_bits does not support 64-bit fields");
1671 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1672}
1673
1674static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1675{
1676 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1677 "vmcs_set_bits does not support 64-bit fields");
1678 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001679}
1680
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001681static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1682{
1683 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1684}
1685
Gleb Natapov2961e8762013-11-25 15:37:13 +02001686static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1687{
1688 vmcs_write32(VM_ENTRY_CONTROLS, val);
1689 vmx->vm_entry_controls_shadow = val;
1690}
1691
1692static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1693{
1694 if (vmx->vm_entry_controls_shadow != val)
1695 vm_entry_controls_init(vmx, val);
1696}
1697
1698static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1699{
1700 return vmx->vm_entry_controls_shadow;
1701}
1702
1703
1704static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1705{
1706 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1707}
1708
1709static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1710{
1711 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1712}
1713
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001714static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1715{
1716 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1717}
1718
Gleb Natapov2961e8762013-11-25 15:37:13 +02001719static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1720{
1721 vmcs_write32(VM_EXIT_CONTROLS, val);
1722 vmx->vm_exit_controls_shadow = val;
1723}
1724
1725static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1726{
1727 if (vmx->vm_exit_controls_shadow != val)
1728 vm_exit_controls_init(vmx, val);
1729}
1730
1731static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1732{
1733 return vmx->vm_exit_controls_shadow;
1734}
1735
1736
1737static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1738{
1739 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1740}
1741
1742static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1743{
1744 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1745}
1746
Avi Kivity2fb92db2011-04-27 19:42:18 +03001747static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1748{
1749 vmx->segment_cache.bitmask = 0;
1750}
1751
1752static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1753 unsigned field)
1754{
1755 bool ret;
1756 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1757
1758 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1759 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1760 vmx->segment_cache.bitmask = 0;
1761 }
1762 ret = vmx->segment_cache.bitmask & mask;
1763 vmx->segment_cache.bitmask |= mask;
1764 return ret;
1765}
1766
1767static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1768{
1769 u16 *p = &vmx->segment_cache.seg[seg].selector;
1770
1771 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1772 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1773 return *p;
1774}
1775
1776static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1777{
1778 ulong *p = &vmx->segment_cache.seg[seg].base;
1779
1780 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1781 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1782 return *p;
1783}
1784
1785static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1786{
1787 u32 *p = &vmx->segment_cache.seg[seg].limit;
1788
1789 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1790 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1791 return *p;
1792}
1793
1794static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1795{
1796 u32 *p = &vmx->segment_cache.seg[seg].ar;
1797
1798 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1799 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1800 return *p;
1801}
1802
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001803static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1804{
1805 u32 eb;
1806
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001807 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Eric Northup54a20552015-11-03 18:03:53 +01001808 (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001809 if ((vcpu->guest_debug &
1810 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1811 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1812 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001813 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001814 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001815 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001816 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001817 if (vcpu->fpu_active)
1818 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001819
1820 /* When we are running a nested L2 guest and L1 specified for it a
1821 * certain exception bitmap, we must trap the same exceptions and pass
1822 * them to L1. When running L2, we will only handle the exceptions
1823 * specified above if L1 did not want them.
1824 */
1825 if (is_guest_mode(vcpu))
1826 eb |= get_vmcs12(vcpu)->exception_bitmap;
1827
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001828 vmcs_write32(EXCEPTION_BITMAP, eb);
1829}
1830
Gleb Natapov2961e8762013-11-25 15:37:13 +02001831static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1832 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001833{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001834 vm_entry_controls_clearbit(vmx, entry);
1835 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001836}
1837
Avi Kivity61d2ef22010-04-28 16:40:38 +03001838static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1839{
1840 unsigned i;
1841 struct msr_autoload *m = &vmx->msr_autoload;
1842
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001843 switch (msr) {
1844 case MSR_EFER:
1845 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001846 clear_atomic_switch_msr_special(vmx,
1847 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001848 VM_EXIT_LOAD_IA32_EFER);
1849 return;
1850 }
1851 break;
1852 case MSR_CORE_PERF_GLOBAL_CTRL:
1853 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001854 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001855 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1856 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1857 return;
1858 }
1859 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001860 }
1861
Avi Kivity61d2ef22010-04-28 16:40:38 +03001862 for (i = 0; i < m->nr; ++i)
1863 if (m->guest[i].index == msr)
1864 break;
1865
1866 if (i == m->nr)
1867 return;
1868 --m->nr;
1869 m->guest[i] = m->guest[m->nr];
1870 m->host[i] = m->host[m->nr];
1871 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1872 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1873}
1874
Gleb Natapov2961e8762013-11-25 15:37:13 +02001875static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1876 unsigned long entry, unsigned long exit,
1877 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1878 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001879{
1880 vmcs_write64(guest_val_vmcs, guest_val);
1881 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001882 vm_entry_controls_setbit(vmx, entry);
1883 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001884}
1885
Avi Kivity61d2ef22010-04-28 16:40:38 +03001886static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1887 u64 guest_val, u64 host_val)
1888{
1889 unsigned i;
1890 struct msr_autoload *m = &vmx->msr_autoload;
1891
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001892 switch (msr) {
1893 case MSR_EFER:
1894 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001895 add_atomic_switch_msr_special(vmx,
1896 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001897 VM_EXIT_LOAD_IA32_EFER,
1898 GUEST_IA32_EFER,
1899 HOST_IA32_EFER,
1900 guest_val, host_val);
1901 return;
1902 }
1903 break;
1904 case MSR_CORE_PERF_GLOBAL_CTRL:
1905 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001906 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001907 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1908 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1909 GUEST_IA32_PERF_GLOBAL_CTRL,
1910 HOST_IA32_PERF_GLOBAL_CTRL,
1911 guest_val, host_val);
1912 return;
1913 }
1914 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001915 case MSR_IA32_PEBS_ENABLE:
1916 /* PEBS needs a quiescent period after being disabled (to write
1917 * a record). Disabling PEBS through VMX MSR swapping doesn't
1918 * provide that period, so a CPU could write host's record into
1919 * guest's memory.
1920 */
1921 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001922 }
1923
Avi Kivity61d2ef22010-04-28 16:40:38 +03001924 for (i = 0; i < m->nr; ++i)
1925 if (m->guest[i].index == msr)
1926 break;
1927
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001928 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001929 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001930 "Can't add msr %x\n", msr);
1931 return;
1932 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001933 ++m->nr;
1934 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1935 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1936 }
1937
1938 m->guest[i].index = msr;
1939 m->guest[i].value = guest_val;
1940 m->host[i].index = msr;
1941 m->host[i].value = host_val;
1942}
1943
Avi Kivity33ed6322007-05-02 16:54:03 +03001944static void reload_tss(void)
1945{
Avi Kivity33ed6322007-05-02 16:54:03 +03001946 /*
1947 * VT restores TR but not its size. Useless.
1948 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001949 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001950 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001951
Avi Kivityd3591922010-07-26 18:32:39 +03001952 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001953 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1954 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001955}
1956
Avi Kivity92c0d902009-10-29 11:00:16 +02001957static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001958{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001959 u64 guest_efer = vmx->vcpu.arch.efer;
1960 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03001961
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001962 if (!enable_ept) {
1963 /*
1964 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1965 * host CPUID is more efficient than testing guest CPUID
1966 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
1967 */
1968 if (boot_cpu_has(X86_FEATURE_SMEP))
1969 guest_efer |= EFER_NX;
1970 else if (!(guest_efer & EFER_NX))
1971 ignore_bits |= EFER_NX;
1972 }
Roel Kluin3a34a882009-08-04 02:08:45 -07001973
Avi Kivity51c6cf62007-08-29 03:48:05 +03001974 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001975 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03001976 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001977 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001978#ifdef CONFIG_X86_64
1979 ignore_bits |= EFER_LMA | EFER_LME;
1980 /* SCE is meaningful only in long mode on Intel */
1981 if (guest_efer & EFER_LMA)
1982 ignore_bits &= ~(u64)EFER_SCE;
1983#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03001984
1985 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001986
1987 /*
1988 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1989 * On CPUs that support "load IA32_EFER", always switch EFER
1990 * atomically, since it's faster than switching it manually.
1991 */
1992 if (cpu_has_load_ia32_efer ||
1993 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001994 if (!(guest_efer & EFER_LMA))
1995 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001996 if (guest_efer != host_efer)
1997 add_atomic_switch_msr(vmx, MSR_EFER,
1998 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001999 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002000 } else {
2001 guest_efer &= ~ignore_bits;
2002 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002003
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002004 vmx->guest_msrs[efer_offset].data = guest_efer;
2005 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2006
2007 return true;
2008 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002009}
2010
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002011static unsigned long segment_base(u16 selector)
2012{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002013 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002014 struct desc_struct *d;
2015 unsigned long table_base;
2016 unsigned long v;
2017
2018 if (!(selector & ~3))
2019 return 0;
2020
Avi Kivityd3591922010-07-26 18:32:39 +03002021 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002022
2023 if (selector & 4) { /* from ldt */
2024 u16 ldt_selector = kvm_read_ldt();
2025
2026 if (!(ldt_selector & ~3))
2027 return 0;
2028
2029 table_base = segment_base(ldt_selector);
2030 }
2031 d = (struct desc_struct *)(table_base + (selector & ~7));
2032 v = get_desc_base(d);
2033#ifdef CONFIG_X86_64
2034 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
2035 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
2036#endif
2037 return v;
2038}
2039
2040static inline unsigned long kvm_read_tr_base(void)
2041{
2042 u16 tr;
2043 asm("str %0" : "=g"(tr));
2044 return segment_base(tr);
2045}
2046
Avi Kivity04d2cc72007-09-10 18:10:54 +03002047static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002048{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002049 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002050 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002051
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002052 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002053 return;
2054
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002055 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002056 /*
2057 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2058 * allow segment selectors with cpl > 0 or ti == 1.
2059 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002060 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002061 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002062 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002063 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002064 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002065 vmx->host_state.fs_reload_needed = 0;
2066 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002067 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002068 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002069 }
Avi Kivity9581d442010-10-19 16:46:55 +02002070 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002071 if (!(vmx->host_state.gs_sel & 7))
2072 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002073 else {
2074 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002075 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002076 }
2077
2078#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002079 savesegment(ds, vmx->host_state.ds_sel);
2080 savesegment(es, vmx->host_state.es_sel);
2081#endif
2082
2083#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002084 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2085 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2086#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002087 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2088 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002089#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002090
2091#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002092 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2093 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002094 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002095#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002096 if (boot_cpu_has(X86_FEATURE_MPX))
2097 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002098 for (i = 0; i < vmx->save_nmsrs; ++i)
2099 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002100 vmx->guest_msrs[i].data,
2101 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002102}
2103
Avi Kivitya9b21b62008-06-24 11:48:49 +03002104static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002105{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002106 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002107 return;
2108
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002109 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002110 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002111#ifdef CONFIG_X86_64
2112 if (is_long_mode(&vmx->vcpu))
2113 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2114#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002115 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002116 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002117#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002118 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002119#else
2120 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002121#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002122 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002123 if (vmx->host_state.fs_reload_needed)
2124 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002125#ifdef CONFIG_X86_64
2126 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2127 loadsegment(ds, vmx->host_state.ds_sel);
2128 loadsegment(es, vmx->host_state.es_sel);
2129 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002130#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002131 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002132#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002133 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002134#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002135 if (vmx->host_state.msr_host_bndcfgs)
2136 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002137 /*
2138 * If the FPU is not active (through the host task or
2139 * the guest vcpu), then restore the cr0.TS bit.
2140 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02002141 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07002142 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05002143 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002144}
2145
Avi Kivitya9b21b62008-06-24 11:48:49 +03002146static void vmx_load_host_state(struct vcpu_vmx *vmx)
2147{
2148 preempt_disable();
2149 __vmx_load_host_state(vmx);
2150 preempt_enable();
2151}
2152
Feng Wu28b835d2015-09-18 22:29:54 +08002153static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2154{
2155 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2156 struct pi_desc old, new;
2157 unsigned int dest;
2158
2159 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002160 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2161 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002162 return;
2163
2164 do {
2165 old.control = new.control = pi_desc->control;
2166
2167 /*
2168 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
2169 * are two possible cases:
2170 * 1. After running 'pre_block', context switch
2171 * happened. For this case, 'sn' was set in
2172 * vmx_vcpu_put(), so we need to clear it here.
2173 * 2. After running 'pre_block', we were blocked,
2174 * and woken up by some other guy. For this case,
2175 * we don't need to do anything, 'pi_post_block'
2176 * will do everything for us. However, we cannot
2177 * check whether it is case #1 or case #2 here
2178 * (maybe, not needed), so we also clear sn here,
2179 * I think it is not a big deal.
2180 */
2181 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
2182 if (vcpu->cpu != cpu) {
2183 dest = cpu_physical_id(cpu);
2184
2185 if (x2apic_enabled())
2186 new.ndst = dest;
2187 else
2188 new.ndst = (dest << 8) & 0xFF00;
2189 }
2190
2191 /* set 'NV' to 'notification vector' */
2192 new.nv = POSTED_INTR_VECTOR;
2193 }
2194
2195 /* Allow posting non-urgent interrupts */
2196 new.sn = 0;
2197 } while (cmpxchg(&pi_desc->control, old.control,
2198 new.control) != old.control);
2199}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002200
Avi Kivity6aa8b732006-12-10 02:21:36 -08002201/*
2202 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2203 * vcpu mutex is already taken.
2204 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002205static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002206{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002207 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002208 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002209 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002211 if (!vmm_exclusive)
2212 kvm_cpu_vmxon(phys_addr);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002213 else if (!already_loaded)
Nadav Har'Eld462b812011-05-24 15:26:10 +03002214 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002216 if (!already_loaded) {
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002217 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002218 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002219
2220 /*
2221 * Read loaded_vmcs->cpu should be before fetching
2222 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2223 * See the comments in __loaded_vmcs_clear().
2224 */
2225 smp_rmb();
2226
Nadav Har'Eld462b812011-05-24 15:26:10 +03002227 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2228 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002229 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002230 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002231 }
2232
2233 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2234 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2235 vmcs_load(vmx->loaded_vmcs->vmcs);
2236 }
2237
2238 if (!already_loaded) {
2239 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2240 unsigned long sysenter_esp;
2241
2242 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002243
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 /*
2245 * Linux uses per-cpu TSS and GDT, so set these when switching
2246 * processors.
2247 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002248 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002249 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250
2251 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2252 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002253
Nadav Har'Eld462b812011-05-24 15:26:10 +03002254 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 }
Feng Wu28b835d2015-09-18 22:29:54 +08002256
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002257 /* Setup TSC multiplier */
2258 if (kvm_has_tsc_control &&
2259 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) {
2260 vmx->current_tsc_ratio = vcpu->arch.tsc_scaling_ratio;
2261 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2262 }
2263
Feng Wu28b835d2015-09-18 22:29:54 +08002264 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002265 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002266}
2267
2268static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2269{
2270 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2271
2272 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002273 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2274 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002275 return;
2276
2277 /* Set SN when the vCPU is preempted */
2278 if (vcpu->preempted)
2279 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280}
2281
2282static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2283{
Feng Wu28b835d2015-09-18 22:29:54 +08002284 vmx_vcpu_pi_put(vcpu);
2285
Avi Kivitya9b21b62008-06-24 11:48:49 +03002286 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002287 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002288 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2289 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002290 kvm_cpu_vmxoff();
2291 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002292}
2293
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002294static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2295{
Avi Kivity81231c62010-01-24 16:26:40 +02002296 ulong cr0;
2297
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002298 if (vcpu->fpu_active)
2299 return;
2300 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002301 cr0 = vmcs_readl(GUEST_CR0);
2302 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2303 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2304 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002305 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002306 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002307 if (is_guest_mode(vcpu))
2308 vcpu->arch.cr0_guest_owned_bits &=
2309 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002310 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002311}
2312
Avi Kivityedcafe32009-12-30 18:07:40 +02002313static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2314
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002315/*
2316 * Return the cr0 value that a nested guest would read. This is a combination
2317 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2318 * its hypervisor (cr0_read_shadow).
2319 */
2320static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2321{
2322 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2323 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2324}
2325static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2326{
2327 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2328 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2329}
2330
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002331static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2332{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002333 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2334 * set this *before* calling this function.
2335 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002336 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002337 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002338 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002339 vcpu->arch.cr0_guest_owned_bits = 0;
2340 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002341 if (is_guest_mode(vcpu)) {
2342 /*
2343 * L1's specified read shadow might not contain the TS bit,
2344 * so now that we turned on shadowing of this bit, we need to
2345 * set this bit of the shadow. Like in nested_vmx_run we need
2346 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2347 * up-to-date here because we just decached cr0.TS (and we'll
2348 * only update vmcs12->guest_cr0 on nested exit).
2349 */
2350 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2351 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2352 (vcpu->arch.cr0 & X86_CR0_TS);
2353 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2354 } else
2355 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002356}
2357
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2359{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002360 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002361
Avi Kivity6de12732011-03-07 12:51:22 +02002362 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2363 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2364 rflags = vmcs_readl(GUEST_RFLAGS);
2365 if (to_vmx(vcpu)->rmode.vm86_active) {
2366 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2367 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2368 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2369 }
2370 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002371 }
Avi Kivity6de12732011-03-07 12:51:22 +02002372 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373}
2374
2375static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2376{
Avi Kivity6de12732011-03-07 12:51:22 +02002377 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2378 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002379 if (to_vmx(vcpu)->rmode.vm86_active) {
2380 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002381 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002382 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383 vmcs_writel(GUEST_RFLAGS, rflags);
2384}
2385
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002386static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2387{
2388 return to_vmx(vcpu)->guest_pkru;
2389}
2390
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002391static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002392{
2393 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2394 int ret = 0;
2395
2396 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002397 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002398 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002399 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002400
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002401 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002402}
2403
2404static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2405{
2406 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2407 u32 interruptibility = interruptibility_old;
2408
2409 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2410
Jan Kiszka48005f62010-02-19 19:38:07 +01002411 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002412 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002413 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002414 interruptibility |= GUEST_INTR_STATE_STI;
2415
2416 if ((interruptibility != interruptibility_old))
2417 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2418}
2419
Avi Kivity6aa8b732006-12-10 02:21:36 -08002420static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2421{
2422 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002424 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002425 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002426 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002427
Glauber Costa2809f5d2009-05-12 16:21:05 -04002428 /* skipping an emulated instruction also counts */
2429 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002430}
2431
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002432/*
2433 * KVM wants to inject page-faults which it got to the guest. This function
2434 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002435 */
Gleb Natapove011c662013-09-25 12:51:35 +03002436static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002437{
2438 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2439
Gleb Natapove011c662013-09-25 12:51:35 +03002440 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002441 return 0;
2442
Jan Kiszka533558b2014-01-04 18:47:20 +01002443 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2444 vmcs_read32(VM_EXIT_INTR_INFO),
2445 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002446 return 1;
2447}
2448
Avi Kivity298101d2007-11-25 13:41:11 +02002449static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002450 bool has_error_code, u32 error_code,
2451 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002452{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002453 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002454 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002455
Gleb Natapove011c662013-09-25 12:51:35 +03002456 if (!reinject && is_guest_mode(vcpu) &&
2457 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002458 return;
2459
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002460 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002461 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002462 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2463 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002464
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002465 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002466 int inc_eip = 0;
2467 if (kvm_exception_is_soft(nr))
2468 inc_eip = vcpu->arch.event_exit_inst_len;
2469 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002470 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002471 return;
2472 }
2473
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002474 if (kvm_exception_is_soft(nr)) {
2475 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2476 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002477 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2478 } else
2479 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2480
2481 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002482}
2483
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002484static bool vmx_rdtscp_supported(void)
2485{
2486 return cpu_has_vmx_rdtscp();
2487}
2488
Mao, Junjiead756a12012-07-02 01:18:48 +00002489static bool vmx_invpcid_supported(void)
2490{
2491 return cpu_has_vmx_invpcid() && enable_ept;
2492}
2493
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494/*
Eddie Donga75beee2007-05-17 18:55:15 +03002495 * Swap MSR entry in host/guest MSR entry array.
2496 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002497static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002498{
Avi Kivity26bb0982009-09-07 11:14:12 +03002499 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002500
2501 tmp = vmx->guest_msrs[to];
2502 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2503 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002504}
2505
Yang Zhang8d146952013-01-25 10:18:50 +08002506static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2507{
2508 unsigned long *msr_bitmap;
2509
Wincy Van670125b2015-03-04 14:31:56 +08002510 if (is_guest_mode(vcpu))
2511 msr_bitmap = vmx_msr_bitmap_nested;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002512 else if (cpu_has_secondary_exec_ctrls() &&
2513 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2514 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Yang Zhang8d146952013-01-25 10:18:50 +08002515 if (is_long_mode(vcpu))
2516 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2517 else
2518 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2519 } else {
2520 if (is_long_mode(vcpu))
2521 msr_bitmap = vmx_msr_bitmap_longmode;
2522 else
2523 msr_bitmap = vmx_msr_bitmap_legacy;
2524 }
2525
2526 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2527}
2528
Eddie Donga75beee2007-05-17 18:55:15 +03002529/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002530 * Set up the vmcs to automatically save and restore system
2531 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2532 * mode, as fiddling with msrs is very expensive.
2533 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002534static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002535{
Avi Kivity26bb0982009-09-07 11:14:12 +03002536 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002537
Eddie Donga75beee2007-05-17 18:55:15 +03002538 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002539#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002540 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002541 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002542 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002543 move_msr_up(vmx, index, save_nmsrs++);
2544 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002545 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002546 move_msr_up(vmx, index, save_nmsrs++);
2547 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002548 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002549 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002550 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002551 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002552 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002553 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002554 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002555 * if efer.sce is enabled.
2556 */
Brian Gerst8c065852010-07-17 09:03:26 -04002557 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002558 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002559 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002560 }
Eddie Donga75beee2007-05-17 18:55:15 +03002561#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002562 index = __find_msr_index(vmx, MSR_EFER);
2563 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002564 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002565
Avi Kivity26bb0982009-09-07 11:14:12 +03002566 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002567
Yang Zhang8d146952013-01-25 10:18:50 +08002568 if (cpu_has_vmx_msr_bitmap())
2569 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002570}
2571
2572/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002574 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2575 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002577static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578{
2579 u64 host_tsc, tsc_offset;
2580
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002581 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002583 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584}
2585
2586/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002587 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2588 * counter, even if a nested guest (L2) is currently running.
2589 */
Paolo Bonzini48d89b92014-08-26 13:27:46 +02002590static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002591{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002592 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002593
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002594 tsc_offset = is_guest_mode(vcpu) ?
2595 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2596 vmcs_read64(TSC_OFFSET);
2597 return host_tsc + tsc_offset;
2598}
2599
Will Auldba904632012-11-29 12:42:50 -08002600static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2601{
2602 return vmcs_read64(TSC_OFFSET);
2603}
2604
Joerg Roedel4051b182011-03-25 09:44:49 +01002605/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002606 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002608static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002610 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002611 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002612 * We're here if L1 chose not to trap WRMSR to TSC. According
2613 * to the spec, this should set L1's TSC; The offset that L1
2614 * set for L2 remains unchanged, and still needs to be added
2615 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002616 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002617 struct vmcs12 *vmcs12;
2618 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2619 /* 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
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002631static void vmx_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002632{
2633 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002634
Zachary Amsdene48672f2010-08-19 22:07:23 -10002635 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002636 if (is_guest_mode(vcpu)) {
2637 /* Even when running L2, the adjustment needs to apply to L1 */
2638 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002639 } else
2640 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2641 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002642}
2643
Nadav Har'El801d3422011-05-25 23:02:23 +03002644static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2645{
2646 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2647 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2648}
2649
2650/*
2651 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2652 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2653 * all guests if the "nested" module option is off, and can also be disabled
2654 * for a single guest by disabling its VMX cpuid bit.
2655 */
2656static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2657{
2658 return nested && guest_cpuid_has_vmx(vcpu);
2659}
2660
Avi Kivity6aa8b732006-12-10 02:21:36 -08002661/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002662 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2663 * returned for the various VMX controls MSRs when nested VMX is enabled.
2664 * The same values should also be used to verify that vmcs12 control fields are
2665 * valid during nested entry from L1 to L2.
2666 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2667 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2668 * bit in the high half is on if the corresponding bit in the control field
2669 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002670 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002671static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002672{
2673 /*
2674 * Note that as a general rule, the high half of the MSRs (bits in
2675 * the control fields which may be 1) should be initialized by the
2676 * intersection of the underlying hardware's MSR (i.e., features which
2677 * can be supported) and the list of features we want to expose -
2678 * because they are known to be properly supported in our code.
2679 * Also, usually, the low half of the MSRs (bits which must be 1) can
2680 * be set to 0, meaning that L1 may turn off any of these bits. The
2681 * reason is that if one of these bits is necessary, it will appear
2682 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2683 * fields of vmcs01 and vmcs02, will turn these bits off - and
2684 * nested_vmx_exit_handled() will not pass related exits to L1.
2685 * These rules have exceptions below.
2686 */
2687
2688 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002689 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002690 vmx->nested.nested_vmx_pinbased_ctls_low,
2691 vmx->nested.nested_vmx_pinbased_ctls_high);
2692 vmx->nested.nested_vmx_pinbased_ctls_low |=
2693 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2694 vmx->nested.nested_vmx_pinbased_ctls_high &=
2695 PIN_BASED_EXT_INTR_MASK |
2696 PIN_BASED_NMI_EXITING |
2697 PIN_BASED_VIRTUAL_NMIS;
2698 vmx->nested.nested_vmx_pinbased_ctls_high |=
2699 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002700 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002701 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002702 vmx->nested.nested_vmx_pinbased_ctls_high |=
2703 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002704
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002705 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002706 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002707 vmx->nested.nested_vmx_exit_ctls_low,
2708 vmx->nested.nested_vmx_exit_ctls_high);
2709 vmx->nested.nested_vmx_exit_ctls_low =
2710 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002711
Wincy Vanb9c237b2015-02-03 23:56:30 +08002712 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002713#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002714 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002715#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002716 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002717 vmx->nested.nested_vmx_exit_ctls_high |=
2718 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002719 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002720 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2721
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002722 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002723 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002724
Jan Kiszka2996fca2014-06-16 13:59:43 +02002725 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002726 vmx->nested.nested_vmx_true_exit_ctls_low =
2727 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002728 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2729
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002730 /* entry controls */
2731 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002732 vmx->nested.nested_vmx_entry_ctls_low,
2733 vmx->nested.nested_vmx_entry_ctls_high);
2734 vmx->nested.nested_vmx_entry_ctls_low =
2735 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2736 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002737#ifdef CONFIG_X86_64
2738 VM_ENTRY_IA32E_MODE |
2739#endif
2740 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002741 vmx->nested.nested_vmx_entry_ctls_high |=
2742 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002743 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002744 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002745
Jan Kiszka2996fca2014-06-16 13:59:43 +02002746 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002747 vmx->nested.nested_vmx_true_entry_ctls_low =
2748 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002749 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2750
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002751 /* cpu-based controls */
2752 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002753 vmx->nested.nested_vmx_procbased_ctls_low,
2754 vmx->nested.nested_vmx_procbased_ctls_high);
2755 vmx->nested.nested_vmx_procbased_ctls_low =
2756 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2757 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002758 CPU_BASED_VIRTUAL_INTR_PENDING |
2759 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002760 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2761 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2762 CPU_BASED_CR3_STORE_EXITING |
2763#ifdef CONFIG_X86_64
2764 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2765#endif
2766 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002767 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2768 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2769 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2770 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002771 /*
2772 * We can allow some features even when not supported by the
2773 * hardware. For example, L1 can specify an MSR bitmap - and we
2774 * can use it to avoid exits to L1 - even when L0 runs L2
2775 * without MSR bitmaps.
2776 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002777 vmx->nested.nested_vmx_procbased_ctls_high |=
2778 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002779 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002780
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002781 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002782 vmx->nested.nested_vmx_true_procbased_ctls_low =
2783 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002784 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2785
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002786 /* secondary cpu-based controls */
2787 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002788 vmx->nested.nested_vmx_secondary_ctls_low,
2789 vmx->nested.nested_vmx_secondary_ctls_high);
2790 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2791 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002792 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002793 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002794 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002795 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002796 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002797 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002798 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002799 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002800
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002801 if (enable_ept) {
2802 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002803 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002804 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002805 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002806 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2807 VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002808 if (cpu_has_vmx_ept_execute_only())
2809 vmx->nested.nested_vmx_ept_caps |=
2810 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002811 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002812 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2813 VMX_EPT_EXTENT_CONTEXT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002814 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002815 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002816
Paolo Bonzinief697a72016-03-18 16:58:38 +01002817 /*
2818 * Old versions of KVM use the single-context version without
2819 * checking for support, so declare that it is supported even
2820 * though it is treated as global context. The alternative is
2821 * not failing the single-context invvpid, and it is worse.
2822 */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002823 if (enable_vpid)
2824 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Paolo Bonzinief697a72016-03-18 16:58:38 +01002825 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT |
Wanpeng Li089d7b62015-10-13 09:18:37 -07002826 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
2827 else
2828 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002829
Radim Krčmář0790ec12015-03-17 14:02:32 +01002830 if (enable_unrestricted_guest)
2831 vmx->nested.nested_vmx_secondary_ctls_high |=
2832 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2833
Jan Kiszkac18911a2013-03-13 16:06:41 +01002834 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002835 rdmsr(MSR_IA32_VMX_MISC,
2836 vmx->nested.nested_vmx_misc_low,
2837 vmx->nested.nested_vmx_misc_high);
2838 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2839 vmx->nested.nested_vmx_misc_low |=
2840 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002841 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002842 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002843}
2844
2845static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2846{
2847 /*
2848 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2849 */
2850 return ((control & high) | low) == control;
2851}
2852
2853static inline u64 vmx_control_msr(u32 low, u32 high)
2854{
2855 return low | ((u64)high << 32);
2856}
2857
Jan Kiszkacae50132014-01-04 18:47:22 +01002858/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002859static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2860{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002861 struct vcpu_vmx *vmx = to_vmx(vcpu);
2862
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002863 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002864 case MSR_IA32_VMX_BASIC:
2865 /*
2866 * This MSR reports some information about VMX support. We
2867 * should return information about the VMX we emulate for the
2868 * guest, and the VMCS structure we give it - not about the
2869 * VMX support of the underlying hardware.
2870 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002871 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002872 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2873 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2874 break;
2875 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2876 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002877 *pdata = vmx_control_msr(
2878 vmx->nested.nested_vmx_pinbased_ctls_low,
2879 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002880 break;
2881 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002882 *pdata = vmx_control_msr(
2883 vmx->nested.nested_vmx_true_procbased_ctls_low,
2884 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002885 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002886 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002887 *pdata = vmx_control_msr(
2888 vmx->nested.nested_vmx_procbased_ctls_low,
2889 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002890 break;
2891 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002892 *pdata = vmx_control_msr(
2893 vmx->nested.nested_vmx_true_exit_ctls_low,
2894 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002895 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002896 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002897 *pdata = vmx_control_msr(
2898 vmx->nested.nested_vmx_exit_ctls_low,
2899 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002900 break;
2901 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002902 *pdata = vmx_control_msr(
2903 vmx->nested.nested_vmx_true_entry_ctls_low,
2904 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002905 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002906 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002907 *pdata = vmx_control_msr(
2908 vmx->nested.nested_vmx_entry_ctls_low,
2909 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002910 break;
2911 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002912 *pdata = vmx_control_msr(
2913 vmx->nested.nested_vmx_misc_low,
2914 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002915 break;
2916 /*
2917 * These MSRs specify bits which the guest must keep fixed (on or off)
2918 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2919 * We picked the standard core2 setting.
2920 */
2921#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2922#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2923 case MSR_IA32_VMX_CR0_FIXED0:
2924 *pdata = VMXON_CR0_ALWAYSON;
2925 break;
2926 case MSR_IA32_VMX_CR0_FIXED1:
2927 *pdata = -1ULL;
2928 break;
2929 case MSR_IA32_VMX_CR4_FIXED0:
2930 *pdata = VMXON_CR4_ALWAYSON;
2931 break;
2932 case MSR_IA32_VMX_CR4_FIXED1:
2933 *pdata = -1ULL;
2934 break;
2935 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002936 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002937 break;
2938 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002939 *pdata = vmx_control_msr(
2940 vmx->nested.nested_vmx_secondary_ctls_low,
2941 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002942 break;
2943 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07002944 *pdata = vmx->nested.nested_vmx_ept_caps |
2945 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002946 break;
2947 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002948 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002949 }
2950
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002951 return 0;
2952}
2953
Haozhong Zhang37e4c992016-06-22 14:59:55 +08002954static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2955 uint64_t val)
2956{
2957 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2958
2959 return !(val & ~valid_bits);
2960}
2961
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002962/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963 * Reads an msr value (of 'msr_index') into 'pdata'.
2964 * Returns 0 on success, non-0 otherwise.
2965 * Assumes vcpu_load() was already called.
2966 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002967static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968{
Avi Kivity26bb0982009-09-07 11:14:12 +03002969 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002971 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002972#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002974 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 break;
2976 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002977 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002979 case MSR_KERNEL_GS_BASE:
2980 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002981 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002982 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002983#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002985 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302986 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002987 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988 break;
2989 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002990 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991 break;
2992 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002993 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994 break;
2995 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002996 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002997 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002998 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002999 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003000 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003001 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003002 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003003 case MSR_IA32_MCG_EXT_CTL:
3004 if (!msr_info->host_initiated &&
3005 !(to_vmx(vcpu)->msr_ia32_feature_control &
3006 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003007 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003008 msr_info->data = vcpu->arch.mcg_ext_ctl;
3009 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003010 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003011 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003012 break;
3013 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3014 if (!nested_vmx_allowed(vcpu))
3015 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003016 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003017 case MSR_IA32_XSS:
3018 if (!vmx_xsaves_supported())
3019 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003020 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003021 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003022 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003023 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003024 return 1;
3025 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003027 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003028 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003029 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003030 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003032 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 }
3034
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 return 0;
3036}
3037
Jan Kiszkacae50132014-01-04 18:47:22 +01003038static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3039
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040/*
3041 * Writes msr value into into the appropriate "register".
3042 * Returns 0 on success, non-0 otherwise.
3043 * Assumes vcpu_load() was already called.
3044 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003045static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003047 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003048 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003049 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003050 u32 msr_index = msr_info->index;
3051 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003052
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003054 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003055 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003056 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003057#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003059 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 vmcs_writel(GUEST_FS_BASE, data);
3061 break;
3062 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003063 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 vmcs_writel(GUEST_GS_BASE, data);
3065 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003066 case MSR_KERNEL_GS_BASE:
3067 vmx_load_host_state(vmx);
3068 vmx->msr_guest_kernel_gs_base = data;
3069 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070#endif
3071 case MSR_IA32_SYSENTER_CS:
3072 vmcs_write32(GUEST_SYSENTER_CS, data);
3073 break;
3074 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003075 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076 break;
3077 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003078 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003080 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003081 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003082 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003083 vmcs_write64(GUEST_BNDCFGS, data);
3084 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303085 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003086 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003088 case MSR_IA32_CR_PAT:
3089 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003090 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3091 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003092 vmcs_write64(GUEST_IA32_PAT, data);
3093 vcpu->arch.pat = data;
3094 break;
3095 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003096 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003097 break;
Will Auldba904632012-11-29 12:42:50 -08003098 case MSR_IA32_TSC_ADJUST:
3099 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003100 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003101 case MSR_IA32_MCG_EXT_CTL:
3102 if ((!msr_info->host_initiated &&
3103 !(to_vmx(vcpu)->msr_ia32_feature_control &
3104 FEATURE_CONTROL_LMCE)) ||
3105 (data & ~MCG_EXT_CTL_LMCE_EN))
3106 return 1;
3107 vcpu->arch.mcg_ext_ctl = data;
3108 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003109 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003110 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003111 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003112 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3113 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003114 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003115 if (msr_info->host_initiated && data == 0)
3116 vmx_leave_nested(vcpu);
3117 break;
3118 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3119 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08003120 case MSR_IA32_XSS:
3121 if (!vmx_xsaves_supported())
3122 return 1;
3123 /*
3124 * The only supported bit as of Skylake is bit 8, but
3125 * it is not supported on KVM.
3126 */
3127 if (data != 0)
3128 return 1;
3129 vcpu->arch.ia32_xss = data;
3130 if (vcpu->arch.ia32_xss != host_xss)
3131 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3132 vcpu->arch.ia32_xss, host_xss);
3133 else
3134 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3135 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003136 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003137 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003138 return 1;
3139 /* Check reserved bit, higher 32 bits should be zero */
3140 if ((data >> 32) != 0)
3141 return 1;
3142 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003144 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003145 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003146 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003147 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003148 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3149 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003150 ret = kvm_set_shared_msr(msr->index, msr->data,
3151 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003152 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003153 if (ret)
3154 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003155 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003156 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003158 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 }
3160
Eddie Dong2cc51562007-05-21 07:28:09 +03003161 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162}
3163
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003164static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003166 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3167 switch (reg) {
3168 case VCPU_REGS_RSP:
3169 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3170 break;
3171 case VCPU_REGS_RIP:
3172 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3173 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003174 case VCPU_EXREG_PDPTR:
3175 if (enable_ept)
3176 ept_save_pdptrs(vcpu);
3177 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003178 default:
3179 break;
3180 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181}
3182
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183static __init int cpu_has_kvm_support(void)
3184{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003185 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186}
3187
3188static __init int vmx_disabled_by_bios(void)
3189{
3190 u64 msr;
3191
3192 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003193 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003194 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003195 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3196 && tboot_enabled())
3197 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003198 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003199 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003200 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003201 && !tboot_enabled()) {
3202 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003203 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003204 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003205 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003206 /* launched w/o TXT and VMX disabled */
3207 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3208 && !tboot_enabled())
3209 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003210 }
3211
3212 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213}
3214
Dongxiao Xu7725b892010-05-11 18:29:38 +08003215static void kvm_cpu_vmxon(u64 addr)
3216{
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003217 intel_pt_handle_vmx(1);
3218
Dongxiao Xu7725b892010-05-11 18:29:38 +08003219 asm volatile (ASM_VMX_VMXON_RAX
3220 : : "a"(&addr), "m"(addr)
3221 : "memory", "cc");
3222}
3223
Radim Krčmář13a34e02014-08-28 15:13:03 +02003224static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225{
3226 int cpu = raw_smp_processor_id();
3227 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003228 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003230 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003231 return -EBUSY;
3232
Nadav Har'Eld462b812011-05-24 15:26:10 +03003233 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003234 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3235 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003236
3237 /*
3238 * Now we can enable the vmclear operation in kdump
3239 * since the loaded_vmcss_on_cpu list on this cpu
3240 * has been initialized.
3241 *
3242 * Though the cpu is not in VMX operation now, there
3243 * is no problem to enable the vmclear operation
3244 * for the loaded_vmcss_on_cpu list is empty!
3245 */
3246 crash_enable_local_vmclear(cpu);
3247
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003249
3250 test_bits = FEATURE_CONTROL_LOCKED;
3251 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3252 if (tboot_enabled())
3253 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3254
3255 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003257 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3258 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003259 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003260
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003261 if (vmm_exclusive) {
3262 kvm_cpu_vmxon(phys_addr);
3263 ept_sync_global();
3264 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003265
Christoph Lameter89cbc762014-08-17 12:30:40 -05003266 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003267
Alexander Graf10474ae2009-09-15 11:37:46 +02003268 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269}
3270
Nadav Har'Eld462b812011-05-24 15:26:10 +03003271static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003272{
3273 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003274 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003275
Nadav Har'Eld462b812011-05-24 15:26:10 +03003276 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3277 loaded_vmcss_on_cpu_link)
3278 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003279}
3280
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003281
3282/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3283 * tricks.
3284 */
3285static void kvm_cpu_vmxoff(void)
3286{
3287 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003288
3289 intel_pt_handle_vmx(0);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003290}
3291
Radim Krčmář13a34e02014-08-28 15:13:03 +02003292static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003294 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003295 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003296 kvm_cpu_vmxoff();
3297 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003298 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299}
3300
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003301static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003302 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003303{
3304 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003305 u32 ctl = ctl_min | ctl_opt;
3306
3307 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3308
3309 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3310 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3311
3312 /* Ensure minimum (required) set of control bits are supported. */
3313 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003314 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003315
3316 *result = ctl;
3317 return 0;
3318}
3319
Avi Kivity110312c2010-12-21 12:54:20 +02003320static __init bool allow_1_setting(u32 msr, u32 ctl)
3321{
3322 u32 vmx_msr_low, vmx_msr_high;
3323
3324 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3325 return vmx_msr_high & ctl;
3326}
3327
Yang, Sheng002c7f72007-07-31 14:23:01 +03003328static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003329{
3330 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003331 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003332 u32 _pin_based_exec_control = 0;
3333 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003334 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003335 u32 _vmexit_control = 0;
3336 u32 _vmentry_control = 0;
3337
Raghavendra K T10166742012-02-07 23:19:20 +05303338 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003339#ifdef CONFIG_X86_64
3340 CPU_BASED_CR8_LOAD_EXITING |
3341 CPU_BASED_CR8_STORE_EXITING |
3342#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003343 CPU_BASED_CR3_LOAD_EXITING |
3344 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003345 CPU_BASED_USE_IO_BITMAPS |
3346 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003347 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003348 CPU_BASED_MWAIT_EXITING |
3349 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003350 CPU_BASED_INVLPG_EXITING |
3351 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003352
Sheng Yangf78e0e22007-10-29 09:40:42 +08003353 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003354 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003355 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003356 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3357 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003358 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003359#ifdef CONFIG_X86_64
3360 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3361 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3362 ~CPU_BASED_CR8_STORE_EXITING;
3363#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003364 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003365 min2 = 0;
3366 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003367 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003368 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003369 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003370 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003371 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003372 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003373 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003374 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003375 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003376 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003377 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003378 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003379 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003380 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003381 if (adjust_vmx_controls(min2, opt2,
3382 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003383 &_cpu_based_2nd_exec_control) < 0)
3384 return -EIO;
3385 }
3386#ifndef CONFIG_X86_64
3387 if (!(_cpu_based_2nd_exec_control &
3388 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3389 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3390#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003391
3392 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3393 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003394 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003395 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3396 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003397
Sheng Yangd56f5462008-04-25 10:13:16 +08003398 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003399 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3400 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003401 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3402 CPU_BASED_CR3_STORE_EXITING |
3403 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003404 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3405 vmx_capability.ept, vmx_capability.vpid);
3406 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003407
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003408 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003409#ifdef CONFIG_X86_64
3410 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3411#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003412 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003413 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003414 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3415 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003416 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003417
Yang Zhang01e439b2013-04-11 19:25:12 +08003418 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Yunhong Jiang64672c92016-06-13 14:19:59 -07003419 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3420 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003421 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3422 &_pin_based_exec_control) < 0)
3423 return -EIO;
3424
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003425 if (cpu_has_broken_vmx_preemption_timer())
3426 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003427 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003428 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003429 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3430
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003431 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003432 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003433 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3434 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003435 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003437 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003438
3439 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3440 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003441 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003442
3443#ifdef CONFIG_X86_64
3444 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3445 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003446 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003447#endif
3448
3449 /* Require Write-Back (WB) memory type for VMCS accesses. */
3450 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003451 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003452
Yang, Sheng002c7f72007-07-31 14:23:01 +03003453 vmcs_conf->size = vmx_msr_high & 0x1fff;
3454 vmcs_conf->order = get_order(vmcs_config.size);
3455 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003456
Yang, Sheng002c7f72007-07-31 14:23:01 +03003457 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3458 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003459 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003460 vmcs_conf->vmexit_ctrl = _vmexit_control;
3461 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003462
Avi Kivity110312c2010-12-21 12:54:20 +02003463 cpu_has_load_ia32_efer =
3464 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3465 VM_ENTRY_LOAD_IA32_EFER)
3466 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3467 VM_EXIT_LOAD_IA32_EFER);
3468
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003469 cpu_has_load_perf_global_ctrl =
3470 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3471 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3472 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3473 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3474
3475 /*
3476 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003477 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003478 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3479 *
3480 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3481 *
3482 * AAK155 (model 26)
3483 * AAP115 (model 30)
3484 * AAT100 (model 37)
3485 * BC86,AAY89,BD102 (model 44)
3486 * BA97 (model 46)
3487 *
3488 */
3489 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3490 switch (boot_cpu_data.x86_model) {
3491 case 26:
3492 case 30:
3493 case 37:
3494 case 44:
3495 case 46:
3496 cpu_has_load_perf_global_ctrl = false;
3497 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3498 "does not work properly. Using workaround\n");
3499 break;
3500 default:
3501 break;
3502 }
3503 }
3504
Borislav Petkov782511b2016-04-04 22:25:03 +02003505 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003506 rdmsrl(MSR_IA32_XSS, host_xss);
3507
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003508 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003509}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510
3511static struct vmcs *alloc_vmcs_cpu(int cpu)
3512{
3513 int node = cpu_to_node(cpu);
3514 struct page *pages;
3515 struct vmcs *vmcs;
3516
Vlastimil Babka96db8002015-09-08 15:03:50 -07003517 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518 if (!pages)
3519 return NULL;
3520 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003521 memset(vmcs, 0, vmcs_config.size);
3522 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523 return vmcs;
3524}
3525
3526static struct vmcs *alloc_vmcs(void)
3527{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003528 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529}
3530
3531static void free_vmcs(struct vmcs *vmcs)
3532{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003533 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534}
3535
Nadav Har'Eld462b812011-05-24 15:26:10 +03003536/*
3537 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3538 */
3539static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3540{
3541 if (!loaded_vmcs->vmcs)
3542 return;
3543 loaded_vmcs_clear(loaded_vmcs);
3544 free_vmcs(loaded_vmcs->vmcs);
3545 loaded_vmcs->vmcs = NULL;
3546}
3547
Sam Ravnborg39959582007-06-01 00:47:13 -07003548static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003549{
3550 int cpu;
3551
Zachary Amsden3230bb42009-09-29 11:38:37 -10003552 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003554 per_cpu(vmxarea, cpu) = NULL;
3555 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556}
3557
Bandan Dasfe2b2012014-04-21 15:20:14 -04003558static void init_vmcs_shadow_fields(void)
3559{
3560 int i, j;
3561
3562 /* No checks for read only fields yet */
3563
3564 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3565 switch (shadow_read_write_fields[i]) {
3566 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003567 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003568 continue;
3569 break;
3570 default:
3571 break;
3572 }
3573
3574 if (j < i)
3575 shadow_read_write_fields[j] =
3576 shadow_read_write_fields[i];
3577 j++;
3578 }
3579 max_shadow_read_write_fields = j;
3580
3581 /* shadowed fields guest access without vmexit */
3582 for (i = 0; i < max_shadow_read_write_fields; i++) {
3583 clear_bit(shadow_read_write_fields[i],
3584 vmx_vmwrite_bitmap);
3585 clear_bit(shadow_read_write_fields[i],
3586 vmx_vmread_bitmap);
3587 }
3588 for (i = 0; i < max_shadow_read_only_fields; i++)
3589 clear_bit(shadow_read_only_fields[i],
3590 vmx_vmread_bitmap);
3591}
3592
Avi Kivity6aa8b732006-12-10 02:21:36 -08003593static __init int alloc_kvm_area(void)
3594{
3595 int cpu;
3596
Zachary Amsden3230bb42009-09-29 11:38:37 -10003597 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003598 struct vmcs *vmcs;
3599
3600 vmcs = alloc_vmcs_cpu(cpu);
3601 if (!vmcs) {
3602 free_kvm_area();
3603 return -ENOMEM;
3604 }
3605
3606 per_cpu(vmxarea, cpu) = vmcs;
3607 }
3608 return 0;
3609}
3610
Gleb Natapov14168782013-01-21 15:36:49 +02003611static bool emulation_required(struct kvm_vcpu *vcpu)
3612{
3613 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3614}
3615
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003616static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003617 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003618{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003619 if (!emulate_invalid_guest_state) {
3620 /*
3621 * CS and SS RPL should be equal during guest entry according
3622 * to VMX spec, but in reality it is not always so. Since vcpu
3623 * is in the middle of the transition from real mode to
3624 * protected mode it is safe to assume that RPL 0 is a good
3625 * default value.
3626 */
3627 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003628 save->selector &= ~SEGMENT_RPL_MASK;
3629 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003630 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003632 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633}
3634
3635static void enter_pmode(struct kvm_vcpu *vcpu)
3636{
3637 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003638 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003639
Gleb Natapovd99e4152012-12-20 16:57:45 +02003640 /*
3641 * Update real mode segment cache. It may be not up-to-date if sement
3642 * register was written while vcpu was in a guest mode.
3643 */
3644 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3645 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3646 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3647 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3648 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3649 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3650
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003651 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652
Avi Kivity2fb92db2011-04-27 19:42:18 +03003653 vmx_segment_cache_clear(vmx);
3654
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003655 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656
3657 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003658 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3659 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660 vmcs_writel(GUEST_RFLAGS, flags);
3661
Rusty Russell66aee912007-07-17 23:34:16 +10003662 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3663 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003664
3665 update_exception_bitmap(vcpu);
3666
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003667 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3668 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3669 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3670 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3671 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3672 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673}
3674
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003675static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676{
Mathias Krause772e0312012-08-30 01:30:19 +02003677 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003678 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003679
Gleb Natapovd99e4152012-12-20 16:57:45 +02003680 var.dpl = 0x3;
3681 if (seg == VCPU_SREG_CS)
3682 var.type = 0x3;
3683
3684 if (!emulate_invalid_guest_state) {
3685 var.selector = var.base >> 4;
3686 var.base = var.base & 0xffff0;
3687 var.limit = 0xffff;
3688 var.g = 0;
3689 var.db = 0;
3690 var.present = 1;
3691 var.s = 1;
3692 var.l = 0;
3693 var.unusable = 0;
3694 var.type = 0x3;
3695 var.avl = 0;
3696 if (save->base & 0xf)
3697 printk_once(KERN_WARNING "kvm: segment base is not "
3698 "paragraph aligned when entering "
3699 "protected mode (seg=%d)", seg);
3700 }
3701
3702 vmcs_write16(sf->selector, var.selector);
3703 vmcs_write32(sf->base, var.base);
3704 vmcs_write32(sf->limit, var.limit);
3705 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003706}
3707
3708static void enter_rmode(struct kvm_vcpu *vcpu)
3709{
3710 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003711 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003713 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3714 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3715 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3716 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3717 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003718 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3719 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003720
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003721 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722
Gleb Natapov776e58e2011-03-13 12:34:27 +02003723 /*
3724 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003725 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003726 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003727 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003728 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3729 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003730
Avi Kivity2fb92db2011-04-27 19:42:18 +03003731 vmx_segment_cache_clear(vmx);
3732
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003733 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003735 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3736
3737 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003738 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003739
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003740 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003741
3742 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003743 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003744 update_exception_bitmap(vcpu);
3745
Gleb Natapovd99e4152012-12-20 16:57:45 +02003746 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3747 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3748 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3749 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3750 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3751 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003752
Eddie Dong8668a3c2007-10-10 14:26:45 +08003753 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003754}
3755
Amit Shah401d10d2009-02-20 22:53:37 +05303756static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3757{
3758 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003759 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3760
3761 if (!msr)
3762 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303763
Avi Kivity44ea2b12009-09-06 15:55:37 +03003764 /*
3765 * Force kernel_gs_base reloading before EFER changes, as control
3766 * of this msr depends on is_long_mode().
3767 */
3768 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003769 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303770 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003771 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303772 msr->data = efer;
3773 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003774 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303775
3776 msr->data = efer & ~EFER_LME;
3777 }
3778 setup_msrs(vmx);
3779}
3780
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003781#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782
3783static void enter_lmode(struct kvm_vcpu *vcpu)
3784{
3785 u32 guest_tr_ar;
3786
Avi Kivity2fb92db2011-04-27 19:42:18 +03003787 vmx_segment_cache_clear(to_vmx(vcpu));
3788
Avi Kivity6aa8b732006-12-10 02:21:36 -08003789 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003790 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003791 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3792 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003793 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003794 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3795 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003796 }
Avi Kivityda38f432010-07-06 11:30:49 +03003797 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003798}
3799
3800static void exit_lmode(struct kvm_vcpu *vcpu)
3801{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003802 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003803 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804}
3805
3806#endif
3807
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003808static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003809{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003810 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003811 if (enable_ept) {
3812 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3813 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003814 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003815 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003816}
3817
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003818static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3819{
3820 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3821}
3822
Avi Kivitye8467fd2009-12-29 18:43:06 +02003823static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3824{
3825 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3826
3827 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3828 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3829}
3830
Avi Kivityaff48ba2010-12-05 18:56:11 +02003831static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3832{
3833 if (enable_ept && is_paging(vcpu))
3834 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3835 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3836}
3837
Anthony Liguori25c4c272007-04-27 09:29:21 +03003838static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003839{
Avi Kivityfc78f512009-12-07 12:16:48 +02003840 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3841
3842 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3843 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003844}
3845
Sheng Yang14394422008-04-28 12:24:45 +08003846static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3847{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003848 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3849
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003850 if (!test_bit(VCPU_EXREG_PDPTR,
3851 (unsigned long *)&vcpu->arch.regs_dirty))
3852 return;
3853
Sheng Yang14394422008-04-28 12:24:45 +08003854 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003855 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3856 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3857 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3858 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003859 }
3860}
3861
Avi Kivity8f5d5492009-05-31 18:41:29 +03003862static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3863{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003864 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3865
Avi Kivity8f5d5492009-05-31 18:41:29 +03003866 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003867 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3868 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3869 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3870 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003871 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003872
3873 __set_bit(VCPU_EXREG_PDPTR,
3874 (unsigned long *)&vcpu->arch.regs_avail);
3875 __set_bit(VCPU_EXREG_PDPTR,
3876 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003877}
3878
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003879static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003880
3881static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3882 unsigned long cr0,
3883 struct kvm_vcpu *vcpu)
3884{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003885 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3886 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003887 if (!(cr0 & X86_CR0_PG)) {
3888 /* From paging/starting to nonpaging */
3889 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003890 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003891 (CPU_BASED_CR3_LOAD_EXITING |
3892 CPU_BASED_CR3_STORE_EXITING));
3893 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003894 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003895 } else if (!is_paging(vcpu)) {
3896 /* From nonpaging to paging */
3897 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003898 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003899 ~(CPU_BASED_CR3_LOAD_EXITING |
3900 CPU_BASED_CR3_STORE_EXITING));
3901 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003902 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003903 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003904
3905 if (!(cr0 & X86_CR0_WP))
3906 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003907}
3908
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3910{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003911 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003912 unsigned long hw_cr0;
3913
Gleb Natapov50378782013-02-04 16:00:28 +02003914 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003915 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003916 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003917 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003918 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003919
Gleb Natapov218e7632013-01-21 15:36:45 +02003920 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3921 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922
Gleb Natapov218e7632013-01-21 15:36:45 +02003923 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3924 enter_rmode(vcpu);
3925 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003926
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003927#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003928 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003929 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003931 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932 exit_lmode(vcpu);
3933 }
3934#endif
3935
Avi Kivity089d0342009-03-23 18:26:32 +02003936 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003937 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3938
Avi Kivity02daab22009-12-30 12:40:26 +02003939 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003940 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003941
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003943 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003944 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003945
3946 /* depends on vcpu->arch.cr0 to be set to a new value */
3947 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003948}
3949
Sheng Yang14394422008-04-28 12:24:45 +08003950static u64 construct_eptp(unsigned long root_hpa)
3951{
3952 u64 eptp;
3953
3954 /* TODO write the value reading from MSR */
3955 eptp = VMX_EPT_DEFAULT_MT |
3956 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003957 if (enable_ept_ad_bits)
3958 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003959 eptp |= (root_hpa & PAGE_MASK);
3960
3961 return eptp;
3962}
3963
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3965{
Sheng Yang14394422008-04-28 12:24:45 +08003966 unsigned long guest_cr3;
3967 u64 eptp;
3968
3969 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003970 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003971 eptp = construct_eptp(cr3);
3972 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003973 if (is_paging(vcpu) || is_guest_mode(vcpu))
3974 guest_cr3 = kvm_read_cr3(vcpu);
3975 else
3976 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003977 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003978 }
3979
Sheng Yang2384d2b2008-01-17 15:14:33 +08003980 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003981 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003982}
3983
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003984static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003985{
Ben Serebrin085e68e2015-04-16 11:58:05 -07003986 /*
3987 * Pass through host's Machine Check Enable value to hw_cr4, which
3988 * is in force while we are in guest mode. Do not let guests control
3989 * this bit, even if host CR4.MCE == 0.
3990 */
3991 unsigned long hw_cr4 =
3992 (cr4_read_shadow() & X86_CR4_MCE) |
3993 (cr4 & ~X86_CR4_MCE) |
3994 (to_vmx(vcpu)->rmode.vm86_active ?
3995 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08003996
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003997 if (cr4 & X86_CR4_VMXE) {
3998 /*
3999 * To use VMXON (and later other VMX instructions), a guest
4000 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4001 * So basically the check on whether to allow nested VMX
4002 * is here.
4003 */
4004 if (!nested_vmx_allowed(vcpu))
4005 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004006 }
4007 if (to_vmx(vcpu)->nested.vmxon &&
4008 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004009 return 1;
4010
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004011 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004012 if (enable_ept) {
4013 if (!is_paging(vcpu)) {
4014 hw_cr4 &= ~X86_CR4_PAE;
4015 hw_cr4 |= X86_CR4_PSE;
4016 } else if (!(cr4 & X86_CR4_PAE)) {
4017 hw_cr4 &= ~X86_CR4_PAE;
4018 }
4019 }
Sheng Yang14394422008-04-28 12:24:45 +08004020
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004021 if (!enable_unrestricted_guest && !is_paging(vcpu))
4022 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004023 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4024 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4025 * to be manually disabled when guest switches to non-paging
4026 * mode.
4027 *
4028 * If !enable_unrestricted_guest, the CPU is always running
4029 * with CR0.PG=1 and CR4 needs to be modified.
4030 * If enable_unrestricted_guest, the CPU automatically
4031 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004032 */
Huaitong Handdba2622016-03-22 16:51:15 +08004033 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004034
Sheng Yang14394422008-04-28 12:24:45 +08004035 vmcs_writel(CR4_READ_SHADOW, cr4);
4036 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004037 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004038}
4039
Avi Kivity6aa8b732006-12-10 02:21:36 -08004040static void vmx_get_segment(struct kvm_vcpu *vcpu,
4041 struct kvm_segment *var, int seg)
4042{
Avi Kivitya9179492011-01-03 14:28:52 +02004043 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004044 u32 ar;
4045
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004046 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004047 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004048 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004049 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004050 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004051 var->base = vmx_read_guest_seg_base(vmx, seg);
4052 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4053 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004054 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004055 var->base = vmx_read_guest_seg_base(vmx, seg);
4056 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4057 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4058 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004059 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060 var->type = ar & 15;
4061 var->s = (ar >> 4) & 1;
4062 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004063 /*
4064 * Some userspaces do not preserve unusable property. Since usable
4065 * segment has to be present according to VMX spec we can use present
4066 * property to amend userspace bug by making unusable segment always
4067 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4068 * segment as unusable.
4069 */
4070 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071 var->avl = (ar >> 12) & 1;
4072 var->l = (ar >> 13) & 1;
4073 var->db = (ar >> 14) & 1;
4074 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075}
4076
Avi Kivitya9179492011-01-03 14:28:52 +02004077static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4078{
Avi Kivitya9179492011-01-03 14:28:52 +02004079 struct kvm_segment s;
4080
4081 if (to_vmx(vcpu)->rmode.vm86_active) {
4082 vmx_get_segment(vcpu, &s, seg);
4083 return s.base;
4084 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004085 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004086}
4087
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004088static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004089{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004090 struct vcpu_vmx *vmx = to_vmx(vcpu);
4091
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004092 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004093 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004094 else {
4095 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004096 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004097 }
Avi Kivity69c73022011-03-07 15:26:44 +02004098}
4099
Avi Kivity653e3102007-05-07 10:55:37 +03004100static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004102 u32 ar;
4103
Avi Kivityf0495f92012-06-07 17:06:10 +03004104 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004105 ar = 1 << 16;
4106 else {
4107 ar = var->type & 15;
4108 ar |= (var->s & 1) << 4;
4109 ar |= (var->dpl & 3) << 5;
4110 ar |= (var->present & 1) << 7;
4111 ar |= (var->avl & 1) << 12;
4112 ar |= (var->l & 1) << 13;
4113 ar |= (var->db & 1) << 14;
4114 ar |= (var->g & 1) << 15;
4115 }
Avi Kivity653e3102007-05-07 10:55:37 +03004116
4117 return ar;
4118}
4119
4120static void vmx_set_segment(struct kvm_vcpu *vcpu,
4121 struct kvm_segment *var, int seg)
4122{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004123 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004124 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004125
Avi Kivity2fb92db2011-04-27 19:42:18 +03004126 vmx_segment_cache_clear(vmx);
4127
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004128 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4129 vmx->rmode.segs[seg] = *var;
4130 if (seg == VCPU_SREG_TR)
4131 vmcs_write16(sf->selector, var->selector);
4132 else if (var->s)
4133 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004134 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004135 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004136
Avi Kivity653e3102007-05-07 10:55:37 +03004137 vmcs_writel(sf->base, var->base);
4138 vmcs_write32(sf->limit, var->limit);
4139 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004140
4141 /*
4142 * Fix the "Accessed" bit in AR field of segment registers for older
4143 * qemu binaries.
4144 * IA32 arch specifies that at the time of processor reset the
4145 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004146 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004147 * state vmexit when "unrestricted guest" mode is turned on.
4148 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4149 * tree. Newer qemu binaries with that qemu fix would not need this
4150 * kvm hack.
4151 */
4152 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004153 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004154
Gleb Natapovf924d662012-12-12 19:10:55 +02004155 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004156
4157out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004158 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159}
4160
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4162{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004163 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164
4165 *db = (ar >> 14) & 1;
4166 *l = (ar >> 13) & 1;
4167}
4168
Gleb Natapov89a27f42010-02-16 10:51:48 +02004169static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004171 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4172 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173}
4174
Gleb Natapov89a27f42010-02-16 10:51:48 +02004175static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004177 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4178 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004179}
4180
Gleb Natapov89a27f42010-02-16 10:51:48 +02004181static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004183 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4184 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185}
4186
Gleb Natapov89a27f42010-02-16 10:51:48 +02004187static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004189 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4190 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004191}
4192
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004193static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4194{
4195 struct kvm_segment var;
4196 u32 ar;
4197
4198 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004199 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004200 if (seg == VCPU_SREG_CS)
4201 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004202 ar = vmx_segment_access_rights(&var);
4203
4204 if (var.base != (var.selector << 4))
4205 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004206 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004207 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004208 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004209 return false;
4210
4211 return true;
4212}
4213
4214static bool code_segment_valid(struct kvm_vcpu *vcpu)
4215{
4216 struct kvm_segment cs;
4217 unsigned int cs_rpl;
4218
4219 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004220 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004221
Avi Kivity1872a3f2009-01-04 23:26:52 +02004222 if (cs.unusable)
4223 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004224 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004225 return false;
4226 if (!cs.s)
4227 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004228 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004229 if (cs.dpl > cs_rpl)
4230 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004231 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004232 if (cs.dpl != cs_rpl)
4233 return false;
4234 }
4235 if (!cs.present)
4236 return false;
4237
4238 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4239 return true;
4240}
4241
4242static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4243{
4244 struct kvm_segment ss;
4245 unsigned int ss_rpl;
4246
4247 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004248 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004249
Avi Kivity1872a3f2009-01-04 23:26:52 +02004250 if (ss.unusable)
4251 return true;
4252 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004253 return false;
4254 if (!ss.s)
4255 return false;
4256 if (ss.dpl != ss_rpl) /* DPL != RPL */
4257 return false;
4258 if (!ss.present)
4259 return false;
4260
4261 return true;
4262}
4263
4264static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4265{
4266 struct kvm_segment var;
4267 unsigned int rpl;
4268
4269 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004270 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004271
Avi Kivity1872a3f2009-01-04 23:26:52 +02004272 if (var.unusable)
4273 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004274 if (!var.s)
4275 return false;
4276 if (!var.present)
4277 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004278 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004279 if (var.dpl < rpl) /* DPL < RPL */
4280 return false;
4281 }
4282
4283 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4284 * rights flags
4285 */
4286 return true;
4287}
4288
4289static bool tr_valid(struct kvm_vcpu *vcpu)
4290{
4291 struct kvm_segment tr;
4292
4293 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4294
Avi Kivity1872a3f2009-01-04 23:26:52 +02004295 if (tr.unusable)
4296 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004297 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004298 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004299 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004300 return false;
4301 if (!tr.present)
4302 return false;
4303
4304 return true;
4305}
4306
4307static bool ldtr_valid(struct kvm_vcpu *vcpu)
4308{
4309 struct kvm_segment ldtr;
4310
4311 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4312
Avi Kivity1872a3f2009-01-04 23:26:52 +02004313 if (ldtr.unusable)
4314 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004315 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004316 return false;
4317 if (ldtr.type != 2)
4318 return false;
4319 if (!ldtr.present)
4320 return false;
4321
4322 return true;
4323}
4324
4325static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4326{
4327 struct kvm_segment cs, ss;
4328
4329 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4330 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4331
Nadav Amitb32a9912015-03-29 16:33:04 +03004332 return ((cs.selector & SEGMENT_RPL_MASK) ==
4333 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004334}
4335
4336/*
4337 * Check if guest state is valid. Returns true if valid, false if
4338 * not.
4339 * We assume that registers are always usable
4340 */
4341static bool guest_state_valid(struct kvm_vcpu *vcpu)
4342{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004343 if (enable_unrestricted_guest)
4344 return true;
4345
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004346 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004347 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004348 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4349 return false;
4350 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4351 return false;
4352 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4353 return false;
4354 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4355 return false;
4356 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4357 return false;
4358 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4359 return false;
4360 } else {
4361 /* protected mode guest state checks */
4362 if (!cs_ss_rpl_check(vcpu))
4363 return false;
4364 if (!code_segment_valid(vcpu))
4365 return false;
4366 if (!stack_segment_valid(vcpu))
4367 return false;
4368 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4369 return false;
4370 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4371 return false;
4372 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4373 return false;
4374 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4375 return false;
4376 if (!tr_valid(vcpu))
4377 return false;
4378 if (!ldtr_valid(vcpu))
4379 return false;
4380 }
4381 /* TODO:
4382 * - Add checks on RIP
4383 * - Add checks on RFLAGS
4384 */
4385
4386 return true;
4387}
4388
Mike Dayd77c26f2007-10-08 09:02:08 -04004389static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004390{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004391 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004392 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004393 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004394
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004395 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004396 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004397 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4398 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004399 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004400 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004401 r = kvm_write_guest_page(kvm, fn++, &data,
4402 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004403 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004404 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004405 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4406 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004407 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004408 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4409 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004410 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004411 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004412 r = kvm_write_guest_page(kvm, fn, &data,
4413 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4414 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004415out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004416 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004417 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418}
4419
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004420static int init_rmode_identity_map(struct kvm *kvm)
4421{
Tang Chenf51770e2014-09-16 18:41:59 +08004422 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004423 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004424 u32 tmp;
4425
Avi Kivity089d0342009-03-23 18:26:32 +02004426 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004427 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004428
4429 /* Protect kvm->arch.ept_identity_pagetable_done. */
4430 mutex_lock(&kvm->slots_lock);
4431
Tang Chenf51770e2014-09-16 18:41:59 +08004432 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004433 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004434
Sheng Yangb927a3c2009-07-21 10:42:48 +08004435 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004436
4437 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004438 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004439 goto out2;
4440
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004441 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004442 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4443 if (r < 0)
4444 goto out;
4445 /* Set up identity-mapping pagetable for EPT in real mode */
4446 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4447 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4448 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4449 r = kvm_write_guest_page(kvm, identity_map_pfn,
4450 &tmp, i * sizeof(tmp), sizeof(tmp));
4451 if (r < 0)
4452 goto out;
4453 }
4454 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004455
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004456out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004457 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004458
4459out2:
4460 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004461 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004462}
4463
Avi Kivity6aa8b732006-12-10 02:21:36 -08004464static void seg_setup(int seg)
4465{
Mathias Krause772e0312012-08-30 01:30:19 +02004466 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004467 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468
4469 vmcs_write16(sf->selector, 0);
4470 vmcs_writel(sf->base, 0);
4471 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004472 ar = 0x93;
4473 if (seg == VCPU_SREG_CS)
4474 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004475
4476 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477}
4478
Sheng Yangf78e0e22007-10-29 09:40:42 +08004479static int alloc_apic_access_page(struct kvm *kvm)
4480{
Xiao Guangrong44841412012-09-07 14:14:20 +08004481 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004482 int r = 0;
4483
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004484 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004485 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004486 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004487 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4488 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004489 if (r)
4490 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004491
Tang Chen73a6d942014-09-11 13:38:00 +08004492 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004493 if (is_error_page(page)) {
4494 r = -EFAULT;
4495 goto out;
4496 }
4497
Tang Chenc24ae0d2014-09-24 15:57:58 +08004498 /*
4499 * Do not pin the page in memory, so that memory hot-unplug
4500 * is able to migrate it.
4501 */
4502 put_page(page);
4503 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004504out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004505 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004506 return r;
4507}
4508
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004509static int alloc_identity_pagetable(struct kvm *kvm)
4510{
Tang Chena255d472014-09-16 18:41:58 +08004511 /* Called with kvm->slots_lock held. */
4512
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004513 int r = 0;
4514
Tang Chena255d472014-09-16 18:41:58 +08004515 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4516
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004517 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4518 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004519
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004520 return r;
4521}
4522
Wanpeng Li991e7a02015-09-16 17:30:05 +08004523static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004524{
4525 int vpid;
4526
Avi Kivity919818a2009-03-23 18:01:29 +02004527 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004528 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004529 spin_lock(&vmx_vpid_lock);
4530 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004531 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004532 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004533 else
4534 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004535 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004536 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004537}
4538
Wanpeng Li991e7a02015-09-16 17:30:05 +08004539static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004540{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004541 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004542 return;
4543 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004544 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004545 spin_unlock(&vmx_vpid_lock);
4546}
4547
Yang Zhang8d146952013-01-25 10:18:50 +08004548#define MSR_TYPE_R 1
4549#define MSR_TYPE_W 2
4550static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4551 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004552{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004553 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004554
4555 if (!cpu_has_vmx_msr_bitmap())
4556 return;
4557
4558 /*
4559 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4560 * have the write-low and read-high bitmap offsets the wrong way round.
4561 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4562 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004563 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004564 if (type & MSR_TYPE_R)
4565 /* read-low */
4566 __clear_bit(msr, msr_bitmap + 0x000 / f);
4567
4568 if (type & MSR_TYPE_W)
4569 /* write-low */
4570 __clear_bit(msr, msr_bitmap + 0x800 / f);
4571
Sheng Yang25c5f222008-03-28 13:18:56 +08004572 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4573 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004574 if (type & MSR_TYPE_R)
4575 /* read-high */
4576 __clear_bit(msr, msr_bitmap + 0x400 / f);
4577
4578 if (type & MSR_TYPE_W)
4579 /* write-high */
4580 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4581
4582 }
4583}
4584
4585static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4586 u32 msr, int type)
4587{
4588 int f = sizeof(unsigned long);
4589
4590 if (!cpu_has_vmx_msr_bitmap())
4591 return;
4592
4593 /*
4594 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4595 * have the write-low and read-high bitmap offsets the wrong way round.
4596 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4597 */
4598 if (msr <= 0x1fff) {
4599 if (type & MSR_TYPE_R)
4600 /* read-low */
4601 __set_bit(msr, msr_bitmap + 0x000 / f);
4602
4603 if (type & MSR_TYPE_W)
4604 /* write-low */
4605 __set_bit(msr, msr_bitmap + 0x800 / f);
4606
4607 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4608 msr &= 0x1fff;
4609 if (type & MSR_TYPE_R)
4610 /* read-high */
4611 __set_bit(msr, msr_bitmap + 0x400 / f);
4612
4613 if (type & MSR_TYPE_W)
4614 /* write-high */
4615 __set_bit(msr, msr_bitmap + 0xc00 / f);
4616
Sheng Yang25c5f222008-03-28 13:18:56 +08004617 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004618}
4619
Wincy Vanf2b93282015-02-03 23:56:03 +08004620/*
4621 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4622 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4623 */
4624static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4625 unsigned long *msr_bitmap_nested,
4626 u32 msr, int type)
4627{
4628 int f = sizeof(unsigned long);
4629
4630 if (!cpu_has_vmx_msr_bitmap()) {
4631 WARN_ON(1);
4632 return;
4633 }
4634
4635 /*
4636 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4637 * have the write-low and read-high bitmap offsets the wrong way round.
4638 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4639 */
4640 if (msr <= 0x1fff) {
4641 if (type & MSR_TYPE_R &&
4642 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4643 /* read-low */
4644 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4645
4646 if (type & MSR_TYPE_W &&
4647 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4648 /* write-low */
4649 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4650
4651 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4652 msr &= 0x1fff;
4653 if (type & MSR_TYPE_R &&
4654 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4655 /* read-high */
4656 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4657
4658 if (type & MSR_TYPE_W &&
4659 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4660 /* write-high */
4661 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4662
4663 }
4664}
4665
Avi Kivity58972972009-02-24 22:26:47 +02004666static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4667{
4668 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004669 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4670 msr, MSR_TYPE_R | MSR_TYPE_W);
4671 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4672 msr, MSR_TYPE_R | MSR_TYPE_W);
4673}
4674
4675static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4676{
4677 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4678 msr, MSR_TYPE_R);
4679 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4680 msr, MSR_TYPE_R);
4681}
4682
4683static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4684{
4685 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4686 msr, MSR_TYPE_R);
4687 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4688 msr, MSR_TYPE_R);
4689}
4690
4691static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4692{
4693 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4694 msr, MSR_TYPE_W);
4695 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4696 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004697}
4698
Andrey Smetanind62caab2015-11-10 15:36:33 +03004699static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004700{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004701 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004702}
4703
Wincy Van705699a2015-02-03 23:58:17 +08004704static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
4705{
4706 struct vcpu_vmx *vmx = to_vmx(vcpu);
4707 int max_irr;
4708 void *vapic_page;
4709 u16 status;
4710
4711 if (vmx->nested.pi_desc &&
4712 vmx->nested.pi_pending) {
4713 vmx->nested.pi_pending = false;
4714 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4715 return 0;
4716
4717 max_irr = find_last_bit(
4718 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4719
4720 if (max_irr == 256)
4721 return 0;
4722
4723 vapic_page = kmap(vmx->nested.virtual_apic_page);
4724 if (!vapic_page) {
4725 WARN_ON(1);
4726 return -ENOMEM;
4727 }
4728 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4729 kunmap(vmx->nested.virtual_apic_page);
4730
4731 status = vmcs_read16(GUEST_INTR_STATUS);
4732 if ((u8)max_irr > ((u8)status & 0xff)) {
4733 status &= ~0xff;
4734 status |= (u8)max_irr;
4735 vmcs_write16(GUEST_INTR_STATUS, status);
4736 }
4737 }
4738 return 0;
4739}
4740
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004741static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4742{
4743#ifdef CONFIG_SMP
4744 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004745 struct vcpu_vmx *vmx = to_vmx(vcpu);
4746
4747 /*
4748 * Currently, we don't support urgent interrupt,
4749 * all interrupts are recognized as non-urgent
4750 * interrupt, so we cannot post interrupts when
4751 * 'SN' is set.
4752 *
4753 * If the vcpu is in guest mode, it means it is
4754 * running instead of being scheduled out and
4755 * waiting in the run queue, and that's the only
4756 * case when 'SN' is set currently, warning if
4757 * 'SN' is set.
4758 */
4759 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
4760
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004761 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4762 POSTED_INTR_VECTOR);
4763 return true;
4764 }
4765#endif
4766 return false;
4767}
4768
Wincy Van705699a2015-02-03 23:58:17 +08004769static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4770 int vector)
4771{
4772 struct vcpu_vmx *vmx = to_vmx(vcpu);
4773
4774 if (is_guest_mode(vcpu) &&
4775 vector == vmx->nested.posted_intr_nv) {
4776 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004777 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004778 /*
4779 * If a posted intr is not recognized by hardware,
4780 * we will accomplish it in the next vmentry.
4781 */
4782 vmx->nested.pi_pending = true;
4783 kvm_make_request(KVM_REQ_EVENT, vcpu);
4784 return 0;
4785 }
4786 return -1;
4787}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004789 * Send interrupt to vcpu via posted interrupt way.
4790 * 1. If target vcpu is running(non-root mode), send posted interrupt
4791 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4792 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4793 * interrupt from PIR in next vmentry.
4794 */
4795static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4796{
4797 struct vcpu_vmx *vmx = to_vmx(vcpu);
4798 int r;
4799
Wincy Van705699a2015-02-03 23:58:17 +08004800 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4801 if (!r)
4802 return;
4803
Yang Zhanga20ed542013-04-11 19:25:15 +08004804 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4805 return;
4806
4807 r = pi_test_and_set_on(&vmx->pi_desc);
4808 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004809 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08004810 kvm_vcpu_kick(vcpu);
4811}
4812
4813static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4814{
4815 struct vcpu_vmx *vmx = to_vmx(vcpu);
4816
4817 if (!pi_test_and_clear_on(&vmx->pi_desc))
4818 return;
4819
4820 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4821}
4822
Avi Kivity6aa8b732006-12-10 02:21:36 -08004823/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004824 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4825 * will not change in the lifetime of the guest.
4826 * Note that host-state that does change is set elsewhere. E.g., host-state
4827 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4828 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004829static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004830{
4831 u32 low32, high32;
4832 unsigned long tmpl;
4833 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004834 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004835
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004836 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004837 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4838
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004839 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004840 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004841 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4842 vmx->host_state.vmcs_host_cr4 = cr4;
4843
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004844 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004845#ifdef CONFIG_X86_64
4846 /*
4847 * Load null selectors, so we can avoid reloading them in
4848 * __vmx_load_host_state(), in case userspace uses the null selectors
4849 * too (the expected case).
4850 */
4851 vmcs_write16(HOST_DS_SELECTOR, 0);
4852 vmcs_write16(HOST_ES_SELECTOR, 0);
4853#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004854 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4855 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004856#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004857 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4858 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4859
4860 native_store_idt(&dt);
4861 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004862 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004863
Avi Kivity83287ea422012-09-16 15:10:57 +03004864 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004865
4866 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4867 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4868 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4869 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4870
4871 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4872 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4873 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4874 }
4875}
4876
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004877static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4878{
4879 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4880 if (enable_ept)
4881 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004882 if (is_guest_mode(&vmx->vcpu))
4883 vmx->vcpu.arch.cr4_guest_owned_bits &=
4884 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004885 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4886}
4887
Yang Zhang01e439b2013-04-11 19:25:12 +08004888static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4889{
4890 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4891
Andrey Smetanind62caab2015-11-10 15:36:33 +03004892 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004893 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07004894 /* Enable the preemption timer dynamically */
4895 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004896 return pin_based_exec_ctrl;
4897}
4898
Andrey Smetanind62caab2015-11-10 15:36:33 +03004899static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4900{
4901 struct vcpu_vmx *vmx = to_vmx(vcpu);
4902
4903 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03004904 if (cpu_has_secondary_exec_ctrls()) {
4905 if (kvm_vcpu_apicv_active(vcpu))
4906 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
4907 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4908 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4909 else
4910 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
4911 SECONDARY_EXEC_APIC_REGISTER_VIRT |
4912 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4913 }
4914
4915 if (cpu_has_vmx_msr_bitmap())
4916 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03004917}
4918
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004919static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4920{
4921 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004922
4923 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4924 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4925
Paolo Bonzini35754c92015-07-29 12:05:37 +02004926 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004927 exec_control &= ~CPU_BASED_TPR_SHADOW;
4928#ifdef CONFIG_X86_64
4929 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4930 CPU_BASED_CR8_LOAD_EXITING;
4931#endif
4932 }
4933 if (!enable_ept)
4934 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4935 CPU_BASED_CR3_LOAD_EXITING |
4936 CPU_BASED_INVLPG_EXITING;
4937 return exec_control;
4938}
4939
4940static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4941{
4942 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004943 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004944 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4945 if (vmx->vpid == 0)
4946 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4947 if (!enable_ept) {
4948 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4949 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004950 /* Enable INVPCID for non-ept guests may cause performance regression. */
4951 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004952 }
4953 if (!enable_unrestricted_guest)
4954 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4955 if (!ple_gap)
4956 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03004957 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004958 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4959 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004960 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004961 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4962 (handle_vmptrld).
4963 We can NOT enable shadow_vmcs here because we don't have yet
4964 a current VMCS12
4965 */
4966 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004967
4968 if (!enable_pml)
4969 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004970
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004971 return exec_control;
4972}
4973
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004974static void ept_set_mmio_spte_mask(void)
4975{
4976 /*
4977 * EPT Misconfigurations can be generated if the value of bits 2:0
4978 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004979 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004980 * spte.
4981 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004982 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004983}
4984
Wanpeng Lif53cd632014-12-02 19:14:58 +08004985#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004986/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004987 * Sets up the vmcs for emulated real mode.
4988 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004989static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004990{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004991#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004992 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004993#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004994 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004995
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004997 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4998 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004999
Abel Gordon4607c2d2013-04-18 14:35:55 +03005000 if (enable_shadow_vmcs) {
5001 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5002 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5003 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005004 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005005 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005006
Avi Kivity6aa8b732006-12-10 02:21:36 -08005007 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5008
Avi Kivity6aa8b732006-12-10 02:21:36 -08005009 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005010 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005011 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005012
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005013 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014
Dan Williamsdfa169b2016-06-02 11:17:24 -07005015 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005016 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5017 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005018 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005019
Andrey Smetanind62caab2015-11-10 15:36:33 +03005020 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005021 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5022 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5023 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5024 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5025
5026 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005027
Li RongQing0bcf2612015-12-03 13:29:34 +08005028 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005029 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005030 }
5031
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005032 if (ple_gap) {
5033 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005034 vmx->ple_window = ple_window;
5035 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005036 }
5037
Xiao Guangrongc3707952011-07-12 03:28:04 +08005038 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5039 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5041
Avi Kivity9581d442010-10-19 16:46:55 +02005042 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5043 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005044 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005045#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 rdmsrl(MSR_FS_BASE, a);
5047 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5048 rdmsrl(MSR_GS_BASE, a);
5049 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5050#else
5051 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5052 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5053#endif
5054
Eddie Dong2cc51562007-05-21 07:28:09 +03005055 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5056 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005057 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005058 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005059 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005060
Radim Krčmář74545702015-04-27 15:11:25 +02005061 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5062 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005063
Paolo Bonzini03916db2014-07-24 14:21:57 +02005064 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005065 u32 index = vmx_msr_index[i];
5066 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005067 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005068
5069 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5070 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005071 if (wrmsr_safe(index, data_low, data_high) < 0)
5072 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005073 vmx->guest_msrs[j].index = i;
5074 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005075 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005076 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005077 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005078
Gleb Natapov2961e8762013-11-25 15:37:13 +02005079
5080 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005081
5082 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005083 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005084
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005085 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005086 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005087
Wanpeng Lif53cd632014-12-02 19:14:58 +08005088 if (vmx_xsaves_supported())
5089 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5090
Peter Feiner4e595162016-07-07 14:49:58 -07005091 if (enable_pml) {
5092 ASSERT(vmx->pml_pg);
5093 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5094 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5095 }
5096
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005097 return 0;
5098}
5099
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005100static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005101{
5102 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005103 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005104 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005105
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005106 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005107
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005108 vmx->soft_vnmi_blocked = 0;
5109
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005110 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005111 kvm_set_cr8(vcpu, 0);
5112
5113 if (!init_event) {
5114 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5115 MSR_IA32_APICBASE_ENABLE;
5116 if (kvm_vcpu_is_reset_bsp(vcpu))
5117 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5118 apic_base_msr.host_initiated = true;
5119 kvm_set_apic_base(vcpu, &apic_base_msr);
5120 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005121
Avi Kivity2fb92db2011-04-27 19:42:18 +03005122 vmx_segment_cache_clear(vmx);
5123
Avi Kivity5706be02008-08-20 15:07:31 +03005124 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005125 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005126 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005127
5128 seg_setup(VCPU_SREG_DS);
5129 seg_setup(VCPU_SREG_ES);
5130 seg_setup(VCPU_SREG_FS);
5131 seg_setup(VCPU_SREG_GS);
5132 seg_setup(VCPU_SREG_SS);
5133
5134 vmcs_write16(GUEST_TR_SELECTOR, 0);
5135 vmcs_writel(GUEST_TR_BASE, 0);
5136 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5137 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5138
5139 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5140 vmcs_writel(GUEST_LDTR_BASE, 0);
5141 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5142 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5143
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005144 if (!init_event) {
5145 vmcs_write32(GUEST_SYSENTER_CS, 0);
5146 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5147 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5148 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5149 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005150
5151 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005152 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005153
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005154 vmcs_writel(GUEST_GDTR_BASE, 0);
5155 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5156
5157 vmcs_writel(GUEST_IDTR_BASE, 0);
5158 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5159
Anthony Liguori443381a2010-12-06 10:53:38 -06005160 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005161 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005162 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005163
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005164 setup_msrs(vmx);
5165
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5167
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005168 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005169 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005170 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005171 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005172 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005173 vmcs_write32(TPR_THRESHOLD, 0);
5174 }
5175
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005176 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005177
Andrey Smetanind62caab2015-11-10 15:36:33 +03005178 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005179 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5180
Sheng Yang2384d2b2008-01-17 15:14:33 +08005181 if (vmx->vpid != 0)
5182 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5183
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005184 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005185 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005186 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005187 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005188 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005189 vmx_fpu_activate(vcpu);
5190 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005191
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005192 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005193}
5194
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005195/*
5196 * In nested virtualization, check if L1 asked to exit on external interrupts.
5197 * For most existing hypervisors, this will always return true.
5198 */
5199static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5200{
5201 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5202 PIN_BASED_EXT_INTR_MASK;
5203}
5204
Bandan Das77b0f5d2014-04-19 18:17:45 -04005205/*
5206 * In nested virtualization, check if L1 has set
5207 * VM_EXIT_ACK_INTR_ON_EXIT
5208 */
5209static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5210{
5211 return get_vmcs12(vcpu)->vm_exit_controls &
5212 VM_EXIT_ACK_INTR_ON_EXIT;
5213}
5214
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005215static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5216{
5217 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5218 PIN_BASED_NMI_EXITING;
5219}
5220
Jan Kiszkac9a79532014-03-07 20:03:15 +01005221static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005222{
5223 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02005224
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005225 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5226 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
5227 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5228}
5229
Jan Kiszkac9a79532014-03-07 20:03:15 +01005230static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005231{
5232 u32 cpu_based_vm_exec_control;
5233
Jan Kiszkac9a79532014-03-07 20:03:15 +01005234 if (!cpu_has_virtual_nmis() ||
5235 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5236 enable_irq_window(vcpu);
5237 return;
5238 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005239
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005240 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5241 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
5242 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5243}
5244
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005245static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005246{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005247 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005248 uint32_t intr;
5249 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005250
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005251 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005252
Avi Kivityfa89a812008-09-01 15:57:51 +03005253 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005254 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005255 int inc_eip = 0;
5256 if (vcpu->arch.interrupt.soft)
5257 inc_eip = vcpu->arch.event_exit_inst_len;
5258 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005259 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005260 return;
5261 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005262 intr = irq | INTR_INFO_VALID_MASK;
5263 if (vcpu->arch.interrupt.soft) {
5264 intr |= INTR_TYPE_SOFT_INTR;
5265 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5266 vmx->vcpu.arch.event_exit_inst_len);
5267 } else
5268 intr |= INTR_TYPE_EXT_INTR;
5269 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005270}
5271
Sheng Yangf08864b2008-05-15 18:23:25 +08005272static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5273{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005274 struct vcpu_vmx *vmx = to_vmx(vcpu);
5275
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005276 if (is_guest_mode(vcpu))
5277 return;
5278
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005279 if (!cpu_has_virtual_nmis()) {
5280 /*
5281 * Tracking the NMI-blocked state in software is built upon
5282 * finding the next open IRQ window. This, in turn, depends on
5283 * well-behaving guests: They have to keep IRQs disabled at
5284 * least as long as the NMI handler runs. Otherwise we may
5285 * cause NMI nesting, maybe breaking the guest. But as this is
5286 * highly unlikely, we can live with the residual risk.
5287 */
5288 vmx->soft_vnmi_blocked = 1;
5289 vmx->vnmi_blocked_time = 0;
5290 }
5291
Jan Kiszka487b3912008-09-26 09:30:56 +02005292 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02005293 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005294 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005295 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005296 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005297 return;
5298 }
Sheng Yangf08864b2008-05-15 18:23:25 +08005299 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5300 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005301}
5302
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005303static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5304{
5305 if (!cpu_has_virtual_nmis())
5306 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005307 if (to_vmx(vcpu)->nmi_known_unmasked)
5308 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005309 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005310}
5311
5312static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5313{
5314 struct vcpu_vmx *vmx = to_vmx(vcpu);
5315
5316 if (!cpu_has_virtual_nmis()) {
5317 if (vmx->soft_vnmi_blocked != masked) {
5318 vmx->soft_vnmi_blocked = masked;
5319 vmx->vnmi_blocked_time = 0;
5320 }
5321 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005322 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005323 if (masked)
5324 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5325 GUEST_INTR_STATE_NMI);
5326 else
5327 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5328 GUEST_INTR_STATE_NMI);
5329 }
5330}
5331
Jan Kiszka2505dc92013-04-14 12:12:47 +02005332static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5333{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005334 if (to_vmx(vcpu)->nested.nested_run_pending)
5335 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005336
Jan Kiszka2505dc92013-04-14 12:12:47 +02005337 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5338 return 0;
5339
5340 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5341 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5342 | GUEST_INTR_STATE_NMI));
5343}
5344
Gleb Natapov78646122009-03-23 12:12:11 +02005345static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5346{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005347 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5348 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005349 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5350 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005351}
5352
Izik Eiduscbc94022007-10-25 00:29:55 +02005353static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5354{
5355 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005356
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005357 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5358 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005359 if (ret)
5360 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005361 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005362 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005363}
5364
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005365static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005366{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005367 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005368 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005369 /*
5370 * Update instruction length as we may reinject the exception
5371 * from user space while in guest debugging mode.
5372 */
5373 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5374 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005375 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005376 return false;
5377 /* fall through */
5378 case DB_VECTOR:
5379 if (vcpu->guest_debug &
5380 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5381 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005382 /* fall through */
5383 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005384 case OF_VECTOR:
5385 case BR_VECTOR:
5386 case UD_VECTOR:
5387 case DF_VECTOR:
5388 case SS_VECTOR:
5389 case GP_VECTOR:
5390 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005391 return true;
5392 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005393 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005394 return false;
5395}
5396
5397static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5398 int vec, u32 err_code)
5399{
5400 /*
5401 * Instruction with address size override prefix opcode 0x67
5402 * Cause the #SS fault with 0 error code in VM86 mode.
5403 */
5404 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5405 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5406 if (vcpu->arch.halt_request) {
5407 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005408 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005409 }
5410 return 1;
5411 }
5412 return 0;
5413 }
5414
5415 /*
5416 * Forward all other exceptions that are valid in real mode.
5417 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5418 * the required debugging infrastructure rework.
5419 */
5420 kvm_queue_exception(vcpu, vec);
5421 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005422}
5423
Andi Kleena0861c02009-06-08 17:37:09 +08005424/*
5425 * Trigger machine check on the host. We assume all the MSRs are already set up
5426 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5427 * We pass a fake environment to the machine check handler because we want
5428 * the guest to be always treated like user space, no matter what context
5429 * it used internally.
5430 */
5431static void kvm_machine_check(void)
5432{
5433#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5434 struct pt_regs regs = {
5435 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5436 .flags = X86_EFLAGS_IF,
5437 };
5438
5439 do_machine_check(&regs, 0);
5440#endif
5441}
5442
Avi Kivity851ba692009-08-24 11:10:17 +03005443static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005444{
5445 /* already handled by vcpu_run */
5446 return 1;
5447}
5448
Avi Kivity851ba692009-08-24 11:10:17 +03005449static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450{
Avi Kivity1155f762007-11-22 11:30:47 +02005451 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005452 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005453 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005454 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005455 u32 vect_info;
5456 enum emulation_result er;
5457
Avi Kivity1155f762007-11-22 11:30:47 +02005458 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005459 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005460
Andi Kleena0861c02009-06-08 17:37:09 +08005461 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005462 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005463
Jan Kiszkae4a41882008-09-26 09:30:46 +02005464 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02005465 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005466
5467 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005468 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005469 return 1;
5470 }
5471
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005472 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005473 if (is_guest_mode(vcpu)) {
5474 kvm_queue_exception(vcpu, UD_VECTOR);
5475 return 1;
5476 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005477 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005478 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005479 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005480 return 1;
5481 }
5482
Avi Kivity6aa8b732006-12-10 02:21:36 -08005483 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005484 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005485 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005486
5487 /*
5488 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5489 * MMIO, it is better to report an internal error.
5490 * See the comments in vmx_handle_exit.
5491 */
5492 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5493 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5494 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5495 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005496 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005497 vcpu->run->internal.data[0] = vect_info;
5498 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005499 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005500 return 0;
5501 }
5502
Avi Kivity6aa8b732006-12-10 02:21:36 -08005503 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005504 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005505 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005506 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005507 trace_kvm_page_fault(cr2, error_code);
5508
Gleb Natapov3298b752009-05-11 13:35:46 +03005509 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005510 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005511 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005512 }
5513
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005514 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005515
5516 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5517 return handle_rmode_exception(vcpu, ex_no, error_code);
5518
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005519 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005520 case AC_VECTOR:
5521 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5522 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005523 case DB_VECTOR:
5524 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5525 if (!(vcpu->guest_debug &
5526 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005527 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005528 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005529 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5530 skip_emulated_instruction(vcpu);
5531
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005532 kvm_queue_exception(vcpu, DB_VECTOR);
5533 return 1;
5534 }
5535 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5536 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5537 /* fall through */
5538 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01005539 /*
5540 * Update instruction length as we may reinject #BP from
5541 * user space while in guest debugging mode. Reading it for
5542 * #DB as well causes no harm, it is not used in that case.
5543 */
5544 vmx->vcpu.arch.event_exit_inst_len =
5545 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005547 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005548 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5549 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005550 break;
5551 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005552 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5553 kvm_run->ex.exception = ex_no;
5554 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005555 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005557 return 0;
5558}
5559
Avi Kivity851ba692009-08-24 11:10:17 +03005560static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005562 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005563 return 1;
5564}
5565
Avi Kivity851ba692009-08-24 11:10:17 +03005566static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005567{
Avi Kivity851ba692009-08-24 11:10:17 +03005568 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005569 return 0;
5570}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005571
Avi Kivity851ba692009-08-24 11:10:17 +03005572static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573{
He, Qingbfdaab02007-09-12 14:18:28 +08005574 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005575 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005576 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005577
He, Qingbfdaab02007-09-12 14:18:28 +08005578 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005579 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005580 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005581
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005582 ++vcpu->stat.io_exits;
5583
5584 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005585 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005586
5587 port = exit_qualification >> 16;
5588 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005589 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005590
5591 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005592}
5593
Ingo Molnar102d8322007-02-19 14:37:47 +02005594static void
5595vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5596{
5597 /*
5598 * Patch in the VMCALL instruction:
5599 */
5600 hypercall[0] = 0x0f;
5601 hypercall[1] = 0x01;
5602 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005603}
5604
Wincy Vanb9c237b2015-02-03 23:56:30 +08005605static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005606{
5607 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005608 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005609
Wincy Vanb9c237b2015-02-03 23:56:30 +08005610 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005611 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5612 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5613 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5614 return (val & always_on) == always_on;
5615}
5616
Guo Chao0fa06072012-06-28 15:16:19 +08005617/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005618static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5619{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005620 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005621 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5622 unsigned long orig_val = val;
5623
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005624 /*
5625 * We get here when L2 changed cr0 in a way that did not change
5626 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005627 * but did change L0 shadowed bits. So we first calculate the
5628 * effective cr0 value that L1 would like to write into the
5629 * hardware. It consists of the L2-owned bits from the new
5630 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005631 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005632 val = (val & ~vmcs12->cr0_guest_host_mask) |
5633 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5634
Wincy Vanb9c237b2015-02-03 23:56:30 +08005635 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005636 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005637
5638 if (kvm_set_cr0(vcpu, val))
5639 return 1;
5640 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005641 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005642 } else {
5643 if (to_vmx(vcpu)->nested.vmxon &&
5644 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5645 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005646 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005647 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005648}
5649
5650static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5651{
5652 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005653 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5654 unsigned long orig_val = val;
5655
5656 /* analogously to handle_set_cr0 */
5657 val = (val & ~vmcs12->cr4_guest_host_mask) |
5658 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5659 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005660 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005661 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005662 return 0;
5663 } else
5664 return kvm_set_cr4(vcpu, val);
5665}
5666
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08005667/* called to set cr0 as appropriate for clts instruction exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005668static void handle_clts(struct kvm_vcpu *vcpu)
5669{
5670 if (is_guest_mode(vcpu)) {
5671 /*
5672 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5673 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5674 * just pretend it's off (also in arch.cr0 for fpu_activate).
5675 */
5676 vmcs_writel(CR0_READ_SHADOW,
5677 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5678 vcpu->arch.cr0 &= ~X86_CR0_TS;
5679 } else
5680 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5681}
5682
Avi Kivity851ba692009-08-24 11:10:17 +03005683static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005684{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005685 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005686 int cr;
5687 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005688 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005689
He, Qingbfdaab02007-09-12 14:18:28 +08005690 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005691 cr = exit_qualification & 15;
5692 reg = (exit_qualification >> 8) & 15;
5693 switch ((exit_qualification >> 4) & 3) {
5694 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005695 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005696 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005697 switch (cr) {
5698 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005699 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005700 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005701 return 1;
5702 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005703 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005704 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005705 return 1;
5706 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005707 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005708 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005709 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005710 case 8: {
5711 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005712 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005713 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005714 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005715 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005716 return 1;
5717 if (cr8_prev <= cr8)
5718 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005719 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005720 return 0;
5721 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005722 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005723 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005724 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005725 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005726 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005727 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005728 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005729 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005730 case 1: /*mov from cr*/
5731 switch (cr) {
5732 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005733 val = kvm_read_cr3(vcpu);
5734 kvm_register_write(vcpu, reg, val);
5735 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005736 skip_emulated_instruction(vcpu);
5737 return 1;
5738 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005739 val = kvm_get_cr8(vcpu);
5740 kvm_register_write(vcpu, reg, val);
5741 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005742 skip_emulated_instruction(vcpu);
5743 return 1;
5744 }
5745 break;
5746 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005747 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005748 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005749 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005750
5751 skip_emulated_instruction(vcpu);
5752 return 1;
5753 default:
5754 break;
5755 }
Avi Kivity851ba692009-08-24 11:10:17 +03005756 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005757 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005758 (int)(exit_qualification >> 4) & 3, cr);
5759 return 0;
5760}
5761
Avi Kivity851ba692009-08-24 11:10:17 +03005762static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005763{
He, Qingbfdaab02007-09-12 14:18:28 +08005764 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005765 int dr, dr7, reg;
5766
5767 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5768 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5769
5770 /* First, if DR does not exist, trigger UD */
5771 if (!kvm_require_dr(vcpu, dr))
5772 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005773
Jan Kiszkaf2483412010-01-20 18:20:20 +01005774 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005775 if (!kvm_require_cpl(vcpu, 0))
5776 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005777 dr7 = vmcs_readl(GUEST_DR7);
5778 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005779 /*
5780 * As the vm-exit takes precedence over the debug trap, we
5781 * need to emulate the latter, either for the host or the
5782 * guest debugging itself.
5783 */
5784 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005785 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005786 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005787 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005788 vcpu->run->debug.arch.exception = DB_VECTOR;
5789 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005790 return 0;
5791 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005792 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005793 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005794 kvm_queue_exception(vcpu, DB_VECTOR);
5795 return 1;
5796 }
5797 }
5798
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005799 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01005800 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5801 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005802
5803 /*
5804 * No more DR vmexits; force a reload of the debug registers
5805 * and reenter on this instruction. The next vmexit will
5806 * retrieve the full state of the debug registers.
5807 */
5808 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5809 return 1;
5810 }
5811
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005812 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5813 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005814 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005815
5816 if (kvm_get_dr(vcpu, dr, &val))
5817 return 1;
5818 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005819 } else
Nadav Amit57773922014-06-18 17:19:23 +03005820 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005821 return 1;
5822
Avi Kivity6aa8b732006-12-10 02:21:36 -08005823 skip_emulated_instruction(vcpu);
5824 return 1;
5825}
5826
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005827static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5828{
5829 return vcpu->arch.dr6;
5830}
5831
5832static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5833{
5834}
5835
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005836static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5837{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005838 get_debugreg(vcpu->arch.db[0], 0);
5839 get_debugreg(vcpu->arch.db[1], 1);
5840 get_debugreg(vcpu->arch.db[2], 2);
5841 get_debugreg(vcpu->arch.db[3], 3);
5842 get_debugreg(vcpu->arch.dr6, 6);
5843 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5844
5845 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01005846 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005847}
5848
Gleb Natapov020df072010-04-13 10:05:23 +03005849static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5850{
5851 vmcs_writel(GUEST_DR7, val);
5852}
5853
Avi Kivity851ba692009-08-24 11:10:17 +03005854static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005855{
Avi Kivity06465c52007-02-28 20:46:53 +02005856 kvm_emulate_cpuid(vcpu);
5857 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005858}
5859
Avi Kivity851ba692009-08-24 11:10:17 +03005860static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005861{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005862 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005863 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005864
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005865 msr_info.index = ecx;
5866 msr_info.host_initiated = false;
5867 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005868 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005869 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005870 return 1;
5871 }
5872
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005873 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005874
Avi Kivity6aa8b732006-12-10 02:21:36 -08005875 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005876 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5877 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005878 skip_emulated_instruction(vcpu);
5879 return 1;
5880}
5881
Avi Kivity851ba692009-08-24 11:10:17 +03005882static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005883{
Will Auld8fe8ab42012-11-29 12:42:12 -08005884 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005885 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5886 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5887 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005888
Will Auld8fe8ab42012-11-29 12:42:12 -08005889 msr.data = data;
5890 msr.index = ecx;
5891 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005892 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005893 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005894 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005895 return 1;
5896 }
5897
Avi Kivity59200272010-01-25 19:47:02 +02005898 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005899 skip_emulated_instruction(vcpu);
5900 return 1;
5901}
5902
Avi Kivity851ba692009-08-24 11:10:17 +03005903static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005904{
Avi Kivity3842d132010-07-27 12:30:24 +03005905 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005906 return 1;
5907}
5908
Avi Kivity851ba692009-08-24 11:10:17 +03005909static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005910{
Eddie Dong85f455f2007-07-06 12:20:49 +03005911 u32 cpu_based_vm_exec_control;
5912
5913 /* clear pending irq */
5914 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5915 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5916 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005917
Avi Kivity3842d132010-07-27 12:30:24 +03005918 kvm_make_request(KVM_REQ_EVENT, vcpu);
5919
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005920 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005921 return 1;
5922}
5923
Avi Kivity851ba692009-08-24 11:10:17 +03005924static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005925{
Avi Kivityd3bef152007-06-05 15:53:05 +03005926 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005927}
5928
Avi Kivity851ba692009-08-24 11:10:17 +03005929static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005930{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03005931 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02005932}
5933
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005934static int handle_invd(struct kvm_vcpu *vcpu)
5935{
Andre Przywara51d8b662010-12-21 11:12:02 +01005936 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005937}
5938
Avi Kivity851ba692009-08-24 11:10:17 +03005939static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005940{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005941 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005942
5943 kvm_mmu_invlpg(vcpu, exit_qualification);
5944 skip_emulated_instruction(vcpu);
5945 return 1;
5946}
5947
Avi Kivityfee84b02011-11-10 14:57:25 +02005948static int handle_rdpmc(struct kvm_vcpu *vcpu)
5949{
5950 int err;
5951
5952 err = kvm_rdpmc(vcpu);
5953 kvm_complete_insn_gp(vcpu, err);
5954
5955 return 1;
5956}
5957
Avi Kivity851ba692009-08-24 11:10:17 +03005958static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005959{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005960 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005961 return 1;
5962}
5963
Dexuan Cui2acf9232010-06-10 11:27:12 +08005964static int handle_xsetbv(struct kvm_vcpu *vcpu)
5965{
5966 u64 new_bv = kvm_read_edx_eax(vcpu);
5967 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5968
5969 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5970 skip_emulated_instruction(vcpu);
5971 return 1;
5972}
5973
Wanpeng Lif53cd632014-12-02 19:14:58 +08005974static int handle_xsaves(struct kvm_vcpu *vcpu)
5975{
5976 skip_emulated_instruction(vcpu);
5977 WARN(1, "this should never happen\n");
5978 return 1;
5979}
5980
5981static int handle_xrstors(struct kvm_vcpu *vcpu)
5982{
5983 skip_emulated_instruction(vcpu);
5984 WARN(1, "this should never happen\n");
5985 return 1;
5986}
5987
Avi Kivity851ba692009-08-24 11:10:17 +03005988static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005989{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005990 if (likely(fasteoi)) {
5991 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5992 int access_type, offset;
5993
5994 access_type = exit_qualification & APIC_ACCESS_TYPE;
5995 offset = exit_qualification & APIC_ACCESS_OFFSET;
5996 /*
5997 * Sane guest uses MOV to write EOI, with written value
5998 * not cared. So make a short-circuit here by avoiding
5999 * heavy instruction emulation.
6000 */
6001 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6002 (offset == APIC_EOI)) {
6003 kvm_lapic_set_eoi(vcpu);
6004 skip_emulated_instruction(vcpu);
6005 return 1;
6006 }
6007 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006008 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006009}
6010
Yang Zhangc7c9c562013-01-25 10:18:51 +08006011static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6012{
6013 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6014 int vector = exit_qualification & 0xff;
6015
6016 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6017 kvm_apic_set_eoi_accelerated(vcpu, vector);
6018 return 1;
6019}
6020
Yang Zhang83d4c282013-01-25 10:18:49 +08006021static int handle_apic_write(struct kvm_vcpu *vcpu)
6022{
6023 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6024 u32 offset = exit_qualification & 0xfff;
6025
6026 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6027 kvm_apic_write_nodecode(vcpu, offset);
6028 return 1;
6029}
6030
Avi Kivity851ba692009-08-24 11:10:17 +03006031static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006032{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006033 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006034 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006035 bool has_error_code = false;
6036 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006037 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006038 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006039
6040 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006041 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006042 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006043
6044 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6045
6046 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006047 if (reason == TASK_SWITCH_GATE && idt_v) {
6048 switch (type) {
6049 case INTR_TYPE_NMI_INTR:
6050 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006051 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006052 break;
6053 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006054 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006055 kvm_clear_interrupt_queue(vcpu);
6056 break;
6057 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006058 if (vmx->idt_vectoring_info &
6059 VECTORING_INFO_DELIVER_CODE_MASK) {
6060 has_error_code = true;
6061 error_code =
6062 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6063 }
6064 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006065 case INTR_TYPE_SOFT_EXCEPTION:
6066 kvm_clear_exception_queue(vcpu);
6067 break;
6068 default:
6069 break;
6070 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006071 }
Izik Eidus37817f22008-03-24 23:14:53 +02006072 tss_selector = exit_qualification;
6073
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006074 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6075 type != INTR_TYPE_EXT_INTR &&
6076 type != INTR_TYPE_NMI_INTR))
6077 skip_emulated_instruction(vcpu);
6078
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006079 if (kvm_task_switch(vcpu, tss_selector,
6080 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6081 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006082 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6083 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6084 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006085 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006086 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006087
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006088 /*
6089 * TODO: What about debug traps on tss switch?
6090 * Are we supposed to inject them and update dr6?
6091 */
6092
6093 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006094}
6095
Avi Kivity851ba692009-08-24 11:10:17 +03006096static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006097{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006098 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006099 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006100 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006101 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08006102
Sheng Yangf9c617f2009-03-25 10:08:52 +08006103 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006104
Sheng Yang14394422008-04-28 12:24:45 +08006105 gla_validity = (exit_qualification >> 7) & 0x3;
6106 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
6107 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
6108 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
6109 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08006110 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08006111 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
6112 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03006113 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6114 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03006115 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08006116 }
6117
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006118 /*
6119 * EPT violation happened while executing iret from NMI,
6120 * "blocked by NMI" bit has to be set before next VM entry.
6121 * There are errata that may cause this bit to not be set:
6122 * AAK134, BY25.
6123 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006124 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6125 cpu_has_virtual_nmis() &&
6126 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006127 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6128
Sheng Yang14394422008-04-28 12:24:45 +08006129 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006130 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006131
Bandan Dasd95c5562016-07-12 18:18:51 -04006132 /* it is a read fault? */
6133 error_code = (exit_qualification << 2) & PFERR_USER_MASK;
6134 /* it is a write fault? */
6135 error_code |= exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03006136 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08006137 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006138 /* ept page table is present? */
Bandan Dasd95c5562016-07-12 18:18:51 -04006139 error_code |= (exit_qualification & 0x38) != 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006140
Yang Zhang25d92082013-08-06 12:00:32 +03006141 vcpu->arch.exit_qualification = exit_qualification;
6142
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006143 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006144}
6145
Avi Kivity851ba692009-08-24 11:10:17 +03006146static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006147{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006148 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006149 gpa_t gpa;
6150
6151 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006152 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006153 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08006154 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006155 return 1;
6156 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006157
Paolo Bonzini450869d2015-11-04 13:41:21 +01006158 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006159 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006160 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6161 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006162
6163 if (unlikely(ret == RET_MMIO_PF_INVALID))
6164 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6165
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006166 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006167 return 1;
6168
6169 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006170 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006171
Avi Kivity851ba692009-08-24 11:10:17 +03006172 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6173 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006174
6175 return 0;
6176}
6177
Avi Kivity851ba692009-08-24 11:10:17 +03006178static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006179{
6180 u32 cpu_based_vm_exec_control;
6181
6182 /* clear pending NMI */
6183 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6184 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6185 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
6186 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006187 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006188
6189 return 1;
6190}
6191
Mohammed Gamal80ced182009-09-01 12:48:18 +02006192static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006193{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006194 struct vcpu_vmx *vmx = to_vmx(vcpu);
6195 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006196 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006197 u32 cpu_exec_ctrl;
6198 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006199 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006200
6201 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6202 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006203
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006204 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006205 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006206 return handle_interrupt_window(&vmx->vcpu);
6207
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006208 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6209 return 1;
6210
Gleb Natapov991eebf2013-04-11 12:10:51 +03006211 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006212
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006213 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006214 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006215 ret = 0;
6216 goto out;
6217 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006218
Avi Kivityde5f70e2012-06-12 20:22:28 +03006219 if (err != EMULATE_DONE) {
6220 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6221 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6222 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006223 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006224 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006225
Gleb Natapov8d76c492013-05-08 18:38:44 +03006226 if (vcpu->arch.halt_request) {
6227 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006228 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006229 goto out;
6230 }
6231
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006232 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006233 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006234 if (need_resched())
6235 schedule();
6236 }
6237
Mohammed Gamal80ced182009-09-01 12:48:18 +02006238out:
6239 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006240}
6241
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006242static int __grow_ple_window(int val)
6243{
6244 if (ple_window_grow < 1)
6245 return ple_window;
6246
6247 val = min(val, ple_window_actual_max);
6248
6249 if (ple_window_grow < ple_window)
6250 val *= ple_window_grow;
6251 else
6252 val += ple_window_grow;
6253
6254 return val;
6255}
6256
6257static int __shrink_ple_window(int val, int modifier, int minimum)
6258{
6259 if (modifier < 1)
6260 return ple_window;
6261
6262 if (modifier < ple_window)
6263 val /= modifier;
6264 else
6265 val -= modifier;
6266
6267 return max(val, minimum);
6268}
6269
6270static void grow_ple_window(struct kvm_vcpu *vcpu)
6271{
6272 struct vcpu_vmx *vmx = to_vmx(vcpu);
6273 int old = vmx->ple_window;
6274
6275 vmx->ple_window = __grow_ple_window(old);
6276
6277 if (vmx->ple_window != old)
6278 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006279
6280 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006281}
6282
6283static void shrink_ple_window(struct kvm_vcpu *vcpu)
6284{
6285 struct vcpu_vmx *vmx = to_vmx(vcpu);
6286 int old = vmx->ple_window;
6287
6288 vmx->ple_window = __shrink_ple_window(old,
6289 ple_window_shrink, ple_window);
6290
6291 if (vmx->ple_window != old)
6292 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006293
6294 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006295}
6296
6297/*
6298 * ple_window_actual_max is computed to be one grow_ple_window() below
6299 * ple_window_max. (See __grow_ple_window for the reason.)
6300 * This prevents overflows, because ple_window_max is int.
6301 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6302 * this process.
6303 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6304 */
6305static void update_ple_window_actual_max(void)
6306{
6307 ple_window_actual_max =
6308 __shrink_ple_window(max(ple_window_max, ple_window),
6309 ple_window_grow, INT_MIN);
6310}
6311
Feng Wubf9f6ac2015-09-18 22:29:55 +08006312/*
6313 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6314 */
6315static void wakeup_handler(void)
6316{
6317 struct kvm_vcpu *vcpu;
6318 int cpu = smp_processor_id();
6319
6320 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6321 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6322 blocked_vcpu_list) {
6323 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6324
6325 if (pi_test_on(pi_desc) == 1)
6326 kvm_vcpu_kick(vcpu);
6327 }
6328 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6329}
6330
Tiejun Chenf2c76482014-10-28 10:14:47 +08006331static __init int hardware_setup(void)
6332{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006333 int r = -ENOMEM, i, msr;
6334
6335 rdmsrl_safe(MSR_EFER, &host_efer);
6336
6337 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6338 kvm_define_shared_msr(i, vmx_msr_index[i]);
6339
6340 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6341 if (!vmx_io_bitmap_a)
6342 return r;
6343
6344 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6345 if (!vmx_io_bitmap_b)
6346 goto out;
6347
6348 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
6349 if (!vmx_msr_bitmap_legacy)
6350 goto out1;
6351
6352 vmx_msr_bitmap_legacy_x2apic =
6353 (unsigned long *)__get_free_page(GFP_KERNEL);
6354 if (!vmx_msr_bitmap_legacy_x2apic)
6355 goto out2;
6356
6357 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
6358 if (!vmx_msr_bitmap_longmode)
6359 goto out3;
6360
6361 vmx_msr_bitmap_longmode_x2apic =
6362 (unsigned long *)__get_free_page(GFP_KERNEL);
6363 if (!vmx_msr_bitmap_longmode_x2apic)
6364 goto out4;
Wincy Van3af18d92015-02-03 23:49:31 +08006365
6366 if (nested) {
6367 vmx_msr_bitmap_nested =
6368 (unsigned long *)__get_free_page(GFP_KERNEL);
6369 if (!vmx_msr_bitmap_nested)
6370 goto out5;
6371 }
6372
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006373 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6374 if (!vmx_vmread_bitmap)
Wincy Van3af18d92015-02-03 23:49:31 +08006375 goto out6;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006376
6377 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6378 if (!vmx_vmwrite_bitmap)
Wincy Van3af18d92015-02-03 23:49:31 +08006379 goto out7;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006380
6381 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6382 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6383
6384 /*
6385 * Allow direct access to the PC debug port (it is often used for I/O
6386 * delays, but the vmexits simply slow things down).
6387 */
6388 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6389 clear_bit(0x80, vmx_io_bitmap_a);
6390
6391 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6392
6393 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6394 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Wincy Van3af18d92015-02-03 23:49:31 +08006395 if (nested)
6396 memset(vmx_msr_bitmap_nested, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006397
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006398 if (setup_vmcs_config(&vmcs_config) < 0) {
6399 r = -EIO;
Wincy Van3af18d92015-02-03 23:49:31 +08006400 goto out8;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006401 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006402
6403 if (boot_cpu_has(X86_FEATURE_NX))
6404 kvm_enable_efer_bits(EFER_NX);
6405
6406 if (!cpu_has_vmx_vpid())
6407 enable_vpid = 0;
6408 if (!cpu_has_vmx_shadow_vmcs())
6409 enable_shadow_vmcs = 0;
6410 if (enable_shadow_vmcs)
6411 init_vmcs_shadow_fields();
6412
6413 if (!cpu_has_vmx_ept() ||
6414 !cpu_has_vmx_ept_4levels()) {
6415 enable_ept = 0;
6416 enable_unrestricted_guest = 0;
6417 enable_ept_ad_bits = 0;
6418 }
6419
6420 if (!cpu_has_vmx_ept_ad_bits())
6421 enable_ept_ad_bits = 0;
6422
6423 if (!cpu_has_vmx_unrestricted_guest())
6424 enable_unrestricted_guest = 0;
6425
Paolo Bonziniad15a292015-01-30 16:18:49 +01006426 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006427 flexpriority_enabled = 0;
6428
Paolo Bonziniad15a292015-01-30 16:18:49 +01006429 /*
6430 * set_apic_access_page_addr() is used to reload apic access
6431 * page upon invalidation. No need to do anything if not
6432 * using the APIC_ACCESS_ADDR VMCS field.
6433 */
6434 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006435 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006436
6437 if (!cpu_has_vmx_tpr_shadow())
6438 kvm_x86_ops->update_cr8_intercept = NULL;
6439
6440 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6441 kvm_disable_largepages();
6442
6443 if (!cpu_has_vmx_ple())
6444 ple_gap = 0;
6445
6446 if (!cpu_has_vmx_apicv())
6447 enable_apicv = 0;
6448
Haozhong Zhang64903d62015-10-20 15:39:09 +08006449 if (cpu_has_vmx_tsc_scaling()) {
6450 kvm_has_tsc_control = true;
6451 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6452 kvm_tsc_scaling_ratio_frac_bits = 48;
6453 }
6454
Tiejun Chenbaa03522014-12-23 16:21:11 +08006455 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6456 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6457 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6458 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6459 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6460 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6461 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6462
6463 memcpy(vmx_msr_bitmap_legacy_x2apic,
6464 vmx_msr_bitmap_legacy, PAGE_SIZE);
6465 memcpy(vmx_msr_bitmap_longmode_x2apic,
6466 vmx_msr_bitmap_longmode, PAGE_SIZE);
6467
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006468 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6469
Roman Kagan3ce424e2016-05-18 17:48:20 +03006470 for (msr = 0x800; msr <= 0x8ff; msr++)
6471 vmx_disable_intercept_msr_read_x2apic(msr);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006472
Roman Kagan3ce424e2016-05-18 17:48:20 +03006473 /* TMCCT */
6474 vmx_enable_intercept_msr_read_x2apic(0x839);
6475 /* TPR */
6476 vmx_disable_intercept_msr_write_x2apic(0x808);
6477 /* EOI */
6478 vmx_disable_intercept_msr_write_x2apic(0x80b);
6479 /* SELF-IPI */
6480 vmx_disable_intercept_msr_write_x2apic(0x83f);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006481
6482 if (enable_ept) {
Bandan Dasd95c5562016-07-12 18:18:51 -04006483 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
Tiejun Chenbaa03522014-12-23 16:21:11 +08006484 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6485 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
Bandan Dasd95c5562016-07-12 18:18:51 -04006486 0ull, VMX_EPT_EXECUTABLE_MASK,
6487 cpu_has_vmx_ept_execute_only() ?
6488 0ull : VMX_EPT_READABLE_MASK);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006489 ept_set_mmio_spte_mask();
6490 kvm_enable_tdp();
6491 } else
6492 kvm_disable_tdp();
6493
6494 update_ple_window_actual_max();
6495
Kai Huang843e4332015-01-28 10:54:28 +08006496 /*
6497 * Only enable PML when hardware supports PML feature, and both EPT
6498 * and EPT A/D bit features are enabled -- PML depends on them to work.
6499 */
6500 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6501 enable_pml = 0;
6502
6503 if (!enable_pml) {
6504 kvm_x86_ops->slot_enable_log_dirty = NULL;
6505 kvm_x86_ops->slot_disable_log_dirty = NULL;
6506 kvm_x86_ops->flush_log_dirty = NULL;
6507 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6508 }
6509
Yunhong Jiang64672c92016-06-13 14:19:59 -07006510 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6511 u64 vmx_msr;
6512
6513 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6514 cpu_preemption_timer_multi =
6515 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6516 } else {
6517 kvm_x86_ops->set_hv_timer = NULL;
6518 kvm_x86_ops->cancel_hv_timer = NULL;
6519 }
6520
Feng Wubf9f6ac2015-09-18 22:29:55 +08006521 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6522
Ashok Rajc45dcc72016-06-22 14:59:56 +08006523 kvm_mce_cap_supported |= MCG_LMCE_P;
6524
Tiejun Chenf2c76482014-10-28 10:14:47 +08006525 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006526
Wincy Van3af18d92015-02-03 23:49:31 +08006527out8:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006528 free_page((unsigned long)vmx_vmwrite_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006529out7:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006530 free_page((unsigned long)vmx_vmread_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006531out6:
6532 if (nested)
6533 free_page((unsigned long)vmx_msr_bitmap_nested);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006534out5:
6535 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6536out4:
6537 free_page((unsigned long)vmx_msr_bitmap_longmode);
6538out3:
6539 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6540out2:
6541 free_page((unsigned long)vmx_msr_bitmap_legacy);
6542out1:
6543 free_page((unsigned long)vmx_io_bitmap_b);
6544out:
6545 free_page((unsigned long)vmx_io_bitmap_a);
6546
6547 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006548}
6549
6550static __exit void hardware_unsetup(void)
6551{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006552 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6553 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6554 free_page((unsigned long)vmx_msr_bitmap_legacy);
6555 free_page((unsigned long)vmx_msr_bitmap_longmode);
6556 free_page((unsigned long)vmx_io_bitmap_b);
6557 free_page((unsigned long)vmx_io_bitmap_a);
6558 free_page((unsigned long)vmx_vmwrite_bitmap);
6559 free_page((unsigned long)vmx_vmread_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006560 if (nested)
6561 free_page((unsigned long)vmx_msr_bitmap_nested);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006562
Tiejun Chenf2c76482014-10-28 10:14:47 +08006563 free_kvm_area();
6564}
6565
Avi Kivity6aa8b732006-12-10 02:21:36 -08006566/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006567 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6568 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6569 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006570static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006571{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006572 if (ple_gap)
6573 grow_ple_window(vcpu);
6574
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006575 skip_emulated_instruction(vcpu);
6576 kvm_vcpu_on_spin(vcpu);
6577
6578 return 1;
6579}
6580
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006581static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006582{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006583 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006584 return 1;
6585}
6586
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006587static int handle_mwait(struct kvm_vcpu *vcpu)
6588{
6589 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6590 return handle_nop(vcpu);
6591}
6592
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006593static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6594{
6595 return 1;
6596}
6597
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006598static int handle_monitor(struct kvm_vcpu *vcpu)
6599{
6600 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6601 return handle_nop(vcpu);
6602}
6603
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006604/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006605 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6606 * We could reuse a single VMCS for all the L2 guests, but we also want the
6607 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6608 * allows keeping them loaded on the processor, and in the future will allow
6609 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6610 * every entry if they never change.
6611 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6612 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6613 *
6614 * The following functions allocate and free a vmcs02 in this pool.
6615 */
6616
6617/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6618static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6619{
6620 struct vmcs02_list *item;
6621 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6622 if (item->vmptr == vmx->nested.current_vmptr) {
6623 list_move(&item->list, &vmx->nested.vmcs02_pool);
6624 return &item->vmcs02;
6625 }
6626
6627 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6628 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08006629 item = list_last_entry(&vmx->nested.vmcs02_pool,
6630 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006631 item->vmptr = vmx->nested.current_vmptr;
6632 list_move(&item->list, &vmx->nested.vmcs02_pool);
6633 return &item->vmcs02;
6634 }
6635
6636 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006637 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006638 if (!item)
6639 return NULL;
6640 item->vmcs02.vmcs = alloc_vmcs();
6641 if (!item->vmcs02.vmcs) {
6642 kfree(item);
6643 return NULL;
6644 }
6645 loaded_vmcs_init(&item->vmcs02);
6646 item->vmptr = vmx->nested.current_vmptr;
6647 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6648 vmx->nested.vmcs02_num++;
6649 return &item->vmcs02;
6650}
6651
6652/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6653static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6654{
6655 struct vmcs02_list *item;
6656 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6657 if (item->vmptr == vmptr) {
6658 free_loaded_vmcs(&item->vmcs02);
6659 list_del(&item->list);
6660 kfree(item);
6661 vmx->nested.vmcs02_num--;
6662 return;
6663 }
6664}
6665
6666/*
6667 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006668 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6669 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006670 */
6671static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6672{
6673 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006674
6675 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006676 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006677 /*
6678 * Something will leak if the above WARN triggers. Better than
6679 * a use-after-free.
6680 */
6681 if (vmx->loaded_vmcs == &item->vmcs02)
6682 continue;
6683
6684 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006685 list_del(&item->list);
6686 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006687 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006688 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006689}
6690
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006691/*
6692 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6693 * set the success or error code of an emulated VMX instruction, as specified
6694 * by Vol 2B, VMX Instruction Reference, "Conventions".
6695 */
6696static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6697{
6698 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6699 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6700 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6701}
6702
6703static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6704{
6705 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6706 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6707 X86_EFLAGS_SF | X86_EFLAGS_OF))
6708 | X86_EFLAGS_CF);
6709}
6710
Abel Gordon145c28d2013-04-18 14:36:55 +03006711static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006712 u32 vm_instruction_error)
6713{
6714 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6715 /*
6716 * failValid writes the error number to the current VMCS, which
6717 * can't be done there isn't a current VMCS.
6718 */
6719 nested_vmx_failInvalid(vcpu);
6720 return;
6721 }
6722 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6723 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6724 X86_EFLAGS_SF | X86_EFLAGS_OF))
6725 | X86_EFLAGS_ZF);
6726 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6727 /*
6728 * We don't need to force a shadow sync because
6729 * VM_INSTRUCTION_ERROR is not shadowed
6730 */
6731}
Abel Gordon145c28d2013-04-18 14:36:55 +03006732
Wincy Vanff651cb2014-12-11 08:52:58 +03006733static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6734{
6735 /* TODO: not to reset guest simply here. */
6736 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6737 pr_warn("kvm: nested vmx abort, indicator %d\n", indicator);
6738}
6739
Jan Kiszkaf4124502014-03-07 20:03:13 +01006740static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6741{
6742 struct vcpu_vmx *vmx =
6743 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6744
6745 vmx->nested.preemption_timer_expired = true;
6746 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6747 kvm_vcpu_kick(&vmx->vcpu);
6748
6749 return HRTIMER_NORESTART;
6750}
6751
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006752/*
Bandan Das19677e32014-05-06 02:19:15 -04006753 * Decode the memory-address operand of a vmx instruction, as recorded on an
6754 * exit caused by such an instruction (run by a guest hypervisor).
6755 * On success, returns 0. When the operand is invalid, returns 1 and throws
6756 * #UD or #GP.
6757 */
6758static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6759 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006760 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006761{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006762 gva_t off;
6763 bool exn;
6764 struct kvm_segment s;
6765
Bandan Das19677e32014-05-06 02:19:15 -04006766 /*
6767 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6768 * Execution", on an exit, vmx_instruction_info holds most of the
6769 * addressing components of the operand. Only the displacement part
6770 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6771 * For how an actual address is calculated from all these components,
6772 * refer to Vol. 1, "Operand Addressing".
6773 */
6774 int scaling = vmx_instruction_info & 3;
6775 int addr_size = (vmx_instruction_info >> 7) & 7;
6776 bool is_reg = vmx_instruction_info & (1u << 10);
6777 int seg_reg = (vmx_instruction_info >> 15) & 7;
6778 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6779 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6780 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6781 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6782
6783 if (is_reg) {
6784 kvm_queue_exception(vcpu, UD_VECTOR);
6785 return 1;
6786 }
6787
6788 /* Addr = segment_base + offset */
6789 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006790 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006791 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006792 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006793 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006794 off += kvm_register_read(vcpu, index_reg)<<scaling;
6795 vmx_get_segment(vcpu, &s, seg_reg);
6796 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006797
6798 if (addr_size == 1) /* 32 bit */
6799 *ret &= 0xffffffff;
6800
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006801 /* Checks for #GP/#SS exceptions. */
6802 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006803 if (is_long_mode(vcpu)) {
6804 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6805 * non-canonical form. This is the only check on the memory
6806 * destination for long mode!
6807 */
6808 exn = is_noncanonical_address(*ret);
6809 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006810 /* Protected mode: apply checks for segment validity in the
6811 * following order:
6812 * - segment type check (#GP(0) may be thrown)
6813 * - usability check (#GP(0)/#SS(0))
6814 * - limit check (#GP(0)/#SS(0))
6815 */
6816 if (wr)
6817 /* #GP(0) if the destination operand is located in a
6818 * read-only data segment or any code segment.
6819 */
6820 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6821 else
6822 /* #GP(0) if the source operand is located in an
6823 * execute-only code segment
6824 */
6825 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006826 if (exn) {
6827 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6828 return 1;
6829 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006830 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6831 */
6832 exn = (s.unusable != 0);
6833 /* Protected mode: #GP(0)/#SS(0) if the memory
6834 * operand is outside the segment limit.
6835 */
6836 exn = exn || (off + sizeof(u64) > s.limit);
6837 }
6838 if (exn) {
6839 kvm_queue_exception_e(vcpu,
6840 seg_reg == VCPU_SREG_SS ?
6841 SS_VECTOR : GP_VECTOR,
6842 0);
6843 return 1;
6844 }
6845
Bandan Das19677e32014-05-06 02:19:15 -04006846 return 0;
6847}
6848
6849/*
Bandan Das3573e222014-05-06 02:19:16 -04006850 * This function performs the various checks including
6851 * - if it's 4KB aligned
6852 * - No bits beyond the physical address width are set
6853 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006854 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006855 */
Bandan Das4291b582014-05-06 02:19:18 -04006856static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6857 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006858{
6859 gva_t gva;
6860 gpa_t vmptr;
6861 struct x86_exception e;
6862 struct page *page;
6863 struct vcpu_vmx *vmx = to_vmx(vcpu);
6864 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6865
6866 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006867 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006868 return 1;
6869
6870 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6871 sizeof(vmptr), &e)) {
6872 kvm_inject_page_fault(vcpu, &e);
6873 return 1;
6874 }
6875
6876 switch (exit_reason) {
6877 case EXIT_REASON_VMON:
6878 /*
6879 * SDM 3: 24.11.5
6880 * The first 4 bytes of VMXON region contain the supported
6881 * VMCS revision identifier
6882 *
6883 * Note - IA32_VMX_BASIC[48] will never be 1
6884 * for the nested case;
6885 * which replaces physical address width with 32
6886 *
6887 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006888 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006889 nested_vmx_failInvalid(vcpu);
6890 skip_emulated_instruction(vcpu);
6891 return 1;
6892 }
6893
6894 page = nested_get_page(vcpu, vmptr);
6895 if (page == NULL ||
6896 *(u32 *)kmap(page) != VMCS12_REVISION) {
6897 nested_vmx_failInvalid(vcpu);
6898 kunmap(page);
6899 skip_emulated_instruction(vcpu);
6900 return 1;
6901 }
6902 kunmap(page);
6903 vmx->nested.vmxon_ptr = vmptr;
6904 break;
Bandan Das4291b582014-05-06 02:19:18 -04006905 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006906 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006907 nested_vmx_failValid(vcpu,
6908 VMXERR_VMCLEAR_INVALID_ADDRESS);
6909 skip_emulated_instruction(vcpu);
6910 return 1;
6911 }
Bandan Das3573e222014-05-06 02:19:16 -04006912
Bandan Das4291b582014-05-06 02:19:18 -04006913 if (vmptr == vmx->nested.vmxon_ptr) {
6914 nested_vmx_failValid(vcpu,
6915 VMXERR_VMCLEAR_VMXON_POINTER);
6916 skip_emulated_instruction(vcpu);
6917 return 1;
6918 }
6919 break;
6920 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006921 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006922 nested_vmx_failValid(vcpu,
6923 VMXERR_VMPTRLD_INVALID_ADDRESS);
6924 skip_emulated_instruction(vcpu);
6925 return 1;
6926 }
6927
6928 if (vmptr == vmx->nested.vmxon_ptr) {
6929 nested_vmx_failValid(vcpu,
6930 VMXERR_VMCLEAR_VMXON_POINTER);
6931 skip_emulated_instruction(vcpu);
6932 return 1;
6933 }
6934 break;
Bandan Das3573e222014-05-06 02:19:16 -04006935 default:
6936 return 1; /* shouldn't happen */
6937 }
6938
Bandan Das4291b582014-05-06 02:19:18 -04006939 if (vmpointer)
6940 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006941 return 0;
6942}
6943
6944/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006945 * Emulate the VMXON instruction.
6946 * Currently, we just remember that VMX is active, and do not save or even
6947 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6948 * do not currently need to store anything in that guest-allocated memory
6949 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6950 * argument is different from the VMXON pointer (which the spec says they do).
6951 */
6952static int handle_vmon(struct kvm_vcpu *vcpu)
6953{
6954 struct kvm_segment cs;
6955 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03006956 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006957 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6958 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006959
6960 /* The Intel VMX Instruction Reference lists a bunch of bits that
6961 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6962 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6963 * Otherwise, we should fail with #UD. We test these now:
6964 */
6965 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6966 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6967 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6968 kvm_queue_exception(vcpu, UD_VECTOR);
6969 return 1;
6970 }
6971
6972 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6973 if (is_long_mode(vcpu) && !cs.l) {
6974 kvm_queue_exception(vcpu, UD_VECTOR);
6975 return 1;
6976 }
6977
6978 if (vmx_get_cpl(vcpu)) {
6979 kvm_inject_gp(vcpu, 0);
6980 return 1;
6981 }
Bandan Das3573e222014-05-06 02:19:16 -04006982
Bandan Das4291b582014-05-06 02:19:18 -04006983 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04006984 return 1;
6985
Abel Gordon145c28d2013-04-18 14:36:55 +03006986 if (vmx->nested.vmxon) {
6987 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6988 skip_emulated_instruction(vcpu);
6989 return 1;
6990 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006991
Haozhong Zhang3b840802016-06-22 14:59:54 +08006992 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006993 != VMXON_NEEDED_FEATURES) {
6994 kvm_inject_gp(vcpu, 0);
6995 return 1;
6996 }
6997
David Matlack4f2777b2016-07-13 17:16:37 -07006998 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
6999 if (!vmx->nested.cached_vmcs12)
7000 return -ENOMEM;
7001
Abel Gordon8de48832013-04-18 14:37:25 +03007002 if (enable_shadow_vmcs) {
7003 shadow_vmcs = alloc_vmcs();
David Matlack4f2777b2016-07-13 17:16:37 -07007004 if (!shadow_vmcs) {
7005 kfree(vmx->nested.cached_vmcs12);
Abel Gordon8de48832013-04-18 14:37:25 +03007006 return -ENOMEM;
David Matlack4f2777b2016-07-13 17:16:37 -07007007 }
Abel Gordon8de48832013-04-18 14:37:25 +03007008 /* mark vmcs as shadow */
7009 shadow_vmcs->revision_id |= (1u << 31);
7010 /* init shadow vmcs */
7011 vmcs_clear(shadow_vmcs);
7012 vmx->nested.current_shadow_vmcs = shadow_vmcs;
7013 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007014
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007015 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7016 vmx->nested.vmcs02_num = 0;
7017
Jan Kiszkaf4124502014-03-07 20:03:13 +01007018 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7019 HRTIMER_MODE_REL);
7020 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7021
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007022 vmx->nested.vmxon = true;
7023
7024 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007025 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007026 return 1;
7027}
7028
7029/*
7030 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7031 * for running VMX instructions (except VMXON, whose prerequisites are
7032 * slightly different). It also specifies what exception to inject otherwise.
7033 */
7034static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7035{
7036 struct kvm_segment cs;
7037 struct vcpu_vmx *vmx = to_vmx(vcpu);
7038
7039 if (!vmx->nested.vmxon) {
7040 kvm_queue_exception(vcpu, UD_VECTOR);
7041 return 0;
7042 }
7043
7044 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7045 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7046 (is_long_mode(vcpu) && !cs.l)) {
7047 kvm_queue_exception(vcpu, UD_VECTOR);
7048 return 0;
7049 }
7050
7051 if (vmx_get_cpl(vcpu)) {
7052 kvm_inject_gp(vcpu, 0);
7053 return 0;
7054 }
7055
7056 return 1;
7057}
7058
Abel Gordone7953d72013-04-18 14:37:55 +03007059static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7060{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007061 if (vmx->nested.current_vmptr == -1ull)
7062 return;
7063
7064 /* current_vmptr and current_vmcs12 are always set/reset together */
7065 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7066 return;
7067
Abel Gordon012f83c2013-04-18 14:39:25 +03007068 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007069 /* copy to memory all shadowed fields in case
7070 they were modified */
7071 copy_shadow_to_vmcs12(vmx);
7072 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007073 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7074 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007075 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007076 }
Wincy Van705699a2015-02-03 23:58:17 +08007077 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007078
7079 /* Flush VMCS12 to guest memory */
7080 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7081 VMCS12_SIZE);
7082
Abel Gordone7953d72013-04-18 14:37:55 +03007083 kunmap(vmx->nested.current_vmcs12_page);
7084 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007085 vmx->nested.current_vmptr = -1ull;
7086 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007087}
7088
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007089/*
7090 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7091 * just stops using VMX.
7092 */
7093static void free_nested(struct vcpu_vmx *vmx)
7094{
7095 if (!vmx->nested.vmxon)
7096 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007097
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007098 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007099 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007100 nested_release_vmcs12(vmx);
Abel Gordone7953d72013-04-18 14:37:55 +03007101 if (enable_shadow_vmcs)
7102 free_vmcs(vmx->nested.current_shadow_vmcs);
David Matlack4f2777b2016-07-13 17:16:37 -07007103 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007104 /* Unpin physical memory we referred to in current vmcs02 */
7105 if (vmx->nested.apic_access_page) {
7106 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007107 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007108 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007109 if (vmx->nested.virtual_apic_page) {
7110 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007111 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007112 }
Wincy Van705699a2015-02-03 23:58:17 +08007113 if (vmx->nested.pi_desc_page) {
7114 kunmap(vmx->nested.pi_desc_page);
7115 nested_release_page(vmx->nested.pi_desc_page);
7116 vmx->nested.pi_desc_page = NULL;
7117 vmx->nested.pi_desc = NULL;
7118 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007119
7120 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007121}
7122
7123/* Emulate the VMXOFF instruction */
7124static int handle_vmoff(struct kvm_vcpu *vcpu)
7125{
7126 if (!nested_vmx_check_permission(vcpu))
7127 return 1;
7128 free_nested(to_vmx(vcpu));
7129 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007130 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007131 return 1;
7132}
7133
Nadav Har'El27d6c862011-05-25 23:06:59 +03007134/* Emulate the VMCLEAR instruction */
7135static int handle_vmclear(struct kvm_vcpu *vcpu)
7136{
7137 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007138 gpa_t vmptr;
7139 struct vmcs12 *vmcs12;
7140 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007141
7142 if (!nested_vmx_check_permission(vcpu))
7143 return 1;
7144
Bandan Das4291b582014-05-06 02:19:18 -04007145 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007146 return 1;
7147
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007148 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007149 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007150
7151 page = nested_get_page(vcpu, vmptr);
7152 if (page == NULL) {
7153 /*
7154 * For accurate processor emulation, VMCLEAR beyond available
7155 * physical memory should do nothing at all. However, it is
7156 * possible that a nested vmx bug, not a guest hypervisor bug,
7157 * resulted in this case, so let's shut down before doing any
7158 * more damage:
7159 */
7160 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
7161 return 1;
7162 }
7163 vmcs12 = kmap(page);
7164 vmcs12->launch_state = 0;
7165 kunmap(page);
7166 nested_release_page(page);
7167
7168 nested_free_vmcs02(vmx, vmptr);
7169
7170 skip_emulated_instruction(vcpu);
7171 nested_vmx_succeed(vcpu);
7172 return 1;
7173}
7174
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007175static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7176
7177/* Emulate the VMLAUNCH instruction */
7178static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7179{
7180 return nested_vmx_run(vcpu, true);
7181}
7182
7183/* Emulate the VMRESUME instruction */
7184static int handle_vmresume(struct kvm_vcpu *vcpu)
7185{
7186
7187 return nested_vmx_run(vcpu, false);
7188}
7189
Nadav Har'El49f705c2011-05-25 23:08:30 +03007190enum vmcs_field_type {
7191 VMCS_FIELD_TYPE_U16 = 0,
7192 VMCS_FIELD_TYPE_U64 = 1,
7193 VMCS_FIELD_TYPE_U32 = 2,
7194 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7195};
7196
7197static inline int vmcs_field_type(unsigned long field)
7198{
7199 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7200 return VMCS_FIELD_TYPE_U32;
7201 return (field >> 13) & 0x3 ;
7202}
7203
7204static inline int vmcs_field_readonly(unsigned long field)
7205{
7206 return (((field >> 10) & 0x3) == 1);
7207}
7208
7209/*
7210 * Read a vmcs12 field. Since these can have varying lengths and we return
7211 * one type, we chose the biggest type (u64) and zero-extend the return value
7212 * to that size. Note that the caller, handle_vmread, might need to use only
7213 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7214 * 64-bit fields are to be returned).
7215 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007216static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7217 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007218{
7219 short offset = vmcs_field_to_offset(field);
7220 char *p;
7221
7222 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007223 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007224
7225 p = ((char *)(get_vmcs12(vcpu))) + offset;
7226
7227 switch (vmcs_field_type(field)) {
7228 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7229 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007230 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007231 case VMCS_FIELD_TYPE_U16:
7232 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007233 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007234 case VMCS_FIELD_TYPE_U32:
7235 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007236 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007237 case VMCS_FIELD_TYPE_U64:
7238 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007239 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007240 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007241 WARN_ON(1);
7242 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007243 }
7244}
7245
Abel Gordon20b97fe2013-04-18 14:36:25 +03007246
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007247static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7248 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007249 short offset = vmcs_field_to_offset(field);
7250 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7251 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007252 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007253
7254 switch (vmcs_field_type(field)) {
7255 case VMCS_FIELD_TYPE_U16:
7256 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007257 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007258 case VMCS_FIELD_TYPE_U32:
7259 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007260 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007261 case VMCS_FIELD_TYPE_U64:
7262 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007263 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007264 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7265 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007266 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007267 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007268 WARN_ON(1);
7269 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007270 }
7271
7272}
7273
Abel Gordon16f5b902013-04-18 14:38:25 +03007274static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7275{
7276 int i;
7277 unsigned long field;
7278 u64 field_value;
7279 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007280 const unsigned long *fields = shadow_read_write_fields;
7281 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007282
Jan Kiszka282da872014-10-08 18:05:39 +02007283 preempt_disable();
7284
Abel Gordon16f5b902013-04-18 14:38:25 +03007285 vmcs_load(shadow_vmcs);
7286
7287 for (i = 0; i < num_fields; i++) {
7288 field = fields[i];
7289 switch (vmcs_field_type(field)) {
7290 case VMCS_FIELD_TYPE_U16:
7291 field_value = vmcs_read16(field);
7292 break;
7293 case VMCS_FIELD_TYPE_U32:
7294 field_value = vmcs_read32(field);
7295 break;
7296 case VMCS_FIELD_TYPE_U64:
7297 field_value = vmcs_read64(field);
7298 break;
7299 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7300 field_value = vmcs_readl(field);
7301 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007302 default:
7303 WARN_ON(1);
7304 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007305 }
7306 vmcs12_write_any(&vmx->vcpu, field, field_value);
7307 }
7308
7309 vmcs_clear(shadow_vmcs);
7310 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007311
7312 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007313}
7314
Abel Gordonc3114422013-04-18 14:38:55 +03007315static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7316{
Mathias Krausec2bae892013-06-26 20:36:21 +02007317 const unsigned long *fields[] = {
7318 shadow_read_write_fields,
7319 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007320 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007321 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007322 max_shadow_read_write_fields,
7323 max_shadow_read_only_fields
7324 };
7325 int i, q;
7326 unsigned long field;
7327 u64 field_value = 0;
7328 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
7329
7330 vmcs_load(shadow_vmcs);
7331
Mathias Krausec2bae892013-06-26 20:36:21 +02007332 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007333 for (i = 0; i < max_fields[q]; i++) {
7334 field = fields[q][i];
7335 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7336
7337 switch (vmcs_field_type(field)) {
7338 case VMCS_FIELD_TYPE_U16:
7339 vmcs_write16(field, (u16)field_value);
7340 break;
7341 case VMCS_FIELD_TYPE_U32:
7342 vmcs_write32(field, (u32)field_value);
7343 break;
7344 case VMCS_FIELD_TYPE_U64:
7345 vmcs_write64(field, (u64)field_value);
7346 break;
7347 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7348 vmcs_writel(field, (long)field_value);
7349 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007350 default:
7351 WARN_ON(1);
7352 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007353 }
7354 }
7355 }
7356
7357 vmcs_clear(shadow_vmcs);
7358 vmcs_load(vmx->loaded_vmcs->vmcs);
7359}
7360
Nadav Har'El49f705c2011-05-25 23:08:30 +03007361/*
7362 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7363 * used before) all generate the same failure when it is missing.
7364 */
7365static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7366{
7367 struct vcpu_vmx *vmx = to_vmx(vcpu);
7368 if (vmx->nested.current_vmptr == -1ull) {
7369 nested_vmx_failInvalid(vcpu);
7370 skip_emulated_instruction(vcpu);
7371 return 0;
7372 }
7373 return 1;
7374}
7375
7376static int handle_vmread(struct kvm_vcpu *vcpu)
7377{
7378 unsigned long field;
7379 u64 field_value;
7380 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7381 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7382 gva_t gva = 0;
7383
7384 if (!nested_vmx_check_permission(vcpu) ||
7385 !nested_vmx_check_vmcs12(vcpu))
7386 return 1;
7387
7388 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007389 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007390 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007391 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007392 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7393 skip_emulated_instruction(vcpu);
7394 return 1;
7395 }
7396 /*
7397 * Now copy part of this value to register or memory, as requested.
7398 * Note that the number of bits actually copied is 32 or 64 depending
7399 * on the guest's mode (32 or 64 bit), not on the given field's length.
7400 */
7401 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007402 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007403 field_value);
7404 } else {
7405 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007406 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007407 return 1;
7408 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7409 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7410 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7411 }
7412
7413 nested_vmx_succeed(vcpu);
7414 skip_emulated_instruction(vcpu);
7415 return 1;
7416}
7417
7418
7419static int handle_vmwrite(struct kvm_vcpu *vcpu)
7420{
7421 unsigned long field;
7422 gva_t gva;
7423 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7424 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007425 /* The value to write might be 32 or 64 bits, depending on L1's long
7426 * mode, and eventually we need to write that into a field of several
7427 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007428 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007429 * bits into the vmcs12 field.
7430 */
7431 u64 field_value = 0;
7432 struct x86_exception e;
7433
7434 if (!nested_vmx_check_permission(vcpu) ||
7435 !nested_vmx_check_vmcs12(vcpu))
7436 return 1;
7437
7438 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007439 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007440 (((vmx_instruction_info) >> 3) & 0xf));
7441 else {
7442 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007443 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007444 return 1;
7445 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007446 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007447 kvm_inject_page_fault(vcpu, &e);
7448 return 1;
7449 }
7450 }
7451
7452
Nadav Amit27e6fb52014-06-18 17:19:26 +03007453 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007454 if (vmcs_field_readonly(field)) {
7455 nested_vmx_failValid(vcpu,
7456 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7457 skip_emulated_instruction(vcpu);
7458 return 1;
7459 }
7460
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007461 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007462 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7463 skip_emulated_instruction(vcpu);
7464 return 1;
7465 }
7466
7467 nested_vmx_succeed(vcpu);
7468 skip_emulated_instruction(vcpu);
7469 return 1;
7470}
7471
Nadav Har'El63846662011-05-25 23:07:29 +03007472/* Emulate the VMPTRLD instruction */
7473static int handle_vmptrld(struct kvm_vcpu *vcpu)
7474{
7475 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007476 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007477
7478 if (!nested_vmx_check_permission(vcpu))
7479 return 1;
7480
Bandan Das4291b582014-05-06 02:19:18 -04007481 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007482 return 1;
7483
Nadav Har'El63846662011-05-25 23:07:29 +03007484 if (vmx->nested.current_vmptr != vmptr) {
7485 struct vmcs12 *new_vmcs12;
7486 struct page *page;
7487 page = nested_get_page(vcpu, vmptr);
7488 if (page == NULL) {
7489 nested_vmx_failInvalid(vcpu);
7490 skip_emulated_instruction(vcpu);
7491 return 1;
7492 }
7493 new_vmcs12 = kmap(page);
7494 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7495 kunmap(page);
7496 nested_release_page_clean(page);
7497 nested_vmx_failValid(vcpu,
7498 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7499 skip_emulated_instruction(vcpu);
7500 return 1;
7501 }
Nadav Har'El63846662011-05-25 23:07:29 +03007502
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007503 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007504 vmx->nested.current_vmptr = vmptr;
7505 vmx->nested.current_vmcs12 = new_vmcs12;
7506 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007507 /*
7508 * Load VMCS12 from guest memory since it is not already
7509 * cached.
7510 */
7511 memcpy(vmx->nested.cached_vmcs12,
7512 vmx->nested.current_vmcs12, VMCS12_SIZE);
7513
Abel Gordon012f83c2013-04-18 14:39:25 +03007514 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007515 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7516 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007517 vmcs_write64(VMCS_LINK_POINTER,
7518 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007519 vmx->nested.sync_shadow_vmcs = true;
7520 }
Nadav Har'El63846662011-05-25 23:07:29 +03007521 }
7522
7523 nested_vmx_succeed(vcpu);
7524 skip_emulated_instruction(vcpu);
7525 return 1;
7526}
7527
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007528/* Emulate the VMPTRST instruction */
7529static int handle_vmptrst(struct kvm_vcpu *vcpu)
7530{
7531 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7532 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7533 gva_t vmcs_gva;
7534 struct x86_exception e;
7535
7536 if (!nested_vmx_check_permission(vcpu))
7537 return 1;
7538
7539 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007540 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007541 return 1;
7542 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7543 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7544 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7545 sizeof(u64), &e)) {
7546 kvm_inject_page_fault(vcpu, &e);
7547 return 1;
7548 }
7549 nested_vmx_succeed(vcpu);
7550 skip_emulated_instruction(vcpu);
7551 return 1;
7552}
7553
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007554/* Emulate the INVEPT instruction */
7555static int handle_invept(struct kvm_vcpu *vcpu)
7556{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007557 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007558 u32 vmx_instruction_info, types;
7559 unsigned long type;
7560 gva_t gva;
7561 struct x86_exception e;
7562 struct {
7563 u64 eptp, gpa;
7564 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007565
Wincy Vanb9c237b2015-02-03 23:56:30 +08007566 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7567 SECONDARY_EXEC_ENABLE_EPT) ||
7568 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007569 kvm_queue_exception(vcpu, UD_VECTOR);
7570 return 1;
7571 }
7572
7573 if (!nested_vmx_check_permission(vcpu))
7574 return 1;
7575
7576 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7577 kvm_queue_exception(vcpu, UD_VECTOR);
7578 return 1;
7579 }
7580
7581 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007582 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007583
Wincy Vanb9c237b2015-02-03 23:56:30 +08007584 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007585
7586 if (!(types & (1UL << type))) {
7587 nested_vmx_failValid(vcpu,
7588 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzini2849eb42016-03-18 16:53:29 +01007589 skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007590 return 1;
7591 }
7592
7593 /* According to the Intel VMX instruction reference, the memory
7594 * operand is read even if it isn't needed (e.g., for type==global)
7595 */
7596 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007597 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007598 return 1;
7599 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7600 sizeof(operand), &e)) {
7601 kvm_inject_page_fault(vcpu, &e);
7602 return 1;
7603 }
7604
7605 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007606 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007607 /*
7608 * TODO: track mappings and invalidate
7609 * single context requests appropriately
7610 */
7611 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007612 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007613 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007614 nested_vmx_succeed(vcpu);
7615 break;
7616 default:
7617 BUG_ON(1);
7618 break;
7619 }
7620
7621 skip_emulated_instruction(vcpu);
7622 return 1;
7623}
7624
Petr Matouseka642fc32014-09-23 20:22:30 +02007625static int handle_invvpid(struct kvm_vcpu *vcpu)
7626{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007627 struct vcpu_vmx *vmx = to_vmx(vcpu);
7628 u32 vmx_instruction_info;
7629 unsigned long type, types;
7630 gva_t gva;
7631 struct x86_exception e;
7632 int vpid;
7633
7634 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7635 SECONDARY_EXEC_ENABLE_VPID) ||
7636 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7637 kvm_queue_exception(vcpu, UD_VECTOR);
7638 return 1;
7639 }
7640
7641 if (!nested_vmx_check_permission(vcpu))
7642 return 1;
7643
7644 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7645 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7646
7647 types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7;
7648
7649 if (!(types & (1UL << type))) {
7650 nested_vmx_failValid(vcpu,
7651 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Paolo Bonzinif6870ee2016-03-18 16:53:42 +01007652 skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007653 return 1;
7654 }
7655
7656 /* according to the intel vmx instruction reference, the memory
7657 * operand is read even if it isn't needed (e.g., for type==global)
7658 */
7659 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7660 vmx_instruction_info, false, &gva))
7661 return 1;
7662 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7663 sizeof(u32), &e)) {
7664 kvm_inject_page_fault(vcpu, &e);
7665 return 1;
7666 }
7667
7668 switch (type) {
Paolo Bonzinief697a72016-03-18 16:58:38 +01007669 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
7670 /*
7671 * Old versions of KVM use the single-context version so we
7672 * have to support it; just treat it the same as all-context.
7673 */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007674 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li5c614b32015-10-13 09:18:36 -07007675 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007676 nested_vmx_succeed(vcpu);
7677 break;
7678 default:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007679 /* Trap individual address invalidation invvpid calls */
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007680 BUG_ON(1);
7681 break;
7682 }
7683
7684 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007685 return 1;
7686}
7687
Kai Huang843e4332015-01-28 10:54:28 +08007688static int handle_pml_full(struct kvm_vcpu *vcpu)
7689{
7690 unsigned long exit_qualification;
7691
7692 trace_kvm_pml_full(vcpu->vcpu_id);
7693
7694 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7695
7696 /*
7697 * PML buffer FULL happened while executing iret from NMI,
7698 * "blocked by NMI" bit has to be set before next VM entry.
7699 */
7700 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7701 cpu_has_virtual_nmis() &&
7702 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7703 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7704 GUEST_INTR_STATE_NMI);
7705
7706 /*
7707 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7708 * here.., and there's no userspace involvement needed for PML.
7709 */
7710 return 1;
7711}
7712
Yunhong Jiang64672c92016-06-13 14:19:59 -07007713static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7714{
7715 kvm_lapic_expired_hv_timer(vcpu);
7716 return 1;
7717}
7718
Nadav Har'El0140cae2011-05-25 23:06:28 +03007719/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007720 * The exit handlers return 1 if the exit was handled fully and guest execution
7721 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7722 * to be done to userspace and return 0.
7723 */
Mathias Krause772e0312012-08-30 01:30:19 +02007724static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007725 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7726 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007727 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007728 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007729 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007730 [EXIT_REASON_CR_ACCESS] = handle_cr,
7731 [EXIT_REASON_DR_ACCESS] = handle_dr,
7732 [EXIT_REASON_CPUID] = handle_cpuid,
7733 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7734 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7735 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7736 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007737 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007738 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007739 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007740 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007741 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007742 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007743 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007744 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007745 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007746 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007747 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007748 [EXIT_REASON_VMOFF] = handle_vmoff,
7749 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007750 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7751 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007752 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007753 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007754 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007755 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007756 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007757 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007758 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7759 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007760 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007761 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007762 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007763 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007764 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007765 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007766 [EXIT_REASON_XSAVES] = handle_xsaves,
7767 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007768 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007769 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007770};
7771
7772static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007773 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007774
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007775static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7776 struct vmcs12 *vmcs12)
7777{
7778 unsigned long exit_qualification;
7779 gpa_t bitmap, last_bitmap;
7780 unsigned int port;
7781 int size;
7782 u8 b;
7783
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007784 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007785 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007786
7787 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7788
7789 port = exit_qualification >> 16;
7790 size = (exit_qualification & 7) + 1;
7791
7792 last_bitmap = (gpa_t)-1;
7793 b = -1;
7794
7795 while (size > 0) {
7796 if (port < 0x8000)
7797 bitmap = vmcs12->io_bitmap_a;
7798 else if (port < 0x10000)
7799 bitmap = vmcs12->io_bitmap_b;
7800 else
Joe Perches1d804d02015-03-30 16:46:09 -07007801 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007802 bitmap += (port & 0x7fff) / 8;
7803
7804 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007805 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007806 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007807 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007808 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007809
7810 port++;
7811 size--;
7812 last_bitmap = bitmap;
7813 }
7814
Joe Perches1d804d02015-03-30 16:46:09 -07007815 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007816}
7817
Nadav Har'El644d7112011-05-25 23:12:35 +03007818/*
7819 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7820 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7821 * disinterest in the current event (read or write a specific MSR) by using an
7822 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7823 */
7824static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7825 struct vmcs12 *vmcs12, u32 exit_reason)
7826{
7827 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7828 gpa_t bitmap;
7829
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007830 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007831 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007832
7833 /*
7834 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7835 * for the four combinations of read/write and low/high MSR numbers.
7836 * First we need to figure out which of the four to use:
7837 */
7838 bitmap = vmcs12->msr_bitmap;
7839 if (exit_reason == EXIT_REASON_MSR_WRITE)
7840 bitmap += 2048;
7841 if (msr_index >= 0xc0000000) {
7842 msr_index -= 0xc0000000;
7843 bitmap += 1024;
7844 }
7845
7846 /* Then read the msr_index'th bit from this bitmap: */
7847 if (msr_index < 1024*8) {
7848 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007849 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007850 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007851 return 1 & (b >> (msr_index & 7));
7852 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007853 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007854}
7855
7856/*
7857 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7858 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7859 * intercept (via guest_host_mask etc.) the current event.
7860 */
7861static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7862 struct vmcs12 *vmcs12)
7863{
7864 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7865 int cr = exit_qualification & 15;
7866 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007867 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007868
7869 switch ((exit_qualification >> 4) & 3) {
7870 case 0: /* mov to cr */
7871 switch (cr) {
7872 case 0:
7873 if (vmcs12->cr0_guest_host_mask &
7874 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007875 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007876 break;
7877 case 3:
7878 if ((vmcs12->cr3_target_count >= 1 &&
7879 vmcs12->cr3_target_value0 == val) ||
7880 (vmcs12->cr3_target_count >= 2 &&
7881 vmcs12->cr3_target_value1 == val) ||
7882 (vmcs12->cr3_target_count >= 3 &&
7883 vmcs12->cr3_target_value2 == val) ||
7884 (vmcs12->cr3_target_count >= 4 &&
7885 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007886 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007887 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007888 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007889 break;
7890 case 4:
7891 if (vmcs12->cr4_guest_host_mask &
7892 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007893 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007894 break;
7895 case 8:
7896 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007897 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007898 break;
7899 }
7900 break;
7901 case 2: /* clts */
7902 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7903 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007904 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007905 break;
7906 case 1: /* mov from cr */
7907 switch (cr) {
7908 case 3:
7909 if (vmcs12->cpu_based_vm_exec_control &
7910 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007911 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007912 break;
7913 case 8:
7914 if (vmcs12->cpu_based_vm_exec_control &
7915 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007916 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007917 break;
7918 }
7919 break;
7920 case 3: /* lmsw */
7921 /*
7922 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7923 * cr0. Other attempted changes are ignored, with no exit.
7924 */
7925 if (vmcs12->cr0_guest_host_mask & 0xe &
7926 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007927 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007928 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7929 !(vmcs12->cr0_read_shadow & 0x1) &&
7930 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07007931 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007932 break;
7933 }
Joe Perches1d804d02015-03-30 16:46:09 -07007934 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007935}
7936
7937/*
7938 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
7939 * should handle it ourselves in L0 (and then continue L2). Only call this
7940 * when in is_guest_mode (L2).
7941 */
7942static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
7943{
Nadav Har'El644d7112011-05-25 23:12:35 +03007944 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7945 struct vcpu_vmx *vmx = to_vmx(vcpu);
7946 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01007947 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03007948
Jan Kiszka542060e2014-01-04 18:47:21 +01007949 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
7950 vmcs_readl(EXIT_QUALIFICATION),
7951 vmx->idt_vectoring_info,
7952 intr_info,
7953 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
7954 KVM_ISA_VMX);
7955
Nadav Har'El644d7112011-05-25 23:12:35 +03007956 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07007957 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007958
7959 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007960 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
7961 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07007962 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007963 }
7964
7965 switch (exit_reason) {
7966 case EXIT_REASON_EXCEPTION_NMI:
7967 if (!is_exception(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07007968 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007969 else if (is_page_fault(intr_info))
7970 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01007971 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01007972 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007973 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01007974 else if (is_debug(intr_info) &&
7975 vcpu->guest_debug &
7976 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
7977 return false;
7978 else if (is_breakpoint(intr_info) &&
7979 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
7980 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007981 return vmcs12->exception_bitmap &
7982 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
7983 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07007984 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007985 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07007986 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007987 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007988 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007989 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007990 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007991 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07007992 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007993 case EXIT_REASON_CPUID:
Marcelo Tosattibc613492014-09-18 18:24:57 -03007994 if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
Joe Perches1d804d02015-03-30 16:46:09 -07007995 return false;
7996 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007997 case EXIT_REASON_HLT:
7998 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
7999 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008000 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008001 case EXIT_REASON_INVLPG:
8002 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8003 case EXIT_REASON_RDPMC:
8004 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008005 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008006 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8007 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8008 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8009 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8010 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8011 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008012 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008013 /*
8014 * VMX instructions trap unconditionally. This allows L1 to
8015 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8016 */
Joe Perches1d804d02015-03-30 16:46:09 -07008017 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008018 case EXIT_REASON_CR_ACCESS:
8019 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8020 case EXIT_REASON_DR_ACCESS:
8021 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8022 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008023 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03008024 case EXIT_REASON_MSR_READ:
8025 case EXIT_REASON_MSR_WRITE:
8026 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8027 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008028 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008029 case EXIT_REASON_MWAIT_INSTRUCTION:
8030 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008031 case EXIT_REASON_MONITOR_TRAP_FLAG:
8032 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008033 case EXIT_REASON_MONITOR_INSTRUCTION:
8034 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8035 case EXIT_REASON_PAUSE_INSTRUCTION:
8036 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8037 nested_cpu_has2(vmcs12,
8038 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8039 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008040 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008041 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008042 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008043 case EXIT_REASON_APIC_ACCESS:
8044 return nested_cpu_has2(vmcs12,
8045 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008046 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008047 case EXIT_REASON_EOI_INDUCED:
8048 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008049 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008050 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008051 /*
8052 * L0 always deals with the EPT violation. If nested EPT is
8053 * used, and the nested mmu code discovers that the address is
8054 * missing in the guest EPT table (EPT12), the EPT violation
8055 * will be injected with nested_ept_inject_page_fault()
8056 */
Joe Perches1d804d02015-03-30 16:46:09 -07008057 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008058 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008059 /*
8060 * L2 never uses directly L1's EPT, but rather L0's own EPT
8061 * table (shadow on EPT) or a merged EPT table that L0 built
8062 * (EPT on EPT). So any problems with the structure of the
8063 * table is L0's fault.
8064 */
Joe Perches1d804d02015-03-30 16:46:09 -07008065 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008066 case EXIT_REASON_WBINVD:
8067 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8068 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008069 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008070 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8071 /*
8072 * This should never happen, since it is not possible to
8073 * set XSS to a non-zero value---neither in L1 nor in L2.
8074 * If if it were, XSS would have to be checked against
8075 * the XSS exit bitmap in vmcs12.
8076 */
8077 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008078 case EXIT_REASON_PREEMPTION_TIMER:
8079 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008080 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008081 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008082 }
8083}
8084
Avi Kivity586f9602010-11-18 13:09:54 +02008085static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8086{
8087 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8088 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8089}
8090
Kai Huanga3eaa862015-11-04 13:46:05 +08008091static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008092{
Kai Huanga3eaa862015-11-04 13:46:05 +08008093 if (vmx->pml_pg) {
8094 __free_page(vmx->pml_pg);
8095 vmx->pml_pg = NULL;
8096 }
Kai Huang843e4332015-01-28 10:54:28 +08008097}
8098
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008099static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008100{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008101 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008102 u64 *pml_buf;
8103 u16 pml_idx;
8104
8105 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8106
8107 /* Do nothing if PML buffer is empty */
8108 if (pml_idx == (PML_ENTITY_NUM - 1))
8109 return;
8110
8111 /* PML index always points to next available PML buffer entity */
8112 if (pml_idx >= PML_ENTITY_NUM)
8113 pml_idx = 0;
8114 else
8115 pml_idx++;
8116
8117 pml_buf = page_address(vmx->pml_pg);
8118 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8119 u64 gpa;
8120
8121 gpa = pml_buf[pml_idx];
8122 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008123 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008124 }
8125
8126 /* reset PML index */
8127 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8128}
8129
8130/*
8131 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8132 * Called before reporting dirty_bitmap to userspace.
8133 */
8134static void kvm_flush_pml_buffers(struct kvm *kvm)
8135{
8136 int i;
8137 struct kvm_vcpu *vcpu;
8138 /*
8139 * We only need to kick vcpu out of guest mode here, as PML buffer
8140 * is flushed at beginning of all VMEXITs, and it's obvious that only
8141 * vcpus running in guest are possible to have unflushed GPAs in PML
8142 * buffer.
8143 */
8144 kvm_for_each_vcpu(i, vcpu, kvm)
8145 kvm_vcpu_kick(vcpu);
8146}
8147
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008148static void vmx_dump_sel(char *name, uint32_t sel)
8149{
8150 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
8151 name, vmcs_read32(sel),
8152 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8153 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8154 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8155}
8156
8157static void vmx_dump_dtsel(char *name, uint32_t limit)
8158{
8159 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8160 name, vmcs_read32(limit),
8161 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8162}
8163
8164static void dump_vmcs(void)
8165{
8166 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8167 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8168 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8169 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8170 u32 secondary_exec_control = 0;
8171 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008172 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008173 int i, n;
8174
8175 if (cpu_has_secondary_exec_ctrls())
8176 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8177
8178 pr_err("*** Guest State ***\n");
8179 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8180 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8181 vmcs_readl(CR0_GUEST_HOST_MASK));
8182 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8183 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8184 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8185 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8186 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8187 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008188 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8189 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8190 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8191 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008192 }
8193 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8194 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8195 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8196 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8197 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8198 vmcs_readl(GUEST_SYSENTER_ESP),
8199 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8200 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8201 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8202 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8203 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8204 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8205 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8206 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8207 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8208 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8209 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8210 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8211 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008212 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8213 efer, vmcs_read64(GUEST_IA32_PAT));
8214 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8215 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008216 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8217 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008218 pr_err("PerfGlobCtl = 0x%016llx\n",
8219 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008220 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008221 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008222 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8223 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8224 vmcs_read32(GUEST_ACTIVITY_STATE));
8225 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8226 pr_err("InterruptStatus = %04x\n",
8227 vmcs_read16(GUEST_INTR_STATUS));
8228
8229 pr_err("*** Host State ***\n");
8230 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8231 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8232 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8233 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8234 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8235 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8236 vmcs_read16(HOST_TR_SELECTOR));
8237 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8238 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8239 vmcs_readl(HOST_TR_BASE));
8240 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8241 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8242 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8243 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8244 vmcs_readl(HOST_CR4));
8245 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8246 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8247 vmcs_read32(HOST_IA32_SYSENTER_CS),
8248 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8249 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008250 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8251 vmcs_read64(HOST_IA32_EFER),
8252 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008253 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008254 pr_err("PerfGlobCtl = 0x%016llx\n",
8255 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008256
8257 pr_err("*** Control State ***\n");
8258 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8259 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8260 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8261 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8262 vmcs_read32(EXCEPTION_BITMAP),
8263 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8264 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8265 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8266 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8267 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8268 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8269 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8270 vmcs_read32(VM_EXIT_INTR_INFO),
8271 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8272 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8273 pr_err(" reason=%08x qualification=%016lx\n",
8274 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8275 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8276 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8277 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008278 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008279 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008280 pr_err("TSC Multiplier = 0x%016llx\n",
8281 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008282 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8283 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8284 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8285 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8286 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008287 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008288 n = vmcs_read32(CR3_TARGET_COUNT);
8289 for (i = 0; i + 1 < n; i += 4)
8290 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8291 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8292 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8293 if (i < n)
8294 pr_err("CR3 target%u=%016lx\n",
8295 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8296 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8297 pr_err("PLE Gap=%08x Window=%08x\n",
8298 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8299 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8300 pr_err("Virtual processor ID = 0x%04x\n",
8301 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8302}
8303
Avi Kivity6aa8b732006-12-10 02:21:36 -08008304/*
8305 * The guest has exited. See if we can fix it or if we need userspace
8306 * assistance.
8307 */
Avi Kivity851ba692009-08-24 11:10:17 +03008308static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008309{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008310 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008311 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008312 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008313
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008314 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
8315
Kai Huang843e4332015-01-28 10:54:28 +08008316 /*
8317 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8318 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8319 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8320 * mode as if vcpus is in root mode, the PML buffer must has been
8321 * flushed already.
8322 */
8323 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008324 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008325
Mohammed Gamal80ced182009-09-01 12:48:18 +02008326 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008327 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008328 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008329
Nadav Har'El644d7112011-05-25 23:12:35 +03008330 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008331 nested_vmx_vmexit(vcpu, exit_reason,
8332 vmcs_read32(VM_EXIT_INTR_INFO),
8333 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008334 return 1;
8335 }
8336
Mohammed Gamal51207022010-05-31 22:40:54 +03008337 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008338 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008339 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8340 vcpu->run->fail_entry.hardware_entry_failure_reason
8341 = exit_reason;
8342 return 0;
8343 }
8344
Avi Kivity29bd8a72007-09-10 17:27:03 +03008345 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008346 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8347 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008348 = vmcs_read32(VM_INSTRUCTION_ERROR);
8349 return 0;
8350 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008351
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008352 /*
8353 * Note:
8354 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8355 * delivery event since it indicates guest is accessing MMIO.
8356 * The vm-exit can be triggered again after return to guest that
8357 * will cause infinite loop.
8358 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008359 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008360 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008361 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008362 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008363 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8364 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8365 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8366 vcpu->run->internal.ndata = 2;
8367 vcpu->run->internal.data[0] = vectoring_info;
8368 vcpu->run->internal.data[1] = exit_reason;
8369 return 0;
8370 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008371
Nadav Har'El644d7112011-05-25 23:12:35 +03008372 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8373 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008374 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008375 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008376 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008377 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008378 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008379 /*
8380 * This CPU don't support us in finding the end of an
8381 * NMI-blocked window if the guest runs with IRQs
8382 * disabled. So we pull the trigger after 1 s of
8383 * futile waiting, but inform the user about this.
8384 */
8385 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8386 "state on VCPU %d after 1 s timeout\n",
8387 __func__, vcpu->vcpu_id);
8388 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008389 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008390 }
8391
Avi Kivity6aa8b732006-12-10 02:21:36 -08008392 if (exit_reason < kvm_vmx_max_exit_handlers
8393 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008394 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008395 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008396 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8397 kvm_queue_exception(vcpu, UD_VECTOR);
8398 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008399 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008400}
8401
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008402static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008403{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008404 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8405
8406 if (is_guest_mode(vcpu) &&
8407 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8408 return;
8409
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008410 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008411 vmcs_write32(TPR_THRESHOLD, 0);
8412 return;
8413 }
8414
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008415 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008416}
8417
Yang Zhang8d146952013-01-25 10:18:50 +08008418static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8419{
8420 u32 sec_exec_control;
8421
8422 /*
8423 * There is not point to enable virtualize x2apic without enable
8424 * apicv
8425 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08008426 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
Andrey Smetanind62caab2015-11-10 15:36:33 +03008427 !kvm_vcpu_apicv_active(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008428 return;
8429
Paolo Bonzini35754c92015-07-29 12:05:37 +02008430 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008431 return;
8432
8433 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8434
8435 if (set) {
8436 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8437 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8438 } else {
8439 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8440 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8441 }
8442 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8443
8444 vmx_set_msr_bitmap(vcpu);
8445}
8446
Tang Chen38b99172014-09-24 15:57:54 +08008447static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8448{
8449 struct vcpu_vmx *vmx = to_vmx(vcpu);
8450
8451 /*
8452 * Currently we do not handle the nested case where L2 has an
8453 * APIC access page of its own; that page is still pinned.
8454 * Hence, we skip the case where the VCPU is in guest mode _and_
8455 * L1 prepared an APIC access page for L2.
8456 *
8457 * For the case where L1 and L2 share the same APIC access page
8458 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8459 * in the vmcs12), this function will only update either the vmcs01
8460 * or the vmcs02. If the former, the vmcs02 will be updated by
8461 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8462 * the next L2->L1 exit.
8463 */
8464 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008465 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Tang Chen38b99172014-09-24 15:57:54 +08008466 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
8467 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8468}
8469
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008470static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008471{
8472 u16 status;
8473 u8 old;
8474
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008475 if (max_isr == -1)
8476 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008477
8478 status = vmcs_read16(GUEST_INTR_STATUS);
8479 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008480 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008481 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008482 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008483 vmcs_write16(GUEST_INTR_STATUS, status);
8484 }
8485}
8486
8487static void vmx_set_rvi(int vector)
8488{
8489 u16 status;
8490 u8 old;
8491
Wei Wang4114c272014-11-05 10:53:43 +08008492 if (vector == -1)
8493 vector = 0;
8494
Yang Zhangc7c9c562013-01-25 10:18:51 +08008495 status = vmcs_read16(GUEST_INTR_STATUS);
8496 old = (u8)status & 0xff;
8497 if ((u8)vector != old) {
8498 status &= ~0xff;
8499 status |= (u8)vector;
8500 vmcs_write16(GUEST_INTR_STATUS, status);
8501 }
8502}
8503
8504static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8505{
Wanpeng Li963fee12014-07-17 19:03:00 +08008506 if (!is_guest_mode(vcpu)) {
8507 vmx_set_rvi(max_irr);
8508 return;
8509 }
8510
Wei Wang4114c272014-11-05 10:53:43 +08008511 if (max_irr == -1)
8512 return;
8513
Wanpeng Li963fee12014-07-17 19:03:00 +08008514 /*
Wei Wang4114c272014-11-05 10:53:43 +08008515 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8516 * handles it.
8517 */
8518 if (nested_exit_on_intr(vcpu))
8519 return;
8520
8521 /*
8522 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008523 * is run without virtual interrupt delivery.
8524 */
8525 if (!kvm_event_needs_reinjection(vcpu) &&
8526 vmx_interrupt_allowed(vcpu)) {
8527 kvm_queue_interrupt(vcpu, max_irr, false);
8528 vmx_inject_irq(vcpu);
8529 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008530}
8531
Andrey Smetanin63086302015-11-10 15:36:32 +03008532static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008533{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008534 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008535 return;
8536
Yang Zhangc7c9c562013-01-25 10:18:51 +08008537 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8538 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8539 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8540 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8541}
8542
Avi Kivity51aa01d2010-07-20 14:31:20 +03008543static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008544{
Avi Kivity00eba012011-03-07 17:24:54 +02008545 u32 exit_intr_info;
8546
8547 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8548 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8549 return;
8550
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008551 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008552 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008553
8554 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008555 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008556 kvm_machine_check();
8557
Gleb Natapov20f65982009-05-11 13:35:55 +03008558 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008559 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008560 (exit_intr_info & INTR_INFO_VALID_MASK)) {
8561 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008562 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008563 kvm_after_handle_nmi(&vmx->vcpu);
8564 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008565}
Gleb Natapov20f65982009-05-11 13:35:55 +03008566
Yang Zhanga547c6d2013-04-11 19:25:10 +08008567static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8568{
8569 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008570 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008571
8572 /*
8573 * If external interrupt exists, IF bit is set in rflags/eflags on the
8574 * interrupt stack frame, and interrupt will be enabled on a return
8575 * from interrupt handler.
8576 */
8577 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8578 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8579 unsigned int vector;
8580 unsigned long entry;
8581 gate_desc *desc;
8582 struct vcpu_vmx *vmx = to_vmx(vcpu);
8583#ifdef CONFIG_X86_64
8584 unsigned long tmp;
8585#endif
8586
8587 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8588 desc = (gate_desc *)vmx->host_idt_base + vector;
8589 entry = gate_offset(*desc);
8590 asm volatile(
8591#ifdef CONFIG_X86_64
8592 "mov %%" _ASM_SP ", %[sp]\n\t"
8593 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8594 "push $%c[ss]\n\t"
8595 "push %[sp]\n\t"
8596#endif
8597 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008598 __ASM_SIZE(push) " $%c[cs]\n\t"
8599 "call *%[entry]\n\t"
8600 :
8601#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008602 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008603#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008604 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008605 :
8606 [entry]"r"(entry),
8607 [ss]"i"(__KERNEL_DS),
8608 [cs]"i"(__KERNEL_CS)
8609 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008610 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008611}
8612
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008613static bool vmx_has_high_real_mode_segbase(void)
8614{
8615 return enable_unrestricted_guest || emulate_invalid_guest_state;
8616}
8617
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008618static bool vmx_mpx_supported(void)
8619{
8620 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8621 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8622}
8623
Wanpeng Li55412b22014-12-02 19:21:30 +08008624static bool vmx_xsaves_supported(void)
8625{
8626 return vmcs_config.cpu_based_2nd_exec_ctrl &
8627 SECONDARY_EXEC_XSAVES;
8628}
8629
Avi Kivity51aa01d2010-07-20 14:31:20 +03008630static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8631{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008632 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008633 bool unblock_nmi;
8634 u8 vector;
8635 bool idtv_info_valid;
8636
8637 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008638
Avi Kivitycf393f72008-07-01 16:20:21 +03008639 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008640 if (vmx->nmi_known_unmasked)
8641 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008642 /*
8643 * Can't use vmx->exit_intr_info since we're not sure what
8644 * the exit reason is.
8645 */
8646 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008647 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8648 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8649 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008650 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008651 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8652 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008653 * SDM 3: 23.2.2 (September 2008)
8654 * Bit 12 is undefined in any of the following cases:
8655 * If the VM exit sets the valid bit in the IDT-vectoring
8656 * information field.
8657 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008658 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008659 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8660 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008661 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8662 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008663 else
8664 vmx->nmi_known_unmasked =
8665 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8666 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008667 } else if (unlikely(vmx->soft_vnmi_blocked))
8668 vmx->vnmi_blocked_time +=
8669 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008670}
8671
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008672static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008673 u32 idt_vectoring_info,
8674 int instr_len_field,
8675 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008676{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008677 u8 vector;
8678 int type;
8679 bool idtv_info_valid;
8680
8681 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008682
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008683 vcpu->arch.nmi_injected = false;
8684 kvm_clear_exception_queue(vcpu);
8685 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008686
8687 if (!idtv_info_valid)
8688 return;
8689
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008690 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008691
Avi Kivity668f6122008-07-02 09:28:55 +03008692 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8693 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008694
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008695 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008696 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008697 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008698 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008699 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008700 * Clear bit "block by NMI" before VM entry if a NMI
8701 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008702 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008703 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008704 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008705 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008706 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008707 /* fall through */
8708 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008709 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008710 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008711 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008712 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008713 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008714 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008715 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008716 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008717 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008718 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008719 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008720 break;
8721 default:
8722 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008723 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008724}
8725
Avi Kivity83422e12010-07-20 14:43:23 +03008726static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8727{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008728 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008729 VM_EXIT_INSTRUCTION_LEN,
8730 IDT_VECTORING_ERROR_CODE);
8731}
8732
Avi Kivityb463a6f2010-07-20 15:06:17 +03008733static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8734{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008735 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008736 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8737 VM_ENTRY_INSTRUCTION_LEN,
8738 VM_ENTRY_EXCEPTION_ERROR_CODE);
8739
8740 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8741}
8742
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008743static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8744{
8745 int i, nr_msrs;
8746 struct perf_guest_switch_msr *msrs;
8747
8748 msrs = perf_guest_get_msrs(&nr_msrs);
8749
8750 if (!msrs)
8751 return;
8752
8753 for (i = 0; i < nr_msrs; i++)
8754 if (msrs[i].host == msrs[i].guest)
8755 clear_atomic_switch_msr(vmx, msrs[i].msr);
8756 else
8757 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8758 msrs[i].host);
8759}
8760
Yunhong Jiang64672c92016-06-13 14:19:59 -07008761void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
8762{
8763 struct vcpu_vmx *vmx = to_vmx(vcpu);
8764 u64 tscl;
8765 u32 delta_tsc;
8766
8767 if (vmx->hv_deadline_tsc == -1)
8768 return;
8769
8770 tscl = rdtsc();
8771 if (vmx->hv_deadline_tsc > tscl)
8772 /* sure to be 32 bit only because checked on set_hv_timer */
8773 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8774 cpu_preemption_timer_multi);
8775 else
8776 delta_tsc = 0;
8777
8778 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8779}
8780
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008781static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008782{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008783 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008784 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008785
8786 /* Record the guest's net vcpu time for enforced NMI injections. */
8787 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8788 vmx->entry_time = ktime_get();
8789
8790 /* Don't enter VMX if guest state is invalid, let the exit handler
8791 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008792 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008793 return;
8794
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008795 if (vmx->ple_window_dirty) {
8796 vmx->ple_window_dirty = false;
8797 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8798 }
8799
Abel Gordon012f83c2013-04-18 14:39:25 +03008800 if (vmx->nested.sync_shadow_vmcs) {
8801 copy_vmcs12_to_shadow(vmx);
8802 vmx->nested.sync_shadow_vmcs = false;
8803 }
8804
Avi Kivity104f2262010-11-18 13:12:52 +02008805 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8806 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8807 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8808 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8809
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008810 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008811 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8812 vmcs_writel(HOST_CR4, cr4);
8813 vmx->host_state.vmcs_host_cr4 = cr4;
8814 }
8815
Avi Kivity104f2262010-11-18 13:12:52 +02008816 /* When single-stepping over STI and MOV SS, we must clear the
8817 * corresponding interruptibility bits in the guest state. Otherwise
8818 * vmentry fails as it then expects bit 14 (BS) in pending debug
8819 * exceptions being set, but that's not correct for the guest debugging
8820 * case. */
8821 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8822 vmx_set_interrupt_shadow(vcpu, 0);
8823
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008824 if (vmx->guest_pkru_valid)
8825 __write_pkru(vmx->guest_pkru);
8826
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008827 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008828 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008829
Yunhong Jiang64672c92016-06-13 14:19:59 -07008830 vmx_arm_hv_timer(vcpu);
8831
Nadav Har'Eld462b812011-05-24 15:26:10 +03008832 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008833 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008834 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008835 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8836 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8837 "push %%" _ASM_CX " \n\t"
8838 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03008839 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008840 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008841 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03008842 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008843 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008844 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8845 "mov %%cr2, %%" _ASM_DX " \n\t"
8846 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008847 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008848 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008849 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008850 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008851 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008852 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008853 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8854 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8855 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8856 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8857 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8858 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008859#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008860 "mov %c[r8](%0), %%r8 \n\t"
8861 "mov %c[r9](%0), %%r9 \n\t"
8862 "mov %c[r10](%0), %%r10 \n\t"
8863 "mov %c[r11](%0), %%r11 \n\t"
8864 "mov %c[r12](%0), %%r12 \n\t"
8865 "mov %c[r13](%0), %%r13 \n\t"
8866 "mov %c[r14](%0), %%r14 \n\t"
8867 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008868#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008869 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008870
Avi Kivity6aa8b732006-12-10 02:21:36 -08008871 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008872 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008873 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008874 "jmp 2f \n\t"
8875 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8876 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008877 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008878 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008879 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008880 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8881 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8882 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8883 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8884 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8885 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8886 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008887#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008888 "mov %%r8, %c[r8](%0) \n\t"
8889 "mov %%r9, %c[r9](%0) \n\t"
8890 "mov %%r10, %c[r10](%0) \n\t"
8891 "mov %%r11, %c[r11](%0) \n\t"
8892 "mov %%r12, %c[r12](%0) \n\t"
8893 "mov %%r13, %c[r13](%0) \n\t"
8894 "mov %%r14, %c[r14](%0) \n\t"
8895 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008896#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008897 "mov %%cr2, %%" _ASM_AX " \n\t"
8898 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008899
Avi Kivityb188c81f2012-09-16 15:10:58 +03008900 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008901 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008902 ".pushsection .rodata \n\t"
8903 ".global vmx_return \n\t"
8904 "vmx_return: " _ASM_PTR " 2b \n\t"
8905 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008906 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008907 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008908 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03008909 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008910 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8911 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8912 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8913 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8914 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8915 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8916 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008917#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008918 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8919 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8920 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8921 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8922 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8923 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8924 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8925 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008926#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008927 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8928 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02008929 : "cc", "memory"
8930#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03008931 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008932 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008933#else
8934 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008935#endif
8936 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08008937
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008938 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
8939 if (debugctlmsr)
8940 update_debugctlmsr(debugctlmsr);
8941
Avi Kivityaa67f602012-08-01 16:48:03 +03008942#ifndef CONFIG_X86_64
8943 /*
8944 * The sysexit path does not restore ds/es, so we must set them to
8945 * a reasonable value ourselves.
8946 *
8947 * We can't defer this to vmx_load_host_state() since that function
8948 * may be executed in interrupt context, which saves and restore segments
8949 * around it, nullifying its effect.
8950 */
8951 loadsegment(ds, __USER_DS);
8952 loadsegment(es, __USER_DS);
8953#endif
8954
Avi Kivity6de4f3a2009-05-31 22:58:47 +03008955 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02008956 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008957 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03008958 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008959 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008960 vcpu->arch.regs_dirty = 0;
8961
Avi Kivity1155f762007-11-22 11:30:47 +02008962 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
8963
Nadav Har'Eld462b812011-05-24 15:26:10 +03008964 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02008965
Avi Kivity51aa01d2010-07-20 14:31:20 +03008966 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008967
Gleb Natapove0b890d2013-09-25 12:51:33 +03008968 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008969 * eager fpu is enabled if PKEY is supported and CR4 is switched
8970 * back on host, so it is safe to read guest PKRU from current
8971 * XSAVE.
8972 */
8973 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
8974 vmx->guest_pkru = __read_pkru();
8975 if (vmx->guest_pkru != vmx->host_pkru) {
8976 vmx->guest_pkru_valid = true;
8977 __write_pkru(vmx->host_pkru);
8978 } else
8979 vmx->guest_pkru_valid = false;
8980 }
8981
8982 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03008983 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
8984 * we did not inject a still-pending event to L1 now because of
8985 * nested_run_pending, we need to re-enable this bit.
8986 */
8987 if (vmx->nested.nested_run_pending)
8988 kvm_make_request(KVM_REQ_EVENT, vcpu);
8989
8990 vmx->nested.nested_run_pending = 0;
8991
Avi Kivity51aa01d2010-07-20 14:31:20 +03008992 vmx_complete_atomic_exit(vmx);
8993 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03008994 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008995}
8996
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008997static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
8998{
8999 struct vcpu_vmx *vmx = to_vmx(vcpu);
9000 int cpu;
9001
9002 if (vmx->loaded_vmcs == &vmx->vmcs01)
9003 return;
9004
9005 cpu = get_cpu();
9006 vmx->loaded_vmcs = &vmx->vmcs01;
9007 vmx_vcpu_put(vcpu);
9008 vmx_vcpu_load(vcpu, cpu);
9009 vcpu->cpu = cpu;
9010 put_cpu();
9011}
9012
Jim Mattson2f1fe812016-07-08 15:36:06 -07009013/*
9014 * Ensure that the current vmcs of the logical processor is the
9015 * vmcs01 of the vcpu before calling free_nested().
9016 */
9017static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9018{
9019 struct vcpu_vmx *vmx = to_vmx(vcpu);
9020 int r;
9021
9022 r = vcpu_load(vcpu);
9023 BUG_ON(r);
9024 vmx_load_vmcs01(vcpu);
9025 free_nested(vmx);
9026 vcpu_put(vcpu);
9027}
9028
Avi Kivity6aa8b732006-12-10 02:21:36 -08009029static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9030{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009031 struct vcpu_vmx *vmx = to_vmx(vcpu);
9032
Kai Huang843e4332015-01-28 10:54:28 +08009033 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009034 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009035 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009036 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009037 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009038 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009039 kfree(vmx->guest_msrs);
9040 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009041 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009042}
9043
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009044static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009045{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009046 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009047 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009048 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009049
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009050 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009051 return ERR_PTR(-ENOMEM);
9052
Wanpeng Li991e7a02015-09-16 17:30:05 +08009053 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009054
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009055 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9056 if (err)
9057 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009058
Peter Feiner4e595162016-07-07 14:49:58 -07009059 err = -ENOMEM;
9060
9061 /*
9062 * If PML is turned on, failure on enabling PML just results in failure
9063 * of creating the vcpu, therefore we can simplify PML logic (by
9064 * avoiding dealing with cases, such as enabling PML partially on vcpus
9065 * for the guest, etc.
9066 */
9067 if (enable_pml) {
9068 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9069 if (!vmx->pml_pg)
9070 goto uninit_vcpu;
9071 }
9072
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009073 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009074 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9075 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009076
Peter Feiner4e595162016-07-07 14:49:58 -07009077 if (!vmx->guest_msrs)
9078 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009079
Nadav Har'Eld462b812011-05-24 15:26:10 +03009080 vmx->loaded_vmcs = &vmx->vmcs01;
9081 vmx->loaded_vmcs->vmcs = alloc_vmcs();
9082 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009083 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009084 if (!vmm_exclusive)
9085 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
9086 loaded_vmcs_init(vmx->loaded_vmcs);
9087 if (!vmm_exclusive)
9088 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009089
Avi Kivity15ad7142007-07-11 18:17:21 +03009090 cpu = get_cpu();
9091 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009092 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009093 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009094 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009095 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009096 if (err)
9097 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009098 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009099 err = alloc_apic_access_page(kvm);
9100 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009101 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009102 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009103
Sheng Yangb927a3c2009-07-21 10:42:48 +08009104 if (enable_ept) {
9105 if (!kvm->arch.ept_identity_map_addr)
9106 kvm->arch.ept_identity_map_addr =
9107 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009108 err = init_rmode_identity_map(kvm);
9109 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009110 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009111 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009112
Wanpeng Li5c614b32015-10-13 09:18:36 -07009113 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009114 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009115 vmx->nested.vpid02 = allocate_vpid();
9116 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009117
Wincy Van705699a2015-02-03 23:58:17 +08009118 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009119 vmx->nested.current_vmptr = -1ull;
9120 vmx->nested.current_vmcs12 = NULL;
9121
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009122 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9123
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009124 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009125
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009126free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009127 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009128 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009129free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009130 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009131free_pml:
9132 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009133uninit_vcpu:
9134 kvm_vcpu_uninit(&vmx->vcpu);
9135free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009136 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009137 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009138 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009139}
9140
Yang, Sheng002c7f72007-07-31 14:23:01 +03009141static void __init vmx_check_processor_compat(void *rtn)
9142{
9143 struct vmcs_config vmcs_conf;
9144
9145 *(int *)rtn = 0;
9146 if (setup_vmcs_config(&vmcs_conf) < 0)
9147 *(int *)rtn = -EIO;
9148 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9149 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9150 smp_processor_id());
9151 *(int *)rtn = -EIO;
9152 }
9153}
9154
Sheng Yang67253af2008-04-25 10:20:22 +08009155static int get_ept_level(void)
9156{
9157 return VMX_EPT_DEFAULT_GAW + 1;
9158}
9159
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009160static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009161{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009162 u8 cache;
9163 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009164
Sheng Yang522c68c2009-04-27 20:35:43 +08009165 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009166 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009167 * 2. EPT with VT-d:
9168 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009169 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009170 * b. VT-d with snooping control feature: snooping control feature of
9171 * VT-d engine can guarantee the cache correctness. Just set it
9172 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009173 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009174 * consistent with host MTRR
9175 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009176 if (is_mmio) {
9177 cache = MTRR_TYPE_UNCACHABLE;
9178 goto exit;
9179 }
9180
9181 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009182 ipat = VMX_EPT_IPAT_BIT;
9183 cache = MTRR_TYPE_WRBACK;
9184 goto exit;
9185 }
9186
9187 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9188 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009189 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009190 cache = MTRR_TYPE_WRBACK;
9191 else
9192 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009193 goto exit;
9194 }
9195
Xiao Guangrongff536042015-06-15 16:55:22 +08009196 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009197
9198exit:
9199 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009200}
9201
Sheng Yang17cc3932010-01-05 19:02:27 +08009202static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009203{
Sheng Yang878403b2010-01-05 19:02:29 +08009204 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9205 return PT_DIRECTORY_LEVEL;
9206 else
9207 /* For shadow and EPT supported 1GB page */
9208 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009209}
9210
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009211static void vmcs_set_secondary_exec_control(u32 new_ctl)
9212{
9213 /*
9214 * These bits in the secondary execution controls field
9215 * are dynamic, the others are mostly based on the hypervisor
9216 * architecture and the guest's CPUID. Do not touch the
9217 * dynamic bits.
9218 */
9219 u32 mask =
9220 SECONDARY_EXEC_SHADOW_VMCS |
9221 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9222 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9223
9224 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9225
9226 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9227 (new_ctl & ~mask) | (cur_ctl & mask));
9228}
9229
Sheng Yang0e851882009-12-18 16:48:46 +08009230static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9231{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009232 struct kvm_cpuid_entry2 *best;
9233 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009234 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009235
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009236 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009237 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9238 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009239 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009240
Paolo Bonzini8b972652015-09-15 17:34:42 +02009241 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009242 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009243 vmx->nested.nested_vmx_secondary_ctls_high |=
9244 SECONDARY_EXEC_RDTSCP;
9245 else
9246 vmx->nested.nested_vmx_secondary_ctls_high &=
9247 ~SECONDARY_EXEC_RDTSCP;
9248 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009249 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009250
Mao, Junjiead756a12012-07-02 01:18:48 +00009251 /* Exposing INVPCID only when PCID is exposed */
9252 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9253 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009254 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9255 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009256 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009257
Mao, Junjiead756a12012-07-02 01:18:48 +00009258 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009259 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009260 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009261
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009262 if (cpu_has_secondary_exec_ctrls())
9263 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009264
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009265 if (nested_vmx_allowed(vcpu))
9266 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9267 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9268 else
9269 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9270 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Sheng Yang0e851882009-12-18 16:48:46 +08009271}
9272
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009273static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9274{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009275 if (func == 1 && nested)
9276 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009277}
9278
Yang Zhang25d92082013-08-06 12:00:32 +03009279static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9280 struct x86_exception *fault)
9281{
Jan Kiszka533558b2014-01-04 18:47:20 +01009282 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9283 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009284
9285 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009286 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009287 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009288 exit_reason = EXIT_REASON_EPT_VIOLATION;
9289 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009290 vmcs12->guest_physical_address = fault->address;
9291}
9292
Nadav Har'El155a97a2013-08-05 11:07:16 +03009293/* Callbacks for nested_ept_init_mmu_context: */
9294
9295static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9296{
9297 /* return the page table to be shadowed - in our case, EPT12 */
9298 return get_vmcs12(vcpu)->ept_pointer;
9299}
9300
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02009301static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009302{
Paolo Bonziniad896af2013-10-02 16:56:14 +02009303 WARN_ON(mmu_is_nested(vcpu));
9304 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009305 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
9306 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009307 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9308 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9309 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9310
9311 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009312}
9313
9314static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9315{
9316 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9317}
9318
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009319static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9320 u16 error_code)
9321{
9322 bool inequality, bit;
9323
9324 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9325 inequality =
9326 (error_code & vmcs12->page_fault_error_code_mask) !=
9327 vmcs12->page_fault_error_code_match;
9328 return inequality ^ bit;
9329}
9330
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009331static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9332 struct x86_exception *fault)
9333{
9334 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9335
9336 WARN_ON(!is_guest_mode(vcpu));
9337
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009338 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009339 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9340 vmcs_read32(VM_EXIT_INTR_INFO),
9341 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009342 else
9343 kvm_inject_page_fault(vcpu, fault);
9344}
9345
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009346static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9347 struct vmcs12 *vmcs12)
9348{
9349 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009350 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009351
9352 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009353 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9354 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009355 return false;
9356
9357 /*
9358 * Translate L1 physical address to host physical
9359 * address for vmcs02. Keep the page pinned, so this
9360 * physical address remains valid. We keep a reference
9361 * to it so we can release it later.
9362 */
9363 if (vmx->nested.apic_access_page) /* shouldn't happen */
9364 nested_release_page(vmx->nested.apic_access_page);
9365 vmx->nested.apic_access_page =
9366 nested_get_page(vcpu, vmcs12->apic_access_addr);
9367 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009368
9369 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009370 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9371 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009372 return false;
9373
9374 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9375 nested_release_page(vmx->nested.virtual_apic_page);
9376 vmx->nested.virtual_apic_page =
9377 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9378
9379 /*
9380 * Failing the vm entry is _not_ what the processor does
9381 * but it's basically the only possibility we have.
9382 * We could still enter the guest if CR8 load exits are
9383 * enabled, CR8 store exits are enabled, and virtualize APIC
9384 * access is disabled; in this case the processor would never
9385 * use the TPR shadow and we could simply clear the bit from
9386 * the execution control. But such a configuration is useless,
9387 * so let's keep the code simple.
9388 */
9389 if (!vmx->nested.virtual_apic_page)
9390 return false;
9391 }
9392
Wincy Van705699a2015-02-03 23:58:17 +08009393 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009394 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9395 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009396 return false;
9397
9398 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9399 kunmap(vmx->nested.pi_desc_page);
9400 nested_release_page(vmx->nested.pi_desc_page);
9401 }
9402 vmx->nested.pi_desc_page =
9403 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9404 if (!vmx->nested.pi_desc_page)
9405 return false;
9406
9407 vmx->nested.pi_desc =
9408 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9409 if (!vmx->nested.pi_desc) {
9410 nested_release_page_clean(vmx->nested.pi_desc_page);
9411 return false;
9412 }
9413 vmx->nested.pi_desc =
9414 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9415 (unsigned long)(vmcs12->posted_intr_desc_addr &
9416 (PAGE_SIZE - 1)));
9417 }
9418
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009419 return true;
9420}
9421
Jan Kiszkaf4124502014-03-07 20:03:13 +01009422static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9423{
9424 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9425 struct vcpu_vmx *vmx = to_vmx(vcpu);
9426
9427 if (vcpu->arch.virtual_tsc_khz == 0)
9428 return;
9429
9430 /* Make sure short timeouts reliably trigger an immediate vmexit.
9431 * hrtimer_start does not guarantee this. */
9432 if (preemption_timeout <= 1) {
9433 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9434 return;
9435 }
9436
9437 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9438 preemption_timeout *= 1000000;
9439 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9440 hrtimer_start(&vmx->nested.preemption_timer,
9441 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9442}
9443
Wincy Van3af18d92015-02-03 23:49:31 +08009444static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9445 struct vmcs12 *vmcs12)
9446{
9447 int maxphyaddr;
9448 u64 addr;
9449
9450 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9451 return 0;
9452
9453 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9454 WARN_ON(1);
9455 return -EINVAL;
9456 }
9457 maxphyaddr = cpuid_maxphyaddr(vcpu);
9458
9459 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9460 ((addr + PAGE_SIZE) >> maxphyaddr))
9461 return -EINVAL;
9462
9463 return 0;
9464}
9465
9466/*
9467 * Merge L0's and L1's MSR bitmap, return false to indicate that
9468 * we do not use the hardware.
9469 */
9470static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9471 struct vmcs12 *vmcs12)
9472{
Wincy Van82f0dd42015-02-03 23:57:18 +08009473 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009474 struct page *page;
9475 unsigned long *msr_bitmap;
9476
9477 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9478 return false;
9479
9480 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
9481 if (!page) {
9482 WARN_ON(1);
9483 return false;
9484 }
9485 msr_bitmap = (unsigned long *)kmap(page);
9486 if (!msr_bitmap) {
9487 nested_release_page_clean(page);
9488 WARN_ON(1);
9489 return false;
9490 }
9491
9492 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009493 if (nested_cpu_has_apic_reg_virt(vmcs12))
9494 for (msr = 0x800; msr <= 0x8ff; msr++)
9495 nested_vmx_disable_intercept_for_msr(
9496 msr_bitmap,
9497 vmx_msr_bitmap_nested,
9498 msr, MSR_TYPE_R);
Wincy Vanf2b93282015-02-03 23:56:03 +08009499 /* TPR is allowed */
9500 nested_vmx_disable_intercept_for_msr(msr_bitmap,
9501 vmx_msr_bitmap_nested,
9502 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9503 MSR_TYPE_R | MSR_TYPE_W);
Wincy Van608406e2015-02-03 23:57:51 +08009504 if (nested_cpu_has_vid(vmcs12)) {
9505 /* EOI and self-IPI are allowed */
9506 nested_vmx_disable_intercept_for_msr(
9507 msr_bitmap,
9508 vmx_msr_bitmap_nested,
9509 APIC_BASE_MSR + (APIC_EOI >> 4),
9510 MSR_TYPE_W);
9511 nested_vmx_disable_intercept_for_msr(
9512 msr_bitmap,
9513 vmx_msr_bitmap_nested,
9514 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9515 MSR_TYPE_W);
9516 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009517 } else {
9518 /*
9519 * Enable reading intercept of all the x2apic
9520 * MSRs. We should not rely on vmcs12 to do any
9521 * optimizations here, it may have been modified
9522 * by L1.
9523 */
9524 for (msr = 0x800; msr <= 0x8ff; msr++)
9525 __vmx_enable_intercept_for_msr(
9526 vmx_msr_bitmap_nested,
9527 msr,
9528 MSR_TYPE_R);
9529
Wincy Vanf2b93282015-02-03 23:56:03 +08009530 __vmx_enable_intercept_for_msr(
9531 vmx_msr_bitmap_nested,
9532 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
Wincy Van82f0dd42015-02-03 23:57:18 +08009533 MSR_TYPE_W);
Wincy Van608406e2015-02-03 23:57:51 +08009534 __vmx_enable_intercept_for_msr(
9535 vmx_msr_bitmap_nested,
9536 APIC_BASE_MSR + (APIC_EOI >> 4),
9537 MSR_TYPE_W);
9538 __vmx_enable_intercept_for_msr(
9539 vmx_msr_bitmap_nested,
9540 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9541 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +08009542 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009543 kunmap(page);
9544 nested_release_page_clean(page);
9545
9546 return true;
9547}
9548
9549static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9550 struct vmcs12 *vmcs12)
9551{
Wincy Van82f0dd42015-02-03 23:57:18 +08009552 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009553 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009554 !nested_cpu_has_vid(vmcs12) &&
9555 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009556 return 0;
9557
9558 /*
9559 * If virtualize x2apic mode is enabled,
9560 * virtualize apic access must be disabled.
9561 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009562 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9563 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009564 return -EINVAL;
9565
Wincy Van608406e2015-02-03 23:57:51 +08009566 /*
9567 * If virtual interrupt delivery is enabled,
9568 * we must exit on external interrupts.
9569 */
9570 if (nested_cpu_has_vid(vmcs12) &&
9571 !nested_exit_on_intr(vcpu))
9572 return -EINVAL;
9573
Wincy Van705699a2015-02-03 23:58:17 +08009574 /*
9575 * bits 15:8 should be zero in posted_intr_nv,
9576 * the descriptor address has been already checked
9577 * in nested_get_vmcs12_pages.
9578 */
9579 if (nested_cpu_has_posted_intr(vmcs12) &&
9580 (!nested_cpu_has_vid(vmcs12) ||
9581 !nested_exit_intr_ack_set(vcpu) ||
9582 vmcs12->posted_intr_nv & 0xff00))
9583 return -EINVAL;
9584
Wincy Vanf2b93282015-02-03 23:56:03 +08009585 /* tpr shadow is needed by all apicv features. */
9586 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9587 return -EINVAL;
9588
9589 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009590}
9591
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009592static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9593 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009594 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009595{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009596 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009597 u64 count, addr;
9598
9599 if (vmcs12_read_any(vcpu, count_field, &count) ||
9600 vmcs12_read_any(vcpu, addr_field, &addr)) {
9601 WARN_ON(1);
9602 return -EINVAL;
9603 }
9604 if (count == 0)
9605 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009606 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009607 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9608 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
9609 pr_warn_ratelimited(
9610 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9611 addr_field, maxphyaddr, count, addr);
9612 return -EINVAL;
9613 }
9614 return 0;
9615}
9616
9617static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9618 struct vmcs12 *vmcs12)
9619{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009620 if (vmcs12->vm_exit_msr_load_count == 0 &&
9621 vmcs12->vm_exit_msr_store_count == 0 &&
9622 vmcs12->vm_entry_msr_load_count == 0)
9623 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009624 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009625 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009626 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009627 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009628 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009629 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009630 return -EINVAL;
9631 return 0;
9632}
9633
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009634static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9635 struct vmx_msr_entry *e)
9636{
9637 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009638 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009639 return -EINVAL;
9640 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9641 e->index == MSR_IA32_UCODE_REV)
9642 return -EINVAL;
9643 if (e->reserved != 0)
9644 return -EINVAL;
9645 return 0;
9646}
9647
9648static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9649 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009650{
9651 if (e->index == MSR_FS_BASE ||
9652 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009653 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9654 nested_vmx_msr_check_common(vcpu, e))
9655 return -EINVAL;
9656 return 0;
9657}
9658
9659static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9660 struct vmx_msr_entry *e)
9661{
9662 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9663 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009664 return -EINVAL;
9665 return 0;
9666}
9667
9668/*
9669 * Load guest's/host's msr at nested entry/exit.
9670 * return 0 for success, entry index for failure.
9671 */
9672static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9673{
9674 u32 i;
9675 struct vmx_msr_entry e;
9676 struct msr_data msr;
9677
9678 msr.host_initiated = false;
9679 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009680 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9681 &e, sizeof(e))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009682 pr_warn_ratelimited(
9683 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9684 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009685 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009686 }
9687 if (nested_vmx_load_msr_check(vcpu, &e)) {
9688 pr_warn_ratelimited(
9689 "%s check failed (%u, 0x%x, 0x%x)\n",
9690 __func__, i, e.index, e.reserved);
9691 goto fail;
9692 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009693 msr.index = e.index;
9694 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009695 if (kvm_set_msr(vcpu, &msr)) {
9696 pr_warn_ratelimited(
9697 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9698 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009699 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009700 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009701 }
9702 return 0;
9703fail:
9704 return i + 1;
9705}
9706
9707static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9708{
9709 u32 i;
9710 struct vmx_msr_entry e;
9711
9712 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009713 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009714 if (kvm_vcpu_read_guest(vcpu,
9715 gpa + i * sizeof(e),
9716 &e, 2 * sizeof(u32))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009717 pr_warn_ratelimited(
9718 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9719 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009720 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009721 }
9722 if (nested_vmx_store_msr_check(vcpu, &e)) {
9723 pr_warn_ratelimited(
9724 "%s check failed (%u, 0x%x, 0x%x)\n",
9725 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009726 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009727 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009728 msr_info.host_initiated = false;
9729 msr_info.index = e.index;
9730 if (kvm_get_msr(vcpu, &msr_info)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009731 pr_warn_ratelimited(
9732 "%s cannot read MSR (%u, 0x%x)\n",
9733 __func__, i, e.index);
9734 return -EINVAL;
9735 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009736 if (kvm_vcpu_write_guest(vcpu,
9737 gpa + i * sizeof(e) +
9738 offsetof(struct vmx_msr_entry, value),
9739 &msr_info.data, sizeof(msr_info.data))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009740 pr_warn_ratelimited(
9741 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009742 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009743 return -EINVAL;
9744 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009745 }
9746 return 0;
9747}
9748
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009749/*
9750 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9751 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009752 * 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 +03009753 * guest in a way that will both be appropriate to L1's requests, and our
9754 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9755 * function also has additional necessary side-effects, like setting various
9756 * vcpu->arch fields.
9757 */
9758static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9759{
9760 struct vcpu_vmx *vmx = to_vmx(vcpu);
9761 u32 exec_control;
9762
9763 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9764 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9765 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9766 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9767 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9768 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9769 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9770 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9771 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9772 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9773 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9774 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9775 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9776 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9777 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9778 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9779 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9780 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9781 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9782 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9783 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9784 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9785 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9786 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9787 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9788 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9789 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9790 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9791 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9792 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9793 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9794 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9795 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9796 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9797 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9798 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9799
Jan Kiszka2996fca2014-06-16 13:59:43 +02009800 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9801 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9802 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9803 } else {
9804 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9805 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9806 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009807 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9808 vmcs12->vm_entry_intr_info_field);
9809 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9810 vmcs12->vm_entry_exception_error_code);
9811 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9812 vmcs12->vm_entry_instruction_len);
9813 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9814 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009815 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009816 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009817 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9818 vmcs12->guest_pending_dbg_exceptions);
9819 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9820 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9821
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009822 if (nested_cpu_has_xsaves(vmcs12))
9823 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009824 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9825
Jan Kiszkaf4124502014-03-07 20:03:13 +01009826 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009827
Paolo Bonzini93140062016-07-06 13:23:51 +02009828 /* Preemption timer setting is only taken from vmcs01. */
9829 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9830 exec_control |= vmcs_config.pin_based_exec_ctrl;
9831 if (vmx->hv_deadline_tsc == -1)
9832 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9833
9834 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +08009835 if (nested_cpu_has_posted_intr(vmcs12)) {
9836 /*
9837 * Note that we use L0's vector here and in
9838 * vmx_deliver_nested_posted_interrupt.
9839 */
9840 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9841 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +08009842 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Wincy Van705699a2015-02-03 23:58:17 +08009843 vmcs_write64(POSTED_INTR_DESC_ADDR,
9844 page_to_phys(vmx->nested.pi_desc_page) +
9845 (unsigned long)(vmcs12->posted_intr_desc_addr &
9846 (PAGE_SIZE - 1)));
9847 } else
9848 exec_control &= ~PIN_BASED_POSTED_INTR;
9849
Jan Kiszkaf4124502014-03-07 20:03:13 +01009850 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009851
Jan Kiszkaf4124502014-03-07 20:03:13 +01009852 vmx->nested.preemption_timer_expired = false;
9853 if (nested_cpu_has_preemption_timer(vmcs12))
9854 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01009855
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009856 /*
9857 * Whether page-faults are trapped is determined by a combination of
9858 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
9859 * If enable_ept, L0 doesn't care about page faults and we should
9860 * set all of these to L1's desires. However, if !enable_ept, L0 does
9861 * care about (at least some) page faults, and because it is not easy
9862 * (if at all possible?) to merge L0 and L1's desires, we simply ask
9863 * to exit on each and every L2 page fault. This is done by setting
9864 * MASK=MATCH=0 and (see below) EB.PF=1.
9865 * Note that below we don't need special code to set EB.PF beyond the
9866 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
9867 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
9868 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
9869 *
9870 * A problem with this approach (when !enable_ept) is that L1 may be
9871 * injected with more page faults than it asked for. This could have
9872 * caused problems, but in practice existing hypervisors don't care.
9873 * To fix this, we will need to emulate the PFEC checking (on the L1
9874 * page tables), using walk_addr(), when injecting PFs to L1.
9875 */
9876 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
9877 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
9878 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
9879 enable_ept ? vmcs12->page_fault_error_code_match : 0);
9880
9881 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +01009882 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +08009883
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009884 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009885 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009886 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009887 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -07009888 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009889 if (nested_cpu_has(vmcs12,
9890 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
9891 exec_control |= vmcs12->secondary_vm_exec_control;
9892
9893 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
9894 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009895 * If translation failed, no matter: This feature asks
9896 * to exit when accessing the given address, and if it
9897 * can never be accessed, this feature won't do
9898 * anything anyway.
9899 */
9900 if (!vmx->nested.apic_access_page)
9901 exec_control &=
9902 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9903 else
9904 vmcs_write64(APIC_ACCESS_ADDR,
9905 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +08009906 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +02009907 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +01009908 exec_control |=
9909 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +08009910 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009911 }
9912
Wincy Van608406e2015-02-03 23:57:51 +08009913 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
9914 vmcs_write64(EOI_EXIT_BITMAP0,
9915 vmcs12->eoi_exit_bitmap0);
9916 vmcs_write64(EOI_EXIT_BITMAP1,
9917 vmcs12->eoi_exit_bitmap1);
9918 vmcs_write64(EOI_EXIT_BITMAP2,
9919 vmcs12->eoi_exit_bitmap2);
9920 vmcs_write64(EOI_EXIT_BITMAP3,
9921 vmcs12->eoi_exit_bitmap3);
9922 vmcs_write16(GUEST_INTR_STATUS,
9923 vmcs12->guest_intr_status);
9924 }
9925
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009926 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
9927 }
9928
9929
9930 /*
9931 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
9932 * Some constant fields are set here by vmx_set_constant_host_state().
9933 * Other fields are different per CPU, and will be set later when
9934 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
9935 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08009936 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009937
9938 /*
9939 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
9940 * entry, but only if the current (host) sp changed from the value
9941 * we wrote last (vmx->host_rsp). This cache is no longer relevant
9942 * if we switch vmcs, and rather than hold a separate cache per vmcs,
9943 * here we just force the write to happen on entry.
9944 */
9945 vmx->host_rsp = 0;
9946
9947 exec_control = vmx_exec_control(vmx); /* L0's desires */
9948 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
9949 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
9950 exec_control &= ~CPU_BASED_TPR_SHADOW;
9951 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009952
9953 if (exec_control & CPU_BASED_TPR_SHADOW) {
9954 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
9955 page_to_phys(vmx->nested.virtual_apic_page));
9956 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
9957 }
9958
Wincy Van3af18d92015-02-03 23:49:31 +08009959 if (cpu_has_vmx_msr_bitmap() &&
Wincy Van670125b2015-03-04 14:31:56 +08009960 exec_control & CPU_BASED_USE_MSR_BITMAPS) {
9961 nested_vmx_merge_msr_bitmap(vcpu, vmcs12);
9962 /* MSR_BITMAP will be set by following vmx_set_efer. */
Wincy Van3af18d92015-02-03 23:49:31 +08009963 } else
9964 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
9965
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009966 /*
Wincy Van3af18d92015-02-03 23:49:31 +08009967 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009968 * Rather, exit every time.
9969 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009970 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
9971 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
9972
9973 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
9974
9975 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
9976 * bitwise-or of what L1 wants to trap for L2, and what we want to
9977 * trap. Note that CR0.TS also needs updating - we do this later.
9978 */
9979 update_exception_bitmap(vcpu);
9980 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
9981 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
9982
Nadav Har'El8049d652013-08-05 11:07:06 +03009983 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
9984 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
9985 * bits are further modified by vmx_set_efer() below.
9986 */
Jan Kiszkaf4124502014-03-07 20:03:13 +01009987 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03009988
9989 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
9990 * emulated by vmx_set_efer(), below.
9991 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02009992 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03009993 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
9994 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009995 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
9996
Jan Kiszka44811c02013-08-04 17:17:27 +02009997 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009998 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02009999 vcpu->arch.pat = vmcs12->guest_ia32_pat;
10000 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010001 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
10002
10003
10004 set_cr4_guest_host_mask(vmx);
10005
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010006 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
10007 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10008
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010009 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10010 vmcs_write64(TSC_OFFSET,
10011 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
10012 else
10013 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010014
10015 if (enable_vpid) {
10016 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010017 * There is no direct mapping between vpid02 and vpid12, the
10018 * vpid02 is per-vCPU for L0 and reused while the value of
10019 * vpid12 is changed w/ one invvpid during nested vmentry.
10020 * The vpid12 is allocated by L1 for L2, so it will not
10021 * influence global bitmap(for vpid01 and vpid02 allocation)
10022 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010023 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010024 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10025 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10026 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10027 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10028 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10029 }
10030 } else {
10031 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10032 vmx_flush_tlb(vcpu);
10033 }
10034
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010035 }
10036
Nadav Har'El155a97a2013-08-05 11:07:16 +030010037 if (nested_cpu_has_ept(vmcs12)) {
10038 kvm_mmu_unload(vcpu);
10039 nested_ept_init_mmu_context(vcpu);
10040 }
10041
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010042 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
10043 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010044 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010045 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10046 else
10047 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10048 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10049 vmx_set_efer(vcpu, vcpu->arch.efer);
10050
10051 /*
10052 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
10053 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
10054 * The CR0_READ_SHADOW is what L2 should have expected to read given
10055 * the specifications by L1; It's not enough to take
10056 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10057 * have more bits than L1 expected.
10058 */
10059 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10060 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10061
10062 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10063 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10064
10065 /* shadow page tables on either EPT or shadow page tables */
10066 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
10067 kvm_mmu_reset_context(vcpu);
10068
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010069 if (!enable_ept)
10070 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10071
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010072 /*
10073 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10074 */
10075 if (enable_ept) {
10076 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10077 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10078 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10079 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10080 }
10081
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010082 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10083 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
10084}
10085
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010086/*
10087 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10088 * for running an L2 nested guest.
10089 */
10090static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10091{
10092 struct vmcs12 *vmcs12;
10093 struct vcpu_vmx *vmx = to_vmx(vcpu);
10094 int cpu;
10095 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +020010096 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +030010097 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010098
10099 if (!nested_vmx_check_permission(vcpu) ||
10100 !nested_vmx_check_vmcs12(vcpu))
10101 return 1;
10102
10103 skip_emulated_instruction(vcpu);
10104 vmcs12 = get_vmcs12(vcpu);
10105
Abel Gordon012f83c2013-04-18 14:39:25 +030010106 if (enable_shadow_vmcs)
10107 copy_shadow_to_vmcs12(vmx);
10108
Nadav Har'El7c177932011-05-25 23:12:04 +030010109 /*
10110 * The nested entry process starts with enforcing various prerequisites
10111 * on vmcs12 as required by the Intel SDM, and act appropriately when
10112 * they fail: As the SDM explains, some conditions should cause the
10113 * instruction to fail, while others will cause the instruction to seem
10114 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10115 * To speed up the normal (success) code path, we should avoid checking
10116 * for misconfigurations which will anyway be caught by the processor
10117 * when using the merged vmcs02.
10118 */
10119 if (vmcs12->launch_state == launch) {
10120 nested_vmx_failValid(vcpu,
10121 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10122 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
10123 return 1;
10124 }
10125
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010126 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10127 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010128 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10129 return 1;
10130 }
10131
Wincy Van3af18d92015-02-03 23:49:31 +080010132 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010133 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10134 return 1;
10135 }
10136
Wincy Van3af18d92015-02-03 23:49:31 +080010137 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +030010138 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10139 return 1;
10140 }
10141
Wincy Vanf2b93282015-02-03 23:56:03 +080010142 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
10143 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10144 return 1;
10145 }
10146
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030010147 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
10148 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10149 return 1;
10150 }
10151
Nadav Har'El7c177932011-05-25 23:12:04 +030010152 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010153 vmx->nested.nested_vmx_true_procbased_ctls_low,
10154 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010155 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010156 vmx->nested.nested_vmx_secondary_ctls_low,
10157 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010158 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010159 vmx->nested.nested_vmx_pinbased_ctls_low,
10160 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010161 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010162 vmx->nested.nested_vmx_true_exit_ctls_low,
10163 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010164 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +080010165 vmx->nested.nested_vmx_true_entry_ctls_low,
10166 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +030010167 {
10168 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
10169 return 1;
10170 }
10171
10172 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
10173 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10174 nested_vmx_failValid(vcpu,
10175 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
10176 return 1;
10177 }
10178
Wincy Vanb9c237b2015-02-03 23:56:30 +080010179 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +030010180 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
10181 nested_vmx_entry_failure(vcpu, vmcs12,
10182 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10183 return 1;
10184 }
10185 if (vmcs12->vmcs_link_pointer != -1ull) {
10186 nested_vmx_entry_failure(vcpu, vmcs12,
10187 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
10188 return 1;
10189 }
10190
10191 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +020010192 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +020010193 * are performed on the field for the IA32_EFER MSR:
10194 * - Bits reserved in the IA32_EFER MSR must be 0.
10195 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10196 * the IA-32e mode guest VM-exit control. It must also be identical
10197 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10198 * CR0.PG) is 1.
10199 */
10200 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
10201 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10202 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10203 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10204 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10205 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
10206 nested_vmx_entry_failure(vcpu, vmcs12,
10207 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10208 return 1;
10209 }
10210 }
10211
10212 /*
10213 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10214 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10215 * the values of the LMA and LME bits in the field must each be that of
10216 * the host address-space size VM-exit control.
10217 */
10218 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10219 ia32e = (vmcs12->vm_exit_controls &
10220 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10221 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10222 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10223 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
10224 nested_vmx_entry_failure(vcpu, vmcs12,
10225 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
10226 return 1;
10227 }
10228 }
10229
10230 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010231 * We're finally done with prerequisite checking, and can start with
10232 * the nested entry.
10233 */
10234
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010235 vmcs02 = nested_get_current_vmcs02(vmx);
10236 if (!vmcs02)
10237 return -ENOMEM;
10238
10239 enter_guest_mode(vcpu);
10240
10241 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
10242
Jan Kiszka2996fca2014-06-16 13:59:43 +020010243 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10244 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10245
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010246 cpu = get_cpu();
10247 vmx->loaded_vmcs = vmcs02;
10248 vmx_vcpu_put(vcpu);
10249 vmx_vcpu_load(vcpu, cpu);
10250 vcpu->cpu = cpu;
10251 put_cpu();
10252
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010253 vmx_segment_cache_clear(vmx);
10254
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010255 prepare_vmcs02(vcpu, vmcs12);
10256
Wincy Vanff651cb2014-12-11 08:52:58 +030010257 msr_entry_idx = nested_vmx_load_msr(vcpu,
10258 vmcs12->vm_entry_msr_load_addr,
10259 vmcs12->vm_entry_msr_load_count);
10260 if (msr_entry_idx) {
10261 leave_guest_mode(vcpu);
10262 vmx_load_vmcs01(vcpu);
10263 nested_vmx_entry_failure(vcpu, vmcs12,
10264 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10265 return 1;
10266 }
10267
10268 vmcs12->launch_state = 1;
10269
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010270 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010271 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010272
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010273 vmx->nested.nested_run_pending = 1;
10274
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010275 /*
10276 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10277 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10278 * returned as far as L1 is concerned. It will only return (and set
10279 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10280 */
10281 return 1;
10282}
10283
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010284/*
10285 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10286 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10287 * This function returns the new value we should put in vmcs12.guest_cr0.
10288 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10289 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10290 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10291 * didn't trap the bit, because if L1 did, so would L0).
10292 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10293 * been modified by L2, and L1 knows it. So just leave the old value of
10294 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10295 * isn't relevant, because if L0 traps this bit it can set it to anything.
10296 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10297 * changed these bits, and therefore they need to be updated, but L0
10298 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10299 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10300 */
10301static inline unsigned long
10302vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10303{
10304 return
10305 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10306 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10307 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10308 vcpu->arch.cr0_guest_owned_bits));
10309}
10310
10311static inline unsigned long
10312vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10313{
10314 return
10315 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10316 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10317 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10318 vcpu->arch.cr4_guest_owned_bits));
10319}
10320
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010321static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10322 struct vmcs12 *vmcs12)
10323{
10324 u32 idt_vectoring;
10325 unsigned int nr;
10326
Gleb Natapov851eb6672013-09-25 12:51:34 +030010327 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010328 nr = vcpu->arch.exception.nr;
10329 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10330
10331 if (kvm_exception_is_soft(nr)) {
10332 vmcs12->vm_exit_instruction_len =
10333 vcpu->arch.event_exit_inst_len;
10334 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10335 } else
10336 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10337
10338 if (vcpu->arch.exception.has_error_code) {
10339 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10340 vmcs12->idt_vectoring_error_code =
10341 vcpu->arch.exception.error_code;
10342 }
10343
10344 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010345 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010346 vmcs12->idt_vectoring_info_field =
10347 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10348 } else if (vcpu->arch.interrupt.pending) {
10349 nr = vcpu->arch.interrupt.nr;
10350 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10351
10352 if (vcpu->arch.interrupt.soft) {
10353 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10354 vmcs12->vm_entry_instruction_len =
10355 vcpu->arch.event_exit_inst_len;
10356 } else
10357 idt_vectoring |= INTR_TYPE_EXT_INTR;
10358
10359 vmcs12->idt_vectoring_info_field = idt_vectoring;
10360 }
10361}
10362
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010363static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10364{
10365 struct vcpu_vmx *vmx = to_vmx(vcpu);
10366
Jan Kiszkaf4124502014-03-07 20:03:13 +010010367 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10368 vmx->nested.preemption_timer_expired) {
10369 if (vmx->nested.nested_run_pending)
10370 return -EBUSY;
10371 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10372 return 0;
10373 }
10374
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010375 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010376 if (vmx->nested.nested_run_pending ||
10377 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010378 return -EBUSY;
10379 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10380 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10381 INTR_INFO_VALID_MASK, 0);
10382 /*
10383 * The NMI-triggered VM exit counts as injection:
10384 * clear this one and block further NMIs.
10385 */
10386 vcpu->arch.nmi_pending = 0;
10387 vmx_set_nmi_mask(vcpu, true);
10388 return 0;
10389 }
10390
10391 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10392 nested_exit_on_intr(vcpu)) {
10393 if (vmx->nested.nested_run_pending)
10394 return -EBUSY;
10395 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010396 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010397 }
10398
Wincy Van705699a2015-02-03 23:58:17 +080010399 return vmx_complete_nested_posted_interrupt(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010400}
10401
Jan Kiszkaf4124502014-03-07 20:03:13 +010010402static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10403{
10404 ktime_t remaining =
10405 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10406 u64 value;
10407
10408 if (ktime_to_ns(remaining) <= 0)
10409 return 0;
10410
10411 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10412 do_div(value, 1000000);
10413 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10414}
10415
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010416/*
10417 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10418 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10419 * and this function updates it to reflect the changes to the guest state while
10420 * L2 was running (and perhaps made some exits which were handled directly by L0
10421 * without going back to L1), and to reflect the exit reason.
10422 * Note that we do not have to copy here all VMCS fields, just those that
10423 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10424 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10425 * which already writes to vmcs12 directly.
10426 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010427static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10428 u32 exit_reason, u32 exit_intr_info,
10429 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010430{
10431 /* update guest state fields: */
10432 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10433 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10434
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010435 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10436 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10437 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10438
10439 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10440 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10441 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10442 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10443 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10444 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10445 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10446 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10447 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10448 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10449 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10450 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10451 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10452 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10453 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10454 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10455 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10456 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10457 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10458 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10459 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10460 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10461 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10462 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10463 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10464 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10465 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10466 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10467 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10468 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10469 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10470 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10471 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10472 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10473 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10474 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10475
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010476 vmcs12->guest_interruptibility_info =
10477 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10478 vmcs12->guest_pending_dbg_exceptions =
10479 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010480 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10481 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10482 else
10483 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010484
Jan Kiszkaf4124502014-03-07 20:03:13 +010010485 if (nested_cpu_has_preemption_timer(vmcs12)) {
10486 if (vmcs12->vm_exit_controls &
10487 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10488 vmcs12->vmx_preemption_timer_value =
10489 vmx_get_preemption_timer_value(vcpu);
10490 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10491 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010492
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010493 /*
10494 * In some cases (usually, nested EPT), L2 is allowed to change its
10495 * own CR3 without exiting. If it has changed it, we must keep it.
10496 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10497 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10498 *
10499 * Additionally, restore L2's PDPTR to vmcs12.
10500 */
10501 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010502 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010503 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10504 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10505 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10506 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10507 }
10508
Wincy Van608406e2015-02-03 23:57:51 +080010509 if (nested_cpu_has_vid(vmcs12))
10510 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10511
Jan Kiszkac18911a2013-03-13 16:06:41 +010010512 vmcs12->vm_entry_controls =
10513 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010514 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010515
Jan Kiszka2996fca2014-06-16 13:59:43 +020010516 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10517 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10518 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10519 }
10520
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010521 /* TODO: These cannot have changed unless we have MSR bitmaps and
10522 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010523 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010524 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010525 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10526 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010527 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10528 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10529 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010530 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010531 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010532 if (nested_cpu_has_xsaves(vmcs12))
10533 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010534
10535 /* update exit information fields: */
10536
Jan Kiszka533558b2014-01-04 18:47:20 +010010537 vmcs12->vm_exit_reason = exit_reason;
10538 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010539
Jan Kiszka533558b2014-01-04 18:47:20 +010010540 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010541 if ((vmcs12->vm_exit_intr_info &
10542 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10543 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10544 vmcs12->vm_exit_intr_error_code =
10545 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010546 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010547 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10548 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10549
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010550 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10551 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10552 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010553 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010554
10555 /*
10556 * Transfer the event that L0 or L1 may wanted to inject into
10557 * L2 to IDT_VECTORING_INFO_FIELD.
10558 */
10559 vmcs12_save_pending_event(vcpu, vmcs12);
10560 }
10561
10562 /*
10563 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10564 * preserved above and would only end up incorrectly in L1.
10565 */
10566 vcpu->arch.nmi_injected = false;
10567 kvm_clear_exception_queue(vcpu);
10568 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010569}
10570
10571/*
10572 * A part of what we need to when the nested L2 guest exits and we want to
10573 * run its L1 parent, is to reset L1's guest state to the host state specified
10574 * in vmcs12.
10575 * This function is to be called not only on normal nested exit, but also on
10576 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10577 * Failures During or After Loading Guest State").
10578 * This function should be called when the active VMCS is L1's (vmcs01).
10579 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010580static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10581 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010582{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010583 struct kvm_segment seg;
10584
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010585 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10586 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010587 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010588 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10589 else
10590 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10591 vmx_set_efer(vcpu, vcpu->arch.efer);
10592
10593 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10594 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010595 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010596 /*
10597 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10598 * actually changed, because it depends on the current state of
10599 * fpu_active (which may have changed).
10600 * Note that vmx_set_cr0 refers to efer set above.
10601 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020010602 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010603 /*
10604 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10605 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10606 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10607 */
10608 update_exception_bitmap(vcpu);
10609 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10610 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10611
10612 /*
10613 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10614 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10615 */
10616 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10617 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10618
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010619 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010620
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010621 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10622 kvm_mmu_reset_context(vcpu);
10623
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010624 if (!enable_ept)
10625 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10626
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010627 if (enable_vpid) {
10628 /*
10629 * Trivially support vpid by letting L2s share their parent
10630 * L1's vpid. TODO: move to a more elaborate solution, giving
10631 * each L2 its own vpid and exposing the vpid feature to L1.
10632 */
10633 vmx_flush_tlb(vcpu);
10634 }
10635
10636
10637 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10638 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10639 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10640 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10641 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010642
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010643 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10644 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10645 vmcs_write64(GUEST_BNDCFGS, 0);
10646
Jan Kiszka44811c02013-08-04 17:17:27 +020010647 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010648 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010649 vcpu->arch.pat = vmcs12->host_ia32_pat;
10650 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010651 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10652 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10653 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010654
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010655 /* Set L1 segment info according to Intel SDM
10656 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10657 seg = (struct kvm_segment) {
10658 .base = 0,
10659 .limit = 0xFFFFFFFF,
10660 .selector = vmcs12->host_cs_selector,
10661 .type = 11,
10662 .present = 1,
10663 .s = 1,
10664 .g = 1
10665 };
10666 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10667 seg.l = 1;
10668 else
10669 seg.db = 1;
10670 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10671 seg = (struct kvm_segment) {
10672 .base = 0,
10673 .limit = 0xFFFFFFFF,
10674 .type = 3,
10675 .present = 1,
10676 .s = 1,
10677 .db = 1,
10678 .g = 1
10679 };
10680 seg.selector = vmcs12->host_ds_selector;
10681 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10682 seg.selector = vmcs12->host_es_selector;
10683 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10684 seg.selector = vmcs12->host_ss_selector;
10685 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10686 seg.selector = vmcs12->host_fs_selector;
10687 seg.base = vmcs12->host_fs_base;
10688 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10689 seg.selector = vmcs12->host_gs_selector;
10690 seg.base = vmcs12->host_gs_base;
10691 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10692 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010693 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010694 .limit = 0x67,
10695 .selector = vmcs12->host_tr_selector,
10696 .type = 11,
10697 .present = 1
10698 };
10699 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10700
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010701 kvm_set_dr(vcpu, 7, 0x400);
10702 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010703
Wincy Van3af18d92015-02-03 23:49:31 +080010704 if (cpu_has_vmx_msr_bitmap())
10705 vmx_set_msr_bitmap(vcpu);
10706
Wincy Vanff651cb2014-12-11 08:52:58 +030010707 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10708 vmcs12->vm_exit_msr_load_count))
10709 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010710}
10711
10712/*
10713 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10714 * and modify vmcs12 to make it see what it would expect to see there if
10715 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10716 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010717static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10718 u32 exit_intr_info,
10719 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010720{
10721 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010722 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10723
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010724 /* trying to cancel vmlaunch/vmresume is a bug */
10725 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10726
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010727 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010728 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10729 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010730
Wincy Vanff651cb2014-12-11 08:52:58 +030010731 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10732 vmcs12->vm_exit_msr_store_count))
10733 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10734
Wanpeng Lif3380ca2014-08-05 12:42:23 +080010735 vmx_load_vmcs01(vcpu);
10736
Bandan Das77b0f5d2014-04-19 18:17:45 -040010737 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10738 && nested_exit_intr_ack_set(vcpu)) {
10739 int irq = kvm_cpu_get_interrupt(vcpu);
10740 WARN_ON(irq < 0);
10741 vmcs12->vm_exit_intr_info = irq |
10742 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10743 }
10744
Jan Kiszka542060e2014-01-04 18:47:21 +010010745 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10746 vmcs12->exit_qualification,
10747 vmcs12->idt_vectoring_info_field,
10748 vmcs12->vm_exit_intr_info,
10749 vmcs12->vm_exit_intr_error_code,
10750 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010751
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010752 vm_entry_controls_reset_shadow(vmx);
10753 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010754 vmx_segment_cache_clear(vmx);
10755
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010756 /* if no vmcs02 cache requested, remove the one we used */
10757 if (VMCS02_POOL_SIZE == 0)
10758 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10759
10760 load_vmcs12_host_state(vcpu, vmcs12);
10761
Paolo Bonzini93140062016-07-06 13:23:51 +020010762 /* Update any VMCS fields that might have changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010763 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020010764 if (vmx->hv_deadline_tsc == -1)
10765 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10766 PIN_BASED_VMX_PREEMPTION_TIMER);
10767 else
10768 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10769 PIN_BASED_VMX_PREEMPTION_TIMER);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010770
10771 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10772 vmx->host_rsp = 0;
10773
10774 /* Unpin physical memory we referred to in vmcs02 */
10775 if (vmx->nested.apic_access_page) {
10776 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010777 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010778 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010779 if (vmx->nested.virtual_apic_page) {
10780 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010781 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010782 }
Wincy Van705699a2015-02-03 23:58:17 +080010783 if (vmx->nested.pi_desc_page) {
10784 kunmap(vmx->nested.pi_desc_page);
10785 nested_release_page(vmx->nested.pi_desc_page);
10786 vmx->nested.pi_desc_page = NULL;
10787 vmx->nested.pi_desc = NULL;
10788 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010789
10790 /*
Tang Chen38b99172014-09-24 15:57:54 +080010791 * We are now running in L2, mmu_notifier will force to reload the
10792 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10793 */
10794 kvm_vcpu_reload_apic_access_page(vcpu);
10795
10796 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010797 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10798 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10799 * success or failure flag accordingly.
10800 */
10801 if (unlikely(vmx->fail)) {
10802 vmx->fail = 0;
10803 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
10804 } else
10805 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010806 if (enable_shadow_vmcs)
10807 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010808
10809 /* in case we halted in L2 */
10810 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010811}
10812
Nadav Har'El7c177932011-05-25 23:12:04 +030010813/*
Jan Kiszka42124922014-01-04 18:47:19 +010010814 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10815 */
10816static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10817{
10818 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +010010819 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +010010820 free_nested(to_vmx(vcpu));
10821}
10822
10823/*
Nadav Har'El7c177932011-05-25 23:12:04 +030010824 * L1's failure to enter L2 is a subset of a normal exit, as explained in
10825 * 23.7 "VM-entry failures during or after loading guest state" (this also
10826 * lists the acceptable exit-reason and exit-qualification parameters).
10827 * It should only be called before L2 actually succeeded to run, and when
10828 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
10829 */
10830static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
10831 struct vmcs12 *vmcs12,
10832 u32 reason, unsigned long qualification)
10833{
10834 load_vmcs12_host_state(vcpu, vmcs12);
10835 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
10836 vmcs12->exit_qualification = qualification;
10837 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010838 if (enable_shadow_vmcs)
10839 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030010840}
10841
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010842static int vmx_check_intercept(struct kvm_vcpu *vcpu,
10843 struct x86_instruction_info *info,
10844 enum x86_intercept_stage stage)
10845{
10846 return X86EMUL_CONTINUE;
10847}
10848
Yunhong Jiang64672c92016-06-13 14:19:59 -070010849#ifdef CONFIG_X86_64
10850/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
10851static inline int u64_shl_div_u64(u64 a, unsigned int shift,
10852 u64 divisor, u64 *result)
10853{
10854 u64 low = a << shift, high = a >> (64 - shift);
10855
10856 /* To avoid the overflow on divq */
10857 if (high >= divisor)
10858 return 1;
10859
10860 /* Low hold the result, high hold rem which is discarded */
10861 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
10862 "rm" (divisor), "0" (low), "1" (high));
10863 *result = low;
10864
10865 return 0;
10866}
10867
10868static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
10869{
10870 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020010871 u64 tscl = rdtsc();
10872 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
10873 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010874
10875 /* Convert to host delta tsc if tsc scaling is enabled */
10876 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
10877 u64_shl_div_u64(delta_tsc,
10878 kvm_tsc_scaling_ratio_frac_bits,
10879 vcpu->arch.tsc_scaling_ratio,
10880 &delta_tsc))
10881 return -ERANGE;
10882
10883 /*
10884 * If the delta tsc can't fit in the 32 bit after the multi shift,
10885 * we can't use the preemption timer.
10886 * It's possible that it fits on later vmentries, but checking
10887 * on every vmentry is costly so we just use an hrtimer.
10888 */
10889 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
10890 return -ERANGE;
10891
10892 vmx->hv_deadline_tsc = tscl + delta_tsc;
10893 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10894 PIN_BASED_VMX_PREEMPTION_TIMER);
10895 return 0;
10896}
10897
10898static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
10899{
10900 struct vcpu_vmx *vmx = to_vmx(vcpu);
10901 vmx->hv_deadline_tsc = -1;
10902 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10903 PIN_BASED_VMX_PREEMPTION_TIMER);
10904}
10905#endif
10906
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010907static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010908{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020010909 if (ple_gap)
10910 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010911}
10912
Kai Huang843e4332015-01-28 10:54:28 +080010913static void vmx_slot_enable_log_dirty(struct kvm *kvm,
10914 struct kvm_memory_slot *slot)
10915{
10916 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
10917 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
10918}
10919
10920static void vmx_slot_disable_log_dirty(struct kvm *kvm,
10921 struct kvm_memory_slot *slot)
10922{
10923 kvm_mmu_slot_set_dirty(kvm, slot);
10924}
10925
10926static void vmx_flush_log_dirty(struct kvm *kvm)
10927{
10928 kvm_flush_pml_buffers(kvm);
10929}
10930
10931static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
10932 struct kvm_memory_slot *memslot,
10933 gfn_t offset, unsigned long mask)
10934{
10935 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
10936}
10937
Feng Wuefc64402015-09-18 22:29:51 +080010938/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080010939 * This routine does the following things for vCPU which is going
10940 * to be blocked if VT-d PI is enabled.
10941 * - Store the vCPU to the wakeup list, so when interrupts happen
10942 * we can find the right vCPU to wake up.
10943 * - Change the Posted-interrupt descriptor as below:
10944 * 'NDST' <-- vcpu->pre_pcpu
10945 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
10946 * - If 'ON' is set during this process, which means at least one
10947 * interrupt is posted for this vCPU, we cannot block it, in
10948 * this case, return 1, otherwise, return 0.
10949 *
10950 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070010951static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080010952{
10953 unsigned long flags;
10954 unsigned int dest;
10955 struct pi_desc old, new;
10956 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
10957
10958 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080010959 !irq_remapping_cap(IRQ_POSTING_CAP) ||
10960 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080010961 return 0;
10962
10963 vcpu->pre_pcpu = vcpu->cpu;
10964 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10965 vcpu->pre_pcpu), flags);
10966 list_add_tail(&vcpu->blocked_vcpu_list,
10967 &per_cpu(blocked_vcpu_on_cpu,
10968 vcpu->pre_pcpu));
10969 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
10970 vcpu->pre_pcpu), flags);
10971
10972 do {
10973 old.control = new.control = pi_desc->control;
10974
10975 /*
10976 * We should not block the vCPU if
10977 * an interrupt is posted for it.
10978 */
10979 if (pi_test_on(pi_desc) == 1) {
10980 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10981 vcpu->pre_pcpu), flags);
10982 list_del(&vcpu->blocked_vcpu_list);
10983 spin_unlock_irqrestore(
10984 &per_cpu(blocked_vcpu_on_cpu_lock,
10985 vcpu->pre_pcpu), flags);
10986 vcpu->pre_pcpu = -1;
10987
10988 return 1;
10989 }
10990
10991 WARN((pi_desc->sn == 1),
10992 "Warning: SN field of posted-interrupts "
10993 "is set before blocking\n");
10994
10995 /*
10996 * Since vCPU can be preempted during this process,
10997 * vcpu->cpu could be different with pre_pcpu, we
10998 * need to set pre_pcpu as the destination of wakeup
10999 * notification event, then we can find the right vCPU
11000 * to wakeup in wakeup handler if interrupts happen
11001 * when the vCPU is in blocked state.
11002 */
11003 dest = cpu_physical_id(vcpu->pre_pcpu);
11004
11005 if (x2apic_enabled())
11006 new.ndst = dest;
11007 else
11008 new.ndst = (dest << 8) & 0xFF00;
11009
11010 /* set 'NV' to 'wakeup vector' */
11011 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11012 } while (cmpxchg(&pi_desc->control, old.control,
11013 new.control) != old.control);
11014
11015 return 0;
11016}
11017
Yunhong Jiangbc225122016-06-13 14:19:58 -070011018static int vmx_pre_block(struct kvm_vcpu *vcpu)
11019{
11020 if (pi_pre_block(vcpu))
11021 return 1;
11022
Yunhong Jiang64672c92016-06-13 14:19:59 -070011023 if (kvm_lapic_hv_timer_in_use(vcpu))
11024 kvm_lapic_switch_to_sw_timer(vcpu);
11025
Yunhong Jiangbc225122016-06-13 14:19:58 -070011026 return 0;
11027}
11028
11029static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011030{
11031 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11032 struct pi_desc old, new;
11033 unsigned int dest;
11034 unsigned long flags;
11035
11036 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011037 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11038 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011039 return;
11040
11041 do {
11042 old.control = new.control = pi_desc->control;
11043
11044 dest = cpu_physical_id(vcpu->cpu);
11045
11046 if (x2apic_enabled())
11047 new.ndst = dest;
11048 else
11049 new.ndst = (dest << 8) & 0xFF00;
11050
11051 /* Allow posting non-urgent interrupts */
11052 new.sn = 0;
11053
11054 /* set 'NV' to 'notification vector' */
11055 new.nv = POSTED_INTR_VECTOR;
11056 } while (cmpxchg(&pi_desc->control, old.control,
11057 new.control) != old.control);
11058
11059 if(vcpu->pre_pcpu != -1) {
11060 spin_lock_irqsave(
11061 &per_cpu(blocked_vcpu_on_cpu_lock,
11062 vcpu->pre_pcpu), flags);
11063 list_del(&vcpu->blocked_vcpu_list);
11064 spin_unlock_irqrestore(
11065 &per_cpu(blocked_vcpu_on_cpu_lock,
11066 vcpu->pre_pcpu), flags);
11067 vcpu->pre_pcpu = -1;
11068 }
11069}
11070
Yunhong Jiangbc225122016-06-13 14:19:58 -070011071static void vmx_post_block(struct kvm_vcpu *vcpu)
11072{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011073 if (kvm_x86_ops->set_hv_timer)
11074 kvm_lapic_switch_to_hv_timer(vcpu);
11075
Yunhong Jiangbc225122016-06-13 14:19:58 -070011076 pi_post_block(vcpu);
11077}
11078
Feng Wubf9f6ac2015-09-18 22:29:55 +080011079/*
Feng Wuefc64402015-09-18 22:29:51 +080011080 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11081 *
11082 * @kvm: kvm
11083 * @host_irq: host irq of the interrupt
11084 * @guest_irq: gsi of the interrupt
11085 * @set: set or unset PI
11086 * returns 0 on success, < 0 on failure
11087 */
11088static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11089 uint32_t guest_irq, bool set)
11090{
11091 struct kvm_kernel_irq_routing_entry *e;
11092 struct kvm_irq_routing_table *irq_rt;
11093 struct kvm_lapic_irq irq;
11094 struct kvm_vcpu *vcpu;
11095 struct vcpu_data vcpu_info;
11096 int idx, ret = -EINVAL;
11097
11098 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011099 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11100 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011101 return 0;
11102
11103 idx = srcu_read_lock(&kvm->irq_srcu);
11104 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11105 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
11106
11107 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11108 if (e->type != KVM_IRQ_ROUTING_MSI)
11109 continue;
11110 /*
11111 * VT-d PI cannot support posting multicast/broadcast
11112 * interrupts to a vCPU, we still use interrupt remapping
11113 * for these kind of interrupts.
11114 *
11115 * For lowest-priority interrupts, we only support
11116 * those with single CPU as the destination, e.g. user
11117 * configures the interrupts via /proc/irq or uses
11118 * irqbalance to make the interrupts single-CPU.
11119 *
11120 * We will support full lowest-priority interrupt later.
11121 */
11122
Radim Krčmář371313132016-07-12 22:09:27 +020011123 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011124 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11125 /*
11126 * Make sure the IRTE is in remapped mode if
11127 * we don't handle it in posted mode.
11128 */
11129 ret = irq_set_vcpu_affinity(host_irq, NULL);
11130 if (ret < 0) {
11131 printk(KERN_INFO
11132 "failed to back to remapped mode, irq: %u\n",
11133 host_irq);
11134 goto out;
11135 }
11136
Feng Wuefc64402015-09-18 22:29:51 +080011137 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011138 }
Feng Wuefc64402015-09-18 22:29:51 +080011139
11140 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11141 vcpu_info.vector = irq.vector;
11142
Feng Wub6ce9782016-01-25 16:53:35 +080011143 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011144 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11145
11146 if (set)
11147 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11148 else {
11149 /* suppress notification event before unposting */
11150 pi_set_sn(vcpu_to_pi_desc(vcpu));
11151 ret = irq_set_vcpu_affinity(host_irq, NULL);
11152 pi_clear_sn(vcpu_to_pi_desc(vcpu));
11153 }
11154
11155 if (ret < 0) {
11156 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11157 __func__);
11158 goto out;
11159 }
11160 }
11161
11162 ret = 0;
11163out:
11164 srcu_read_unlock(&kvm->irq_srcu, idx);
11165 return ret;
11166}
11167
Ashok Rajc45dcc72016-06-22 14:59:56 +080011168static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11169{
11170 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11171 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11172 FEATURE_CONTROL_LMCE;
11173 else
11174 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11175 ~FEATURE_CONTROL_LMCE;
11176}
11177
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030011178static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011179 .cpu_has_kvm_support = cpu_has_kvm_support,
11180 .disabled_by_bios = vmx_disabled_by_bios,
11181 .hardware_setup = hardware_setup,
11182 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011183 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011184 .hardware_enable = hardware_enable,
11185 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011186 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020011187 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011188
11189 .vcpu_create = vmx_create_vcpu,
11190 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011191 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011192
Avi Kivity04d2cc72007-09-10 18:10:54 +030011193 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011194 .vcpu_load = vmx_vcpu_load,
11195 .vcpu_put = vmx_vcpu_put,
11196
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011197 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011198 .get_msr = vmx_get_msr,
11199 .set_msr = vmx_set_msr,
11200 .get_segment_base = vmx_get_segment_base,
11201 .get_segment = vmx_get_segment,
11202 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011203 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011204 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011205 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011206 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011207 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011208 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011209 .set_cr3 = vmx_set_cr3,
11210 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011211 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011212 .get_idt = vmx_get_idt,
11213 .set_idt = vmx_set_idt,
11214 .get_gdt = vmx_get_gdt,
11215 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011216 .get_dr6 = vmx_get_dr6,
11217 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011218 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011219 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011220 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011221 .get_rflags = vmx_get_rflags,
11222 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011223
11224 .get_pkru = vmx_get_pkru,
11225
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020011226 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020011227 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011228
11229 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011230
Avi Kivity6aa8b732006-12-10 02:21:36 -080011231 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011232 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011233 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011234 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11235 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011236 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011237 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011238 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011239 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011240 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011241 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011242 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011243 .get_nmi_mask = vmx_get_nmi_mask,
11244 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011245 .enable_nmi_window = enable_nmi_window,
11246 .enable_irq_window = enable_irq_window,
11247 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011248 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011249 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011250 .get_enable_apicv = vmx_get_enable_apicv,
11251 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011252 .load_eoi_exitmap = vmx_load_eoi_exitmap,
11253 .hwapic_irr_update = vmx_hwapic_irr_update,
11254 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011255 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11256 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011257
Izik Eiduscbc94022007-10-25 00:29:55 +020011258 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011259 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011260 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011261
Avi Kivity586f9602010-11-18 13:09:54 +020011262 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011263
Sheng Yang17cc3932010-01-05 19:02:27 +080011264 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011265
11266 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011267
11268 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011269 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011270
11271 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011272
11273 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011274
Will Auldba904632012-11-29 12:42:50 -080011275 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011276 .write_tsc_offset = vmx_write_tsc_offset,
Haozhong Zhang58ea6762015-10-20 15:39:06 +080011277 .adjust_tsc_offset_guest = vmx_adjust_tsc_offset_guest,
Nadav Har'Eld5c17852011-08-02 15:54:20 +030011278 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011279
11280 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011281
11282 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011283 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011284 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011285 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011286
11287 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011288
11289 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011290
11291 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11292 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11293 .flush_log_dirty = vmx_flush_log_dirty,
11294 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011295
Feng Wubf9f6ac2015-09-18 22:29:55 +080011296 .pre_block = vmx_pre_block,
11297 .post_block = vmx_post_block,
11298
Wei Huang25462f72015-06-19 15:45:05 +020011299 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011300
11301 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011302
11303#ifdef CONFIG_X86_64
11304 .set_hv_timer = vmx_set_hv_timer,
11305 .cancel_hv_timer = vmx_cancel_hv_timer,
11306#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011307
11308 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011309};
11310
11311static int __init vmx_init(void)
11312{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011313 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11314 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011315 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011316 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011317
Dave Young2965faa2015-09-09 15:38:55 -070011318#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011319 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11320 crash_vmclear_local_loaded_vmcss);
11321#endif
11322
He, Qingfdef3ad2007-04-30 09:45:24 +030011323 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011324}
11325
11326static void __exit vmx_exit(void)
11327{
Dave Young2965faa2015-09-09 15:38:55 -070011328#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011329 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011330 synchronize_rcu();
11331#endif
11332
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011333 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011334}
11335
11336module_init(vmx_init)
11337module_exit(vmx_exit)